Fix broken GetKeyDB and related

This commit is contained in:
Matt Nadareski
2025-01-14 14:22:18 -05:00
parent 688e7772e4
commit 222944fae6
13 changed files with 51 additions and 113 deletions

View File

@@ -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>

View File

@@ -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>

View File

@@ -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