using System.Xml.Serialization;
using Newtonsoft.Json;
using SabreTools.Data.Extensions;
using SabreTools.Data.Models.Metadata;
using SabreTools.Text.Extensions;
namespace SabreTools.Metadata.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;
[JsonIgnore]
public bool DiskAreaSpecified
{
get
{
var diskArea = Read(DiskAreaKey);
return diskArea is not null && !string.IsNullOrEmpty(diskArea.GetName());
}
}
[JsonIgnore]
public bool PartSpecified
{
get
{
var part = Read(PartKey);
return part is not null
&& (!string.IsNullOrEmpty(part.GetName())
|| !string.IsNullOrEmpty(part.ReadString(Data.Models.Metadata.Part.InterfaceKey)));
}
}
#endregion
#region Constructors
public Disk() : base()
{
Write(DupeTypeKey, 0x00);
Write(Data.Models.Metadata.Disk.StatusKey, ItemStatus.None.AsStringValue());
}
public Disk(Data.Models.Metadata.Disk item) : base(item)
{
Write(DupeTypeKey, 0x00);
// Process flag values
bool? optional = ReadBool(Data.Models.Metadata.Disk.OptionalKey);
if (optional is not null)
Write(Data.Models.Metadata.Disk.OptionalKey, optional.FromYesNo());
string? status = ReadString(Data.Models.Metadata.Disk.StatusKey);
if (status is not null)
Write(Data.Models.Metadata.Disk.StatusKey, status.AsItemStatus()?.AsStringValue());
bool? writable = ReadBool(Data.Models.Metadata.Disk.WritableKey);
if (writable is not null)
Write(Data.Models.Metadata.Disk.WritableKey, writable.FromYesNo());
// Process hash values
string? md5 = ReadString(Data.Models.Metadata.Disk.MD5Key);
if (md5 is not null)
Write(Data.Models.Metadata.Disk.MD5Key, TextHelper.NormalizeMD5(md5));
string? sha1 = ReadString(Data.Models.Metadata.Disk.SHA1Key);
if (sha1 is not null)
Write(Data.Models.Metadata.Disk.SHA1Key, TextHelper.NormalizeSHA1(sha1));
}
public Disk(Data.Models.Metadata.Disk item, Machine machine, Source source) : this(item)
{
Write(SourceKey, source);
CopyMachineInformation(machine);
}
#endregion
#region Cloning Methods
///
public override object Clone() => new Disk(_internal.Clone() as Data.Models.Metadata.Disk ?? []);
///
/// Convert a disk to the closest Rom approximation
///
///
public Rom ConvertToRom()
{
var rom = new Rom(_internal.ConvertToRom()!);
// Create a DataArea if there was an existing DiskArea
var diskArea = Read(DiskAreaKey);
if (diskArea is not null)
{
var dataArea = new DataArea();
string? diskAreaName = diskArea.ReadString(Data.Models.Metadata.DiskArea.NameKey);
dataArea.Write(Data.Models.Metadata.DataArea.NameKey, diskAreaName);
rom.Write(Rom.DataAreaKey, dataArea);
}
rom.Write(DupeTypeKey, Read(DupeTypeKey));
rom.Write(MachineKey, GetMachine()?.Clone() as Machine);
rom.Write(Rom.PartKey, Read(PartKey)?.Clone() as Part);
rom.Write(RemoveKey, ReadBool(RemoveKey));
rom.Write(SourceKey, Read(SourceKey)?.Clone() as Source);
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);
///
/// 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 = ReadString(Data.Models.Metadata.Disk.MD5Key);
break;
case ItemKey.SHA1:
key = ReadString(Data.Models.Metadata.Disk.SHA1Key);
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
}
}