using System; using System.Linq; using System.Text; using System.Xml.Serialization; using SabreTools.Core; using SabreTools.Core.Tools; using SabreTools.FileTypes; using Newtonsoft.Json; using Newtonsoft.Json.Converters; namespace SabreTools.DatItems.Formats { /// /// Represents a generic file within a set /// [JsonObject("rom"), XmlRoot("rom")] public class Rom : DatItem { #region Private instance variables private byte[] _crc; // 8 bytes private byte[] _md5; // 16 bytes private byte[] _sha1; // 20 bytes private byte[] _sha256; // 32 bytes private byte[] _sha384; // 48 bytes private byte[] _sha512; // 64 bytes private byte[] _spamsum; // variable bytes #endregion #region Fields #region Common /// /// Name of the item /// [JsonProperty("name")] [XmlElement("name")] public string Name { get; set; } /// /// What BIOS is required for this rom /// [JsonProperty("bios", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("bios")] public string Bios { get; set; } /// /// Byte size of the rom /// [JsonProperty("size", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("size")] public long? Size { get; set; } = null; [JsonIgnore] public bool SizeSpecified { get { return Size != null; } } /// /// File CRC32 hash /// [JsonProperty("crc", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("crc")] public string CRC { get { return _crc.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_crc); } set { _crc = (value == "null" ? Constants.CRCZeroBytes : Utilities.StringToByteArray(CleanCRC32(value))); } } /// /// File MD5 hash /// [JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("md5")] public string MD5 { get { return _md5.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_md5); } set { _md5 = Utilities.StringToByteArray(CleanMD5(value)); } } /// /// File SHA-1 hash /// [JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("sha1")] public string SHA1 { get { return _sha1.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha1); } set { _sha1 = Utilities.StringToByteArray(CleanSHA1(value)); } } /// /// File SHA-256 hash /// [JsonProperty("sha256", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("sha256")] public string SHA256 { get { return _sha256.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha256); } set { _sha256 = Utilities.StringToByteArray(CleanSHA256(value)); } } /// /// File SHA-384 hash /// [JsonProperty("sha384", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("sha384")] public string SHA384 { get { return _sha384.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha384); } set { _sha384 = Utilities.StringToByteArray(CleanSHA384(value)); } } /// /// File SHA-512 hash /// [JsonProperty("sha512", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("sha512")] public string SHA512 { get { return _sha512.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha512); } set { _sha512 = Utilities.StringToByteArray(CleanSHA512(value)); } } /// /// File SpamSum fuzzy hash /// [JsonProperty("spamsum", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("spamsum")] public string SpamSum { get { return _spamsum.IsNullOrEmpty() ? null : Encoding.UTF8.GetString(_spamsum); } set { _spamsum = Encoding.UTF8.GetBytes(value ?? string.Empty); } } /// /// Rom name to merge from parent /// [JsonProperty("merge", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("merge")] public string MergeTag { get; set; } /// /// Rom region /// [JsonProperty("region", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("biregionos")] public string Region { get; set; } /// /// Data offset within rom /// [JsonProperty("offset", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("offset")] public string Offset { get; set; } /// /// File created date /// [JsonProperty("date", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("date")] public string Date { get; set; } /// /// Rom dump status /// [JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)] [JsonConverter(typeof(StringEnumConverter))] [XmlElement("status")] public ItemStatus ItemStatus { get; set; } [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; set; } = null; [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; set; } = null; [JsonIgnore] public bool InvertedSpecified { get { return Inverted != null; } } #endregion #region AttractMode /// /// Alternate name for the item /// [JsonProperty("alt_romname", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("alt_romname")] public string AltName { get; set; } /// /// Alternate title for the item /// [JsonProperty("alt_title", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("alt_title")] public string AltTitle { get; set; } #endregion #region OpenMSX /// /// OpenMSX sub item type /// [JsonProperty("original", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("original")] public Original Original { get; set; } = null; [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; set; } [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; set; } /// /// Item remark (like a comment) /// [JsonProperty("remark", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("remark")] public string Remark { get; set; } /// /// Boot state /// [JsonProperty("boot", DefaultValueHandling = DefaultValueHandling.Ignore)] [XmlElement("boot")] public string Boot { get; set; } #endregion #region SoftwareList /// /// Data area information /// [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; set; } [JsonIgnore] public bool LoadFlagSpecified { get { return LoadFlag != LoadFlag.NULL; } } /// /// Original hardware part associated with the item /// [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; set; } #endregion #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 = baseFile.CRC; _md5 = baseFile.MD5; _sha1 = baseFile.SHA1; _sha256 = baseFile.SHA256; _sha384 = baseFile.SHA384; _sha512 = baseFile.SHA512; _spamsum = 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, Bios = this.Bios, Size = this.Size, _crc = this._crc, _md5 = this._md5, _sha1 = this._sha1, _sha256 = this._sha256, _sha384 = this._sha384, _sha512 = this._sha512, _spamsum = this._spamsum, MergeTag = this.MergeTag, Region = this.Region, Offset = this.Offset, Date = this.Date, ItemStatus = this.ItemStatus, Optional = this.Optional, Inverted = this.Inverted, AltName = this.AltName, AltTitle = this.AltTitle, Original = this.Original, OpenMSXSubType = this.OpenMSXSubType, OpenMSXType = this.OpenMSXType, Remark = this.Remark, Boot = this.Boot, DataArea = this.DataArea, LoadFlag = this.LoadFlag, Part = this.Part, Value = this.Value, }; } /// /// 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 = this._crc, MD5 = this._md5, SHA1 = this._sha1, SHA256 = this._sha256, SHA384 = this._sha384, SHA512 = this._sha512, SpamSum = this._spamsum, }; } #endregion #region Comparision Methods public override bool Equals(DatItem other) { bool dupefound = false; // If we don't have a rom, return false if (ItemType != other.ItemType) return dupefound; // Otherwise, treat it as a Rom Rom newOther = other as Rom; // If all hashes are empty but they're both nodump and the names match, then they're dupes if ((ItemStatus == ItemStatus.Nodump && newOther.ItemStatus == ItemStatus.Nodump) && Name == newOther.Name && !HasHashes() && !newOther.HasHashes()) { dupefound = true; } // If we have a file that has no known size, rely on the hashes only else if (Size == null && HashMatch(newOther)) { dupefound = true; } // Otherwise if we get a partial match else if (Size == newOther.Size && HashMatch(newOther)) { dupefound = true; } return dupefound; } /// /// 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 (_crc.IsNullOrEmpty() && !other._crc.IsNullOrEmpty()) _crc = other._crc; if (_md5.IsNullOrEmpty() && !other._md5.IsNullOrEmpty()) _md5 = other._md5; if (_sha1.IsNullOrEmpty() && !other._sha1.IsNullOrEmpty()) _sha1 = other._sha1; if (_sha256.IsNullOrEmpty() && !other._sha256.IsNullOrEmpty()) _sha256 = other._sha256; if (_sha384.IsNullOrEmpty() && !other._sha384.IsNullOrEmpty()) _sha384 = other._sha384; if (_sha512.IsNullOrEmpty() && !other._sha512.IsNullOrEmpty()) _sha512 = other._sha512; if (_spamsum.IsNullOrEmpty() && !other._spamsum.IsNullOrEmpty()) _spamsum = other._spamsum; } /// /// Get unique duplicate suffix on name collision /// /// String representing the suffix public string GetDuplicateSuffix() { if (!_crc.IsNullOrEmpty()) return $"_{CRC}"; else if (!_md5.IsNullOrEmpty()) return $"_{MD5}"; else if (!_sha1.IsNullOrEmpty()) return $"_{SHA1}"; else if (!_sha256.IsNullOrEmpty()) return $"_{SHA256}"; else if (!_sha384.IsNullOrEmpty()) return $"_{SHA384}"; else if (!_sha512.IsNullOrEmpty()) return $"_{SHA512}"; else if (!_spamsum.IsNullOrEmpty()) return $"_{SpamSum}"; else return "_1"; } /// /// Returns if the Rom contains any hashes /// /// True if any hash exists, false otherwise public bool HasHashes() { return !_crc.IsNullOrEmpty() || !_md5.IsNullOrEmpty() || !_sha1.IsNullOrEmpty() || !_sha256.IsNullOrEmpty() || !_sha384.IsNullOrEmpty() || !_sha512.IsNullOrEmpty() || !_spamsum.IsNullOrEmpty(); } /// /// 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() { return (_crc != null && _crc.SequenceEqual(Constants.CRCZeroBytes)) || (_md5 != null && _md5.SequenceEqual(Constants.MD5ZeroBytes)) || (_sha1 != null && _sha1.SequenceEqual(Constants.SHA1ZeroBytes)) || (_sha256 != null && _sha256.SequenceEqual(Constants.SHA256ZeroBytes)) || (_sha384 != null && _sha384.SequenceEqual(Constants.SHA384ZeroBytes)) || (_sha512 != null && _sha512.SequenceEqual(Constants.SHA512ZeroBytes)) || (_spamsum != null && _spamsum.SequenceEqual(Constants.SpamSumZeroBytes)); } /// /// Returns if there are no, non-empty hashes in common with another Rom /// /// Rom to compare against /// True if at least one hash is not mutually exclusive, false otherwise private bool HasCommonHash(Rom other) { return !(_crc.IsNullOrEmpty() ^ other._crc.IsNullOrEmpty()) || !(_md5.IsNullOrEmpty() ^ other._md5.IsNullOrEmpty()) || !(_sha1.IsNullOrEmpty() ^ other._sha1.IsNullOrEmpty()) || !(_sha256.IsNullOrEmpty() ^ other._sha256.IsNullOrEmpty()) || !(_sha384.IsNullOrEmpty() ^ other._sha384.IsNullOrEmpty()) || !(_sha512.IsNullOrEmpty() ^ other._sha512.IsNullOrEmpty()) || !(_spamsum.IsNullOrEmpty() ^ other._spamsum.IsNullOrEmpty()); } /// /// Returns if any hashes are common with another Rom /// /// Rom to compare against /// True if any hashes are in common, false otherwise private bool HashMatch(Rom other) { // If either have no hashes, we return false, otherwise this would be a false positive if (!HasHashes() || !other.HasHashes()) return false; // If neither have hashes in common, we return false, otherwise this would be a false positive if (!HasCommonHash(other)) return false; // Return if all hashes match according to merge rules return ConditionalHashEquals(_crc, other._crc) && ConditionalHashEquals(_md5, other._md5) && ConditionalHashEquals(_sha1, other._sha1) && ConditionalHashEquals(_sha256, other._sha256) && ConditionalHashEquals(_sha384, other._sha384) && ConditionalHashEquals(_sha512, other._sha512) && ConditionalHashEquals(_spamsum, other._spamsum); } #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 if (key == null) key = string.Empty; return key; } #endregion } }