public static bool StabilizeGame() // used after update is detected to uninstall half clobbered mods { Dictionary <string, List <string> > restoreMap = GetFilesToRestore(); foreach (KeyValuePair <string, List <string> > modpack in restoreMap) { foreach (KeyValuePair <string, string> entry in Config.Patched[modpack.Key].files) { if (modpack.Value.Contains(entry.Key)) { Backups.RestoreBak(Utility.ExpandPath(entry.Key)); } else { Backups.DeleteBak(Utility.ExpandPath(entry.Key)); } } Config.RmPatched(modpack.Key); } Config.MCC_version = Config.GetCurrentBuild(); Config.SaveCfg(); Backups.SaveBackups(); return(true); }
public static void DoResetApp() { Patched = new Dictionary <string, patchedEntry>(); MCC_version = GetCurrentBuild(); SaveCfg(); MyMods.LoadModpacks(); if (!Backups.DeleteAll(true)) { Utility.ShowMsg("There was an issue deleting at least one backup. Please delete these in the Backups tab to avoid restoring an old " + "version of the file in the future.", "Error"); } Backups.LoadBackups(); }
public static void ShowFullPathCheckbox_Click(object sender, EventArgs e) { Config.fullBakPath = Program.MasterForm.fullBakPath_chb.Checked; foreach (Panel p in Program.MasterForm.bakListPanel.Controls.OfType <Panel>()) { CheckBox chb = (CheckBox)p.GetChildAtPoint(Config.BackupsChbPoint); if (Config.fullBakPath) // swapping from filename to full path { chb.Text = Config.dirtyPadding + Backups.GetBakKey(chb.Text.Replace(Config.dirtyPadding, "")); p.Width = chb.Width + 40; } else // swapping from full path to filename { chb.Text = Config.dirtyPadding + Backups._baks[chb.Text.Replace(Config.dirtyPadding, "")]; p.Width = Config.backupPanelWidth; } } }
public static int UnpatchModpacks(List <CheckBox> toUnpatch) { bool packErr = false; Program.MasterForm.PBar_show(toUnpatch.Count); foreach (CheckBox chb in toUnpatch) { Program.MasterForm.PBar_update(); string modpackname = chb.Text.Replace(Config.dirtyPadding, ""); int ret = UnpatchModpack(modpackname); if (ret == 2) { packErr = true; chb.Checked = true; } else if (ret == 3) { setModpackStatePartial((Panel)chb.Parent); } else // modpack was unpatched { Config.RmPatched(modpackname); ((PictureBox)chb.Parent.GetChildAtPoint(Config.MyModsEnabledPoint)).Image = Properties.Resources.redDot_15px; } } if (Config.DeleteOldBaks) // update backup pane because backups will have been deleted { Backups.SaveBackups(); Backups.UpdateBackupList(); } Program.MasterForm.PBar_hide(); if (packErr) // fail / partial success - At least one modpack was not patched { return(2); } else // success, no errors { return(0); } }
public static void DelAllBaksBtn_Click(object sender, EventArgs e) { Backups.DeleteAll(false); }
public static int UnpatchModpack(string modpackname) { try { ModpackCfg modpackConfig = Modpacks.GetModpackConfig(modpackname); using (ZipArchive archive = ZipFile.OpenRead(Config.Modpack_dir + @"\" + modpackname + ".zip")) { if (modpackConfig == null) { Utility.ShowMsg("Could not unpatch '" + modpackname + "' because the modpack's configuration is corrupted or missing." + "\r\nPlease restore from backups using the Backups tab or verify integrity of game files on Steam.", "Error"); return(2); } List <ModpackEntry> restored = new List <ModpackEntry>(); // track restored files in case of failure mid unpatch foreach (ModpackEntry entry in modpackConfig.entries) { if (String.IsNullOrEmpty(entry.type) || entry.type == "replace" || entry.type == "patch") // assume replace type entry if null { if (!Backups.RestoreBak(Utility.ExpandPath(entry.dest))) { // repatch restored mod files bool err = false; foreach (ModpackEntry e in restored) { int r = PatchFile(archive, e); if (r == 2 || r == 3) { err = true; } } if (err) { Utility.ShowMsg("Critical error encountered while unpatching '" + modpackname + "'." + "\r\nYou may need to verify your game files on steam or reinstall.", "Error"); } return(3); } } else if (entry.type == "create") { if (!Utility.DeleteFile(Utility.ExpandPath(entry.dest))) { Utility.ShowMsg("Could not delete the file '" + Utility.ExpandPath(entry.dest) + "'. This may affect your game. " + "if you encounter issues please delete this file manually.", "Warning"); } } else { Utility.ShowMsg("Unknown modfile type in modpack config.\r\nCould not install the '" + modpackname + "' modpack.", "Error"); } restored.Add(entry); } } } catch (FileNotFoundException) { Utility.ShowMsg("Could not unpatch '" + modpackname + "'. Could not find the modpack file to read the config from.", "Error"); return(2); // Unpatch failed due to error } catch (InvalidDataException) { Utility.ShowMsg("Could not unpatch '" + modpackname + "'. The modpack file appears corrupted and the config cannot be read.", "Error"); return(2); // Unpatch failed due to error } return(0); }
private static int PatchFile(ZipArchive archive, ModpackEntry entry) { string destination = Utility.ExpandPath(entry.dest); bool baksMade = false; ZipArchiveEntry modFile = archive.GetEntry(entry.src); if (modFile == null) { return(3); } if (String.IsNullOrEmpty(entry.type) || entry.type == "replace") // assume replace type entry { if (File.Exists(destination)) { if (Backups.CreateBackup(destination, false) == 0) { baksMade = true; } if (!Utility.DeleteFile(destination)) { return(2); } } try { modFile.ExtractToFile(destination); } catch (IOException) { return(2); } if (baksMade) { return(1); } else { return(0); } } else if (entry.type == "patch") { string unmoddedPath = Utility.ExpandPath(entry.orig); bool fileIsPatchedOver = (unmoddedPath == destination); if (File.Exists(destination) || fileIsPatchedOver) { if (Backups.CreateBackup(destination, false) == 0) { baksMade = true; } } if (File.Exists(destination)) { if (!Utility.DeleteFile(destination)) { return(2); } } // use the backup that was created if the original file and the destination are the same, because the destination has been deleted if (fileIsPatchedOver) { unmoddedPath = Config.Backup_dir + @"\" + Backups._baks[unmoddedPath]; } if (!AssemblyPatching.ApplyPatch(modFile, Path.GetFileName(entry.src), unmoddedPath, destination)) { return(5); // no extra error message } if (baksMade) { return(1); } else { return(0); } } else if (entry.type == "create") { if (File.Exists(destination)) { if (!Utility.DeleteFile(destination)) { return(2); } } try { modFile.ExtractToFile(destination); } catch (IOException) { return(2); } return(0); } else { return(4); } }
private static int PatchModpack(string modpackname) { string retStr = WillOverwriteOtherMod(modpackname); if (!String.IsNullOrEmpty(retStr)) { Utility.ShowMsg("Installing '" + modpackname + "' would overwrite files for '" + retStr + "'. Modpack will be skipped.", "Error"); return(2); } bool baksMade = false; try { ModpackCfg modpackConfig = Modpacks.GetModpackConfig(modpackname); using (ZipArchive archive = ZipFile.OpenRead(Config.Modpack_dir + @"\" + modpackname + ".zip")) { if (modpackConfig == null) { Utility.ShowMsg("The file '" + modpackname + ".zip' is either not a compatible modpack or the config is corrupted." + "\r\nTry using the 'Create Modpack' Tab to convert this mod into a compatible modpack.", "Error"); return(2); } List <string> patched = new List <string>(); // track patched files in case of failure mid patch foreach (ModpackEntry entry in modpackConfig.entries) { int r = PatchFile(archive, entry); if (r != 0 && r != 1) { string errMsg = ""; if (r == 2) { errMsg = "File Access Exception.\n" + "If you're using the Microsoft Store version of the game, please run this tool as an administrator.\n" + "If the game is running, exit it and try again.\r\n"; } else if (r == 3) { errMsg = "This modpack appears to be missing files.\r\n"; } else if (r == 4) { errMsg = "Unknown modfile type in modpack config.\r\n"; } Utility.ShowMsg(errMsg + "Could not install the '" + modpackname + "' modpack.", "Error"); if (Backups.RestoreBaks(patched) != 0) { Utility.ShowMsg("At least one file restore failed. Your game is likely in an unstable state.", "Warning"); return(3); } return(2); } else if (r == 1) { baksMade = true; } patched.Add(Utility.ExpandPath(entry.dest)); } } } catch (FileNotFoundException) { Utility.ShowMsg("Could not find the '" + modpackname + "' modpack.", "Error"); return(2); } catch (InvalidDataException) { Utility.ShowMsg("The modpack '" + modpackname + "' appears corrupted." + "\r\nThis modpack cannot be installed.", "Error"); return(2); } if (baksMade) { return(1); } else { return(0); } }