mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move Sort methods to DatFile where they are used
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sort a list of DatItem objects by SourceID, Game, and Name (in order)
|
||||
/// </summary>
|
||||
/// <param name="items">List of DatItem objects representing the items to be sorted</param>
|
||||
/// <param name="norename">True if files are not renamed, false otherwise</param>
|
||||
/// <returns>True if it sorted correctly, false otherwise</returns>
|
||||
public static bool Sort(ref List<DatItem> 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<Machine>(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
||||
string? yMachineName = y.GetFieldValue<Machine>(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<ItemType>() - yType.AsEnumValue<ItemType>();
|
||||
|
||||
// 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<Source?>(DatItem.SourceKey)?.Index;
|
||||
int? ySourceIndex = y.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
|
||||
return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Absorb the error
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sort a list of DatItem objects by SourceID, Game, and Name (in order)
|
||||
/// </summary>
|
||||
/// <param name="mappings">List of item ID to DatItem mappings representing the items to be sorted</param>
|
||||
/// <param name="norename">True if files are not renamed, false otherwise</param>
|
||||
/// <returns>True if it sorted correctly, false otherwise</returns>
|
||||
public static bool SortDB(ref List<KeyValuePair<long, DatItem>> mappings, bool norename)
|
||||
{
|
||||
mappings.Sort(delegate (KeyValuePair<long, DatItem> x, KeyValuePair<long, DatItem> 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<Machine>(DatItem.MachineKey)?.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
||||
string? yMachineName = y.Value.GetFieldValue<Machine>(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<ItemType>() - yType.AsEnumValue<ItemType>();
|
||||
|
||||
// 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<Source?>(DatItem.SourceKey)?.Index;
|
||||
int? ySourceIndex = y.Value.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
|
||||
return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Absorb the error
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region SuperDAT
|
||||
|
||||
Reference in New Issue
Block a user