public static List <SideloaderUpdateItem> ShowWindow(ModUpdateProgressDialog owner, IEnumerable <SideloaderUpdateItem> updateTasks) { try { using (var w = new ModUpdateSelectDialog()) { w.Icon = owner.Icon; w.StartPosition = FormStartPosition.CenterParent; w._updateTasks = updateTasks.OrderBy(x => x.UpToDate).ThenBy(x => x.RelativePath).ToList(); w.ShowDialog(); return(w._selectedItems); } } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Failed to get updates", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(null); }
private async void ModUpdateProgress_Shown(object sender, EventArgs e) { try { progressBar1.Style = ProgressBarStyle.Marquee; progressBar1.Value = 0; progressBar1.Maximum = 1; labelProgress.Text = "N/A"; labelPercent.Text = ""; SetStatus("Searching for mod updates..."); var updateTasks = await _megaUpdater.GetUpdateTasksAsync(_cancelToken.Token); _cancelToken.Token.ThrowIfCancellationRequested(); progressBar1.Style = ProgressBarStyle.Blocks; if (updateTasks.All(x => x.UpToDate)) { SetStatus("Everything is up to date!"); progressBar1.Value = progressBar1.Maximum; _cancelToken.Cancel(); return; } SetStatus($"Found {updateTasks.Count} updates, waiting for user confirmation."); updateTasks = ModUpdateSelectDialog.ShowWindow(this, updateTasks); if (updateTasks == null) { throw new OperationCanceledException(); } _overallSize = FileSize.SumFileSizes(updateTasks.Select(x => x.Size)); _completedSize = FileSize.Empty; progressBar1.Maximum = updateTasks.Count; for (var index = 0; index < updateTasks.Count; index++) { _cancelToken.Token.ThrowIfCancellationRequested(); var task = updateTasks[index]; labelProgress.Text = (index + 1) + " / " + updateTasks.Count; SetStatus("Downloading " + task.Name); await UpdateSingleItem(task); _completedSize += task.Size; progressBar1.Value = index + 1; } var s = $"Successfully updated/removed {updateTasks.Count} mods!"; SetStatus(s, true, true); MessageBox.Show(s, "Finished updating", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (OperationCanceledException) { SetStatus("Operation was cancelled by the user.", true, true); } catch (Exception ex) { var exceptions = ex is AggregateException aex?aex.Flatten().InnerExceptions : (ICollection <Exception>) new[] { ex }; SetStatus("Crash while updating mods, aborting.", true, true); SetStatus(string.Join("\n---\n", exceptions), false, true); MessageBox.Show("Something unexpected happened and the update could not be completed. Make sure that your internet connection is stable, " + "and that you did not hit your download limit on Mega, then try again.\n\nError message (check log for more):\n" + string.Join("\n", exceptions.Select(x => x.Message)), "Update failed", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { _cancelToken.Cancel(); labelPercent.Text = _completedSize == FileSize.Empty ? "" : $"Downloaded {_completedSize} out of {_overallSize}."; progressBar1.Style = ProgressBarStyle.Blocks; button1.Enabled = true; button1.Text = "OK"; } }