using System.Xml.Serialization;
using Newtonsoft.Json;
using SabreTools.Core;
using SabreTools.Core.Tools;
namespace SabreTools.DatItems.Formats
{
///
/// Represents Aaruformat images which use internal hashes
///
[JsonObject("media"), XmlRoot("media")]
public sealed class Media : DatItem
{
#region Fields
/// />
protected override ItemType ItemType => ItemType.Media;
#endregion
#region Constructors
public Media() : base()
{
SetFieldValue(DatItem.DupeTypeKey, 0x00);
}
public Media(Models.Metadata.Media item) : base(item)
{
SetFieldValue(DatItem.DupeTypeKey, 0x00);
// Process hash values
if (GetStringFieldValue(Models.Metadata.Media.MD5Key) != null)
SetFieldValue(Models.Metadata.Media.MD5Key, TextHelper.NormalizeMD5(GetStringFieldValue(Models.Metadata.Media.MD5Key)));
if (GetStringFieldValue(Models.Metadata.Media.SHA1Key) != null)
SetFieldValue(Models.Metadata.Media.SHA1Key, TextHelper.NormalizeSHA1(GetStringFieldValue(Models.Metadata.Media.SHA1Key)));
if (GetStringFieldValue(Models.Metadata.Media.SHA256Key) != null)
SetFieldValue(Models.Metadata.Media.SHA256Key, TextHelper.NormalizeSHA256(GetStringFieldValue(Models.Metadata.Media.SHA256Key)));
}
#endregion
#region Cloning Methods
///
/// Convert a media to the closest Rom approximation
///
///
public Rom ConvertToRom()
{
var rom = new Rom(_internal.ConvertToRom()!);
rom.SetFieldValue(DatItem.DupeTypeKey, GetFieldValue(DatItem.DupeTypeKey));
rom.SetFieldValue(DatItem.MachineKey, GetFieldValue(DatItem.MachineKey));
rom.SetFieldValue(DatItem.RemoveKey, GetBoolFieldValue(DatItem.RemoveKey));
rom.SetFieldValue(DatItem.SourceKey, GetFieldValue(DatItem.SourceKey));
return rom;
}
#endregion
#region Comparision Methods
///
/// Fill any missing size and hash information from another Media
///
/// Media to fill information from
public void FillMissingInformation(Media 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;
// 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:
return base.GetKey(bucketedBy, machine, source, lower, norename);
}
// Double and triple check the key for corner cases
key ??= string.Empty;
if (lower)
key = key.ToLowerInvariant();
return key;
}
#endregion
}
}