Create and use Hashfile serializer

This commit is contained in:
Matt Nadareski
2023-07-28 21:34:34 -04:00
parent 8cf93a4b3b
commit 5adcd5f18b
6 changed files with 1296 additions and 474 deletions

View File

@@ -76,7 +76,7 @@ namespace SabreTools.Serialization
/// Write header information to the current writer
/// </summary>
/// <param name="header">ClrMamePro representing the header information</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteHeader(Models.ClrMamePro.ClrMamePro? header, ClrMameProWriter writer)
{
// If the header information is missing, we can't do anything
@@ -109,7 +109,7 @@ namespace SabreTools.Serialization
/// Write games information to the current writer
/// </summary>
/// <param name="games">Array of GameBase objects representing the games information</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteGames(GameBase[]? games, ClrMameProWriter writer)
{
// If the games information is missing, we can't do anything
@@ -128,7 +128,7 @@ namespace SabreTools.Serialization
/// Write game information to the current writer
/// </summary>
/// <param name="game">GameBase object representing the game information</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteGame(GameBase game, ClrMameProWriter writer)
{
// If the game information is missing, we can't do anything
@@ -183,7 +183,7 @@ namespace SabreTools.Serialization
/// Write releases information to the current writer
/// </summary>
/// <param name="releases">Array of Release objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteReleases(Release[]? releases, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -206,7 +206,7 @@ namespace SabreTools.Serialization
/// Write biossets information to the current writer
/// </summary>
/// <param name="biossets">Array of BiosSet objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteBiosSets(BiosSet[]? biossets, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -227,7 +227,7 @@ namespace SabreTools.Serialization
/// Write roms information to the current writer
/// </summary>
/// <param name="roms">Array of Rom objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteRoms(Rom[]? roms, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -266,7 +266,7 @@ namespace SabreTools.Serialization
/// Write disks information to the current writer
/// </summary>
/// <param name="disks">Array of Disk objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteDisks(Disk[]? disks, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -290,7 +290,7 @@ namespace SabreTools.Serialization
/// Write medias information to the current writer
/// </summary>
/// <param name="medias">Array of Media objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteMedia(Media[]? medias, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -313,7 +313,7 @@ namespace SabreTools.Serialization
/// Write samples information to the current writer
/// </summary>
/// <param name="samples">Array of Sample objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteSamples(Sample[]? samples, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -332,7 +332,7 @@ namespace SabreTools.Serialization
/// Write archives information to the current writer
/// </summary>
/// <param name="archives">Array of Archive objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteArchives(Archive[]? archives, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -351,7 +351,7 @@ namespace SabreTools.Serialization
/// Write chips information to the current writer
/// </summary>
/// <param name="chips">Array of Chip objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteChips(Chip[]? chips, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -373,7 +373,7 @@ namespace SabreTools.Serialization
/// Write video information to the current writer
/// </summary>
/// <param name="video">Video object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteVideo(Video? video, ClrMameProWriter writer)
{
// If the item is missing, we can't do anything
@@ -395,7 +395,7 @@ namespace SabreTools.Serialization
/// Write sound information to the current writer
/// </summary>
/// <param name="sound">Sound object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteSound(Sound? sound, ClrMameProWriter writer)
{
// If the item is missing, we can't do anything
@@ -411,7 +411,7 @@ namespace SabreTools.Serialization
/// Write input information to the current writer
/// </summary>
/// <param name="input">Input object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteInput(Input? input, ClrMameProWriter writer)
{
// If the item is missing, we can't do anything
@@ -432,7 +432,7 @@ namespace SabreTools.Serialization
/// Write dipswitches information to the current writer
/// </summary>
/// <param name="dipswitches">Array of DipSwitch objects to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteDipSwitches(DipSwitch[]? dipswitches, ClrMameProWriter writer)
{
// If the array is missing, we can't do anything
@@ -456,7 +456,7 @@ namespace SabreTools.Serialization
/// Write driver information to the current writer
/// </summary>
/// <param name="driver">Driver object to write</param>
/// <param name="reader">ClrMameProReader representing the metadata file</param>
/// <param name="writer">ClrMameProWriter representing the output</param>
private static void WriteDriver(Driver? driver, ClrMameProWriter writer)
{
// If the item is missing, we can't do anything

View File

@@ -7,9 +7,9 @@ using SabreTools.Core;
namespace SabreTools.Serialization
{
/// <summary>
/// Serializer for hashfile variants
/// Deserializer for hashfile variants
/// </summary>
public class Hashfile
public partial class Hashfile
{
/// <summary>
/// Deserializes a hashfile variant to the defined type

View File

@@ -0,0 +1,232 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.Core;
using SabreTools.IO.Writers;
using SabreTools.Models.Hashfile;
namespace SabreTools.Serialization
{
/// <summary>
/// Serializer for hashfile variants
/// </summary>
public partial class Hashfile
{
/// <summary>
/// Serializes the defined type to a hashfile variant file
/// </summary>
/// <param name="hashfile">Data to serialize</param>
/// <param name="path">Path to the file to serialize to</param>
/// <param name="hash">Hash corresponding to the hashfile variant</param>
/// <returns>True on successful serialization, false otherwise</returns>
public static bool SerializeToFile(Models.Hashfile.Hashfile? hashfile, string path, Hash hash)
{
try
{
using var stream = SerializeToStream(hashfile, hash);
if (stream == null)
return false;
using var fs = File.OpenWrite(path);
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fs);
return true;
}
catch
{
// TODO: Handle logging the exception
return false;
}
}
/// <summary>
/// Serializes the defined type to a stream
/// </summary>
/// <param name="hashfile">Data to serialize</param>
/// <param name="hash">Hash corresponding to the hashfile variant</param>
/// <returns>Stream containing serialized data on success, null otherwise</returns>
public static Stream? SerializeToStream(Models.Hashfile.Hashfile? hashfile, Hash hash)
{
try
{
// If the metadata file is null
if (hashfile == null)
return null;
// Setup the writer and output
var stream = new MemoryStream();
var writer = new SeparatedValueWriter(stream, Encoding.UTF8) { Separator = ' ', Quotes = false };
// Write out the items, if they exist
switch (hash)
{
case Hash.CRC:
WriteSFV(hashfile.SFV, writer);
break;
case Hash.MD5:
WriteMD5(hashfile.MD5, writer);
break;
case Hash.SHA1:
WriteSHA1(hashfile.SHA1, writer);
break;
case Hash.SHA256:
WriteSHA256(hashfile.SHA256, writer);
break;
case Hash.SHA384:
WriteSHA384(hashfile.SHA384, writer);
break;
case Hash.SHA512:
WriteSHA512(hashfile.SHA512, writer);
break;
case Hash.SpamSum:
WriteSpamSum(hashfile.SpamSum, writer);
break;
default:
throw new ArgumentOutOfRangeException(nameof(hash));
}
// Return the stream
return stream;
}
catch
{
// TODO: Handle logging the exception
return null;
}
}
/// <summary>
/// Write SFV information to the current writer
/// </summary>
/// <param name="sfvs">Array of SFV objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSFV(SFV[]? sfvs, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sfvs == null || !sfvs.Any())
return;
// Loop through and write out the items
foreach (var sfv in sfvs)
{
writer.WriteValues(new string[] { sfv.File, sfv.Hash });
writer.Flush();
}
}
/// <summary>
/// Write MD5 information to the current writer
/// </summary>
/// <param name="md5s">Array of MD5 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteMD5(MD5[]? md5s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (md5s == null || !md5s.Any())
return;
// Loop through and write out the items
foreach (var md5 in md5s)
{
writer.WriteValues(new string[] { md5.Hash, md5.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA1 information to the current writer
/// </summary>
/// <param name="sha1s">Array of SHA1 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA1(SHA1[]? sha1s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha1s == null || !sha1s.Any())
return;
// Loop through and write out the items
foreach (var sha1 in sha1s)
{
writer.WriteValues(new string[] { sha1.Hash, sha1.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA256 information to the current writer
/// </summary>
/// <param name="sha256s">Array of SHA256 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA256(SHA256[]? sha256s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha256s == null || !sha256s.Any())
return;
// Loop through and write out the items
foreach (var sha256 in sha256s)
{
writer.WriteValues(new string[] { sha256.Hash, sha256.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA384 information to the current writer
/// </summary>
/// <param name="sha384s">Array of SHA384 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA384(SHA384[]? sha384s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha384s == null || !sha384s.Any())
return;
// Loop through and write out the items
foreach (var sha384 in sha384s)
{
writer.WriteValues(new string[] { sha384.Hash, sha384.File });
writer.Flush();
}
}
/// <summary>
/// Write SHA512 information to the current writer
/// </summary>
/// <param name="sha512s">Array of SHA512 objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSHA512(SHA512[]? sha512s, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (sha512s == null || !sha512s.Any())
return;
// Loop through and write out the items
foreach (var sha512 in sha512s)
{
writer.WriteValues(new string[] { sha512.Hash, sha512.File });
writer.Flush();
}
}
/// <summary>
/// Write SpamSum information to the current writer
/// </summary>
/// <param name="spamsums">Array of SpamSum objects representing the files</param>
/// <param name="writer">SeparatedValueWriter representing the output</param>
private static void WriteSpamSum(SpamSum[]? spamsums, SeparatedValueWriter writer)
{
// If the item information is missing, we can't do anything
if (spamsums == null || !spamsums.Any())
return;
// Loop through and write out the items
foreach (var spamsum in spamsums)
{
writer.WriteValues(new string[] { spamsum.Hash, spamsum.File });
writer.Flush();
}
}
}
}