private async Task ApplyUpdate(string token, UpdateTarget target, IProgress <double> progress, IProgress <bool> canCancel, IProgress <string> description, CancellationToken cancellationToken) { canCancel.Report(true); description.Report(App.Instance.GetLocalizedResourceString("UpdatingFactorioStage1Description")); var files = await FactorioUpdater.DownloadUpdatePackagesAsync(GlobalCredentials.Instance.Username, token, target, progress, cancellationToken); if (!cancellationToken.IsCancellationRequested) { progress.Report(0); canCancel.Report(false); description.Report(App.Instance.GetLocalizedResourceString("UpdatingFactorioStage2Description")); await SelectedVersion.UpdateAsync(files, progress); } }
private async Task UpdateSelectedVersion() { string token; if (GlobalCredentials.Instance.LogIn(Window, out token)) { UpdateInfo updateInfo = UpdateWebsite.GetUpdateInfo(GlobalCredentials.Instance.Username, token); List <UpdateStep> updateSteps = updateInfo.Package.Where(step => step.From >= SelectedVersion.Version).ToList(); if (updateSteps.Count > 0) { List <UpdateTarget> targets = FactorioUpdater.GetUpdateTargets(SelectedVersion, FactorioVersions, updateSteps); var updateListWindow = new UpdateListWindow() { Owner = Window }; var updateListViewModel = (UpdateListViewModel)updateListWindow.ViewModel; updateListViewModel.UpdateTargets = targets; bool?result = updateListWindow.ShowDialog(); if (result.HasValue && result.Value) { UpdateTarget target = updateListViewModel.SelectedTarget; var progressWindow = new ProgressWindow { Owner = Window }; var progressViewModel = (ProgressViewModel)progressWindow.ViewModel; progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("UpdatingFactorioAction"); var cancellationSource = new CancellationTokenSource(); progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel(); var progress = new Progress <double>(value => progressViewModel.Progress = value); var stageProgress = new Progress <UpdaterStageInfo>(value => { progressViewModel.CanCancel = value.CanCancel; progressViewModel.ProgressDescription = value.Description; }); try { Task closeWindowTask = null; try { Task updateTask = FactorioUpdater.ApplyUpdateAsync(SelectedVersion, GlobalCredentials.Instance.Username, token, target, progress, stageProgress, cancellationSource.Token); closeWindowTask = updateTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close)); progressWindow.ShowDialog(); await updateTask; } finally { if (closeWindowTask != null) { await closeWindowTask; } } } catch (HttpRequestException) { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("InternetConnection", MessageType.Error), App.Instance.GetLocalizedMessageTitle("InternetConnection", MessageType.Error), MessageBoxButton.OK, MessageBoxImage.Error); } catch (CriticalUpdaterException) { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("FactorioUpdaterCritical", MessageType.Error), App.Instance.GetLocalizedMessageTitle("FactorioUpdaterCritical", MessageType.Error), MessageBoxButton.OK, MessageBoxImage.Error); } } } else { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("NoFactorioUpdate", MessageType.Information), App.Instance.GetLocalizedMessageTitle("NoFactorioUpdate", MessageType.Information), MessageBoxButton.OK, MessageBoxImage.Information); } } }
private async Task UpdateSelectedVersion() { if ((SelectedVersion == null) || !SelectedVersion.CanUpdate) { return; } if (GlobalCredentials.Instance.LogIn(Window, out string token)) { try { var progressWindow = new ProgressWindow { Owner = Window }; var progressViewModel = (ProgressViewModel)progressWindow.ViewModel; progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("FetchingVersionsAction"); progressViewModel.IsIndeterminate = true; progressViewModel.CanCancel = false; List <UpdateStep> updateSteps = null; Task closeWindowTask = null; try { var getUpdateStepsTask = GetUpdateSteps(token); closeWindowTask = getUpdateStepsTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close)); progressWindow.ShowDialog(); updateSteps = await getUpdateStepsTask; } finally { if (closeWindowTask != null) { await closeWindowTask; } } if (updateSteps.Count > 0) { var targets = FactorioUpdater.GetUpdateTargets(SelectedVersion, updateSteps); if (ShowUpdateWindow(targets, out var target)) { await UpdateSelectedVersionInternal(token, target); } } else { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("NoFactorioUpdate", MessageType.Information), App.Instance.GetLocalizedMessageTitle("NoFactorioUpdate", MessageType.Information), MessageBoxButton.OK, MessageBoxImage.Information); } } catch (WebException) { MessageBox.Show(Window, App.Instance.GetLocalizedMessage("RetrievingVersions", MessageType.Error), App.Instance.GetLocalizedMessageTitle("RetrievingVersions", MessageType.Error), MessageBoxButton.OK, MessageBoxImage.Error); } } }