2023-08-04 23:48:17 -04:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2023-08-04 22:16:00 -04:00
|
|
|
namespace SabreTools.Serialization
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Serializer for DosCenter models to internal structure
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class Internal
|
|
|
|
|
{
|
|
|
|
|
#region Serialize
|
|
|
|
|
|
2023-08-04 23:48:17 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.DosCenter.Game"/> to <cref="Models.Internal.Machine"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Models.Internal.Machine ConvertMachineFromDosCenter(Models.DosCenter.Game item)
|
|
|
|
|
{
|
|
|
|
|
var machine = new Models.Internal.Machine
|
|
|
|
|
{
|
|
|
|
|
[Models.Internal.Machine.NameKey] = item.Name,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (item.File != null && item.File.Any())
|
|
|
|
|
{
|
|
|
|
|
var roms = new List<Models.Internal.Rom>();
|
|
|
|
|
foreach (var file in item.File)
|
|
|
|
|
{
|
|
|
|
|
roms.Add(ConvertFromDosCenter(file));
|
|
|
|
|
}
|
|
|
|
|
machine[Models.Internal.Machine.RomKey] = roms.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return machine;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 22:16:00 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.DosCenter.File"/> to <cref="Models.Internal.Rom"/>
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static Models.Internal.Rom ConvertFromDosCenter(Models.DosCenter.File item)
|
|
|
|
|
{
|
|
|
|
|
var rom = new Models.Internal.Rom
|
|
|
|
|
{
|
|
|
|
|
[Models.Internal.Rom.NameKey] = item.Name,
|
|
|
|
|
[Models.Internal.Rom.SizeKey] = item.Size,
|
|
|
|
|
[Models.Internal.Rom.CRCKey] = item.CRC,
|
|
|
|
|
[Models.Internal.Rom.DateKey] = item.Date,
|
|
|
|
|
};
|
|
|
|
|
return rom;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Deserialize
|
|
|
|
|
|
2023-08-04 23:48:17 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Machine"/> to <cref="Models.DosCenter.Game"/>
|
|
|
|
|
/// </summary>
|
2023-08-08 12:04:07 -04:00
|
|
|
public static Models.DosCenter.Game? ConvertMachineToDosCenter(Models.Internal.Machine? item)
|
2023-08-04 23:48:17 -04:00
|
|
|
{
|
2023-08-08 12:04:07 -04:00
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-04 23:48:17 -04:00
|
|
|
var game = new Models.DosCenter.Game
|
|
|
|
|
{
|
|
|
|
|
Name = item.ReadString(Models.Internal.Machine.NameKey),
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-08 12:04:07 -04:00
|
|
|
var roms = item.Read<Models.Internal.Rom[]>(Models.Internal.Machine.RomKey);
|
|
|
|
|
game.File = roms?.Select(ConvertToDosCenter)?.ToArray();
|
2023-08-04 23:48:17 -04:00
|
|
|
|
|
|
|
|
return game;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-04 22:16:00 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.DosCenter.File"/>
|
|
|
|
|
/// </summary>
|
2023-08-08 12:04:07 -04:00
|
|
|
public static Models.DosCenter.File? ConvertToDosCenter(Models.Internal.Rom? item)
|
2023-08-04 22:16:00 -04:00
|
|
|
{
|
2023-08-08 12:04:07 -04:00
|
|
|
if (item == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-04 22:16:00 -04:00
|
|
|
var file = new Models.DosCenter.File
|
|
|
|
|
{
|
|
|
|
|
Name = item.ReadString(Models.Internal.Rom.NameKey),
|
|
|
|
|
Size = item.ReadString(Models.Internal.Rom.SizeKey),
|
|
|
|
|
CRC = item.ReadString(Models.Internal.Rom.CRCKey),
|
|
|
|
|
Date = item.ReadString(Models.Internal.Rom.DateKey),
|
|
|
|
|
};
|
|
|
|
|
return file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|