From 9dc3f4bf2da00409c088b69056801edb8e1a04b3 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Fri, 17 May 2013 23:44:33 +1200 Subject: [PATCH] Add IFolder.AllFileNames() for listing the filenames in a package. --- OpenRA.FileFormats/Filesystem/Folder.cs | 6 +++ .../Filesystem/InstallShieldPackage.cs | 10 ++++- OpenRA.FileFormats/Filesystem/MixFile.cs | 37 +++++++++++++++++++ OpenRA.FileFormats/Filesystem/ZipFile.cs | 7 +++- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/OpenRA.FileFormats/Filesystem/Folder.cs b/OpenRA.FileFormats/Filesystem/Folder.cs index 805e2746de..63773ca599 100644 --- a/OpenRA.FileFormats/Filesystem/Folder.cs +++ b/OpenRA.FileFormats/Filesystem/Folder.cs @@ -54,6 +54,12 @@ namespace OpenRA.FileFormats } } + public IEnumerable AllFileNames() + { + foreach (var filename in Directory.GetFiles(path, "*", SearchOption.TopDirectoryOnly)) + yield return Path.GetFileName(filename); + } + public bool Exists(string filename) { return File.Exists(Path.Combine(path, filename)); diff --git a/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs b/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs index cf59751aa9..d4a444250e 100644 --- a/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs +++ b/OpenRA.FileFormats/Filesystem/InstallShieldPackage.cs @@ -18,6 +18,7 @@ namespace OpenRA.FileFormats public class InstallShieldPackage : IFolder { readonly Dictionary index = new Dictionary(); + readonly List filenames; readonly Stream s; readonly long dataStart = 255; int priority; @@ -25,6 +26,7 @@ namespace OpenRA.FileFormats public InstallShieldPackage(string filename, int priority) { this.priority = priority; + filenames = new List(); s = FileSystem.Open(filename); // Parse package header @@ -77,7 +79,8 @@ namespace OpenRA.FileFormats var FileName = new String(reader.ReadChars(NameLength)); var hash = PackageEntry.HashFilename(FileName); - index.Add(hash, new PackageEntry(hash,AccumulatedData, CompressedSize)); + index.Add(hash, new PackageEntry(hash, AccumulatedData, CompressedSize)); + filenames.Add(FileName); AccumulatedData += CompressedSize; // Skip to the end of the chunk @@ -107,6 +110,11 @@ namespace OpenRA.FileFormats return index.Keys; } + public IEnumerable AllFileNames() + { + return filenames; + } + public bool Exists(string filename) { return index.ContainsKey(PackageEntry.HashFilename(filename)); diff --git a/OpenRA.FileFormats/Filesystem/MixFile.cs b/OpenRA.FileFormats/Filesystem/MixFile.cs index 8e32eb46a4..8920491594 100644 --- a/OpenRA.FileFormats/Filesystem/MixFile.cs +++ b/OpenRA.FileFormats/Filesystem/MixFile.cs @@ -20,6 +20,7 @@ namespace OpenRA.FileFormats Stream GetContent(string filename); bool Exists(string filename); IEnumerable AllFileHashes(); + IEnumerable AllFileNames(); void Write(Dictionary contents); int Priority { get; } } @@ -163,6 +164,42 @@ namespace OpenRA.FileFormats return index.Keys; } + public IEnumerable AllFileNames() + { + var lookup = new Dictionary(); + if (Exists("local mix database.dat")) + { + var db = new XccLocalDatabase(GetContent("local mix database.dat")); + foreach (var e in db.Entries) + { + var hash = PackageEntry.HashFilename(e); + if (!lookup.ContainsKey(hash)) + lookup.Add(hash, e); + + var crc = PackageEntry.CrcHashFilename(e); + if (!lookup.ContainsKey(crc)) + lookup.Add(crc, e); + } + } + + if (FileSystem.Exists("global mix database.dat")) + { + var db = new XccGlobalDatabase(FileSystem.Open("global mix database.dat")); + foreach (var e in db.Entries) + { + var hash = PackageEntry.HashFilename(e); + if (!lookup.ContainsKey(hash)) + lookup.Add(hash, e); + + var crc = PackageEntry.CrcHashFilename(e); + if (!lookup.ContainsKey(crc)) + lookup.Add(crc, e); + } + } + + return index.Keys.Select(k => lookup.ContainsKey(k) ? lookup[k] : "Unknown File [{0}]".F(k)); + } + public bool Exists(string filename) { return (index.ContainsKey(PackageEntry.HashFilename(filename)) || index.ContainsKey(PackageEntry.CrcHashFilename(filename))); diff --git a/OpenRA.FileFormats/Filesystem/ZipFile.cs b/OpenRA.FileFormats/Filesystem/ZipFile.cs index caa0158e5e..91e8b44984 100644 --- a/OpenRA.FileFormats/Filesystem/ZipFile.cs +++ b/OpenRA.FileFormats/Filesystem/ZipFile.cs @@ -51,7 +51,6 @@ namespace OpenRA.FileFormats public Stream GetContent(string filename) { - using (var z = pkg.GetInputStream(pkg.GetEntry(filename))) { var ms = new MemoryStream(); @@ -71,6 +70,12 @@ namespace OpenRA.FileFormats yield return PackageEntry.HashFilename(entry.Name); } + public IEnumerable AllFileNames() + { + foreach(ZipEntry entry in pkg) + yield return entry.Name; + } + public bool Exists(string filename) { return pkg.GetEntry(filename) != null;