示例#1
0
        public async Task <bool> InstallVersionAsync(Control window, UpdateInfo selectedUpdateInfo)
        {
            if (!CurrentGame.IsAvailable())
            {
                _logger.Error($"Install localization mode path unavailable: {CurrentGame.RootFolderPath}");
                MessageBox.Show(window, Resources.Localization_File_ErrorText,
                                Resources.Localization_File_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (!Program.Settings.AcceptInstallWarning)
            {
                var dialogResult = MessageBox.Show(window, Resources.Localization_InstallWarning_Text,
                                                   Resources.Localization_InstallWarning_Title, MessageBoxButtons.YesNo,
                                                   MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if (dialogResult != DialogResult.Yes)
                {
                    return(false);
                }
                Program.Settings.AcceptInstallWarning = true;
                Program.SaveAppSettings();
            }
            _logger.Info($"Install localization: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
            bool          status          = false;
            DirectoryInfo?downloadDirInfo = null;

            using var progressDlg = new ProgressForm();
            try
            {
                window.Enabled = false;
                Cursor.Current = Cursors.WaitCursor;
                var downloadDialogAdapter = new DownloadProgressDialogAdapter(selectedUpdateInfo.GetVersion());
                progressDlg.BindAdapter(downloadDialogAdapter);
                progressDlg.Show(window);
                downloadDirInfo = Directory.CreateDirectory(Path.Combine(CurrentGame.RootFolderPath, "download_" + Path.GetRandomFileName()));
                var packageIndex   = new LocalizationPackageIndex(CurrentGame.RootFolderPath);
                var downloadResult = await CurrentRepository.DownloadAsync(selectedUpdateInfo, downloadDirInfo.FullName, packageIndex,
                                                                           progressDlg.CancelToken, downloadDialogAdapter);

                progressDlg.BindAdapter(new InstallProgressDialogAdapter());
                using var gameMutex = new GameMutex();
                if (!GameMutexController.AcquireWithRetryDialog(progressDlg, gameMutex))
                {
                    _logger.Info($"Install localization aborted by user because game running");
                    return(false);
                }
                var installStatus = downloadResult switch
                {
                    FullDownoadResult fullResult => CurrentRepository.Installer.Install(fullResult.ArchiveFilePath, CurrentGame.RootFolderPath),
                    IncrementalDownloadResult incrementalResult => CurrentRepository.Installer.Install(incrementalResult.DownloadPath, CurrentGame.RootFolderPath, incrementalResult.DiffList),
                    _ => throw new InvalidOperationException("Download result is empty")
                };
                switch (installStatus)
                {
                case InstallStatus.Success:
                    if (selectedUpdateInfo is GitHubUpdateInfo githubUpateInfo)
                    {
                        CurrentRepository.Installer.WriteTimestamp(githubUpateInfo.Released, CurrentGame.RootFolderPath);
                    }
                    GameSettings.Load();
                    gameMutex.Release();
                    progressDlg.CurrentTaskProgress = 1.0f;
                    RepositoryManager.SetInstalledRepository(CurrentRepository, selectedUpdateInfo.GetVersion());
                    status = true;
                    break;

                case InstallStatus.PackageError:
                    gameMutex.Release();
                    _logger.Error($"Failed install localization due to package error: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_Package_ErrorText,
                                    Resources.Localization_Package_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case InstallStatus.VerifyError:
                    gameMutex.Release();
                    _logger.Error($"Failed install localization due to core verify error: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_Verify_ErrorText,
                                    Resources.Localization_Verify_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case InstallStatus.FileError:
                    gameMutex.Release();
                    _logger.Error($"Failed install localization due to file error: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_File_ErrorText,
                                    Resources.Localization_File_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                default:
                    gameMutex.Release();
                    _logger.Error($"Failed install localization: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_Install_ErrorText,
                                    Resources.Localization_Install_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }
            catch (Exception e)
            {
                if (!progressDlg.IsCanceledByUser)
                {
                    _logger.Error(e, $"Error during install localization: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    if (e is HttpRequestException)
                    {
                        MessageBox.Show(window, Resources.Localization_Download_ErrorText + '\n' + e.Message,
                                        Resources.Localization_Download_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show(window, Resources.Localization_Download_ErrorText,
                                        Resources.Localization_Download_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                window.Enabled = true;
                progressDlg.Hide();
                if (downloadDirInfo != null && selectedUpdateInfo is GitHubUpdateInfo)
                {
                    if (downloadDirInfo.Exists && !FileUtils.DeleteDirectoryNoThrow(downloadDirInfo, true))
                    {
                        _logger.Warn($"Failed remove download directory: {downloadDirInfo.FullName}");
                    }
                }
            }
            return(status);
        }
        public async Task <bool> InstallVersionAsync(Control window, UpdateInfo selectedUpdateInfo)
        {
            if (!CurrentGame.IsAvailable())
            {
                _logger.Error($"Install localization mode path unavailable: {CurrentGame.RootFolderPath}");
                MessageBox.Show(window, Resources.Localization_File_ErrorText,
                                Resources.Localization_File_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }
            if (!Program.Settings.AcceptInstallWarning)
            {
                var dialogResult = MessageBox.Show(window, Resources.Localization_InstallWarning_Text,
                                                   Resources.Localization_InstallWarning_Title, MessageBoxButtons.YesNo,
                                                   MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if (dialogResult != DialogResult.Yes)
                {
                    return(false);
                }
                Program.Settings.AcceptInstallWarning = true;
                Program.SaveAppSettings();
            }
            _logger.Info($"Install localization: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
            bool status = false;

            using var progressDlg = new ProgressForm();
            try
            {
                window.Enabled = false;
                Cursor.Current = Cursors.WaitCursor;
                var downloadDialogAdapter = new DownloadProgressDialogAdapter(selectedUpdateInfo.GetVersion());
                progressDlg.BindAdapter(downloadDialogAdapter);
                progressDlg.Show(window);
                var filePath = await CurrentRepository.DownloadAsync(selectedUpdateInfo, Path.GetTempPath(),
                                                                     progressDlg.CancelToken, downloadDialogAdapter);

                progressDlg.BindAdapter(new InstallProgressDialogAdapter());
                var result = CurrentRepository.Installer.Install(filePath, CurrentGame.RootFolderPath);
                switch (result)
                {
                case InstallStatus.Success:
                    GameSettings.Load();
                    progressDlg.CurrentTaskProgress = 1.0f;
                    RepositoryManager.SetInstalledRepository(CurrentRepository, selectedUpdateInfo.GetVersion());
                    status = true;
                    break;

                case InstallStatus.PackageError:
                    _logger.Error($"Failed install localization due to package error: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_Package_ErrorText,
                                    Resources.Localization_Package_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case InstallStatus.VerifyError:
                    _logger.Error($"Failed install localization due to core verify error: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_Verify_ErrorText,
                                    Resources.Localization_Verify_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                case InstallStatus.FileError:
                    _logger.Error($"Failed install localization due to file error: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_File_ErrorText,
                                    Resources.Localization_File_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;

                default:
                    _logger.Error($"Failed install localization: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    MessageBox.Show(progressDlg, Resources.Localization_Install_ErrorText,
                                    Resources.Localization_Install_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                }
            }
            catch (Exception e)
            {
                if (!progressDlg.IsCanceledByUser)
                {
                    _logger.Error(e, $"Error during install localization: {CurrentGame.Mode}, {selectedUpdateInfo.Dump()}");
                    if (e is HttpRequestException)
                    {
                        MessageBox.Show(window, Resources.Localization_Download_ErrorText + '\n' + e.Message,
                                        Resources.Localization_Download_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        MessageBox.Show(window, Resources.Localization_Download_ErrorText,
                                        Resources.Localization_Download_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            finally
            {
                Cursor.Current = Cursors.Default;
                window.Enabled = true;
                progressDlg.Hide();
            }
            return(status);
        }