2024-04-03 20:55:02 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
2025-09-26 13:06:18 -04:00
|
|
|
using SabreTools.Data.Models.RomCenter;
|
2026-04-02 23:45:17 -04:00
|
|
|
using SabreTools.Text.Extensions;
|
2026-03-24 19:17:25 -04:00
|
|
|
using SabreTools.Text.INI;
|
2023-09-09 01:26:45 -04:00
|
|
|
|
2026-01-27 12:03:01 -05:00
|
|
|
#pragma warning disable CA1847 // Use char literal for a single character lookup
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2023-09-09 01:26:45 -04:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class RomCenter : BaseBinaryReader<MetadataFile>
|
2023-09-09 01:26:45 -04:00
|
|
|
{
|
2024-04-03 20:55:02 -04:00
|
|
|
/// <inheritdoc/>
|
2024-04-04 01:55:05 -04:00
|
|
|
public override MetadataFile? Deserialize(Stream? data)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2025-09-22 20:07:18 -04:00
|
|
|
// If the data is invalid
|
2026-01-25 14:30:18 -05:00
|
|
|
if (data is null || !data.CanRead)
|
2024-11-28 22:57:12 -05:00
|
|
|
return null;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-27 12:43:52 -05:00
|
|
|
try
|
|
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Setup the reader and output
|
2026-03-24 19:17:25 -04:00
|
|
|
var reader = new Reader(data, Encoding.UTF8)
|
2024-11-28 22:57:12 -05:00
|
|
|
{
|
|
|
|
|
ValidateRows = false,
|
|
|
|
|
};
|
|
|
|
|
var dat = new MetadataFile();
|
2024-11-27 12:43:52 -05:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Loop through and parse out the values
|
|
|
|
|
var roms = new List<Rom>();
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
|
{
|
|
|
|
|
// If we have no next line
|
|
|
|
|
if (!reader.ReadNextLine())
|
|
|
|
|
break;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Ignore certain row types
|
|
|
|
|
switch (reader.RowType)
|
|
|
|
|
{
|
2026-03-24 19:17:25 -04:00
|
|
|
case RowType.None:
|
|
|
|
|
case RowType.Comment:
|
2024-11-28 22:57:12 -05:00
|
|
|
continue;
|
2026-01-25 16:15:05 -05:00
|
|
|
|
2026-03-24 19:17:25 -04:00
|
|
|
case RowType.SectionHeader:
|
2024-11-28 22:57:12 -05:00
|
|
|
switch (reader.Section?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "credits":
|
|
|
|
|
dat.Credits ??= new Credits();
|
|
|
|
|
break;
|
|
|
|
|
case "dat":
|
|
|
|
|
dat.Dat ??= new Dat();
|
|
|
|
|
break;
|
|
|
|
|
case "emulator":
|
|
|
|
|
dat.Emulator ??= new Emulator();
|
|
|
|
|
break;
|
|
|
|
|
case "games":
|
|
|
|
|
dat.Games ??= new Games();
|
|
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
}
|
2026-01-25 16:15:05 -05:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
continue;
|
2026-01-25 16:15:05 -05:00
|
|
|
|
2026-03-24 19:17:25 -04:00
|
|
|
case RowType.KeyValue:
|
|
|
|
|
case RowType.Invalid:
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If we're in credits
|
|
|
|
|
if (reader.Section?.ToLowerInvariant() == "credits")
|
|
|
|
|
{
|
|
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Credits ??= new Credits();
|
|
|
|
|
|
|
|
|
|
switch (reader.KeyValuePair?.Key?.ToLowerInvariant())
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
case "author":
|
|
|
|
|
dat.Credits.Author = reader.KeyValuePair?.Value;
|
2024-04-03 20:55:02 -04:00
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
case "version":
|
|
|
|
|
dat.Credits.Version = reader.KeyValuePair?.Value;
|
2024-04-03 20:55:02 -04:00
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
case "email":
|
|
|
|
|
dat.Credits.Email = reader.KeyValuePair?.Value;
|
2024-04-03 20:55:02 -04:00
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
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;
|
2024-04-03 20:55:02 -04:00
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
break;
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If we're in dat
|
|
|
|
|
else if (reader.Section?.ToLowerInvariant() == "dat")
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Dat ??= new Dat();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05: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;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If we're in emulator
|
|
|
|
|
else if (reader.Section?.ToLowerInvariant() == "emulator")
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Emulator ??= new Emulator();
|
|
|
|
|
|
|
|
|
|
switch (reader.KeyValuePair?.Key?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "refname":
|
|
|
|
|
dat.Emulator.RefName = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "version":
|
|
|
|
|
dat.Emulator.Version = reader.KeyValuePair?.Value;
|
|
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
break;
|
2024-11-28 22:57:12 -05:00
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If we're in games
|
|
|
|
|
else if (reader.Section?.ToLowerInvariant() == "games")
|
|
|
|
|
{
|
|
|
|
|
// Create the section if we haven't already
|
|
|
|
|
dat.Games ??= new Games();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// If the line doesn't contain the delimiter
|
2024-11-29 22:20:46 -05:00
|
|
|
if (!(reader.CurrentLine?.Contains("¬") ?? false))
|
2024-11-28 22:57:12 -05:00
|
|
|
continue;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Otherwise, separate out the line
|
|
|
|
|
string[] splitLine = reader.CurrentLine.Split('¬');
|
|
|
|
|
var rom = new Rom
|
|
|
|
|
{
|
|
|
|
|
// EMPTY = splitLine[0]
|
|
|
|
|
ParentName = splitLine[1],
|
|
|
|
|
ParentDescription = splitLine[2],
|
|
|
|
|
GameName = splitLine[3],
|
|
|
|
|
GameDescription = splitLine[4],
|
|
|
|
|
RomName = splitLine[5],
|
|
|
|
|
RomCRC = splitLine[6],
|
2026-04-02 23:45:17 -04:00
|
|
|
RomSize = NumberHelper.ConvertToInt64(splitLine[7]),
|
2024-11-28 22:57:12 -05:00
|
|
|
RomOf = splitLine[8],
|
|
|
|
|
MergeName = splitLine[9],
|
|
|
|
|
// EMPTY = splitLine[10]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
roms.Add(rom);
|
|
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Add extra pieces and return
|
2026-01-25 14:32:49 -05:00
|
|
|
if (dat.Games is not null && roms.Count > 0)
|
2024-11-28 22:57:12 -05:00
|
|
|
{
|
|
|
|
|
dat.Games.Rom = [.. roms];
|
|
|
|
|
return dat;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
catch
|
2024-11-27 14:04:20 -05:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Ignore the actual error
|
|
|
|
|
return null;
|
2024-11-27 14:04:20 -05:00
|
|
|
}
|
2023-09-09 01:26:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|