diff --git a/SabreTools.DatFiles/DatFile.cs b/SabreTools.DatFiles/DatFile.cs index f0c1739e..c188e5b9 100644 --- a/SabreTools.DatFiles/DatFile.cs +++ b/SabreTools.DatFiles/DatFile.cs @@ -8,6 +8,7 @@ using SabreTools.DatItems; using SabreTools.DatItems.Formats; using SabreTools.Hashing; using SabreTools.IO.Logging; +using SabreTools.Matching.Compare; namespace SabreTools.DatFiles { @@ -723,7 +724,7 @@ namespace SabreTools.DatFiles List output = []; // First we want to make sure the list is in alphabetical order - DatFileTool.Sort(ref datItems, true); + Sort(ref datItems, true); // Now we want to loop through and check names DatItem? lastItem = null; @@ -802,7 +803,7 @@ namespace SabreTools.DatFiles } // One last sort to make sure this is ordered - DatFileTool.Sort(ref output, true); + Sort(ref output, true); return output; } @@ -822,7 +823,7 @@ namespace SabreTools.DatFiles List> output = []; // First we want to make sure the list is in alphabetical order - DatFileTool.SortDB(ref mappings, true); + SortDB(ref mappings, true); // Now we want to loop through and check names DatItem? lastItem = null; @@ -898,7 +899,7 @@ namespace SabreTools.DatFiles } // One last sort to make sure this is ordered - DatFileTool.SortDB(ref output, true); + SortDB(ref output, true); return output; } @@ -972,6 +973,114 @@ namespace SabreTools.DatFiles return false; } + /// + /// 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 static bool Sort(ref List items, bool norename) + { + items.Sort(delegate (DatItem x, DatItem y) + { + try + { + var nc = new NaturalComparer(); + + // If machine names don't match + string? xMachineName = x.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); + string? yMachineName = y.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); + if (xMachineName != yMachineName) + return nc.Compare(xMachineName, yMachineName); + + // If types don't match + string? xType = x.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); + string? yType = y.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); + if (xType != yType) + return xType.AsEnumValue() - yType.AsEnumValue(); + + // If directory names don't match + string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); + string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); + if (xDirectoryName != yDirectoryName) + return nc.Compare(xDirectoryName, yDirectoryName); + + // 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); + } + catch + { + // Absorb the error + return 0; + } + }); + + return true; + } + + /// + /// Sort a list of DatItem objects by SourceID, Game, and Name (in order) + /// + /// List of item ID to DatItem mappings representing the items to be sorted + /// True if files are not renamed, false otherwise + /// True if it sorted correctly, false otherwise + private static bool SortDB(ref List> mappings, bool norename) + { + 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 + string? xMachineName = x.Value.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); + string? yMachineName = y.Value.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); + if (xMachineName != yMachineName) + return nc.Compare(xMachineName, yMachineName); + + // If types don't match + string? xType = x.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); + string? yType = y.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); + if (xType != yType) + return xType.AsEnumValue() - yType.AsEnumValue(); + + // If directory names don't match + string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName() ?? string.Empty)); + string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName() ?? string.Empty)); + if (xDirectoryName != yDirectoryName) + return nc.Compare(xDirectoryName, yDirectoryName); + + // 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); + } + catch + { + // Absorb the error + return 0; + } + }); + + return true; + } + #endregion } } diff --git a/SabreTools.DatFiles/DatFileTool.cs b/SabreTools.DatFiles/DatFileTool.cs index c7cde689..167a3ea2 100644 --- a/SabreTools.DatFiles/DatFileTool.cs +++ b/SabreTools.DatFiles/DatFileTool.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; #if NET40_OR_GREATER || NETCOREAPP using System.Threading.Tasks; #endif @@ -11,7 +10,6 @@ using SabreTools.DatItems; using SabreTools.DatItems.Formats; using SabreTools.IO; using SabreTools.IO.Logging; -using SabreTools.Matching.Compare; namespace SabreTools.DatFiles { @@ -194,114 +192,6 @@ namespace SabreTools.DatFiles return output; } - /// - /// 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 - public static bool Sort(ref List items, bool norename) - { - items.Sort(delegate (DatItem x, DatItem y) - { - try - { - var nc = new NaturalComparer(); - - // If machine names don't match - string? xMachineName = x.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); - string? yMachineName = y.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); - if (xMachineName != yMachineName) - return nc.Compare(xMachineName, yMachineName); - - // If types don't match - string? xType = x.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); - string? yType = y.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); - if (xType != yType) - return xType.AsEnumValue() - yType.AsEnumValue(); - - // If directory names don't match - string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty)); - string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty)); - if (xDirectoryName != yDirectoryName) - return nc.Compare(xDirectoryName, yDirectoryName); - - // 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); - } - catch - { - // Absorb the error - return 0; - } - }); - - return true; - } - - /// - /// Sort a list of DatItem objects by SourceID, Game, and Name (in order) - /// - /// List of item ID to DatItem mappings representing the items to be sorted - /// True if files are not renamed, false otherwise - /// True if it sorted correctly, false otherwise - public static bool SortDB(ref List> mappings, bool norename) - { - 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 - string? xMachineName = x.Value.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); - string? yMachineName = y.Value.GetFieldValue(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey); - if (xMachineName != yMachineName) - return nc.Compare(xMachineName, yMachineName); - - // If types don't match - string? xType = x.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); - string? yType = y.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); - if (xType != yType) - return xType.AsEnumValue() - yType.AsEnumValue(); - - // If directory names don't match - string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Value.GetName() ?? string.Empty)); - string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Value.GetName() ?? string.Empty)); - if (xDirectoryName != yDirectoryName) - return nc.Compare(xDirectoryName, yDirectoryName); - - // 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); - } - catch - { - // Absorb the error - return 0; - } - }); - - return true; - } - #endregion #region SuperDAT