public static bool LoadModpacks() { Modpacks.EnsureModpackFolderExists(); Program.MasterForm.modListPanel.Controls.Clear(); string[] fileEntries = Directory.GetFiles(Config.Modpack_dir); foreach (string file in fileEntries) { string modpackName = Path.GetFileName(file).Replace(".zip", ""); ModpackCfg modpackConfig = Modpacks.GetModpackConfig(modpackName); if (modpackConfig != null) { ModListPanel_add(modpackName, modpackConfig.MCC_version); } } return(true); }
private static List <string> FilterNeededBackups(List <string> paths) { List <string> requiredBaks = new List <string>(); foreach (string enabledModpack in Config.GetEnabledModpacks()) { ModpackCfg modpackConfig = Modpacks.GetModpackConfig(enabledModpack); foreach (ModpackEntry entry in modpackConfig.entries) { foreach (string path in paths) { if (path == Utility.ExpandPath(entry.dest)) { requiredBaks.Add(path); } } } } return(requiredBaks); }
public static bool AddPatched(string modpackName) { Dictionary <string, string> modfiles = new Dictionary <string, string>(); ModpackCfg mCfg = Modpacks.GetModpackConfig(modpackName); using (ZipArchive archive = ZipFile.OpenRead(Config.Modpack_dir + @"\" + modpackName + ".zip")) { if (mCfg == null) { Utility.ShowMsg("Cannot set state to enabled. The file '" + modpackName + ".zip' is either not a compatible modpack or the config is corrupted.", "Error"); return(false); } List <string> patched = new List <string>(); // track patched files in case of failure mid patch foreach (ModpackEntry entry in mCfg.entries) { modfiles[entry.dest] = Modpacks.GetMD5(Utility.ExpandPath(entry.dest)); } } Patched[modpackName] = new patchedEntry(); Patched[modpackName].files = modfiles; return(true); }
private static string WillOverwriteOtherMod(string modpack) { ModpackCfg primaryConfig = Modpacks.GetModpackConfig(modpack); foreach (string enabledModpack in Config.GetEnabledModpacks()) { ModpackCfg modpackConfig = Modpacks.GetModpackConfig(enabledModpack); // Deliberately not checking for null so program throws a stack trace // This should never happen, but if it does I want the user to let me know about it foreach (ModpackEntry entry in modpackConfig.entries) { foreach (ModpackEntry primary in primaryConfig.entries) { if (entry.dest == primary.dest) { return(enabledModpack); } } } } return(null); }
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 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); } }