From 5daa42636b9fcba420fb37edb71ef6d5073edf1a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sat, 4 Jan 2025 22:50:36 -0500 Subject: [PATCH] Split GetInfo stream implementation --- SabreTools.DatTools/Rebuilder.cs | 9 ++- SabreTools.FileTypes/Archives/GZipArchive.cs | 4 +- SabreTools.FileTypes/Archives/RarArchive.cs | 2 +- .../Archives/SevenZipArchive.cs | 5 +- SabreTools.FileTypes/Archives/TapeArchive.cs | 2 +- SabreTools.FileTypes/Archives/XZArchive.cs | 4 +- SabreTools.FileTypes/Archives/ZipArchive.cs | 8 +-- SabreTools.FileTypes/FileTypeTool.cs | 55 ++++++++++++++----- 8 files changed, 60 insertions(+), 29 deletions(-) diff --git a/SabreTools.DatTools/Rebuilder.cs b/SabreTools.DatTools/Rebuilder.cs index 7889a56d..04e56beb 100644 --- a/SabreTools.DatTools/Rebuilder.cs +++ b/SabreTools.DatTools/Rebuilder.cs @@ -499,7 +499,8 @@ namespace SabreTools.DatTools if (rule.TransformStream(fileStream, transformStream, keepReadOpen: true, keepWriteOpen: true)) { // Get the file informations that we will be using - Rom headerless = new(FileTypeTool.GetInfo(transformStream, keepReadOpen: true)); + HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + Rom headerless = new(FileTypeTool.GetInfo(transformStream, hashes, keepReadOpen: true)); // If we have duplicates and we're not filtering if (ShouldRebuild(datFile, headerless, transformStream, false, out dupes)) @@ -573,7 +574,8 @@ namespace SabreTools.DatTools string? machinename = null; // Get the item from the current file - Rom item = new(FileTypeTool.GetInfo(stream, keepReadOpen: true)); + HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + Rom item = new(FileTypeTool.GetInfo(stream, hashes, keepReadOpen: true)); item.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.DescriptionKey, Path.GetFileNameWithoutExtension(item.GetName())); item.GetFieldValue(DatItem.MachineKey)!.SetFieldValue(Models.Metadata.Machine.NameKey, Path.GetFileNameWithoutExtension(item.GetName())); @@ -630,7 +632,8 @@ namespace SabreTools.DatTools string? machinename = null; // Get the item from the current file - var item = new Rom(FileTypeTool.GetInfo(stream, keepReadOpen: true)); + HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; + var item = new Rom(FileTypeTool.GetInfo(stream, hashes, keepReadOpen: true)); // Create a machine for the current item var machine = new Machine(); diff --git a/SabreTools.FileTypes/Archives/GZipArchive.cs b/SabreTools.FileTypes/Archives/GZipArchive.cs index b1cebf29..6efc5baa 100644 --- a/SabreTools.FileTypes/Archives/GZipArchive.cs +++ b/SabreTools.FileTypes/Archives/GZipArchive.cs @@ -239,7 +239,7 @@ namespace SabreTools.FileTypes.Archives var gz = new gZip(); ZipReturn ret = gz.ZipFileOpen(Filename); ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize); - gzipEntryRom = FileTypeTool.GetInfo(gzstream, hashes: _hashTypes); + gzipEntryRom = FileTypeTool.GetInfo(gzstream, _hashTypes); gzipEntryRom.Filename = gz.GetLocalFile(0).Filename; gzipEntryRom.Parent = gamename; gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null); @@ -440,7 +440,7 @@ namespace SabreTools.FileTypes.Archives outDir = Path.GetFullPath(outDir); // Now get the Rom info for the file so we have hashes and size - baseFile = FileTypeTool.GetInfo(inputStream, keepReadOpen: true); + baseFile = FileTypeTool.GetInfo(inputStream, _hashTypes, keepReadOpen: true); // Get the output file name string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty); diff --git a/SabreTools.FileTypes/Archives/RarArchive.cs b/SabreTools.FileTypes/Archives/RarArchive.cs index 88e8046c..3b22dba0 100644 --- a/SabreTools.FileTypes/Archives/RarArchive.cs +++ b/SabreTools.FileTypes/Archives/RarArchive.cs @@ -210,7 +210,7 @@ namespace SabreTools.FileTypes.Archives else { using Stream entryStream = entry.OpenEntryStream(); - rarEntryRom = FileTypeTool.GetInfo(entryStream, size: entry.Size, hashes: _hashTypes); + rarEntryRom = FileTypeTool.GetInfo(entryStream, entry.Size, _hashTypes); } // Fill in common details and add to the list diff --git a/SabreTools.FileTypes/Archives/SevenZipArchive.cs b/SabreTools.FileTypes/Archives/SevenZipArchive.cs index 342b9198..6007f126 100644 --- a/SabreTools.FileTypes/Archives/SevenZipArchive.cs +++ b/SabreTools.FileTypes/Archives/SevenZipArchive.cs @@ -282,7 +282,10 @@ namespace SabreTools.FileTypes.Archives // Otherwise, use the stream directly else { - zipEntryRom = FileTypeTool.GetInfo(readStream, size: (long)zf.GetLocalFile(i).UncompressedSize, hashes: _hashTypes, keepReadOpen: true); + zipEntryRom = FileTypeTool.GetInfo(readStream, + (long)zf.GetLocalFile(i).UncompressedSize, + _hashTypes, + keepReadOpen: true); } // Fill in common details and add to the list diff --git a/SabreTools.FileTypes/Archives/TapeArchive.cs b/SabreTools.FileTypes/Archives/TapeArchive.cs index 0ab6096e..5873c53b 100644 --- a/SabreTools.FileTypes/Archives/TapeArchive.cs +++ b/SabreTools.FileTypes/Archives/TapeArchive.cs @@ -203,7 +203,7 @@ namespace SabreTools.FileTypes.Archives else { using Stream entryStream = entry.OpenEntryStream(); - tarEntryRom = FileTypeTool.GetInfo(entryStream, size: entry.Size, hashes: _hashTypes); + tarEntryRom = FileTypeTool.GetInfo(entryStream, entry.Size, _hashTypes); } // Fill in common details and add to the list diff --git a/SabreTools.FileTypes/Archives/XZArchive.cs b/SabreTools.FileTypes/Archives/XZArchive.cs index ef3d913c..984169d4 100644 --- a/SabreTools.FileTypes/Archives/XZArchive.cs +++ b/SabreTools.FileTypes/Archives/XZArchive.cs @@ -215,7 +215,7 @@ namespace SabreTools.FileTypes.Archives else { var xzStream = new XZStream(File.OpenRead(Filename!)); - xzEntryRom = FileTypeTool.GetInfo(xzStream, hashes: _hashTypes); + xzEntryRom = FileTypeTool.GetInfo(xzStream, _hashTypes); xzEntryRom.Filename = gamename; xzStream.Dispose(); } @@ -330,7 +330,7 @@ namespace SabreTools.FileTypes.Archives outDir = Path.GetFullPath(outDir); // Now get the Rom info for the file so we have hashes and size - baseFile = FileTypeTool.GetInfo(inputStream, keepReadOpen: true); + baseFile = FileTypeTool.GetInfo(inputStream, _hashTypes, keepReadOpen: true); // Get the output file name string outfile = Path.Combine(outDir, Core.Tools.Utilities.GetDepotPath(baseFile.SHA1, Depth)!); diff --git a/SabreTools.FileTypes/Archives/ZipArchive.cs b/SabreTools.FileTypes/Archives/ZipArchive.cs index 61a50b95..bb0c37e5 100644 --- a/SabreTools.FileTypes/Archives/ZipArchive.cs +++ b/SabreTools.FileTypes/Archives/ZipArchive.cs @@ -362,8 +362,8 @@ namespace SabreTools.FileTypes.Archives else { zipEntryRom = FileTypeTool.GetInfo(readStream, - size: (long)localFile.UncompressedSize, - hashes: _hashTypes, + (long)localFile.UncompressedSize, + _hashTypes, keepReadOpen: true); } @@ -419,8 +419,8 @@ namespace SabreTools.FileTypes.Archives else { zipEntryRom = FileTypeTool.GetInfo(readStream, - size: localFile.Length, - hashes: _hashTypes, + localFile.Length, + _hashTypes, keepReadOpen: false); } diff --git a/SabreTools.FileTypes/FileTypeTool.cs b/SabreTools.FileTypes/FileTypeTool.cs index b27041f3..ea2589bf 100644 --- a/SabreTools.FileTypes/FileTypeTool.cs +++ b/SabreTools.FileTypes/FileTypeTool.cs @@ -1,4 +1,3 @@ -using System; using System.IO; using SabreTools.FileTypes.Aaru; using SabreTools.FileTypes.Archives; @@ -20,8 +19,8 @@ namespace SabreTools.FileTypes /// /// Filename to get information from /// Hashes to include in the information - /// Populated BaseFile object if success, null on error - public static BaseFile? GetInfo(string input, HashType[] hashes) + /// Populated BaseFile object if success, empty on error + public static BaseFile GetInfo(string input, HashType[] hashes) => GetInfo(input, header: null, hashes, asFiles: 0x00); /// @@ -30,8 +29,8 @@ namespace SabreTools.FileTypes /// Filename to get information from /// Hashes to include in the information /// TreatAsFiles representing special format scanning - /// Populated BaseFile object if success, null on error - public static BaseFile? GetInfo(string input, HashType[] hashes, TreatAsFile asFiles) + /// Populated BaseFile object if success, empty on error + public static BaseFile GetInfo(string input, HashType[] hashes, TreatAsFile asFiles) => GetInfo(input, header: null, hashes, asFiles); /// @@ -41,12 +40,12 @@ namespace SabreTools.FileTypes /// Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise /// Hashes to include in the information /// TreatAsFiles representing special format scanning - /// Populated BaseFile object if success, null on error - public static BaseFile? GetInfo(string input, string? header, HashType[] hashes, TreatAsFile asFiles) + /// Populated BaseFile object if success, empty on error + public static BaseFile GetInfo(string input, string? header, HashType[] hashes, TreatAsFile asFiles) { // Add safeguard if file doesn't exist if (!File.Exists(input)) - return null; + return new BaseFile(); // Get input information var fileType = GetFileType(input); @@ -97,22 +96,48 @@ namespace SabreTools.FileTypes } /// - /// Retrieve file information for a single file + /// Retrieve file information for a single stream /// - /// Filename to get information from + /// Stream to get information from + /// Hashes to include in the information + /// Populated BaseFile object if success, null on error + public static BaseFile GetInfo(Stream? input, HashType[] hashes) + => GetInfo(input, size: -1, hashes, keepReadOpen: false); + + /// + /// Retrieve file information for a single stream + /// + /// Stream to get information from /// Size of the input stream /// Hashes to include in the information - /// True if the underlying read stream should be kept open, false otherwise + /// Populated BaseFile object if success, null on error + public static BaseFile GetInfo(Stream? input, long size, HashType[] hashes) + => GetInfo(input, size, hashes, keepReadOpen: false); + + /// + /// Retrieve file information for a single stream + /// + /// Stream to get information from + /// Hashes to include in the information + /// Indicates if the underlying read stream should be kept open + /// Populated BaseFile object if success, null on error + public static BaseFile GetInfo(Stream? input, HashType[] hashes, bool keepReadOpen) + => GetInfo(input, size: -1, hashes, keepReadOpen); + + /// + /// Retrieve file information for a single file + /// + /// Stream to get information from + /// Size of the input stream + /// Hashes to include in the information + /// Indicates if the underlying read stream should be kept open /// Populated BaseFile object if success, empty one on error - public static BaseFile GetInfo(Stream? input, long size = -1, HashType[]? hashes = null, bool keepReadOpen = false) + public static BaseFile GetInfo(Stream? input, long size, HashType[]? hashes, bool keepReadOpen) { // If we have no stream if (input == null) return new BaseFile(); - // If no hashes are set, use the standard array - hashes ??= [HashType.CRC32, HashType.MD5, HashType.SHA1]; - // If we want to automatically set the size if (size == -1) size = input.Length;