private void UpdateClassicMod(OnlineContent.ModUpdateInfo ui) { NamedBackgroundWorker bw = new NamedBackgroundWorker(@"ModUpdaterThread-" + ui.mod.ModName); bw.DoWork += (a, b) => { OperationInProgress = true; ui.UpdateInProgress = true; ui.Indeterminate = false; ui.DownloadButtonText = M3L.GetString(M3L.string_downloading); //void updateProgressCallback(long bytesReceived, long totalBytes) //{ // ui.By //} bool errorShown = false; void errorCallback(string message) { if (!errorShown) { errorShown = true; Application.Current.Dispatcher.Invoke(delegate { M3L.ShowDialog(window, M3L.GetString(M3L.string_interp_errorOccuredWhileUpdatingXErrorMessage, ui.mod.ModName, message), M3L.GetString(M3L.string_interp_errorUpdatingX, ui.mod.ModName), MessageBoxButton.OK, MessageBoxImage.Error); } ); } } var stagingDirectory = Directory.CreateDirectory(Path.Combine(Utilities.GetTempPath(), Path.GetFileName(ui.mod.ModPath))).FullName; var modUpdated = OnlineContent.UpdateMod(ui, stagingDirectory, errorCallback); ui.UpdateInProgress = false; ui.CanUpdate = !modUpdated; AnyModUpdated |= modUpdated; ui.DownloadButtonText = ui.CanUpdate ? M3L.GetString(M3L.string_downloadUpdate) : M3L.GetString(M3L.string_updated); Utilities.DeleteFilesAndFoldersRecursively(stagingDirectory); }; bw.RunWorkerCompleted += (a, b) => { OperationInProgress = false; CommandManager.InvalidateRequerySuggested(); }; bw.RunWorkerAsync(); }
private void ApplyUpdateToMod(object obj) { if (obj is OnlineContent.ModUpdateInfo ui) { NamedBackgroundWorker bw = new NamedBackgroundWorker("ModUpdaterThread-" + ui.mod.ModName); bw.DoWork += (a, b) => { ui.UpdateInProgress = true; ui.Indeterminate = false; ui.DownloadButtonText = "Downloading"; //void updateProgressCallback(long bytesReceived, long totalBytes) //{ // ui.By //} bool errorShown = false; void errorCallback(string message) { if (!errorShown) { errorShown = true; Application.Current.Dispatcher.Invoke(delegate { Xceed.Wpf.Toolkit.MessageBox.Show($"Error occured while updating {ui.mod.ModName}:\n{message}", $"Error updating {ui.mod.ModName}", MessageBoxButton.OK, MessageBoxImage.Error); } ); } } var stagingDirectory = Directory.CreateDirectory(Path.Combine(Utilities.GetTempPath(), Path.GetFileName(ui.mod.ModPath))).FullName; var modUpdated = OnlineContent.UpdateMod(ui, stagingDirectory, errorCallback); ui.UpdateInProgress = false; ui.CanUpdate = !modUpdated; AnyModUpdated |= modUpdated; ui.DownloadButtonText = ui.CanUpdate ? "Download update" : "Updated"; Utilities.DeleteFilesAndFoldersRecursively(stagingDirectory); }; bw.RunWorkerCompleted += (a, b) => { CommandManager.InvalidateRequerySuggested(); }; bw.RunWorkerAsync(); } }
private void UpdateClassicMod(OnlineContent.ModUpdateInfo ui) { NamedBackgroundWorker nbw = new NamedBackgroundWorker(@"ModUpdaterThread-" + ui.mod.ModName); nbw.WorkerReportsProgress = true; nbw.ProgressChanged += (a, b) => { if (b.UserState is double d) { mainwindow.TaskBarItemInfoHandler.ProgressValue = d; } }; nbw.DoWork += (a, b) => { OperationInProgress = true; ui.UpdateInProgress = true; ui.Indeterminate = false; ui.DownloadButtonText = M3L.GetString(M3L.string_downloading); ui.ProgressChanged += (a, b) => { if (b.totalToDl != 0 && nbw.IsBusy) //? IsBusy needs to be here for some reason or it crashes, like progress comes in late or something. { nbw.ReportProgress(0, b.currentDl * 1.0 / b.totalToDl); } }; bool errorShown = false; void errorCallback(string message) { if (!errorShown) { errorShown = true; Application.Current.Dispatcher.Invoke(delegate { M3L.ShowDialog(window, M3L.GetString(M3L.string_interp_errorOccuredWhileUpdatingXErrorMessage, ui.mod.ModName, message), M3L.GetString(M3L.string_interp_errorUpdatingX, ui.mod.ModName), MessageBoxButton.OK, MessageBoxImage.Error); } ); } } var stagingDirectory = Directory.CreateDirectory(Path.Combine(Utilities.GetTempPath(), Path.GetFileName(ui.mod.ModPath))).FullName; var modUpdated = OnlineContent.UpdateMod(ui, stagingDirectory, errorCallback); ui.UpdateInProgress = false; ui.CanUpdate = !modUpdated; AnyModUpdated |= modUpdated; ui.DownloadButtonText = ui.CanUpdate ? M3L.GetString(M3L.string_downloadUpdate) : M3L.GetString(M3L.string_updated); Utilities.DeleteFilesAndFoldersRecursively(stagingDirectory); }; nbw.RunWorkerCompleted += (a, b) => { if (b.Error != null) { Log.Error($@"Exception occured in {nbw.Name} thread: {b.Error.Message}"); } Analytics.TrackEvent(@"Updated mod", new Dictionary <string, string>() { { @"Type", @"Classic" }, { @"ModName", ui.mod.ModName }, { @"Result", ui.CanUpdate ? @"Success" : @"Failed" } }); mainwindow.TaskBarItemInfoHandler.ProgressState = TaskbarItemProgressState.None; OperationInProgress = false; CommandManager.InvalidateRequerySuggested(); }; mainwindow.TaskBarItemInfoHandler.ProgressValue = 0; mainwindow.TaskBarItemInfoHandler.ProgressState = TaskbarItemProgressState.Normal; nbw.RunWorkerAsync(); }