using System.Collections.Generic; using System.Linq; namespace SabreTools.Serialization { /// /// Serializer for OfflineList models to internal structure /// public partial class Internal { #region Serialize /// /// Convert from to /// public static Models.Internal.Machine ConvertMachineFromOfflineList(Models.OfflineList.Game item) { var machine = new Models.Internal.Machine { [Models.Internal.Machine.ImageNumberKey] = item.ImageNumber, [Models.Internal.Machine.ReleaseNumberKey] = item.ReleaseNumber, [Models.Internal.Machine.NameKey] = item.Title, [Models.Internal.Machine.SaveTypeKey] = item.SaveType, [Models.Internal.Machine.PublisherKey] = item.Publisher, [Models.Internal.Machine.LocationKey] = item.Location, [Models.Internal.Machine.SourceRomKey] = item.SourceRom, [Models.Internal.Machine.LanguageKey] = item.Language, [Models.Internal.Machine.Im1CRCKey] = item.Im1CRC, [Models.Internal.Machine.Im2CRCKey] = item.Im2CRC, [Models.Internal.Machine.CommentKey] = item.Comment, [Models.Internal.Machine.DuplicateIDKey] = item.DuplicateID, }; if (item.Files?.RomCRC != null && item.Files.RomCRC.Any()) { var roms = new List(); foreach (var file in item.Files.RomCRC) { var rom = ConvertFromOfflineList(file); rom[Models.Internal.Rom.SizeKey] = item.RomSize; roms.Add(rom); } machine[Models.Internal.Machine.RomKey] = roms.ToArray(); } return machine; } /// /// Convert from to an array of /// public static Models.Internal.Rom[] ConvertFromOfflineList(Models.OfflineList.Files item) { var roms = new List(); foreach (var romCRC in item.RomCRC) { roms.Add(ConvertFromOfflineList(romCRC)); } return roms.ToArray(); } /// /// Convert from to /// public static Models.Internal.Rom ConvertFromOfflineList(Models.OfflineList.FileRomCRC item) { var rom = new Models.Internal.Rom { [Models.Internal.Rom.ExtensionKey] = item.Extension, [Models.Internal.Rom.CRCKey] = item.Content, }; return rom; } #endregion #region Deserialize /// /// Convert from to /// public static Models.OfflineList.Game? ConvertMachineToOfflineList(Models.Internal.Machine? item) { if (item == null) return null; var game = new Models.OfflineList.Game { ImageNumber = item.ReadString(Models.Internal.Machine.ImageNumberKey), ReleaseNumber = item.ReadString(Models.Internal.Machine.ReleaseNumberKey), Title = item.ReadString(Models.Internal.Machine.NameKey), SaveType = item.ReadString(Models.Internal.Machine.SaveTypeKey), Publisher = item.ReadString(Models.Internal.Machine.PublisherKey), Location = item.ReadString(Models.Internal.Machine.LocationKey), SourceRom = item.ReadString(Models.Internal.Machine.SourceRomKey), Language = item.ReadString(Models.Internal.Machine.LanguageKey), Im1CRC = item.ReadString(Models.Internal.Machine.Im1CRCKey), Im2CRC = item.ReadString(Models.Internal.Machine.Im2CRCKey), Comment = item.ReadString(Models.Internal.Machine.CommentKey), DuplicateID = item.ReadString(Models.Internal.Machine.DuplicateIDKey), }; var roms = item.Read(Models.Internal.Machine.RomKey); game.RomSize = roms? .Select(rom => rom.ReadString(Models.Internal.Rom.SizeKey))? .FirstOrDefault(s => s != null); var romCRCs = roms?.Select(ConvertToOfflineList).ToArray(); game.Files = new Models.OfflineList.Files { RomCRC = romCRCs }; return game; } /// /// Convert from to /// private static Models.OfflineList.FileRomCRC? ConvertToOfflineList(Models.Internal.Rom? item) { if (item == null) return null; var fileRomCRC = new Models.OfflineList.FileRomCRC { Extension = item.ReadString(Models.Internal.Rom.ExtensionKey), Content = item.ReadString(Models.Internal.Rom.CRCKey), }; return fileRomCRC; } #endregion } }