From 243e3507d6de3953211f59f2ebf96d2e9efd1fcf Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 21 Dec 2021 14:20:27 -0800 Subject: [PATCH] Fix and better document DatItem.Sort There was an issue around how items from different sources were handled, in that sources were checked first and not last. This was due to a few assumptions about source IDs and DAT order in general. This may end up causing different output hashes for merged DATs. --- SabreTools.DatItems/DatItem.cs | 39 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/SabreTools.DatItems/DatItem.cs b/SabreTools.DatItems/DatItem.cs index 2e42a7a5..50512d64 100644 --- a/SabreTools.DatItems/DatItem.cs +++ b/SabreTools.DatItems/DatItem.cs @@ -749,25 +749,40 @@ namespace SabreTools.DatItems try { NaturalComparer nc = new NaturalComparer(); - if (x.Source.Index == y.Source.Index) - { - if (x.Machine.Name == y.Machine.Name) - { - if (x.ItemType == y.ItemType) - { - if (Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)) == Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty))) - return nc.Compare(Path.GetFileName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)), Path.GetFileName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty))); - return nc.Compare(Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)), Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty))); + // If machine names match, more refinement is needed + if (x.Machine.Name == y.Machine.Name) + { + // If item types match, more refinement is needed + if (x.ItemType == y.ItemType) + { + string xDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); + string yDirectoryName = Path.GetDirectoryName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); + + // If item directory names match, more refinement is needed + if (xDirectoryName == yDirectoryName) + { + string xName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); + string yName = Path.GetFileName(Utilities.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); + + // If item names match, then compare on machine or source, depending on the flag + if (xName == yName) + return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.Source.Index - y.Source.Index); + + // Otherwise, just sort based on item names + return nc.Compare(xName, yName); } - return x.ItemType - y.ItemType; + // Otherwise, just sort based on directory name + return nc.Compare(xDirectoryName, yDirectoryName); } - return nc.Compare(x.Machine.Name, y.Machine.Name); + // Otherwise, just sort based on item type + return x.ItemType - y.ItemType; } - return (norename ? nc.Compare(x.Machine.Name, y.Machine.Name) : x.Source.Index - y.Source.Index); + // Otherwise, just sort based on machine name + return nc.Compare(x.Machine.Name, y.Machine.Name); } catch {