From 935d99146498c8b5c6093f429a0d0bf7c76f8997 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 May 2025 10:33:57 -0400 Subject: [PATCH] Fix ordering issue by source --- SabreTools.DatFiles/DatFile.cs | 10 ++++++---- SabreTools.DatFiles/ItemDictionary.cs | 25 +++++++++++++------------ SabreTools.DatFiles/ItemDictionaryDB.cs | 5 +++-- SabreTools.DatTools/Diffing.cs | 5 +++-- SabreTools.DatTools/Splitter.cs | 2 +- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index ac0ce347..076b2976 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -1123,12 +1123,13 @@ namespace SabreTools.DatFiles /// True if it sorted correctly, false otherwise private static bool Sort(ref List items, bool norename) { + // Create the comparer extenal to the delegate + var nc = new NaturalComparer(); + items.Sort(delegate (DatItem x, DatItem y) { try { - var nc = new NaturalComparer(); - // If machine names don't match string? xMachineName = x.GetMachine()?.GetName(); string? yMachineName = y.GetMachine()?.GetName(); @@ -1176,12 +1177,13 @@ namespace SabreTools.DatFiles /// True if it sorted correctly, false otherwise private static bool SortDB(ref List> mappings, bool norename) { + // Create the comparer extenal to the delegate + var nc = new NaturalComparer(); + mappings.Sort(delegate (KeyValuePair x, KeyValuePair y) { try { - var nc = new NaturalComparer(); - // TODO: Fix this since DB uses an external map for machines // If machine names don't match diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index 322c3d78..ee4586d5 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -398,7 +398,7 @@ namespace SabreTools.DatFiles List sortedList = GetItemsForBucket(key); // Sort and merge the list - Sort(ref sortedList, false); + Sort(ref sortedList); sortedList = Merge(sortedList); // Add the list back to the dictionary @@ -798,7 +798,7 @@ namespace SabreTools.DatFiles List sortedList = GetItemsForBucket(key); // Sort the list of items to be consistent - Sort(ref sortedList, false); + Sort(ref sortedList); // Add the list back to the dictionary RemoveBucket(key); @@ -814,15 +814,22 @@ namespace SabreTools.DatFiles /// Sort a list of DatItem objects by SourceID, Game, and Name (in order) /// /// List of DatItem objects representing the items to be sorted - /// True if files are not renamed, false otherwise /// True if it sorted correctly, false otherwise - private bool Sort(ref List items, bool norename) + private bool Sort(ref List items) { + // Create the comparer extenal to the delegate + var nc = new NaturalComparer(); + + // Sort by machine, type, item name, and source items.Sort(delegate (DatItem x, DatItem y) { try { - var nc = new NaturalComparer(); + // Compare on source + int xSourceIndex = x.GetFieldValue(DatItem.SourceKey)?.Index ?? 0; + int ySourceIndex = y.GetFieldValue(DatItem.SourceKey)?.Index ?? 0; + if (xSourceIndex != ySourceIndex) + return xSourceIndex - ySourceIndex; // Get the machines Machine? xMachine = x.GetMachine(); @@ -849,13 +856,7 @@ namespace SabreTools.DatFiles // If item names don't match string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); - if (xName != yName) - return nc.Compare(xName, yName); - - // Otherwise, compare on machine or source, depending on the flag - int? xSourceIndex = x.GetFieldValue(DatItem.SourceKey)?.Index; - int? ySourceIndex = y.GetFieldValue(DatItem.SourceKey)?.Index; - return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0); + return nc.Compare(xName, yName); } catch { diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index 780d4025..ab36a016 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -1153,12 +1153,13 @@ namespace SabreTools.DatFiles /// True if it sorted correctly, false otherwise private bool Sort(ref List> itemMappings, bool norename) { + // Create the comparer extenal to the delegate + var nc = new NaturalComparer(); + itemMappings.Sort(delegate (KeyValuePair x, KeyValuePair y) { try { - var nc = new NaturalComparer(); - // Get the machines Machine? xMachine = _machines[_itemToMachineMapping[x.Key]]; Machine? yMachine = _machines[_itemToMachineMapping[y.Key]]; diff --git a/SabreTools.DatTools/Diffing.cs b/SabreTools.DatTools/Diffing.cs index e5003d40..bafdd8ca 100644 --- a/SabreTools.DatTools/Diffing.cs +++ b/SabreTools.DatTools/Diffing.cs @@ -256,7 +256,8 @@ namespace SabreTools.DatTools List outDats = []; // Ensure the current DatFile is sorted optimally - datFile.BucketBy(ItemKey.CRC); + datFile.BucketBy(ItemKey.CRC, norename: false); + datFile.Deduplicate(); // Loop through each of the inputs and get or create a new DatData object InternalStopwatch watch = new("Initializing and filling all output DATs"); @@ -306,7 +307,7 @@ namespace SabreTools.DatTools foreach (var key in datFile.Items.SortedKeys) #endif { - List items = ItemDictionary.Merge(datFile.GetItemsForBucket(key)); + List items = datFile.GetItemsForBucket(key); // If the rom list is empty or null, just skip it if (items == null || items.Count == 0) diff --git a/SabreTools.DatTools/Splitter.cs b/SabreTools.DatTools/Splitter.cs index 14df2328..1c99b7b8 100644 --- a/SabreTools.DatTools/Splitter.cs +++ b/SabreTools.DatTools/Splitter.cs @@ -552,7 +552,7 @@ namespace SabreTools.DatTools /// -1 for a coming before b, 0 for a == b, 1 for a coming after b private static int SplitByLevelSort(string a, string b) { - NaturalComparer nc = new(); + var nc = new NaturalComparer(); int adeep = a.Count(c => c == '/' || c == '\\'); int bdeep = b.Count(c => c == '/' || c == '\\');