public Task <bool> UninstallMod(InstalledMod mod, bool skipPreventRemoveCheck = false, IProgress <ProgressReport> progess = null) { return(Task.Run(() => { progess?.Report(new ProgressReport($"Preparing to uninstall { mod } ...", 0.1f)); if (!skipPreventRemoveCheck && mod.usedBy.Count > 0) { progess?.Report(new ProgressReport($"Could not uninstall mod: other mods are depending on it.", 1f)); return false; } if (!skipPreventRemoveCheck && mod.preventRemoval) { progess?.Report(new ProgressReport($"Could not uninstall mod: the removal of this mod was prevented.", 1f)); return false; } for (int f = 0; f < mod.affectedFiles.Count; f++) { string file = Path.Combine(BeatSaberDirectory, mod.affectedFiles[f]); progess?.Report(new ProgressReport($"Removing { mod.Name } ... { file }", (float)(f + 1) / mod.affectedFiles.Count * 0.9f)); if (File.Exists(file)) { File.Delete(file); } DirectoryExtensions.DeleteEmptyParentDirectories(Path.GetDirectoryName(file)); } foreach (string uses in mod.uses) { InstalledMod usesMod = GetInstalledMod(uses); usesMod?.usedBy.Remove(mod.Name); } progess?.Report(new ProgressReport($"Mod { mod.ToString() } was uninstalled successfully.", 1f)); InstalledMods.Remove(mod); SaveConfig(); return true; })); }
/// <summary> /// Uninstalls a game mod. /// </summary> public async Task Uninstall(Mod m) { string modPath = AppDomain.CurrentDomain.BaseDirectory + "\\mods\\" + m.Slug; string backupPath = AppDomain.CurrentDomain.BaseDirectory + "\\backups\\" + m.Slug; Console.WriteLine("Uninstall Task"); if (Directory.Exists(backupPath)) { foreach (var f in Directory.GetFiles(modPath)) { string fileName = Path.GetFileName(f); string from = modPath + "\\" + fileName; string destin = settings.PSO2Dir + "\\" + fileName; string backup = backupPath + "\\" + fileName; if (!File.Exists(backup)) { continue; } // TODO: if backup file does not exists or not vanilla file, download patch from official site var UninstallTask = new Helpers.FileCopy(backup, destin); if (!await Task.Run(() => UninstallTask.StartCopy())) { // If error, download patch from official site await Task.Run(() => Helpers.DownloadPatch(fileName, destin)); } } Directory.Delete(backupPath, true); } else { MessageBox.Show("The backup is missing or something... mod won't really uninstall, do a file check plz"); } if (m.ToolInfo == null) { m.ToolInfo = ""; } m.ToolInfo.Replace(Mod.ModBrokenMessage, ""); InstalledMods.Remove(m); AvailableMods.Add(m); UpdateSettings(); }
public static void RemoveMod(IInstalledMod mod) { SyncContext.Send(state => InstalledMods.Remove(mod), null); }
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); } } }