Files
SabreTools/SabreTools.DatItems/Formats/Rom.cs

180 lines
5.6 KiB
C#
Raw Normal View History

2024-03-10 16:49:07 -04:00
using System.Xml.Serialization;
2022-11-03 12:22:17 -07:00
using Newtonsoft.Json;
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2021-02-02 10:23:43 -08:00
namespace SabreTools.DatItems.Formats
{
2019-01-08 17:40:12 -08:00
/// <summary>
/// Represents a generic file within a set
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("rom"), XmlRoot("rom")]
2024-03-10 20:39:54 -04:00
public sealed class Rom : DatItem<Models.Metadata.Rom>
2019-01-08 17:40:12 -08:00
{
2024-03-10 16:49:07 -04:00
#region Constants
/// <summary>
/// Non-standard key for inverted logic
/// </summary>
public const string DataAreaKey = "DATAAREA";
/// <summary>
/// Non-standard key for inverted logic
/// </summary>
public const string PartKey = "PART";
#endregion
2020-08-20 13:17:14 -07:00
#region Fields
2019-01-08 17:40:12 -08:00
2024-03-10 20:39:54 -04:00
/// <inheritdoc>/>
protected override ItemType ItemType => ItemType.Rom;
/// <inheritdoc>/>
protected override string? NameKey => Models.Metadata.Rom.NameKey;
2020-09-07 22:00:02 -07:00
[JsonIgnore]
2024-03-09 21:34:26 -05:00
public bool ItemStatusSpecified
2019-01-08 17:40:12 -08:00
{
2024-03-09 21:34:26 -05:00
get
{
2024-03-11 16:26:28 -04:00
var status = GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue<ItemStatus>();
2024-03-09 21:34:26 -05:00
return status != ItemStatus.NULL && status != ItemStatus.None;
}
}
2020-09-07 22:00:02 -07:00
[JsonIgnore]
2024-03-09 21:34:26 -05:00
public bool OriginalSpecified
{
2024-03-09 21:34:26 -05:00
get
{
var original = GetFieldValue<Original?>("ORIGINAL");
return original != null && original != default;
}
2023-08-14 22:33:05 -04:00
}
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool DataAreaSpecified
{
get
{
2024-03-10 16:49:07 -04:00
var dataArea = GetFieldValue<DataArea?>(Rom.DataAreaKey);
2024-03-09 21:34:26 -05:00
return dataArea != null
&& (!string.IsNullOrEmpty(dataArea.GetName())
|| dataArea.GetInt64FieldValue(Models.Metadata.DataArea.SizeKey) != null
|| dataArea.GetInt64FieldValue(Models.Metadata.DataArea.WidthKey) != null
|| dataArea.GetStringFieldValue(Models.Metadata.DataArea.EndiannessKey).AsEnumValue<Endianness>() != Endianness.NULL);
2020-09-07 22:00:02 -07:00
}
}
[JsonIgnore]
public bool PartSpecified
{
get
{
2024-03-10 16:49:07 -04:00
var part = GetFieldValue<Part?>(Rom.PartKey);
2024-03-09 21:34:26 -05:00
return part != null
&& (!string.IsNullOrEmpty(part.GetName())
|| !string.IsNullOrEmpty(part.GetStringFieldValue(Models.Metadata.Part.InterfaceKey)));
2020-09-07 22:00:02 -07:00
}
}
2024-03-09 23:43:43 -05:00
#endregion
2019-01-08 17:40:12 -08:00
#region Constructors
2024-03-10 20:39:54 -04:00
public Rom() : base()
2019-01-08 17:40:12 -08:00
{
2024-03-10 16:49:07 -04:00
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
SetFieldValue<string?>(Models.Metadata.Rom.StatusKey, ItemStatus.None.AsStringValue());
2019-01-08 17:40:12 -08:00
}
2024-03-10 20:39:54 -04:00
public Rom(Models.Metadata.Rom item) : base(item)
2023-08-14 14:53:28 -04:00
{
2024-03-10 20:39:54 -04:00
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
2023-08-14 14:53:28 -04:00
}
2019-01-08 17:40:12 -08:00
#endregion
#region Comparision Methods
2020-08-17 17:28:32 -07:00
/// <summary>
/// Fill any missing size and hash information from another Rom
/// </summary>
/// <param name="other">Rom to fill information from</param>
2023-08-14 22:33:05 -04:00
public void FillMissingInformation(Rom other) => _internal.FillMissingHashes(other?._internal);
2020-08-17 17:28:32 -07:00
/// <summary>
/// Get unique duplicate suffix on name collision
/// </summary>
/// <returns>String representing the suffix</returns>
2023-08-14 22:33:05 -04:00
public string GetDuplicateSuffix() => _internal.GetDuplicateSuffix();
2020-08-17 17:28:32 -07:00
2020-09-17 23:37:42 -07:00
/// <summary>
/// Returns if the Rom contains any hashes
/// </summary>
/// <returns>True if any hash exists, false otherwise</returns>
2023-08-14 22:33:05 -04:00
public bool HasHashes() => _internal.HasHashes();
2020-09-17 23:37:42 -07:00
/// <summary>
/// Returns if all of the hashes are set to their 0-byte values
/// </summary>
/// <returns>True if any hash matches the 0-byte value, false otherwise</returns>
2023-08-14 22:33:05 -04:00
public bool HasZeroHash() => _internal.HasZeroHash();
2020-08-17 14:57:54 -07:00
2019-01-08 17:40:12 -08:00
#endregion
2020-08-17 17:28:32 -07:00
#region Sorting and Merging
2020-12-14 15:31:28 -08:00
/// <inheritdoc/>
public override string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
2020-08-17 17:28:32 -07:00
{
// Set the output key as the default blank string
string? key;
2020-08-17 17:28:32 -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:
key = GetStringFieldValue(Models.Metadata.Rom.CRCKey);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.MD5:
key = GetStringFieldValue(Models.Metadata.Rom.MD5Key);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA1:
key = GetStringFieldValue(Models.Metadata.Rom.SHA1Key);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA256:
key = GetStringFieldValue(Models.Metadata.Rom.SHA256Key);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA384:
key = GetStringFieldValue(Models.Metadata.Rom.SHA384Key);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA512:
key = GetStringFieldValue(Models.Metadata.Rom.SHA512Key);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SpamSum:
key = GetStringFieldValue(Models.Metadata.Rom.SpamSumKey);
break;
2020-08-17 17:28:32 -07:00
// Let the base handle generic stuff
default:
return base.GetKey(bucketedBy, lower, norename);
}
// Double and triple check the key for corner cases
key ??= string.Empty;
2020-08-17 17:28:32 -07:00
return key;
}
#endregion
2019-01-08 17:40:12 -08:00
}
}