Rewrite sort to get machine name from dictionary

This commit is contained in:
Matt Nadareski
2024-03-13 10:27:22 -04:00
parent 5983c4ea74
commit bff92b1c30

View File

@@ -412,31 +412,29 @@ namespace SabreTools.DatFiles
#endif #endif
} }
// TODO: Rewrite this to get the machine name from the mapping dictionary and not from the item itself
/// <summary> /// <summary>
/// Sort a list of File objects by SourceID, Game, and Name (in order) /// Sort a list of File objects by SourceID, Game, and Name (in order)
/// </summary> /// </summary>
/// <param name="itemMappings">List of File objects representing the roms to be sorted</param> /// <param name="itemMappings">List of File objects representing the roms to be sorted</param>
/// <param name="norename">True if files are not renamed, false otherwise</param> /// <param name="norename">True if files are not renamed, false otherwise</param>
/// <returns>True if it sorted correctly, false otherwise</returns> /// <returns>True if it sorted correctly, false otherwise</returns>
private static bool Sort(ref List<(long, DatItem)> itemMappings, bool norename) private bool Sort(ref List<(long, DatItem)> itemMappings, bool norename)
{ {
itemMappings.Sort(delegate ((long, DatItem) x, (long, DatItem) y) itemMappings.Sort(delegate ((long, DatItem) x, (long, DatItem) y)
{ {
try try
{ {
NaturalComparer nc = new(); var nc = new NaturalComparer();
// Get all required values // Get all required values
string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Item2.GetName() ?? string.Empty)); string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.Item2.GetName() ?? string.Empty));
string? xMachineName = x.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey); string? xMachineName = _machines[_itemToMachineMapping[x.Item1]].GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Item2.GetName() ?? string.Empty)); string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.Item2.GetName() ?? string.Empty));
int? xSourceIndex = x.Item2.GetFieldValue<Source?>(DatItem.SourceKey)?.Index; int? xSourceIndex = x.Item2.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
string? xType = x.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); string? xType = x.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Item2.GetName() ?? string.Empty)); string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.Item2.GetName() ?? string.Empty));
string? yMachineName = y.Item2.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey); string? yMachineName = _machines[_itemToMachineMapping[y.Item1]].GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Item2.GetName() ?? string.Empty)); string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.Item2.GetName() ?? string.Empty));
int? ySourceIndex = y.Item2.GetFieldValue<Source?>(DatItem.SourceKey)?.Index; int? ySourceIndex = y.Item2.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
string? yType = y.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey); string? yType = y.Item2.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
@@ -479,6 +477,8 @@ namespace SabreTools.DatFiles
return true; return true;
} }
// TODO: Write a method that deduplicates items based on any of the fields selected
#endregion #endregion
} }
} }