mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move some static functionality linked with TreatAsFile
This commit is contained in:
@@ -64,7 +64,7 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve the names in the block
|
// Resolve the names in the block
|
||||||
datItems = DatItem.ResolveNames(datItems);
|
datItems = DatItemTool.ResolveNames(datItems);
|
||||||
|
|
||||||
for (int index = 0; index < datItems.Count; index++)
|
for (int index = 0; index < datItems.Count; index++)
|
||||||
{
|
{
|
||||||
@@ -124,7 +124,7 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve the names in the block
|
// Resolve the names in the block
|
||||||
var items = DatItem.ResolveNamesDB([.. itemsDict]);
|
var items = DatItemTool.ResolveNamesDB([.. itemsDict]);
|
||||||
|
|
||||||
foreach (var kvp in items)
|
foreach (var kvp in items)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -399,7 +399,7 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve the names in the block
|
// Resolve the names in the block
|
||||||
datItems = DatItem.ResolveNames(datItems);
|
datItems = DatItemTool.ResolveNames(datItems);
|
||||||
|
|
||||||
for (int index = 0; index < datItems.Count; index++)
|
for (int index = 0; index < datItems.Count; index++)
|
||||||
{
|
{
|
||||||
@@ -479,7 +479,7 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve the names in the block
|
// Resolve the names in the block
|
||||||
var items = DatItem.ResolveNamesDB([.. itemsDict]);
|
var items = DatItemTool.ResolveNamesDB([.. itemsDict]);
|
||||||
|
|
||||||
foreach (var kvp in items)
|
foreach (var kvp in items)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -227,7 +227,7 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve the names in the block
|
// Resolve the names in the block
|
||||||
datItems = DatItem.ResolveNames(datItems);
|
datItems = DatItemTool.ResolveNames(datItems);
|
||||||
|
|
||||||
for (int index = 0; index < datItems.Count; index++)
|
for (int index = 0; index < datItems.Count; index++)
|
||||||
{
|
{
|
||||||
@@ -308,7 +308,7 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Resolve the names in the block
|
// Resolve the names in the block
|
||||||
var items = DatItem.ResolveNamesDB([.. itemsDict]);
|
var items = DatItemTool.ResolveNamesDB([.. itemsDict]);
|
||||||
|
|
||||||
foreach (var kvp in items)
|
foreach (var kvp in items)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -759,11 +759,11 @@ namespace SabreTools.DatFiles
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Sort the list of items to be consistent
|
// Sort the list of items to be consistent
|
||||||
DatItem.Sort(ref sortedlist, false);
|
DatItemTool.Sort(ref sortedlist, false);
|
||||||
|
|
||||||
// If we're merging the roms, do so
|
// If we're merging the roms, do so
|
||||||
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
|
if (dedupeType == DedupeType.Full || (dedupeType == DedupeType.Game && bucketBy == ItemKey.Machine))
|
||||||
sortedlist = DatItem.Merge(sortedlist);
|
sortedlist = DatItemTool.Merge(sortedlist);
|
||||||
|
|
||||||
// Add the list back to the dictionary
|
// Add the list back to the dictionary
|
||||||
Reset(key);
|
Reset(key);
|
||||||
@@ -794,7 +794,7 @@ namespace SabreTools.DatFiles
|
|||||||
|
|
||||||
// Sort the list of items to be consistent
|
// Sort the list of items to be consistent
|
||||||
if (sortedlist != null)
|
if (sortedlist != null)
|
||||||
DatItem.Sort(ref sortedlist, false);
|
DatItemTool.Sort(ref sortedlist, false);
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
});
|
});
|
||||||
#else
|
#else
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
@@ -9,10 +6,8 @@ using SabreTools.Core;
|
|||||||
using SabreTools.Core.Filter;
|
using SabreTools.Core.Filter;
|
||||||
using SabreTools.Core.Tools;
|
using SabreTools.Core.Tools;
|
||||||
using SabreTools.DatItems.Formats;
|
using SabreTools.DatItems.Formats;
|
||||||
using SabreTools.FileTypes;
|
|
||||||
using SabreTools.Hashing;
|
using SabreTools.Hashing;
|
||||||
using SabreTools.IO.Logging;
|
using SabreTools.IO.Logging;
|
||||||
using SabreTools.Matching.Compare;
|
|
||||||
|
|
||||||
namespace SabreTools.DatItems
|
namespace SabreTools.DatItems
|
||||||
{
|
{
|
||||||
@@ -104,8 +99,6 @@ namespace SabreTools.DatItems
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Instance Methods
|
|
||||||
|
|
||||||
#region Accessors
|
#region Accessors
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -122,44 +115,6 @@ namespace SabreTools.DatItems
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Constructors
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Create a specific type of DatItem to be used based on a BaseFile
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="baseFile">BaseFile containing information to be created</param>
|
|
||||||
/// <param name="asFile">TreatAsFile representing special format scanning</param>
|
|
||||||
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
|
|
||||||
public static DatItem? Create(BaseFile? baseFile, TreatAsFile asFile = 0x00)
|
|
||||||
{
|
|
||||||
return baseFile switch
|
|
||||||
{
|
|
||||||
// Disk
|
|
||||||
#if NET20 || NET35
|
|
||||||
FileTypes.CHD.CHDFile when (asFile & TreatAsFile.CHD) == 0 => new Disk(baseFile),
|
|
||||||
#else
|
|
||||||
FileTypes.CHD.CHDFile when !asFile.HasFlag(TreatAsFile.CHD) => new Disk(baseFile),
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Media
|
|
||||||
#if NET20 || NET35
|
|
||||||
FileTypes.Aaru.AaruFormat when (asFile & TreatAsFile.AaruFormat) == 0 => new Media(baseFile),
|
|
||||||
#else
|
|
||||||
FileTypes.Aaru.AaruFormat when !asFile.HasFlag(TreatAsFile.AaruFormat) => new Media(baseFile),
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Rom
|
|
||||||
BaseArchive => new Rom(baseFile),
|
|
||||||
Folder => null, // Folders cannot be a DatItem
|
|
||||||
BaseFile => new Rom(baseFile),
|
|
||||||
|
|
||||||
// Miscellaneous
|
|
||||||
_ => null,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Cloning Methods
|
#region Cloning Methods
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -510,442 +465,6 @@ namespace SabreTools.DatItems
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#endregion // Instance Methods
|
|
||||||
|
|
||||||
#region Static Methods
|
|
||||||
|
|
||||||
#region Sorting and Merging
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Merge an arbitrary set of ROMs based on the supplied information
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
|
||||||
/// <returns>A List of DatItem objects representing the merged roms</returns>
|
|
||||||
public static List<DatItem> Merge(List<DatItem>? infiles)
|
|
||||||
{
|
|
||||||
// Check for null or blank roms first
|
|
||||||
if (infiles == null || infiles.Count == 0)
|
|
||||||
return [];
|
|
||||||
|
|
||||||
// Create output list
|
|
||||||
List<DatItem> outfiles = [];
|
|
||||||
|
|
||||||
// Then deduplicate them by checking to see if data matches previous saved roms
|
|
||||||
int nodumpCount = 0;
|
|
||||||
for (int f = 0; f < infiles.Count; f++)
|
|
||||||
{
|
|
||||||
DatItem item = infiles[f];
|
|
||||||
|
|
||||||
// If we somehow have a null item, skip
|
|
||||||
if (item == null)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
|
|
||||||
if (item is not Disk && item is not Formats.File && item is not Media && item is not Rom)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// If it's a nodump, add and skip
|
|
||||||
if (item is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
|
||||||
{
|
|
||||||
outfiles.Add(item);
|
|
||||||
nodumpCount++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
else if (item is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
|
||||||
{
|
|
||||||
outfiles.Add(item);
|
|
||||||
nodumpCount++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// If it's the first non-nodump rom in the list, don't touch it
|
|
||||||
else if (outfiles.Count == 0 || outfiles.Count == nodumpCount)
|
|
||||||
{
|
|
||||||
outfiles.Add(item);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the rom is a duplicate
|
|
||||||
DupeType dupetype = 0x00;
|
|
||||||
DatItem saveditem = new Blank();
|
|
||||||
int pos = -1;
|
|
||||||
for (int i = 0; i < outfiles.Count; i++)
|
|
||||||
{
|
|
||||||
DatItem lastrom = outfiles[i];
|
|
||||||
|
|
||||||
// Get the duplicate status
|
|
||||||
dupetype = item.GetDuplicateStatus(lastrom);
|
|
||||||
|
|
||||||
// If it's a duplicate, skip adding it to the output but add any missing information
|
|
||||||
if (dupetype != 0x00)
|
|
||||||
{
|
|
||||||
saveditem = lastrom;
|
|
||||||
pos = i;
|
|
||||||
|
|
||||||
// Disks, Media, and Roms have more information to fill
|
|
||||||
if (item is Disk disk && saveditem is Disk savedDisk)
|
|
||||||
savedDisk.FillMissingInformation(disk);
|
|
||||||
else if (item is Formats.File fileItem && saveditem is Formats.File savedFile)
|
|
||||||
savedFile.FillMissingInformation(fileItem);
|
|
||||||
else if (item is Media media && saveditem is Media savedMedia)
|
|
||||||
savedMedia.FillMissingInformation(media);
|
|
||||||
else if (item is Rom romItem && saveditem is Rom savedRom)
|
|
||||||
savedRom.FillMissingInformation(romItem);
|
|
||||||
|
|
||||||
saveditem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
|
||||||
|
|
||||||
// If the current system has a lower ID than the previous, set the system accordingly
|
|
||||||
if (item.GetFieldValue<Source?>(DatItem.SourceKey)?.Index < saveditem.GetFieldValue<Source?>(DatItem.SourceKey)?.Index)
|
|
||||||
{
|
|
||||||
item.SetFieldValue<Source?>(DatItem.SourceKey, item.GetFieldValue<Source?>(DatItem.SourceKey)!.Clone() as Source);
|
|
||||||
saveditem.CopyMachineInformation(item);
|
|
||||||
saveditem.SetName(item.GetName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the current machine is a child of the new machine, use the new machine instead
|
|
||||||
if (saveditem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey) == item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)
|
|
||||||
|| saveditem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey) == item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey))
|
|
||||||
{
|
|
||||||
saveditem.CopyMachineInformation(item);
|
|
||||||
saveditem.SetName(item.GetName());
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no duplicate is found, add it to the list
|
|
||||||
if (dupetype == 0x00)
|
|
||||||
{
|
|
||||||
outfiles.Add(item);
|
|
||||||
}
|
|
||||||
// Otherwise, if a new rom information is found, add that
|
|
||||||
else
|
|
||||||
{
|
|
||||||
outfiles.RemoveAt(pos);
|
|
||||||
outfiles.Insert(pos, saveditem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Then return the result
|
|
||||||
return outfiles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Resolve name duplicates in an arbitrary set of ROMs based on the supplied information
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
|
||||||
/// <returns>A List of DatItem objects representing the renamed roms</returns>
|
|
||||||
public static List<DatItem> ResolveNames(List<DatItem> infiles)
|
|
||||||
{
|
|
||||||
// Create the output list
|
|
||||||
List<DatItem> output = [];
|
|
||||||
|
|
||||||
// First we want to make sure the list is in alphabetical order
|
|
||||||
Sort(ref infiles, true);
|
|
||||||
|
|
||||||
// Now we want to loop through and check names
|
|
||||||
DatItem? lastItem = null;
|
|
||||||
string? lastrenamed = null;
|
|
||||||
int lastid = 0;
|
|
||||||
for (int i = 0; i < infiles.Count; i++)
|
|
||||||
{
|
|
||||||
DatItem datItem = infiles[i];
|
|
||||||
|
|
||||||
// If we have the first item, we automatically add it
|
|
||||||
if (lastItem == null)
|
|
||||||
{
|
|
||||||
output.Add(datItem);
|
|
||||||
lastItem = datItem;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the last item name, if applicable
|
|
||||||
string lastItemName = lastItem.GetName()
|
|
||||||
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
|
||||||
?? string.Empty;
|
|
||||||
|
|
||||||
// Get the current item name, if applicable
|
|
||||||
string datItemName = datItem.GetName()
|
|
||||||
?? datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
|
||||||
?? string.Empty;
|
|
||||||
|
|
||||||
// If the current item exactly matches the last item, then we don't add it
|
|
||||||
#if NET20 || NET35
|
|
||||||
if ((datItem.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
|
|
||||||
#else
|
|
||||||
if (datItem.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the current name matches the previous name, rename the current item
|
|
||||||
else if (datItemName == lastItemName)
|
|
||||||
{
|
|
||||||
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
|
|
||||||
|
|
||||||
if (datItem is Disk || datItem is Formats.File || datItem is Media || datItem is Rom)
|
|
||||||
{
|
|
||||||
datItemName += GetDuplicateSuffix(datItem);
|
|
||||||
lastrenamed ??= datItemName;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have a conflict with the last renamed item, do the right thing
|
|
||||||
if (datItemName == lastrenamed)
|
|
||||||
{
|
|
||||||
lastrenamed = datItemName;
|
|
||||||
datItemName += (lastid == 0 ? string.Empty : "_" + lastid);
|
|
||||||
lastid++;
|
|
||||||
}
|
|
||||||
// If we have no conflict, then we want to reset the lastrenamed and id
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lastrenamed = null;
|
|
||||||
lastid = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the item name back to the datItem
|
|
||||||
datItem.SetName(datItemName);
|
|
||||||
|
|
||||||
output.Add(datItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we say that we have a valid named file
|
|
||||||
else
|
|
||||||
{
|
|
||||||
output.Add(datItem);
|
|
||||||
lastItem = datItem;
|
|
||||||
lastrenamed = null;
|
|
||||||
lastid = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// One last sort to make sure this is ordered
|
|
||||||
Sort(ref output, true);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Resolve name duplicates in an arbitrary set of ROMs based on the supplied information
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
|
||||||
/// <returns>A List of DatItem objects representing the renamed roms</returns>
|
|
||||||
public static List<KeyValuePair<long, DatItem>> ResolveNamesDB(List<KeyValuePair<long, DatItem>> infiles)
|
|
||||||
{
|
|
||||||
// Create the output dict
|
|
||||||
List<KeyValuePair<long, DatItem>> output = [];
|
|
||||||
|
|
||||||
// First we want to make sure the list is in alphabetical order
|
|
||||||
Sort(ref infiles, true);
|
|
||||||
|
|
||||||
// Now we want to loop through and check names
|
|
||||||
DatItem? lastItem = null;
|
|
||||||
string? lastrenamed = null;
|
|
||||||
int lastid = 0;
|
|
||||||
foreach (var datItem in infiles)
|
|
||||||
{
|
|
||||||
// If we have the first item, we automatically add it
|
|
||||||
if (lastItem == null)
|
|
||||||
{
|
|
||||||
output.Add(datItem);
|
|
||||||
lastItem = datItem.Value;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the last item name, if applicable
|
|
||||||
string lastItemName = lastItem.GetName()
|
|
||||||
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
|
||||||
?? string.Empty;
|
|
||||||
|
|
||||||
// Get the current item name, if applicable
|
|
||||||
string datItemName = datItem.Value.GetName()
|
|
||||||
?? datItem.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
|
||||||
?? string.Empty;
|
|
||||||
|
|
||||||
// If the current item exactly matches the last item, then we don't add it
|
|
||||||
#if NET20 || NET35
|
|
||||||
if ((datItem.Value.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
|
|
||||||
#else
|
|
||||||
if (datItem.Value.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
|
|
||||||
#endif
|
|
||||||
{
|
|
||||||
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the current name matches the previous name, rename the current item
|
|
||||||
else if (datItemName == lastItemName)
|
|
||||||
{
|
|
||||||
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
|
|
||||||
|
|
||||||
if (datItem.Value is Disk || datItem.Value is Formats.File || datItem.Value is Media || datItem.Value is Rom)
|
|
||||||
{
|
|
||||||
datItemName += GetDuplicateSuffix(datItem.Value);
|
|
||||||
lastrenamed ??= datItemName;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we have a conflict with the last renamed item, do the right thing
|
|
||||||
if (datItemName == lastrenamed)
|
|
||||||
{
|
|
||||||
lastrenamed = datItemName;
|
|
||||||
datItemName += (lastid == 0 ? string.Empty : "_" + lastid);
|
|
||||||
lastid++;
|
|
||||||
}
|
|
||||||
// If we have no conflict, then we want to reset the lastrenamed and id
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lastrenamed = null;
|
|
||||||
lastid = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the item name back to the datItem
|
|
||||||
datItem.Value.SetName(datItemName);
|
|
||||||
output.Add(datItem);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we say that we have a valid named file
|
|
||||||
else
|
|
||||||
{
|
|
||||||
output.Add(datItem);
|
|
||||||
lastItem = datItem.Value;
|
|
||||||
lastrenamed = null;
|
|
||||||
lastid = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// One last sort to make sure this is ordered
|
|
||||||
Sort(ref output, true);
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Get duplicate suffix based on the item type
|
|
||||||
/// </summary>
|
|
||||||
private static string GetDuplicateSuffix(DatItem datItem)
|
|
||||||
{
|
|
||||||
return datItem switch
|
|
||||||
{
|
|
||||||
Disk disk => disk.GetDuplicateSuffix(),
|
|
||||||
Formats.File file => file.GetDuplicateSuffix(),
|
|
||||||
Media media => media.GetDuplicateSuffix(),
|
|
||||||
Rom rom => rom.GetDuplicateSuffix(),
|
|
||||||
_ => "_1",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Sort a list of File objects by SourceID, Game, and Name (in order)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="roms">List of File objects representing the roms 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> roms, bool norename)
|
|
||||||
{
|
|
||||||
roms.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 File objects by SourceID, Game, and Name (in order)
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="roms">List of File objects representing the roms 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<KeyValuePair<long, DatItem>> roms, bool norename)
|
|
||||||
{
|
|
||||||
roms.Sort(delegate (KeyValuePair<long, DatItem> x, KeyValuePair<long, DatItem> y)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var nc = new NaturalComparer();
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
#endregion // Static Methods
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
490
SabreTools.DatItems/DatItemTool.cs
Normal file
490
SabreTools.DatItems/DatItemTool.cs
Normal file
@@ -0,0 +1,490 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using SabreTools.Core.Tools;
|
||||||
|
using SabreTools.DatItems.Formats;
|
||||||
|
using SabreTools.FileTypes;
|
||||||
|
using SabreTools.IO.Logging;
|
||||||
|
using SabreTools.Matching.Compare;
|
||||||
|
|
||||||
|
namespace SabreTools.DatItems
|
||||||
|
{
|
||||||
|
public static class DatItemTool
|
||||||
|
{
|
||||||
|
#region Logging
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Static logger for static methods
|
||||||
|
/// </summary>
|
||||||
|
private static readonly Logger staticLogger = new();
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Creation
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a specific type of DatItem to be used based on a BaseFile
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="baseFile">BaseFile containing information to be created</param>
|
||||||
|
/// <param name="asFile">TreatAsFile representing special format scanning</param>
|
||||||
|
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
|
||||||
|
public static DatItem? CreateDatItem(BaseFile? baseFile, TreatAsFile asFile = 0x00)
|
||||||
|
{
|
||||||
|
return baseFile switch
|
||||||
|
{
|
||||||
|
// Disk
|
||||||
|
#if NET20 || NET35
|
||||||
|
FileTypes.CHD.CHDFile when (asFile & TreatAsFile.CHD) == 0 => new Disk(baseFile),
|
||||||
|
#else
|
||||||
|
FileTypes.CHD.CHDFile when !asFile.HasFlag(TreatAsFile.CHD) => new Disk(baseFile),
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Media
|
||||||
|
#if NET20 || NET35
|
||||||
|
FileTypes.Aaru.AaruFormat when (asFile & TreatAsFile.AaruFormat) == 0 => new Media(baseFile),
|
||||||
|
#else
|
||||||
|
FileTypes.Aaru.AaruFormat when !asFile.HasFlag(TreatAsFile.AaruFormat) => new Media(baseFile),
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Rom
|
||||||
|
BaseArchive => new Rom(baseFile),
|
||||||
|
Folder => null, // Folders cannot be a DatItem
|
||||||
|
BaseFile => new Rom(baseFile),
|
||||||
|
|
||||||
|
// Miscellaneous
|
||||||
|
_ => null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Sorting and Merging
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Merge an arbitrary set of DatItems based on the supplied information
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
||||||
|
/// <returns>A List of DatItem objects representing the merged roms</returns>
|
||||||
|
public static List<DatItem> Merge(List<DatItem>? infiles)
|
||||||
|
{
|
||||||
|
// Check for null or blank roms first
|
||||||
|
if (infiles == null || infiles.Count == 0)
|
||||||
|
return [];
|
||||||
|
|
||||||
|
// Create output list
|
||||||
|
List<DatItem> outfiles = [];
|
||||||
|
|
||||||
|
// Then deduplicate them by checking to see if data matches previous saved roms
|
||||||
|
int nodumpCount = 0;
|
||||||
|
for (int f = 0; f < infiles.Count; f++)
|
||||||
|
{
|
||||||
|
DatItem item = infiles[f];
|
||||||
|
|
||||||
|
// If we somehow have a null item, skip
|
||||||
|
if (item == null)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
|
||||||
|
if (item is not Disk && item is not Formats.File && item is not Media && item is not Rom)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// If it's a nodump, add and skip
|
||||||
|
if (item is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
||||||
|
{
|
||||||
|
outfiles.Add(item);
|
||||||
|
nodumpCount++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (item is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
|
||||||
|
{
|
||||||
|
outfiles.Add(item);
|
||||||
|
nodumpCount++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// If it's the first non-nodump rom in the list, don't touch it
|
||||||
|
else if (outfiles.Count == 0 || outfiles.Count == nodumpCount)
|
||||||
|
{
|
||||||
|
outfiles.Add(item);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the rom is a duplicate
|
||||||
|
DupeType dupetype = 0x00;
|
||||||
|
DatItem saveditem = new Blank();
|
||||||
|
int pos = -1;
|
||||||
|
for (int i = 0; i < outfiles.Count; i++)
|
||||||
|
{
|
||||||
|
DatItem lastrom = outfiles[i];
|
||||||
|
|
||||||
|
// Get the duplicate status
|
||||||
|
dupetype = item.GetDuplicateStatus(lastrom);
|
||||||
|
|
||||||
|
// If it's a duplicate, skip adding it to the output but add any missing information
|
||||||
|
if (dupetype != 0x00)
|
||||||
|
{
|
||||||
|
saveditem = lastrom;
|
||||||
|
pos = i;
|
||||||
|
|
||||||
|
// Disks, Media, and Roms have more information to fill
|
||||||
|
if (item is Disk disk && saveditem is Disk savedDisk)
|
||||||
|
savedDisk.FillMissingInformation(disk);
|
||||||
|
else if (item is Formats.File fileItem && saveditem is Formats.File savedFile)
|
||||||
|
savedFile.FillMissingInformation(fileItem);
|
||||||
|
else if (item is Media media && saveditem is Media savedMedia)
|
||||||
|
savedMedia.FillMissingInformation(media);
|
||||||
|
else if (item is Rom romItem && saveditem is Rom savedRom)
|
||||||
|
savedRom.FillMissingInformation(romItem);
|
||||||
|
|
||||||
|
saveditem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
||||||
|
|
||||||
|
// If the current system has a lower ID than the previous, set the system accordingly
|
||||||
|
if (item.GetFieldValue<Source?>(DatItem.SourceKey)?.Index < saveditem.GetFieldValue<Source?>(DatItem.SourceKey)?.Index)
|
||||||
|
{
|
||||||
|
item.SetFieldValue<Source?>(DatItem.SourceKey, item.GetFieldValue<Source?>(DatItem.SourceKey)!.Clone() as Source);
|
||||||
|
saveditem.CopyMachineInformation(item);
|
||||||
|
saveditem.SetName(item.GetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the current machine is a child of the new machine, use the new machine instead
|
||||||
|
if (saveditem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey) == item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey)
|
||||||
|
|| saveditem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.RomOfKey) == item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey))
|
||||||
|
{
|
||||||
|
saveditem.CopyMachineInformation(item);
|
||||||
|
saveditem.SetName(item.GetName());
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no duplicate is found, add it to the list
|
||||||
|
if (dupetype == 0x00)
|
||||||
|
{
|
||||||
|
outfiles.Add(item);
|
||||||
|
}
|
||||||
|
// Otherwise, if a new rom information is found, add that
|
||||||
|
else
|
||||||
|
{
|
||||||
|
outfiles.RemoveAt(pos);
|
||||||
|
outfiles.Insert(pos, saveditem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then return the result
|
||||||
|
return outfiles;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve name duplicates in an arbitrary set of DatItems based on the supplied information
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
||||||
|
/// <returns>A List of DatItem objects representing the renamed roms</returns>
|
||||||
|
public static List<DatItem> ResolveNames(List<DatItem> infiles)
|
||||||
|
{
|
||||||
|
// Create the output list
|
||||||
|
List<DatItem> output = [];
|
||||||
|
|
||||||
|
// First we want to make sure the list is in alphabetical order
|
||||||
|
Sort(ref infiles, true);
|
||||||
|
|
||||||
|
// Now we want to loop through and check names
|
||||||
|
DatItem? lastItem = null;
|
||||||
|
string? lastrenamed = null;
|
||||||
|
int lastid = 0;
|
||||||
|
for (int i = 0; i < infiles.Count; i++)
|
||||||
|
{
|
||||||
|
DatItem datItem = infiles[i];
|
||||||
|
|
||||||
|
// If we have the first item, we automatically add it
|
||||||
|
if (lastItem == null)
|
||||||
|
{
|
||||||
|
output.Add(datItem);
|
||||||
|
lastItem = datItem;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the last item name, if applicable
|
||||||
|
string lastItemName = lastItem.GetName()
|
||||||
|
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
||||||
|
?? string.Empty;
|
||||||
|
|
||||||
|
// Get the current item name, if applicable
|
||||||
|
string datItemName = datItem.GetName()
|
||||||
|
?? datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
||||||
|
?? string.Empty;
|
||||||
|
|
||||||
|
// If the current item exactly matches the last item, then we don't add it
|
||||||
|
#if NET20 || NET35
|
||||||
|
if ((datItem.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
|
||||||
|
#else
|
||||||
|
if (datItem.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the current name matches the previous name, rename the current item
|
||||||
|
else if (datItemName == lastItemName)
|
||||||
|
{
|
||||||
|
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
|
||||||
|
|
||||||
|
if (datItem is Disk || datItem is Formats.File || datItem is Media || datItem is Rom)
|
||||||
|
{
|
||||||
|
datItemName += GetDuplicateSuffix(datItem);
|
||||||
|
lastrenamed ??= datItemName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have a conflict with the last renamed item, do the right thing
|
||||||
|
if (datItemName == lastrenamed)
|
||||||
|
{
|
||||||
|
lastrenamed = datItemName;
|
||||||
|
datItemName += (lastid == 0 ? string.Empty : "_" + lastid);
|
||||||
|
lastid++;
|
||||||
|
}
|
||||||
|
// If we have no conflict, then we want to reset the lastrenamed and id
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lastrenamed = null;
|
||||||
|
lastid = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the item name back to the datItem
|
||||||
|
datItem.SetName(datItemName);
|
||||||
|
|
||||||
|
output.Add(datItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, we say that we have a valid named file
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output.Add(datItem);
|
||||||
|
lastItem = datItem;
|
||||||
|
lastrenamed = null;
|
||||||
|
lastid = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// One last sort to make sure this is ordered
|
||||||
|
Sort(ref output, true);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Resolve name duplicates in an arbitrary set of DatItems based on the supplied information
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="infiles">List of File objects representing the roms to be merged</param>
|
||||||
|
/// <returns>A List of DatItem objects representing the renamed roms</returns>
|
||||||
|
public static List<KeyValuePair<long, DatItem>> ResolveNamesDB(List<KeyValuePair<long, DatItem>> infiles)
|
||||||
|
{
|
||||||
|
// Create the output dict
|
||||||
|
List<KeyValuePair<long, DatItem>> output = [];
|
||||||
|
|
||||||
|
// First we want to make sure the list is in alphabetical order
|
||||||
|
Sort(ref infiles, true);
|
||||||
|
|
||||||
|
// Now we want to loop through and check names
|
||||||
|
DatItem? lastItem = null;
|
||||||
|
string? lastrenamed = null;
|
||||||
|
int lastid = 0;
|
||||||
|
foreach (var datItem in infiles)
|
||||||
|
{
|
||||||
|
// If we have the first item, we automatically add it
|
||||||
|
if (lastItem == null)
|
||||||
|
{
|
||||||
|
output.Add(datItem);
|
||||||
|
lastItem = datItem.Value;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the last item name, if applicable
|
||||||
|
string lastItemName = lastItem.GetName()
|
||||||
|
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
||||||
|
?? string.Empty;
|
||||||
|
|
||||||
|
// Get the current item name, if applicable
|
||||||
|
string datItemName = datItem.Value.GetName()
|
||||||
|
?? datItem.Value.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
|
||||||
|
?? string.Empty;
|
||||||
|
|
||||||
|
// If the current item exactly matches the last item, then we don't add it
|
||||||
|
#if NET20 || NET35
|
||||||
|
if ((datItem.Value.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
|
||||||
|
#else
|
||||||
|
if (datItem.Value.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the current name matches the previous name, rename the current item
|
||||||
|
else if (datItemName == lastItemName)
|
||||||
|
{
|
||||||
|
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
|
||||||
|
|
||||||
|
if (datItem.Value is Disk || datItem.Value is Formats.File || datItem.Value is Media || datItem.Value is Rom)
|
||||||
|
{
|
||||||
|
datItemName += GetDuplicateSuffix(datItem.Value);
|
||||||
|
lastrenamed ??= datItemName;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we have a conflict with the last renamed item, do the right thing
|
||||||
|
if (datItemName == lastrenamed)
|
||||||
|
{
|
||||||
|
lastrenamed = datItemName;
|
||||||
|
datItemName += (lastid == 0 ? string.Empty : "_" + lastid);
|
||||||
|
lastid++;
|
||||||
|
}
|
||||||
|
// If we have no conflict, then we want to reset the lastrenamed and id
|
||||||
|
else
|
||||||
|
{
|
||||||
|
lastrenamed = null;
|
||||||
|
lastid = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the item name back to the datItem
|
||||||
|
datItem.Value.SetName(datItemName);
|
||||||
|
output.Add(datItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, we say that we have a valid named file
|
||||||
|
else
|
||||||
|
{
|
||||||
|
output.Add(datItem);
|
||||||
|
lastItem = datItem.Value;
|
||||||
|
lastrenamed = null;
|
||||||
|
lastid = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// One last sort to make sure this is ordered
|
||||||
|
Sort(ref output, true);
|
||||||
|
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Get duplicate suffix based on the item type
|
||||||
|
/// </summary>
|
||||||
|
private static string GetDuplicateSuffix(DatItem datItem)
|
||||||
|
{
|
||||||
|
return datItem switch
|
||||||
|
{
|
||||||
|
Disk disk => disk.GetDuplicateSuffix(),
|
||||||
|
Formats.File file => file.GetDuplicateSuffix(),
|
||||||
|
Media media => media.GetDuplicateSuffix(),
|
||||||
|
Rom rom => rom.GetDuplicateSuffix(),
|
||||||
|
_ => "_1",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sort a list of File objects by SourceID, Game, and Name (in order)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="roms">List of File objects representing the roms 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> roms, bool norename)
|
||||||
|
{
|
||||||
|
roms.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 File objects by SourceID, Game, and Name (in order)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="roms">List of File objects representing the roms 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<KeyValuePair<long, DatItem>> roms, bool norename)
|
||||||
|
{
|
||||||
|
roms.Sort(delegate (KeyValuePair<long, DatItem> x, KeyValuePair<long, DatItem> y)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var nc = new NaturalComparer();
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -577,7 +577,7 @@ namespace SabreTools.DatTools
|
|||||||
foreach (var key in datFile.Items.Keys)
|
foreach (var key in datFile.Items.Keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
List<DatItem> items = DatItem.Merge(datFile.Items[key]);
|
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||||
|
|
||||||
// If the rom list is empty or null, just skip it
|
// If the rom list is empty or null, just skip it
|
||||||
if (items == null || items.Count == 0)
|
if (items == null || items.Count == 0)
|
||||||
@@ -802,7 +802,7 @@ namespace SabreTools.DatTools
|
|||||||
foreach (var key in datFile.Items.Keys)
|
foreach (var key in datFile.Items.Keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
List<DatItem> items = DatItem.Merge(datFile.Items[key]);
|
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||||
|
|
||||||
// If the rom list is empty or null, just skip it
|
// If the rom list is empty or null, just skip it
|
||||||
if (items == null || items.Count == 0)
|
if (items == null || items.Count == 0)
|
||||||
@@ -1012,7 +1012,7 @@ namespace SabreTools.DatTools
|
|||||||
foreach (var key in datFile.Items.Keys)
|
foreach (var key in datFile.Items.Keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
List<DatItem> items = DatItem.Merge(datFile.Items[key]);
|
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||||
|
|
||||||
// If the rom list is empty or null, just skip it
|
// If the rom list is empty or null, just skip it
|
||||||
if (items == null || items.Count == 0)
|
if (items == null || items.Count == 0)
|
||||||
@@ -1326,7 +1326,7 @@ namespace SabreTools.DatTools
|
|||||||
foreach (var key in datFile.Items.Keys)
|
foreach (var key in datFile.Items.Keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
List<DatItem> items = DatItem.Merge(datFile.Items[key]);
|
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||||
|
|
||||||
// If the rom list is empty or null, just skip it
|
// If the rom list is empty or null, just skip it
|
||||||
if (items == null || items.Count == 0)
|
if (items == null || items.Count == 0)
|
||||||
|
|||||||
@@ -263,7 +263,7 @@ namespace SabreTools.DatTools
|
|||||||
foreach (var baseFile in extracted)
|
foreach (var baseFile in extracted)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
DatItem? datItem = DatItem.Create(baseFile);
|
DatItem? datItem = DatItemTool.CreateDatItem(baseFile);
|
||||||
if (datItem == null)
|
if (datItem == null)
|
||||||
#if NET40_OR_GREATER || NETCOREAPP
|
#if NET40_OR_GREATER || NETCOREAPP
|
||||||
return;
|
return;
|
||||||
@@ -407,7 +407,7 @@ namespace SabreTools.DatTools
|
|||||||
logger.Verbose($"'{Path.GetFileName(item)}' treated like a file");
|
logger.Verbose($"'{Path.GetFileName(item)}' treated like a file");
|
||||||
var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey);
|
var header = datFile.Header.GetStringFieldValue(Models.Metadata.Header.HeaderKey);
|
||||||
BaseFile? baseFile = FileTypeTool.GetInfo(item, header, _hashes);
|
BaseFile? baseFile = FileTypeTool.GetInfo(item, header, _hashes);
|
||||||
DatItem? datItem = DatItem.Create(baseFile, asFile);
|
DatItem? datItem = DatItemTool.CreateDatItem(baseFile, asFile);
|
||||||
if (datItem != null)
|
if (datItem != null)
|
||||||
ProcessFileHelper(datFile, item, datItem, basePath, string.Empty);
|
ProcessFileHelper(datFile, item, datItem, basePath, string.Empty);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -353,7 +353,7 @@ namespace SabreTools.DatTools
|
|||||||
{
|
{
|
||||||
foreach (BaseFile entry in entries)
|
foreach (BaseFile entry in entries)
|
||||||
{
|
{
|
||||||
DatItem? internalDatItem = DatItem.Create(entry);
|
DatItem? internalDatItem = DatItemTool.CreateDatItem(entry);
|
||||||
if (internalDatItem == null)
|
if (internalDatItem == null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|||||||
@@ -904,7 +904,7 @@ namespace SabreTools.DatTools
|
|||||||
foreach (var key in datFile.Items.Keys)
|
foreach (var key in datFile.Items.Keys)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
List<DatItem> items = DatItem.Merge(datFile.Items[key]);
|
List<DatItem> items = DatItemTool.Merge(datFile.Items[key]);
|
||||||
|
|
||||||
// If the rom list is empty or null, just skip it
|
// If the rom list is empty or null, just skip it
|
||||||
if (items == null || items.Count == 0)
|
if (items == null || items.Count == 0)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace SabreTools.Test.DatItems
|
|||||||
public void CreateBaseFileTest(FileType fileType, ItemType? expected)
|
public void CreateBaseFileTest(FileType fileType, ItemType? expected)
|
||||||
{
|
{
|
||||||
var baseFile = CreateBaseFile(fileType);
|
var baseFile = CreateBaseFile(fileType);
|
||||||
var actual = DatItem.Create(baseFile);
|
var actual = DatItemTool.CreateDatItem(baseFile);
|
||||||
Assert.Equal(expected, actual?.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>());
|
Assert.Equal(expected, actual?.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user