using System.Xml.Serialization; using Newtonsoft.Json; using SabreTools.Data.Extensions; using SabreTools.Text.Extensions; namespace SabreTools.Metadata.DatItems.Formats { /// /// Represents Aaruformat images which use internal hashes /// [JsonObject("media"), XmlRoot("media")] public sealed class Media : DatItem { #region Properties public string? MD5 { get => _internal.MD5; set => _internal.MD5 = value; } /// /> public override Data.Models.Metadata.ItemType ItemType => Data.Models.Metadata.ItemType.Media; public string? Name { get => _internal.Name; set => _internal.Name = value; } public string? SHA1 { get => _internal.SHA1; set => _internal.SHA1 = value; } public string? SHA256 { get => _internal.SHA256; set => _internal.SHA256 = value; } public string? SpamSum { get => _internal.SpamSum; set => _internal.SpamSum = value; } #endregion #region Constructors public Media() : base() { DupeType = 0x00; } public Media(Data.Models.Metadata.Media item) : base(item) { DupeType = 0x00; // Process hash values if (MD5 is not null) MD5 = TextHelper.NormalizeMD5(MD5); if (SHA1 is not null) SHA1 = TextHelper.NormalizeSHA1(SHA1); if (SHA256 is not null) SHA256 = TextHelper.NormalizeSHA256(SHA256); } public Media(Data.Models.Metadata.Media item, Machine machine, Source source) : this(item) { Source = source; CopyMachineInformation(machine); } public Media(Data.Models.Metadata.Media item, long machineIndex, long sourceIndex) : this(item) { SourceIndex = sourceIndex; MachineIndex = machineIndex; } #endregion #region Accessors /// public override string? GetName() => Name; /// public override void SetName(string? name) => Name = name; #endregion #region Cloning Methods /// public override object Clone() => new Media(GetInternalClone()); /// public override Data.Models.Metadata.Media GetInternalClone() => _internal.Clone() as Data.Models.Metadata.Media ?? new(); /// /// Convert a media to the closest Rom approximation /// /// public Rom ConvertToRom() { var rom = new Rom(_internal.ConvertToRom()!); rom.DupeType = DupeType; rom.Machine = Machine?.Clone() as Machine; rom.RemoveFlag = RemoveFlag; rom.Source = Source?.Clone() as Source; return rom; } #endregion #region Comparision Methods /// public override bool Equals(DatItem? other) { // If the other item is null if (other is null) return false; // If the type matches if (other is Media otherMedia) return _internal.PartialEquals(otherMedia._internal); // Everything else fails return false; } /// /// Fill any missing size and hash information from another Media /// /// Media to fill information from public void FillMissingInformation(Media other) => _internal.FillMissingHashes(other._internal); /// /// 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, Machine? machine, Source? source, bool lower = true, bool norename = true) { // Set the output key as the default blank string string? key; #pragma warning disable IDE0010 // Now determine what the key should be based on the bucketedBy value switch (bucketedBy) { case ItemKey.MD5: key = MD5; break; case ItemKey.SHA1: key = SHA1; break; case ItemKey.SHA256: key = SHA256; break; case ItemKey.SpamSum: key = SpamSum; break; // Let the base handle generic stuff default: return base.GetKey(bucketedBy, machine, source, lower, norename); } #pragma warning restore IDE0010 // Double and triple check the key for corner cases key ??= string.Empty; if (lower) key = key.ToLowerInvariant(); return key; } #endregion } }