Esempio n. 1
0
        public void UninstallMod(ModFile mod)
        {
            Log.Information("Uninstalling Mod: " + mod.GetID());
            List <ulong> modFiles = new List <ulong>(this.Index.ModEntryMap[mod.GetID()]);
            Dictionary <string, WADFile> wadFiles = new Dictionary <string, WADFile>();

            this.Index.StartEdit();

            //In this loop we remove the installed WAD entries
            foreach (ulong modFile in modFiles)
            {
                List <string> modFileWadFiles = this.Index.Mod[modFile];

                //Initialize WAD files for entry deletion
                foreach (string modFileWadFile in modFileWadFiles)
                {
                    if (!wadFiles.ContainsKey(modFileWadFile))
                    {
                        wadFiles.Add(modFileWadFile, new WADFile(string.Format(@"{0}\{1}", OVERLAY_FOLDER, modFileWadFile)));
                    }

                    WADFile wad = wadFiles[modFileWadFile];
                    wad.RemoveEntry(modFile);
                }

                this.Index.RemoveModFile(modFile, mod.GetID());
            }

            //Now we need to either delete empty WAD files or fill the ones from which we removed the entries with original files
            //if the modified ones are the same as original then we need to delete those too
            foreach (KeyValuePair <string, WADFile> wadFile in wadFiles)
            {
                //If the WAD isn't being used by any other mod or is empty we can delete it
                if (!this.Index.WadModMap[wadFile.Key].Any(x => x != mod.GetID()) ||
                    wadFile.Value.Entries.Count == 0)
                {
                    wadFile.Value.Dispose();
                    File.Delete(string.Format(@"{0}\{1}", OVERLAY_FOLDER, wadFile.Key));
                }
                //If it's used by some other mods we need to merge it into the original WAD
                else
                {
                    string  gameWadPath    = string.Format(@"{0}\{1}", this.LeagueFolder, wadFile.Key);
                    string  overlayWadPath = string.Format(@"{0}\{1}", OVERLAY_FOLDER, wadFile.Key);
                    WADFile originalWad    = new WADFile(gameWadPath);

                    using (WADFile mergedWad = WADMerger.Merge(originalWad, wadFile.Value))
                    {
                        mergedWad.Write(overlayWadPath + ".temp");
                    }

                    wadFile.Value.Dispose();
                    File.Delete(overlayWadPath);
                    File.Move(overlayWadPath + ".temp", overlayWadPath);
                }
            }

            this.Database.ChangeModState(mod.GetID(), false);
            this.Index.RemoveMod(mod.GetID());
            this.Index.EndEdit();
        }
Esempio n. 2
0
        private void WriteModWADFiles(ModFile mod)
        {
            Action <KeyValuePair <string, WADFile> > writeWadFileDelegate = new Action <KeyValuePair <string, WADFile> >(WriteWadFile);

            if (Config.Get <bool>("ParallelWadInstallation"))
            {
                ParallelOptions parallelOptions = new ParallelOptions()
                {
                    MaxDegreeOfParallelism = Environment.ProcessorCount
                };

                Parallel.ForEach(mod.WadFiles, parallelOptions, (modWadFile) =>
                {
                    writeWadFileDelegate.Invoke(modWadFile);
                });
            }
            else
            {
                foreach (KeyValuePair <string, WADFile> modWadFile in mod.WadFiles)
                {
                    writeWadFileDelegate.Invoke(modWadFile);
                }
            }

            void WriteWadFile(KeyValuePair <string, WADFile> modWadFile)
            {
                string wadPath           = this.Index.FindWADPath(modWadFile.Key);
                string overlayModWadPath = string.Format(@"{0}\{1}", OVERLAY_FOLDER, wadPath);
                string gameModWadPath    = string.Format(@"{0}\{1}", this.LeagueFolder, wadPath);

                //Check if the WAD already exists, if it does, we need to merge the 2 WADs
                //if it doesnt, then we need to copy it from the game directory
                if (!File.Exists(overlayModWadPath))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(overlayModWadPath));

                    WADFile baseWad           = new WADFile(gameModWadPath);
                    bool    returnedModdedWad = false;
                    using (WADFile mergedWad = WADMerger.Merge(baseWad, modWadFile.Value, out returnedModdedWad))
                    {
                        mergedWad.Write(overlayModWadPath);
                    }

                    if (returnedModdedWad)
                    {
                        baseWad.Dispose();
                    }
                    else
                    {
                        modWadFile.Value.Dispose();
                    }
                }
                else
                {
                    File.Move(overlayModWadPath, overlayModWadPath + ".temp");

                    using (WADFile mergedWad = WADMerger.Merge(new WADFile(overlayModWadPath + ".temp"), modWadFile.Value))
                    {
                        mergedWad.Write(overlayModWadPath);
                    }

                    //Delete temp wad file
                    File.Delete(overlayModWadPath + ".temp");
                    modWadFile.Value.Dispose();
                }
            }
        }