2020-12-14 10:58:43 -08:00
|
|
|
|
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;
|
2020-12-08 16:37:08 -08:00
|
|
|
|
using SabreTools.Core.Tools;
|
2020-12-08 14:53:49 -08:00
|
|
|
|
using SabreTools.FileTypes;
|
2016-09-19 20:36:12 -07:00
|
|
|
|
|
2021-02-02 10:23:43 -08:00
|
|
|
|
namespace SabreTools.DatItems.Formats
|
2016-09-19 18:04:24 -07:00
|
|
|
|
{
|
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
|
|
|
|
|
2020-09-03 11:02:06 -07: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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? Name
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.NameKey);
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.NameKey] = value;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? MD5
|
2019-01-08 17:40:12 -08:00
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.MD5Key);
|
|
|
|
|
|
set => _internal[Models.Metadata.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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? SHA1
|
2019-01-08 17:40:12 -08:00
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.SHA1Key);
|
|
|
|
|
|
set => _internal[Models.Metadata.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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? MergeTag
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.MergeKey);
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.MergeKey] = value;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? Region
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.RegionKey);
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.RegionKey] = value;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? Index
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.IndexKey);
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.IndexKey] = value;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public bool? Writable
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadBool(Models.Metadata.Disk.WritableKey);
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.WritableKey] = value;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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))]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public ItemStatus ItemStatus
|
|
|
|
|
|
{
|
2024-03-05 15:24:11 -05:00
|
|
|
|
get => _internal.ReadString(Models.Metadata.Disk.StatusKey).AsEnumValue<ItemStatus>();
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.StatusKey] = value.AsStringValue<ItemStatus>(useSecond: false);
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public bool? Optional
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
get => _internal.ReadBool(Models.Metadata.Disk.OptionalKey);
|
|
|
|
|
|
set => _internal[Models.Metadata.Disk.OptionalKey] = value;
|
2023-08-14 13:17:51 -04:00
|
|
|
|
}
|
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
|
|
|
|
|
|
|
2020-09-03 11:02:06 -07:00
|
|
|
|
#region SoftwareList
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Disk area information
|
|
|
|
|
|
/// </summary>
|
2023-08-14 22:33:05 -04:00
|
|
|
|
/// <remarks>Hack on top of internal model</remarks>
|
2022-11-03 12:22:17 -07:00
|
|
|
|
[JsonProperty("diskarea", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("diskarea")]
|
2023-08-14 22:33:05 -04:00
|
|
|
|
public DiskArea? DiskArea
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _internal.Read<DiskArea>("DISKAREA");
|
|
|
|
|
|
set => _internal["DISKAREA"] = value;
|
|
|
|
|
|
}
|
2020-09-03 11:02:06 -07:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-03 11:19:06 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Original hardware part associated with the item
|
|
|
|
|
|
/// </summary>
|
2023-08-14 22:33:05 -04:00
|
|
|
|
/// <remarks>Hack on top of internal model</remarks>
|
2022-11-03 12:22:17 -07:00
|
|
|
|
[JsonProperty("part", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("part")]
|
2023-08-14 22:33:05 -04:00
|
|
|
|
public Part? Part
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _internal.Read<Part>("PART");
|
|
|
|
|
|
set => _internal["PART"] = value;
|
|
|
|
|
|
}
|
2020-09-03 11:19:06 -07:00
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-03 11:02:06 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#endregion // Fields
|
|
|
|
|
|
|
2020-08-17 17:28:32 -07:00
|
|
|
|
#region Accessors
|
|
|
|
|
|
|
2020-12-13 14:01:16 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-03-08 15:31:21 -05:00
|
|
|
|
public override string? GetName() => GetFieldValue<string>(Models.Metadata.Disk.NameKey);
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2020-12-13 14:01:16 -08:00
|
|
|
|
/// <inheritdoc/>
|
2024-03-08 15:31:21 -05:00
|
|
|
|
public override void SetName(string? name) => SetFieldValue(Models.Metadata.Disk.NameKey, name);
|
2020-12-13 14:01:16 -08:00
|
|
|
|
|
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()
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
_internal = new Models.Metadata.Disk();
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Machine = new Machine();
|
|
|
|
|
|
|
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>
|
2020-08-27 16:57:22 -07:00
|
|
|
|
/// Create a Disk object from a BaseFile
|
2019-01-08 17:40:12 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Disk(BaseFile baseFile)
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
_internal = new Models.Metadata.Disk();
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Machine = new Machine();
|
|
|
|
|
|
|
2020-08-20 13:17:14 -07:00
|
|
|
|
Name = baseFile.Filename;
|
2023-08-14 13:36:37 -04:00
|
|
|
|
MD5 = TextHelper.ByteArrayToString(baseFile.MD5);
|
|
|
|
|
|
SHA1 = TextHelper.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()
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemType = this.ItemType,
|
|
|
|
|
|
DupeType = this.DupeType,
|
|
|
|
|
|
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Machine = this.Machine.Clone() as Machine ?? new Machine(),
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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
|
|
|
|
|
2024-02-28 19:19:50 -05:00
|
|
|
|
_internal = this._internal?.Clone() as Models.Metadata.Disk ?? [],
|
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,
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Parent = this.Machine.Name,
|
2023-08-14 13:36:37 -04:00
|
|
|
|
MD5 = TextHelper.StringToByteArray(this.MD5),
|
|
|
|
|
|
SHA1 = TextHelper.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()
|
|
|
|
|
|
{
|
2023-08-14 22:33:05 -04:00
|
|
|
|
var rom = new Rom(_internal.ConvertToRom())
|
2019-09-20 10:30:30 -07:00
|
|
|
|
{
|
|
|
|
|
|
ItemType = ItemType.Rom,
|
|
|
|
|
|
DupeType = this.DupeType,
|
|
|
|
|
|
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Machine = this.Machine.Clone() as Machine ?? new Machine(),
|
2023-08-14 13:17:51 -04:00
|
|
|
|
Source = this.Source?.Clone() as Source,
|
2020-08-20 13:17:14 -07:00
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
|
2023-08-14 13:17:51 -04:00
|
|
|
|
DataArea = new DataArea { Name = this.DiskArea?.Name },
|
2020-09-03 11:19:06 -07:00
|
|
|
|
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
|
|
|
|
|
|
|
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>
|
2023-08-14 22:33:05 -04:00
|
|
|
|
public void FillMissingInformation(Disk other) => _internal.FillMissingHashes(other?._internal);
|
2020-08-17 17:28:32 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get unique duplicate suffix on name collision
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>String representing the suffix</returns>
|
2023-08-14 22:33:05 -04:00
|
|
|
|
public string GetDuplicateSuffix() => _internal.GetDuplicateSuffix();
|
2020-08-17 17:28:32 -07:00
|
|
|
|
|
2024-03-05 01:42:42 -05: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
|
2023-08-14 13:17:51 -04:00
|
|
|
|
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
|
2023-04-19 16:39:58 -04:00
|
|
|
|
key ??= string.Empty;
|
2020-08-17 17:28:32 -07:00
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2019-01-08 17:40:12 -08:00
|
|
|
|
}
|
2016-09-19 18:04:24 -07:00
|
|
|
|
}
|