2016-09-19 20:52:55 -07:00
|
|
|
|
using System;
|
2024-03-12 13:00:34 -04:00
|
|
|
|
using System.Reflection;
|
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;
|
2024-03-12 22:52:36 -04:00
|
|
|
|
using SabreTools.Core.Filter;
|
2020-12-08 16:37:08 -08:00
|
|
|
|
using SabreTools.Core.Tools;
|
2021-02-02 10:23:43 -08:00
|
|
|
|
using SabreTools.DatItems.Formats;
|
2024-03-06 11:23:22 -05:00
|
|
|
|
using SabreTools.Hashing;
|
2024-10-24 00:36:44 -04:00
|
|
|
|
using SabreTools.IO.Logging;
|
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))]
|
2024-03-12 22:22:12 -04:00
|
|
|
|
public abstract class DatItem : ModelBackedItem<Models.Metadata.DatItem>, IEquatable<DatItem>, IComparable<DatItem>, ICloneable
|
2019-01-08 12:11:55 -08:00
|
|
|
|
{
|
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; }
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
2020-06-10 22:37:19 -07:00
|
|
|
|
#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>
|
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
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
2025-01-06 15:37:32 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Conditionally copy all machine information from another item
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item">Existing item to copy information from</param>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The cases when Machine data is updated:
|
|
|
|
|
|
/// - Current machine is a clone of the other machine
|
|
|
|
|
|
/// - Current machine is a rom of the other machine
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public void ConditionalUpdateMachine(DatItem item)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the machines for the two items
|
|
|
|
|
|
Machine? selfMachine = GetFieldValue<Machine>(DatItem.MachineKey);
|
|
|
|
|
|
Machine? itemMachine = item.GetFieldValue<Machine>(DatItem.MachineKey);
|
|
|
|
|
|
|
|
|
|
|
|
// If either machine is missing
|
|
|
|
|
|
if (selfMachine == null || itemMachine == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the required strings
|
|
|
|
|
|
string? selfCloneOf = selfMachine.GetStringFieldValue(Models.Metadata.Machine.CloneOfKey);
|
|
|
|
|
|
string? selfRomOf = selfMachine.GetStringFieldValue(Models.Metadata.Machine.RomOfKey);
|
|
|
|
|
|
string? otherMachineName = itemMachine.GetStringFieldValue(Models.Metadata.Machine.NameKey);
|
|
|
|
|
|
|
|
|
|
|
|
// If the other machine has no name
|
|
|
|
|
|
if (otherMachineName == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// If the current machine is a child of the new machine, use the new machine instead
|
|
|
|
|
|
if (selfCloneOf == otherMachineName)
|
|
|
|
|
|
{
|
|
|
|
|
|
CopyMachineInformation(item);
|
|
|
|
|
|
SetName(item.GetName());
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (selfRomOf == otherMachineName)
|
|
|
|
|
|
{
|
|
|
|
|
|
CopyMachineInformation(item);
|
|
|
|
|
|
SetName(item.GetName());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Conditionally copy all source information from another item
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item">Existing item to copy information from</param>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// The cases when Source data is updated:
|
|
|
|
|
|
/// - Current source data has an index higher than the other item
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public void ConditionalUpdateSource(DatItem item)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Get the sources for comparison
|
|
|
|
|
|
Source? selfSource = GetFieldValue<Source?>(DatItem.SourceKey);
|
|
|
|
|
|
Source? itemSource = item.GetFieldValue<Source?>(DatItem.SourceKey);
|
|
|
|
|
|
|
|
|
|
|
|
// If either source is missing
|
|
|
|
|
|
if (selfSource == null || itemSource == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Use the new source if less than
|
|
|
|
|
|
if (selfSource.Index > itemSource.Index)
|
|
|
|
|
|
{
|
|
|
|
|
|
SetFieldValue<Source?>(DatItem.SourceKey, itemSource.Clone() as Source);
|
|
|
|
|
|
CopyMachineInformation(item);
|
|
|
|
|
|
SetName(item.GetName());
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
/// <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);
|
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-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);
|
|
|
|
|
|
}
|
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-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
|
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);
|
2024-03-11 15:46:44 -04:00
|
|
|
|
if (lastMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) == currentMachine?.GetStringFieldValue(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);
|
2024-03-11 15:46:44 -04:00
|
|
|
|
if (lastMachine?.GetStringFieldValue(Models.Metadata.Machine.NameKey) == currentMachine?.GetStringFieldValue(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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-20 02:10:38 -04:00
|
|
|
|
/// <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
|
2024-03-20 02:10:38 -04:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-01-08 12:11:55 -08:00
|
|
|
|
#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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.CRC32Str;
|
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:
|
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}";
|
2020-07-15 09:41:59 -07:00
|
|
|
|
if (lower)
|
|
|
|
|
|
key = key.ToLowerInvariant();
|
2019-01-08 12:11:55 -08:00
|
|
|
|
|
2024-03-20 02:10:38 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.MD5:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.MD5Str;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SHA1:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA1Str;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SHA256:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA256Str;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SHA384:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA384Str;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SHA512:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA512Str;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SpamSum:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SpamSumStr;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
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;
|
2024-03-20 02:10:38 -04:00
|
|
|
|
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}";
|
2024-03-20 02:10:38 -04:00
|
|
|
|
if (lower)
|
|
|
|
|
|
key = key.ToLowerInvariant();
|
|
|
|
|
|
|
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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.MD5Str;
|
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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA1Str;
|
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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA256Str;
|
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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA384Str;
|
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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SHA512Str;
|
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:
|
2024-11-13 03:55:33 -05:00
|
|
|
|
key = ZeroHash.SpamSumStr;
|
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
|
|
|
|
|
|
}
|
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)
|
2024-03-11 15:46:44 -04:00
|
|
|
|
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>));
|
2024-03-12 13:00:34 -04:00
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|