private async Task UpdateModAsyncInner(Mod oldMod, ModRelease newestRelease, string token, IProgress <double> progress, CancellationToken cancellationToken) { FileInfo modFile = await ModWebsite.UpdateReleaseAsync(newestRelease, GlobalCredentials.Instance.Username, token, progress, cancellationToken); Mod newMod; if (App.Instance.Settings.AlwaysUpdateZipped || (oldMod is ZippedMod)) { newMod = new ZippedMod(oldMod.Name, newestRelease.Version, newestRelease.FactorioVersion, modFile, InstalledMods, InstalledModpacks); } else { DirectoryInfo modDirectory = await Task.Run(() => { progress.Report(2); DirectoryInfo modsDirectory = App.Instance.Settings.GetModDirectory(newestRelease.FactorioVersion); ZipFile.ExtractToDirectory(modFile.FullName, modsDirectory.FullName); modFile.Delete(); return(new DirectoryInfo(Path.Combine(modsDirectory.FullName, modFile.NameWithoutExtension()))); }); newMod = new ExtractedMod(oldMod.Name, newestRelease.Version, newestRelease.FactorioVersion, modDirectory, InstalledMods, InstalledModpacks); } InstalledMods.Add(newMod); if (oldMod.Update(newMod)) { InstalledModpacks.ExchangeMods(oldMod, newMod); } ModpackTemplateList.Instance.Update(InstalledModpacks); ModpackTemplateList.Instance.Save(); }
private async Task UpdateSelectedModRelease() { string token; if (GlobalCredentials.Instance.LogIn(Window, out token)) { ModRelease newestRelease = GetNewestRelease(ExtendedInfo, SelectedRelease); Mod mod = InstalledMods.FindByFactorioVersion(SelectedMod.Name, newestRelease.FactorioVersion); var zippedMod = mod as ZippedMod; var extractedMod = mod as ExtractedMod; var cancellationSource = new CancellationTokenSource(); var progressWindow = new ProgressWindow { Owner = Window }; var progressViewModel = (ProgressViewModel)progressWindow.ViewModel; progressViewModel.ActionName = App.Instance.GetLocalizedResourceString("UpdatingAction"); progressViewModel.ProgressDescription = string.Format(App.Instance.GetLocalizedResourceString("DownloadingDescription"), newestRelease.FileName); progressViewModel.CanCancel = true; progressViewModel.CancelRequested += (sender, e) => cancellationSource.Cancel(); IProgress <double> progress = new Progress <double>(p => { if (p > 1) { progressViewModel.ProgressDescription = App.Instance.GetLocalizedResourceString("ExtractingDescription"); progressViewModel.IsIndeterminate = true; progressViewModel.CanCancel = false; } else { progressViewModel.Progress = p; } }); try { Task closeWindowTask = null; try { Task downloadTask = ModWebsite.UpdateReleaseAsync(newestRelease, GlobalCredentials.Instance.Username, token, progress, cancellationSource.Token); if (extractedMod != null) { downloadTask = downloadTask.ContinueWith(t => { progress.Report(2); FileInfo modFile = ((Task <FileInfo>)t).Result; DirectoryInfo modDirectory = App.Instance.Settings.GetModDirectory(newestRelease.FactorioVersion); ZipFile.ExtractToDirectory(modFile.FullName, modDirectory.FullName); modFile.Delete(); return(new DirectoryInfo(Path.Combine(modDirectory.FullName, modFile.NameWithoutExtension()))); }, TaskContinuationOptions.NotOnFaulted | TaskContinuationOptions.NotOnCanceled); } closeWindowTask = downloadTask.ContinueWith(t => progressWindow.Dispatcher.Invoke(progressWindow.Close)); progressWindow.ShowDialog(); if (zippedMod != null) { FileInfo newModFile = await(Task <FileInfo>) downloadTask; if (zippedMod.FactorioVersion == newestRelease.FactorioVersion) { zippedMod.Update(newModFile, newestRelease.Version); } else { var newMod = new ZippedMod(zippedMod.Name, newestRelease.Version, newestRelease.FactorioVersion, newModFile, InstalledMods, MainViewModel.Instance.Modpacks); InstalledMods.Add(newMod); foreach (var modpack in MainViewModel.Instance.Modpacks) { ModReference reference; if (modpack.Contains(zippedMod, out reference)) { modpack.Mods.Remove(reference); modpack.Mods.Add(new ModReference(newMod, modpack)); } } zippedMod.File.Delete(); InstalledMods.Remove(extractedMod); } } if (extractedMod != null) { DirectoryInfo newModDirectory = await(Task <DirectoryInfo>) downloadTask; if (extractedMod.FactorioVersion == newestRelease.FactorioVersion) { extractedMod.Update(newModDirectory, newestRelease.Version); } else { var newMod = new ExtractedMod(extractedMod.Name, newestRelease.Version, newestRelease.FactorioVersion, newModDirectory, InstalledMods, MainViewModel.Instance.Modpacks); InstalledMods.Add(newMod); foreach (var modpack in MainViewModel.Instance.Modpacks) { ModReference reference; if (modpack.Contains(extractedMod, out reference)) { modpack.Mods.Remove(reference); modpack.Mods.Add(new ModReference(newMod, modpack)); } } extractedMod.Directory.Delete(true); InstalledMods.Remove(extractedMod); ModpackTemplateList.Instance.Update(MainViewModel.Instance.Modpacks); ModpackTemplateList.Instance.Save(); } } } 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); return; } SelectedRelease = null; foreach (var release in SelectedReleases) { release.IsInstalled = InstalledMods.Contains(selectedMod.Name, release.Version); release.IsVersionInstalled = !release.IsInstalled && InstalledMods.ContainsByFactorioVersion(selectedMod.Name, release.FactorioVersion); } } }