mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move sorting and merging to DatFileTool
This commit is contained in:
@@ -793,7 +793,7 @@ namespace SabreTools.DatFiles
|
||||
List<DatItem> output = [];
|
||||
|
||||
// First we want to make sure the list is in alphabetical order
|
||||
DatItemTool.Sort(ref items, true);
|
||||
DatFileTool.Sort(ref items, true);
|
||||
|
||||
// Now we want to loop through and check names
|
||||
DatItem? lastItem = null;
|
||||
@@ -874,7 +874,7 @@ namespace SabreTools.DatFiles
|
||||
}
|
||||
|
||||
// One last sort to make sure this is ordered
|
||||
DatItemTool.Sort(ref output, true);
|
||||
DatFileTool.Sort(ref output, true);
|
||||
|
||||
return output;
|
||||
}
|
||||
@@ -890,7 +890,7 @@ namespace SabreTools.DatFiles
|
||||
List<KeyValuePair<long, DatItem>> output = [];
|
||||
|
||||
// First we want to make sure the list is in alphabetical order
|
||||
DatItemTool.SortDB(ref mappings, true);
|
||||
DatFileTool.SortDB(ref mappings, true);
|
||||
|
||||
// Now we want to loop through and check names
|
||||
DatItem? lastItem = null;
|
||||
@@ -968,7 +968,7 @@ namespace SabreTools.DatFiles
|
||||
}
|
||||
|
||||
// One last sort to make sure this is ordered
|
||||
DatItemTool.SortDB(ref output, true);
|
||||
DatFileTool.SortDB(ref output, true);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
@@ -5,9 +5,12 @@ using System.Linq;
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
using System.Threading.Tasks;
|
||||
#endif
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.IO;
|
||||
using SabreTools.IO.Logging;
|
||||
using SabreTools.Matching.Compare;
|
||||
|
||||
namespace SabreTools.DatFiles
|
||||
{
|
||||
@@ -25,6 +28,221 @@ namespace SabreTools.DatFiles
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Merge an arbitrary set of DatItems based on the supplied information
|
||||
/// </summary>
|
||||
/// <param name="items">List of DatItem objects representing the items to be merged</param>
|
||||
/// <returns>A List of DatItem objects representing the merged items</returns>
|
||||
public static List<DatItem> Merge(List<DatItem>? items)
|
||||
{
|
||||
// Check for null or blank inputs first
|
||||
if (items == null || items.Count == 0)
|
||||
return [];
|
||||
|
||||
// Create output list
|
||||
List<DatItem> output = [];
|
||||
|
||||
// Then deduplicate them by checking to see if data matches previous saved roms
|
||||
int nodumpCount = 0;
|
||||
foreach (DatItem datItem in items)
|
||||
{
|
||||
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
|
||||
if (datItem is not Disk && datItem is not DatItems.Formats.File && datItem is not Media && datItem is not Rom)
|
||||
continue;
|
||||
|
||||
// If it's a nodump, add and skip
|
||||
if (datItem is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
||||
{
|
||||
output.Add(datItem);
|
||||
nodumpCount++;
|
||||
continue;
|
||||
}
|
||||
else if (datItem is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
||||
{
|
||||
output.Add(datItem);
|
||||
nodumpCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// If it's the first non-nodump item in the list, don't touch it
|
||||
if (output.Count == nodumpCount)
|
||||
{
|
||||
output.Add(datItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find the index of the first duplicate, if one exists
|
||||
int pos = output.FindIndex(lastItem => datItem.GetDuplicateStatus(lastItem) != 0x00);
|
||||
if (pos < 0)
|
||||
{
|
||||
output.Add(datItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the duplicate item
|
||||
DatItem savedItem = output[pos];
|
||||
DupeType dupetype = datItem.GetDuplicateStatus(savedItem);
|
||||
|
||||
// Disks, File, Media, and Roms have more information to fill
|
||||
if (datItem is Disk diskItem && savedItem is Disk savedDisk)
|
||||
savedDisk.FillMissingInformation(diskItem);
|
||||
else if (datItem is DatItems.Formats.File fileItem && savedItem is DatItems.Formats.File savedFile)
|
||||
savedFile.FillMissingInformation(fileItem);
|
||||
else if (datItem is Media mediaItem && savedItem is Media savedMedia)
|
||||
savedMedia.FillMissingInformation(mediaItem);
|
||||
else if (datItem is Rom romItem && savedItem is Rom savedRom)
|
||||
savedRom.FillMissingInformation(romItem);
|
||||
|
||||
// Set the duplicate type on the saved item
|
||||
savedItem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
||||
|
||||
// Get the sources associated with the items
|
||||
var savedSource = savedItem.GetFieldValue<Source?>(DatItem.SourceKey);
|
||||
var itemSource = datItem.GetFieldValue<Source?>(DatItem.SourceKey);
|
||||
|
||||
// Get the machines associated with the items
|
||||
var savedMachine = savedItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
var itemMachine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
|
||||
// If the current source has a lower ID than the saved, use the saved source
|
||||
if (itemSource?.Index < savedSource?.Index)
|
||||
{
|
||||
datItem.SetFieldValue<Source?>(DatItem.SourceKey, savedSource.Clone() as Source);
|
||||
savedItem.CopyMachineInformation(datItem);
|
||||
savedItem.SetName(datItem.GetName());
|
||||
}
|
||||
|
||||
// If the saved machine is a child of the current machine, use the current machine instead
|
||||
if (savedMachine?.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey) == itemMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey)
|
||||
|| savedMachine?.GetStringFieldValue(Models.Metadata.Machine.RomOfKey) == itemMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey))
|
||||
{
|
||||
savedItem.CopyMachineInformation(datItem);
|
||||
savedItem.SetName(datItem.GetName());
|
||||
}
|
||||
|
||||
// Replace the original item in the list
|
||||
output.RemoveAt(pos);
|
||||
output.Insert(pos, savedItem);
|
||||
}
|
||||
|
||||
// Then return the result
|
||||
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
|
||||
|
||||
/// <summary>
|
||||
/// Apply SuperDAT naming logic to a merged DatFile
|
||||
/// </summary>
|
||||
@@ -158,6 +376,10 @@ namespace SabreTools.DatFiles
|
||||
#endif
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Replacement
|
||||
|
||||
/// <summary>
|
||||
/// Replace item values from the base set represented by the current DAT
|
||||
/// </summary>
|
||||
@@ -374,6 +596,10 @@ namespace SabreTools.DatFiles
|
||||
watch.Stop();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Diffing
|
||||
|
||||
/// <summary>
|
||||
/// Output diffs against a base set represented by the current DAT
|
||||
/// </summary>
|
||||
@@ -576,7 +802,7 @@ namespace SabreTools.DatFiles
|
||||
foreach (var key in datFile.Items.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||
List<DatItem> items = Merge(datFile.Items[key]);
|
||||
|
||||
// If the rom list is empty or null, just skip it
|
||||
if (items == null || items.Count == 0)
|
||||
@@ -801,7 +1027,7 @@ namespace SabreTools.DatFiles
|
||||
foreach (var key in datFile.Items.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||
List<DatItem> items = Merge(datFile.Items[key]);
|
||||
|
||||
// If the rom list is empty or null, just skip it
|
||||
if (items == null || items.Count == 0)
|
||||
@@ -1011,7 +1237,7 @@ namespace SabreTools.DatFiles
|
||||
foreach (var key in datFile.Items.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||
List<DatItem> items = Merge(datFile.Items[key]);
|
||||
|
||||
// If the rom list is empty or null, just skip it
|
||||
if (items == null || items.Count == 0)
|
||||
@@ -1163,6 +1389,10 @@ namespace SabreTools.DatFiles
|
||||
return outerDiffData;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Population
|
||||
|
||||
/// <summary>
|
||||
/// Populate from multiple paths while returning the invividual headers
|
||||
/// </summary>
|
||||
@@ -1325,7 +1555,7 @@ namespace SabreTools.DatFiles
|
||||
foreach (var key in datFile.Items.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||
List<DatItem> items = Merge(datFile.Items[key]);
|
||||
|
||||
// If the rom list is empty or null, just skip it
|
||||
if (items == null || items.Count == 0)
|
||||
@@ -1406,5 +1636,7 @@ namespace SabreTools.DatFiles
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -759,11 +759,11 @@ namespace SabreTools.DatFiles
|
||||
#endif
|
||||
|
||||
// Sort the list of items to be consistent
|
||||
DatItemTool.Sort(ref sortedlist, false);
|
||||
DatFileTool.Sort(ref sortedlist, false);
|
||||
|
||||
// If we're merging the roms, do so
|
||||
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
|
||||
sortedlist = DatItemTool.Merge(sortedlist);
|
||||
sortedlist = DatFileTool.Merge(sortedlist);
|
||||
|
||||
// Add the list back to the dictionary
|
||||
Reset(key);
|
||||
@@ -794,7 +794,7 @@ namespace SabreTools.DatFiles
|
||||
|
||||
// Sort the list of items to be consistent
|
||||
if (sortedlist != null)
|
||||
DatItemTool.Sort(ref sortedlist, false);
|
||||
DatFileTool.Sort(ref sortedlist, false);
|
||||
#if NET40_OR_GREATER || NETCOREAPP
|
||||
});
|
||||
#else
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using SabreTools.Core.Tools;
|
||||
using SabreTools.DatItems.Formats;
|
||||
using SabreTools.FileTypes;
|
||||
@@ -7,7 +5,6 @@ using SabreTools.FileTypes.Aaru;
|
||||
using SabreTools.FileTypes.CHD;
|
||||
using SabreTools.IO.Extensions;
|
||||
using SabreTools.IO.Logging;
|
||||
using SabreTools.Matching.Compare;
|
||||
|
||||
namespace SabreTools.DatItems
|
||||
{
|
||||
@@ -266,218 +263,5 @@ namespace SabreTools.DatItems
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Sorting and Merging
|
||||
|
||||
/// <summary>
|
||||
/// Merge an arbitrary set of DatItems based on the supplied information
|
||||
/// </summary>
|
||||
/// <param name="items">List of DatItem objects representing the items to be merged</param>
|
||||
/// <returns>A List of DatItem objects representing the merged items</returns>
|
||||
public static List<DatItem> Merge(List<DatItem>? items)
|
||||
{
|
||||
// Check for null or blank inputs first
|
||||
if (items == null || items.Count == 0)
|
||||
return [];
|
||||
|
||||
// Create output list
|
||||
List<DatItem> output = [];
|
||||
|
||||
// Then deduplicate them by checking to see if data matches previous saved roms
|
||||
int nodumpCount = 0;
|
||||
foreach (DatItem datItem in items)
|
||||
{
|
||||
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
|
||||
if (datItem is not Disk && datItem is not Formats.File && datItem is not Media && datItem is not Rom)
|
||||
continue;
|
||||
|
||||
// If it's a nodump, add and skip
|
||||
if (datItem is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
||||
{
|
||||
output.Add(datItem);
|
||||
nodumpCount++;
|
||||
continue;
|
||||
}
|
||||
else if (datItem is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
||||
{
|
||||
output.Add(datItem);
|
||||
nodumpCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// If it's the first non-nodump item in the list, don't touch it
|
||||
if (output.Count == nodumpCount)
|
||||
{
|
||||
output.Add(datItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Find the index of the first duplicate, if one exists
|
||||
int pos = output.FindIndex(lastItem => datItem.GetDuplicateStatus(lastItem) != 0x00);
|
||||
if (pos < 0)
|
||||
{
|
||||
output.Add(datItem);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Get the duplicate item
|
||||
DatItem savedItem = output[pos];
|
||||
DupeType dupetype = datItem.GetDuplicateStatus(savedItem);
|
||||
|
||||
// Disks, File, Media, and Roms have more information to fill
|
||||
if (datItem is Disk diskItem && savedItem is Disk savedDisk)
|
||||
savedDisk.FillMissingInformation(diskItem);
|
||||
else if (datItem is Formats.File fileItem && savedItem is Formats.File savedFile)
|
||||
savedFile.FillMissingInformation(fileItem);
|
||||
else if (datItem is Media mediaItem && savedItem is Media savedMedia)
|
||||
savedMedia.FillMissingInformation(mediaItem);
|
||||
else if (datItem is Rom romItem && savedItem is Rom savedRom)
|
||||
savedRom.FillMissingInformation(romItem);
|
||||
|
||||
// Set the duplicate type on the saved item
|
||||
savedItem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
||||
|
||||
// Get the sources associated with the items
|
||||
var savedSource = savedItem.GetFieldValue<Source?>(DatItem.SourceKey);
|
||||
var itemSource = datItem.GetFieldValue<Source?>(DatItem.SourceKey);
|
||||
|
||||
// Get the machines associated with the items
|
||||
var savedMachine = savedItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
var itemMachine = datItem.GetFieldValue<Machine>(DatItem.MachineKey);
|
||||
|
||||
// If the current source has a lower ID than the saved, use the saved source
|
||||
if (itemSource?.Index < savedSource?.Index)
|
||||
{
|
||||
datItem.SetFieldValue<Source?>(DatItem.SourceKey, savedSource.Clone() as Source);
|
||||
savedItem.CopyMachineInformation(datItem);
|
||||
savedItem.SetName(datItem.GetName());
|
||||
}
|
||||
|
||||
// If the saved machine is a child of the current machine, use the current machine instead
|
||||
if (savedMachine?.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey) == itemMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey)
|
||||
|| savedMachine?.GetStringFieldValue(Models.Metadata.Machine.RomOfKey) == itemMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey))
|
||||
{
|
||||
savedItem.CopyMachineInformation(datItem);
|
||||
savedItem.SetName(datItem.GetName());
|
||||
}
|
||||
|
||||
// Replace the original item in the list
|
||||
output.RemoveAt(pos);
|
||||
output.Insert(pos, savedItem);
|
||||
}
|
||||
|
||||
// Then return the result
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -30,7 +30,6 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="SabreTools.IO" Version="1.6.2" />
|
||||
<PackageReference Include="SabreTools.Matching" Version="1.5.1" />
|
||||
<PackageReference Include="SabreTools.Models" Version="1.5.8" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@@ -904,7 +904,7 @@ namespace SabreTools.DatTools
|
||||
foreach (var key in datFile.Items.Keys)
|
||||
#endif
|
||||
{
|
||||
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||
List<DatItem> items = DatFileTool.Merge(datFile.Items[key]);
|
||||
|
||||
// If the rom list is empty or null, just skip it
|
||||
if (items == null || items.Count == 0)
|
||||
|
||||
Reference in New Issue
Block a user