File types don't care about As flags

This commit is contained in:
Matt Nadareski
2025-01-05 21:35:06 -05:00
parent 792ed1e924
commit 19914685a7
8 changed files with 94 additions and 59 deletions

View File

@@ -128,27 +128,30 @@ namespace SabreTools.DatItems
/// Create a specific type of DatItem to be used based on a BaseFile /// Create a specific type of DatItem to be used based on a BaseFile
/// </summary> /// </summary>
/// <param name="baseFile">BaseFile containing information to be created</param> /// <param name="baseFile">BaseFile containing information to be created</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param>
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns> /// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
public static DatItem? Create(BaseFile? baseFile) public static DatItem? Create(BaseFile? baseFile, TreatAsFile asFiles = 0x00)
{ {
return baseFile switch return baseFile switch
{ {
// Disk // Disk
FileTypes.CHD.CHDFile => new Disk(baseFile), #if NET20 || NET35
FileTypes.CHD.CHDFile when (asFiles & TreatAsFile.CHD) == 0 => new Disk(baseFile),
#else
FileTypes.CHD.CHDFile when !asFiles.HasFlag(TreatAsFile.CHD) => new Disk(baseFile),
#endif
// Media // Media
FileTypes.Aaru.AaruFormat => new Media(baseFile), #if NET20 || NET35
FileTypes.Aaru.AaruFormat when (asFiles & TreatAsFile.AaruFormat) == 0 => new Media(baseFile),
#else
FileTypes.Aaru.AaruFormat when !asFiles.HasFlag(TreatAsFile.AaruFormat) => new Media(baseFile),
#endif
// Rom // Rom
FileTypes.Archives.GZipArchive => new Rom(baseFile), BaseArchive => new Rom(baseFile),
FileTypes.Archives.RarArchive => new Rom(baseFile), Folder => null, // Folders cannot be a DatItem
FileTypes.Archives.SevenZipArchive => new Rom(baseFile), BaseFile => new Rom(baseFile),
FileTypes.Archives.TapeArchive => new Rom(baseFile),
FileTypes.Archives.XZArchive => new Rom(baseFile),
FileTypes.Archives.ZipArchive => new Rom(baseFile),
FileTypes.BaseArchive => new Rom(baseFile),
FileTypes.Folder => null, // Folders cannot be a DatItem
FileTypes.BaseFile => new Rom(baseFile),
// Miscellaneous // Miscellaneous
_ => null, _ => null,

View File

@@ -75,8 +75,17 @@ namespace SabreTools.DatItems.Formats
public Disk(BaseFile baseFile) : base() public Disk(BaseFile baseFile) : base()
{ {
SetName(baseFile.Filename); SetName(baseFile.Filename);
SetFieldValue<string?>(Models.Metadata.Disk.MD5Key, baseFile.MD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Disk.SHA1Key, baseFile.SHA1.ToHexString()); if (baseFile is FileTypes.CHD.CHDFile chd)
{
SetFieldValue<string?>(Models.Metadata.Disk.MD5Key, chd.InternalMD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Disk.SHA1Key, chd.InternalSHA1.ToHexString());
}
else
{
SetFieldValue<string?>(Models.Metadata.Disk.MD5Key, baseFile.MD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Disk.SHA1Key, baseFile.SHA1.ToHexString());
}
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00); SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
} }
@@ -132,7 +141,7 @@ namespace SabreTools.DatItems.Formats
public string GetDuplicateSuffix() => _internal.GetDuplicateSuffix(); public string GetDuplicateSuffix() => _internal.GetDuplicateSuffix();
#endregion #endregion
#region Sorting and Merging #region Sorting and Merging
/// <inheritdoc/> /// <inheritdoc/>

View File

@@ -37,10 +37,21 @@ namespace SabreTools.DatItems.Formats
public Media(BaseFile baseFile) : base() public Media(BaseFile baseFile) : base()
{ {
SetName(baseFile.Filename); SetName(baseFile.Filename);
SetFieldValue<string?>(Models.Metadata.Media.MD5Key, baseFile.MD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SHA1Key, baseFile.SHA1.ToHexString()); if (baseFile is FileTypes.Aaru.AaruFormat aif)
SetFieldValue<string?>(Models.Metadata.Media.SHA256Key, baseFile.SHA256.ToHexString()); {
SetFieldValue<string?>(Models.Metadata.Media.SpamSumKey, System.Text.Encoding.UTF8.GetString(baseFile.SpamSum ?? [])); SetFieldValue<string?>(Models.Metadata.Media.MD5Key, aif.InternalMD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SHA1Key, aif.InternalSHA1.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SHA256Key, aif.InternalSHA256.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SpamSumKey, System.Text.Encoding.UTF8.GetString(aif.InternalSpamSum ?? []));
}
else
{
SetFieldValue<string?>(Models.Metadata.Media.MD5Key, baseFile.MD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SHA1Key, baseFile.SHA1.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SHA256Key, baseFile.SHA256.ToHexString());
SetFieldValue<string?>(Models.Metadata.Media.SpamSumKey, System.Text.Encoding.UTF8.GetString(baseFile.SpamSum ?? []));
}
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00); SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
} }

View File

@@ -406,8 +406,8 @@ namespace SabreTools.DatTools
{ {
logger.Verbose($"'{Path.GetFileName(item)}' treated like a file"); logger.Verbose($"'{Path.GetFileName(item)}' treated like a file");
var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey); var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey);
BaseFile? baseFile = FileTypeTool.GetInfo(item, header, _hashes, asFiles); BaseFile? baseFile = FileTypeTool.GetInfo(item, header, _hashes);
DatItem? datItem = DatItem.Create(baseFile); DatItem? datItem = DatItem.Create(baseFile, asFiles);
if (datItem != null) if (datItem != null)
ProcessFileHelper(datFile, item, datItem, basePath, string.Empty); ProcessFileHelper(datFile, item, datItem, basePath, string.Empty);
} }

View File

@@ -324,7 +324,7 @@ namespace SabreTools.DatTools
// If the entries list is null, we encountered an error or have a file and should scan externally // If the entries list is null, we encountered an error or have a file and should scan externally
if (entries == null && System.IO.File.Exists(file)) if (entries == null && System.IO.File.Exists(file))
{ {
BaseFile? internalFileInfo = FileTypeTool.GetInfo(file, hashTypes, asFiles); BaseFile? internalFileInfo = FileTypeTool.GetInfo(file, hashTypes);
// Create the correct DatItem // Create the correct DatItem
DatItem? internalDatItem; DatItem? internalDatItem;

View File

@@ -211,19 +211,15 @@ namespace SabreTools.FileTypes.Aaru
case AaruChecksumAlgorithm.Invalid: case AaruChecksumAlgorithm.Invalid:
break; break;
case AaruChecksumAlgorithm.Md5: case AaruChecksumAlgorithm.Md5:
aif.MD5 = checksumEntry.checksum;
aif.InternalMD5 = checksumEntry.checksum; aif.InternalMD5 = checksumEntry.checksum;
break; break;
case AaruChecksumAlgorithm.Sha1: case AaruChecksumAlgorithm.Sha1:
aif.SHA1 = checksumEntry.checksum;
aif.InternalSHA1 = checksumEntry.checksum; aif.InternalSHA1 = checksumEntry.checksum;
break; break;
case AaruChecksumAlgorithm.Sha256: case AaruChecksumAlgorithm.Sha256:
aif.SHA256 = checksumEntry.checksum;
aif.InternalSHA256 = checksumEntry.checksum; aif.InternalSHA256 = checksumEntry.checksum;
break; break;
case AaruChecksumAlgorithm.SpamSum: case AaruChecksumAlgorithm.SpamSum:
aif.SpamSum = checksumEntry.checksum;
aif.InternalSpamSum = checksumEntry.checksum; aif.InternalSpamSum = checksumEntry.checksum;
break; break;
} }

View File

@@ -54,33 +54,27 @@ namespace SabreTools.FileTypes.CHD
HeaderV1 v1 => new CHDFile HeaderV1 v1 => new CHDFile
{ {
_header = header, _header = header,
MD5 = v1.MD5,
InternalMD5 = v1.MD5, InternalMD5 = v1.MD5,
}, },
HeaderV2 v2 => new CHDFile HeaderV2 v2 => new CHDFile
{ {
_header = header, _header = header,
MD5 = v2.MD5,
InternalMD5 = v2.MD5, InternalMD5 = v2.MD5,
}, },
HeaderV3 v3 => new CHDFile HeaderV3 v3 => new CHDFile
{ {
_header = header, _header = header,
MD5 = v3.MD5,
InternalMD5 = v3.MD5, InternalMD5 = v3.MD5,
SHA1 = v3.SHA1,
InternalSHA1 = v3.SHA1, InternalSHA1 = v3.SHA1,
}, },
HeaderV4 v4 => new CHDFile HeaderV4 v4 => new CHDFile
{ {
_header = header, _header = header,
SHA1 = v4.SHA1,
InternalSHA1 = v4.SHA1, InternalSHA1 = v4.SHA1,
}, },
HeaderV5 v5 => new CHDFile HeaderV5 v5 => new CHDFile
{ {
_header = header, _header = header,
SHA1 = v5.SHA1,
InternalSHA1 = v5.SHA1, InternalSHA1 = v5.SHA1,
}, },
_ => null, _ => null,

View File

@@ -21,17 +21,7 @@ namespace SabreTools.FileTypes
/// <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, empty 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);
/// <summary>
/// Retrieve file information for a single file
/// </summary>
/// <param name="input">Filename to get information from</param>
/// <param name="hashes">Hashes to include in the information</param>
/// <param name="asFiles">TreatAsFiles representing special format scanning</param>
/// <returns>Populated BaseFile object if success, empty on error</returns>
public static BaseFile GetInfo(string input, HashType[] hashes, TreatAsFile asFiles)
=> GetInfo(input, header: null, hashes, asFiles);
/// <summary> /// <summary>
/// Retrieve file information for a single file /// Retrieve file information for a single file
@@ -39,9 +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="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>
/// <returns>Populated BaseFile object if success, empty 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)
{ {
// Add safeguard if file doesn't exist // Add safeguard if file doesn't exist
if (!File.Exists(input)) if (!File.Exists(input))
@@ -52,7 +41,7 @@ namespace SabreTools.FileTypes
// Get input information // Get input information
var fileType = GetFileType(input); var fileType = GetFileType(input);
Stream inputStream = GetInfoStream(input, header); Stream inputStream = GetInfoStream(input, header);
BaseFile? baseFile = GetBaseFile(inputStream, fileType, hashes, asFiles); BaseFile? baseFile = GetBaseFile(inputStream, fileType, hashes);
// Dispose of the input stream // Dispose of the input stream
inputStream.Dispose(); inputStream.Dispose();
@@ -162,21 +151,54 @@ namespace SabreTools.FileTypes
/// <summary> /// <summary>
/// Get the correct base file based on the type and filter options /// Get the correct base file based on the type and filter options
/// </summary> /// </summary>
private static BaseFile? GetBaseFile(Stream inputStream, FileType? fileType, HashType[] hashes, TreatAsFile asFiles) private static BaseFile? GetBaseFile(Stream inputStream, FileType? fileType, HashType[] hashes)
{ {
#if NET20 || NET35 // Get external file information
if (fileType == FileType.AaruFormat && (asFiles & TreatAsFile.AaruFormat) == 0) BaseFile? baseFile = GetInfo(inputStream, hashes, keepReadOpen: true);
return AaruFormat.Create(inputStream);
else if (fileType == FileType.CHD && (asFiles & TreatAsFile.CHD) == 0)
return CHDFile.Create(inputStream);
#else
if (fileType == FileType.AaruFormat && !asFiles.HasFlag(TreatAsFile.AaruFormat))
return AaruFormat.Create(inputStream);
else if (fileType == FileType.CHD && !asFiles.HasFlag(TreatAsFile.CHD))
return CHDFile.Create(inputStream);
#endif
return GetInfo(inputStream, hashes, keepReadOpen: false); // Get internal hashes, if they exist
if (fileType == FileType.AaruFormat)
{
AaruFormat? aif = AaruFormat.Create(inputStream);
if (aif != null)
{
aif.Filename = baseFile.Filename;
aif.Parent = baseFile.Parent;
aif.Date = baseFile.Date;
aif.Size = baseFile.Size;
aif.CRC = baseFile.CRC;
aif.MD5 = baseFile.MD5;
aif.SHA1 = baseFile.SHA1;
aif.SHA256 = baseFile.SHA256;
aif.SHA384 = baseFile.SHA384;
aif.SHA512 = baseFile.SHA512;
aif.SpamSum = baseFile.SpamSum;
return aif;
}
}
else if (fileType == FileType.CHD)
{
CHDFile? chd = CHDFile.Create(inputStream);
if (chd != null)
{
chd.Filename = baseFile.Filename;
chd.Parent = baseFile.Parent;
chd.Date = baseFile.Date;
chd.Size = baseFile.Size;
chd.CRC = baseFile.CRC;
chd.MD5 = baseFile.MD5;
chd.SHA1 = baseFile.SHA1;
chd.SHA256 = baseFile.SHA256;
chd.SHA384 = baseFile.SHA384;
chd.SHA512 = baseFile.SHA512;
chd.SpamSum = baseFile.SpamSum;
return chd;
}
}
return baseFile;
} }
/// <summary> /// <summary>