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.DosCenter;
|
2026-03-24 19:17:25 -04:00
|
|
|
using SabreTools.Text.ClrMamePro;
|
2023-09-08 23:32:42 -04:00
|
|
|
|
2025-09-26 14:57:20 -04:00
|
|
|
namespace SabreTools.Serialization.Readers
|
2023-09-08 23:32:42 -04:00
|
|
|
{
|
2025-09-26 15:02:43 -04:00
|
|
|
public class DosCenter : BaseBinaryReader<MetadataFile>
|
2023-09-08 23:32:42 -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) { DosCenter = true };
|
2024-11-28 22:57:12 -05:00
|
|
|
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
|
|
|
|
|
string? lastTopLevel = reader.TopLevel;
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
Game? game = null;
|
|
|
|
|
var games = new List<Game>();
|
2025-09-26 13:06:18 -04:00
|
|
|
var files = new List<Data.Models.DosCenter.File>();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
case CmpRowType.None:
|
|
|
|
|
case CmpRowType.Comment:
|
|
|
|
|
continue;
|
|
|
|
|
case CmpRowType.EndTopLevel:
|
|
|
|
|
switch (lastTopLevel)
|
|
|
|
|
{
|
|
|
|
|
case "game":
|
2026-01-25 14:32:49 -05:00
|
|
|
if (game is not null)
|
2024-11-28 22:57:12 -05:00
|
|
|
{
|
|
|
|
|
game.File = [.. files];
|
|
|
|
|
games.Add(game);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
game = null;
|
|
|
|
|
files.Clear();
|
|
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// TODO: Log invalid values
|
|
|
|
|
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
|
|
|
|
|
|
|
|
case CmpRowType.TopLevel:
|
|
|
|
|
case CmpRowType.Standalone:
|
|
|
|
|
case CmpRowType.Internal:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// TODO: Log invalid values
|
|
|
|
|
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 at the root
|
|
|
|
|
if (reader.RowType == CmpRowType.TopLevel)
|
|
|
|
|
{
|
|
|
|
|
lastTopLevel = reader.TopLevel;
|
|
|
|
|
switch (reader.TopLevel)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
case "doscenter":
|
2025-09-26 13:06:18 -04:00
|
|
|
dat.DosCenter = new Data.Models.DosCenter.DosCenter();
|
2024-11-28 22:57:12 -05:00
|
|
|
break;
|
2024-04-03 20:55:02 -04:00
|
|
|
case "game":
|
2024-11-28 22:57:12 -05:00
|
|
|
game = new Game();
|
2024-04-03 20:55:02 -04:00
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
// TODO: Log invalid values
|
|
|
|
|
break;
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
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 the doscenter block
|
|
|
|
|
else if (reader.TopLevel == "doscenter" && reader.RowType == CmpRowType.Standalone)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create the block if we haven't already
|
2025-09-26 13:06:18 -04:00
|
|
|
dat.DosCenter ??= new Data.Models.DosCenter.DosCenter();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
switch (reader.Standalone?.Key?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "name:":
|
|
|
|
|
dat.DosCenter.Name = reader.Standalone?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "description:":
|
|
|
|
|
dat.DosCenter.Description = reader.Standalone?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "version:":
|
|
|
|
|
dat.DosCenter.Version = reader.Standalone?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "date:":
|
|
|
|
|
dat.DosCenter.Date = reader.Standalone?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "author:":
|
|
|
|
|
dat.DosCenter.Author = reader.Standalone?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "homepage:":
|
|
|
|
|
dat.DosCenter.Homepage = reader.Standalone?.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "comment:":
|
|
|
|
|
dat.DosCenter.Comment = reader.Standalone?.Value;
|
|
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
// TODO: Log invalid values
|
|
|
|
|
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 a game block
|
|
|
|
|
else if (reader.TopLevel == "game" && reader.RowType == CmpRowType.Standalone)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// Create the block if we haven't already
|
|
|
|
|
game ??= new Game();
|
2024-04-03 20:55:02 -04:00
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
switch (reader.Standalone?.Key?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "name":
|
|
|
|
|
game.Name = reader.Standalone?.Value;
|
|
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
// TODO: Log invalid values
|
|
|
|
|
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 a file block
|
|
|
|
|
else if (reader.TopLevel == "game" && reader.RowType == CmpRowType.Internal)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
// If we have an unknown type, log it
|
|
|
|
|
if (reader.InternalName != "file")
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// Create the file and add to the list
|
|
|
|
|
var file = CreateFile(reader);
|
2026-01-25 14:32:49 -05:00
|
|
|
if (file is not null)
|
2024-11-28 22:57:12 -05:00
|
|
|
files.Add(file);
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
// Add extra pieces and return
|
|
|
|
|
if (games.Count > 0)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2024-11-28 22:57:12 -05:00
|
|
|
dat.Game = [.. games];
|
|
|
|
|
return dat;
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
2024-11-28 22:57:12 -05:00
|
|
|
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
|
|
|
}
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create a File object from the current reader context
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="reader">ClrMameProReader representing the metadata file</param>
|
|
|
|
|
/// <returns>File object created from the reader context</returns>
|
2026-03-24 19:17:25 -04:00
|
|
|
private static Data.Models.DosCenter.File? CreateFile(Reader reader)
|
2024-04-03 20:55:02 -04:00
|
|
|
{
|
2026-01-25 14:30:18 -05:00
|
|
|
if (reader.Internal is null)
|
2024-04-03 20:55:02 -04:00
|
|
|
return null;
|
|
|
|
|
|
2025-09-26 13:06:18 -04:00
|
|
|
var file = new Data.Models.DosCenter.File();
|
2024-04-03 20:55:02 -04:00
|
|
|
foreach (var kvp in reader.Internal)
|
|
|
|
|
{
|
|
|
|
|
switch (kvp.Key?.ToLowerInvariant())
|
|
|
|
|
{
|
|
|
|
|
case "name":
|
|
|
|
|
file.Name = kvp.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "size":
|
|
|
|
|
file.Size = kvp.Value;
|
|
|
|
|
break;
|
|
|
|
|
case "crc":
|
|
|
|
|
file.CRC = kvp.Value;
|
|
|
|
|
break;
|
2024-12-10 15:41:12 -05:00
|
|
|
case "sha1":
|
|
|
|
|
file.SHA1 = kvp.Value;
|
|
|
|
|
break;
|
2024-04-03 20:55:02 -04:00
|
|
|
case "date":
|
|
|
|
|
file.Date = kvp.Value;
|
|
|
|
|
break;
|
2026-01-25 16:15:05 -05:00
|
|
|
default:
|
|
|
|
|
// TODO: Log invalid values
|
|
|
|
|
break;
|
2024-04-03 20:55:02 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return file;
|
2023-09-08 23:32:42 -04:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-24 09:31:28 -04:00
|
|
|
}
|