2023-07-13 16:26:03 -04:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using SabreTools.Core;
|
2023-08-09 22:35:51 -04:00
|
|
|
using SabreTools.Models.Hashfile;
|
2023-07-13 16:26:03 -04:00
|
|
|
|
|
|
|
|
namespace SabreTools.Serialization
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2023-07-28 21:34:34 -04:00
|
|
|
/// Deserializer for hashfile variants
|
2023-07-13 16:26:03 -04:00
|
|
|
/// </summary>
|
2023-07-28 21:34:34 -04:00
|
|
|
public partial class Hashfile
|
2023-07-13 16:26:03 -04:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes a hashfile variant to the defined type
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="path">Path to the file to deserialize</param>
|
|
|
|
|
/// <param name="hash">Hash corresponding to the hashfile variant</param>
|
|
|
|
|
/// <returns>Deserialized data on success, null on failure</returns>
|
|
|
|
|
public static Models.Hashfile.Hashfile? Deserialize(string path, Hash hash)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
using var stream = PathProcessor.OpenStream(path);
|
|
|
|
|
return Deserialize(stream, hash);
|
2023-07-13 16:26:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes a hashfile variant in a stream to the defined type
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="stream">Stream to deserialize</param>
|
|
|
|
|
/// <param name="hash">Hash corresponding to the hashfile variant</param>
|
|
|
|
|
/// <returns>Deserialized data on success, null on failure</returns>
|
|
|
|
|
public static Models.Hashfile.Hashfile? Deserialize(Stream? stream, Hash hash)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
// If the stream is null
|
|
|
|
|
if (stream == null)
|
|
|
|
|
return default;
|
2023-07-13 16:26:03 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Setup the reader and output
|
|
|
|
|
var reader = new StreamReader(stream);
|
|
|
|
|
var dat = new Models.Hashfile.Hashfile();
|
|
|
|
|
var additional = new List<string>();
|
2023-07-13 16:26:03 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Loop through the rows and parse out values
|
|
|
|
|
var hashes = new List<object>();
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
// Read and split the line
|
|
|
|
|
string? line = reader.ReadLine();
|
|
|
|
|
string[]? lineParts = line?.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
|
|
|
|
if (lineParts == null)
|
|
|
|
|
continue;
|
2023-07-13 16:26:03 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Parse the line into a hash
|
2023-07-13 16:26:03 -04:00
|
|
|
switch (hash)
|
|
|
|
|
{
|
|
|
|
|
case Hash.CRC:
|
2023-08-09 22:35:51 -04:00
|
|
|
var sfv = new SFV
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
File = string.Join(" ", lineParts[..^1]),
|
|
|
|
|
Hash = string.Join(" ", lineParts[^1]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(sfv);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.MD5:
|
2023-08-09 22:35:51 -04:00
|
|
|
var md5 = new MD5
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
Hash = lineParts[0],
|
|
|
|
|
File = string.Join(" ", lineParts[1..]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(md5);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA1:
|
2023-08-09 22:35:51 -04:00
|
|
|
var sha1 = new SHA1
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
Hash = lineParts[0],
|
|
|
|
|
File = string.Join(" ", lineParts[1..]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(sha1);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA256:
|
2023-08-09 22:35:51 -04:00
|
|
|
var sha256 = new SHA256
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
Hash = lineParts[0],
|
|
|
|
|
File = string.Join(" ", lineParts[1..]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(sha256);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA384:
|
2023-08-09 22:35:51 -04:00
|
|
|
var sha384 = new SHA384
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
Hash = lineParts[0],
|
|
|
|
|
File = string.Join(" ", lineParts[1..]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(sha384);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA512:
|
2023-08-09 22:35:51 -04:00
|
|
|
var sha512 = new SHA512
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
Hash = lineParts[0],
|
|
|
|
|
File = string.Join(" ", lineParts[1..]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(sha512);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SpamSum:
|
2023-08-09 22:35:51 -04:00
|
|
|
var spamSum = new SpamSum
|
2023-07-30 09:00:15 -04:00
|
|
|
{
|
|
|
|
|
Hash = lineParts[0],
|
|
|
|
|
File = string.Join(" ", lineParts[1..]),
|
|
|
|
|
};
|
|
|
|
|
hashes.Add(spamSum);
|
2023-07-13 16:26:03 -04:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
|
|
|
|
|
// Assign the hashes to the hashfile and return
|
|
|
|
|
switch (hash)
|
2023-07-13 16:26:03 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
case Hash.CRC:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.SFV = hashes.Cast<SFV>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.MD5:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.MD5 = hashes.Cast<MD5>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA1:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.SHA1 = hashes.Cast<SHA1>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA256:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.SHA256 = hashes.Cast<SHA256>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA384:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.SHA384 = hashes.Cast<SHA384>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SHA512:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.SHA512 = hashes.Cast<SHA512>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
|
|
|
|
case Hash.SpamSum:
|
2023-08-09 22:35:51 -04:00
|
|
|
dat.SpamSum = hashes.Cast<SpamSum>().ToArray();
|
2023-07-30 09:00:15 -04:00
|
|
|
break;
|
2023-07-13 16:26:03 -04:00
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
dat.ADDITIONAL_ELEMENTS = additional.ToArray();
|
|
|
|
|
return dat;
|
2023-07-13 16:26:03 -04:00
|
|
|
}
|
2023-08-09 22:35:51 -04:00
|
|
|
|
|
|
|
|
#region Internal
|
|
|
|
|
|
2023-08-10 00:59:36 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.MetadataFile"/> to an array of <cref="Models.Hashfile.Hashfile"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
/// <remarks>TODO: Add machine name prefixes to all items</remarks>
|
|
|
|
|
public static Models.Hashfile.Hashfile? ConvertFromInternalModel(Models.Internal.MetadataFile? item, Hash hash)
|
2023-08-10 00:59:36 -04:00
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var machines = item.Read<Models.Internal.Machine[]>(Models.Internal.MetadataFile.MachineKey);
|
2023-08-10 11:35:32 -04:00
|
|
|
if (machines == null || !machines.Any())
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var hashfiles = machines
|
|
|
|
|
.Where(m => m != null)
|
|
|
|
|
.Select(machine => ConvertMachineFromInternalModel(machine, hash));
|
|
|
|
|
|
|
|
|
|
var sfvs = new List<SFV>();
|
|
|
|
|
var md5s = new List<MD5>();
|
|
|
|
|
var sha1s = new List<SHA1>();
|
|
|
|
|
var sha256s = new List<SHA256>();
|
|
|
|
|
var sha384s = new List<SHA384>();
|
|
|
|
|
var sha512s = new List<SHA512>();
|
|
|
|
|
var spamsums = new List<SpamSum>();
|
|
|
|
|
|
|
|
|
|
foreach (var hashfile in hashfiles)
|
|
|
|
|
{
|
|
|
|
|
if (hashfile.SFV != null && hashfile.SFV.Any())
|
|
|
|
|
sfvs.AddRange(hashfile.SFV);
|
|
|
|
|
if (hashfile.MD5 != null && hashfile.MD5.Any())
|
|
|
|
|
md5s.AddRange(hashfile.MD5);
|
|
|
|
|
if (hashfile.SHA1 != null && hashfile.SHA1.Any())
|
|
|
|
|
sha1s.AddRange(hashfile.SHA1);
|
|
|
|
|
if (hashfile.SHA256 != null && hashfile.SHA256.Any())
|
|
|
|
|
sha256s.AddRange(hashfile.SHA256);
|
|
|
|
|
if (hashfile.SHA384 != null && hashfile.SHA384.Any())
|
|
|
|
|
sha384s.AddRange(hashfile.SHA384);
|
|
|
|
|
if (hashfile.SHA512 != null && hashfile.SHA512.Any())
|
|
|
|
|
sha512s.AddRange(hashfile.SHA512);
|
|
|
|
|
if (hashfile.SpamSum != null && hashfile.SpamSum.Any())
|
|
|
|
|
spamsums.AddRange(hashfile.SpamSum);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hashfileItem = new Models.Hashfile.Hashfile();
|
|
|
|
|
|
|
|
|
|
if (sfvs.Any())
|
|
|
|
|
hashfileItem.SFV = sfvs.ToArray();
|
|
|
|
|
if (md5s.Any())
|
|
|
|
|
hashfileItem.MD5 = md5s.ToArray();
|
|
|
|
|
if (sha1s.Any())
|
|
|
|
|
hashfileItem.SHA1 = sha1s.ToArray();
|
|
|
|
|
if (sha256s.Any())
|
|
|
|
|
hashfileItem.SHA256 = sha256s.ToArray();
|
|
|
|
|
if (sha384s.Any())
|
|
|
|
|
hashfileItem.SHA384 = sha384s.ToArray();
|
|
|
|
|
if (sha512s.Any())
|
|
|
|
|
hashfileItem.SHA512 = sha512s.ToArray();
|
|
|
|
|
if (spamsums.Any())
|
|
|
|
|
hashfileItem.SpamSum = spamsums.ToArray();
|
|
|
|
|
|
|
|
|
|
return hashfileItem;
|
2023-08-10 00:59:36 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 22:35:51 -04:00
|
|
|
/// <summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
/// Convert from <cref="Models.Internal.MetadataFile"/> to an array of <cref="Models.Hashfile.Hashfile"/>
|
2023-08-09 22:35:51 -04:00
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
public static Models.Hashfile.Hashfile[]? ConvertArrayFromInternalModel(Models.Internal.MetadataFile? item, Hash hash)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-10 11:35:32 -04:00
|
|
|
var machines = item.Read<Models.Internal.Machine[]>(Models.Internal.MetadataFile.MachineKey);
|
|
|
|
|
if (machines != null && machines.Any())
|
|
|
|
|
{
|
|
|
|
|
return machines
|
|
|
|
|
.Where(m => m != null)
|
|
|
|
|
.Select(machine => ConvertMachineFromInternalModel(machine, hash))
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Machine"/> to <cref="Models.Hashfile.Hashfile"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static Models.Hashfile.Hashfile ConvertMachineFromInternalModel(Models.Internal.Machine item, Hash hash)
|
|
|
|
|
{
|
2023-08-09 22:35:51 -04:00
|
|
|
var roms = item.Read<Models.Internal.Rom[]>(Models.Internal.Machine.RomKey);
|
2023-08-10 11:35:32 -04:00
|
|
|
if (roms == null)
|
|
|
|
|
return new Models.Hashfile.Hashfile();
|
|
|
|
|
|
2023-08-09 22:35:51 -04:00
|
|
|
return new Models.Hashfile.Hashfile
|
|
|
|
|
{
|
2023-08-10 11:35:32 -04:00
|
|
|
SFV = hash == Hash.CRC
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToSFV)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
|
|
|
|
MD5 = hash == Hash.MD5
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToMD5)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
|
|
|
|
SHA1 = hash == Hash.SHA1
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToSHA1)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
|
|
|
|
SHA256 = hash == Hash.SHA256
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToSHA256)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
|
|
|
|
SHA384 = hash == Hash.SHA384
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToSHA384)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
|
|
|
|
SHA512 = hash == Hash.SHA512
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToSHA512)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
|
|
|
|
SpamSum = hash == Hash.SpamSum
|
|
|
|
|
? roms
|
|
|
|
|
.Where(r => r != null)
|
|
|
|
|
.Select(ConvertToSpamSum)
|
|
|
|
|
.ToArray()
|
|
|
|
|
: null,
|
2023-08-09 22:35:51 -04:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.MD5"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static MD5 ConvertToMD5(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var md5 = new MD5
|
|
|
|
|
{
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.MD5Key),
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
};
|
|
|
|
|
return md5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.SFV"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static SFV ConvertToSFV(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var sfv = new SFV
|
|
|
|
|
{
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.CRCKey),
|
|
|
|
|
};
|
|
|
|
|
return sfv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.SHA1"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static SHA1 ConvertToSHA1(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var sha1 = new SHA1
|
|
|
|
|
{
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.SHA1Key),
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
};
|
|
|
|
|
return sha1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.SHA256"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static SHA256 ConvertToSHA256(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var sha256 = new SHA256
|
|
|
|
|
{
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.SHA256Key),
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
};
|
|
|
|
|
return sha256;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.SHA384"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static SHA384 ConvertToSHA384(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var sha384 = new SHA384
|
|
|
|
|
{
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.SHA384Key),
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
};
|
|
|
|
|
return sha384;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.SHA512"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static SHA512 ConvertToSHA512(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var sha512 = new SHA512
|
|
|
|
|
{
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.SHA512Key),
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
};
|
|
|
|
|
return sha512;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Hashfile.SpamSum"/>
|
|
|
|
|
/// </summary>
|
2023-08-10 11:35:32 -04:00
|
|
|
private static SpamSum ConvertToSpamSum(Models.Internal.Rom item)
|
2023-08-09 22:35:51 -04:00
|
|
|
{
|
|
|
|
|
var spamsum = new SpamSum
|
|
|
|
|
{
|
|
|
|
|
Hash = item.ReadString(Models.Internal.Rom.SpamSumKey),
|
|
|
|
|
File = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
};
|
|
|
|
|
return spamsum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2023-07-13 16:26:03 -04:00
|
|
|
}
|
|
|
|
|
}
|