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

169 lines
5.4 KiB
C#
Raw Normal View History

using System.Xml.Serialization;
2022-11-03 12:22:17 -07:00
using Newtonsoft.Json;
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;
2024-10-24 00:46:28 -04:00
using SabreTools.IO.Extensions;
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")]
2024-03-10 20:39:54 -04:00
public sealed class Disk : DatItem<Models.Metadata.Disk>
2019-01-08 17:40:12 -08:00
{
2024-03-10 16:49:07 -04:00
#region Constants
/// <summary>
/// Non-standard key for inverted logic
/// </summary>
public const string DiskAreaKey = "DISKAREA";
/// <summary>
/// Non-standard key for inverted logic
/// </summary>
public const string PartKey = "PART";
#endregion
2020-08-20 13:17:14 -07:00
#region Fields
2019-01-08 17:40:12 -08:00
2024-03-10 20:39:54 -04:00
/// <inheritdoc>/>
protected override ItemType ItemType => ItemType.Disk;
/// <inheritdoc>/>
protected override string? NameKey => Models.Metadata.Disk.NameKey;
2020-09-07 22:00:02 -07:00
[JsonIgnore]
public bool DiskAreaSpecified
{
get
{
2024-03-10 16:49:07 -04:00
var diskArea = GetFieldValue<DiskArea?>(Disk.DiskAreaKey);
2024-03-09 21:34:26 -05:00
return diskArea != null && !string.IsNullOrEmpty(diskArea.GetName());
2020-09-07 22:00:02 -07:00
}
}
[JsonIgnore]
public bool PartSpecified
{
get
{
2024-03-10 16:49:07 -04:00
var part = GetFieldValue<Part?>(Disk.PartKey);
2024-03-09 21:34:26 -05:00
return part != null
&& (!string.IsNullOrEmpty(part.GetName())
|| !string.IsNullOrEmpty(part.GetStringFieldValue(Models.Metadata.Part.InterfaceKey)));
2020-09-07 22:00:02 -07:00
}
}
#endregion
2019-01-08 17:40:12 -08:00
#region Constructors
2024-03-10 20:39:54 -04:00
public Disk() : base()
2019-01-08 17:40:12 -08:00
{
2024-03-10 16:49:07 -04:00
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
SetFieldValue<string?>(Models.Metadata.Disk.StatusKey, ItemStatus.None.AsStringValue());
2019-01-08 17:40:12 -08:00
}
2024-03-10 20:39:54 -04:00
public Disk(Models.Metadata.Disk item) : base(item)
2019-01-08 17:40:12 -08:00
{
2024-03-10 20:39:54 -04:00
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
}
2023-08-15 01:38:01 -04:00
2024-03-10 20:39:54 -04:00
public Disk(BaseFile baseFile) : base()
{
2024-03-08 20:42:24 -05:00
SetName(baseFile.Filename);
2024-12-06 13:20:36 -05:00
SetFieldValue<string?>(Models.Metadata.Disk.MD5Key, baseFile.MD5.ToHexString());
SetFieldValue<string?>(Models.Metadata.Disk.SHA1Key, baseFile.SHA1.ToHexString());
2019-01-08 17:40:12 -08:00
2024-03-10 16:49:07 -04:00
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
2019-01-08 17:40:12 -08:00
}
#endregion
#region Cloning Methods
2020-12-08 11:09:05 -08:00
/// <summary>
/// Convert Disk object to a BaseFile
/// </summary>
public BaseFile ConvertToBaseFile()
{
return new BaseFile()
{
2024-03-08 20:42:24 -05:00
Filename = this.GetName(),
Parent = GetFieldValue<Machine>(DatItem.MachineKey)!.GetStringFieldValue(Models.Metadata.Machine.NameKey),
2024-12-06 13:20:36 -05:00
MD5 = GetStringFieldValue(Models.Metadata.Disk.MD5Key).FromHexString(),
SHA1 = GetStringFieldValue(Models.Metadata.Disk.SHA1Key).FromHexString(),
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(_internal.ConvertToRom()!);
2024-03-10 16:49:07 -04:00
rom.GetFieldValue<DataArea?>(Rom.DataAreaKey)?.SetName(this.GetFieldValue<DiskArea?>(Disk.DiskAreaKey)?.GetName());
rom.SetFieldValue<DupeType>(DatItem.DupeTypeKey, GetFieldValue<DupeType>(DatItem.DupeTypeKey));
rom.SetFieldValue<Machine>(DatItem.MachineKey, GetFieldValue<Machine>(DatItem.MachineKey)!.Clone() as Machine ?? new Machine());
rom.SetFieldValue<bool?>(DatItem.RemoveKey, GetBoolFieldValue(DatItem.RemoveKey));
2024-03-10 16:49:07 -04:00
rom.SetFieldValue<Source?>(DatItem.SourceKey, GetFieldValue<Source?>(DatItem.SourceKey));
2024-03-08 20:42:24 -05:00
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
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:
key = GetStringFieldValue(Models.Metadata.Disk.MD5Key);
2020-08-17 17:28:32 -07:00
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA1:
key = GetStringFieldValue(Models.Metadata.Disk.SHA1Key);
2020-08-17 17:28:32 -07:00
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
}
}