Let calling locations handle Stream state

This commit is contained in:
Matt Nadareski
2025-01-06 10:28:12 -05:00
parent 40fcf7924a
commit 3d3d22b5d3
6 changed files with 13 additions and 39 deletions

View File

@@ -500,7 +500,7 @@ namespace SabreTools.DatTools
{ {
// Get the file informations that we will be using // Get the file informations that we will be using
HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
Rom headerless = new(FileTypeTool.GetInfo(transformStream, hashes, keepReadOpen: true)); Rom headerless = new(FileTypeTool.GetInfo(transformStream, hashes));
// 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))
@@ -575,7 +575,7 @@ namespace SabreTools.DatTools
// Get the item from the current file // Get the item from the current file
HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
Rom item = new(FileTypeTool.GetInfo(stream, hashes, keepReadOpen: true)); Rom item = new(FileTypeTool.GetInfo(stream, hashes));
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()));
@@ -633,7 +633,7 @@ namespace SabreTools.DatTools
// Get the item from the current file // Get the item from the current file
HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1]; HashType[] hashes = [HashType.CRC32, HashType.MD5, HashType.SHA1];
var item = new Rom(FileTypeTool.GetInfo(stream, hashes, keepReadOpen: true)); var item = new Rom(FileTypeTool.GetInfo(stream, hashes));
// Create a machine for the current item // Create a machine for the current item
var machine = new Machine(); var machine = new Machine();

View File

@@ -244,7 +244,7 @@ namespace SabreTools.FileTypes.Archives
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);
gzstream!.Dispose(); gzstream?.Dispose();
} }
// Fill in common details and add to the list // Fill in common details and add to the list
@@ -438,7 +438,7 @@ namespace SabreTools.FileTypes.Archives
outDir = Path.GetFullPath(outDir); outDir = Path.GetFullPath(outDir);
// If the base file is null, get the hash information // If the base file is null, get the hash information
baseFile ??= FileTypeTool.GetInfo(stream, _hashTypes, keepReadOpen: true); baseFile ??= FileTypeTool.GetInfo(stream, _hashTypes);
// 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

@@ -282,9 +282,7 @@ namespace SabreTools.FileTypes.Archives
// Otherwise, use the stream directly // Otherwise, use the stream directly
else else
{ {
zipEntryRom = FileTypeTool.GetInfo(readStream, zipEntryRom = FileTypeTool.GetInfo(readStream, _hashTypes);
_hashTypes,
keepReadOpen: true);
} }
// Fill in common details and add to the list // Fill in common details and add to the list

View File

@@ -332,7 +332,7 @@ namespace SabreTools.FileTypes.Archives
outDir = Path.GetFullPath(outDir); outDir = Path.GetFullPath(outDir);
// If the base file is null, get the hash information // If the base file is null, get the hash information
baseFile ??= FileTypeTool.GetInfo(stream, _hashTypes, keepReadOpen: true); baseFile ??= FileTypeTool.GetInfo(stream, _hashTypes);
// 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

@@ -361,9 +361,7 @@ namespace SabreTools.FileTypes.Archives
// Otherwise, use the stream directly // Otherwise, use the stream directly
else else
{ {
zipEntryRom = FileTypeTool.GetInfo(readStream, zipEntryRom = FileTypeTool.GetInfo(readStream, _hashTypes);
_hashTypes,
keepReadOpen: true);
} }
// Fill in common details and add to the list // Fill in common details and add to the list
@@ -417,9 +415,7 @@ namespace SabreTools.FileTypes.Archives
// Otherwise, use the stream directly // Otherwise, use the stream directly
else else
{ {
zipEntryRom = FileTypeTool.GetInfo(readStream, zipEntryRom = FileTypeTool.GetInfo(readStream, _hashTypes);
_hashTypes,
keepReadOpen: true);
} }
// Fill in common details and add to the list // Fill in common details and add to the list

View File

@@ -60,24 +60,13 @@ namespace SabreTools.FileTypes
} }
} }
/// <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>
/// <returns>Populated BaseFile object if success, null on error</returns>
public static BaseFile GetInfo(Stream? input, HashType[] hashes)
=> GetInfo(input, hashes, keepReadOpen: false);
/// <summary> /// <summary>
/// Retrieve file information for a single file /// Retrieve file information for a single file
/// </summary> /// </summary>
/// <param name="input">Stream to get information from</param> /// <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="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, HashType[] hashes, bool keepReadOpen) public static BaseFile GetInfo(Stream? input, HashType[] hashes)
{ {
// If we have no stream // If we have no stream
if (input == null) if (input == null)
@@ -103,17 +92,8 @@ namespace SabreTools.FileTypes
SpamSum = hashDict.ContainsKey(HashType.SpamSum) ? hashDict[HashType.SpamSum].FromHexString() : null, SpamSum = hashDict.ContainsKey(HashType.SpamSum) ? hashDict[HashType.SpamSum].FromHexString() : null,
}; };
// Deal with the input stream // Deal with the input stream and return
if (!keepReadOpen) input.SeekIfPossible();
{
input.Close();
input.Dispose();
}
else
{
input.SeekIfPossible();
}
return baseFile; return baseFile;
} }
catch catch
@@ -148,7 +128,7 @@ namespace SabreTools.FileTypes
private static BaseFile? GetBaseFile(Stream input, FileType? fileType, HashType[] hashes) private static BaseFile? GetBaseFile(Stream input, FileType? fileType, HashType[] hashes)
{ {
// Get external file information // Get external file information
BaseFile? baseFile = GetInfo(input, hashes, keepReadOpen: true); BaseFile? baseFile = GetInfo(input, hashes);
// Get internal hashes, if they exist // Get internal hashes, if they exist
if (fileType == FileType.AaruFormat) if (fileType == FileType.AaruFormat)