/// <summary> /// Initializes an OMod in OMod-ready archive format. /// </summary> /// <param name="p_booUseCache">Whether to use the mod cache.</param> /// <param name="p_mcmModCacheManager">The manager for the current game mode's mod cache.</param> /// <param name="p_stgScriptTypeRegistry">The registry of supported script types.</param> private void InitializeUnpackedOmod(bool p_booUseCache, IModCacheManager p_mcmModCacheManager, IScriptTypeRegistry p_stgScriptTypeRegistry) { if (p_booUseCache) { m_arcCacheFile = p_mcmModCacheManager.GetCacheFile(this); //check to make sure the cache isn't bad if ((m_arcCacheFile != null) && (!m_arcCacheFile.ContainsFile(GetRealPath(Path.Combine(CONVERSION_FOLDER, "config"))) || !ValidateConfig(GetSpecialFile("config")))) { //bad cache - clear it m_arcCacheFile.Dispose(); m_arcCacheFile = null; } } //check for script m_booHasInstallScript = false; foreach (IScriptType stpScript in p_stgScriptTypeRegistry.Types) { foreach (string strScriptName in stpScript.FileNames) { if (ContainsFile(Path.Combine(CONVERSION_FOLDER, strScriptName))) { StreamReader sreScript = null; string strCode = String.Empty; if (File.Exists(Path.Combine(CONVERSION_FOLDER, strScriptName))) { sreScript = new StreamReader(Path.Combine(CONVERSION_FOLDER, strScriptName)); strCode = sreScript.ReadToEnd(); sreScript.Close(); } else strCode = TextUtil.ByteToString(GetFile(Path.Combine(CONVERSION_FOLDER, strScriptName))).Trim('\0'); if (!String.IsNullOrEmpty(strCode)) { if (stpScript.ValidateScript(stpScript.LoadScript(strCode))) { m_booHasInstallScript = true; m_stpInstallScriptType = stpScript; break; } } } } if (m_booHasInstallScript) break; } //check for readme m_booHasReadme = ContainsFile(Path.Combine(CONVERSION_FOLDER, "readme")); //check for screenshot m_booHasScreenshot = ContainsFile(Path.Combine(CONVERSION_FOLDER, "screenshot")); if (p_booUseCache && (m_arcCacheFile == null)) { string strTmpInfo = p_mcmModCacheManager.FileUtility.CreateTempDirectory(); try { FileUtil.WriteAllBytes(Path.Combine(strTmpInfo, GetRealPath(Path.Combine(CONVERSION_FOLDER, "config"))), GetSpecialFile("config")); if (m_booHasReadme) FileUtil.WriteAllBytes(Path.Combine(strTmpInfo, GetRealPath(Path.Combine(CONVERSION_FOLDER, "readme"))), GetSpecialFile("readme")); if (m_booHasScreenshot) FileUtil.WriteAllBytes(Path.Combine(strTmpInfo, GetRealPath(Path.Combine(CONVERSION_FOLDER, ScreenshotPath))), GetSpecialFile(ScreenshotPath)); m_arcCacheFile = p_mcmModCacheManager.CreateCacheFile(this, strTmpInfo); } finally { FileUtil.ForceDelete(strTmpInfo); } } LoadInfo(GetSpecialFile("config")); }
/// <summary> /// A simple constructor that initializes the FOMod from the specified file. /// </summary> /// <param name="p_strFilePath">The mod file from which to create the FOMod.</param> /// <param name="p_mftModFormat">The format of the mod.</param> /// <param name="p_strStopFolders">A list of folders names that indicate the root of the mod file structure.</param> /// <param name="p_strPluginsDirectoryName">The name of the folder that contains plugins.</param> /// <param name="p_mcmModCacheManager">The manager for the current game mode's mod cache.</param> /// <param name="p_stgScriptTypeRegistry">The registry of supported script types.</param> public FOMod(string p_strFilePath, FOModFormat p_mftModFormat, IEnumerable<string> p_enmStopFolders, string p_strPluginsDirectoryName, IEnumerable<string> p_enmPluginExtensions, IModCacheManager p_mcmModCacheManager, IScriptTypeRegistry p_stgScriptTypeRegistry, bool p_booUsePlugins) { StopFolders = new List<string>(p_enmStopFolders); if (!StopFolders.Contains("fomod", StringComparer.OrdinalIgnoreCase)) StopFolders.Add("fomod"); PluginsDirectoryName = p_strPluginsDirectoryName; PluginExtensions = new List<string>(p_enmPluginExtensions); Format = p_mftModFormat; ScriptTypeRegistry = p_stgScriptTypeRegistry; bool p_booUseCache = true; m_booUsesPlugins = p_booUsePlugins; bool booCheckNested = true; bool booCheckPrefix = true; bool booCheckScript = true; bool booUpdateCacheInfo = false; bool booDirtyCache = false; string strCheckPrefix = null; string strCheckScriptPath = null; string strCheckScriptType = null; m_strFilePath = p_strFilePath; m_arcFile = new Archive(p_strFilePath); #region Check for cacheInfo.txt file m_arcCacheFile = p_mcmModCacheManager.GetCacheFile(m_strFilePath); if (m_arcCacheFile != null) { if (m_arcCacheFile.ContainsFile("cacheInfo.txt")) { byte[] bCacheInfo = m_arcCacheFile.GetFileContents("cacheInfo.txt"); string sCacheInfo = Encoding.UTF8.GetString(bCacheInfo, 0, bCacheInfo.Length); string[] strPref = sCacheInfo.Split(new string[] { "@@" }, StringSplitOptions.RemoveEmptyEntries); if (strPref.Length > 0) { booCheckNested = Convert.ToBoolean(strPref[0]); if (strPref.Length > 1) { strCheckPrefix = strPref[1]; foreach (string Folder in IgnoreFolders) { if (strCheckPrefix.IndexOf(Folder, StringComparison.InvariantCultureIgnoreCase) >= 0) { booCheckNested = true; strCheckPrefix = String.Empty; booDirtyCache = true; break; } } if (!booDirtyCache) { if (strCheckPrefix.Equals("-")) strCheckPrefix = String.Empty; booCheckPrefix = false; if (strPref.Length > 2) { strCheckScriptPath = strPref[2]; if (strCheckScriptPath.Equals("-")) strCheckScriptPath = String.Empty; strCheckScriptType = strPref[3]; if (strCheckScriptType.Equals("-")) strCheckScriptType = String.Empty; booCheckScript = false; } } } } } } #endregion if (booCheckNested) { #region Temporary fix for nested .dazip files string[] strNested = m_arcFile.GetFiles("", "*.dazip", true); if (strNested.Length == 1) { string strFilePath = Path.Combine(Path.Combine(Path.GetTempPath(), "NMM"), strNested[0]); FileUtil.WriteAllBytes(strFilePath, GetFile(strNested[0])); if (File.Exists(strFilePath)) { m_arcFile = new Archive(strFilePath); m_strNestedFilePath = strFilePath; } } #endregion } m_arcFile.ReadOnlyInitProgressUpdated += new CancelProgressEventHandler(ArchiveFile_ReadOnlyInitProgressUpdated); if (booCheckPrefix) { FindPathPrefix(); booUpdateCacheInfo = true; } else { m_strPrefixPath = String.IsNullOrEmpty(strCheckPrefix) ? String.Empty : strCheckPrefix; } //check for script if (booCheckScript) { foreach (IScriptType stpScript in p_stgScriptTypeRegistry.Types) { foreach (string strScriptName in stpScript.FileNames) { string strScriptPath = Path.Combine("fomod", strScriptName); if (ContainsFile(strScriptPath)) { m_strInstallScriptPath = strScriptPath; m_stpInstallScriptType = stpScript; break; } } if (!String.IsNullOrEmpty(m_strInstallScriptPath)) break; } booUpdateCacheInfo = true; } else { m_strInstallScriptPath = strCheckScriptPath; m_stpInstallScriptType = String.IsNullOrEmpty(strCheckScriptType) ? null : p_stgScriptTypeRegistry.Types.FirstOrDefault(x => x.TypeName.Equals(strCheckScriptType)); } if (p_booUseCache) { m_arcCacheFile = p_mcmModCacheManager.GetCacheFile(m_strFilePath); //check to make sure the cache isn't bad if ((m_arcCacheFile != null) && !m_arcCacheFile.ContainsFile(GetRealPath("fomod/info.xml"))) { //bad cache - clear it m_arcCacheFile.Dispose(); m_arcCacheFile = null; } } m_arcFile.FilesChanged += new EventHandler(Archive_FilesChanged); //check for readme string strBaseName = Path.GetFileNameWithoutExtension(p_strFilePath); for (int i = 0; i < Readme.ValidExtensions.Length; i++) if (ContainsFile("readme - " + strBaseName + Readme.ValidExtensions[i])) { m_strReadmePath = "Readme - " + strBaseName + Readme.ValidExtensions[i]; break; } if (String.IsNullOrEmpty(m_strReadmePath)) for (int i = 0; i < Readme.ValidExtensions.Length; i++) if (ContainsFile("docs/readme - " + strBaseName + Readme.ValidExtensions[i])) { m_strReadmePath = "docs/Readme - " + strBaseName + Readme.ValidExtensions[i]; break; } //check for screenshot string[] strScreenshots; if (p_booUseCache && (m_arcCacheFile != null)) strScreenshots = m_arcCacheFile.GetFiles(GetRealPath("fomod"), "screenshot*", false); else strScreenshots = m_arcFile.GetFiles(GetRealPath("fomod"), "screenshot*", false); //TODO make sure the file is a valid image if (strScreenshots.Length > 0) m_strScreenshotPath = strScreenshots[0]; if (p_booUseCache && (m_arcCacheFile == null)) { string strTmpInfo = p_mcmModCacheManager.FileUtility.CreateTempDirectory(); try { Directory.CreateDirectory(Path.Combine(strTmpInfo, GetRealPath("fomod"))); if (ContainsFile("fomod/info.xml")) FileUtil.WriteAllBytes(Path.Combine(strTmpInfo, GetRealPath("fomod/info.xml")), GetFile("fomod/info.xml")); else FileUtil.WriteAllText(Path.Combine(strTmpInfo, GetRealPath("fomod/info.xml")), "<fomod/>"); if (!String.IsNullOrEmpty(m_strReadmePath)) FileUtil.WriteAllBytes(Path.Combine(strTmpInfo, GetRealPath(m_strReadmePath)), GetFile(m_strReadmePath)); if (!String.IsNullOrEmpty(m_strScreenshotPath)) FileUtil.WriteAllBytes(Path.Combine(strTmpInfo, GetRealPath(m_strScreenshotPath)), GetFile(m_strScreenshotPath)); m_arcCacheFile = p_mcmModCacheManager.CreateCacheFile(this, strTmpInfo); } finally { FileUtil.ForceDelete(strTmpInfo); } } if (booUpdateCacheInfo || (!m_arcCacheFile.ContainsFile("cacheInfo.txt"))) { Byte[] bteText = new UTF8Encoding(true).GetBytes(String.Format("{0}@@{1}@@{2}@@{3}", (!String.IsNullOrEmpty(m_strNestedFilePath)).ToString(), String.IsNullOrEmpty(m_strPrefixPath) ? "-" : m_strPrefixPath, String.IsNullOrEmpty(m_strInstallScriptPath) ? "-" : m_strInstallScriptPath, (m_stpInstallScriptType == null) ? "-" : m_stpInstallScriptType.TypeName)); if (bteText != null) m_arcCacheFile.ReplaceFile("cacheInfo.txt", bteText); } ModName = Path.GetFileNameWithoutExtension(m_strFilePath); LoadInfo(); }