diff --git a/SabreTools.Library/DatFiles/DatFile.cs b/SabreTools.Library/DatFiles/DatFile.cs index d94904fb..d835293a 100644 --- a/SabreTools.Library/DatFiles/DatFile.cs +++ b/SabreTools.Library/DatFiles/DatFile.cs @@ -2094,7 +2094,7 @@ namespace SabreTools.Library.DatFiles // If we have an archive and we're supposed to scan it if (archive != null && !asFiles.HasFlag(TreatAsFile.Archive)) - extracted = archive.GetChildren(omitFromScan: omitFromScan, date: addDate); + extracted = archive.GetChildren(date: addDate); // If the file should be skipped based on type, do so now if ((extracted != null && skipFileType == SkipFileType.Archive) @@ -2109,7 +2109,7 @@ namespace SabreTools.Library.DatFiles // Otherwise, add all of the found items else - ProcessArchive(newItem, newBasePath, addBlanks, archive, extracted); + ProcessArchive(newItem, newBasePath, addBlanks, archive, extracted, omitFromScan); // Cue to delete the file if it's a copy if (copyFiles && item != newItem) @@ -2178,7 +2178,8 @@ namespace SabreTools.Library.DatFiles /// True if blank items should be created for empty folders, false otherwise /// BaseArchive to get blank folders from, if necessary /// List of BaseFiles representing the internal files - private void ProcessArchive(string item, string basePath, bool addBlanks, BaseArchive archive, List extracted) + /// Hash flag saying what hashes should not be calculated + private void ProcessArchive(string item, string basePath, bool addBlanks, BaseArchive archive, List extracted, Hash omitFromScan) { // Get the parent path for all items string parent = (Path.GetDirectoryName(Path.GetFullPath(item)) + Path.DirectorySeparatorChar).Remove(0, basePath.Length) + Path.GetFileNameWithoutExtension(item); @@ -2187,6 +2188,7 @@ namespace SabreTools.Library.DatFiles Parallel.ForEach(extracted, Globals.ParallelOptions, baseFile => { DatItem datItem = DatItem.Create(baseFile); + datItem.RemoveFields(omitFromScan.AsFields()); ProcessFileHelper(item, datItem, basePath, parent); }); @@ -2220,8 +2222,9 @@ namespace SabreTools.Library.DatFiles { Globals.Logger.Verbose($"'{Path.GetFileName(item)}' treated like a file"); BaseFile baseFile = FileExtensions.GetInfo(item, addDate, Header.HeaderSkipper, asFiles); - baseFile.RemoveHashes(omitFromScan); - ProcessFileHelper(item, DatItem.Create(baseFile), basePath, string.Empty); + DatItem datItem = DatItem.Create(baseFile); + datItem.RemoveFields(omitFromScan.AsFields()); + ProcessFileHelper(item, datItem, basePath, string.Empty); } /// @@ -2607,21 +2610,16 @@ namespace SabreTools.Library.DatFiles bool isTorrentGzip = tgz.IsTorrent(); // Get the base archive first - BaseArchive archive = BaseArchive.Create(file); + BaseArchive archive = BaseArchive.Create(file, quickScan); // Now get all extracted items from the archive if (archive != null) - { - // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - entries = archive.GetChildren(omitFromScan: (quickScan ? Hash.SecureHashes : Hash.DeepHashes), date: date); - } + entries = archive.GetChildren(date: date); // If the entries list is null, we encountered an error or have a file and should scan externally if (entries == null && File.Exists(file)) { - // TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually BaseFile internalFileInfo = FileExtensions.GetInfo(file, asFiles: asFiles); - internalFileInfo.RemoveHashes(quickScan ? Hash.SecureHashes : Hash.DeepHashes); // Create the correct DatItem DatItem internalDatItem; diff --git a/SabreTools.Library/FileTypes/BaseArchive.cs b/SabreTools.Library/FileTypes/BaseArchive.cs index 3661fa9d..0d0e1d46 100644 --- a/SabreTools.Library/FileTypes/BaseArchive.cs +++ b/SabreTools.Library/FileTypes/BaseArchive.cs @@ -2,7 +2,6 @@ using System.IO; using SabreTools.Library.Data; -using SabreTools.Library.DatFiles; using SabreTools.Library.DatItems; using SabreTools.Library.IO; @@ -12,6 +11,8 @@ namespace SabreTools.Library.FileTypes { #region Protected instance variables + protected bool QuickScan { get; set; } = false; + // Buffer size used by archives protected const int _bufferSize = 4096 * 128; @@ -40,8 +41,9 @@ namespace SabreTools.Library.FileTypes /// Create an archive object from a filename, if possible /// /// Name of the file to create the archive from + /// True to use archive header values, false otherwise /// Archive object representing the inputs - public static BaseArchive Create(string input) + public static BaseArchive Create(string input, bool quickScan = false) { BaseArchive archive = null; @@ -81,6 +83,10 @@ namespace SabreTools.Library.FileTypes break; } + // Set the quickscan flag + if (archive != null) + archive.QuickScan = quickScan; + return archive; } @@ -147,11 +153,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override abstract List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false); + public override abstract List GetChildren(bool date = false); /// /// Generate a list of empty folders in an archive diff --git a/SabreTools.Library/FileTypes/BaseFile.cs b/SabreTools.Library/FileTypes/BaseFile.cs index 7d4220c8..49c8fdcb 100644 --- a/SabreTools.Library/FileTypes/BaseFile.cs +++ b/SabreTools.Library/FileTypes/BaseFile.cs @@ -99,35 +99,5 @@ namespace SabreTools.Library.FileTypes } #endregion - - #region Manipulation - - /// - /// Remove hashes from the metadata - /// - /// Hash representing hashes to remove - public void RemoveHashes(Hash remove) - { - if (remove.HasFlag(Hash.CRC)) - CRC = null; - if (remove.HasFlag(Hash.MD5)) - MD5 = null; -#if NET_FRAMEWORK - if (remove.HasFlag(Hash.RIPEMD160)) - RIPEMD160 = null; -#endif - if (remove.HasFlag(Hash.SHA1)) - SHA1 = null; - if (remove.HasFlag(Hash.SHA256)) - SHA256 = null; - if (remove.HasFlag(Hash.SHA384)) - SHA384 = null; - if (remove.HasFlag(Hash.SHA512)) - SHA512 = null; - if (remove.HasFlag(Hash.SpamSum)) - SpamSum = null; - } - - #endregion } } diff --git a/SabreTools.Library/FileTypes/Folder.cs b/SabreTools.Library/FileTypes/Folder.cs index 490bb55c..ebb839da 100644 --- a/SabreTools.Library/FileTypes/Folder.cs +++ b/SabreTools.Library/FileTypes/Folder.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using SabreTools.Library.Data; -using SabreTools.Library.DatFiles; using SabreTools.Library.DatItems; using SabreTools.Library.IO; using SabreTools.Library.Tools; @@ -257,11 +256,9 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of immediate children from the current folder /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of BaseFile objects representing the found data - /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public virtual List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public virtual List GetChildren(bool date = false) { if (_children == null || _children.Count == 0) { @@ -269,7 +266,6 @@ namespace SabreTools.Library.FileTypes foreach (string file in Directory.EnumerateFiles(this.Filename, "*", SearchOption.TopDirectoryOnly)) { BaseFile nf = FileExtensions.GetInfo(file, date: date); - nf.RemoveHashes(omitFromScan); _children.Add(nf); } diff --git a/SabreTools.Library/FileTypes/GZipArchive.cs b/SabreTools.Library/FileTypes/GZipArchive.cs index 330b6de5..c78c3dbe 100644 --- a/SabreTools.Library/FileTypes/GZipArchive.cs +++ b/SabreTools.Library/FileTypes/GZipArchive.cs @@ -189,11 +189,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { if (_children == null || _children.Count == 0) { @@ -212,8 +211,8 @@ namespace SabreTools.Library.FileTypes { try { - // If secure hashes are disabled, do a quickscan - if (omitFromScan == Hash.SecureHashes) + // Perform a quickscan, if flagged to + if (QuickScan) { BaseFile tempRom = new BaseFile() { @@ -234,7 +233,6 @@ namespace SabreTools.Library.FileTypes ZipReturn ret = gz.ZipFileOpen(this.Filename); ret = gz.ZipFileOpenReadStream(0, out Stream gzstream, out ulong streamSize); BaseFile gzipEntryRom = gzstream.GetInfo(); - gzipEntryRom.RemoveHashes(omitFromScan); gzipEntryRom.Filename = gz.Filename(0); gzipEntryRom.Parent = gamename; gzipEntryRom.Date = (date && gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null); diff --git a/SabreTools.Library/FileTypes/LRZipArchive.cs b/SabreTools.Library/FileTypes/LRZipArchive.cs index 48b6ae2c..3574e5ac 100644 --- a/SabreTools.Library/FileTypes/LRZipArchive.cs +++ b/SabreTools.Library/FileTypes/LRZipArchive.cs @@ -78,11 +78,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { throw new NotImplementedException(); } diff --git a/SabreTools.Library/FileTypes/LZ4Archive.cs b/SabreTools.Library/FileTypes/LZ4Archive.cs index 099ca6b6..f1f56edf 100644 --- a/SabreTools.Library/FileTypes/LZ4Archive.cs +++ b/SabreTools.Library/FileTypes/LZ4Archive.cs @@ -78,11 +78,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { throw new NotImplementedException(); } diff --git a/SabreTools.Library/FileTypes/RarArchive.cs b/SabreTools.Library/FileTypes/RarArchive.cs index a3ba9481..f2490842 100644 --- a/SabreTools.Library/FileTypes/RarArchive.cs +++ b/SabreTools.Library/FileTypes/RarArchive.cs @@ -175,11 +175,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { List found = new List(); string gamename = Path.GetFileNameWithoutExtension(this.Filename); @@ -189,8 +188,8 @@ namespace SabreTools.Library.FileTypes SharpCompress.Archives.Rar.RarArchive ra = SharpCompress.Archives.Rar.RarArchive.Open(FileExtensions.TryOpenRead(this.Filename)); foreach (RarArchiveEntry entry in ra.Entries.Where(e => e != null && !e.IsDirectory)) { - // If secure hashes are disabled, do a quickscan - if (omitFromScan == Hash.SecureHashes) + // Perform a quickscan, if flagged to + if (QuickScan) { found.Add(new BaseFile { @@ -207,7 +206,6 @@ namespace SabreTools.Library.FileTypes { Stream entryStream = entry.OpenEntryStream(); BaseFile rarEntryRom = entryStream.GetInfo(size: entry.Size); - rarEntryRom.RemoveHashes(omitFromScan); rarEntryRom.Filename = entry.Key; rarEntryRom.Parent = gamename; rarEntryRom.Date = entry.LastModifiedTime?.ToString("yyyy/MM/dd hh:mm:ss"); diff --git a/SabreTools.Library/FileTypes/SevenZipArchive.cs b/SabreTools.Library/FileTypes/SevenZipArchive.cs index ff5a1bfa..21defa9c 100644 --- a/SabreTools.Library/FileTypes/SevenZipArchive.cs +++ b/SabreTools.Library/FileTypes/SevenZipArchive.cs @@ -259,11 +259,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { List found = new List(); string gamename = Path.GetFileNameWithoutExtension(this.Filename); @@ -299,8 +298,8 @@ namespace SabreTools.Library.FileTypes continue; } - // If secure hashes are disabled, do a quickscan - if (omitFromScan == Hash.SecureHashes) + // Perform a quickscan, if flagged to + if (QuickScan) { string newname = zf.Filename(i); long newsize = (long)zf.UncompressedSize(i); @@ -319,7 +318,6 @@ namespace SabreTools.Library.FileTypes else { BaseFile zipEntryRom = readStream.GetInfo(size: (long)zf.UncompressedSize(i), keepReadOpen: true); - zipEntryRom.RemoveHashes(omitFromScan); zipEntryRom.Filename = zf.Filename(i); zipEntryRom.Parent = gamename; found.Add(zipEntryRom); diff --git a/SabreTools.Library/FileTypes/TapeArchive.cs b/SabreTools.Library/FileTypes/TapeArchive.cs index 9a2dfebd..514a9fcc 100644 --- a/SabreTools.Library/FileTypes/TapeArchive.cs +++ b/SabreTools.Library/FileTypes/TapeArchive.cs @@ -180,11 +180,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { List found = new List(); string gamename = Path.GetFileNameWithoutExtension(this.Filename); @@ -194,8 +193,8 @@ namespace SabreTools.Library.FileTypes TarArchive ta = TarArchive.Open(FileExtensions.TryOpenRead(this.Filename)); foreach (TarArchiveEntry entry in ta.Entries.Where(e => e != null && !e.IsDirectory)) { - // If secure hashes are disabled, do a quickscan - if (omitFromScan == Hash.SecureHashes) + // Perform a quickscan, if flagged to + if (QuickScan) { found.Add(new BaseFile { @@ -212,7 +211,6 @@ namespace SabreTools.Library.FileTypes { Stream entryStream = entry.OpenEntryStream(); BaseFile tarEntryRom = entryStream.GetInfo(size: entry.Size); - tarEntryRom.RemoveHashes(omitFromScan); tarEntryRom.Filename = entry.Key; tarEntryRom.Parent = gamename; tarEntryRom.Date = entry.LastModifiedTime?.ToString("yyyy/MM/dd hh:mm:ss"); diff --git a/SabreTools.Library/FileTypes/XZArchive.cs b/SabreTools.Library/FileTypes/XZArchive.cs index 8fac3f5c..67f9f3e7 100644 --- a/SabreTools.Library/FileTypes/XZArchive.cs +++ b/SabreTools.Library/FileTypes/XZArchive.cs @@ -181,11 +181,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { if (_children == null || _children.Count == 0) { @@ -204,8 +203,8 @@ namespace SabreTools.Library.FileTypes { try { - // If secure hashes are disabled, do a quickscan - if (omitFromScan == Hash.SecureHashes) + // Perform a quickscan, if flagged to + if (QuickScan) { BaseFile tempRom = new BaseFile() { @@ -224,7 +223,6 @@ namespace SabreTools.Library.FileTypes { var xzStream = new XZStream(File.OpenRead(this.Filename)); BaseFile xzEntryRom = xzStream.GetInfo(); - xzEntryRom.RemoveHashes(omitFromScan); xzEntryRom.Filename = gamename; xzEntryRom.Parent = gamename; _children.Add(xzEntryRom); diff --git a/SabreTools.Library/FileTypes/ZPAQArchive.cs b/SabreTools.Library/FileTypes/ZPAQArchive.cs index dac91832..991953a4 100644 --- a/SabreTools.Library/FileTypes/ZPAQArchive.cs +++ b/SabreTools.Library/FileTypes/ZPAQArchive.cs @@ -78,11 +78,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { throw new NotImplementedException(); } diff --git a/SabreTools.Library/FileTypes/ZipArchive.cs b/SabreTools.Library/FileTypes/ZipArchive.cs index ad668890..cbd4e196 100644 --- a/SabreTools.Library/FileTypes/ZipArchive.cs +++ b/SabreTools.Library/FileTypes/ZipArchive.cs @@ -260,11 +260,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { List found = new List(); string gamename = Path.GetFileNameWithoutExtension(this.Filename); @@ -300,8 +299,8 @@ namespace SabreTools.Library.FileTypes continue; } - // If secure hashes are disabled, do a quickscan - if (omitFromScan == Hash.SecureHashes) + // Perform a quickscan, if flagged to + if (QuickScan) { string newname = zf.Filename(i); long newsize = (long)zf.UncompressedSize(i); @@ -322,7 +321,6 @@ namespace SabreTools.Library.FileTypes else { BaseFile zipEntryRom = readStream.GetInfo(size: (long)zf.UncompressedSize(i), keepReadOpen: true); - zipEntryRom.RemoveHashes(omitFromScan); zipEntryRom.Filename = zf.Filename(i); zipEntryRom.Parent = gamename; string convertedDate = zf.LastModified(i).ToString("yyyy/MM/dd hh:mm:ss"); diff --git a/SabreTools.Library/FileTypes/ZstdArchive.cs b/SabreTools.Library/FileTypes/ZstdArchive.cs index 03954e5a..42b6b50b 100644 --- a/SabreTools.Library/FileTypes/ZstdArchive.cs +++ b/SabreTools.Library/FileTypes/ZstdArchive.cs @@ -79,11 +79,10 @@ namespace SabreTools.Library.FileTypes /// /// Generate a list of DatItem objects from the header values in an archive /// - /// Hash representing the hashes that should be skipped /// True if entry dates should be included, false otherwise (default) /// List of DatItem objects representing the found data /// TODO: All instances of Hash.DeepHashes should be made into 0x0 eventually - public override List GetChildren(Hash omitFromScan = Hash.DeepHashes, bool date = false) + public override List GetChildren(bool date = false) { throw new NotImplementedException(); } diff --git a/SabreTools.Library/Tools/Converters.cs b/SabreTools.Library/Tools/Converters.cs index b61fe75d..e0954d41 100644 --- a/SabreTools.Library/Tools/Converters.cs +++ b/SabreTools.Library/Tools/Converters.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Collections.Generic; +using System.Text.RegularExpressions; using SabreTools.Library.DatFiles; using SabreTools.Library.DatItems; @@ -42,6 +43,35 @@ namespace SabreTools.Library.Tools } } + /// + /// Get the fields associated with each hash type + /// + public static List AsFields(this Hash hash) + { + List fields = new List(); + + if (hash.HasFlag(Hash.CRC)) + fields.Add(Field.DatItem_CRC); + if (hash.HasFlag(Hash.MD5)) + fields.Add(Field.DatItem_MD5); +#if NET_FRAMEWORK + if (hash.HasFlag(Hash.RIPEMD160)) + fields.Add(Field.DatItem_RIPEMD160); +#endif + if (hash.HasFlag(Hash.SHA1)) + fields.Add(Field.DatItem_SHA1); + if (hash.HasFlag(Hash.SHA256)) + fields.Add(Field.DatItem_SHA256); + if (hash.HasFlag(Hash.SHA384)) + fields.Add(Field.DatItem_SHA384); + if (hash.HasFlag(Hash.SHA512)) + fields.Add(Field.DatItem_SHA512); + if (hash.HasFlag(Hash.SpamSum)) + fields.Add(Field.DatItem_SpamSum); + + return fields; + } + /// /// Get the default OutputFormat associated with each PackingFlag ///