using System.IO; using System.Linq; using System.Text; using SabreTools.IO.Writers; using SabreTools.Models.SeparatedValue; namespace SabreTools.Serialization { /// /// Serializer for separated-value variants /// public partial class SeparatedValue { /// /// Serializes the defined type to a separated-value variant /// /// Data to serialize /// Path to the file to serialize to /// Character delimiter between values /// True on successful serialization, false otherwise public static bool SerializeToFile(MetadataFile? metadataFile, string path, char delim) { using var stream = SerializeToStream(metadataFile, delim); if (stream == null) return false; using var fs = File.OpenWrite(path); stream.CopyTo(fs); return true; } /// /// Serializes the defined type to a stream /// /// Data to serialize /// Character delimiter between values /// Stream containing serialized data on success, null otherwise public static Stream? SerializeToStream(MetadataFile? metadataFile, char delim) { // 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 = delim, Quotes = true }; // TODO: Include flag to write out long or short header // Write the short header WriteHeader(writer); // Write out the rows, if they exist WriteRows(metadataFile.Row, writer); // Return the stream stream.Seek(0, SeekOrigin.Begin); return stream; } /// /// Write header information to the current writer /// /// SeparatedValueWriter representing the output private static void WriteHeader(SeparatedValueWriter writer) { var headerArray = new string?[] { "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(); } /// /// Write rows information to the current writer /// /// Array of Row objects representing the rows information /// SeparatedValueWriter representing the output 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 string?[] { 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(); } } } }