Split GetInfo stream implementation

This commit is contained in:
Matt Nadareski
2025-01-04 22:50:36 -05:00
parent b784a3b0bf
commit 5daa42636b
8 changed files with 60 additions and 29 deletions

View File

@@ -499,7 +499,8 @@ namespace SabreTools.DatTools
if (rule.TransformStream(fileStream, transformStream, keepReadOpen: true, keepWriteOpen: true)) if (rule.TransformStream(fileStream, transformStream, keepReadOpen: true, keepWriteOpen: true))
{ {
// Get the file informations that we will be using // 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 we have duplicates and we're not filtering
if (ShouldRebuild(datFile, headerless, transformStream, false, out dupes)) if (ShouldRebuild(datFile, headerless, transformStream, false, out dupes))
@@ -573,7 +574,8 @@ namespace SabreTools.DatTools
string? machinename = null; string? machinename = null;
// Get the item from the current file // 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<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Path.GetFileNameWithoutExtension(item.GetName())); item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.DescriptionKey, Path.GetFileNameWithoutExtension(item.GetName()));
item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetFileNameWithoutExtension(item.GetName())); item.GetFieldValue<Machine>(DatItem.MachineKey)!.SetFieldValue<string?>(Models.Metadata.Machine.NameKey, Path.GetFileNameWithoutExtension(item.GetName()));
@@ -630,7 +632,8 @@ namespace SabreTools.DatTools
string? machinename = null; string? machinename = null;
// Get the item from the current file // 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 // Create a machine for the current item
var machine = new Machine(); var machine = new Machine();

View File

@@ -239,7 +239,7 @@ namespace SabreTools.FileTypes.Archives
var gz = new gZip(); var gz = new gZip();
ZipReturn ret = gz.ZipFileOpen(Filename); ZipReturn ret = gz.ZipFileOpen(Filename);
ret = gz.ZipFileOpenReadStream(0, out Stream? gzstream, out ulong streamSize); 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.Filename = gz.GetLocalFile(0).Filename;
gzipEntryRom.Parent = gamename; gzipEntryRom.Parent = gamename;
gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null); gzipEntryRom.Date = (gz.TimeStamp > 0 ? gz.TimeStamp.ToString() : null);
@@ -440,7 +440,7 @@ namespace SabreTools.FileTypes.Archives
outDir = Path.GetFullPath(outDir); outDir = Path.GetFullPath(outDir);
// Now get the Rom info for the file so we have hashes and size // 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 // Get the output file name
string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty); string outfile = Path.Combine(outDir, Utilities.GetDepotPath(baseFile.SHA1, Depth) ?? string.Empty);

View File

@@ -210,7 +210,7 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
using Stream entryStream = entry.OpenEntryStream(); 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 // Fill in common details and add to the list

View File

@@ -282,7 +282,10 @@ namespace SabreTools.FileTypes.Archives
// Otherwise, use the stream directly // Otherwise, use the stream directly
else 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 // Fill in common details and add to the list

View File

@@ -203,7 +203,7 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
using Stream entryStream = entry.OpenEntryStream(); 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 // Fill in common details and add to the list

View File

@@ -215,7 +215,7 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
var xzStream = new XZStream(File.OpenRead(Filename!)); var xzStream = new XZStream(File.OpenRead(Filename!));
xzEntryRom = FileTypeTool.GetInfo(xzStream, hashes: _hashTypes); xzEntryRom = FileTypeTool.GetInfo(xzStream, _hashTypes);
xzEntryRom.Filename = gamename; xzEntryRom.Filename = gamename;
xzStream.Dispose(); xzStream.Dispose();
} }
@@ -330,7 +330,7 @@ namespace SabreTools.FileTypes.Archives
outDir = Path.GetFullPath(outDir); outDir = Path.GetFullPath(outDir);
// Now get the Rom info for the file so we have hashes and size // 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 // Get the output file name
string outfile = Path.Combine(outDir, Core.Tools.Utilities.GetDepotPath(baseFile.SHA1, Depth)!); string outfile = Path.Combine(outDir, Core.Tools.Utilities.GetDepotPath(baseFile.SHA1, Depth)!);

View File

@@ -362,8 +362,8 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
zipEntryRom = FileTypeTool.GetInfo(readStream, zipEntryRom = FileTypeTool.GetInfo(readStream,
size: (long)localFile.UncompressedSize, (long)localFile.UncompressedSize,
hashes: _hashTypes, _hashTypes,
keepReadOpen: true); keepReadOpen: true);
} }
@@ -419,8 +419,8 @@ namespace SabreTools.FileTypes.Archives
else else
{ {
zipEntryRom = FileTypeTool.GetInfo(readStream, zipEntryRom = FileTypeTool.GetInfo(readStream,
size: localFile.Length, localFile.Length,
hashes: _hashTypes, _hashTypes,
keepReadOpen: false); keepReadOpen: false);
} }

View File

@@ -1,4 +1,3 @@
using System;
using System.IO; using System.IO;
using SabreTools.FileTypes.Aaru; using SabreTools.FileTypes.Aaru;
using SabreTools.FileTypes.Archives; using SabreTools.FileTypes.Archives;
@@ -20,8 +19,8 @@ namespace SabreTools.FileTypes
/// </summary> /// </summary>
/// <param name="input">Filename to get information from</param> /// <param name="input">Filename to get information from</param>
/// <param name="hashes">Hashes to include in the information</param> /// <param name="hashes">Hashes to include in the information</param>
/// <returns>Populated BaseFile object if success, null on error</returns> /// <returns>Populated BaseFile object if success, empty on error</returns>
public static BaseFile? GetInfo(string input, HashType[] hashes) public static BaseFile GetInfo(string input, HashType[] hashes)
=> GetInfo(input, header: null, hashes, asFiles: 0x00); => GetInfo(input, header: null, hashes, asFiles: 0x00);
/// <summary> /// <summary>
@@ -30,8 +29,8 @@ namespace SabreTools.FileTypes
/// <param name="input">Filename to get information from</param> /// <param name="input">Filename to get information from</param>
/// <param name="hashes">Hashes to include in the information</param> /// <param name="hashes">Hashes to include in the information</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <param name="asFiles">TreatAsFiles representing special format scanning</param>
/// <returns>Populated BaseFile object if success, null on error</returns> /// <returns>Populated BaseFile object if success, empty on error</returns>
public static BaseFile? GetInfo(string input, HashType[] hashes, TreatAsFile asFiles) public static BaseFile GetInfo(string input, HashType[] hashes, TreatAsFile asFiles)
=> GetInfo(input, header: null, hashes, asFiles); => GetInfo(input, header: null, hashes, asFiles);
/// <summary> /// <summary>
@@ -41,12 +40,12 @@ namespace SabreTools.FileTypes
/// <param name="header">Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise</param> /// <param name="header">Populated string representing the name of the skipper to use, a blank string to use the first available checker, null otherwise</param>
/// <param name="hashes">Hashes to include in the information</param> /// <param name="hashes">Hashes to include in the information</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param> /// <param name="asFiles">TreatAsFiles representing special format scanning</param>
/// <returns>Populated BaseFile object if success, null on error</returns> /// <returns>Populated BaseFile object if success, empty on error</returns>
public static BaseFile? GetInfo(string input, string? header, HashType[] hashes, TreatAsFile asFiles) public static BaseFile GetInfo(string input, string? header, HashType[] hashes, TreatAsFile asFiles)
{ {
// Add safeguard if file doesn't exist // Add safeguard if file doesn't exist
if (!File.Exists(input)) if (!File.Exists(input))
return null; return new BaseFile();
// Get input information // Get input information
var fileType = GetFileType(input); var fileType = GetFileType(input);
@@ -97,22 +96,48 @@ namespace SabreTools.FileTypes
} }
/// <summary> /// <summary>
/// Retrieve file information for a single file /// Retrieve file information for a single stream
/// </summary> /// </summary>
/// <param name="input">Filename to get information from</param> /// <param name="input">Stream to get information from</param>
/// <param name="hashes">Hashes to include in the information</param>
/// <returns>Populated BaseFile object if success, null on error</returns>
public static BaseFile GetInfo(Stream? input, HashType[] hashes)
=> GetInfo(input, size: -1, hashes, keepReadOpen: false);
/// <summary>
/// Retrieve file information for a single stream
/// </summary>
/// <param name="input">Stream to get information from</param>
/// <param name="size">Size of the input stream</param> /// <param name="size">Size of the input stream</param>
/// <param name="hashes">Hashes to include in the information</param> /// <param name="hashes">Hashes to include in the information</param>
/// <param name="keepReadOpen">True if the underlying read stream should be kept open, false otherwise</param> /// <returns>Populated BaseFile object if success, null on error</returns>
public static BaseFile GetInfo(Stream? input, long size, HashType[] hashes)
=> GetInfo(input, size, hashes, keepReadOpen: false);
/// <summary>
/// Retrieve file information for a single stream
/// </summary>
/// <param name="input">Stream to get information from</param>
/// <param name="hashes">Hashes to include in the information</param>
/// <param name="keepReadOpen">Indicates if the underlying read stream should be kept open</param>
/// <returns>Populated BaseFile object if success, null on error</returns>
public static BaseFile GetInfo(Stream? input, HashType[] hashes, bool keepReadOpen)
=> GetInfo(input, size: -1, hashes, keepReadOpen);
/// <summary>
/// Retrieve file information for a single file
/// </summary>
/// <param name="input">Stream to get information from</param>
/// <param name="size">Size of the input stream</param>
/// <param name="hashes">Hashes to include in the information</param>
/// <param name="keepReadOpen">Indicates if the underlying read stream should be kept open</param>
/// <returns>Populated BaseFile object if success, empty one on error</returns> /// <returns>Populated BaseFile object if success, empty one on error</returns>
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 we have no stream
if (input == null) if (input == null)
return new BaseFile(); 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 we want to automatically set the size
if (size == -1) if (size == -1)
size = input.Length; size = input.Length;