public async Task <ConfigData?> LoadDatabaseAsync(Control window, string languageName, bool forceReload = false)
        {
            using var progressDlg = new ProgressForm();
            try
            {
                if (!forceReload && IsDatabaseAlreadyLoaded(languageName))
                {
                    return(_configDataLoader.BuildData(languageName));
                }
                progressDlg.BindAdapter(new CheckForUpdateDialogAdapter());
                progressDlg.Show(window);
                await _configDataLoader.LoadDatabaseAsync(progressDlg.CancelToken, forceReload);

                progressDlg.CurrentTaskProgress = 0.5f;
                if (_configDataLoader.DatabaseLoaded)
                {
                    await LoadDatabaseLanguageAsync(languageName, progressDlg.CancelToken, forceReload);

                    progressDlg.CurrentTaskProgress = 0.9f;
                    return(_configDataLoader.BuildData(languageName));
                }
                else
                {
                    _logger.Error("Failed parse settings database");
                    MessageBox.Show(window, Resources.GameSettings_FailedParseDb_Text,
                                    Resources.Localization_Error_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception e)
            {
                if (!progressDlg.IsCanceledByUser)
                {
                    _logger.Error(e, "Error load settings database");
                    MessageBox.Show(window, Resources.GameSettings_FailedLoadDb_Text,
                                    Resources.Localization_Error_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            finally
            {
                progressDlg.Hide();
            }
            if (forceReload && _configDataLoader.DatabaseLoaded)
            {
                try
                {
                    return(_configDataLoader.BuildData(languageName));
                }
                catch (Exception e)
                {
                    _logger.Error(e, "Error build database data");
                }
            }
            return(null);
        }
Exemple #2
0
        public bool Uninstall(Control window)
        {
            if (CurrentInstallation.InstalledVersion != null)
            {
                if (!CurrentGame.IsAvailable())
                {
                    _logger.Error($"Uninstall localization mode path unavailable: {CurrentGame.RootFolderPath}");
                    MessageBox.Show(window, Resources.Localization_Uninstall_ErrorText,
                                    Resources.Localization_Uninstall_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
                var dialogResult = MessageBox.Show(window, Resources.Localization_Uninstall_QuestionText,
                                                   Resources.Localization_Uninstall_QuestionTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialogResult == DialogResult.No)
                {
                    return(false);
                }
                _logger.Info($"Uninstall localization: {CurrentGame.Mode}, {CurrentInstallation.Repository} {CurrentInstallation.InstalledVersion}");
                bool status = false;
                using var progressDlg = new ProgressForm();
                try
                {
                    progressDlg.BindAdapter(new UninstallProgressDialogAdapter());
                    progressDlg.Show(window);
                    using var gameMutex = new GameMutex();
                    if (!GameMutexController.AcquireWithRetryDialog(progressDlg, gameMutex))
                    {
                        _logger.Info($"Uninstall localization aborted by user because game running");
                        return(false);
                    }
                    switch (CurrentRepository.Installer.Uninstall(CurrentGame.RootFolderPath))
                    {
                    case UninstallStatus.Success:
                        GameSettings.RemoveCurrentLanguage();
                        GameSettings.Load();
                        gameMutex.Release();
                        progressDlg.CurrentTaskProgress = 1.0f;
                        RepositoryManager.RemoveInstalledRepository(CurrentRepository);
                        status = true;
                        break;

                    case UninstallStatus.Partial:
                        GameSettings.RemoveCurrentLanguage();
                        GameSettings.Load();
                        gameMutex.Release();
                        progressDlg.CurrentTaskProgress = 1.0f;
                        RepositoryManager.RemoveInstalledRepository(CurrentRepository);
                        status = true;
                        _logger.Warn($"Localization uninstalled partially: {CurrentGame.Mode}");
                        MessageBox.Show(progressDlg, Resources.Localization_Uninstall_WarningText,
                                        Resources.Localization_Uninstall_WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        break;

                    default:
                        gameMutex.Release();
                        _logger.Error($"Failed uninstall localization: {CurrentGame.Mode}");
                        MessageBox.Show(progressDlg, Resources.Localization_Uninstall_ErrorText,
                                        Resources.Localization_Uninstall_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        break;
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Error during uninstall localization: {CurrentGame.Mode}");
                    MessageBox.Show(window, Resources.Localization_Uninstall_ErrorText,
                                    Resources.Localization_Uninstall_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    progressDlg.Hide();
                }
                return(status);
            }
            return(true);
        }
Exemple #3
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);
        }