2023-07-29 21:24:16 -04:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using SabreTools.IO.Writers;
|
|
|
|
|
using SabreTools.Models.SeparatedValue;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Serialization
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for separated-value variants
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class SeparatedValue
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes the defined type to a separated-value variant
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="metadataFile">Data to serialize</param>
|
|
|
|
|
/// <param name="path">Path to the file to serialize to</param>
|
|
|
|
|
/// <param name="delim">Character delimiter between values</param>
|
|
|
|
|
/// <returns>True on successful serialization, false otherwise</returns>
|
|
|
|
|
public static bool SerializeToFile(MetadataFile? metadataFile, string path, char delim)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
using var stream = SerializeToStream(metadataFile, delim);
|
|
|
|
|
if (stream == null)
|
2023-07-29 21:24:16 -04:00
|
|
|
return false;
|
2023-07-30 09:00:15 -04:00
|
|
|
|
|
|
|
|
using var fs = File.OpenWrite(path);
|
|
|
|
|
stream.CopyTo(fs);
|
|
|
|
|
return true;
|
2023-07-29 21:24:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializes the defined type to a stream
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="metadataFile">Data to serialize</param>
|
|
|
|
|
/// <param name="delim">Character delimiter between values</param>
|
|
|
|
|
/// <returns>Stream containing serialized data on success, null otherwise</returns>
|
|
|
|
|
public static Stream? SerializeToStream(MetadataFile? metadataFile, char delim)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
// If the metadata file is null
|
|
|
|
|
if (metadataFile == null)
|
|
|
|
|
return null;
|
2023-07-29 21:24:16 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Setup the writer and output
|
|
|
|
|
var stream = new MemoryStream();
|
|
|
|
|
var writer = new SeparatedValueWriter(stream, Encoding.UTF8) { Separator = delim, Quotes = true };
|
2023-07-29 21:24:16 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// TODO: Include flag to write out long or short header
|
|
|
|
|
// Write the short header
|
|
|
|
|
WriteHeader(writer);
|
2023-07-29 21:24:16 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Write out the rows, if they exist
|
|
|
|
|
WriteRows(metadataFile.Row, writer);
|
2023-07-29 21:24:16 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Return the stream
|
2023-07-30 21:27:02 -04:00
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
2023-07-30 09:00:15 -04:00
|
|
|
return stream;
|
2023-07-29 21:24:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write header information to the current writer
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="writer">SeparatedValueWriter representing the output</param>
|
|
|
|
|
private static void WriteHeader(SeparatedValueWriter writer)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
var headerArray = new string?[]
|
2023-07-29 21:24:16 -04:00
|
|
|
{
|
|
|
|
|
"File Name",
|
|
|
|
|
"Internal Name",
|
|
|
|
|
"Description",
|
|
|
|
|
"Game Name",
|
|
|
|
|
"Game Description",
|
|
|
|
|
"Type",
|
|
|
|
|
"Rom Name",
|
|
|
|
|
"Disk Name",
|
|
|
|
|
"Size",
|
|
|
|
|
"CRC",
|
|
|
|
|
"MD5",
|
|
|
|
|
"SHA1",
|
|
|
|
|
"SHA256",
|
|
|
|
|
//"SHA384",
|
|
|
|
|
//"SHA512",
|
|
|
|
|
//"SpamSum",
|
|
|
|
|
"Status",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
writer.WriteHeader(headerArray);
|
|
|
|
|
writer.Flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
var rowArray = new string?[]
|
2023-07-29 21:24:16 -04:00
|
|
|
{
|
|
|
|
|
row.FileName,
|
|
|
|
|
row.InternalName,
|
|
|
|
|
row.Description,
|
|
|
|
|
row.GameName,
|
|
|
|
|
row.GameDescription,
|
|
|
|
|
row.Type,
|
|
|
|
|
row.RomName,
|
|
|
|
|
row.DiskName,
|
|
|
|
|
row.Size,
|
|
|
|
|
row.CRC,
|
|
|
|
|
row.MD5,
|
|
|
|
|
row.SHA1,
|
|
|
|
|
row.SHA256,
|
|
|
|
|
//row.SHA384,
|
|
|
|
|
//row.SHA512,
|
|
|
|
|
//row.SpamSum,
|
|
|
|
|
row.Status,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
writer.WriteValues(rowArray);
|
|
|
|
|
writer.Flush();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|