Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        public static void CreateModpackBtn_Click(object sender, EventArgs e)
        {
            string modpackName = Program.MasterForm.modpackName_txt.Text;
            IEnumerable <Panel> modFilesList = Program.MasterForm.createFilesPanel.Controls.OfType <Panel>();

            if (modFilesList.Count() == 0)
            {
                Utility.ShowMsg("Please add at least one modded file entry", "Error");
                return;
            }
            if (String.IsNullOrEmpty(modpackName))
            {
                Utility.ShowMsg("Please enter a modpack name", "Error");
                return;
            }

            List <String> chk  = new List <string>();
            ModpackCfg    mCfg = new ModpackCfg {
                MCC_version = Utility.ReadFirstLine(Config.MCC_home + @"\build_tag.txt")
            };

            foreach (Panel row in modFilesList)
            {
                string  srcText     = row.GetChildAtPoint(Config.sourceTextBoxPoint).Text;
                TextBox origTextbox = new TextBox();    // setting this to an empty value to avoid compile error on an impossible CS0165
                string  destText;
                if ((string)row.Tag == "normal")
                {
                    destText = row.GetChildAtPoint(Config.destTextBoxPoint).Text;
                }
                else
                {
                    origTextbox = (TextBox)row.GetChildAtPoint(Config.origTextBoxPoint);
                    destText    = row.GetChildAtPoint(Config.destTextBoxPointAlt).Text;
                }

                if (string.IsNullOrEmpty(srcText) || string.IsNullOrEmpty(destText) || ((string)row.Tag == "alt" && string.IsNullOrEmpty(origTextbox.Text)))
                {
                    Utility.ShowMsg("Filepaths cannot be empty.", "Error");
                    return;
                }
                if (!File.Exists(srcText))
                {
                    Utility.ShowMsg("The source file '" + srcText + "' does not exist.", "Error");
                    return;
                }
                if (!destText.StartsWith(Config.MCC_home))
                {
                    Utility.ShowMsg("Destination files must be located within the MCC install directory. " +
                                    "You may need to configure this directory if you haven't done so already.", "Error");
                    return;
                }
                if ((string)row.Tag == "alt" && origTextbox.Enabled && !origTextbox.Text.StartsWith(Config.MCC_home))
                {
                    Utility.ShowMsg("Unmodified map files must be selected at their default install location within the MCC install directory to allow the patch " +
                                    "to be correctly applied when this modpack is installed. The file you selected does not appear to lie inside the MCC install directory." +
                                    "\r\nYou may need to configure this directory if you haven't done so already.", "Error");
                    return;
                }
                string patchType;
                if (Path.GetExtension(srcText) == ".asmp")
                {
                    patchType = "patch";
                }
                else
                {
                    bool isOriginalFile;
                    try {
                        isOriginalFile = Utility.IsHaloFile(Utility.CompressPath(destText));
                    } catch (JsonReaderException) {
                        Utility.ShowMsg(@"MCC Mod Manager could not parse Formats\filetree.json", "Error");
                        return;
                    }

                    if (isOriginalFile)
                    {
                        patchType = "replace";
                    }
                    else
                    {
                        patchType = "create";
                    }
                }

                mCfg.entries.Add(new ModpackEntry {
                    src  = srcText,
                    orig = (patchType == "patch") ? Utility.CompressPath(origTextbox.Text) : null,
                    dest = Utility.CompressPath(destText),  // make modpack compatable with any MCC_home directory
                    type = patchType
                });
                chk.Add(destText);
            }

            if (chk.Distinct().Count() != chk.Count)
            {
                Utility.ShowMsg("You have multiple files trying to write to the same destination.", "Error");
                return;
            }

            EnsureModpackFolderExists();
            String modpackFilename = modpackName + ".zip";
            String zipPath         = Config.Modpack_dir + @"\" + modpackFilename;

            if (File.Exists(zipPath))
            {
                Utility.ShowMsg("A modpack with that name already exists.", "Error");
                return;
            }

            Program.MasterForm.PBar_show(mCfg.entries.Count);
            try {
                using (var archive = ZipFile.Open(zipPath, ZipArchiveMode.Create)) {
                    foreach (var entry in mCfg.entries)
                    {
                        Program.MasterForm.PBar_update();
                        String fileName = Path.GetFileName(entry.src);
                        archive.CreateEntryFromFile(entry.src, fileName); // TODO: Fix issues when two source files have same name but diff path
                        entry.src = fileName;                             // change src path to just modpack after archive creation but before json serialization
                    }
                    ZipArchiveEntry configFile = archive.CreateEntry("modpack_config.cfg");
                    string          json       = JsonConvert.SerializeObject(mCfg, Formatting.Indented);
                    using (StreamWriter writer = new StreamWriter(configFile.Open())) {
                        writer.WriteLine(json);
                    }
                    ZipArchiveEntry readmeFile = archive.CreateEntry("README.txt");
                    using (StreamWriter writer = new StreamWriter(readmeFile.Open())) {
                        if (String.IsNullOrEmpty(Program.MasterForm.readmeTxt.Text))
                        {
                            writer.WriteLine(Config._defaultReadmeText);
                        }
                        else
                        {
                            writer.WriteLine(Program.MasterForm.readmeTxt.Text);
                        }
                    }
                }
            } catch (NotSupportedException) {
                Utility.ShowMsg("The modpack name you have provided is not a valid filename on Windows.", "Error");
                return;
            }

            Utility.ShowMsg("Modpack '" + modpackFilename + "' created.", "Info");
            Program.MasterForm.PBar_hide();
            ResetCreateModpacksTab();
            MyMods.LoadModpacks();
            return;
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
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);
            }
        }