mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Create and use AttractMode serializer
This commit is contained in:
111
SabreTools.DatFiles/Formats/AttractMode.Reader.cs
Normal file
111
SabreTools.DatFiles/Formats/AttractMode.Reader.cs
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using SabreTools.Core;
|
||||||
|
using SabreTools.DatItems;
|
||||||
|
using SabreTools.DatItems.Formats;
|
||||||
|
|
||||||
|
namespace SabreTools.DatFiles.Formats
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents parsing of an AttractMode DAT
|
||||||
|
/// </summary>
|
||||||
|
internal partial class AttractMode : DatFile
|
||||||
|
{
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Deserialize the input file
|
||||||
|
var metadataFile = Serialization.AttractMode.Deserialize(filename);
|
||||||
|
|
||||||
|
// Convert the row data to the internal format
|
||||||
|
ConvertRows(metadataFile?.Row, filename, indexId, statsOnly);
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (!throwOnError)
|
||||||
|
{
|
||||||
|
string message = $"'{filename}' - An error occurred during parsing";
|
||||||
|
logger.Error(ex, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Converters
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert rows information
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="rows">Array of deserialized models to convert</param>
|
||||||
|
/// <param name="filename">Name of the file to be parsed</param>
|
||||||
|
/// <param name="indexId">Index ID for the DAT</param>
|
||||||
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||||
|
private void ConvertRows(Models.AttractMode.Row[]? rows, string filename, int indexId, bool statsOnly)
|
||||||
|
{
|
||||||
|
// If the rows array is missing, we can't do anything
|
||||||
|
if (rows == null || !rows.Any())
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Loop through the rows and add
|
||||||
|
foreach (var row in rows)
|
||||||
|
{
|
||||||
|
ConvertRow(row, filename, indexId, statsOnly);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert rows information
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="row">Deserialized model to convert</param>
|
||||||
|
/// <param name="filename">Name of the file to be parsed</param>
|
||||||
|
/// <param name="indexId">Index ID for the DAT</param>
|
||||||
|
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
|
||||||
|
private void ConvertRow(Models.AttractMode.Row? row, string filename, int indexId, bool statsOnly)
|
||||||
|
{
|
||||||
|
// If the row is missing, we can't do anything
|
||||||
|
if (row == null)
|
||||||
|
return;
|
||||||
|
|
||||||
|
var rom = new Rom()
|
||||||
|
{
|
||||||
|
Name = "-",
|
||||||
|
Size = Constants.SizeZero,
|
||||||
|
CRC = Constants.CRCZero,
|
||||||
|
MD5 = Constants.MD5Zero,
|
||||||
|
SHA1 = Constants.SHA1Zero,
|
||||||
|
ItemStatus = ItemStatus.None,
|
||||||
|
|
||||||
|
Machine = new Machine
|
||||||
|
{
|
||||||
|
Name = row.Name,
|
||||||
|
Description = row.Title,
|
||||||
|
CloneOf = row.CloneOf,
|
||||||
|
Year = row.Year,
|
||||||
|
Manufacturer = row.Manufacturer,
|
||||||
|
Category = row.Category,
|
||||||
|
Players = row.Players,
|
||||||
|
Rotation = row.Rotation,
|
||||||
|
Control = row.Control,
|
||||||
|
Status = row.Status,
|
||||||
|
DisplayCount = row.DisplayCount,
|
||||||
|
DisplayType = row.DisplayType,
|
||||||
|
Comment = row.Extra,
|
||||||
|
Buttons = row.Buttons
|
||||||
|
},
|
||||||
|
|
||||||
|
AltName = row.AltRomname,
|
||||||
|
AltTitle = row.AltTitle,
|
||||||
|
// TODO: Add extended fields
|
||||||
|
|
||||||
|
Source = new Source
|
||||||
|
{
|
||||||
|
Index = indexId,
|
||||||
|
Name = filename,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Now process and add the rom
|
||||||
|
ParseAddHelper(rom, statsOnly);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
143
SabreTools.DatFiles/Formats/AttractMode.Writer.cs
Normal file
143
SabreTools.DatFiles/Formats/AttractMode.Writer.cs
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using SabreTools.Core;
|
||||||
|
using SabreTools.DatItems;
|
||||||
|
using SabreTools.DatItems.Formats;
|
||||||
|
|
||||||
|
namespace SabreTools.DatFiles.Formats
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Represents parsing and writing of an AttractMode DAT
|
||||||
|
/// </summary>
|
||||||
|
internal partial class AttractMode : DatFile
|
||||||
|
{
|
||||||
|
/// <inheritdoc/>
|
||||||
|
protected override ItemType[] GetSupportedTypes()
|
||||||
|
{
|
||||||
|
return new ItemType[] { ItemType.Rom };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem)
|
||||||
|
{
|
||||||
|
List<DatItemField> missingFields = new();
|
||||||
|
|
||||||
|
// Check item name
|
||||||
|
if (string.IsNullOrWhiteSpace(datItem.GetName()))
|
||||||
|
missingFields.Add(DatItemField.Name);
|
||||||
|
|
||||||
|
return missingFields;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc/>
|
||||||
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
logger.User($"Writing to '{outfile}'...");
|
||||||
|
|
||||||
|
var metadataFile = CreateMetadataFile(ignoreblanks);
|
||||||
|
if (!Serialization.AttractMode.SerializeToFile(metadataFile, outfile))
|
||||||
|
{
|
||||||
|
logger.Warning($"File '{outfile}' could not be written! See the log for more details.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) when (!throwOnError)
|
||||||
|
{
|
||||||
|
logger.Error(ex);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Converters
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a MetadataFile from the current internal information
|
||||||
|
/// <summary>
|
||||||
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
|
||||||
|
private Models.AttractMode.MetadataFile CreateMetadataFile(bool ignoreblanks)
|
||||||
|
{
|
||||||
|
var metadataFile = new Models.AttractMode.MetadataFile
|
||||||
|
{
|
||||||
|
Row = CreateRows(ignoreblanks)
|
||||||
|
};
|
||||||
|
return metadataFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create an array of Row from the current internal information
|
||||||
|
/// <summary>
|
||||||
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise</param>
|
||||||
|
private Models.AttractMode.Row[]? CreateRows(bool ignoreblanks)
|
||||||
|
{
|
||||||
|
// If we don't have items, we can't do anything
|
||||||
|
if (this.Items == null || !this.Items.Any())
|
||||||
|
return null;
|
||||||
|
|
||||||
|
// Create a list of hold the rows
|
||||||
|
var rows = new List<Models.AttractMode.Row>();
|
||||||
|
|
||||||
|
// Loop through the sorted items and create games for them
|
||||||
|
foreach (string key in Items.SortedKeys)
|
||||||
|
{
|
||||||
|
var items = Items.FilteredItems(key);
|
||||||
|
if (items == null || !items.Any())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
// Loop through and convert the items to respective lists
|
||||||
|
foreach (var item in items)
|
||||||
|
{
|
||||||
|
// Skip if we're ignoring the item
|
||||||
|
if (ShouldIgnore(item, ignoreblanks))
|
||||||
|
continue;
|
||||||
|
|
||||||
|
switch (item)
|
||||||
|
{
|
||||||
|
case Rom rom:
|
||||||
|
rows.Add(CreateRow(rom));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Populate the games
|
||||||
|
|
||||||
|
return rows.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Create a Row from the current Rom DatItem
|
||||||
|
/// <summary>
|
||||||
|
private Models.AttractMode.Row CreateRow(Rom rom)
|
||||||
|
{
|
||||||
|
var row = new Models.AttractMode.Row
|
||||||
|
{
|
||||||
|
Name = rom.Machine.Name,
|
||||||
|
Title = rom.Machine.Description,
|
||||||
|
Emulator = Header.FileName,
|
||||||
|
CloneOf = rom.Machine.CloneOf,
|
||||||
|
Year = rom.Machine.Year,
|
||||||
|
Manufacturer = rom.Machine.Manufacturer,
|
||||||
|
Category = rom.Machine.Category,
|
||||||
|
Players = rom.Machine.Players,
|
||||||
|
Rotation = rom.Machine.Rotation,
|
||||||
|
Control = rom.Machine.Control,
|
||||||
|
Status = rom.Machine.Status,
|
||||||
|
DisplayCount = rom.Machine.DisplayCount,
|
||||||
|
DisplayType = rom.Machine.DisplayType,
|
||||||
|
AltRomname = rom.AltName,
|
||||||
|
AltTitle = rom.AltTitle,
|
||||||
|
Extra = rom.Machine.Comment,
|
||||||
|
Buttons = rom.Machine.Buttons,
|
||||||
|
// TODO: Add extended fields
|
||||||
|
};
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,20 +1,9 @@
|
|||||||
using System;
|
namespace SabreTools.DatFiles.Formats
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
using SabreTools.Core;
|
|
||||||
using SabreTools.DatItems;
|
|
||||||
using SabreTools.DatItems.Formats;
|
|
||||||
using SabreTools.IO;
|
|
||||||
using SabreTools.IO.Readers;
|
|
||||||
using SabreTools.IO.Writers;
|
|
||||||
|
|
||||||
namespace SabreTools.DatFiles.Formats
|
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Represents parsing and writing of an AttractMode DAT
|
/// Represents an AttractMode DAT
|
||||||
/// </summary>
|
/// </summary>
|
||||||
internal class AttractMode : DatFile
|
internal partial class AttractMode : DatFile
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Constructor designed for casting a base DatFile
|
/// Constructor designed for casting a base DatFile
|
||||||
@@ -24,241 +13,5 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
: base(datFile)
|
: base(datFile)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
|
|
||||||
{
|
|
||||||
// Open a file reader
|
|
||||||
Encoding enc = filename.GetEncoding();
|
|
||||||
SeparatedValueReader svr = new(System.IO.File.OpenRead(filename), enc)
|
|
||||||
{
|
|
||||||
Header = true,
|
|
||||||
Quotes = false,
|
|
||||||
Separator = ';',
|
|
||||||
VerifyFieldCount = true
|
|
||||||
};
|
|
||||||
|
|
||||||
// If we're somehow at the end of the stream already, we can't do anything
|
|
||||||
if (svr.EndOfStream)
|
|
||||||
return;
|
|
||||||
|
|
||||||
// Read in the header
|
|
||||||
svr.ReadHeader();
|
|
||||||
|
|
||||||
// Header values should match
|
|
||||||
// #Name;Title;Emulator;CloneOf;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons
|
|
||||||
|
|
||||||
// Loop through all of the data lines
|
|
||||||
while (!svr.EndOfStream)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// Get the current line, split and parse
|
|
||||||
svr.ReadNextLine();
|
|
||||||
|
|
||||||
Rom rom = new()
|
|
||||||
{
|
|
||||||
Name = "-",
|
|
||||||
Size = Constants.SizeZero,
|
|
||||||
CRC = Constants.CRCZero,
|
|
||||||
MD5 = Constants.MD5Zero,
|
|
||||||
SHA1 = Constants.SHA1Zero,
|
|
||||||
ItemStatus = ItemStatus.None,
|
|
||||||
|
|
||||||
Machine = new Machine
|
|
||||||
{
|
|
||||||
Name = svr.Line[0], // #Name
|
|
||||||
Description = svr.Line[1], // Title
|
|
||||||
CloneOf = svr.Line[3], // CloneOf
|
|
||||||
Year = svr.Line[4], // Year
|
|
||||||
Manufacturer = svr.Line[5], // Manufacturer
|
|
||||||
Category = svr.Line[6], // Category
|
|
||||||
Players = svr.Line[7], // Players
|
|
||||||
Rotation = svr.Line[8], // Rotation
|
|
||||||
Control = svr.Line[9], // Control
|
|
||||||
Status = svr.Line[10], // Status
|
|
||||||
DisplayCount = svr.Line[11], // DisplayCount
|
|
||||||
DisplayType = svr.Line[12], // DisplayType
|
|
||||||
Comment = svr.Line[15], // Extra
|
|
||||||
Buttons = svr.Line[16], // Buttons
|
|
||||||
},
|
|
||||||
|
|
||||||
AltName = svr.Line[13], // AltRomname
|
|
||||||
AltTitle = svr.Line[14], // AltTitle
|
|
||||||
|
|
||||||
Source = new Source
|
|
||||||
{
|
|
||||||
Index = indexId,
|
|
||||||
Name = filename,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
// Now process and add the rom
|
|
||||||
ParseAddHelper(rom, statsOnly);
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (!throwOnError)
|
|
||||||
{
|
|
||||||
string message = $"'{filename}' - There was an error parsing line {svr.LineNumber} '{svr.CurrentLine}'";
|
|
||||||
logger.Error(ex, message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
svr.Dispose();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
protected override ItemType[] GetSupportedTypes()
|
|
||||||
{
|
|
||||||
return new ItemType[] { ItemType.Rom };
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
protected override List<DatItemField> GetMissingRequiredFields(DatItem datItem)
|
|
||||||
{
|
|
||||||
// TODO: Check required fields
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc/>
|
|
||||||
public override bool WriteToFile(string outfile, bool ignoreblanks = false, bool throwOnError = false)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
logger.User($"Writing to '{outfile}'...");
|
|
||||||
FileStream fs = System.IO.File.Create(outfile);
|
|
||||||
|
|
||||||
// If we get back null for some reason, just log and return
|
|
||||||
if (fs == null)
|
|
||||||
{
|
|
||||||
logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
SeparatedValueWriter svw = new(fs, new UTF8Encoding(false))
|
|
||||||
{
|
|
||||||
Quotes = false,
|
|
||||||
Separator = ';',
|
|
||||||
VerifyFieldCount = true
|
|
||||||
};
|
|
||||||
|
|
||||||
// Write out the header
|
|
||||||
WriteHeader(svw);
|
|
||||||
|
|
||||||
// Use a sorted list of games to output
|
|
||||||
foreach (string key in Items.SortedKeys)
|
|
||||||
{
|
|
||||||
ConcurrentList<DatItem> datItems = Items.FilteredItems(key);
|
|
||||||
|
|
||||||
// If this machine doesn't contain any writable items, skip
|
|
||||||
if (!ContainsWritable(datItems))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// Resolve the names in the block
|
|
||||||
datItems = DatItem.ResolveNames(datItems);
|
|
||||||
|
|
||||||
for (int index = 0; index < datItems.Count; index++)
|
|
||||||
{
|
|
||||||
DatItem datItem = datItems[index];
|
|
||||||
|
|
||||||
// Check for a "null" item
|
|
||||||
datItem = ProcessNullifiedItem(datItem);
|
|
||||||
|
|
||||||
// Write out the item if we're not ignoring
|
|
||||||
if (!ShouldIgnore(datItem, ignoreblanks))
|
|
||||||
WriteDatItem(svw, datItem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.User($"'{outfile}' written!{Environment.NewLine}");
|
|
||||||
svw.Dispose();
|
|
||||||
fs.Dispose();
|
|
||||||
}
|
|
||||||
catch (Exception ex) when (!throwOnError)
|
|
||||||
{
|
|
||||||
logger.Error(ex);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write out DAT header using the supplied StreamWriter
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="svw">SeparatedValueWriter to output to</param>
|
|
||||||
private void WriteHeader(SeparatedValueWriter svw)
|
|
||||||
{
|
|
||||||
string[] headers = new string[]
|
|
||||||
{
|
|
||||||
"#Name",
|
|
||||||
"Title",
|
|
||||||
"Emulator",
|
|
||||||
"CloneOf",
|
|
||||||
"Year",
|
|
||||||
"Manufacturer",
|
|
||||||
"Category",
|
|
||||||
"Players",
|
|
||||||
"Rotation",
|
|
||||||
"Control",
|
|
||||||
"Status",
|
|
||||||
"DisplayCount",
|
|
||||||
"DisplayType",
|
|
||||||
"AltRomname",
|
|
||||||
"AltTitle",
|
|
||||||
"Extra",
|
|
||||||
"Buttons",
|
|
||||||
};
|
|
||||||
|
|
||||||
svw.WriteHeader(headers);
|
|
||||||
|
|
||||||
svw.Flush();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Write out Game start using the supplied StreamWriter
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="svw">SeparatedValueWriter to output to</param>
|
|
||||||
/// <param name="datItem">DatItem object to be output</param>
|
|
||||||
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
|
|
||||||
private void WriteDatItem(SeparatedValueWriter svw, DatItem datItem)
|
|
||||||
{
|
|
||||||
// No game should start with a path separator
|
|
||||||
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
|
|
||||||
|
|
||||||
// Pre-process the item name
|
|
||||||
ProcessItemName(datItem, true);
|
|
||||||
|
|
||||||
// Build the state
|
|
||||||
switch (datItem.ItemType)
|
|
||||||
{
|
|
||||||
case ItemType.Rom:
|
|
||||||
var rom = datItem as Rom;
|
|
||||||
string[] fields = new string[]
|
|
||||||
{
|
|
||||||
rom.Machine.Name,
|
|
||||||
rom.Machine.Description,
|
|
||||||
Header.FileName,
|
|
||||||
rom.Machine.CloneOf,
|
|
||||||
rom.Machine.Year,
|
|
||||||
rom.Machine.Manufacturer,
|
|
||||||
rom.Machine.Category,
|
|
||||||
rom.Machine.Players,
|
|
||||||
rom.Machine.Rotation,
|
|
||||||
rom.Machine.Control,
|
|
||||||
rom.ItemStatus.ToString(),
|
|
||||||
rom.Machine.DisplayCount,
|
|
||||||
rom.Machine.DisplayType,
|
|
||||||
rom.AltName,
|
|
||||||
rom.AltTitle,
|
|
||||||
rom.Machine.Comment,
|
|
||||||
rom.Machine.Buttons,
|
|
||||||
};
|
|
||||||
|
|
||||||
svw.WriteValues(fields);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
svw.Flush();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ namespace SabreTools.DatFiles.Formats
|
|||||||
string message = $"'{filename}' - An error occurred during parsing";
|
string message = $"'{filename}' - An error occurred during parsing";
|
||||||
logger.Error(ex, message);
|
logger.Error(ex, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Converters
|
#region Converters
|
||||||
|
|||||||
@@ -5,47 +5,47 @@ namespace SabreTools.Models.AttractMode
|
|||||||
/// <remarks>Also called Romname</remarks>
|
/// <remarks>Also called Romname</remarks>
|
||||||
public string Name { get; set; }
|
public string Name { get; set; }
|
||||||
|
|
||||||
public string Title { get; set; }
|
public string? Title { get; set; }
|
||||||
|
|
||||||
public string Emulator { get; set; }
|
public string? Emulator { get; set; }
|
||||||
|
|
||||||
public string CloneOf { get; set; }
|
public string? CloneOf { get; set; }
|
||||||
|
|
||||||
public string Year { get; set; }
|
public string? Year { get; set; }
|
||||||
|
|
||||||
public string Manufacturer { get; set; }
|
public string? Manufacturer { get; set; }
|
||||||
|
|
||||||
public string Category { get; set; }
|
public string? Category { get; set; }
|
||||||
|
|
||||||
public string Players { get; set; }
|
public string? Players { get; set; }
|
||||||
|
|
||||||
public string Rotation { get; set; }
|
public string? Rotation { get; set; }
|
||||||
|
|
||||||
public string Control { get; set; }
|
public string? Control { get; set; }
|
||||||
|
|
||||||
public string Status { get; set; }
|
public string? Status { get; set; }
|
||||||
|
|
||||||
public string DisplayCount { get; set; }
|
public string? DisplayCount { get; set; }
|
||||||
|
|
||||||
public string DisplayType { get; set; }
|
public string? DisplayType { get; set; }
|
||||||
|
|
||||||
public string AltRomname { get; set; }
|
public string? AltRomname { get; set; }
|
||||||
|
|
||||||
public string AltTitle { get; set; }
|
public string? AltTitle { get; set; }
|
||||||
|
|
||||||
public string Extra { get; set; }
|
public string? Extra { get; set; }
|
||||||
|
|
||||||
public string Buttons { get; set; }
|
public string? Buttons { get; set; }
|
||||||
|
|
||||||
public string Favorite { get; set; }
|
public string? Favorite { get; set; }
|
||||||
|
|
||||||
public string Tags { get; set; }
|
public string? Tags { get; set; }
|
||||||
|
|
||||||
public string PlayedCount { get; set; }
|
public string? PlayedCount { get; set; }
|
||||||
|
|
||||||
public string PlayedTime { get; set; }
|
public string? PlayedTime { get; set; }
|
||||||
|
|
||||||
public string FileIsAvailable { get; set; }
|
public string? FileIsAvailable { get; set; }
|
||||||
|
|
||||||
#region DO NOT USE IN PRODUCTION
|
#region DO NOT USE IN PRODUCTION
|
||||||
|
|
||||||
|
|||||||
140
SabreTools.Serialization/AttractMode.Deserializer.cs
Normal file
140
SabreTools.Serialization/AttractMode.Deserializer.cs
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using SabreTools.IO.Readers;
|
||||||
|
using SabreTools.Models.AttractMode;
|
||||||
|
|
||||||
|
namespace SabreTools.Serialization
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Separated value deserializer for AttractMode romlists
|
||||||
|
/// </summary>
|
||||||
|
public partial class AttractMode
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Deserializes an AttractMode romlist to the defined type
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path">Path to the file to deserialize</param>
|
||||||
|
/// <returns>Deserialized data on success, null on failure</returns>
|
||||||
|
public static MetadataFile? Deserialize(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using var stream = PathProcessor.OpenStream(path);
|
||||||
|
return Deserialize(stream);
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// TODO: Handle logging the exception
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deserializes an AttractMode romlist in a stream to the defined type
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">Stream to deserialize</param>
|
||||||
|
/// <returns>Deserialized data on success, null on failure</returns>
|
||||||
|
public static MetadataFile? Deserialize(Stream? stream)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// If the stream is null
|
||||||
|
if (stream == null)
|
||||||
|
return default;
|
||||||
|
|
||||||
|
// Setup the reader and output
|
||||||
|
var reader = new SeparatedValueReader(stream, Encoding.UTF8)
|
||||||
|
{
|
||||||
|
Separator = ';',
|
||||||
|
VerifyFieldCount = false,
|
||||||
|
};
|
||||||
|
var dat = new MetadataFile();
|
||||||
|
|
||||||
|
// Read the header values first
|
||||||
|
if (!reader.ReadHeader())
|
||||||
|
return null;
|
||||||
|
|
||||||
|
dat.Header = reader.HeaderValues.ToArray();
|
||||||
|
|
||||||
|
// Loop through the rows and parse out values
|
||||||
|
var rows = new List<Row>();
|
||||||
|
while (!reader.EndOfStream)
|
||||||
|
{
|
||||||
|
// If we have no next line
|
||||||
|
if (!reader.ReadNextLine())
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Parse the line into a row
|
||||||
|
Row? row = null;
|
||||||
|
if (reader.Line.Count < HeaderWithRomnameCount)
|
||||||
|
{
|
||||||
|
row = new Row
|
||||||
|
{
|
||||||
|
Name = reader.Line[0],
|
||||||
|
Title = reader.Line[1],
|
||||||
|
Emulator = reader.Line[2],
|
||||||
|
CloneOf = reader.Line[3],
|
||||||
|
Year = reader.Line[4],
|
||||||
|
Manufacturer = reader.Line[5],
|
||||||
|
Category = reader.Line[6],
|
||||||
|
Players = reader.Line[7],
|
||||||
|
Rotation = reader.Line[8],
|
||||||
|
Control = reader.Line[9],
|
||||||
|
Status = reader.Line[10],
|
||||||
|
DisplayCount = reader.Line[11],
|
||||||
|
DisplayType = reader.Line[12],
|
||||||
|
AltRomname = reader.Line[13],
|
||||||
|
AltTitle = reader.Line[14],
|
||||||
|
Extra = reader.Line[15],
|
||||||
|
Buttons = reader.Line[16],
|
||||||
|
};
|
||||||
|
|
||||||
|
// If we have additional fields
|
||||||
|
if (reader.Line.Count > HeaderWithoutRomnameCount)
|
||||||
|
row.ADDITIONAL_ELEMENTS = reader.Line.Skip(HeaderWithoutRomnameCount).ToArray();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
row = new Row
|
||||||
|
{
|
||||||
|
Name = reader.Line[0],
|
||||||
|
Title = reader.Line[1],
|
||||||
|
Emulator = reader.Line[2],
|
||||||
|
CloneOf = reader.Line[3],
|
||||||
|
Year = reader.Line[4],
|
||||||
|
Manufacturer = reader.Line[5],
|
||||||
|
Category = reader.Line[6],
|
||||||
|
Players = reader.Line[7],
|
||||||
|
Rotation = reader.Line[8],
|
||||||
|
Control = reader.Line[9],
|
||||||
|
Status = reader.Line[10],
|
||||||
|
DisplayCount = reader.Line[11],
|
||||||
|
DisplayType = reader.Line[12],
|
||||||
|
AltRomname = reader.Line[13],
|
||||||
|
AltTitle = reader.Line[14],
|
||||||
|
Extra = reader.Line[15],
|
||||||
|
Buttons = reader.Line[16],
|
||||||
|
};
|
||||||
|
|
||||||
|
// If we have additional fields
|
||||||
|
if (reader.Line.Count > HeaderWithRomnameCount)
|
||||||
|
row.ADDITIONAL_ELEMENTS = reader.Line.Skip(HeaderWithRomnameCount).ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
rows.Add(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign the rows to the Dat and return
|
||||||
|
dat.Row = rows.ToArray();
|
||||||
|
return dat;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
// TODO: Handle logging the exception
|
||||||
|
return default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
114
SabreTools.Serialization/AttractMode.Serializer.cs
Normal file
114
SabreTools.Serialization/AttractMode.Serializer.cs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
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)
|
||||||
|
{
|
||||||
|
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 = ';', Quotes = false };
|
||||||
|
|
||||||
|
// TODO: Include flag to write out long or short header
|
||||||
|
// Write the short header
|
||||||
|
writer.WriteString(HeaderWithoutRomname); // TODO: Convert to array of values
|
||||||
|
|
||||||
|
// 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 string[]
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,146 +1,14 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using SabreTools.IO.Readers;
|
|
||||||
using SabreTools.Models.AttractMode;
|
|
||||||
|
|
||||||
namespace SabreTools.Serialization
|
namespace SabreTools.Serialization
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Separated value serializer for AttractMode romlists
|
/// Separated value serializer/deserializer for AttractMode romlists
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class AttractMode
|
public partial class AttractMode
|
||||||
{
|
{
|
||||||
private const string HeaderWithoutRomname = "#Name;Title;Emulator;CloneOf;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons";
|
private const string HeaderWithoutRomname = "#Name;Title;Emulator;CloneOf;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons";
|
||||||
private const int HeaderWithoutRomnameCount = 17;
|
private const int HeaderWithoutRomnameCount = 17;
|
||||||
|
|
||||||
private const string HeaderWithRomname = "#Romname;Title;Emulator;Cloneof;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons;Favourite;Tags;PlayedCount;PlayedTime;FileIsAvailable";
|
private const string HeaderWithRomname = "#Romname;Title;Emulator;Cloneof;Year;Manufacturer;Category;Players;Rotation;Control;Status;DisplayCount;DisplayType;AltRomname;AltTitle;Extra;Buttons;Favourite;Tags;PlayedCount;PlayedTime;FileIsAvailable";
|
||||||
private const int HeaderWithRomnameCount = 22;
|
private const int HeaderWithRomnameCount = 22;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deserializes an AttractMode romlist to the defined type
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="path">Path to the file to deserialize</param>
|
|
||||||
/// <returns>Deserialized data on success, null on failure</returns>
|
|
||||||
public static MetadataFile? Deserialize(string path)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
using var stream = PathProcessor.OpenStream(path);
|
|
||||||
return Deserialize(stream);
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// TODO: Handle logging the exception
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Deserializes an AttractMode romlist in a stream to the defined type
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="stream">Stream to deserialize</param>
|
|
||||||
/// <returns>Deserialized data on success, null on failure</returns>
|
|
||||||
public static MetadataFile? Deserialize(Stream? stream)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// If the stream is null
|
|
||||||
if (stream == null)
|
|
||||||
return default;
|
|
||||||
|
|
||||||
// Setup the reader and output
|
|
||||||
var reader = new SeparatedValueReader(stream, Encoding.UTF8)
|
|
||||||
{
|
|
||||||
Separator = ';',
|
|
||||||
VerifyFieldCount = false,
|
|
||||||
};
|
|
||||||
var dat = new MetadataFile();
|
|
||||||
|
|
||||||
// Read the header values first
|
|
||||||
if (!reader.ReadHeader())
|
|
||||||
return null;
|
|
||||||
|
|
||||||
dat.Header = reader.HeaderValues.ToArray();
|
|
||||||
|
|
||||||
// Loop through the rows and parse out values
|
|
||||||
var rows = new List<Row>();
|
|
||||||
while (!reader.EndOfStream)
|
|
||||||
{
|
|
||||||
// If we have no next line
|
|
||||||
if (!reader.ReadNextLine())
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Parse the line into a row
|
|
||||||
Row? row = null;
|
|
||||||
if (reader.Line.Count < HeaderWithRomnameCount)
|
|
||||||
{
|
|
||||||
row = new Row
|
|
||||||
{
|
|
||||||
Name = reader.Line[0],
|
|
||||||
Title = reader.Line[1],
|
|
||||||
Emulator = reader.Line[2],
|
|
||||||
CloneOf = reader.Line[3],
|
|
||||||
Year = reader.Line[4],
|
|
||||||
Manufacturer = reader.Line[5],
|
|
||||||
Category = reader.Line[6],
|
|
||||||
Players = reader.Line[7],
|
|
||||||
Rotation = reader.Line[8],
|
|
||||||
Control = reader.Line[9],
|
|
||||||
Status = reader.Line[10],
|
|
||||||
DisplayCount = reader.Line[11],
|
|
||||||
DisplayType = reader.Line[12],
|
|
||||||
AltRomname = reader.Line[13],
|
|
||||||
AltTitle = reader.Line[14],
|
|
||||||
Extra = reader.Line[15],
|
|
||||||
Buttons = reader.Line[16],
|
|
||||||
};
|
|
||||||
|
|
||||||
// If we have additional fields
|
|
||||||
if (reader.Line.Count > HeaderWithoutRomnameCount)
|
|
||||||
row.ADDITIONAL_ELEMENTS = reader.Line.Skip(HeaderWithoutRomnameCount).ToArray();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
row = new Row
|
|
||||||
{
|
|
||||||
Name = reader.Line[0],
|
|
||||||
Title = reader.Line[1],
|
|
||||||
Emulator = reader.Line[2],
|
|
||||||
CloneOf = reader.Line[3],
|
|
||||||
Year = reader.Line[4],
|
|
||||||
Manufacturer = reader.Line[5],
|
|
||||||
Category = reader.Line[6],
|
|
||||||
Players = reader.Line[7],
|
|
||||||
Rotation = reader.Line[8],
|
|
||||||
Control = reader.Line[9],
|
|
||||||
Status = reader.Line[10],
|
|
||||||
DisplayCount = reader.Line[11],
|
|
||||||
DisplayType = reader.Line[12],
|
|
||||||
AltRomname = reader.Line[13],
|
|
||||||
AltTitle = reader.Line[14],
|
|
||||||
Extra = reader.Line[15],
|
|
||||||
Buttons = reader.Line[16],
|
|
||||||
};
|
|
||||||
|
|
||||||
// If we have additional fields
|
|
||||||
if (reader.Line.Count > HeaderWithRomnameCount)
|
|
||||||
row.ADDITIONAL_ELEMENTS = reader.Line.Skip(HeaderWithRomnameCount).ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
rows.Add(row);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign the rows to the Dat and return
|
|
||||||
dat.Row = rows.ToArray();
|
|
||||||
return dat;
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
// TODO: Handle logging the exception
|
|
||||||
return default;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user