From c3c080852c0564be90ebf6890cff4e124c72635f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 19 May 2025 10:39:32 -0400 Subject: [PATCH] Use consistent sorting in all places --- SabreTools.DatFiles/DatFile.cs | 36 ++++++++++++++----------- SabreTools.DatFiles/ItemDictionary.cs | 26 ++++++++++-------- SabreTools.DatFiles/ItemDictionaryDB.cs | 19 +++++++------ 3 files changed, 47 insertions(+), 34 deletions(-) diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index 076b2976..08eeeda9 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -1130,6 +1130,15 @@ namespace SabreTools.DatFiles { try { + // Compare on source if renaming + if (!norename) + { + int xSourceIndex = x.GetFieldValue(DatItem.SourceKey)?.Index ?? 0; + int ySourceIndex = y.GetFieldValue(DatItem.SourceKey)?.Index ?? 0; + if (xSourceIndex != ySourceIndex) + return xSourceIndex - ySourceIndex; + } + // If machine names don't match string? xMachineName = x.GetMachine()?.GetName(); string? yMachineName = y.GetMachine()?.GetName(); @@ -1151,13 +1160,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 { @@ -1184,7 +1187,16 @@ namespace SabreTools.DatFiles { try { - // TODO: Fix this since DB uses an external map for machines + // TODO: Fix this since DB uses an external map for machines and sources + + // Compare on source if renaming + if (!norename) + { + int xSourceIndex = x.Value.GetFieldValue(DatItem.SourceKey)?.Index ?? 0; + int ySourceIndex = y.Value.GetFieldValue(DatItem.SourceKey)?.Index ?? 0; + if (xSourceIndex != ySourceIndex) + return xSourceIndex - ySourceIndex; + } // If machine names don't match string? xMachineName = x.Value.GetMachine()?.GetName(); @@ -1207,13 +1219,7 @@ namespace SabreTools.DatFiles // If item names don't match string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName() ?? string.Empty)); string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName() ?? string.Empty)); - if (xName != yName) - return nc.Compare(xName, yName); - - // Otherwise, compare on machine or source, depending on the flag - int? xSourceIndex = x.Value.GetFieldValue(DatItem.SourceKey)?.Index; - int? ySourceIndex = y.Value.GetFieldValue(DatItem.SourceKey)?.Index; - return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0); + return nc.Compare(xName, yName); } catch { diff --git a/SabreTools.DatFiles/ItemDictionary.cs b/SabreTools.DatFiles/ItemDictionary.cs index ee4586d5..ff207a45 100644 --- a/SabreTools.DatFiles/ItemDictionary.cs +++ b/SabreTools.DatFiles/ItemDictionary.cs @@ -378,7 +378,7 @@ namespace SabreTools.DatFiles // Sort the dictionary to be consistent _logger.User($"Sorting roms by {bucketBy}"); - PerformSorting(); + PerformSorting(norename); } /// @@ -398,7 +398,7 @@ namespace SabreTools.DatFiles List sortedList = GetItemsForBucket(key); // Sort and merge the list - Sort(ref sortedList); + Sort(ref sortedList, norename: false); sortedList = Merge(sortedList); // Add the list back to the dictionary @@ -784,7 +784,7 @@ namespace SabreTools.DatFiles /// /// Perform inplace sorting of the dictionary /// - private void PerformSorting() + private void PerformSorting(bool norename) { #if NET452_OR_GREATER || NETCOREAPP Parallel.ForEach(SortedKeys, Core.Globals.ParallelOptions, key => @@ -798,7 +798,7 @@ namespace SabreTools.DatFiles List sortedList = GetItemsForBucket(key); // Sort the list of items to be consistent - Sort(ref sortedList); + Sort(ref sortedList, norename); // Add the list back to the dictionary RemoveBucket(key); @@ -814,22 +814,26 @@ 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) + private bool Sort(ref List items, bool norename) { // 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 { - // 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; + // Compare on source if renaming + if (!norename) + { + 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(); diff --git a/SabreTools.DatFiles/ItemDictionaryDB.cs b/SabreTools.DatFiles/ItemDictionaryDB.cs index ab36a016..7a08cdae 100644 --- a/SabreTools.DatFiles/ItemDictionaryDB.cs +++ b/SabreTools.DatFiles/ItemDictionaryDB.cs @@ -1155,11 +1155,20 @@ namespace SabreTools.DatFiles { // Create the comparer extenal to the delegate var nc = new NaturalComparer(); - + itemMappings.Sort(delegate (KeyValuePair x, KeyValuePair y) { try { + // Compare on source if renaming + if (!norename) + { + int xSourceIndex = GetSourceForItem(x.Key).Value?.Index ?? 0; + int ySourceIndex = GetSourceForItem(y.Key).Value?.Index ?? 0; + if (xSourceIndex != ySourceIndex) + return xSourceIndex - ySourceIndex; + } + // Get the machines Machine? xMachine = _machines[_itemToMachineMapping[x.Key]]; Machine? yMachine = _machines[_itemToMachineMapping[y.Key]]; @@ -1185,13 +1194,7 @@ namespace SabreTools.DatFiles // If item names don't match string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName())); string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName())); - if (xName != yName) - return nc.Compare(xName, yName); - - // Otherwise, compare on machine or source, depending on the flag - int? xSourceIndex = GetSourceForItem(x.Key).Value?.Index; - int? ySourceIndex = GetSourceForItem(y.Key).Value?.Index; - return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0); + return nc.Compare(xName, yName); } catch {