Files
SabreTools/SabreTools.DatItems/Formats/Disk.cs

353 lines
11 KiB
C#
Raw Normal View History

using System.Xml.Serialization;
2022-11-03 12:22:17 -07:00
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
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;
2021-02-02 10:23:43 -08:00
namespace SabreTools.DatItems.Formats
{
2019-01-08 17:40:12 -08:00
/// <summary>
/// Represents Compressed Hunks of Data (CHD) formatted disks which use internal hashes
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("disk"), XmlRoot("disk")]
2019-01-08 17:40:12 -08:00
public class Disk : DatItem
{
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>
2022-11-03 12:22:17 -07:00
[JsonProperty("name"), XmlElement("name")]
public string? Name
{
get => _disk.ReadString(Models.Internal.Disk.NameKey);
set => _disk[Models.Internal.Disk.NameKey] = value;
}
2020-09-02 12:19:12 -07:00
2019-01-08 17:40:12 -08:00
/// <summary>
/// Data MD5 hash
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("md5")]
public string? MD5
2019-01-08 17:40:12 -08:00
{
get => _disk.ReadString(Models.Internal.Disk.MD5Key);
set => _disk[Models.Internal.Disk.MD5Key] = TextHelper.NormalizeMD5(value);
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// Data SHA-1 hash
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha1")]
public string? SHA1
2019-01-08 17:40:12 -08:00
{
get => _disk.ReadString(Models.Internal.Disk.SHA1Key);
set => _disk[Models.Internal.Disk.SHA1Key] = TextHelper.NormalizeSHA1(value);
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// Disk name to merge from parent
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("merge", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("merge")]
public string? MergeTag
{
get => _disk.ReadString(Models.Internal.Disk.MergeKey);
set => _disk[Models.Internal.Disk.MergeKey] = value;
}
2019-01-08 17:40:12 -08:00
/// <summary>
/// Disk region
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("region", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("region")]
public string? Region
{
get => _disk.ReadString(Models.Internal.Disk.RegionKey);
set => _disk[Models.Internal.Disk.RegionKey] = value;
}
2019-01-08 17:40:12 -08:00
/// <summary>
/// Disk index
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("index", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("index")]
public string? Index
{
get => _disk.ReadString(Models.Internal.Disk.IndexKey);
set => _disk[Models.Internal.Disk.IndexKey] = value;
}
2019-01-08 17:40:12 -08:00
/// <summary>
/// Disk writable flag
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("writable", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("writable")]
public bool? Writable
{
get => _disk.ReadBool(Models.Internal.Disk.WritableKey);
set => _disk[Models.Internal.Disk.WritableKey] = value;
}
2019-01-08 17:40:12 -08:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool WritableSpecified { get { return Writable != null; } }
2019-01-08 17:40:12 -08:00
/// <summary>
/// Disk dump status
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("status", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("status")]
2020-08-23 22:23:55 -07:00
[JsonConverter(typeof(StringEnumConverter))]
public ItemStatus ItemStatus
{
get => _disk.ReadString(Models.Internal.Disk.StatusKey).AsItemStatus();
set => _disk[Models.Internal.Disk.StatusKey] = value.FromItemStatus(yesno: false);
}
2019-01-08 17:40:12 -08:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool ItemStatusSpecified { get { return ItemStatus != ItemStatus.NULL; } }
2019-01-08 17:40:12 -08:00
/// <summary>
/// Determine if the disk is optional in the set
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("optional", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("optional")]
public bool? Optional
{
get => _disk.ReadBool(Models.Internal.Disk.OptionalKey);
set => _disk[Models.Internal.Disk.OptionalKey] = value;
}
2019-01-08 17:40:12 -08:00
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool OptionalSpecified { get { return Optional != null; } }
2019-01-08 17:40:12 -08:00
#endregion
#region SoftwareList
/// <summary>
/// Disk area information
/// </summary>
/// <remarks>This is inverted from the internal model</remarks>
2022-11-03 12:22:17 -07:00
[JsonProperty("diskarea", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("diskarea")]
public DiskArea? DiskArea { get; set; }
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool DiskAreaSpecified
{
get
{
2020-09-07 22:33:48 -07:00
return DiskArea != null
&& !string.IsNullOrEmpty(DiskArea.Name);
2020-09-07 22:00:02 -07:00
}
}
/// <summary>
/// Original hardware part associated with the item
/// </summary>
/// <remarks>This is inverted from the internal model</remarks>
2022-11-03 12:22:17 -07:00
[JsonProperty("part", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("part")]
public Part? Part { get; set; }
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
}
}
#endregion
/// <summary>
/// Internal Disk model
/// </summary>
[JsonIgnore]
private Models.Internal.Disk _disk = new();
#endregion // Fields
2020-08-17 17:28:32 -07:00
#region Accessors
/// <inheritdoc/>
public override string? GetName() => Name;
2020-09-02 12:19:12 -07:00
/// <inheritdoc/>
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 Disk object
/// </summary>
public Disk()
{
2020-08-20 13:17:14 -07:00
Name = string.Empty;
ItemType = ItemType.Disk;
DupeType = 0x00;
ItemStatus = ItemStatus.None;
2019-01-08 17:40:12 -08:00
}
/// <summary>
/// Create a Disk object from a BaseFile
2019-01-08 17:40:12 -08:00
/// </summary>
/// <param name="baseFile"></param>
public Disk(BaseFile baseFile)
{
2020-08-20 13:17:14 -07:00
Name = baseFile.Filename;
MD5 = Utilities.ByteArrayToString(baseFile.MD5);
SHA1 = Utilities.ByteArrayToString(baseFile.SHA1);
2019-01-08 17:40:12 -08:00
2020-08-20 13:17:14 -07:00
ItemType = ItemType.Disk;
DupeType = 0x00;
ItemStatus = ItemStatus.None;
2019-01-08 17:40:12 -08:00
}
#endregion
#region Cloning Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
2019-01-08 17:40:12 -08:00
public override object Clone()
{
return new Disk()
{
Name = this.Name,
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
2020-08-20 13:17:14 -07:00
Remove = this.Remove,
2019-01-08 17:40:12 -08:00
_disk = this._disk?.Clone() as Models.Internal.Disk ?? new Models.Internal.Disk(),
DiskArea = this.DiskArea,
Part = this.Part,
2019-01-08 17:40:12 -08:00
};
}
2020-12-08 11:09:05 -08:00
/// <summary>
/// Convert Disk object to a BaseFile
/// </summary>
public BaseFile ConvertToBaseFile()
{
return new BaseFile()
{
Filename = this.Name,
Parent = this.Machine?.Name,
MD5 = Utilities.StringToByteArray(this.MD5),
SHA1 = Utilities.StringToByteArray(this.SHA1),
2020-12-08 11:09:05 -08:00
};
}
2019-09-20 10:30:30 -07:00
/// <summary>
/// Convert a disk to the closest Rom approximation
/// </summary>
/// <returns></returns>
public Rom ConvertToRom()
{
var rom = new Rom()
{
Name = this.Name + ".chd",
2019-09-20 10:30:30 -07:00
ItemType = ItemType.Rom,
DupeType = this.DupeType,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
2020-08-20 13:17:14 -07:00
Remove = this.Remove,
MergeTag = this.MergeTag,
Region = this.Region,
ItemStatus = this.ItemStatus,
Optional = this.Optional,
2019-09-20 10:30:30 -07:00
MD5 = this.MD5,
SHA1 = this.SHA1,
DataArea = new DataArea { Name = this.DiskArea?.Name },
Part = this.Part,
2019-09-20 10:30:30 -07:00
};
return rom;
}
2019-01-08 17:40:12 -08:00
#endregion
#region Comparision Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
public override bool Equals(DatItem? other)
2019-01-08 17:40:12 -08:00
{
// If we don't have a Disk, return false
if (ItemType != other?.ItemType || other is not Disk otherInternal)
return false;
2019-01-08 17:40:12 -08:00
// Compare the internal models
return _disk.EqualTo(otherInternal._disk);
2019-01-08 17:40:12 -08:00
}
2020-08-17 17:28:32 -07:00
/// <summary>
/// Fill any missing size and hash information from another Disk
/// </summary>
/// <param name="other">Disk to fill information from</param>
public void FillMissingInformation(Disk other)
{
if (string.IsNullOrWhiteSpace(MD5) && !string.IsNullOrWhiteSpace(other.MD5))
MD5 = other.MD5;
2020-08-17 17:28:32 -07:00
if (string.IsNullOrWhiteSpace(SHA1) && !string.IsNullOrWhiteSpace(other.SHA1))
SHA1 = other.SHA1;
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 (!string.IsNullOrWhiteSpace(MD5))
2020-08-17 17:28:32 -07:00
return $"_{MD5}";
else if (!string.IsNullOrWhiteSpace(SHA1))
2020-08-17 17:28:32 -07:00
return $"_{SHA1}";
else
return "_1";
}
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.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;
// 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;
2020-08-17 17:28:32 -07:00
return key;
}
#endregion
2019-01-08 17:40:12 -08:00
}
}