Files
SabreTools/SabreTools.DatItems/DatItem.cs

1068 lines
42 KiB
C#
Raw Normal View History

2016-09-19 20:52:55 -07:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
2020-09-07 14:47:27 -07:00
using System.Xml.Serialization;
using Newtonsoft.Json;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Filter;
using SabreTools.Core.Tools;
2021-02-02 10:23:43 -08:00
using SabreTools.DatItems.Formats;
2020-12-08 14:53:49 -08:00
using SabreTools.FileTypes;
2024-03-06 11:23:22 -05:00
using SabreTools.Hashing;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging;
2024-10-19 11:43:11 -04:00
using SabreTools.Matching.Compare;
2016-09-19 20:52:55 -07:00
2020-12-08 15:15:41 -08:00
namespace SabreTools.DatItems
2016-09-19 20:52:55 -07:00
{
/// <summary>
/// Base class for all items included in a set
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("datitem"), XmlRoot("datitem")]
2020-09-07 14:47:27 -07:00
[XmlInclude(typeof(Adjuster))]
[XmlInclude(typeof(Analog))]
[XmlInclude(typeof(Archive))]
[XmlInclude(typeof(BiosSet))]
[XmlInclude(typeof(Blank))]
[XmlInclude(typeof(Chip))]
[XmlInclude(typeof(Condition))]
[XmlInclude(typeof(Configuration))]
[XmlInclude(typeof(ConfLocation))]
[XmlInclude(typeof(ConfSetting))]
2020-09-07 14:47:27 -07:00
[XmlInclude(typeof(Control))]
[XmlInclude(typeof(DataArea))]
[XmlInclude(typeof(Device))]
2024-03-10 20:39:54 -04:00
[XmlInclude(typeof(DeviceRef))]
[XmlInclude(typeof(DipLocation))]
2020-09-07 14:47:27 -07:00
[XmlInclude(typeof(DipSwitch))]
[XmlInclude(typeof(DipValue))]
2020-09-07 14:47:27 -07:00
[XmlInclude(typeof(Disk))]
[XmlInclude(typeof(DiskArea))]
[XmlInclude(typeof(Display))]
[XmlInclude(typeof(Driver))]
[XmlInclude(typeof(Extension))]
[XmlInclude(typeof(Feature))]
[XmlInclude(typeof(Info))]
[XmlInclude(typeof(Input))]
[XmlInclude(typeof(Instance))]
[XmlInclude(typeof(Media))]
[XmlInclude(typeof(Part))]
[XmlInclude(typeof(PartFeature))]
[XmlInclude(typeof(Port))]
[XmlInclude(typeof(RamOption))]
[XmlInclude(typeof(Release))]
[XmlInclude(typeof(Rom))]
[XmlInclude(typeof(Sample))]
2024-03-10 20:39:54 -04:00
[XmlInclude(typeof(SharedFeat))]
2020-09-07 14:47:27 -07:00
[XmlInclude(typeof(Slot))]
[XmlInclude(typeof(SlotOption))]
[XmlInclude(typeof(SoftwareList))]
[XmlInclude(typeof(Sound))]
public abstract class DatItem : ModelBackedItem<Models.Metadata.DatItem>, IEquatable<DatItem>, IComparable<DatItem>, ICloneable
{
2024-03-10 16:49:07 -04:00
#region Constants
/// <summary>
2024-03-10 16:49:07 -04:00
/// Duplicate type when compared to another item
/// </summary>
2024-03-10 16:49:07 -04:00
public const string DupeTypeKey = "DUPETYPE";
/// <summary>
2024-03-10 16:49:07 -04:00
/// Machine associated with the item
/// </summary>
2024-03-10 16:49:07 -04:00
public const string MachineKey = "MACHINE";
2023-08-14 22:33:05 -04:00
/// <summary>
2024-03-10 16:49:07 -04:00
/// Flag if item should be removed
2023-08-14 22:33:05 -04:00
/// </summary>
2024-03-10 16:49:07 -04:00
public const string RemoveKey = "REMOVE";
/// <summary>
2024-03-10 16:49:07 -04:00
/// Source information
/// </summary>
2024-03-10 16:49:07 -04:00
public const string SourceKey = "SOURCE";
#endregion
2024-03-10 16:49:07 -04:00
#region Fields
2024-03-10 20:45:54 -04:00
/// <summary>
/// Item type for the object
/// </summary>
protected abstract ItemType ItemType { get; }
#endregion
#region Logging
/// <summary>
/// Static logger for static methods
/// </summary>
[JsonIgnore, XmlIgnore]
2024-03-10 17:05:44 -04:00
protected static readonly Logger staticLogger = new();
#endregion
#region Instance Methods
#region Accessors
2020-09-02 12:19:12 -07:00
/// <summary>
/// Gets the name to use for a DatItem
/// </summary>
/// <returns>Name if available, null otherwise</returns>
public virtual string? GetName() => null;
2020-09-02 12:19:12 -07:00
/// <summary>
/// Sets the name to use for a DatItem
/// </summary>
/// <param name="name">Name to set for the item</param>
public virtual void SetName(string? name) { }
#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>
/// <returns>DatItem of the specific internal type that corresponds to the inputs</returns>
2024-02-28 19:19:50 -05:00
public static DatItem? Create(BaseFile? baseFile)
{
2024-07-19 15:35:23 -04:00
return baseFile switch
{
2024-07-19 15:35:23 -04:00
// Disk
FileTypes.CHD.CHDFile => new Disk(baseFile),
// Media
FileTypes.Aaru.AaruFormat => new Media(baseFile),
// Rom
FileTypes.Archives.GZipArchive => new Rom(baseFile),
FileTypes.Archives.RarArchive => new Rom(baseFile),
FileTypes.Archives.SevenZipArchive => new Rom(baseFile),
FileTypes.Archives.TapeArchive => new Rom(baseFile),
FileTypes.Archives.XZArchive => new Rom(baseFile),
FileTypes.Archives.ZipArchive => new Rom(baseFile),
2024-10-24 02:32:21 -04:00
FileTypes.BaseArchive => new Rom(baseFile),
FileTypes.Folder => null, // Folders cannot be a DatItem
FileTypes.BaseFile => new Rom(baseFile),
2024-07-19 15:35:23 -04:00
// Miscellaneous
2024-10-24 02:32:21 -04:00
_ => null,
};
}
#endregion
#region Cloning Methods
/// <summary>
/// Clone the DatItem
/// </summary>
/// <returns>Clone of the DatItem</returns>
public abstract object Clone();
/// <summary>
/// Copy all machine information over in one shot
/// </summary>
/// <param name="item">Existing item to copy information from</param>
public void CopyMachineInformation(DatItem item)
{
2024-10-24 05:11:17 -04:00
var machine = item.GetFieldValue<Machine>(DatItem.MachineKey);
CopyMachineInformation(machine);
}
/// <summary>
/// Copy all machine information over in one shot
/// </summary>
/// <param name="machine">Existing machine to copy information from</param>
public void CopyMachineInformation(Machine? machine)
{
if (machine == null)
return;
2024-02-28 19:19:50 -05:00
if (machine.Clone() is Machine cloned)
2024-03-10 16:49:07 -04:00
SetFieldValue<Machine>(DatItem.MachineKey, cloned);
}
#endregion
#region Comparision Methods
2022-11-03 12:23:10 -07:00
/// <inheritdoc/>
public int CompareTo(DatItem? other)
{
try
{
if (GetName() == other?.GetName())
2020-09-02 15:07:25 -07:00
return Equals(other) ? 0 : 1;
return string.Compare(GetName(), other?.GetName());
}
catch
{
return 1;
}
}
/// <summary>
/// Determine if an item is a duplicate using partial matching logic
/// </summary>
/// <param name="other">DatItem to use as a baseline</param>
2022-11-03 12:23:10 -07:00
/// <returns>True if the items are duplicates, false otherwise</returns>
2023-08-14 22:33:05 -04:00
public virtual bool Equals(DatItem? other)
{
// If we don't have a matched type, return false
2024-03-11 16:26:28 -04:00
if (GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>() != other?.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>())
2023-08-14 22:33:05 -04:00
return false;
// Compare the internal models
return _internal.EqualTo(other._internal);
}
/// <summary>
/// Return the duplicate status of two items
/// </summary>
/// <param name="lastItem">DatItem to check against</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public DupeType GetDuplicateStatus(DatItem? lastItem)
{
DupeType output = 0x00;
// If we don't have a duplicate at all, return none
2020-08-17 17:28:32 -07:00
if (!Equals(lastItem))
return output;
// If the duplicate is external already or should be, set it
2024-12-28 20:15:32 -05:00
#if NET20 || NET35
2024-03-10 21:03:53 -04:00
if ((lastItem.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.External) != 0
|| lastItem?.GetFieldValue<Source?>(DatItem.SourceKey)?.Index != GetFieldValue<Source?>(DatItem.SourceKey)?.Index)
2024-02-28 22:07:00 -05:00
#else
2024-03-10 21:03:53 -04:00
if (lastItem.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.External)
|| lastItem?.GetFieldValue<Source?>(DatItem.SourceKey)?.Index != GetFieldValue<Source?>(DatItem.SourceKey)?.Index)
2024-02-28 22:07:00 -05:00
#endif
{
2024-03-10 21:03:53 -04:00
var currentMachine = GetFieldValue<Machine>(DatItem.MachineKey);
var lastMachine = lastItem?.GetFieldValue<Machine>(DatItem.MachineKey);
if (lastMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) == currentMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) && lastItem?.GetName() == GetName())
output = DupeType.External | DupeType.All;
else
output = DupeType.External | DupeType.Hash;
}
// Otherwise, it's considered an internal dupe
else
{
2024-03-10 21:03:53 -04:00
var currentMachine = GetFieldValue<Machine>(DatItem.MachineKey);
var lastMachine = lastItem?.GetFieldValue<Machine>(DatItem.MachineKey);
if (lastMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) == currentMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) && lastItem?.GetName() == GetName())
output = DupeType.Internal | DupeType.All;
else
output = DupeType.Internal | DupeType.Hash;
}
return output;
}
/// <summary>
/// Return the duplicate status of two items
/// </summary>
/// <param name="source">Source associated with this item</param>
/// <param name="lastItem">DatItem to check against</param>
/// <param name="lastSource">Source associated with the last item</param>
/// <returns>The DupeType corresponding to the relationship between the two</returns>
public DupeType GetDuplicateStatus(Source? source, DatItem? lastItem, Source? lastSource)
{
DupeType output = 0x00;
// If we don't have a duplicate at all, return none
if (!Equals(lastItem))
return output;
// If the duplicate is external already or should be, set it
2024-12-28 20:15:32 -05:00
#if NET20 || NET35
if ((lastItem.GetFieldValue<DupeType>(DatItem.DupeTypeKey) & DupeType.External) != 0
|| lastSource?.Index != source?.Index)
#else
if (lastItem.GetFieldValue<DupeType>(DatItem.DupeTypeKey).HasFlag(DupeType.External)
|| lastSource?.Index != source?.Index)
#endif
{
var currentMachine = GetFieldValue<Machine>(DatItem.MachineKey);
var lastMachine = lastItem?.GetFieldValue<Machine>(DatItem.MachineKey);
if (lastMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) == currentMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) && lastItem?.GetName() == GetName())
output = DupeType.External | DupeType.All;
else
output = DupeType.External | DupeType.Hash;
}
// Otherwise, it's considered an internal dupe
else
{
var currentMachine = GetFieldValue<Machine>(DatItem.MachineKey);
var lastMachine = lastItem?.GetFieldValue<Machine>(DatItem.MachineKey);
if (lastMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) == currentMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) && lastItem?.GetName() == GetName())
output = DupeType.Internal | DupeType.All;
else
output = DupeType.Internal | DupeType.Hash;
}
return output;
}
#endregion
2024-03-05 01:42:42 -05:00
#region Manipulation
2024-03-05 02:56:50 -05:00
/// <summary>
/// Runs a filter and determines if it passes or not
/// </summary>
/// <param name="filterRunner">Filter runner to use for checking</param>
/// <returns>True if the item passes the filter, false otherwise</returns>
public bool PassesFilter(FilterRunner filterRunner)
{
2024-03-10 16:49:07 -04:00
if (!GetFieldValue<Machine>(DatItem.MachineKey)!.PassesFilter(filterRunner))
2024-03-05 02:56:50 -05:00
return false;
return filterRunner.Run(_internal);
}
2024-03-05 01:42:42 -05:00
/// <summary>
/// Remove a field from the DatItem
/// </summary>
2024-03-05 16:37:52 -05:00
/// <param name="fieldName">Field to remove</param>
2024-03-05 01:42:42 -05:00
/// <returns>True if the removal was successful, false otherwise</returns>
2024-03-05 20:07:38 -05:00
public bool RemoveField(string? fieldName)
=> FieldManipulator.RemoveField(_internal, fieldName);
/// <summary>
/// Replace a field from another DatItem
/// </summary>
/// <param name="other">DatItem to replace field from</param>
/// <param name="fieldName">Field to replace</param>
/// <returns>True if the replacement was successful, false otherwise</returns>
public bool ReplaceField(DatItem? other, string? fieldName)
=> FieldManipulator.ReplaceField(other?._internal, _internal, fieldName);
2024-03-05 01:42:42 -05:00
2024-03-05 16:37:52 -05:00
/// <summary>
/// Set a field in the DatItem from a mapping string
/// </summary>
/// <param name="fieldName">Field to set</param>
/// <param name="value">String representing the value to set</param>
/// <returns>True if the removal was successful, false otherwise</returns>
/// <remarks>This only performs minimal validation before setting</remarks>
2024-03-05 20:07:38 -05:00
public bool SetField(string? fieldName, string value)
=> FieldManipulator.SetField(_internal, fieldName, value);
2024-03-05 16:37:52 -05:00
2024-03-05 01:42:42 -05:00
#endregion
#region Sorting and Merging
/// <summary>
2020-07-15 10:47:13 -07:00
/// Get the dictionary key that should be used for a given item and bucketing type
/// </summary>
2020-12-14 15:31:28 -08:00
/// <param name="bucketedBy">ItemKey value representing what key to get</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// <returns>String representing the key to be used for the DatItem</returns>
2020-12-14 15:31:28 -08:00
public virtual string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
{
// Set the output key as the default blank string
string key = string.Empty;
2020-07-15 10:47:13 -07:00
// Now determine what the key should be based on the bucketedBy value
switch (bucketedBy)
{
2020-12-14 15:31:28 -08:00
case ItemKey.CRC:
2024-11-13 03:55:33 -05:00
key = ZeroHash.CRC32Str;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.Machine:
2024-10-24 05:11:17 -04:00
string sourceString = string.Empty;
if (!norename)
{
var source = GetFieldValue<Source?>(DatItem.SourceKey);
if (source != null)
sourceString = source.Index.ToString().PadLeft(10, '0') + "-";
}
string machineString = "Default";
var machine = GetFieldValue<Machine>(DatItem.MachineKey);
if (machine != null)
{
var machineName = machine.GetStringFieldValue(Models.Metadata.Machine.NameKey);
if (!string.IsNullOrEmpty(machineName))
machineString = machineName!;
}
key = $"{sourceString}{machineString}";
if (lower)
key = key.ToLowerInvariant();
break;
case ItemKey.MD5:
2024-11-13 03:55:33 -05:00
key = ZeroHash.MD5Str;
break;
case ItemKey.SHA1:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA1Str;
break;
case ItemKey.SHA256:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA256Str;
break;
case ItemKey.SHA384:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA384Str;
break;
case ItemKey.SHA512:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA512Str;
break;
case ItemKey.SpamSum:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SpamSumStr;
break;
}
// Double and triple check the key for corner cases
key ??= string.Empty;
return key;
}
/// <summary>
/// Get the dictionary key that should be used for a given item and bucketing type
/// </summary>
/// <param name="bucketedBy">ItemKey value representing what key to get</param>
/// <param name="source">Source associated with the item for renaming</param>
/// <param name="lower">True if the key should be lowercased (default), false otherwise</param>
/// <param name="norename">True if games should only be compared on game and file name, false if system and source are counted</param>
/// <returns>String representing the key to be used for the DatItem</returns>
public virtual string GetKey(ItemKey bucketedBy, Source? source, bool lower = true, bool norename = true)
{
// Set the output key as the default blank string
string key = string.Empty;
// Now determine what the key should be based on the bucketedBy value
switch (bucketedBy)
{
case ItemKey.CRC:
2024-11-13 03:55:33 -05:00
key = ZeroHash.CRC32Str;
break;
case ItemKey.Machine:
2024-10-24 05:11:17 -04:00
string sourceString = string.Empty;
if (!norename && source != null)
sourceString = source.Index.ToString().PadLeft(10, '0') + "-";
string machineString = "Default";
var machine = GetFieldValue<Machine>(DatItem.MachineKey);
if (machine != null)
{
var machineName = machine.GetStringFieldValue(Models.Metadata.Machine.NameKey);
if (!string.IsNullOrEmpty(machineName))
machineString = machineName!;
}
key = $"{sourceString}{machineString}";
if (lower)
key = key.ToLowerInvariant();
break;
2020-12-14 15:31:28 -08:00
case ItemKey.MD5:
2024-11-13 03:55:33 -05:00
key = ZeroHash.MD5Str;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA1:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA1Str;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA256:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA256Str;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA384:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA384Str;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA512:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SHA512Str;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SpamSum:
2024-11-13 03:55:33 -05:00
key = ZeroHash.SpamSumStr;
break;
}
// Double and triple check the key for corner cases
key ??= string.Empty;
return key;
}
#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)
2024-02-28 19:19:50 -05:00
return [];
// Create output list
List<DatItem> outfiles = [];
// Then deduplicate them by checking to see if data matches previous saved roms
int nodumpCount = 0;
2020-07-19 13:42:07 -07:00
for (int f = 0; f < infiles.Count; f++)
{
2024-03-10 16:49:07 -04:00
DatItem item = infiles[f];
2020-07-19 13:42:07 -07:00
2020-10-12 11:11:40 -07:00
// If we somehow have a null item, skip
2024-03-10 16:49:07 -04:00
if (item == null)
2020-10-12 11:11:40 -07:00
continue;
2023-04-07 16:13:15 -04:00
// If we don't have a Disk, File, Media, or Rom, we skip checking for duplicates
2024-03-10 16:49:07 -04:00
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
2024-03-11 16:26:28 -04:00
if (item is Rom rom && rom.GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
{
2024-03-10 16:49:07 -04:00
outfiles.Add(item);
nodumpCount++;
continue;
}
2024-03-11 16:26:28 -04:00
else if (item is Disk disk && disk.GetStringFieldValue(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>() == ItemStatus.Nodump)
{
2024-03-10 16:49:07 -04:00
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)
{
2024-03-10 16:49:07 -04:00
outfiles.Add(item);
continue;
}
// Check if the rom is a duplicate
DupeType dupetype = 0x00;
2020-08-17 17:28:32 -07:00
DatItem saveditem = new Blank();
int pos = -1;
for (int i = 0; i < outfiles.Count; i++)
{
DatItem lastrom = outfiles[i];
// Get the duplicate status
2024-03-10 16:49:07 -04:00
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
2024-03-10 16:49:07 -04:00
if (item is Disk disk && saveditem is Disk savedDisk)
savedDisk.FillMissingInformation(disk);
2024-03-10 16:49:07 -04:00
else if (item is Formats.File fileItem && saveditem is Formats.File savedFile)
savedFile.FillMissingInformation(fileItem);
2024-03-10 16:49:07 -04:00
else if (item is Media media && saveditem is Media savedMedia)
savedMedia.FillMissingInformation(media);
2024-03-10 16:49:07 -04:00
else if (item is Rom romItem && saveditem is Rom savedRom)
savedRom.FillMissingInformation(romItem);
2024-03-10 16:49:07 -04:00
saveditem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
// If the current system has a lower ID than the previous, set the system accordingly
2024-03-10 16:49:07 -04:00
if (item.GetFieldValue<Source?>(DatItem.SourceKey)?.Index < saveditem.GetFieldValue<Source?>(DatItem.SourceKey)?.Index)
{
2024-03-10 16:49:07 -04:00
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))
{
2024-03-10 16:49:07 -04:00
saveditem.CopyMachineInformation(item);
saveditem.SetName(item.GetName());
}
break;
}
}
// If no duplicate is found, add it to the list
if (dupetype == 0x00)
{
2024-03-10 16:49:07 -04:00
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;
}
2020-09-02 12:19:12 -07:00
// Get the last item name, if applicable
2024-10-24 05:11:17 -04:00
string lastItemName = lastItem.GetName()
?? lastItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty;
2020-09-02 12:19:12 -07:00
// Get the current item name, if applicable
2024-10-24 05:11:17 -04:00
string datItemName = datItem.GetName()
?? datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue()
?? string.Empty;
2020-09-02 12:19:12 -07:00
// If the current item exactly matches the last item, then we don't add it
2024-12-28 20:15:32 -05:00
#if NET20 || NET35
2024-02-28 22:07:00 -05:00
if ((datItem.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
#else
if (datItem.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
2024-02-28 22:07:00 -05:00
#endif
{
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
continue;
}
// If the current name matches the previous name, rename the current item
2020-09-02 12:19:12 -07:00
else if (datItemName == lastItemName)
{
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
2024-03-10 16:49:07 -04:00
if (datItem is Disk || datItem is Formats.File || datItem is Media || datItem is Rom)
{
2020-09-02 12:19:12 -07:00
datItemName += GetDuplicateSuffix(datItem);
lastrenamed ??= datItemName;
}
// If we have a conflict with the last renamed item, do the right thing
2020-09-02 12:19:12 -07:00
if (datItemName == lastrenamed)
{
2020-09-02 12:19:12 -07:00
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;
}
2020-09-02 12:19:12 -07:00
// Set the item name back to the datItem
datItem.SetName(datItemName);
2020-09-02 12:19:12 -07:00
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
2024-12-28 20:15:32 -05:00
#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>
2020-08-28 01:13:55 -07:00
/// 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
{
2024-03-13 10:40:30 -04:00
var nc = new NaturalComparer();
2024-03-13 10:45:08 -04:00
// If machine names don't match
2024-03-13 10:40:30 -04:00
string? xMachineName = x.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
string? yMachineName = y.GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey);
2024-03-13 10:43:05 -04:00
if (xMachineName != yMachineName)
return nc.Compare(xMachineName, yMachineName);
2024-03-13 10:43:05 -04:00
// If types don't match
2024-03-13 10:45:08 -04:00
string? xType = x.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
string? yType = y.GetStringFieldValue(Models.Metadata.DatItem.TypeKey);
2024-03-13 10:43:05 -04:00
if (xType != yType)
2024-03-13 10:40:30 -04:00
return xType.AsEnumValue<ItemType>() - yType.AsEnumValue<ItemType>();
2024-03-13 10:43:05 -04:00
// If directory names don't match
2024-03-13 10:45:08 -04:00
string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
2024-03-13 10:43:05 -04:00
if (xDirectoryName != yDirectoryName)
return nc.Compare(xDirectoryName, yDirectoryName);
// If item names don't match
2024-03-13 10:45:08 -04:00
string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
2024-03-13 10:43:05 -04:00
if (xName != yName)
return nc.Compare(xName, yName);
// Otherwise, compare on machine or source, depending on the flag
2024-03-13 10:45:08 -04:00
int? xSourceIndex = x.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
int? ySourceIndex = y.GetFieldValue<Source?>(DatItem.SourceKey)?.Index;
2024-03-13 10:43:05 -04:00
return (norename ? nc.Compare(xMachineName, yMachineName) : (xSourceIndex - ySourceIndex) ?? 0);
}
2020-12-08 13:23:59 -08:00
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
}
2024-03-10 17:14:36 -04:00
/// <summary>
/// Base class for all items included in a set that are backed by an internal model
/// </summary>
public abstract class DatItem<T> : DatItem, IEquatable<DatItem<T>>, IComparable<DatItem<T>>, ICloneable where T : Models.Metadata.DatItem
{
#region Fields
2024-03-10 20:39:54 -04:00
/// <summary>
/// Key for accessing the item name, if it exists
/// </summary>
protected abstract string? NameKey { get; }
#endregion
#region Constructors
/// <summary>
/// Create a default, empty object
/// </summary>
public DatItem()
{
_internal = Activator.CreateInstance<T>();
SetName(string.Empty);
SetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey, ItemType);
SetFieldValue<Machine>(DatItem.MachineKey, new Machine());
}
/// <summary>
/// Create an object from the internal model
/// </summary>
public DatItem(T item)
{
_internal = item;
SetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey, ItemType);
SetFieldValue<Machine>(DatItem.MachineKey, new Machine());
}
#endregion
2024-03-10 20:39:54 -04:00
#region Accessors
/// <inheritdoc/>
public override string? GetName()
{
if (NameKey != null)
return GetStringFieldValue(NameKey);
2024-03-10 20:39:54 -04:00
return null;
}
/// <inheritdoc/>
public override void SetName(string? name)
{
if (NameKey != null)
SetFieldValue(NameKey, name);
}
2024-03-11 01:51:17 -04:00
/// <summary>
/// Get a clone of the current internal model
/// </summary>
public T GetInternalClone() => (_internal.Clone() as T)!;
2024-03-10 20:39:54 -04:00
#endregion
2024-03-10 17:14:36 -04:00
#region Cloning Methods
/// <summary>
/// Clone the DatItem
/// </summary>
/// <returns>Clone of the DatItem</returns>
2024-03-10 20:39:54 -04:00
public override object Clone()
{
2024-11-12 21:12:06 -05:00
var concrete = Array.Find(Assembly.GetExecutingAssembly().GetTypes(),
t => !t.IsAbstract && t.IsClass && t.BaseType == typeof(DatItem<T>));
var clone = Activator.CreateInstance(concrete!);
(clone as DatItem<T>)!._internal = _internal?.Clone() as T ?? Activator.CreateInstance<T>();
2024-03-10 20:39:54 -04:00
return clone;
}
2024-03-10 17:14:36 -04:00
#endregion
#region Comparision Methods
/// <inheritdoc/>
public int CompareTo(DatItem<T>? other)
{
try
{
if (GetName() == other?.GetName())
return Equals(other) ? 0 : 1;
return string.Compare(GetName(), other?.GetName());
}
catch
{
return 1;
}
}
/// <summary>
/// Determine if an item is a duplicate using partial matching logic
/// </summary>
/// <param name="other">DatItem to use as a baseline</param>
/// <returns>True if the items are duplicates, false otherwise</returns>
public virtual bool Equals(DatItem<T>? other)
{
// If we don't have a matched type, return false
2024-03-11 16:26:28 -04:00
if (GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>() != other?.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>())
2024-03-10 17:14:36 -04:00
return false;
// Compare the internal models
return _internal.EqualTo(other._internal);
}
#endregion
}
2016-09-19 20:52:55 -07:00
}