Fix GetDuplicates strange logic

This commit is contained in:
Matt Nadareski
2025-01-14 19:51:59 -05:00
parent 651802a776
commit 0e67113200
2 changed files with 17 additions and 33 deletions

View File

@@ -454,6 +454,7 @@ namespace SabreTools.DatFiles
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param> /// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
/// <returns>List of matched DatItem objects</returns> /// <returns>List of matched DatItem objects</returns>
/// <remarks>This also sets the remove flag on any duplicates found</remarks> /// <remarks>This also sets the remove flag on any duplicates found</remarks>
/// TODO: Figure out if removal should be a flag or just removed entirely
internal List<DatItem> GetDuplicates(DatItem datItem, bool sorted = false) internal List<DatItem> GetDuplicates(DatItem datItem, bool sorted = false)
{ {
// Check for an empty rom list first // Check for an empty rom list first
@@ -464,33 +465,27 @@ namespace SabreTools.DatFiles
string key = SortAndGetKey(datItem, sorted); string key = SortAndGetKey(datItem, sorted);
// Get the items for the current key, if possible // Get the items for the current key, if possible
List<DatItem> roms = GetItemsForBucket(key, filter: false); List<DatItem> items = GetItemsForBucket(key, filter: false);
if (roms.Count == 0) if (items.Count == 0)
return []; return [];
// Remove the current key
RemoveBucket(key);
// Try to find duplicates // Try to find duplicates
List<DatItem> output = []; List<DatItem> output = [];
for (int i = 0; i < roms.Count; i++) foreach (DatItem other in items)
{ {
DatItem other = roms[i]; // Skip items marked for removal
if (other.GetBoolFieldValue(DatItem.RemoveKey) == true) if (other.GetBoolFieldValue(DatItem.RemoveKey) == true)
{
AddItem(key, other);
continue; continue;
}
// Mark duplicates for future removal
if (datItem.Equals(other)) if (datItem.Equals(other))
{ {
other.SetFieldValue<bool?>(DatItem.RemoveKey, true); other.SetFieldValue<bool?>(DatItem.RemoveKey, true);
output.Add(other); output.Add(other);
} }
AddItem(key, other);
} }
// Return any matching items
return output; return output;
} }

View File

@@ -727,49 +727,38 @@ namespace SabreTools.DatFiles
/// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param> /// <param name="sorted">True if the DAT is already sorted accordingly, false otherwise (default)</param>
/// <returns>List of matched DatItem objects</returns> /// <returns>List of matched DatItem objects</returns>
/// <remarks>This also sets the remove flag on any duplicates found</remarks> /// <remarks>This also sets the remove flag on any duplicates found</remarks>
/// TODO: Figure out if removal should be a flag or just removed entirely
internal Dictionary<long, DatItem> GetDuplicates(KeyValuePair<long, DatItem> datItem, bool sorted = false) internal Dictionary<long, DatItem> GetDuplicates(KeyValuePair<long, DatItem> datItem, bool sorted = false)
{ {
Dictionary<long, DatItem> output = [];
// Check for an empty rom list first // Check for an empty rom list first
if (DatStatistics.TotalCount == 0) if (DatStatistics.TotalCount == 0)
return output; return [];
// We want to get the proper key for the DatItem // We want to get the proper key for the DatItem
string key = SortAndGetKey(datItem, sorted); string key = SortAndGetKey(datItem, sorted);
// If the key doesn't exist, return the empty list // If the key doesn't exist, return the empty list
var roms = GetItemsForBucket(key); var items = GetItemsForBucket(key);
if (roms == null || roms.Count == 0) if (items.Count == 0)
return output; return [];
// Try to find duplicates // Try to find duplicates
Dictionary<long, DatItem> left = []; Dictionary<long, DatItem> output = [];
foreach (var rom in roms) foreach (var rom in items)
{ {
// Skip items marked for removal
if (rom.Value.GetBoolFieldValue(DatItem.RemoveKey) == true) if (rom.Value.GetBoolFieldValue(DatItem.RemoveKey) == true)
{
left[rom.Key] = rom.Value;
continue; continue;
}
// Mark duplicates for future removal
if (datItem.Value.Equals(rom.Value)) if (datItem.Value.Equals(rom.Value))
{ {
rom.Value.SetFieldValue<bool?>(DatItem.RemoveKey, true); rom.Value.SetFieldValue<bool?>(DatItem.RemoveKey, true);
output[rom.Key] = rom.Value; output[rom.Key] = rom.Value;
} }
else
{
left[rom.Key] = rom.Value;
}
} }
// Add back all roms with the proper flags // Return any matching items
#if NET40_OR_GREATER || NETCOREAPP
_buckets.TryAdd(key, [.. output.Keys, .. left.Keys]);
#else
_buckets[key] = [.. output.Keys, .. left.Keys];
#endif
return output; return output;
} }