2016-09-19 20:52:55 -07:00
|
|
|
|
using System;
|
2020-06-10 22:37:19 -07:00
|
|
|
|
using System.IO;
|
2020-09-07 14:47:27 -07:00
|
|
|
|
using System.Xml.Serialization;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
using Newtonsoft.Json;
|
2020-12-08 13:23:59 -08:00
|
|
|
|
using SabreTools.Core;
|
2020-12-08 16:37:08 -08:00
|
|
|
|
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-05 02:56:50 -05:00
|
|
|
|
using SabreTools.Filter;
|
2024-03-06 11:23:22 -05:00
|
|
|
|
using SabreTools.Hashing;
|
2020-12-07 14:29:45 -08:00
|
|
|
|
using SabreTools.Logging;
|
2024-02-29 21:20:44 -05:00
|
|
|
|
using SabreTools.Matching;
|
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
|
|
|
|
{
|
2019-01-08 12:11:55 -08: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))]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
[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))]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
[XmlInclude(typeof(DipLocation))]
|
2020-09-07 14:47:27 -07:00
|
|
|
|
[XmlInclude(typeof(DipSwitch))]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
[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))]
|
2019-01-08 12:11:55 -08:00
|
|
|
|
public abstract class DatItem : IEquatable<DatItem>, IComparable<DatItem>, ICloneable
|
|
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
#region Constants
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-10 16:49:07 -04:00
|
|
|
|
/// Duplicate type when compared to another item
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2024-03-10 16:49:07 -04:00
|
|
|
|
public const string DupeTypeKey = "DUPETYPE";
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-10 16:49:07 -04:00
|
|
|
|
/// Machine associated with the item
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </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";
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2024-03-10 16:49:07 -04:00
|
|
|
|
/// Source information
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2024-03-10 16:49:07 -04:00
|
|
|
|
public const string SourceKey = "SOURCE";
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2024-03-10 16:49:07 -04:00
|
|
|
|
#region Fields
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2024-03-10 20:45:54 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Item type for the object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected abstract ItemType ItemType { get; }
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <summary>
|
2024-03-10 16:49:07 -04:00
|
|
|
|
/// Internal model wrapped by this DatItem
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-10 17:08:25 -04:00
|
|
|
|
protected Models.Metadata.DatItem _internal = [];
|
2020-08-19 13:12:42 -07:00
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
|
#endregion
|
2023-08-10 23:22:14 -04:00
|
|
|
|
|
2020-10-07 15:42:30 -07:00
|
|
|
|
#region Logging
|
|
|
|
|
|
|
2020-10-07 16:37:10 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Static logger for static methods
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore, XmlIgnore]
|
2024-03-10 17:05:44 -04:00
|
|
|
|
protected static readonly Logger staticLogger = new();
|
2020-10-07 15:42:30 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Instance Methods
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
#region Accessors
|
|
|
|
|
|
|
2024-03-08 15:31:21 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get the value from a field based on the type provided
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Type of the value to get from the internal model</typeparam>
|
|
|
|
|
|
/// <param name="fieldName">Field to retrieve</param>
|
|
|
|
|
|
/// <returns>Value from the field, if possible</returns>
|
|
|
|
|
|
public T? GetFieldValue<T>(string? fieldName)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Invalid field cannot be processed
|
|
|
|
|
|
if (string.IsNullOrEmpty(fieldName))
|
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the value based on the type
|
|
|
|
|
|
return _internal.Read<T>(fieldName!);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set the value from a field based on the type provided
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Type of the value to set in the internal model</typeparam>
|
|
|
|
|
|
/// <param name="fieldName">Field to set</param>
|
|
|
|
|
|
/// <param name="value">Value to set</param>
|
|
|
|
|
|
/// <returns>True if the value was set, false otherwise</returns>
|
|
|
|
|
|
public bool SetFieldValue<T>(string? fieldName, T? value)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Invalid field cannot be processed
|
|
|
|
|
|
if (string.IsNullOrEmpty(fieldName))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Set the value based on the type
|
|
|
|
|
|
_internal[fieldName!] = value;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
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>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public virtual string? GetName() => null;
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2020-12-13 14:01:16 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Sets the name to use for a DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="name">Name to set for the item</param>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public virtual void SetName(string? name) { }
|
2020-12-13 14:01:16 -08:00
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
#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)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2024-02-28 19:19:50 -05:00
|
|
|
|
return baseFile?.Type switch
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
|
FileType.AaruFormat => new Media(baseFile),
|
|
|
|
|
|
FileType.CHD => new Disk(baseFile),
|
|
|
|
|
|
FileType.GZipArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.LRZipArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.LZ4Archive => new Rom(baseFile),
|
|
|
|
|
|
FileType.None => new Rom(baseFile),
|
|
|
|
|
|
FileType.RarArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.SevenZipArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.TapeArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.XZArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.ZipArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.ZPAQArchive => new Rom(baseFile),
|
|
|
|
|
|
FileType.ZstdArchive => new Rom(baseFile),
|
|
|
|
|
|
_ => null,
|
|
|
|
|
|
};
|
2020-07-15 09:41:59 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#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-03-10 16:49:07 -04:00
|
|
|
|
if (item?.GetFieldValue<Machine>(DatItem.MachineKey) == null)
|
2023-08-10 23:22:14 -04:00
|
|
|
|
return;
|
|
|
|
|
|
|
2024-03-10 16:49:07 -04:00
|
|
|
|
if (item.GetFieldValue<Machine>(DatItem.MachineKey)!.Clone() is Machine cloned)
|
|
|
|
|
|
SetFieldValue<Machine>(DatItem.MachineKey, cloned);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Copy all machine information over in one shot
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="machine">Existing machine to copy information from</param>
|
2023-08-10 23:22:14 -04:00
|
|
|
|
public void CopyMachineInformation(Machine? machine)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2023-08-10 23:22:14 -04:00
|
|
|
|
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);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Comparision Methods
|
|
|
|
|
|
|
2022-11-03 12:23:10 -07:00
|
|
|
|
/// <inheritdoc/>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public int CompareTo(DatItem? other)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
if (GetName() == other?.GetName())
|
2020-09-02 15:07:25 -07:00
|
|
|
|
return Equals(other) ? 0 : 1;
|
|
|
|
|
|
|
2023-08-14 13:17:51 -04:00
|
|
|
|
return string.Compare(GetName(), other?.GetName());
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return 1;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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-10 16:49:07 -04:00
|
|
|
|
if (GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey) != other?.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey))
|
2023-08-14 22:33:05 -04:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Compare the internal models
|
|
|
|
|
|
return _internal.EqualTo(other._internal);
|
|
|
|
|
|
}
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
/// <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>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public DupeType GetDuplicateStatus(DatItem? lastItem)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
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))
|
2019-01-08 12:11:55 -08:00
|
|
|
|
return output;
|
|
|
|
|
|
|
|
|
|
|
|
// If the duplicate is external already or should be, set it
|
2024-02-28 22:07:00 -05:00
|
|
|
|
#if NETFRAMEWORK
|
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
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2024-03-10 21:03:53 -04:00
|
|
|
|
var currentMachine = GetFieldValue<Machine>(DatItem.MachineKey);
|
|
|
|
|
|
var lastMachine = lastItem?.GetFieldValue<Machine>(DatItem.MachineKey);
|
|
|
|
|
|
if (lastMachine?.GetFieldValue<string?>(Models.Metadata.Machine.NameKey) == currentMachine?.GetFieldValue<string?>(Models.Metadata.Machine.NameKey) && lastItem?.GetName() == GetName())
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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?.GetFieldValue<string?>(Models.Metadata.Machine.NameKey) == currentMachine?.GetFieldValue<string?>(Models.Metadata.Machine.NameKey) && lastItem?.GetName() == GetName())
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#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
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </summary>
|
2020-12-14 15:31:28 -08:00
|
|
|
|
/// <param name="bucketedBy">ItemKey value representing what key to get</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <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)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Set the output key as the default blank string
|
|
|
|
|
|
string key = string.Empty;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 10:47:13 -07:00
|
|
|
|
// Now determine what the key should be based on the bucketedBy value
|
|
|
|
|
|
switch (bucketedBy)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
{
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.CRC:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.CRCZero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.Machine:
|
2020-07-15 09:41:59 -07:00
|
|
|
|
key = (norename ? string.Empty
|
2024-03-10 16:49:07 -04:00
|
|
|
|
: GetFieldValue<Source?>(DatItem.SourceKey)?.Index.ToString().PadLeft(10, '0')
|
2020-07-15 09:41:59 -07:00
|
|
|
|
+ "-")
|
2024-03-10 16:49:07 -04:00
|
|
|
|
+ (string.IsNullOrEmpty(GetFieldValue<Machine>(DatItem.MachineKey)?.GetFieldValue<string?>(Models.Metadata.Machine.NameKey))
|
2020-07-15 09:41:59 -07:00
|
|
|
|
? "Default"
|
2024-03-10 16:49:07 -04:00
|
|
|
|
: GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey)!);
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (lower)
|
|
|
|
|
|
key = key.ToLowerInvariant();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2023-04-19 16:39:58 -04:00
|
|
|
|
key ??= "null";
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.MD5:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.MD5Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA1:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA1Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA256:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA256Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA384:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA384Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA512:
|
2020-08-17 17:28:32 -07:00
|
|
|
|
key = Constants.SHA512Zero;
|
2020-07-15 09:41:59 -07:00
|
|
|
|
break;
|
2020-09-04 15:02:15 -07:00
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SpamSum:
|
2020-09-17 23:37:42 -07:00
|
|
|
|
key = Constants.SpamSumZero;
|
2020-09-04 15:02:15 -07:00
|
|
|
|
break;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Double and triple check the key for corner cases
|
2023-04-19 16:39:58 -04:00
|
|
|
|
key ??= string.Empty;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
return key;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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>
|
2024-02-28 19:19:50 -05:00
|
|
|
|
public static ConcurrentList<DatItem> Merge(ConcurrentList<DatItem>? infiles)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Check for null or blank roms first
|
|
|
|
|
|
if (infiles == null || infiles.Count == 0)
|
2024-02-28 19:19:50 -05:00
|
|
|
|
return [];
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// Create output list
|
2024-02-28 19:19:50 -05:00
|
|
|
|
ConcurrentList<DatItem> outfiles = [];
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// 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++)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
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)
|
2023-04-07 16:13:15 -04:00
|
|
|
|
{
|
2019-01-08 12:11:55 -08:00
|
|
|
|
continue;
|
2023-04-07 16:13:15 -04:00
|
|
|
|
}
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// If it's a nodump, add and skip
|
2024-03-10 16:49:07 -04:00
|
|
|
|
if (item is Rom rom && rom.GetFieldValue<ItemStatus>(Models.Metadata.Rom.StatusKey) == ItemStatus.Nodump)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
outfiles.Add(item);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
nodumpCount++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2024-03-10 16:49:07 -04:00
|
|
|
|
else if (item is Disk disk && disk.GetFieldValue<ItemStatus>(Models.Metadata.Disk.StatusKey) == ItemStatus.Nodump)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
outfiles.Add(item);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if the rom is a duplicate
|
|
|
|
|
|
DupeType dupetype = 0x00;
|
2020-08-17 17:28:32 -07:00
|
|
|
|
DatItem saveditem = new Blank();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// If it's a duplicate, skip adding it to the output but add any missing information
|
|
|
|
|
|
if (dupetype != 0x00)
|
|
|
|
|
|
{
|
|
|
|
|
|
saveditem = lastrom;
|
|
|
|
|
|
pos = i;
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
// 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)
|
2023-08-14 13:17:51 -04:00
|
|
|
|
savedDisk.FillMissingInformation(disk);
|
2024-03-10 16:49:07 -04:00
|
|
|
|
else if (item is Formats.File fileItem && saveditem is Formats.File savedFile)
|
2023-08-14 13:17:51 -04:00
|
|
|
|
savedFile.FillMissingInformation(fileItem);
|
2024-03-10 16:49:07 -04:00
|
|
|
|
else if (item is Media media && saveditem is Media savedMedia)
|
2023-08-14 13:17:51 -04:00
|
|
|
|
savedMedia.FillMissingInformation(media);
|
2024-03-10 16:49:07 -04:00
|
|
|
|
else if (item is Rom romItem && saveditem is Rom savedRom)
|
2023-08-14 13:17:51 -04:00
|
|
|
|
savedRom.FillMissingInformation(romItem);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2024-03-10 16:49:07 -04:00
|
|
|
|
saveditem.SetFieldValue<DupeType>(DatItem.DupeTypeKey, dupetype);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// 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)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
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());
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If the current machine is a child of the new machine, use the new machine instead
|
2024-03-10 16:49:07 -04:00
|
|
|
|
if (saveditem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.CloneOfKey) == item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey)
|
|
|
|
|
|
|| saveditem.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.RomOfKey) == item.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey))
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
saveditem.CopyMachineInformation(item);
|
|
|
|
|
|
saveditem.SetName(item.GetName());
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
// 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>
|
2021-07-18 21:00:01 -07:00
|
|
|
|
public static ConcurrentList<DatItem> ResolveNames(ConcurrentList<DatItem> infiles)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Create the output list
|
2024-02-28 19:19:50 -05:00
|
|
|
|
ConcurrentList<DatItem> output = [];
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
|
|
|
|
|
// 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
|
2023-08-14 13:17:51 -04:00
|
|
|
|
DatItem? lastItem = null;
|
|
|
|
|
|
string? lastrenamed = null;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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-03-10 16:49:07 -04:00
|
|
|
|
string lastItemName = lastItem.GetName() ?? lastItem.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey).ToString();
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
|
|
|
|
|
// Get the current item name, if applicable
|
2024-03-10 16:49:07 -04:00
|
|
|
|
string datItemName = datItem.GetName() ?? datItem.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey).ToString();
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
// If the current item exactly matches the last item, then we don't add it
|
2024-02-28 22:07:00 -05:00
|
|
|
|
#if NETFRAMEWORK
|
|
|
|
|
|
if ((datItem.GetDuplicateStatus(lastItem) & DupeType.All) != 0)
|
|
|
|
|
|
#else
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (datItem.GetDuplicateStatus(lastItem).HasFlag(DupeType.All))
|
2024-02-28 22:07:00 -05:00
|
|
|
|
#endif
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-10-07 16:37:10 -07:00
|
|
|
|
staticLogger.Verbose($"Exact duplicate found for '{datItemName}'");
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-10-07 16:37:10 -07:00
|
|
|
|
staticLogger.Verbose($"Name duplicate found for '{datItemName}'");
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2024-03-10 16:49:07 -04:00
|
|
|
|
if (datItem is Disk || datItem is Formats.File || datItem is Media || datItem is Rom)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-09-02 12:19:12 -07:00
|
|
|
|
datItemName += GetDuplicateSuffix(datItem);
|
|
|
|
|
|
lastrenamed ??= datItemName;
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2020-09-02 12:19:12 -07:00
|
|
|
|
lastrenamed = datItemName;
|
|
|
|
|
|
datItemName += (lastid == 0 ? string.Empty : "_" + lastid);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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
|
2020-12-14 10:11:20 -08:00
|
|
|
|
datItem.SetName(datItemName);
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08: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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get duplicate suffix based on the item type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private static string GetDuplicateSuffix(DatItem datItem)
|
|
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
return datItem switch
|
|
|
|
|
|
{
|
|
|
|
|
|
Disk disk => disk.GetDuplicateSuffix(),
|
|
|
|
|
|
Formats.File file => file.GetDuplicateSuffix(),
|
|
|
|
|
|
Media media => media.GetDuplicateSuffix(),
|
|
|
|
|
|
Rom rom => rom.GetDuplicateSuffix(),
|
|
|
|
|
|
_ => "_1",
|
|
|
|
|
|
};
|
2020-06-10 22:37:19 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <summary>
|
2020-08-28 01:13:55 -07:00
|
|
|
|
/// Sort a list of File objects by SourceID, Game, and Name (in order)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// </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>
|
2021-07-18 21:00:01 -07:00
|
|
|
|
public static bool Sort(ref ConcurrentList<DatItem> roms, bool norename)
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
roms.Sort(delegate (DatItem x, DatItem y)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2023-04-19 16:39:58 -04:00
|
|
|
|
NaturalComparer nc = new();
|
2021-12-21 14:20:27 -08:00
|
|
|
|
|
|
|
|
|
|
// If machine names match, more refinement is needed
|
2024-03-10 16:49:07 -04:00
|
|
|
|
if (x.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey) == y.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey))
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// If item types match, more refinement is needed
|
2024-03-10 16:49:07 -04:00
|
|
|
|
if (x.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey) == y.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey))
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
string? xDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
|
|
|
|
|
|
string? yDirectoryName = Path.GetDirectoryName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
|
2021-12-21 14:20:27 -08:00
|
|
|
|
|
|
|
|
|
|
// If item directory names match, more refinement is needed
|
|
|
|
|
|
if (xDirectoryName == yDirectoryName)
|
2020-08-31 16:46:04 -07:00
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
string? xName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(x.GetName() ?? string.Empty));
|
|
|
|
|
|
string? yName = Path.GetFileName(TextHelper.RemovePathUnsafeCharacters(y.GetName() ?? string.Empty));
|
2021-12-21 14:20:27 -08:00
|
|
|
|
|
|
|
|
|
|
// If item names match, then compare on machine or source, depending on the flag
|
|
|
|
|
|
if (xName == yName)
|
2024-03-10 16:49:07 -04:00
|
|
|
|
return (norename ? nc.Compare(x.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey), y.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey)) : (x.GetFieldValue<Source?>(DatItem.SourceKey)?.Index - y.GetFieldValue<Source?>(DatItem.SourceKey)?.Index) ?? 0);
|
2020-08-31 15:33:05 -07:00
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on item names
|
|
|
|
|
|
return nc.Compare(xName, yName);
|
2020-08-31 16:46:04 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on directory name
|
|
|
|
|
|
return nc.Compare(xDirectoryName, yDirectoryName);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on item type
|
2024-03-10 16:49:07 -04:00
|
|
|
|
return x.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey) - y.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey);
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2020-06-10 22:37:19 -07:00
|
|
|
|
|
2021-12-21 14:20:27 -08:00
|
|
|
|
// Otherwise, just sort based on machine name
|
2024-03-10 16:49:07 -04:00
|
|
|
|
return nc.Compare(x.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey), y.GetFieldValue<Machine>(DatItem.MachineKey)!.GetFieldValue<string?>(Models.Metadata.Machine.NameKey));
|
2019-01-08 12:11:55 -08:00
|
|
|
|
}
|
2020-12-08 13:23:59 -08:00
|
|
|
|
catch
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
|
|
|
|
|
// Absorb the error
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2019-09-20 10:30:16 -07:00
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2024-03-10 17:38:44 -04:00
|
|
|
|
#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; }
|
|
|
|
|
|
|
2024-03-10 17:38:44 -04:00
|
|
|
|
#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 GetFieldValue<string?>(NameKey);
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public override void SetName(string? name)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (NameKey != null)
|
|
|
|
|
|
SetFieldValue(NameKey, name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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()
|
|
|
|
|
|
{
|
|
|
|
|
|
var clone = Activator.CreateInstance<DatItem<T>>();
|
|
|
|
|
|
clone._internal = _internal?.Clone() as T ?? Activator.CreateInstance<T>();
|
|
|
|
|
|
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
|
|
|
|
|
|
if (GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey) != other?.GetFieldValue<ItemType>(Models.Metadata.DatItem.TypeKey))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Compare the internal models
|
|
|
|
|
|
return _internal.EqualTo(other._internal);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
2016-09-19 20:52:55 -07:00
|
|
|
|
}
|