mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Fix broken GetKeyDB and related
This commit is contained in:
@@ -282,15 +282,6 @@ namespace SabreTools.DatFiles
|
||||
public List<DatItem> GetDuplicates(DatItem datItem, bool sorted = false)
|
||||
=> Items.GetDuplicates(datItem, sorted);
|
||||
|
||||
/// <summary>
|
||||
/// List all duplicates found in a DAT based on a DatItem
|
||||
/// </summary>
|
||||
/// <param name="datItem">Item to try to match</param>
|
||||
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
||||
/// <returns>List of matched DatItem objects</returns>
|
||||
public Dictionary<long, DatItem> GetDuplicatesDB(DatItem datItem, bool sorted = false)
|
||||
=> ItemsDB.GetDuplicates(datItem, sorted);
|
||||
|
||||
/// <summary>
|
||||
/// List all duplicates found in a DAT based on a DatItem
|
||||
/// </summary>
|
||||
|
||||
@@ -246,7 +246,7 @@ namespace SabreTools.DatFiles
|
||||
}
|
||||
|
||||
// Get the key and add the file
|
||||
key = item.GetKey(ItemKey.Machine);
|
||||
key = GetBucketKey(item, _bucketedBy, lower: true, norename: true);
|
||||
|
||||
// If only adding statistics, we add an empty key for games and then just item stats
|
||||
if (statsOnly)
|
||||
@@ -644,6 +644,26 @@ namespace SabreTools.DatFiles
|
||||
return ItemKey.CRC;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the bucketing key for a given item
|
||||
/// <param name="datItem">The current item</param>
|
||||
/// <param name="bucketBy">ItemKey value representing what key to get</param>
|
||||
/// <param name="lower">True if the key should be lowercased, false otherwise</param>
|
||||
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
||||
/// </summary>
|
||||
private string GetBucketKey(DatItem datItem, ItemKey bucketBy, bool lower, bool norename)
|
||||
{
|
||||
if (datItem == null)
|
||||
return string.Empty;
|
||||
|
||||
// Treat NULL like machine
|
||||
if (bucketBy == ItemKey.NULL)
|
||||
bucketBy = ItemKey.Machine;
|
||||
|
||||
// Get the bucket key
|
||||
return datItem.GetKey(bucketBy, lower, norename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Perform bucketing based on the item key provided
|
||||
/// </summary>
|
||||
|
||||
@@ -698,55 +698,6 @@ namespace SabreTools.DatFiles
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List all duplicates found in a DAT based on a DatItem
|
||||
/// </summary>
|
||||
/// <param name="datItem">Item to try to match</param>
|
||||
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
|
||||
/// <returns>List of matched DatItem objects</returns>
|
||||
internal Dictionary<long, DatItem> GetDuplicates(DatItem datItem, bool sorted = false)
|
||||
{
|
||||
Dictionary<long, DatItem> output = [];
|
||||
|
||||
// Check for an empty rom list first
|
||||
if (DatStatistics.TotalCount == 0)
|
||||
return output;
|
||||
|
||||
// We want to get the proper key for the DatItem
|
||||
string key = SortAndGetKey(datItem, sorted);
|
||||
|
||||
// If the key doesn't exist, return the empty list
|
||||
var roms = GetItemsForBucket(key);
|
||||
if (roms == null || roms.Count == 0)
|
||||
return output;
|
||||
|
||||
// Try to find duplicates
|
||||
Dictionary<long, DatItem> left = [];
|
||||
foreach (var rom in roms)
|
||||
{
|
||||
if (rom.Value.GetBoolFieldValue(DatItem.RemoveKey) == true)
|
||||
continue;
|
||||
|
||||
if (datItem.Equals(rom.Value))
|
||||
{
|
||||
rom.Value.SetFieldValue<bool?>(DatItem.RemoveKey, true);
|
||||
output[rom.Key] = rom.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
left[rom.Key] = rom.Value;
|
||||
}
|
||||
}
|
||||
|
||||
// Add back all roms with the proper flags
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
_buckets.TryAdd(key, [.. output.Keys, .. left.Keys]);
|
||||
#else
|
||||
_buckets[key] = [.. output.Keys, .. left.Keys];
|
||||
#endif
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// List all duplicates found in a DAT based on a DatItem
|
||||
/// </summary>
|
||||
@@ -991,27 +942,18 @@ namespace SabreTools.DatFiles
|
||||
return string.Empty;
|
||||
#endif
|
||||
|
||||
var machine = GetMachineForItem(itemIndex);
|
||||
if (machine.Value == null)
|
||||
return string.Empty;
|
||||
|
||||
var source = GetSourceForItem(itemIndex);
|
||||
|
||||
string sourceKeyPadded = source.Value?.Index.ToString().PadLeft(10, '0') + '-';
|
||||
string machineName = machine.Value.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? "Default";
|
||||
|
||||
string bucketKey = bucketBy switch
|
||||
{
|
||||
// Treat NULL like machine
|
||||
ItemKey.NULL => (norename ? string.Empty : sourceKeyPadded) + machineName,
|
||||
ItemKey.Machine => (norename ? string.Empty : sourceKeyPadded) + machineName,
|
||||
_ => datItem.GetKeyDB(bucketBy, source.Value, lower, norename),
|
||||
};
|
||||
var machine = GetMachineForItem(itemIndex);
|
||||
string machineName = machine.Value?.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? "Default";
|
||||
|
||||
if (lower)
|
||||
bucketKey = bucketKey.ToLowerInvariant();
|
||||
// Treat NULL like machine
|
||||
if (bucketBy == ItemKey.NULL)
|
||||
bucketBy = ItemKey.Machine;
|
||||
|
||||
return bucketKey;
|
||||
// Get the bucket key
|
||||
return datItem.GetKeyDB(bucketBy, machine.Value, source.Value, lower, norename);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1246,7 +1188,7 @@ namespace SabreTools.DatFiles
|
||||
BucketBy(GetBestAvailable(), DedupeType.None);
|
||||
|
||||
// Now that we have the sorted type, we get the proper key
|
||||
return datItem.GetKeyDB(_bucketedBy, null);
|
||||
return datItem.GetKeyDB(_bucketedBy, null, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1262,8 +1204,9 @@ namespace SabreTools.DatFiles
|
||||
BucketBy(GetBestAvailable(), DedupeType.None);
|
||||
|
||||
// Now that we have the sorted type, we get the proper key
|
||||
var machine = GetMachineForItem(datItem.Key);
|
||||
var source = GetSourceForItem(datItem.Key);
|
||||
return datItem.Value.GetKeyDB(_bucketedBy, source.Value);
|
||||
return datItem.Value.GetKeyDB(_bucketedBy, machine.Value, source.Value);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -762,9 +762,8 @@ namespace SabreTools.DatItems.Test
|
||||
machine.SetFieldValue(Models.Metadata.Machine.NameKey, "Machine");
|
||||
|
||||
DatItem datItem = new Blank();
|
||||
datItem.SetFieldValue(DatItem.MachineKey, machine);
|
||||
|
||||
string actual = datItem.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
string actual = datItem.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
@@ -821,7 +820,6 @@ namespace SabreTools.DatItems.Test
|
||||
machine.SetFieldValue(Models.Metadata.Machine.NameKey, "Machine");
|
||||
|
||||
DatItem datItem = new Rom();
|
||||
datItem.SetFieldValue(DatItem.MachineKey, machine);
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.CRCKey, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.MD2Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.MD4Key, "DEADBEEF");
|
||||
@@ -832,7 +830,7 @@ namespace SabreTools.DatItems.Test
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.SHA512Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.SpamSumKey, "BASE64");
|
||||
|
||||
string actual = datItem.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
string actual = datItem.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -356,11 +356,10 @@ namespace SabreTools.DatItems.Test.Formats
|
||||
machine.SetFieldValue(Models.Metadata.Machine.NameKey, "Machine");
|
||||
|
||||
DatItem datItem = new Disk();
|
||||
datItem.SetFieldValue(DatItem.MachineKey, machine);
|
||||
datItem.SetFieldValue(Models.Metadata.Disk.MD5Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Disk.SHA1Key, "DEADBEEF");
|
||||
|
||||
string actual = datItem.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
string actual = datItem.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -467,9 +467,8 @@ namespace SabreTools.DatItems.Test.Formats
|
||||
SHA1 = "DEADBEEF",
|
||||
SHA256 = "DEADBEEF",
|
||||
};
|
||||
datItem.SetFieldValue(DatItem.MachineKey, machine);
|
||||
|
||||
string actual = datItem.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
string actual = datItem.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -436,9 +436,8 @@ namespace SabreTools.DatItems.Test.Formats
|
||||
datItem.SetFieldValue(Models.Metadata.Media.SHA1Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Media.SHA256Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Media.SpamSumKey, "DEADBEEF");
|
||||
datItem.SetFieldValue(DatItem.MachineKey, machine);
|
||||
|
||||
string actual = datItem.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
string actual = datItem.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -703,9 +703,8 @@ namespace SabreTools.DatItems.Test.Formats
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.SHA384Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.SHA512Key, "DEADBEEF");
|
||||
datItem.SetFieldValue(Models.Metadata.Rom.SpamSumKey, "DEADBEEF");
|
||||
datItem.SetFieldValue(DatItem.MachineKey, machine);
|
||||
|
||||
string actual = datItem.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
string actual = datItem.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
Assert.Equal(expected, actual);
|
||||
}
|
||||
|
||||
|
||||
@@ -447,15 +447,19 @@ namespace SabreTools.DatItems
|
||||
/// Get the dictionary key that should be used for a given item and bucketing type
|
||||
/// </summary>
|
||||
/// <param name="bucketedBy">ItemKey value representing what key to get</param>
|
||||
/// <param name="machine">Machine associated with the item for renaming</param>
|
||||
/// <param name="source">Source associated with the item for renaming</param>
|
||||
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
|
||||
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
|
||||
/// <returns>String representing the key to be used for the DatItem</returns>
|
||||
public virtual string GetKeyDB(ItemKey bucketedBy, Source? source, bool lower = true, bool norename = true)
|
||||
public virtual string GetKeyDB(ItemKey bucketedBy, Machine? machine, Source? source, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string key = string.Empty;
|
||||
|
||||
string sourceKeyPadded = source?.Index.ToString().PadLeft(10, '0') + '-';
|
||||
string machineName = machine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) ?? "Default";
|
||||
|
||||
// Now determine what the key should be based on the bucketedBy value
|
||||
switch (bucketedBy)
|
||||
{
|
||||
@@ -464,21 +468,7 @@ namespace SabreTools.DatItems
|
||||
break;
|
||||
|
||||
case ItemKey.Machine:
|
||||
string sourceString = string.Empty;
|
||||
if (!norename && source != null)
|
||||
sourceString = source.Index.ToString().PadLeft(10, '0') + "-";
|
||||
|
||||
// TODO: This will never have a value for DB
|
||||
string machineString = "Default";
|
||||
var machine = GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
if (machine != null)
|
||||
{
|
||||
var machineName = machine.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
||||
if (!string.IsNullOrEmpty(machineName))
|
||||
machineString = machineName!;
|
||||
}
|
||||
|
||||
key = $"{sourceString}{machineString}";
|
||||
key = (norename ? string.Empty : sourceKeyPadded) + machineName;
|
||||
break;
|
||||
|
||||
case ItemKey.MD2:
|
||||
|
||||
@@ -175,7 +175,7 @@ namespace SabreTools.DatItems.Formats
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Source? source, bool lower = true, bool norename = true)
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Machine? machine, Source? source, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string? key;
|
||||
@@ -193,7 +193,7 @@ namespace SabreTools.DatItems.Formats
|
||||
|
||||
// Let the base handle generic stuff
|
||||
default:
|
||||
return base.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
return base.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
}
|
||||
|
||||
// Double and triple check the key for corner cases
|
||||
|
||||
@@ -334,7 +334,7 @@ namespace SabreTools.DatItems.Formats
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Source? source, bool lower = true, bool norename = true)
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Machine? machine, Source? source, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string? key;
|
||||
@@ -360,7 +360,7 @@ namespace SabreTools.DatItems.Formats
|
||||
|
||||
// Let the base handle generic stuff
|
||||
default:
|
||||
return base.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
return base.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
}
|
||||
|
||||
// Double and triple check the key for corner cases
|
||||
|
||||
@@ -127,7 +127,7 @@ namespace SabreTools.DatItems.Formats
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Source? source, bool lower = true, bool norename = true)
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Machine? machine, Source? source, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string? key;
|
||||
@@ -153,7 +153,7 @@ namespace SabreTools.DatItems.Formats
|
||||
|
||||
// Let the base handle generic stuff
|
||||
default:
|
||||
return base.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
return base.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
}
|
||||
|
||||
// Double and triple check the key for corner cases
|
||||
|
||||
@@ -218,7 +218,7 @@ namespace SabreTools.DatItems.Formats
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Source? source, bool lower = true, bool norename = true)
|
||||
public override string GetKeyDB(ItemKey bucketedBy, Machine? machine, Source? source, bool lower = true, bool norename = true)
|
||||
{
|
||||
// Set the output key as the default blank string
|
||||
string? key;
|
||||
@@ -264,7 +264,7 @@ namespace SabreTools.DatItems.Formats
|
||||
|
||||
// Let the base handle generic stuff
|
||||
default:
|
||||
return base.GetKeyDB(bucketedBy, source, lower, norename);
|
||||
return base.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
||||
}
|
||||
|
||||
// Double and triple check the key for corner cases
|
||||
|
||||
Reference in New Issue
Block a user