Files
SabreTools/SabreTools.DatFiles/Formats/Hashfile.Reader.cs

593 lines
22 KiB
C#
Raw Normal View History

2023-07-28 21:34:34 -04:00
using System;
using System.IO;
using System.Linq;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
namespace SabreTools.DatFiles.Formats
{
/// <summary>
/// Represents parsing of a hashfile such as an SFV, MD5, or SHA-1 file
/// </summary>
internal partial class Hashfile : DatFile
{
/// <inheritdoc/>
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{
try
{
// Deserialize the input file
2023-09-11 01:20:21 -04:00
var hashfile = new Serialization.Files.Hashfile().Deserialize(filename, _hash);
2023-07-28 21:34:34 -04:00
// Convert items
switch (_hash)
{
2023-09-11 10:27:17 -04:00
case Serialization.Hash.CRC:
2023-07-28 21:34:34 -04:00
ConvertSFV(hashfile?.SFV, filename, indexId, statsOnly);
break;
2023-09-11 10:27:17 -04:00
case Serialization.Hash.MD5:
2023-07-28 21:34:34 -04:00
ConvertMD5(hashfile?.MD5, filename, indexId, statsOnly);
break;
2023-09-11 10:27:17 -04:00
case Serialization.Hash.SHA1:
2023-07-28 21:34:34 -04:00
ConvertSHA1(hashfile?.SHA1, filename, indexId, statsOnly);
break;
2023-09-11 10:27:17 -04:00
case Serialization.Hash.SHA256:
2023-07-28 21:34:34 -04:00
ConvertSHA256(hashfile?.SHA256, filename, indexId, statsOnly);
break;
2023-09-11 10:27:17 -04:00
case Serialization.Hash.SHA384:
2023-07-28 21:34:34 -04:00
ConvertSHA384(hashfile?.SHA384, filename, indexId, statsOnly);
break;
2023-09-11 10:27:17 -04:00
case Serialization.Hash.SHA512:
2023-07-28 21:34:34 -04:00
ConvertSHA512(hashfile?.SHA512, filename, indexId, statsOnly);
break;
2023-09-11 10:27:17 -04:00
case Serialization.Hash.SpamSum:
2023-07-28 21:34:34 -04:00
ConvertSpamSum(hashfile?.SpamSum, filename, indexId, statsOnly);
break;
}
}
catch (Exception ex) when (!throwOnError)
{
string message = $"'{filename}' - An error occurred during parsing";
logger.Error(ex, message);
}
}
#region Converters
/// <summary>
/// Create a machine from the filename
/// </summary>
/// <param name="filename">Filename to derive from</param>
/// <returns>Filled machine and new filename on success, null on error</returns>
private static (Machine?, string?) DeriveMachine(string? filename)
2023-07-28 21:34:34 -04:00
{
// If the filename is missing, we can't do anything
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(filename))
2023-07-28 21:34:34 -04:00
return (null, null);
string machineName = Path.GetFileNameWithoutExtension(filename);
if (filename.Contains('/'))
{
2024-02-28 22:54:56 -05:00
string[] split = filename!.Split('/');
2023-07-28 21:34:34 -04:00
machineName = split[0];
2024-02-28 22:54:56 -05:00
filename = filename.Substring(machineName.Length + 1);
2023-07-28 21:34:34 -04:00
}
else if (filename.Contains('\\'))
{
2024-02-28 22:54:56 -05:00
string[] split = filename!.Split('\\');
2023-07-28 21:34:34 -04:00
machineName = split[0];
2024-02-28 22:54:56 -05:00
filename = filename.Substring(machineName.Length + 1);
2023-07-28 21:34:34 -04:00
}
var machine = new Machine { Name = machineName };
return (machine, filename);
}
/// <summary>
/// Derive the item type from the filename
/// </summary>
/// <param name="filename">Filename to derive from</param>
/// <returns>ItemType representing the item (Rom by default), ItemType.NULL on error</returns>
private static ItemType DeriveItemType(string? filename)
2023-07-28 21:34:34 -04:00
{
// If the filename is missing, we can't do anything
2024-02-28 22:54:56 -05:00
if (string.IsNullOrEmpty(filename))
2023-07-28 21:34:34 -04:00
return ItemType.NULL;
// If we end in the CHD extension
2024-02-28 22:54:56 -05:00
if (filename!.EndsWith(".chd", StringComparison.OrdinalIgnoreCase))
2023-07-28 21:34:34 -04:00
return ItemType.Disk;
// If we end in an Aaruformat extension
if (filename.EndsWith(".aaru", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".aaruf", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".aaruformat", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".aif", StringComparison.OrdinalIgnoreCase)
|| filename.EndsWith(".dicf", StringComparison.OrdinalIgnoreCase))
{
return ItemType.Media;
}
// Everything else is assumed to be a generic item
return ItemType.Rom;
}
/// <summary>
/// Convert SFV information
/// </summary>
/// <param name="sfvs">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSFV(Models.Hashfile.SFV[]? sfvs, string filename, int indexId, bool statsOnly)
{
// If the sfv array is missing, we can't do anything
if (sfvs == null || !sfvs.Any())
return;
// Loop through and add the items
foreach (var sfv in sfvs)
{
// Skip if we have an invalid item
if (sfv == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(sfv.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(sfv.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Disk: // Should not happen with CRC32 hashes
case ItemType.Media: // Should not happen with CRC32 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
CRC = sfv.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert MD5 information
/// </summary>
/// <param name="md5s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertMD5(Models.Hashfile.MD5[]? md5s, string filename, int indexId, bool statsOnly)
{
// If the md5 array is missing, we can't do anything
if (md5s == null || !md5s.Any())
return;
// Loop through and add the items
foreach (var md5 in md5s)
{
// Skip if we have an invalid item
if (md5 == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(md5.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(md5.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Disk:
var disk = new Disk
{
Name = itemName,
MD5 = md5.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
disk.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(disk, statsOnly);
break;
case ItemType.Media:
var media = new Media
{
Name = itemName,
MD5 = md5.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
media.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(media, statsOnly);
break;
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
MD5 = md5.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA1 information
/// </summary>
/// <param name="sha1s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA1(Models.Hashfile.SHA1[]? sha1s, string filename, int indexId, bool statsOnly)
{
// If the sha1 array is missing, we can't do anything
if (sha1s == null || !sha1s.Any())
return;
// Loop through and add the items
foreach (var sha1 in sha1s)
{
// Skip if we have an invalid item
if (sha1 == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(sha1.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(sha1.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Disk:
var disk = new Disk
{
Name = itemName,
SHA1 = sha1.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
disk.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(disk, statsOnly);
break;
case ItemType.Media:
var media = new Media
{
Name = itemName,
SHA1 = sha1.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
media.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(media, statsOnly);
break;
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA1 = sha1.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA256 information
/// </summary>
/// <param name="sha256s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA256(Models.Hashfile.SHA256[]? sha256s, string filename, int indexId, bool statsOnly)
{
// If the sha256 array is missing, we can't do anything
if (sha256s == null || !sha256s.Any())
return;
// Loop through and add the items
foreach (var sha256 in sha256s)
{
// Skip if we have an invalid item
if (sha256 == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(sha256.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(sha256.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Media:
var media = new Media
{
Name = itemName,
SHA256 = sha256.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
media.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(media, statsOnly);
break;
case ItemType.Disk: // Should not happen with SHA-256 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA256 = sha256.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA384 information
/// </summary>
/// <param name="sha384s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA384(Models.Hashfile.SHA384[]? sha384s, string filename, int indexId, bool statsOnly)
{
// If the sha384 array is missing, we can't do anything
if (sha384s == null || !sha384s.Any())
return;
// Loop through and add the items
foreach (var sha384 in sha384s)
{
// Skip if we have an invalid item
if (sha384 == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(sha384.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(sha384.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Disk: // Should not happen with SHA-384 hashes
case ItemType.Media: // Should not happen with SHA-384 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA384 = sha384.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SHA512 information
/// </summary>
/// <param name="sha512s">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSHA512(Models.Hashfile.SHA512[]? sha512s, string filename, int indexId, bool statsOnly)
{
// If the sha512 array is missing, we can't do anything
if (sha512s == null || !sha512s.Any())
return;
// Loop through and add the items
foreach (var sha512 in sha512s)
{
// Skip if we have an invalid item
if (sha512 == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(sha512.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(sha512.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Disk: // Should not happen with SHA-512 hashes
case ItemType.Media: // Should not happen with SHA-512 hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SHA512 = sha512.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
/// <summary>
/// Convert SpamSum information
/// </summary>
/// <param name="spamsums">Array of deserialized models to convert</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
private void ConvertSpamSum(Models.Hashfile.SpamSum[]? spamsums, string filename, int indexId, bool statsOnly)
{
// If the spamsum array is missing, we can't do anything
if (spamsums == null || !spamsums.Any())
return;
// Loop through and add the items
foreach (var spamsum in spamsums)
{
// Skip if we have an invalid item
if (spamsum == null)
continue;
2023-07-28 21:34:34 -04:00
// Get the item type
ItemType itemType = DeriveItemType(spamsum.File);
if (itemType == ItemType.NULL)
continue;
(var machine, string? itemName) = DeriveMachine(spamsum.File);
2023-07-28 21:34:34 -04:00
switch (itemType)
{
case ItemType.Media:
var media = new Media
{
Name = itemName,
SpamSum = spamsum.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
media.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(media, statsOnly);
break;
case ItemType.Disk: // Should not happen with SpamSum fuzzy hashes
case ItemType.Rom:
var rom = new Rom
{
Name = itemName,
Size = null,
SpamSum = spamsum.Hash,
2023-07-30 23:05:45 -04:00
2023-07-28 21:34:34 -04:00
Source = new Source
{
Index = indexId,
Name = filename,
},
};
2023-07-30 23:05:45 -04:00
rom.CopyMachineInformation(machine);
2023-07-28 21:34:34 -04:00
ParseAddHelper(rom, statsOnly);
break;
default:
continue;
}
}
}
#endregion
}
}