Files
SabreTools/SabreTools.DatItems/DatItem.cs

446 lines
14 KiB
C#
Raw Normal View History

2016-09-19 20:52:55 -07:00
using System;
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;
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
{
/// <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]
2025-01-08 16:59:44 -05:00
protected static readonly Logger _staticLogger = new();
#endregion
#region Accessors
2025-05-02 16:46:20 -04:00
/// <summary>
/// Get the machine for a DatItem
/// </summary>
/// <returns>Machine if available, null otherwise</returns>
/// <remarks>Relies on <see cref="MachineKey"/></remarks>
2025-05-02 20:33:51 -04:00
public Machine? GetMachine() => _internal.Read<Machine>(MachineKey);
2025-05-02 16:46:20 -04:00
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>
2025-05-02 20:33:51 -04:00
public virtual string? GetName() => _internal.GetName();
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>
2025-05-02 20:33:51 -04:00
public virtual void SetName(string? name) => _internal.SetName(name);
#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)
{
2025-01-07 19:51:55 -05:00
// If there is no machine
2025-05-02 16:46:20 -04:00
if (!item._internal.ContainsKey(MachineKey))
2025-01-07 19:51:55 -05:00
return;
2025-05-02 16:46:20 -04:00
var machine = item.GetMachine();
2024-10-24 05:11:17 -04:00
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)
2025-05-02 16:46:20 -04:00
SetFieldValue<Machine>(MachineKey, cloned);
}
#endregion
#region Comparision Methods
2022-11-03 12:23:10 -07:00
/// <inheritdoc/>
public int CompareTo(DatItem? other)
{
// If the other item doesn't exist
if (other == null)
return 1;
2025-01-08 10:32:11 -05:00
// Get the names to avoid changing values
string? selfName = GetName();
string? otherName = other.GetName();
2025-01-07 20:26:20 -05:00
2025-01-08 10:32:11 -05:00
// If the names are equal
if (selfName == otherName)
return Equals(other) ? 0 : 1;
2020-09-02 15:07:25 -07:00
2025-01-08 10:32:11 -05:00
// If `otherName` is null, Compare will return > 0
// If `selfName` is null, Compare will return < 0
return string.Compare(selfName, otherName, StringComparison.Ordinal);
}
/// <inheritdoc/>
public override bool Equals(ModelBackedItem? other)
{
// If other is null
if (other == null)
return false;
// If the type is mismatched
if (other is not DatItem otherItem)
return false;
// Compare internal models
return _internal.Equals(otherItem);
}
2025-01-08 15:28:47 -05:00
/// <inheritdoc/>
public override bool Equals(ModelBackedItem<Models.Metadata.DatItem>? other)
{
// If other is null
if (other == null)
return false;
// If the type is mismatched
if (other is not DatItem otherItem)
return false;
// Compare internal models
return _internal.Equals(otherItem);
}
/// <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 the other item is null
if (other == null)
return false;
// Get the types for comparison
2025-05-11 22:55:38 -04:00
ItemType selfType = GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsItemType();
ItemType otherType = other.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsItemType();
2023-08-14 22:33:05 -04:00
// If we don't have a matched type, return false
if (selfType != otherType)
2023-08-14 22:33:05 -04:00
return false;
// Compare the internal models
return _internal.EqualTo(other._internal);
}
#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>
2025-01-08 12:57:46 -05:00
/// <returns>True if the item and its machine passes the filter, false otherwise</returns>
2024-03-05 02:56:50 -05:00
public bool PassesFilter(FilterRunner filterRunner)
{
2025-05-02 16:46:20 -04:00
var machine = GetMachine();
2025-01-07 16:41:38 -05:00
if (machine != null && !machine.PassesFilter(filterRunner))
2024-03-05 02:56:50 -05:00
return false;
return filterRunner.Run(_internal);
}
2025-01-08 12:57:46 -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 PassesFilterDB(FilterRunner filterRunner)
=> filterRunner.Run(_internal);
2024-03-05 01:42:42 -05:00
#endregion
#region Sorting and Merging
/// <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>
2025-01-14 14:22:18 -05:00
/// <param name="machine">Machine associated with the item for renaming</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, Machine? machine, Source? source, bool lower = true, bool norename = true)
{
// Set the output key as the default blank string
string key = string.Empty;
2025-01-14 14:22:18 -05:00
string sourceKeyPadded = source?.Index.ToString().PadLeft(10, '0') + '-';
string machineName = machine?.GetName() ?? "Default";
2025-01-14 14:22:18 -05:00
// 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:
2025-01-14 14:22:18 -05:00
key = (norename ? string.Empty : sourceKeyPadded) + machineName;
break;
2025-01-09 05:44:34 -05:00
case ItemKey.MD2:
key = ZeroHash.GetString(HashType.MD2);
break;
2025-01-09 05:26:36 -05:00
2025-01-09 05:44:34 -05:00
case ItemKey.MD4:
key = ZeroHash.GetString(HashType.MD4);
break;
2025-01-09 05:26:36 -05:00
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;
2025-01-08 13:32:09 -05:00
if (lower)
key = key.ToLowerInvariant();
return key;
}
#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
{
#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);
2025-05-02 16:46:20 -04:00
SetFieldValue<Machine>(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);
2025-05-02 16:46:20 -04:00
SetFieldValue<Machine>(MachineKey, new Machine());
}
#endregion
2024-03-10 17:14:36 -04:00
#region Cloning Methods
/// <summary>
/// Clone the DatItem
/// </summary>
/// <returns>Clone of the DatItem</returns>
2025-01-08 13:54:51 -05:00
/// <remarks>
/// Throws an exception if there is a DatItem implementation
/// that is not a part of this library.
/// </remarks>
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
2025-01-07 16:53:57 -05:00
/// <summary>
/// Get a clone of the current internal model
/// </summary>
public virtual T GetInternalClone() => (_internal.Clone() as T)!;
2025-01-07 16:53:57 -05:00
2024-03-10 17:14:36 -04:00
#endregion
#region Comparision Methods
/// <inheritdoc/>
public int CompareTo(DatItem<T>? other)
{
// If the other item doesn't exist
if (other == null)
return 1;
2025-01-08 10:32:11 -05:00
// Get the names to avoid changing values
string? selfName = GetName();
string? otherName = other.GetName();
2025-01-07 20:26:20 -05:00
2025-01-08 10:32:11 -05:00
// If the names are equal
if (selfName == otherName)
return Equals(other) ? 0 : 1;
2024-03-10 17:14:36 -04:00
2025-01-08 10:32:11 -05:00
// If `otherName` is null, Compare will return > 0
// If `selfName` is null, Compare will return < 0
return string.Compare(selfName, otherName, StringComparison.Ordinal);
2024-03-10 17:14:36 -04:00
}
/// <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 the other value is null
if (other == null)
return false;
// Get the types for comparison
2025-05-11 22:55:38 -04:00
ItemType selfType = GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsItemType();
ItemType otherType = other.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsItemType();
2024-03-10 17:14:36 -04:00
// If we don't have a matched type, return false
if (selfType != otherType)
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
}