using System.Collections.Generic;
using System.Linq;
using SabreTools.Models.Internal;
namespace SabreTools.Serialization
{
///
/// Serializer for OpenMSX models to internal structure
///
public partial class Internal
{
#region Serialize
///
/// Convert from to
///
public static MetadataFile ConvertToInternalModel(Models.OpenMSX.SoftwareDb item)
{
var metadataFile = new MetadataFile
{
[MetadataFile.HeaderKey] = ConvertHeaderToInternalModel(item),
};
if (item?.Software != null && item.Software.Any())
metadataFile[MetadataFile.MachineKey] = item.Software.Select(ConvertMachineToInternalModel).ToArray();
return metadataFile;
}
///
/// Convert from to
///
public static Header ConvertHeaderToInternalModel(Models.OpenMSX.SoftwareDb item)
{
var header = new Header
{
[Header.TimestampKey] = item.Timestamp,
};
return header;
}
///
/// Convert from to
///
public static Machine ConvertMachineToInternalModel(Models.OpenMSX.Software item)
{
var machine = new Machine
{
[Machine.NameKey] = item.Title,
[Machine.GenMSXIDKey] = item.GenMSXID,
[Machine.SystemKey] = item.System,
[Machine.CompanyKey] = item.Company,
[Machine.YearKey] = item.Year,
[Machine.CountryKey] = item.Country,
};
if (item.Dump != null && item.Dump.Any())
{
var dumps = new List();
foreach (var dump in item.Dump)
{
dumps.Add(ConvertToInternalModel(dump));
}
machine[Machine.DumpKey] = dumps.ToArray();
}
return machine;
}
///
/// Convert from to
///
public static Dump ConvertToInternalModel(Models.OpenMSX.Dump item)
{
var dump = new Dump();
if (item.Original != null)
dump[Dump.OriginalKey] = ConvertToInternalModel(item.Original);
switch (item.Rom)
{
case Models.OpenMSX.Rom rom:
dump[Dump.RomKey] = ConvertToInternalModel(rom);
break;
case Models.OpenMSX.MegaRom megaRom:
dump[Dump.MegaRomKey] = ConvertToInternalModel(megaRom);
break;
case Models.OpenMSX.SCCPlusCart sccPlusCart:
dump[Dump.SCCPlusCartKey] = ConvertToInternalModel(sccPlusCart);
break;
}
return dump;
}
///
/// Convert from to
///
public static Original ConvertToInternalModel(Models.OpenMSX.Original item)
{
var original = new Original
{
[Original.ValueKey] = item.Value,
[Original.ContentKey] = item.Content,
};
return original;
}
///
/// Convert from to
///
public static Rom ConvertToInternalModel(Models.OpenMSX.RomBase item)
{
var rom = new Rom
{
[Rom.StartKey] = item.Start,
[Rom.TypeKey] = item.Type,
[Rom.SHA1Key] = item.Hash,
[Rom.RemarkKey] = item.Remark,
};
return rom;
}
#endregion
#region Deserialize
///
/// Convert from to
///
public static Models.OpenMSX.SoftwareDb? ConvertHeaderToOpenMSX(Header? item)
{
if (item == null)
return null;
var softwareDb = new Models.OpenMSX.SoftwareDb
{
Timestamp = item.ReadString(Header.TimestampKey),
};
return softwareDb;
}
///
/// Convert from to
///
public static Models.OpenMSX.Software? ConvertMachineToOpenMSX(Machine? item)
{
if (item == null)
return null;
var game = new Models.OpenMSX.Software
{
Title = item.ReadString(Machine.NameKey),
GenMSXID = item.ReadString(Machine.GenMSXIDKey),
System = item.ReadString(Machine.SystemKey),
Company = item.ReadString(Machine.CompanyKey),
Year = item.ReadString(Machine.YearKey),
Country = item.ReadString(Machine.CountryKey),
};
var dumps = item.Read(Machine.DumpKey);
game.Dump = dumps?.Select(ConvertToOpenMSX)?.ToArray();
return game;
}
///
/// Convert from to
///
private static Models.OpenMSX.Dump? ConvertToOpenMSX(Dump? item)
{
if (item == null)
return null;
var dump = new Models.OpenMSX.Dump();
var original = item.Read(Dump.OriginalKey);
dump.Original = ConvertToOpenMSX(original);
var rom = item.Read(Dump.RomKey);
dump.Rom = ConvertToOpenMSXRom(rom);
var megaRom = item.Read(Dump.MegaRomKey);
dump.Rom = ConvertToOpenMSXMegaRom(megaRom);
var sccPlusCart = item.Read(Dump.SCCPlusCartKey);
dump.Rom = ConvertToOpenMSXSCCPlusCart(sccPlusCart);
return dump;
}
///
/// Convert from to
///
private static Models.OpenMSX.MegaRom? ConvertToOpenMSXMegaRom(Rom? item)
{
if (item == null)
return null;
var megaRom = new Models.OpenMSX.MegaRom
{
Start = item.ReadString(Rom.StartKey),
Type = item.ReadString(Rom.TypeKey),
Hash = item.ReadString(Rom.SHA1Key),
Remark = item.ReadString(Rom.RemarkKey),
};
return megaRom;
}
///
/// Convert from to
///
private static Models.OpenMSX.Original? ConvertToOpenMSX(Original? item)
{
if (item == null)
return null;
var original = new Models.OpenMSX.Original
{
Value = item.ReadString(Original.ValueKey),
Content = item.ReadString(Original.ContentKey),
};
return original;
}
///
/// Convert from to
///
private static Models.OpenMSX.Rom? ConvertToOpenMSXRom(Rom? item)
{
if (item == null)
return null;
var rom = new Models.OpenMSX.Rom
{
Start = item.ReadString(Rom.StartKey),
Type = item.ReadString(Rom.TypeKey),
Hash = item.ReadString(Rom.SHA1Key),
Remark = item.ReadString(Rom.RemarkKey),
};
return rom;
}
///
/// Convert from to
///
private static Models.OpenMSX.SCCPlusCart? ConvertToOpenMSXSCCPlusCart(Rom? item)
{
if (item == null)
return null;
var sccPlusCart = new Models.OpenMSX.SCCPlusCart
{
Start = item.ReadString(Rom.StartKey),
Type = item.ReadString(Rom.TypeKey),
Hash = item.ReadString(Rom.SHA1Key),
Remark = item.ReadString(Rom.RemarkKey),
};
return sccPlusCart;
}
#endregion
}
}