Files
SabreTools/SabreTools.DatItems/Formats/Media.cs

239 lines
7.1 KiB
C#
Raw Normal View History

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;
using SabreTools.Core.Tools;
2020-12-08 14:53:49 -08:00
using SabreTools.FileTypes;
2021-02-02 10:23:43 -08:00
namespace SabreTools.DatItems.Formats
{
/// <summary>
/// Represents Aaruformat images which use internal hashes
/// </summary>
2020-09-08 10:12:41 -07:00
[JsonObject("media"), XmlRoot("media")]
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")]
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
/// <summary>
/// Data MD5 hash
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("md5", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("md5")]
public string? MD5
{
get => _media.ReadString(Models.Internal.Media.MD5Key);
set => _media[Models.Internal.Media.MD5Key] = TextHelper.NormalizeMD5(value);
}
/// <summary>
/// Data SHA-1 hash
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("sha1", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha1")]
public string? SHA1
{
get => _media.ReadString(Models.Internal.Media.SHA1Key);
set => _media[Models.Internal.Media.SHA1Key] = TextHelper.NormalizeSHA1(value);
}
/// <summary>
/// Data SHA-256 hash
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("sha256", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("sha256")]
public string? SHA256
{
get => _media.ReadString(Models.Internal.Media.SHA256Key);
set => _media[Models.Internal.Media.SHA256Key] = TextHelper.NormalizeSHA256(value);
}
/// <summary>
/// File SpamSum fuzzy hash
/// </summary>
2022-11-03 12:22:17 -07:00
[JsonProperty("spamsum", DefaultValueHandling = DefaultValueHandling.Ignore), XmlElement("spamsum")]
public string? SpamSum
{
get => _media.ReadString(Models.Internal.Media.SpamSumKey);
set => _media[Models.Internal.Media.SpamSumKey] = value;
}
/// <summary>
/// Internal Media model
/// </summary>
[JsonIgnore]
private Models.Internal.Media _media = new();
#endregion
#region Accessors
/// <inheritdoc/>
public override string? GetName() => Name;
2020-09-02 12:19:12 -07:00
/// <inheritdoc/>
public override void SetName(string? name) => Name = name;
#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>());
ItemType = ItemType.Media;
DupeType = 0x00;
}
#endregion
#region Cloning Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
public override object Clone()
{
return new Media()
{
ItemType = this.ItemType,
DupeType = this.DupeType,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
Remove = this.Remove,
_media = this._media?.Clone() as Models.Internal.Media ?? new Models.Internal.Media(),
};
}
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
};
}
/// <summary>
/// Convert a media to the closest Rom approximation
/// </summary>
/// <returns></returns>
public Rom ConvertToRom()
{
2023-08-14 14:53:28 -04:00
var rom = new Rom(_media.ConvertToRom())
{
ItemType = ItemType.Rom,
DupeType = this.DupeType,
Machine = this.Machine?.Clone() as Machine,
Source = this.Source?.Clone() as Source,
Remove = this.Remove,
};
return rom;
}
#endregion
#region Comparision Methods
2022-11-03 12:22:17 -07:00
/// <inheritdoc/>
public override bool Equals(DatItem? other)
{
// If we don't have a Media, return false
if (ItemType != other?.ItemType || other is not Media otherInternal)
return false;
// Compare the internal models
return _media.EqualTo(otherInternal._media);
}
/// <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:29:10 -04:00
public void FillMissingInformation(Media other) => _media.FillMissingHashes(other?._media);
/// <summary>
/// Get unique duplicate suffix on name collision
/// </summary>
/// <returns>String representing the suffix</returns>
2023-08-14 19:29:10 -04:00
public string GetDuplicateSuffix() => _media.GetDuplicateSuffix();
#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)
{
// 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)
{
2020-12-14 15:31:28 -08:00
case ItemKey.MD5:
key = MD5;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA1:
key = SHA1;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SHA256:
key = SHA256;
break;
2020-12-14 15:31:28 -08:00
case ItemKey.SpamSum:
key = SpamSum;
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;
return key;
}
#endregion
}
}