static public void CloseAll() { LoggingManager.SendMessage("ModManager - Closing current mod (if any is loaded)"); if (ModuleFile != null) { ModuleFile.Close(); ModuleFile = null; } if (ModAttribArchives != null) { foreach (SGAFile sga in ModAttribArchives) { sga.Close(); } ModAttribArchives.Clear(); } if (ModDataArchives != null) { foreach (SGAFile sga in ModDataArchives) { sga.Close(); } ModDataArchives.Clear(); } FileManager.ReleaseFileTrees(); if (ModUnloaded != null) { ModUnloaded(); } ModName = null; // normally it isn't a good idea to manually invoke the GarbageCollection but in this case we're releasing // gigabytes of resources; the overall performance greatly benefits from this LoggingManager.SendMessage("ModManager - Manual GarbageCollection in all generations in progess..."); GC.Collect(); }
// Todo: clean up this method! it's a mess! /// <exception cref="CopeDoW2Exception">The global section of the selected module file is invalid!</exception> static public void LoadModule(string path) { if (IsAnythingLoaded) { CloseAll(); } LoggingManager.SendMessage("ModManager - Loading module file " + path); GameDirectory = path.SubstringBeforeLast('\\', true); DateTime t1 = DateTime.Now; ModuleFile = new ModuleFile(path); // get basic mod information from module file try { ModAttribDirectory = path.SubstringBeforeLast('\\', true) + ((ModuleFile.ModuleSectionFileList)ModuleFile["attrib:common"]).GetFolderByIndex(0) + '\\'; ModDataDirectory = path.SubstringBeforeLast('\\', true) + ((ModuleFile.ModuleSectionFileList)ModuleFile["data:common"]).GetFolderByIndex(0) + '\\'; var globalSection = ModuleFile["global"] as ModuleFile.ModuleSectionKeyValue; if (globalSection != null) { ModName = globalSection["Name"]; s_sModFolder = globalSection["ModFolder"]; if (globalSection.KeyExists("UCSBaseIndex")) { UCSManager.NextIndex = uint.Parse(globalSection["UCSBaseIndex"]); } } else { throw new CopeDoW2Exception("The global section of the selected module file is invalid!"); } } catch (CopeDoW2Exception e) { LoggingManager.SendError("ModManager - Error loading module file: " + e.Message); LoggingManager.HandleException(e); UIHelper.ShowError("Error loading module file " + path.SubstringAfterLast('\\') + ": " + e.Message + ". See log file for more information."); ModuleFile.Close(); if (LoadingFailed != null) { LoadingFailed(); } return; } if (ModAttribDirectory == ModDataDirectory) { LoggingManager.SendError( "ModManager - The data:common directory and the attrib:common directory of the mod overlap! May cause serious trouble!"); UIHelper.ShowError( "The data:common directory and the attrib:common directory of the mod overlap! This tool does NOT support such module files." + "Please correct the module file before loading it, read the user guide for help."); ModuleFile.Close(); if (LoadingFailed != null) { LoadingFailed(); } return; } // load mod data if (ModDataArchives == null) { ModDataArchives = new List <SGAFile>(); } else { ModDataArchives.Clear(); } if (ModAttribArchives == null) { ModAttribArchives = new List <SGAFile>(); } else { ModAttribArchives.Clear(); } LoggingManager.SendMessage("ModManager - Loading mod resources"); List <SGAFile> currentArchives; foreach (ModuleFile.ModuleSection ms in ModuleFile) { if (ms is ModuleFile.ModuleSectionFileList && ms.SectionName == "attrib:common") { currentArchives = ModAttribArchives; } else if (ms is ModuleFile.ModuleSectionFileList && ms.SectionName == "data:common") { currentArchives = ModDataArchives; } else { continue; } var tmp = (ModuleFile.ModuleSectionFileList)ms; for (int i = 0; i < tmp.ArchiveCount; i++) { if (!File.Exists(ModuleFile.FilePath.SubstringBeforeLast('\\', true) + tmp.GetArchiveByIndex(i))) { continue; } try { currentArchives.Add( new SGAFile(ModuleFile.FilePath.SubstringBeforeLast('\\', true) + tmp.GetArchiveByIndex(i), FileAccess.Read, FileShare.Read)); } catch (Exception e) { LoggingManager.SendError("ModManager - Error loading SGA-file '" + tmp.GetArchiveByIndex(i) + "':" + e.Message); LoggingManager.HandleException(e); UIHelper.ShowError("Error loading SGA-files: " + e.Message + " See log file for more information."); ModuleFile.Close(); if (LoadingFailed != null) { LoadingFailed(); } return; } } } ModuleFile.Close(); DateTime t2 = DateTime.Now; LoggingManager.SendMessage("ModManager - Module file successfully loaded in " + (t2 - t1).TotalSeconds + " seconds"); FileManager.FillTrees(); TryToGetKeyProvider(); if (ModLoaded != null) { ModLoaded(); } }