Migrate away from Core.Hash

This commit is contained in:
Matt Nadareski
2024-03-04 23:56:05 -05:00
parent 68d40422e4
commit 8f594f09a6
34 changed files with 235 additions and 316 deletions

View File

@@ -12,6 +12,7 @@ using Newtonsoft.Json;
using SabreTools.Core;
using SabreTools.DatItems;
using SabreTools.DatItems.Formats;
using SabreTools.Hashing;
using SabreTools.IO;
using SabreTools.Logging;
using SabreTools.Matching;
@@ -179,7 +180,7 @@ namespace SabreTools.DatFiles
/// Number of hashes for each hash type
/// </summary>
[JsonIgnore, XmlIgnore]
public Dictionary<Hash, long> HashCounts { get; private set; } = [];
public Dictionary<HashType, long> HashCounts { get; private set; } = [];
/// <summary>
/// Number of items for each item status
@@ -347,8 +348,8 @@ namespace SabreTools.DatFiles
case Disk disk:
if (disk.ItemStatus != ItemStatus.Nodump)
{
AddHashCount(Hash.MD5, string.IsNullOrEmpty(disk.MD5) ? 0 : 1);
AddHashCount(Hash.SHA1, string.IsNullOrEmpty(disk.SHA1) ? 0 : 1);
AddHashCount(HashType.MD5, string.IsNullOrEmpty(disk.MD5) ? 0 : 1);
AddHashCount(HashType.SHA1, string.IsNullOrEmpty(disk.SHA1) ? 0 : 1);
}
AddStatusCount(ItemStatus.BadDump, disk.ItemStatus == ItemStatus.BadDump ? 1 : 0);
@@ -357,22 +358,22 @@ namespace SabreTools.DatFiles
AddStatusCount(ItemStatus.Verified, disk.ItemStatus == ItemStatus.Verified ? 1 : 0);
break;
case Media media:
AddHashCount(Hash.MD5, string.IsNullOrEmpty(media.MD5) ? 0 : 1);
AddHashCount(Hash.SHA1, string.IsNullOrEmpty(media.SHA1) ? 0 : 1);
AddHashCount(Hash.SHA256, string.IsNullOrEmpty(media.SHA256) ? 0 : 1);
AddHashCount(Hash.SpamSum, string.IsNullOrEmpty(media.SpamSum) ? 0 : 1);
AddHashCount(HashType.MD5, string.IsNullOrEmpty(media.MD5) ? 0 : 1);
AddHashCount(HashType.SHA1, string.IsNullOrEmpty(media.SHA1) ? 0 : 1);
AddHashCount(HashType.SHA256, string.IsNullOrEmpty(media.SHA256) ? 0 : 1);
AddHashCount(HashType.SpamSum, string.IsNullOrEmpty(media.SpamSum) ? 0 : 1);
break;
case Rom rom:
if (rom.ItemStatus != ItemStatus.Nodump)
{
TotalSize += rom.Size ?? 0;
AddHashCount(Hash.CRC, string.IsNullOrEmpty(rom.CRC) ? 0 : 1);
AddHashCount(Hash.MD5, string.IsNullOrEmpty(rom.MD5) ? 0 : 1);
AddHashCount(Hash.SHA1, string.IsNullOrEmpty(rom.SHA1) ? 0 : 1);
AddHashCount(Hash.SHA256, string.IsNullOrEmpty(rom.SHA256) ? 0 : 1);
AddHashCount(Hash.SHA384, string.IsNullOrEmpty(rom.SHA384) ? 0 : 1);
AddHashCount(Hash.SHA512, string.IsNullOrEmpty(rom.SHA512) ? 0 : 1);
AddHashCount(Hash.SpamSum, string.IsNullOrEmpty(rom.SpamSum) ? 0 : 1);
AddHashCount(HashType.CRC32, string.IsNullOrEmpty(rom.CRC) ? 0 : 1);
AddHashCount(HashType.MD5, string.IsNullOrEmpty(rom.MD5) ? 0 : 1);
AddHashCount(HashType.SHA1, string.IsNullOrEmpty(rom.SHA1) ? 0 : 1);
AddHashCount(HashType.SHA256, string.IsNullOrEmpty(rom.SHA256) ? 0 : 1);
AddHashCount(HashType.SHA384, string.IsNullOrEmpty(rom.SHA384) ? 0 : 1);
AddHashCount(HashType.SHA512, string.IsNullOrEmpty(rom.SHA512) ? 0 : 1);
AddHashCount(HashType.SpamSum, string.IsNullOrEmpty(rom.SpamSum) ? 0 : 1);
}
AddStatusCount(ItemStatus.BadDump, rom.ItemStatus == ItemStatus.BadDump ? 1 : 0);
@@ -655,8 +656,8 @@ namespace SabreTools.DatFiles
case Disk disk:
if (disk.ItemStatus != ItemStatus.Nodump)
{
RemoveHashCount(Hash.MD5, string.IsNullOrEmpty(disk.MD5) ? 0 : 1);
RemoveHashCount(Hash.SHA1, string.IsNullOrEmpty(disk.SHA1) ? 0 : 1);
RemoveHashCount(HashType.MD5, string.IsNullOrEmpty(disk.MD5) ? 0 : 1);
RemoveHashCount(HashType.SHA1, string.IsNullOrEmpty(disk.SHA1) ? 0 : 1);
}
RemoveStatusCount(ItemStatus.BadDump, disk.ItemStatus == ItemStatus.BadDump ? 1 : 0);
@@ -665,22 +666,22 @@ namespace SabreTools.DatFiles
RemoveStatusCount(ItemStatus.Verified, disk.ItemStatus == ItemStatus.Verified ? 1 : 0);
break;
case Media media:
RemoveHashCount(Hash.MD5, string.IsNullOrEmpty(media.MD5) ? 0 : 1);
RemoveHashCount(Hash.SHA1, string.IsNullOrEmpty(media.SHA1) ? 0 : 1);
RemoveHashCount(Hash.SHA256, string.IsNullOrEmpty(media.SHA256) ? 0 : 1);
RemoveHashCount(Hash.SpamSum, string.IsNullOrEmpty(media.SpamSum) ? 0 : 1);
RemoveHashCount(HashType.MD5, string.IsNullOrEmpty(media.MD5) ? 0 : 1);
RemoveHashCount(HashType.SHA1, string.IsNullOrEmpty(media.SHA1) ? 0 : 1);
RemoveHashCount(HashType.SHA256, string.IsNullOrEmpty(media.SHA256) ? 0 : 1);
RemoveHashCount(HashType.SpamSum, string.IsNullOrEmpty(media.SpamSum) ? 0 : 1);
break;
case Rom rom:
if (rom.ItemStatus != ItemStatus.Nodump)
{
TotalSize -= rom.Size ?? 0;
RemoveHashCount(Hash.CRC, string.IsNullOrEmpty(rom.CRC) ? 0 : 1);
RemoveHashCount(Hash.MD5, string.IsNullOrEmpty(rom.MD5) ? 0 : 1);
RemoveHashCount(Hash.SHA1, string.IsNullOrEmpty(rom.SHA1) ? 0 : 1);
RemoveHashCount(Hash.SHA256, string.IsNullOrEmpty(rom.SHA256) ? 0 : 1);
RemoveHashCount(Hash.SHA384, string.IsNullOrEmpty(rom.SHA384) ? 0 : 1);
RemoveHashCount(Hash.SHA512, string.IsNullOrEmpty(rom.SHA512) ? 0 : 1);
RemoveHashCount(Hash.SpamSum, string.IsNullOrEmpty(rom.SpamSum) ? 0 : 1);
RemoveHashCount(HashType.CRC32, string.IsNullOrEmpty(rom.CRC) ? 0 : 1);
RemoveHashCount(HashType.MD5, string.IsNullOrEmpty(rom.MD5) ? 0 : 1);
RemoveHashCount(HashType.SHA1, string.IsNullOrEmpty(rom.SHA1) ? 0 : 1);
RemoveHashCount(HashType.SHA256, string.IsNullOrEmpty(rom.SHA256) ? 0 : 1);
RemoveHashCount(HashType.SHA384, string.IsNullOrEmpty(rom.SHA384) ? 0 : 1);
RemoveHashCount(HashType.SHA512, string.IsNullOrEmpty(rom.SHA512) ? 0 : 1);
RemoveHashCount(HashType.SpamSum, string.IsNullOrEmpty(rom.SpamSum) ? 0 : 1);
}
RemoveStatusCount(ItemStatus.BadDump, rom.ItemStatus == ItemStatus.BadDump ? 1 : 0);
@@ -695,16 +696,16 @@ namespace SabreTools.DatFiles
/// <summary>
/// Get the item count for a given hash type, defaulting to 0 if it does not exist
/// </summary>
/// <param name="hash">Hash type to retrieve</param>
/// <param name="hashType">Hash type to retrieve</param>
/// <returns>The number of items with that hash, if it exists</returns>
public long GetHashCount(Hash hash)
public long GetHashCount(HashType hashType)
{
lock (HashCounts)
{
if (!HashCounts.ContainsKey(hash))
if (!HashCounts.ContainsKey(hashType))
return 0;
return HashCounts[hash];
return HashCounts[hashType];
}
}
@@ -743,36 +744,36 @@ namespace SabreTools.DatFiles
/// <summary>
/// Increment the hash count for a given hash type
/// </summary>
/// <param name="hash">Hash type to increment</param>
/// <param name="hashType">Hash type to increment</param>
/// <param name="interval">Amount to increment by, defaults to 1</param>
private void AddHashCount(Hash hash, long interval = 1)
private void AddHashCount(HashType hashType, long interval = 1)
{
lock (HashCounts)
{
if (!HashCounts.ContainsKey(hash))
HashCounts[hash] = 0;
if (!HashCounts.ContainsKey(hashType))
HashCounts[hashType] = 0;
HashCounts[hash] += interval;
if (HashCounts[hash] < 0)
HashCounts[hash] = 0;
HashCounts[hashType] += interval;
if (HashCounts[hashType] < 0)
HashCounts[hashType] = 0;
}
}
/// <summary>
/// Decrement the hash count for a given hash type
/// </summary>
/// <param name="hash">Hash type to increment</param>
/// <param name="hashType">Hash type to increment</param>
/// <param name="interval">Amount to increment by, defaults to 1</param>
private void RemoveHashCount(Hash hash, long interval = 1)
private void RemoveHashCount(HashType hashType, long interval = 1)
{
lock (HashCounts)
{
if (!HashCounts.ContainsKey(hash))
if (!HashCounts.ContainsKey(hashType))
return;
HashCounts[hash] -= interval;
if (HashCounts[hash] < 0)
HashCounts[hash] = 0;
HashCounts[hashType] -= interval;
if (HashCounts[hashType] < 0)
HashCounts[hashType] = 0;
}
}
@@ -1206,23 +1207,23 @@ CREATE TABLE IF NOT EXISTS groups (
long nodumpCount = GetStatusCount(ItemStatus.Nodump);
// If all items are supposed to have a SHA-512, we bucket by that
if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(Hash.SHA512))
if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(HashType.SHA512))
return ItemKey.SHA512;
// If all items are supposed to have a SHA-384, we bucket by that
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(Hash.SHA384))
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(HashType.SHA384))
return ItemKey.SHA384;
// If all items are supposed to have a SHA-256, we bucket by that
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(Hash.SHA256))
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(HashType.SHA256))
return ItemKey.SHA256;
// If all items are supposed to have a SHA-1, we bucket by that
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(Hash.SHA1))
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(HashType.SHA1))
return ItemKey.SHA1;
// If all items are supposed to have a MD5, we bucket by that
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(Hash.MD5))
else if (diskCount + mediaCount + romCount - nodumpCount == GetHashCount(HashType.MD5))
return ItemKey.MD5;
// Otherwise, we bucket by CRC