using System.Xml.Serialization; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using SabreTools.Core; using SabreTools.Core.Tools; using SabreTools.FileTypes; namespace SabreTools.DatItems.Formats { /// /// Represents a generic file within a set /// [JsonObject("rom"), XmlRoot("rom")] public class Rom : DatItem { #region Fields #region Common /// /// Name of the item /// [JsonProperty("name"), XmlElement("name")] public string? Name { get => _rom.ReadString(Models.Internal.Rom.NameKey); set => _rom[Models.Internal.Rom.NameKey] = value; } /// /// What BIOS is required for this rom /// [JsonProperty("bios", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("bios")] public string? Bios { get => _rom.ReadString(Models.Internal.Rom.BiosKey); set => _rom[Models.Internal.Rom.BiosKey] = value; } /// /// Byte size of the rom /// [JsonProperty("size", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("size")] public long? Size { get => _rom.ReadLong(Models.Internal.Rom.SizeKey); set => _rom[Models.Internal.Rom.SizeKey] = value; } [JsonIgnore] public bool SizeSpecified { get { return Size != null; } } /// /// File CRC32 hash /// [JsonProperty("crc", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("crc")] public string? CRC { get => _rom.ReadString(Models.Internal.Rom.CRCKey); set => _rom[Models.Internal.Rom.CRCKey] = TextHelper.NormalizeCRC32(value); } /// /// File MD5 hash /// [JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("md5")] public string? MD5 { get => _rom.ReadString(Models.Internal.Rom.MD5Key); set => _rom[Models.Internal.Rom.MD5Key] = TextHelper.NormalizeMD5(value); } /// /// File SHA-1 hash /// [JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha1")] public string? SHA1 { get => _rom.ReadString(Models.Internal.Rom.SHA1Key); set => _rom[Models.Internal.Rom.SHA1Key] = TextHelper.NormalizeSHA1(value); } /// /// File SHA-256 hash /// [JsonProperty("sha256", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha256")] public string? SHA256 { get => _rom.ReadString(Models.Internal.Rom.SHA256Key); set => _rom[Models.Internal.Rom.SHA256Key] = TextHelper.NormalizeSHA256(value); } /// /// File SHA-384 hash /// [JsonProperty("sha384", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha384")] public string? SHA384 { get => _rom.ReadString(Models.Internal.Rom.SHA384Key); set => _rom[Models.Internal.Rom.SHA384Key] = TextHelper.NormalizeSHA384(value); } /// /// File SHA-512 hash /// [JsonProperty("sha512", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha512")] public string? SHA512 { get => _rom.ReadString(Models.Internal.Rom.SHA512Key); set => _rom[Models.Internal.Rom.SHA512Key] = TextHelper.NormalizeSHA512(value); } /// /// File SpamSum fuzzy hash /// [JsonProperty("spamsum", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("spamsum")] public string? SpamSum { get => _rom.ReadString(Models.Internal.Rom.SpamSumKey); set => _rom[Models.Internal.Rom.SpamSumKey] = value; } /// /// Rom name to merge from parent /// [JsonProperty("merge", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("merge")] public string? MergeTag { get => _rom.ReadString(Models.Internal.Rom.MergeKey); set => _rom[Models.Internal.Rom.MergeKey] = value; } /// /// Rom region /// [JsonProperty("region", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("biregionos")] public string? Region { get => _rom.ReadString(Models.Internal.Rom.RegionKey); set => _rom[Models.Internal.Rom.RegionKey] = value; } /// /// Data offset within rom /// [JsonProperty("offset", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("offset")] public string? Offset { get => _rom.ReadString(Models.Internal.Rom.OffsetKey); set => _rom[Models.Internal.Rom.OffsetKey] = value; } /// /// File created date /// [JsonProperty("date", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("date")] public string? Date { get => _rom.ReadString(Models.Internal.Rom.DateKey); set => _rom[Models.Internal.Rom.DateKey] = value; } /// /// Rom dump status /// [JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("status")] [JsonConverter(typeof(StringEnumConverter))] public ItemStatus ItemStatus { get => _rom.ReadString(Models.Internal.Rom.StatusKey).AsItemStatus(); set => _rom[Models.Internal.Rom.StatusKey] = value.FromItemStatus(yesno: false); } [JsonIgnore] public bool ItemStatusSpecified { get { return ItemStatus != ItemStatus.NULL && ItemStatus != ItemStatus.None; } } /// /// Determine if the rom is optional in the set /// [JsonProperty("optional", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("optional")] public bool? Optional { get => _rom.ReadBool(Models.Internal.Rom.OptionalKey); set => _rom[Models.Internal.Rom.OptionalKey] = value; } [JsonIgnore] public bool OptionalSpecified { get { return Optional != null; } } /// /// Determine if the CRC32 hash is inverted /// [JsonProperty("inverted", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("inverted")] public bool? Inverted { get => _rom.ReadBool(Models.Internal.Rom.InvertedKey); set => _rom[Models.Internal.Rom.InvertedKey] = value; } [JsonIgnore] public bool InvertedSpecified { get { return Inverted != null; } } #endregion #region Archive.org /// /// Source of file /// [JsonProperty("ado_source", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("ado_source")] public string? ArchiveDotOrgSource { get => _rom.ReadString(Models.Internal.Rom.SourceKey); set => _rom[Models.Internal.Rom.SourceKey] = value; } /// /// Archive.org recognized file format /// [JsonProperty("ado_format", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("ado_format")] public string? ArchiveDotOrgFormat { get => _rom.ReadString(Models.Internal.Rom.FormatKey); set => _rom[Models.Internal.Rom.FormatKey] = value; } /// /// Original filename /// [JsonProperty("original_filename", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("original_filename")] public string? OriginalFilename { get => _rom.ReadString(Models.Internal.Rom.OriginalKey); set => _rom[Models.Internal.Rom.OriginalKey] = value; } /// /// Image rotation /// /// /// TODO: This might be Int32? /// [JsonProperty("rotation", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("rotation")] public string? Rotation { get => _rom.ReadString(Models.Internal.Rom.RotationKey); set => _rom[Models.Internal.Rom.RotationKey] = value; } /// /// Summation value? /// [JsonProperty("summation", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("summation")] public string? Summation { get => _rom.ReadString(Models.Internal.Rom.SummationKey); set => _rom[Models.Internal.Rom.SummationKey] = value; } #endregion #region AttractMode /// /// Alternate name for the item /// [JsonProperty("alt_romname", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("alt_romname")] public string? AltName { get => _rom.ReadString(Models.Internal.Rom.AltRomnameKey); set => _rom[Models.Internal.Rom.AltRomnameKey] = value; } /// /// Alternate title for the item /// [JsonProperty("alt_title", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("alt_title")] public string? AltTitle { get => _rom.ReadString(Models.Internal.Rom.AltTitleKey); set => _rom[Models.Internal.Rom.AltTitleKey] = value; } #endregion #region Logiqx /// /// Alternate title for the item /// [JsonProperty("mia", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("mia")] public bool? MIA { get => _rom.ReadBool(Models.Internal.Rom.MIAKey); set => _rom[Models.Internal.Rom.MIAKey] = value; } [JsonIgnore] public bool MIASpecified { get { return MIA != null; } } #endregion #region OpenMSX /// /// OpenMSX sub item type /// /// This is inverted from the internal model [JsonProperty("original", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("original")] public Original? Original { get; set; } [JsonIgnore] public bool OriginalSpecified { get { return Original != null && Original != default; } } /// /// OpenMSX sub item type /// [JsonProperty("openmsx_subtype", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("openmsx_subtype")] [JsonConverter(typeof(StringEnumConverter))] public OpenMSXSubType OpenMSXSubType { get => _rom.ReadString(Models.Internal.Rom.OpenMSXMediaType).AsOpenMSXSubType(); set => _rom[Models.Internal.Rom.OpenMSXMediaType] = value.FromOpenMSXSubType(); } [JsonIgnore] public bool OpenMSXSubTypeSpecified { get { return OpenMSXSubType != OpenMSXSubType.NULL; } } /// /// OpenMSX sub item type /// /// Not related to the subtype above [JsonProperty("openmsx_type", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("openmsx_type")] public string? OpenMSXType { get => _rom.ReadString(Models.Internal.Rom.OpenMSXType); set => _rom[Models.Internal.Rom.OpenMSXType] = value; } /// /// Item remark (like a comment) /// [JsonProperty("remark", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("remark")] public string? Remark { get => _rom.ReadString(Models.Internal.Rom.RemarkKey); set => _rom[Models.Internal.Rom.RemarkKey] = value; } /// /// Boot state /// /// TODO: Investigate where this value came from? [JsonProperty("boot", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("boot")] public string? Boot { get; set; } #endregion #region SoftwareList /// /// Data area information /// /// This is inverted from the internal model [JsonProperty("dataarea", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("dataarea")] public DataArea? DataArea { get; set; } = null; [JsonIgnore] public bool DataAreaSpecified { get { return DataArea != null && (!string.IsNullOrEmpty(DataArea.Name) || DataArea.SizeSpecified || DataArea.WidthSpecified || DataArea.EndiannessSpecified); } } /// /// Loading flag /// [JsonProperty("loadflag", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("loadflag")] [JsonConverter(typeof(StringEnumConverter))] public LoadFlag LoadFlag { get => _rom.ReadString(Models.Internal.Rom.LoadFlagKey).AsLoadFlag(); set => _rom[Models.Internal.Rom.LoadFlagKey] = value.FromLoadFlag(); } [JsonIgnore] public bool LoadFlagSpecified { get { return LoadFlag != LoadFlag.NULL; } } /// /// Original hardware part associated with the item /// /// This is inverted from the internal model [JsonProperty("part", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("part")] public Part? Part { get; set; } = null; [JsonIgnore] public bool PartSpecified { get { return Part != null && (!string.IsNullOrEmpty(Part.Name) || !string.IsNullOrEmpty(Part.Interface)); } } /// /// SoftwareList value associated with the item /// [JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("value")] public string? Value { get => _rom.ReadString(Models.Internal.Rom.ValueKey); set => _rom[Models.Internal.Rom.ValueKey] = value; } #endregion /// /// Internal Rom model /// [JsonIgnore] private Models.Internal.Rom _rom = new(); #endregion // Fields #region Accessors /// public override string? GetName() => Name; /// public override void SetName(string? name) => Name = name; #endregion #region Constructors /// /// Create a default, empty Rom object /// public Rom() { Name = null; ItemType = ItemType.Rom; DupeType = 0x00; ItemStatus = ItemStatus.None; } /// /// Create a "blank" Rom object /// /// /// /// public Rom(string name, string machineName) { Name = name; ItemType = ItemType.Rom; Size = null; ItemStatus = ItemStatus.None; Machine = new Machine { Name = machineName, Description = machineName, }; } /// /// Create a Rom object from a BaseFile /// /// public Rom(BaseFile baseFile) { Name = baseFile.Filename; Size = baseFile.Size; CRC = Utilities.ByteArrayToString(baseFile.CRC); MD5 = Utilities.ByteArrayToString(baseFile.MD5); SHA1 = Utilities.ByteArrayToString(baseFile.SHA1); SHA256 = Utilities.ByteArrayToString(baseFile.SHA256); SHA384 = Utilities.ByteArrayToString(baseFile.SHA384); SHA512 = Utilities.ByteArrayToString(baseFile.SHA512); SpamSum = Utilities.ByteArrayToString(baseFile.SpamSum); ItemType = ItemType.Rom; DupeType = 0x00; ItemStatus = ItemStatus.None; Date = baseFile.Date; } #endregion #region Cloning Methods /// public override object Clone() { return new Rom() { Name = this.Name, ItemType = this.ItemType, DupeType = this.DupeType, Machine = this.Machine?.Clone() as Machine, Source = this.Source?.Clone() as Source, Remove = this.Remove, _rom = this._rom?.Clone() as Models.Internal.Rom ?? new Models.Internal.Rom(), DataArea = this.DataArea, Part = this.Part, }; } /// /// Convert Rom object to a BaseFile /// public BaseFile ConvertToBaseFile() { return new BaseFile() { Filename = this.Name, Parent = this.Machine?.Name, Date = this.Date, Size = this.Size, CRC = Utilities.StringToByteArray(this.CRC), MD5 = Utilities.StringToByteArray(this.MD5), SHA1 = Utilities.StringToByteArray(this.SHA1), SHA256 = Utilities.StringToByteArray(this.SHA256), SHA384 = Utilities.StringToByteArray(this.SHA384), SHA512 = Utilities.StringToByteArray(this.SHA512), SpamSum = Utilities.StringToByteArray(this.SpamSum), }; } #endregion #region Comparision Methods /// public override bool Equals(DatItem? other) { // If we don't have a Rom, return false if (ItemType != other?.ItemType || other is not Rom otherInternal) return false; // Compare the internal models return _rom.EqualTo(otherInternal._rom); } /// /// Fill any missing size and hash information from another Rom /// /// Rom to fill information from public void FillMissingInformation(Rom other) { if (Size == null && other.Size != null) Size = other.Size; if (string.IsNullOrWhiteSpace(CRC) && !string.IsNullOrWhiteSpace(other.CRC)) CRC = other.CRC; if (string.IsNullOrWhiteSpace(MD5) && !string.IsNullOrWhiteSpace(other.MD5)) MD5 = other.MD5; if (string.IsNullOrWhiteSpace(SHA1) && !string.IsNullOrWhiteSpace(other.SHA1)) SHA1 = other.SHA1; if (string.IsNullOrWhiteSpace(SHA256) && !string.IsNullOrWhiteSpace(other.SHA256)) SHA256 = other.SHA256; if (string.IsNullOrWhiteSpace(SHA384) && !string.IsNullOrWhiteSpace(other.SHA384)) SHA384 = other.SHA384; if (string.IsNullOrWhiteSpace(SHA512) && !string.IsNullOrWhiteSpace(other.SHA512)) SHA512 = other.SHA512; if (string.IsNullOrWhiteSpace(SpamSum) && !string.IsNullOrWhiteSpace(other.SpamSum)) SpamSum = other.SpamSum; } /// /// Get unique duplicate suffix on name collision /// /// String representing the suffix public string GetDuplicateSuffix() { if (!string.IsNullOrWhiteSpace(CRC)) return $"_{CRC}"; else if (!string.IsNullOrWhiteSpace(MD5)) return $"_{MD5}"; else if (!string.IsNullOrWhiteSpace(SHA1)) return $"_{SHA1}"; else if (!string.IsNullOrWhiteSpace(SHA256)) return $"_{SHA256}"; else if (!string.IsNullOrWhiteSpace(SHA384)) return $"_{SHA384}"; else if (!string.IsNullOrWhiteSpace(SHA512)) return $"_{SHA512}"; else if (!string.IsNullOrWhiteSpace(SpamSum)) return $"_{SpamSum}"; else return "_1"; } /// /// Returns if the Rom contains any hashes /// /// True if any hash exists, false otherwise public bool HasHashes() => _rom.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() => _rom.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 = CRC; break; case ItemKey.MD5: key = MD5; break; case ItemKey.SHA1: key = SHA1; break; case ItemKey.SHA256: key = SHA256; break; case ItemKey.SHA384: key = SHA384; break; case ItemKey.SHA512: key = SHA512; break; case ItemKey.SpamSum: key = SpamSum; 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; return key; } #endregion } }