using System.Xml.Serialization;
using Newtonsoft.Json;
using SabreTools.Core;
using SabreTools.Core.Tools;
namespace SabreTools.DatItems.Formats
{
///
/// Represents a generic file within a set
///
[JsonObject("rom"), XmlRoot("rom")]
public sealed class Rom : DatItem
{
#region Constants
///
/// Non-standard key for inverted logic
///
public const string DataAreaKey = "DATAAREA";
///
/// Non-standard key for inverted logic
///
public const string PartKey = "PART";
#endregion
#region Fields
/// />
protected override ItemType ItemType => ItemType.Rom;
/// />
protected override string? NameKey => Models.Metadata.Rom.NameKey;
[JsonIgnore]
public bool ItemStatusSpecified
{
get
{
var status = GetStringFieldValue(Models.Metadata.Rom.StatusKey).AsEnumValue();
return status != ItemStatus.NULL && status != ItemStatus.None;
}
}
[JsonIgnore]
public bool OriginalSpecified
{
get
{
var original = GetFieldValue("ORIGINAL");
return original != null && original != default;
}
}
[JsonIgnore]
public bool DataAreaSpecified
{
get
{
var dataArea = GetFieldValue(Rom.DataAreaKey);
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.NULL);
}
}
[JsonIgnore]
public bool PartSpecified
{
get
{
var part = GetFieldValue(Rom.PartKey);
return part != null
&& (!string.IsNullOrEmpty(part.GetName())
|| !string.IsNullOrEmpty(part.GetStringFieldValue(Models.Metadata.Part.InterfaceKey)));
}
}
#endregion
#region Constructors
public Rom() : base()
{
SetFieldValue(DatItem.DupeTypeKey, 0x00);
SetFieldValue(Models.Metadata.Rom.StatusKey, ItemStatus.None.AsStringValue());
}
public Rom(Models.Metadata.Rom item) : base(item)
{
SetFieldValue(DatItem.DupeTypeKey, 0x00);
}
#endregion
#region Comparision Methods
///
/// Fill any missing size and hash information from another Rom
///
/// Rom to fill information from
public void FillMissingInformation(Rom other)
=> _internal.FillMissingHashes(other._internal);
///
/// Get unique duplicate suffix on name collision
///
/// String representing the suffix
public string GetDuplicateSuffix() => _internal.GetDuplicateSuffix();
///
/// Returns if the Rom contains any hashes
///
/// True if any hash exists, false otherwise
public bool HasHashes() => _internal.HasHashes();
///
/// Returns if all of the hashes are set to their 0-byte values
///
/// True if any hash matches the 0-byte value, false otherwise
public bool HasZeroHash() => _internal.HasZeroHash();
#endregion
#region Sorting and Merging
///
public override string GetKey(ItemKey bucketedBy, bool lower = true, bool norename = true)
{
// Set the output key as the default blank string
string? key;
// Now determine what the key should be based on the bucketedBy value
switch (bucketedBy)
{
case ItemKey.CRC:
key = GetStringFieldValue(Models.Metadata.Rom.CRCKey);
break;
case ItemKey.MD5:
key = GetStringFieldValue(Models.Metadata.Rom.MD5Key);
break;
case ItemKey.SHA1:
key = GetStringFieldValue(Models.Metadata.Rom.SHA1Key);
break;
case ItemKey.SHA256:
key = GetStringFieldValue(Models.Metadata.Rom.SHA256Key);
break;
case ItemKey.SHA384:
key = GetStringFieldValue(Models.Metadata.Rom.SHA384Key);
break;
case ItemKey.SHA512:
key = GetStringFieldValue(Models.Metadata.Rom.SHA512Key);
break;
case ItemKey.SpamSum:
key = GetStringFieldValue(Models.Metadata.Rom.SpamSumKey);
break;
// 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;
if (lower)
key = key.ToLowerInvariant();
return key;
}
#endregion
}
}