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;
|
2020-12-08 16:37:08 -08:00
|
|
|
|
using SabreTools.Core.Tools;
|
2020-12-08 14:53:49 -08:00
|
|
|
|
using SabreTools.FileTypes;
|
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")]
|
2020-08-27 16:57:22 -07:00
|
|
|
|
public class Media : DatItem
|
|
|
|
|
|
{
|
|
|
|
|
|
#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.Media.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.Media.NameKey, name);
|
2020-12-13 14:01:16 -08:00
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a default, empty Media object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Media()
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
_internal = new Models.Metadata.Media();
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Machine = new Machine();
|
|
|
|
|
|
|
2024-03-08 20:42:24 -05:00
|
|
|
|
SetName(string.Empty);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
ItemType = ItemType.Media;
|
|
|
|
|
|
DupeType = 0x00;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-09 21:46:38 -05:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a Media object from the internal model
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Media(Models.Metadata.Media? item)
|
|
|
|
|
|
{
|
|
|
|
|
|
_internal = item ?? [];
|
|
|
|
|
|
Machine = new Machine();
|
|
|
|
|
|
|
|
|
|
|
|
ItemType = ItemType.Media;
|
|
|
|
|
|
DupeType = 0x00;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a Media object from a BaseFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="baseFile"></param>
|
|
|
|
|
|
public Media(BaseFile baseFile)
|
|
|
|
|
|
{
|
2023-09-04 23:51:37 -04:00
|
|
|
|
_internal = new Models.Metadata.Media();
|
2023-08-15 01:38:01 -04:00
|
|
|
|
Machine = new Machine();
|
|
|
|
|
|
|
2024-03-08 20:42:24 -05:00
|
|
|
|
SetName(baseFile.Filename);
|
2024-03-09 21:34:26 -05:00
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.MD5Key, TextHelper.ByteArrayToString(baseFile.MD5));
|
|
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.SHA1Key, TextHelper.ByteArrayToString(baseFile.SHA1));
|
|
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.SHA256Key, TextHelper.ByteArrayToString(baseFile.SHA256));
|
|
|
|
|
|
SetFieldValue<string?>(Models.Metadata.Media.SpamSumKey, System.Text.Encoding.UTF8.GetString(baseFile.SpamSum ?? []));
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
|
|
|
|
|
ItemType = ItemType.Media;
|
|
|
|
|
|
DupeType = 0x00;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Cloning Methods
|
|
|
|
|
|
|
2022-11-03 12:22:17 -07:00
|
|
|
|
/// <inheritdoc/>
|
2020-08-27 16:57:22 -07:00
|
|
|
|
public override object Clone()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Media()
|
|
|
|
|
|
{
|
|
|
|
|
|
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-27 16:57:22 -07:00
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
|
2024-02-28 19:19:50 -05:00
|
|
|
|
_internal = this._internal?.Clone() as Models.Metadata.Media ?? [],
|
2020-08-27 16:57:22 -07:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-08 11:09:05 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Convert Media object to a BaseFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public BaseFile ConvertToBaseFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new BaseFile()
|
|
|
|
|
|
{
|
2024-03-08 20:42:24 -05:00
|
|
|
|
Filename = this.GetName(),
|
2024-03-09 23:43:43 -05:00
|
|
|
|
Parent = this.Machine.GetFieldValue<string?>(Models.Metadata.Machine.NameKey),
|
2024-03-09 21:34:26 -05:00
|
|
|
|
MD5 = TextHelper.StringToByteArray(GetFieldValue<string?>(Models.Metadata.Media.MD5Key)),
|
|
|
|
|
|
SHA1 = TextHelper.StringToByteArray(GetFieldValue<string?>(Models.Metadata.Media.SHA1Key)),
|
|
|
|
|
|
SHA256 = TextHelper.StringToByteArray(GetFieldValue<string?>(Models.Metadata.Media.SHA256Key)),
|
|
|
|
|
|
SpamSum = System.Text.Encoding.UTF8.GetBytes(GetFieldValue<string?>(Models.Metadata.Media.SpamSumKey) ?? string.Empty),
|
2020-12-08 11:09:05 -08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
/// <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()
|
|
|
|
|
|
{
|
2023-08-14 22:33:05 -04:00
|
|
|
|
var rom = new Rom(_internal.ConvertToRom())
|
2020-08-27 16:57:22 -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-27 16:57:22 -07:00
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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>
|
2023-08-14 22:33:05 -04:00
|
|
|
|
public void FillMissingInformation(Media other) => _internal.FillMissingHashes(other?._internal);
|
2020-08-27 16:57:22 -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-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-09 21:34:26 -05:00
|
|
|
|
key = GetFieldValue<string?>(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-09 21:34:26 -05:00
|
|
|
|
key = GetFieldValue<string?>(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-09 21:34:26 -05:00
|
|
|
|
key = GetFieldValue<string?>(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-09 21:34:26 -05:00
|
|
|
|
key = GetFieldValue<string?>(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;
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|