2023-07-13 21:27:45 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using SabreTools.IO.Readers;
|
|
|
|
|
using SabreTools.Models.RomCenter;
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Serialization
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for RomCenter INI files
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class RomCenter
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes a RomCenter INI file 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)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
using var stream = PathProcessor.OpenStream(path);
|
|
|
|
|
return Deserialize(stream);
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Deserializes a RomCenter INI file 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)
|
|
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
// If the stream is null
|
|
|
|
|
if (stream == null)
|
|
|
|
|
return default;
|
|
|
|
|
|
|
|
|
|
// Setup the reader and output
|
|
|
|
|
var reader = new IniReader(stream, Encoding.UTF8)
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
ValidateRows = false,
|
|
|
|
|
};
|
|
|
|
|
var dat = new MetadataFile();
|
|
|
|
|
|
|
|
|
|
// Loop through and parse out the values
|
|
|
|
|
var roms = new List<Rom>();
|
|
|
|
|
var additional = new List<string>();
|
|
|
|
|
var creditsAdditional = new List<string>();
|
|
|
|
|
var datAdditional = new List<string>();
|
|
|
|
|
var emulatorAdditional = new List<string>();
|
|
|
|
|
var gamesAdditional = new List<string>();
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
// If we have no next line
|
|
|
|
|
if (!reader.ReadNextLine())
|
|
|
|
|
break;
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Ignore certain row types
|
|
|
|
|
switch (reader.RowType)
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
case IniRowType.None:
|
|
|
|
|
case IniRowType.Comment:
|
|
|
|
|
continue;
|
|
|
|
|
case IniRowType.SectionHeader:
|
|
|
|
|
switch (reader.Section.ToLowerInvariant())
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
case "credits":
|
|
|
|
|
dat.Credits ??= new Credits();
|
2023-07-13 21:27:45 -04:00
|
|
|
break;
|
2023-07-30 09:00:15 -04:00
|
|
|
case "dat":
|
|
|
|
|
dat.Dat ??= new Dat();
|
2023-07-13 21:27:45 -04:00
|
|
|
break;
|
2023-07-30 09:00:15 -04:00
|
|
|
case "emulator":
|
|
|
|
|
dat.Emulator ??= new Emulator();
|
2023-07-13 21:27:45 -04:00
|
|
|
break;
|
2023-07-30 09:00:15 -04:00
|
|
|
case "games":
|
|
|
|
|
dat.Games ??= new Games();
|
2023-07-13 21:27:45 -04:00
|
|
|
break;
|
|
|
|
|
default:
|
2023-07-30 09:00:15 -04:00
|
|
|
additional.Add(reader.CurrentLine);
|
2023-07-13 21:27:45 -04:00
|
|
|
break;
|
|
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// If we're in credits
|
|
|
|
|
if (reader.Section.ToLowerInvariant() == "credits")
|
|
|
|
|
{
|
|
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Credits ??= new Credits();
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
switch (reader.KeyValuePair?.Key?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "author":
|
|
|
|
|
dat.Credits.Author = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "version":
|
|
|
|
|
dat.Credits.Version = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "email":
|
|
|
|
|
dat.Credits.Email = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "homepage":
|
|
|
|
|
dat.Credits.Homepage = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "url":
|
|
|
|
|
dat.Credits.Url = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "date":
|
|
|
|
|
dat.Credits.Date = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "comment":
|
|
|
|
|
dat.Credits.Comment = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
creditsAdditional.Add(reader.CurrentLine);
|
|
|
|
|
break;
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
}
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// If we're in dat
|
|
|
|
|
else if (reader.Section.ToLowerInvariant() == "dat")
|
|
|
|
|
{
|
|
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Dat ??= new Dat();
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
switch (reader.KeyValuePair?.Key?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "version":
|
|
|
|
|
dat.Dat.Version = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "plugin":
|
|
|
|
|
dat.Dat.Plugin = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "split":
|
|
|
|
|
dat.Dat.Split = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "merge":
|
|
|
|
|
dat.Dat.Merge = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
datAdditional.Add(reader.CurrentLine);
|
|
|
|
|
break;
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If we're in emulator
|
|
|
|
|
else if (reader.Section.ToLowerInvariant() == "emulator")
|
|
|
|
|
{
|
|
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Emulator ??= new Emulator();
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
switch (reader.KeyValuePair?.Key?.ToLowerInvariant())
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
case "refname":
|
|
|
|
|
dat.Emulator.RefName = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "version":
|
|
|
|
|
dat.Emulator.Version = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
emulatorAdditional.Add(reader.CurrentLine);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// If we're in games
|
|
|
|
|
else if (reader.Section.ToLowerInvariant() == "games")
|
|
|
|
|
{
|
|
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Games ??= new Games();
|
2023-07-13 21:27:45 -04:00
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// If the line doesn't contain the delimiter
|
|
|
|
|
if (!reader.CurrentLine.Contains('¬'))
|
|
|
|
|
{
|
|
|
|
|
gamesAdditional.Add(reader.CurrentLine);
|
|
|
|
|
continue;
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
// Otherwise, separate out the line
|
|
|
|
|
string[] splitLine = reader.CurrentLine.Split('¬');
|
|
|
|
|
var rom = new Rom
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
// EMPTY = splitLine[0]
|
|
|
|
|
ParentName = splitLine[1],
|
|
|
|
|
ParentDescription = splitLine[2],
|
|
|
|
|
GameName = splitLine[3],
|
|
|
|
|
GameDescription = splitLine[4],
|
|
|
|
|
RomName = splitLine[5],
|
|
|
|
|
RomCRC = splitLine[6],
|
|
|
|
|
RomSize = splitLine[7],
|
|
|
|
|
RomOf = splitLine[8],
|
|
|
|
|
MergeName = splitLine[9],
|
|
|
|
|
// EMPTY = splitLine[10]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (splitLine.Length > 11)
|
|
|
|
|
rom.ADDITIONAL_ELEMENTS = splitLine.Skip(11).ToArray();
|
|
|
|
|
|
|
|
|
|
roms.Add(rom);
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
|
|
|
|
|
2023-07-30 09:00:15 -04:00
|
|
|
else
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
additional.Add(item: reader.CurrentLine);
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
|
|
|
|
|
// Add extra pieces and return
|
|
|
|
|
dat.ADDITIONAL_ELEMENTS = additional.ToArray();
|
|
|
|
|
if (dat.Credits != null)
|
|
|
|
|
dat.Credits.ADDITIONAL_ELEMENTS = creditsAdditional.ToArray();
|
|
|
|
|
if (dat.Dat != null)
|
|
|
|
|
dat.Dat.ADDITIONAL_ELEMENTS = datAdditional.ToArray();
|
|
|
|
|
if (dat.Emulator != null)
|
|
|
|
|
dat.Emulator.ADDITIONAL_ELEMENTS = emulatorAdditional.ToArray();
|
|
|
|
|
if (dat.Games != null)
|
2023-07-13 21:27:45 -04:00
|
|
|
{
|
2023-07-30 09:00:15 -04:00
|
|
|
dat.Games.Rom = roms.ToArray();
|
|
|
|
|
dat.Games.ADDITIONAL_ELEMENTS = gamesAdditional.ToArray();
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
2023-07-30 09:00:15 -04:00
|
|
|
return dat;
|
2023-07-13 21:27:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|