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

722 lines
24 KiB
C#
Raw Normal View History

2020-08-21 10:16:05 -07:00
using System;
using System.Linq;
using System.Text;
2020-09-07 14:47:27 -07:00
using System.Xml.Serialization;
2020-08-20 13:17:14 -07:00
2020-12-08 13:23:59 -08:00
using SabreTools.Core;
using SabreTools.Core.Tools;
2020-12-08 14:53:49 -08:00
using SabreTools.FileTypes;
2020-06-15 21:00:09 -07:00
using Newtonsoft.Json;
2020-08-23 22:23:55 -07:00
using Newtonsoft.Json.Converters;
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")]
2019-01-08 17:40:12 -08:00
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
2019-01-08 17:40:12 -08:00
#endregion
2020-08-20 13:17:14 -07:00
#region Fields
2019-01-08 17:40:12 -08:00
#region Common
2020-09-02 12:19:12 -07:00
/// <summary>
/// Name of the item
/// </summary>
[JsonProperty("name")]
2020-09-07 22:00:02 -07:00
[XmlElement("name")]
2020-09-02 12:19:12 -07:00
public string Name { get; set; }
2019-01-08 17:40:12 -08:00
/// <summary>
/// What BIOS is required for this rom
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("bios", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("bios")]
2019-01-08 17:40:12 -08:00
public string Bios { get; set; }
/// <summary>
/// Byte size of the rom
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("size", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("size")]
2020-09-07 22:33:48 -07:00
public long? Size { get; set; } = null;
2019-01-08 17:40:12 -08:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool SizeSpecified { get { return Size != null; } }
2019-01-08 17:40:12 -08:00
/// <summary>
/// File CRC32 hash
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("crc", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("crc")]
2019-01-08 17:40:12 -08:00
public string CRC
{
get { return _crc.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_crc); }
set { _crc = (value == "null" ? Constants.CRCZeroBytes : Utilities.StringToByteArray(CleanCRC32(value))); }
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// File MD5 hash
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("md5")]
2019-01-08 17:40:12 -08:00
public string MD5
{
get { return _md5.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_md5); }
set { _md5 = Utilities.StringToByteArray(CleanMD5(value)); }
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// File SHA-1 hash
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("sha1")]
2019-01-08 17:40:12 -08:00
public string SHA1
{
get { return _sha1.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha1); }
set { _sha1 = Utilities.StringToByteArray(CleanSHA1(value)); }
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// File SHA-256 hash
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("sha256", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("sha256")]
2019-01-08 17:40:12 -08:00
public string SHA256
{
get { return _sha256.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha256); }
set { _sha256 = Utilities.StringToByteArray(CleanSHA256(value)); }
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// File SHA-384 hash
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("sha384", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("sha384")]
2019-01-08 17:40:12 -08:00
public string SHA384
{
get { return _sha384.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha384); }
set { _sha384 = Utilities.StringToByteArray(CleanSHA384(value)); }
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// File SHA-512 hash
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("sha512", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("sha512")]
2019-01-08 17:40:12 -08:00
public string SHA512
{
get { return _sha512.IsNullOrEmpty() ? null : Utilities.ByteArrayToString(_sha512); }
set { _sha512 = Utilities.StringToByteArray(CleanSHA512(value)); }
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// File SpamSum fuzzy hash
/// </summary>
[JsonProperty("spamsum", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("spamsum")]
public string SpamSum
{
get { return _spamsum.IsNullOrEmpty() ? null : Encoding.UTF8.GetString(_spamsum); }
2020-09-07 13:41:08 -07:00
set { _spamsum = Encoding.UTF8.GetBytes(value ?? string.Empty); }
}
2019-01-08 17:40:12 -08:00
/// <summary>
/// Rom name to merge from parent
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("merge", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("merge")]
2019-01-08 17:40:12 -08:00
public string MergeTag { get; set; }
/// <summary>
/// Rom region
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("region", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("biregionos")]
2019-01-08 17:40:12 -08:00
public string Region { get; set; }
/// <summary>
/// Data offset within rom
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("offset", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("offset")]
2019-01-08 17:40:12 -08:00
public string Offset { get; set; }
/// <summary>
/// File created date
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("date", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("date")]
2019-01-08 17:40:12 -08:00
public string Date { get; set; }
/// <summary>
/// Rom dump status
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-08-23 22:23:55 -07:00
[JsonConverter(typeof(StringEnumConverter))]
2020-09-07 14:47:27 -07:00
[XmlElement("status")]
2019-01-08 17:40:12 -08:00
public ItemStatus ItemStatus { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ItemStatusSpecified { get { return ItemStatus != ItemStatus.NULL && ItemStatus != ItemStatus.None; } }
2019-01-08 17:40:12 -08:00
/// <summary>
/// Determine if the rom is optional in the set
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("optional", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("optional")]
2020-09-07 22:33:48 -07:00
public bool? Optional { get; set; } = null;
2019-01-08 17:40:12 -08:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool OptionalSpecified { get { return Optional != null; } }
2020-07-28 17:00:19 -07:00
/// <summary>
/// Determine if the CRC32 hash is inverted
/// </summary>
2020-08-24 11:56:49 -07:00
[JsonProperty("inverted", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("inverted")]
2020-09-07 22:33:48 -07:00
public bool? Inverted { get; set; } = null;
2020-07-28 17:00:19 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool InvertedSpecified { get { return Inverted != null; } }
2019-01-08 17:40:12 -08:00
#endregion
#region Archive.org
/// <summary>
/// Source of file
/// </summary>
[JsonProperty("ado_source", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("ado_source")]
public string ArchiveDotOrgSource { get; set; }
/// <summary>
/// Archive.org recognized file format
/// </summary>
[JsonProperty("ado_format", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("ado_format")]
public string ArchiveDotOrgFormat { get; set; }
/// <summary>
/// Original filename
/// </summary>
[JsonProperty("original_filename", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("original_filename")]
public string OriginalFilename { get; set; }
/// <summary>
/// Image rotation
/// </summary>
/// <remarks>
/// TODO: This might be Int32?
/// </remarks>
[JsonProperty("rotation", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("rotation")]
public string Rotation { get; set; }
/// <summary>
/// Image rotation
/// </summary>
[JsonProperty("summation", DefaultValueHandling = DefaultValueHandling.Ignore)]
[XmlElement("summation")]
public string Summation { get; set; }
#endregion
2020-09-03 13:01:33 -07:00
#region AttractMode
/// <summary>
/// Alternate name for the item
/// </summary>
[JsonProperty("alt_romname", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("alt_romname")]
2020-09-03 13:01:33 -07:00
public string AltName { get; set; }
/// <summary>
/// Alternate title for the item
/// </summary>
[JsonProperty("alt_title", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("alt_title")]
2020-09-03 13:01:33 -07:00
public string AltTitle { get; set; }
#endregion
#region OpenMSX
/// <summary>
/// OpenMSX sub item type
/// </summary>
[JsonProperty("original", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("original")]
2020-09-07 22:33:48 -07:00
public Original Original { get; set; } = null;
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool OriginalSpecified { get { return Original != null && Original != default; } }
/// <summary>
/// OpenMSX sub item type
/// </summary>
[JsonProperty("openmsx_subtype", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("openmsx_subtype")]
[JsonConverter(typeof(StringEnumConverter))]
public OpenMSXSubType OpenMSXSubType { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool OpenMSXSubTypeSpecified { get { return OpenMSXSubType != OpenMSXSubType.NULL; } }
/// <summary>
/// OpenMSX sub item type
/// </summary>
/// <remarks>Not related to the subtype above</remarks>
[JsonProperty("openmsx_type", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("openmsx_type")]
public string OpenMSXType { get; set; }
/// <summary>
/// Item remark (like a comment)
/// </summary>
[JsonProperty("remark", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("remark")]
public string Remark { get; set; }
/// <summary>
/// Boot state
/// </summary>
[JsonProperty("boot", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("boot")]
public string Boot { get; set; }
#endregion
#region SoftwareList
/// <summary>
/// Data area information
/// </summary>
[JsonProperty("dataarea", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("dataarea")]
2020-09-07 22:33:48 -07:00
public DataArea DataArea { get; set; } = null;
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool DataAreaSpecified
{
get
{
2020-09-07 22:33:48 -07:00
return DataArea != null
&& (!string.IsNullOrEmpty(DataArea.Name)
2020-09-23 16:24:59 -07:00
|| DataArea.SizeSpecified
|| DataArea.WidthSpecified
|| DataArea.EndiannessSpecified);
2020-09-07 22:00:02 -07:00
}
}
2020-09-03 12:06:54 -07:00
/// <summary>
/// Loading flag
/// </summary>
[JsonProperty("loadflag", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("loadflag")]
[JsonConverter(typeof(StringEnumConverter))]
public LoadFlag LoadFlag { get; set; }
2020-09-03 12:06:54 -07:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool LoadFlagSpecified { get { return LoadFlag != LoadFlag.NULL; } }
/// <summary>
/// Original hardware part associated with the item
/// </summary>
[JsonProperty("part", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("part")]
2020-09-07 22:33:48 -07:00
public Part Part { get; set; } = null;
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool PartSpecified
{
get
{
2020-09-07 22:33:48 -07:00
return Part != null
&& (!string.IsNullOrEmpty(Part.Name)
|| !string.IsNullOrEmpty(Part.Interface));
2020-09-07 22:00:02 -07:00
}
}
/// <summary>
/// SoftwareList value associated with the item
/// </summary>
[JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore)]
2020-09-07 14:47:27 -07:00
[XmlElement("value")]
public string Value { get; set; }
#endregion
#endregion // Fields
2020-08-17 17:28:32 -07:00
#region Accessors
/// <inheritdoc/>
2020-12-14 10:15:28 -08:00
public override string GetName() => Name;
2020-09-02 12:19:12 -07:00
/// <inheritdoc/>
2020-12-14 10:15:28 -08:00
public override void SetName(string name) => Name = name;
2020-08-17 17:28:32 -07:00
#endregion
2019-01-08 17:40:12 -08:00
#region Constructors
/// <summary>
/// Create a default, empty Rom object
/// </summary>
public Rom()
{
2020-08-24 13:59:59 -07:00
Name = null;
2020-08-20 13:17:14 -07:00
ItemType = ItemType.Rom;
DupeType = 0x00;
ItemStatus = ItemStatus.None;
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// Create a "blank" Rom object
/// </summary>
/// <param name="name"></param>
/// <param name="machineName"></param>
/// <param name="omitFromScan"></param>
public Rom(string name, string machineName)
2019-01-08 17:40:12 -08:00
{
2020-08-20 13:17:14 -07:00
Name = name;
ItemType = ItemType.Rom;
2020-09-04 23:03:27 -07:00
Size = null;
2020-08-20 13:17:14 -07:00
ItemStatus = ItemStatus.None;
2019-01-08 17:40:12 -08:00
2020-08-20 13:17:14 -07:00
Machine = new Machine
2019-01-08 17:40:12 -08:00
{
Name = machineName,
Description = machineName,
};
}
/// <summary>
/// Create a Rom object from a BaseFile
/// </summary>
/// <param name="baseFile"></param>
public Rom(BaseFile baseFile)
{
2020-08-20 13:17:14 -07:00
Name = baseFile.Filename;
2020-09-04 23:03:27 -07:00
Size = baseFile.Size;
2019-01-08 17:40:12 -08:00
_crc = baseFile.CRC;
_md5 = baseFile.MD5;
_sha1 = baseFile.SHA1;
_sha256 = baseFile.SHA256;
_sha384 = baseFile.SHA384;
_sha512 = baseFile.SHA512;
_spamsum = baseFile.SpamSum;
2019-01-08 17:40:12 -08:00
2020-08-20 13:17:14 -07:00
ItemType = ItemType.Rom;
DupeType = 0x00;
ItemStatus = ItemStatus.None;
Date = baseFile.Date;
2019-01-08 17:40:12 -08:00
}
#endregion
#region Cloning Methods
public override object Clone()
{
return new Rom()
{
Name = this.Name,
ItemType = this.ItemType,
DupeType = this.DupeType,
2020-08-20 13:17:14 -07:00
Machine = this.Machine.Clone() as Machine,
Source = this.Source.Clone() as Source,
Remove = this.Remove,
2019-01-08 17:40:12 -08:00
2020-08-20 13:17:14 -07:00
Bios = this.Bios,
2019-01-08 17:40:12 -08:00
Size = this.Size,
_crc = this._crc,
_md5 = this._md5,
_sha1 = this._sha1,
_sha256 = this._sha256,
_sha384 = this._sha384,
_sha512 = this._sha512,
_spamsum = this._spamsum,
2020-08-20 13:17:14 -07:00
MergeTag = this.MergeTag,
Region = this.Region,
Offset = this.Offset,
2019-01-08 17:40:12 -08:00
Date = this.Date,
2020-08-20 13:17:14 -07:00
ItemStatus = this.ItemStatus,
Optional = this.Optional,
Inverted = this.Inverted,
2020-09-03 13:01:33 -07:00
AltName = this.AltName,
AltTitle = this.AltTitle,
Original = this.Original,
OpenMSXSubType = this.OpenMSXSubType,
OpenMSXType = this.OpenMSXType,
Remark = this.Remark,
Boot = this.Boot,
DataArea = this.DataArea,
2020-09-03 13:01:33 -07:00
LoadFlag = this.LoadFlag,
Part = this.Part,
Value = this.Value,
2019-01-08 17:40:12 -08:00
};
}
2020-12-08 11:09:05 -08:00
/// <summary>
/// Convert Rom object to a BaseFile
/// </summary>
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,
};
}
2019-01-08 17:40:12 -08:00
#endregion
#region Comparision Methods
public override bool Equals(DatItem other)
{
bool dupefound = false;
// If we don't have a rom, return false
2020-08-17 14:57:54 -07:00
if (ItemType != other.ItemType)
2019-01-08 17:40:12 -08:00
return dupefound;
// Otherwise, treat it as a Rom
Rom newOther = other as Rom;
2019-01-08 17:40:12 -08:00
// If all hashes are empty but they're both nodump and the names match, then they're dupes
2020-08-17 14:57:54 -07:00
if ((ItemStatus == ItemStatus.Nodump && newOther.ItemStatus == ItemStatus.Nodump)
&& Name == newOther.Name
&& !HasHashes() && !newOther.HasHashes())
2019-01-08 17:40:12 -08:00
{
dupefound = true;
}
2019-09-20 10:55:03 -07:00
// If we have a file that has no known size, rely on the hashes only
2020-09-04 23:03:27 -07:00
else if (Size == null && HashMatch(newOther))
2019-09-20 10:55:03 -07:00
{
dupefound = true;
}
2019-01-08 17:40:12 -08:00
// Otherwise if we get a partial match
2020-08-17 14:57:54 -07:00
else if (Size == newOther.Size && HashMatch(newOther))
2019-01-08 17:40:12 -08:00
{
dupefound = true;
}
return dupefound;
}
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>
public void FillMissingInformation(Rom other)
{
2020-09-04 23:03:27 -07:00
if (Size == null && other.Size != null)
2020-08-17 17:28:32 -07:00
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;
2020-08-17 17:28:32 -07:00
}
/// <summary>
/// Get unique duplicate suffix on name collision
/// </summary>
/// <returns>String representing the suffix</returns>
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}";
2020-08-17 17:28:32 -07:00
else
return "_1";
}
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>
public bool HasHashes()
{
return !_crc.IsNullOrEmpty()
|| !_md5.IsNullOrEmpty()
|| !_sha1.IsNullOrEmpty()
|| !_sha256.IsNullOrEmpty()
|| !_sha384.IsNullOrEmpty()
|| !_sha512.IsNullOrEmpty()
|| !_spamsum.IsNullOrEmpty();
}
/// <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>
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));
}
2020-08-17 14:57:54 -07:00
/// <summary>
/// Returns if there are no, non-empty hashes in common with another Rom
/// </summary>
/// <param name="other">Rom to compare against</param>
/// <returns>True if at least one hash is not mutually exclusive, false otherwise</returns>
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());
2020-08-17 14:57:54 -07:00
}
/// <summary>
/// Returns if any hashes are common with another Rom
/// </summary>
/// <param name="other">Rom to compare against</param>
/// <returns>True if any hashes are in common, false otherwise</returns>
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);
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:
2020-08-17 17:28:32 -07:00
key = CRC;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.MD5:
2020-08-17 17:28:32 -07:00
key = MD5;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA1:
2020-08-17 17:28:32 -07:00
key = SHA1;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA256:
2020-08-17 17:28:32 -07:00
key = SHA256;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA384:
2020-08-17 17:28:32 -07:00
key = SHA384;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA512:
2020-08-17 17:28:32 -07:00
key = SHA512;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SpamSum:
key = SpamSum;
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
if (key == null)
key = string.Empty;
return key;
}
#endregion
2019-01-08 17:40:12 -08:00
}
}