mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Create and use Everdrive SMDB serializer
This commit is contained in:
102
SabreTools.Serialization/EverdriveSMDB.Serializer.cs
Normal file
102
SabreTools.Serialization/EverdriveSMDB.Serializer.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using SabreTools.IO.Writers;
|
||||
using SabreTools.Models.EverdriveSMDB;
|
||||
|
||||
namespace SabreTools.Serialization
|
||||
{
|
||||
/// <summary>
|
||||
/// Separated value serializer for Everdrive SMDBs
|
||||
/// </summary>
|
||||
public partial class EverdriveSMDB
|
||||
{
|
||||
/// <summary>
|
||||
/// Serializes the defined type to an Everdrive SMDB file
|
||||
/// </summary>
|
||||
/// <param name="metadataFile">Data to serialize</param>
|
||||
/// <param name="path">Path to the file to serialize to</param>
|
||||
/// <returns>True on successful serialization, false otherwise</returns>
|
||||
public static bool SerializeToFile(MetadataFile? metadataFile, string path)
|
||||
{
|
||||
try
|
||||
{
|
||||
using var stream = SerializeToStream(metadataFile);
|
||||
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="metadataFile">Data to serialize</param>
|
||||
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
||||
public static Stream? SerializeToStream(MetadataFile? metadataFile)
|
||||
{
|
||||
try
|
||||
{
|
||||
// If the metadata file is null
|
||||
if (metadataFile == null)
|
||||
return null;
|
||||
|
||||
// Setup the writer and output
|
||||
var stream = new MemoryStream();
|
||||
var writer = new SeparatedValueWriter(stream, Encoding.UTF8) { Separator = '\t', Quotes = false };
|
||||
|
||||
// Write out the rows, if they exist
|
||||
WriteRows(metadataFile.Row, writer);
|
||||
|
||||
// Return the stream
|
||||
return stream;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// TODO: Handle logging the exception
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write rows information to the current writer
|
||||
/// </summary>
|
||||
/// <param name="rows">Array of Row objects representing the rows information</param>
|
||||
/// <param name="writer">SeparatedValueWriter representing the output</param>
|
||||
private static void WriteRows(Row[]? rows, SeparatedValueWriter writer)
|
||||
{
|
||||
// If the games information is missing, we can't do anything
|
||||
if (rows == null || !rows.Any())
|
||||
return;
|
||||
|
||||
// Loop through and write out the rows
|
||||
foreach (var row in rows)
|
||||
{
|
||||
var rowArray = new List<string>
|
||||
{
|
||||
row.SHA256,
|
||||
row.Name,
|
||||
row.SHA1,
|
||||
row.MD5,
|
||||
row.CRC32,
|
||||
};
|
||||
|
||||
if (row.Size != null)
|
||||
rowArray.Add(row.Size);
|
||||
|
||||
writer.WriteValues(rowArray.ToArray());
|
||||
writer.Flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user