public Hmod(string mod, string[] installedHmods = null) { isInstalled = false; if (installedHmods != null) { isInstalled = installedHmods.Contains(mod); } RawName = mod; this.HmodPath = null; this.isFile = false; string[] readmeFiles = new string[] { "readme.txt", "readme.md", "readme" }; string usermodsDirectory = Path.Combine(Program.BaseDirectoryExternal, "user_mods"); string cacheDir = Shared.PathCombine(Program.BaseDirectoryExternal, "user_mods", "readme_cache"); string cacheFile = Path.Combine(cacheDir, $"{mod}.xml"); Dictionary <string, string> readmeData = new Dictionary <string, string>(); try { var dir = Path.Combine(usermodsDirectory, mod + ".hmod"); if (Directory.Exists(dir)) { isFile = false; HmodPath = dir; foreach (var f in readmeFiles) { var fn = Path.Combine(dir, f); if (File.Exists(fn)) { readmeData.Add(f.ToLower(), File.ReadAllText(fn)); } } } else if (File.Exists(dir)) { isFile = true; HmodPath = dir; ReadmeCache cache; FileInfo info = new FileInfo(dir); bool skipExtraction = false; if (File.Exists(cacheFile)) { try { cache = XMLSerialization.DeserializeXMLFileToObject <ReadmeCache>(cacheFile); if (cache.LastModified == info.LastWriteTimeUtc) { skipExtraction = true; readmeData = cache.getReadmeDictionary(); } } catch { } } if (!skipExtraction) { using (var extractor = ArchiveFactory.Open(dir)) { var tar = new MemoryStream(); extractor.Entries.First().OpenEntryStream().CopyTo(tar); tar.Seek(0, SeekOrigin.Begin); using (var extractorTar = ArchiveFactory.Open(tar)) { foreach (var f in extractorTar.Entries) { foreach (var readmeFilename in readmeFiles) { if (f.Key.ToLower() != readmeFilename && f.Key.ToLower() != $"./{readmeFilename}") { continue; } var o = new MemoryStream(); f.OpenEntryStream().CopyTo(o); readmeData.Add(readmeFilename, Encoding.UTF8.GetString(o.ToArray())); } } } } cache = new ReadmeCache(readmeData, "", info.LastWriteTimeUtc); if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } File.WriteAllText(cacheFile, cache.Serialize()); } } else { if (File.Exists(cacheFile)) { try { ReadmeCache cache; cache = XMLSerialization.DeserializeXMLFileToObject <ReadmeCache>(cacheFile); readmeData = cache.getReadmeDictionary(); } catch { } } } } catch { } string readme; bool markdown = false; if (readmeData.TryGetValue("readme.md", out readme)) { markdown = true; } else if (readmeData.TryGetValue("readme.txt", out readme)) { } else if (readmeData.TryGetValue("readme", out readme)) { } else { readme = ""; } this.Readme = new HmodReadme(readme, markdown); if (!this.Readme.frontMatter.TryGetValue("Name", out this.Name)) { this.Name = mod; } if (!this.Readme.frontMatter.TryGetValue("Category", out this.Category)) { this.Category = Properties.Resources.Unknown; } if (!this.Readme.frontMatter.TryGetValue("Creator", out this.Creator)) { this.Creator = Properties.Resources.Unknown; } }
public Hmod(string mod) { RawName = mod; this.HmodPath = null; this.isFile = false; string[] readmeFiles = new string[] { "readme.txt", "readme.md", "readme" }; string usermodsDirectory = Path.Combine(Program.BaseDirectoryExternal, "user_mods"); string cacheDir = Shared.PathCombine(Program.BaseDirectoryExternal, "user_mods", "readme_cache"); string cacheFile = Path.Combine(cacheDir, $"{mod}.xml"); Dictionary <string, string> readmeData = new Dictionary <string, string>(); try { var dir = Path.Combine(usermodsDirectory, mod + ".hmod"); if (Directory.Exists(dir)) { isFile = false; HmodPath = dir; foreach (var f in readmeFiles) { var fn = Path.Combine(dir, f); if (File.Exists(fn)) { readmeData.Add(f.ToLower(), File.ReadAllText(fn)); } } } else if (File.Exists(dir)) { isFile = true; HmodPath = dir; ReadmeCache cache; FileInfo info = new FileInfo(dir); bool skipExtraction = false; if (File.Exists(cacheFile)) { try { cache = XMLSerialization.DeserializeXMLFileToObject <ReadmeCache>(cacheFile); if (cache.LastModified == info.LastWriteTimeUtc) { skipExtraction = true; readmeData = cache.getReadmeDictionary(); } } catch { } } if (!skipExtraction) { SevenZipExtractor.SetLibraryPath(Path.Combine(Program.BaseDirectoryInternal, IntPtr.Size == 8 ? @"tools\7z64.dll" : @"tools\7z.dll")); using (var szExtractor = new SevenZipExtractor(dir)) { var tar = new MemoryStream(); szExtractor.ExtractFile(0, tar); tar.Seek(0, SeekOrigin.Begin); using (var szExtractorTar = new SevenZipExtractor(tar)) { foreach (var f in szExtractorTar.ArchiveFileNames) { if (readmeFiles.Contains(f.ToLower())) { var o = new MemoryStream(); szExtractorTar.ExtractFile(f, o); readmeData.Add(f.ToLower(), Encoding.UTF8.GetString(o.ToArray())); } } } } cache = new ReadmeCache(readmeData, "", info.LastWriteTimeUtc); if (!Directory.Exists(cacheDir)) { Directory.CreateDirectory(cacheDir); } File.WriteAllText(cacheFile, cache.Serialize()); } } else { if (File.Exists(cacheFile)) { try { ReadmeCache cache; cache = XMLSerialization.DeserializeXMLFileToObject <ReadmeCache>(cacheFile); readmeData = cache.getReadmeDictionary(); } catch { } } } } catch { } string readme; bool markdown = false; if (readmeData.TryGetValue("readme.md", out readme)) { markdown = true; } else if (readmeData.TryGetValue("readme.txt", out readme)) { } else if (readmeData.TryGetValue("readme", out readme)) { } else { readme = ""; } this.Readme = new HmodReadme(readme, markdown); if (!this.Readme.frontMatter.TryGetValue("Name", out this.Name)) { this.Name = mod; } if (!this.Readme.frontMatter.TryGetValue("Category", out this.Category)) { this.Category = Properties.Resources.Unknown; } if (!this.Readme.frontMatter.TryGetValue("Creator", out this.Creator)) { this.Creator = Properties.Resources.Unknown; } }