diff --git a/SabreTools.Serialization/Internal.Listrom.cs b/SabreTools.Serialization/Internal.Listrom.cs index e77d6887..722051de 100644 --- a/SabreTools.Serialization/Internal.Listrom.cs +++ b/SabreTools.Serialization/Internal.Listrom.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.Linq; + namespace SabreTools.Serialization { /// @@ -7,6 +10,37 @@ namespace SabreTools.Serialization { #region Serialize + /// + /// Convert from to + /// + public static Models.Internal.Machine ConvertMachineFromListrom(Models.Listrom.Set item) + { + var machine = new Models.Internal.Machine(); + if (!string.IsNullOrWhiteSpace(item.Device)) + { + machine[Models.Internal.Machine.NameKey] = item.Device; + machine[Models.Internal.Machine.IsDeviceKey] = "yes"; + } + else + { + machine[Models.Internal.Machine.NameKey] = item.Driver; + } + + if (item.Row != null && item.Row.Any()) + { + var datItems = new List(); + foreach (var file in item.Row) + { + datItems.Add(ConvertFromListrom(file)); + } + + machine[Models.Internal.Machine.DiskKey] = datItems.Where(i => i.ReadString(Models.Internal.DatItem.TypeKey) == "disk").ToArray(); + machine[Models.Internal.Machine.RomKey] = datItems.Where(i => i.ReadString(Models.Internal.DatItem.TypeKey) == "rom").ToArray(); + } + + return machine; + } + /// /// Convert from to /// @@ -51,6 +85,41 @@ namespace SabreTools.Serialization #region Deserialize + /// + /// Convert from to + /// + public static Models.Listrom.Set ConvertMachineToListrom(Models.Internal.Machine item) + { + var set = new Models.Listrom.Set(); + if (item.ReadString(Models.Internal.Machine.IsDeviceKey) == "yes") + set.Device = item.ReadString(Models.Internal.Machine.NameKey); + else + set.Driver = item.ReadString(Models.Internal.Machine.NameKey); + + var rowItems = new List(); + + if (item.ContainsKey(Models.Internal.Machine.RomKey) && item[Models.Internal.Machine.RomKey] is Models.Internal.Rom[] roms) + { + foreach (var rom in roms) + { + rowItems.Add(ConvertToListrom(rom)); + } + } + + if (item.ContainsKey(Models.Internal.Machine.DiskKey) && item[Models.Internal.Machine.DiskKey] is Models.Internal.Disk[] disks) + { + foreach (var disk in disks) + { + rowItems.Add(ConvertToListrom(disk)); + } + } + + if (rowItems.Any()) + set.Row = rowItems.ToArray(); + + return set; + } + /// /// Convert from to ///