using System.Xml.Serialization; using Newtonsoft.Json; using SabreTools.Core; using SabreTools.Core.Tools; namespace SabreTools.DatItems.Formats { /// /// Represents Compressed Hunks of Data (CHD) formatted disks which use internal hashes /// [JsonObject("disk"), XmlRoot("disk")] public sealed class Disk : DatItem { #region Constants /// /// Non-standard key for inverted logic /// public const string DiskAreaKey = "DISKAREA"; /// /// Non-standard key for inverted logic /// public const string PartKey = "PART"; #endregion #region Fields /// /> protected override ItemType ItemType => ItemType.Disk; /// /> protected override string? NameKey => Models.Metadata.Disk.NameKey; [JsonIgnore] public bool DiskAreaSpecified { get { var diskArea = GetFieldValue(Disk.DiskAreaKey); return diskArea != null && !string.IsNullOrEmpty(diskArea.GetName()); } } [JsonIgnore] public bool PartSpecified { get { var part = GetFieldValue(Disk.PartKey); return part != null && (!string.IsNullOrEmpty(part.GetName()) || !string.IsNullOrEmpty(part.GetStringFieldValue(Models.Metadata.Part.InterfaceKey))); } } #endregion #region Constructors public Disk() : base() { SetFieldValue(DatItem.DupeTypeKey, 0x00); SetFieldValue(Models.Metadata.Disk.StatusKey, ItemStatus.None.AsStringValue()); } public Disk(Models.Metadata.Disk item) : base(item) { SetFieldValue(DatItem.DupeTypeKey, 0x00); } #endregion #region Cloning Methods /// /// Convert a disk to the closest Rom approximation /// /// public Rom ConvertToRom() { var rom = new Rom(_internal.ConvertToRom()!); rom.GetFieldValue(Rom.DataAreaKey)?.SetName(GetFieldValue(Disk.DiskAreaKey)?.GetName()); rom.SetFieldValue(DatItem.DupeTypeKey, GetFieldValue(DatItem.DupeTypeKey)); rom.SetFieldValue(DatItem.MachineKey, GetFieldValue(DatItem.MachineKey)!.Clone() as Machine ?? new Machine()); rom.SetFieldValue(DatItem.RemoveKey, GetBoolFieldValue(DatItem.RemoveKey)); rom.SetFieldValue(DatItem.SourceKey, GetFieldValue(DatItem.SourceKey)); return rom; } #endregion #region Comparision Methods /// /// Fill any missing size and hash information from another Disk /// /// Disk to fill information from public void FillMissingInformation(Disk other) => _internal.FillMissingHashes(other._internal); /// /// Get unique duplicate suffix on name collision /// /// String representing the suffix public string GetDuplicateSuffix() => _internal.GetDuplicateSuffix(); #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.MD5: key = GetStringFieldValue(Models.Metadata.Disk.MD5Key); break; case ItemKey.SHA1: key = GetStringFieldValue(Models.Metadata.Disk.SHA1Key); 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; } /// public override string GetKeyDB(ItemKey bucketedBy, Source? source, 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.MD5: key = GetStringFieldValue(Models.Metadata.Disk.MD5Key); break; case ItemKey.SHA1: key = GetStringFieldValue(Models.Metadata.Disk.SHA1Key); break; // Let the base handle generic stuff default: return base.GetKeyDB(bucketedBy, source, lower, norename); } // Double and triple check the key for corner cases key ??= string.Empty; if (lower) key = key.ToLowerInvariant(); return key; } #endregion } }