/// <summary> /// Determines if the given archive is a valid PFP. /// </summary> /// <param name="p_arcPFP">The archive to validate as a PFP.</param> /// <returns> /// An error string describing why the specified file is not a valid PFP, or /// <lang langref="null" /> if the given archive is a valid PFP. /// </returns> protected static string ValidatePFP(Archive p_arcPFP) { if (!p_arcPFP.ContainsFile("metadata.xml")) { return "Missing metadata.xml file."; } var xmlMeta = new XmlDocument(); using (var msmMeta = new MemoryStream(p_arcPFP.GetFileContents("metadata.xml"))) { xmlMeta.Load(msmMeta); msmMeta.Close(); } var xndSources = xmlMeta.SelectSingleNode("premadeFomodPack/sources"); foreach (XmlNode xndSource in xndSources.ChildNodes) { if ((xndSource.Attributes["name"] == null) || String.IsNullOrEmpty(xndSource.Attributes["name"].Value) || (xndSource.Attributes["url"] == null) || String.IsNullOrEmpty(xndSource.Attributes["url"].Value)) { return "Invalid metadata.xml file."; } } return null; }
/// <summary> /// A simple constructor that initializes the object with the given values. /// </summary> /// <param name="p_strPFPPath">The path to the PFP file.</param> public PremadeFomodPack(string p_strPFPPath) { m_arcPFP = new Archive(p_strPFPPath); string strError = ValidatePFP(m_arcPFP); if (!String.IsNullOrEmpty(strError)) throw new ArgumentException("Specified Premade FOMod Pack is not valid: " + strError, "p_strPFPPath"); m_xmlMeta = new XmlDocument(); using (MemoryStream msmMeta = new MemoryStream(m_arcPFP.GetFileContents("metadata.xml"))) { m_xmlMeta.Load(msmMeta); msmMeta.Close(); } foreach (string strDirectory in m_arcPFP.GetDirectories("/")) if (strDirectory.StartsWith("Premade", StringComparison.InvariantCultureIgnoreCase)) { m_strPremadePath = strDirectory; break; } }
/// <summary> /// A simple constructor that initializes the object. /// </summary> /// <param name="path">The path to the fomod file.</param> internal fomod(string path, bool p_booUseCache) { this.filepath = path; ModName = System.IO.Path.GetFileNameWithoutExtension(path); m_arcFile = new Archive(path); m_strCachePath = Path.Combine(Program.GameMode.ModInfoCacheDirectory, ModName + ".zip"); if (p_booUseCache && File.Exists(m_strCachePath)) m_arcCacheFile = new Archive(m_strCachePath); FindPathPrefix(); m_arcFile.FilesChanged += new EventHandler(Archive_FilesChanged); baseName = ModName.ToLowerInvariant(); Author = DEFAULT_AUTHOR; Description = Email = Website = string.Empty; HumanReadableVersion = DEFAULT_VERSION; MachineVersion = DefaultVersion; MinFommVersion = DefaultMinFommVersion; Groups = new string[0]; isActive = (InstallLog.Current.GetModKey(this.baseName) != null); //check for script for (int i = 0; i < FomodScript.ScriptNames.Length; i++) if (ContainsFile("fomod/" + FomodScript.ScriptNames[i])) { m_strScriptPath = "fomod/" + FomodScript.ScriptNames[i]; break; } //check for readme for (int i = 0; i < Readme.ValidExtensions.Length; i++) if (ContainsFile("readme - " + baseName + Readme.ValidExtensions[i])) { m_strReadmePath = "Readme - " + Path.GetFileNameWithoutExtension(path) + Readme.ValidExtensions[i]; break; } if (String.IsNullOrEmpty(m_strReadmePath)) for (int i = 0; i < Readme.ValidExtensions.Length; i++) if (ContainsFile("docs/readme - " + baseName + Readme.ValidExtensions[i])) { m_strReadmePath = "docs/Readme - " + Path.GetFileNameWithoutExtension(path) + Readme.ValidExtensions[i]; break; } //check for screenshot string[] extensions = new string[] { ".png", ".jpg", ".bmp" }; for (int i = 0; i < extensions.Length; i++) if (ContainsFile("fomod/screenshot" + extensions[i])) { m_strScreenshotPath = "fomod/screenshot" + extensions[i]; break; } if (p_booUseCache && !File.Exists(m_strCachePath) && (m_arcFile.IsSolid || m_arcFile.ReadOnly)) { string strTmpInfo = Program.CreateTempDirectory(); try { Directory.CreateDirectory(Path.Combine(strTmpInfo, GetPrefixAdjustedPath("fomod"))); if (ContainsFile("fomod/info.xml")) File.WriteAllBytes(Path.Combine(strTmpInfo, GetPrefixAdjustedPath("fomod/info.xml")), GetFileContents("fomod/info.xml")); else File.WriteAllText(Path.Combine(strTmpInfo, GetPrefixAdjustedPath("fomod/info.xml")), "<fomod/>"); if (!String.IsNullOrEmpty(m_strReadmePath)) File.WriteAllBytes(Path.Combine(strTmpInfo, GetPrefixAdjustedPath(m_strReadmePath)), GetFileContents(m_strReadmePath)); if (!String.IsNullOrEmpty(m_strScreenshotPath)) File.WriteAllBytes(Path.Combine(strTmpInfo, GetPrefixAdjustedPath(m_strScreenshotPath)), GetFileContents(m_strScreenshotPath)); string[] strFilesToCompress = Directory.GetFiles(strTmpInfo, "*.*", SearchOption.AllDirectories); if (strFilesToCompress.Length > 0) { SevenZipCompressor szcCompressor = new SevenZipCompressor(); szcCompressor.ArchiveFormat = OutArchiveFormat.Zip; szcCompressor.CompressionLevel = CompressionLevel.Ultra; szcCompressor.CompressDirectory(strTmpInfo, m_strCachePath); } } finally { FileUtil.ForceDelete(strTmpInfo); } if (File.Exists(m_strCachePath)) m_arcCacheFile = new Archive(m_strCachePath); } LoadInfo(); }