diff --git a/SabreTools.Serialization/Internal.OpenMSX.cs b/SabreTools.Serialization/Internal.OpenMSX.cs deleted file mode 100644 index f29ed5e3..00000000 --- a/SabreTools.Serialization/Internal.OpenMSX.cs +++ /dev/null @@ -1,266 +0,0 @@ -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 - } -} \ No newline at end of file diff --git a/SabreTools.Serialization/OpenMSX.Deserializer.cs b/SabreTools.Serialization/OpenMSX.Deserializer.cs new file mode 100644 index 00000000..a8c6ae08 --- /dev/null +++ b/SabreTools.Serialization/OpenMSX.Deserializer.cs @@ -0,0 +1,150 @@ +using System.Linq; +using SabreTools.Models.OpenMSX; + +namespace SabreTools.Serialization +{ + /// + /// XML deserializer for OpenMSX software database files + /// + public partial class OpenMSX : XmlSerializer + { + // TODO: Add deserialization of entire SoftwareDb + #region Internal + + /// + /// Convert from to + /// + public static SoftwareDb? ConvertHeaderFromInternalModel(Models.Internal.Header? item) + { + if (item == null) + return null; + + var softwareDb = new SoftwareDb + { + Timestamp = item.ReadString(Models.Internal.Header.TimestampKey), + }; + return softwareDb; + } + + /// + /// Convert from to + /// + public static Software? ConvertMachineFromInternalModel(Models.Internal.Machine? item) + { + if (item == null) + return null; + + var game = new Software + { + Title = item.ReadString(Models.Internal.Machine.NameKey), + GenMSXID = item.ReadString(Models.Internal.Machine.GenMSXIDKey), + System = item.ReadString(Models.Internal.Machine.SystemKey), + Company = item.ReadString(Models.Internal.Machine.CompanyKey), + Year = item.ReadString(Models.Internal.Machine.YearKey), + Country = item.ReadString(Models.Internal.Machine.CountryKey), + }; + + var dumps = item.Read(Models.Internal.Machine.DumpKey); + game.Dump = dumps?.Select(ConvertFromInternalModel)?.ToArray(); + + return game; + } + + /// + /// Convert from to + /// + private static Dump? ConvertFromInternalModel(Models.Internal.Dump? item) + { + if (item == null) + return null; + + var dump = new Dump(); + + var original = item.Read(Models.Internal.Dump.OriginalKey); + dump.Original = ConvertFromInternalModel(original); + + var rom = item.Read(Models.Internal.Dump.RomKey); + dump.Rom = ConvertRomFromInternalModel(rom); + + var megaRom = item.Read(Models.Internal.Dump.MegaRomKey); + dump.Rom = ConvertMegaRomFromInternalModel(megaRom); + + var sccPlusCart = item.Read(Models.Internal.Dump.SCCPlusCartKey); + dump.Rom = ConvertSCCPlusCartFromInternalModel(sccPlusCart); + + return dump; + } + + /// + /// Convert from to + /// + private static MegaRom? ConvertMegaRomFromInternalModel(Models.Internal.Rom? item) + { + if (item == null) + return null; + + var megaRom = new MegaRom + { + Start = item.ReadString(Models.Internal.Rom.StartKey), + Type = item.ReadString(Models.Internal.Rom.TypeKey), + Hash = item.ReadString(Models.Internal.Rom.SHA1Key), + Remark = item.ReadString(Models.Internal.Rom.RemarkKey), + }; + return megaRom; + } + + /// + /// Convert from to + /// + private static Original? ConvertFromInternalModel(Models.Internal.Original? item) + { + if (item == null) + return null; + + var original = new Original + { + Value = item.ReadString(Models.Internal.Original.ValueKey), + Content = item.ReadString(Models.Internal.Original.ContentKey), + }; + return original; + } + + /// + /// Convert from to + /// + private static Rom? ConvertRomFromInternalModel(Models.Internal.Rom? item) + { + if (item == null) + return null; + + var rom = new Rom + { + Start = item.ReadString(Models.Internal.Rom.StartKey), + Type = item.ReadString(Models.Internal.Rom.TypeKey), + Hash = item.ReadString(Models.Internal.Rom.SHA1Key), + Remark = item.ReadString(Models.Internal.Rom.RemarkKey), + }; + return rom; + } + + /// + /// Convert from to + /// + private static SCCPlusCart? ConvertSCCPlusCartFromInternalModel(Models.Internal.Rom? item) + { + if (item == null) + return null; + + var sccPlusCart = new SCCPlusCart + { + Start = item.ReadString(Models.Internal.Rom.StartKey), + Type = item.ReadString(Models.Internal.Rom.TypeKey), + Hash = item.ReadString(Models.Internal.Rom.SHA1Key), + Remark = item.ReadString(Models.Internal.Rom.RemarkKey), + }; + return sccPlusCart; + } + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.Serialization/OpenMSX.Serializer.cs b/SabreTools.Serialization/OpenMSX.Serializer.cs new file mode 100644 index 00000000..bf3e6842 --- /dev/null +++ b/SabreTools.Serialization/OpenMSX.Serializer.cs @@ -0,0 +1,129 @@ +using System.IO; +using System.Linq; +using SabreTools.Models.OpenMSX; + +namespace SabreTools.Serialization +{ + /// + /// XML serializer for OpenMSX software database files + /// + public partial class OpenMSX : XmlSerializer + { + /// + public static bool SerializeToFileWithDocType(SoftwareDb obj, string path) + => SerializeToFile(obj, path, DocTypeName, DocTypePubId, DocTypeSysId, DocTypeSysId); + + /// + public static Stream? SerializeToStreamWithDocType(SoftwareDb obj, string path) + => SerializeToStream(obj, DocTypeName, DocTypePubId, DocTypeSysId, DocTypeSysId); + + #region Internal + + /// + /// Convert from to + /// + public static Models.Internal.MetadataFile ConvertToInternalModel(SoftwareDb item) + { + var metadataFile = new Models.Internal.MetadataFile + { + [Models.Internal.MetadataFile.HeaderKey] = ConvertHeaderToInternalModel(item), + }; + + if (item?.Software != null && item.Software.Any()) + metadataFile[Models.Internal.MetadataFile.MachineKey] = item.Software.Select(ConvertMachineToInternalModel).ToArray(); + + return metadataFile; + } + + /// + /// Convert from to + /// + public static Models.Internal.Header ConvertHeaderToInternalModel(SoftwareDb item) + { + var header = new Models.Internal.Header + { + [Models.Internal.Header.TimestampKey] = item.Timestamp, + }; + return header; + } + + /// + /// Convert from to + /// + public static Models.Internal.Machine ConvertMachineToInternalModel(Software item) + { + var machine = new Models.Internal.Machine + { + [Models.Internal.Machine.NameKey] = item.Title, + [Models.Internal.Machine.GenMSXIDKey] = item.GenMSXID, + [Models.Internal.Machine.SystemKey] = item.System, + [Models.Internal.Machine.CompanyKey] = item.Company, + [Models.Internal.Machine.YearKey] = item.Year, + [Models.Internal.Machine.CountryKey] = item.Country, + }; + + if (item.Dump != null && item.Dump.Any()) + machine[Models.Internal.Machine.DumpKey] = item.Dump.Select(ConvertToInternalModel).ToArray(); + + return machine; + } + + /// + /// Convert from to + /// + public static Models.Internal.Dump ConvertToInternalModel(Dump item) + { + var dump = new Models.Internal.Dump(); + + if (item.Original != null) + dump[Models.Internal.Dump.OriginalKey] = ConvertToInternalModel(item.Original); + + switch (item.Rom) + { + case Rom rom: + dump[Models.Internal.Dump.RomKey] = ConvertToInternalModel(rom); + break; + + case MegaRom megaRom: + dump[Models.Internal.Dump.MegaRomKey] = ConvertToInternalModel(megaRom); + break; + + case SCCPlusCart sccPlusCart: + dump[Models.Internal.Dump.SCCPlusCartKey] = ConvertToInternalModel(sccPlusCart); + break; + } + + return dump; + } + + /// + /// Convert from to + /// + public static Models.Internal.Original ConvertToInternalModel(Original item) + { + var original = new Models.Internal.Original + { + [Models.Internal.Original.ValueKey] = item.Value, + [Models.Internal.Original.ContentKey] = item.Content, + }; + return original; + } + + /// + /// Convert from to + /// + public static Models.Internal.Rom ConvertToInternalModel(RomBase item) + { + var rom = new Models.Internal.Rom + { + [Models.Internal.Rom.StartKey] = item.Start, + [Models.Internal.Rom.TypeKey] = item.Type, + [Models.Internal.Rom.SHA1Key] = item.Hash, + [Models.Internal.Rom.RemarkKey] = item.Remark, + }; + return rom; + } + + #endregion + } +} \ No newline at end of file diff --git a/SabreTools.Serialization/OpenMSX.cs b/SabreTools.Serialization/OpenMSX.cs index 17d94692..e3069fe3 100644 --- a/SabreTools.Serialization/OpenMSX.cs +++ b/SabreTools.Serialization/OpenMSX.cs @@ -1,11 +1,9 @@ -using System.IO; - namespace SabreTools.Serialization { /// /// XML deserializer for OpenMSX software database files /// - public class OpenMSX : XmlSerializer + public partial class OpenMSX : XmlSerializer { /// /// name field for DOCTYPE @@ -26,13 +24,5 @@ namespace SabreTools.Serialization /// subset field for DOCTYPE /// public const string? DocTypeSubset = null; - - /// - public static bool SerializeToFileWithDocType(Models.OpenMSX.SoftwareDb obj, string path) - => SerializeToFile(obj, path, DocTypeName, DocTypePubId, DocTypeSysId, DocTypeSysId); - - /// - public static Stream? SerializeToStreamWithDocType(Models.OpenMSX.SoftwareDb obj, string path) - => SerializeToStream(obj, DocTypeName, DocTypePubId, DocTypeSysId, DocTypeSysId); } } \ No newline at end of file