2024-03-05 01:42:42 -05:00
|
|
|
|
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;
|
2025-01-11 22:00:26 -05:00
|
|
|
|
using SabreTools.Core.Tools;
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
2021-02-02 10:23:43 -08:00
|
|
|
|
namespace SabreTools.DatItems.Formats
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents Aaruformat images which use internal hashes
|
|
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonObject("media"), XmlRoot("media")]
|
2024-03-10 20:39:54 -04:00
|
|
|
|
public sealed class Media : DatItem<Models.Metadata.Media>
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
2024-03-10 20:39:54 -04:00
|
|
|
|
#region Fields
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
2024-03-10 20:39:54 -04:00
|
|
|
|
/// <inheritdoc>/>
|
|
|
|
|
|
protected override ItemType ItemType => ItemType.Media;
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
2024-03-10 20:39:54 -04:00
|
|
|
|
public Media() : base()
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-10 20:39:54 -04:00
|
|
|
|
public Media(Models.Metadata.Media item) : base(item)
|
2024-03-09 21:46:38 -05:00
|
|
|
|
{
|
2024-03-10 16:49:07 -04:00
|
|
|
|
SetFieldValue<DupeType>(DatItem.DupeTypeKey, 0x00);
|
2025-01-11 22:00:26 -05:00
|
|
|
|
|
|
|
|
|
|
// Process hash values
|
|
|
|
|
|
if (GetStringFieldValue(Models.Metadata.Media.MD5Key) != null)
|
|
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.MD5Key, TextHelper.NormalizeMD5(GetStringFieldValue(Models.Metadata.Media.MD5Key)));
|
|
|
|
|
|
if (GetStringFieldValue(Models.Metadata.Media.SHA1Key) != null)
|
|
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.SHA1Key, TextHelper.NormalizeSHA1(GetStringFieldValue(Models.Metadata.Media.SHA1Key)));
|
|
|
|
|
|
if (GetStringFieldValue(Models.Metadata.Media.SHA256Key) != null)
|
|
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.SHA256Key, TextHelper.NormalizeSHA256(GetStringFieldValue(Models.Metadata.Media.SHA256Key)));
|
2024-03-09 21:46:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Cloning Methods
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-09-04 15:02:15 -07:00
|
|
|
|
/// Convert a media to the closest Rom approximation
|
2020-08-27 16:57:22 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public Rom ConvertToRom()
|
|
|
|
|
|
{
|
2024-03-10 17:00:11 -04:00
|
|
|
|
var rom = new Rom(_internal.ConvertToRom()!);
|
2025-01-08 16:36:58 -05:00
|
|
|
|
|
2024-03-10 16:49:07 -04:00
|
|
|
|
rom.SetFieldValue<DupeType>(DatItem.DupeTypeKey, GetFieldValue<DupeType>(DatItem.DupeTypeKey));
|
|
|
|
|
|
rom.SetFieldValue<Machine>(DatItem.MachineKey, GetFieldValue<Machine>(DatItem.MachineKey));
|
2024-03-11 15:46:44 -04:00
|
|
|
|
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));
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
|
|
|
|
|
return rom;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Comparision Methods
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fill any missing size and hash information from another Media
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="other">Media to fill information from</param>
|
2025-01-06 15:37:32 -05:00
|
|
|
|
public void FillMissingInformation(Media other)
|
2025-01-07 14:55:56 -05:00
|
|
|
|
=> _internal.FillMissingHashes(other._internal);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
2025-01-08 16:19:31 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns if the Rom contains any hashes
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if any hash exists, false otherwise</returns>
|
|
|
|
|
|
public bool HasHashes() => _internal.HasHashes();
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Returns if all of the hashes are set to their 0-byte values
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if any hash matches the 0-byte value, false otherwise</returns>
|
|
|
|
|
|
public bool HasZeroHash() => _internal.HasZeroHash();
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#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-27 16:57:22 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Set the output key as the default blank string
|
2023-08-14 13:17:51 -04:00
|
|
|
|
string? key;
|
2020-08-27 16:57:22 -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:
|
2024-03-11 15:46:44 -04:00
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.MD5Key);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA1:
|
2024-03-11 15:46:44 -04:00
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.SHA1Key);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA256:
|
2024-03-11 15:46:44 -04:00
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.SHA256Key);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SpamSum:
|
2024-03-11 15:46:44 -04:00
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.SpamSumKey);
|
2020-09-04 15:02:15 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
// 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;
|
2025-01-08 13:20:59 -05:00
|
|
|
|
if (lower)
|
|
|
|
|
|
key = key.ToLowerInvariant();
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-08 13:32:09 -05:00
|
|
|
|
/// <inheritdoc/>
|
2025-01-14 14:22:18 -05:00
|
|
|
|
public override string GetKeyDB(ItemKey bucketedBy, Machine? machine, Source? source, bool lower = true, bool norename = true)
|
2025-01-08 13:32:09 -05:00
|
|
|
|
{
|
|
|
|
|
|
// 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.Media.MD5Key);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SHA1:
|
|
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.SHA1Key);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SHA256:
|
|
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.SHA256Key);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case ItemKey.SpamSum:
|
|
|
|
|
|
key = GetStringFieldValue(Models.Metadata.Media.SpamSumKey);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Let the base handle generic stuff
|
|
|
|
|
|
default:
|
2025-01-14 14:22:18 -05:00
|
|
|
|
return base.GetKeyDB(bucketedBy, machine, source, lower, norename);
|
2025-01-08 13:32:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Double and triple check the key for corner cases
|
|
|
|
|
|
key ??= string.Empty;
|
|
|
|
|
|
if (lower)
|
|
|
|
|
|
key = key.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|