2023-08-14 14:53:28 -04:00
|
|
|
|
using System;
|
|
|
|
|
|
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 Fields
|
|
|
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _media.ReadString(Models.Internal.Media.NameKey);
|
|
|
|
|
|
set => _media[Models.Internal.Media.NameKey] = value;
|
|
|
|
|
|
}
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2020-08-27 16:57:22 -07: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
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
get => _media.ReadString(Models.Internal.Media.MD5Key);
|
|
|
|
|
|
set => _media[Models.Internal.Media.MD5Key] = TextHelper.NormalizeMD5(value);
|
2020-08-27 16:57:22 -07: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
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
get => _media.ReadString(Models.Internal.Media.SHA1Key);
|
|
|
|
|
|
set => _media[Models.Internal.Media.SHA1Key] = TextHelper.NormalizeSHA1(value);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Data SHA-256 hash
|
|
|
|
|
|
/// </summary>
|
2022-11-03 12:22:17 -07:00
|
|
|
|
[JsonProperty("sha256", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha256")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? SHA256
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
get => _media.ReadString(Models.Internal.Media.SHA256Key);
|
|
|
|
|
|
set => _media[Models.Internal.Media.SHA256Key] = TextHelper.NormalizeSHA256(value);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-04 15:02:15 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// File SpamSum fuzzy hash
|
|
|
|
|
|
/// </summary>
|
2022-11-03 12:22:17 -07:00
|
|
|
|
[JsonProperty("spamsum", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("spamsum")]
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public string? SpamSum
|
2020-09-04 15:02:15 -07:00
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
get => _media.ReadString(Models.Internal.Media.SpamSumKey);
|
|
|
|
|
|
set => _media[Models.Internal.Media.SpamSumKey] = value;
|
2020-09-04 15:02:15 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-08-14 13:17:51 -04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal Media model
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonIgnore]
|
|
|
|
|
|
private Models.Internal.Media _media = new();
|
|
|
|
|
|
|
2020-08-27 16:57:22 -07:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Accessors
|
|
|
|
|
|
|
2020-12-13 14:01:16 -08:00
|
|
|
|
/// <inheritdoc/>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public override string? GetName() => Name;
|
2020-09-02 12:19:12 -07:00
|
|
|
|
|
2020-12-13 14:01:16 -08:00
|
|
|
|
/// <inheritdoc/>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public override void SetName(string? name) => Name = 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()
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = string.Empty;
|
|
|
|
|
|
ItemType = ItemType.Media;
|
|
|
|
|
|
DupeType = 0x00;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a Media object from a BaseFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="baseFile"></param>
|
|
|
|
|
|
public Media(BaseFile baseFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
Name = baseFile.Filename;
|
2023-08-14 13:36:37 -04:00
|
|
|
|
MD5 = TextHelper.ByteArrayToString(baseFile.MD5);
|
|
|
|
|
|
SHA1 = TextHelper.ByteArrayToString(baseFile.SHA1);
|
|
|
|
|
|
SHA256 = TextHelper.ByteArrayToString(baseFile.SHA256);
|
2023-08-14 14:53:28 -04:00
|
|
|
|
SpamSum = System.Text.Encoding.UTF8.GetString(baseFile.SpamSum ?? Array.Empty<byte>());
|
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-14 13:17:51 -04:00
|
|
|
|
Machine = this.Machine?.Clone() as Machine,
|
|
|
|
|
|
Source = this.Source?.Clone() as Source,
|
2020-08-27 16:57:22 -07:00
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
|
2023-08-14 13:17:51 -04:00
|
|
|
|
_media = this._media?.Clone() as Models.Internal.Media ?? new Models.Internal.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()
|
|
|
|
|
|
{
|
|
|
|
|
|
Filename = this.Name,
|
|
|
|
|
|
Parent = this.Machine?.Name,
|
2023-08-14 13:36:37 -04:00
|
|
|
|
MD5 = TextHelper.StringToByteArray(this.MD5),
|
|
|
|
|
|
SHA1 = TextHelper.StringToByteArray(this.SHA1),
|
|
|
|
|
|
SHA256 = TextHelper.StringToByteArray(this.SHA256),
|
2023-08-14 14:53:28 -04:00
|
|
|
|
SpamSum = System.Text.Encoding.UTF8.GetBytes(this.SpamSum ?? 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 14:53:28 -04:00
|
|
|
|
var rom = new Rom(_media.ConvertToRom())
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
|
|
|
|
|
ItemType = ItemType.Rom,
|
|
|
|
|
|
DupeType = this.DupeType,
|
|
|
|
|
|
|
2023-08-14 13:17:51 -04:00
|
|
|
|
Machine = this.Machine?.Clone() as Machine,
|
|
|
|
|
|
Source = this.Source?.Clone() as Source,
|
2020-08-27 16:57:22 -07:00
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return rom;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Comparision Methods
|
|
|
|
|
|
|
2022-11-03 12:22:17 -07:00
|
|
|
|
/// <inheritdoc/>
|
2023-08-14 13:17:51 -04:00
|
|
|
|
public override bool Equals(DatItem? other)
|
2020-08-27 16:57:22 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If we don't have a Media, return false
|
2023-08-14 13:17:51 -04:00
|
|
|
|
if (ItemType != other?.ItemType || other is not Media otherInternal)
|
|
|
|
|
|
return false;
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
2023-08-14 13:17:51 -04:00
|
|
|
|
// Compare the internal models
|
|
|
|
|
|
return _media.EqualTo(otherInternal._media);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fill any missing size and hash information from another Media
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="other">Media to fill information from</param>
|
2023-08-14 19:06:08 -04:00
|
|
|
|
public void FillMissingInformation(Media other) => _media?.FillMissingHashes(other?._media);
|
2020-08-27 16:57:22 -07:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Get unique duplicate suffix on name collision
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>String representing the suffix</returns>
|
|
|
|
|
|
public string GetDuplicateSuffix()
|
|
|
|
|
|
{
|
2023-08-14 13:17:51 -04:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(MD5))
|
2020-08-27 16:57:22 -07:00
|
|
|
|
return $"_{MD5}";
|
2023-08-14 13:17:51 -04:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(SHA1))
|
2020-08-27 16:57:22 -07:00
|
|
|
|
return $"_{SHA1}";
|
2023-08-14 13:17:51 -04:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(SHA256))
|
2020-08-27 16:57:22 -07:00
|
|
|
|
return $"_{SHA256}";
|
2023-08-14 13:17:51 -04:00
|
|
|
|
else if (!string.IsNullOrWhiteSpace(SpamSum))
|
2020-09-04 15:02:15 -07:00
|
|
|
|
return $"_{SpamSum}";
|
2020-08-27 16:57:22 -07:00
|
|
|
|
else
|
|
|
|
|
|
return "_1";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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:
|
2020-08-27 16:57:22 -07:00
|
|
|
|
key = MD5;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA1:
|
2020-08-27 16:57:22 -07:00
|
|
|
|
key = SHA1;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SHA256:
|
2020-08-27 16:57:22 -07:00
|
|
|
|
key = SHA256;
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2020-12-14 15:31:28 -08:00
|
|
|
|
case ItemKey.SpamSum:
|
2020-09-04 15:02:15 -07:00
|
|
|
|
key = SpamSum;
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|