Files
SabreTools/SabreTools.Serialization/AttractMode.Serializer.cs

103 lines
3.4 KiB
C#
Raw Normal View History

2023-07-29 00:06:31 -04:00
using System.IO;
using System.Linq;
using System.Text;
using SabreTools.IO.Writers;
using SabreTools.Models.AttractMode;
namespace SabreTools.Serialization
{
/// <summary>
/// Separated value serializer for AttractMode romlists
/// </summary>
public partial class AttractMode
{
/// <summary>
/// Serializes the defined type to an AttractMode romlist
/// </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)
{
2023-07-30 09:00:15 -04:00
using var stream = SerializeToStream(metadataFile);
if (stream == null)
2023-07-29 00:06:31 -04:00
return false;
2023-07-30 09:00:15 -04:00
using var fs = File.OpenWrite(path);
stream.Seek(0, SeekOrigin.Begin);
stream.CopyTo(fs);
return true;
2023-07-29 00:06:31 -04:00
}
/// <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)
{
2023-07-30 09:00:15 -04:00
// If the metadata file is null
if (metadataFile == null)
return null;
2023-07-29 00:06:31 -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 = ';',
Quotes = false,
VerifyFieldCount = false,
};
2023-07-29 00:06:31 -04:00
2023-07-30 09:00:15 -04:00
// TODO: Include flag to write out long or short header
// Write the short header
writer.WriteString(HeaderWithoutRomname); // TODO: Convert to array of values
2023-07-29 00:06:31 -04:00
2023-07-30 09:00:15 -04:00
// Write out the rows, if they exist
WriteRows(metadataFile.Row, writer);
2023-07-29 00:06:31 -04:00
2023-07-30 09:00:15 -04:00
// Return the stream
return stream;
2023-07-29 00:06:31 -04:00
}
/// <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 00:06:31 -04:00
{
row.Name,
row.Title,
row.Emulator,
row.CloneOf,
row.Year,
row.Manufacturer,
row.Category,
row.Players,
row.Rotation,
row.Control,
row.Status,
row.DisplayCount,
row.DisplayType,
row.AltRomname,
row.AltTitle,
row.Extra,
row.Buttons,
};
writer.WriteValues(rowArray);
writer.Flush();
}
}
}
}