mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move Listrom internal serialization
This commit is contained in:
@@ -1,194 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using SabreTools.Models.Internal;
|
|
||||||
|
|
||||||
namespace SabreTools.Serialization
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Serializer for Listrom models to internal structure
|
|
||||||
/// </summary>
|
|
||||||
public partial class Internal
|
|
||||||
{
|
|
||||||
#region Serialize
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Models.Listrom.MetadataFile"/> to <cref="MetadataFile"/>
|
|
||||||
/// </summary>
|
|
||||||
public static MetadataFile ConvertToInternalModel(Models.Listrom.MetadataFile item)
|
|
||||||
{
|
|
||||||
var metadataFile = new MetadataFile
|
|
||||||
{
|
|
||||||
[MetadataFile.HeaderKey] = ConvertHeaderToInternalModel(item),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (item?.Set != null && item.Set.Any())
|
|
||||||
metadataFile[MetadataFile.MachineKey] = item.Set.Select(ConvertMachineToInternalModel).ToArray();
|
|
||||||
|
|
||||||
return metadataFile;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Models.Listrom.MetadataFile"/> to <cref="Header"/>
|
|
||||||
/// </summary>
|
|
||||||
private static Header ConvertHeaderToInternalModel(Models.Listrom.MetadataFile item)
|
|
||||||
{
|
|
||||||
var header = new Header
|
|
||||||
{
|
|
||||||
[Header.NameKey] = "MAME Listrom",
|
|
||||||
};
|
|
||||||
return header;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Models.Listrom.Set"/> to <cref="Machine"/>
|
|
||||||
/// </summary>
|
|
||||||
private static Machine ConvertMachineToInternalModel(Models.Listrom.Set item)
|
|
||||||
{
|
|
||||||
var machine = new Machine();
|
|
||||||
if (!string.IsNullOrWhiteSpace(item.Device))
|
|
||||||
{
|
|
||||||
machine[Machine.NameKey] = item.Device;
|
|
||||||
machine[Machine.IsDeviceKey] = "yes";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
machine[Machine.NameKey] = item.Driver;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item.Row != null && item.Row.Any())
|
|
||||||
{
|
|
||||||
var datItems = new List<DatItem>();
|
|
||||||
foreach (var file in item.Row)
|
|
||||||
{
|
|
||||||
datItems.Add(ConvertToInternalModel(file));
|
|
||||||
}
|
|
||||||
|
|
||||||
machine[Machine.DiskKey] = datItems.Where(i => i.ReadString(DatItem.TypeKey) == "disk")?.ToArray();
|
|
||||||
machine[Machine.RomKey] = datItems.Where(i => i.ReadString(DatItem.TypeKey) == "rom")?.ToArray();
|
|
||||||
}
|
|
||||||
|
|
||||||
return machine;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Models.Listrom.Row"/> to <cref="DatItem"/>
|
|
||||||
/// </summary>
|
|
||||||
private static DatItem ConvertToInternalModel(Models.Listrom.Row item)
|
|
||||||
{
|
|
||||||
if (item.Size == null)
|
|
||||||
{
|
|
||||||
var disk = new Disk
|
|
||||||
{
|
|
||||||
[Disk.NameKey] = item.Name,
|
|
||||||
[Disk.MD5Key] = item.MD5,
|
|
||||||
[Disk.SHA1Key] = item.SHA1,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (item.NoGoodDumpKnown)
|
|
||||||
disk[Disk.StatusKey] = "nodump";
|
|
||||||
else if (item.Bad)
|
|
||||||
disk[Disk.StatusKey] = "baddump";
|
|
||||||
|
|
||||||
return disk;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var rom = new Rom
|
|
||||||
{
|
|
||||||
[Rom.NameKey] = item.Name,
|
|
||||||
[Rom.SizeKey] = item.Size,
|
|
||||||
[Rom.CRCKey] = item.CRC,
|
|
||||||
[Rom.SHA1Key] = item.SHA1,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (item.NoGoodDumpKnown)
|
|
||||||
rom[Rom.StatusKey] = "nodump";
|
|
||||||
else if (item.Bad)
|
|
||||||
rom[Rom.StatusKey] = "baddump";
|
|
||||||
|
|
||||||
return rom;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region Deserialize
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Machine"/> to <cref="Models.Listrom.Set"/>
|
|
||||||
/// </summary>
|
|
||||||
public static Models.Listrom.Set? ConvertMachineToListrom(Machine? item)
|
|
||||||
{
|
|
||||||
if (item == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var set = new Models.Listrom.Set();
|
|
||||||
if (item.ReadString(Machine.IsDeviceKey) == "yes")
|
|
||||||
set.Device = item.ReadString(Machine.NameKey);
|
|
||||||
else
|
|
||||||
set.Driver = item.ReadString(Machine.NameKey);
|
|
||||||
|
|
||||||
var rowItems = new List<Models.Listrom.Row>();
|
|
||||||
|
|
||||||
var roms = item.Read<Rom[]>(Machine.RomKey);
|
|
||||||
if (roms != null)
|
|
||||||
rowItems.AddRange(roms.Select(ConvertToListrom));
|
|
||||||
|
|
||||||
var disks = item.Read<Disk[]>(Machine.DiskKey);
|
|
||||||
if (disks != null)
|
|
||||||
rowItems.AddRange(disks.Select(ConvertToListrom));
|
|
||||||
|
|
||||||
set.Row = rowItems.ToArray();
|
|
||||||
return set;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Disk"/> to <cref="Models.Listrom.Row"/>
|
|
||||||
/// </summary>
|
|
||||||
private static Models.Listrom.Row? ConvertToListrom(Disk? item)
|
|
||||||
{
|
|
||||||
if (item == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var row = new Models.Listrom.Row
|
|
||||||
{
|
|
||||||
Name = item.ReadString(Disk.NameKey),
|
|
||||||
MD5 = item.ReadString(Disk.MD5Key),
|
|
||||||
SHA1 = item.ReadString(Disk.SHA1Key),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (item[Disk.StatusKey] as string == "nodump")
|
|
||||||
row.NoGoodDumpKnown = true;
|
|
||||||
else if (item[Disk.StatusKey] as string == "baddump")
|
|
||||||
row.Bad = true;
|
|
||||||
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Convert from <cref="Rom"/> to <cref="Models.Listrom.Row"/>
|
|
||||||
/// </summary>
|
|
||||||
private static Models.Listrom.Row? ConvertToListrom(Rom? item)
|
|
||||||
{
|
|
||||||
if (item == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
var row = new Models.Listrom.Row
|
|
||||||
{
|
|
||||||
Name = item.ReadString(Rom.NameKey),
|
|
||||||
Size = item.ReadString(Rom.SizeKey),
|
|
||||||
CRC = item.ReadString(Rom.CRCKey),
|
|
||||||
SHA1 = item.ReadString(Rom.SHA1Key),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (item[Rom.StatusKey] as string == "nodump")
|
|
||||||
row.NoGoodDumpKnown = true;
|
|
||||||
else if (item[Rom.StatusKey] as string == "baddump")
|
|
||||||
row.Bad = true;
|
|
||||||
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using SabreTools.Models.Listrom;
|
using SabreTools.Models.Listrom;
|
||||||
|
|
||||||
@@ -186,5 +187,85 @@ namespace SabreTools.Serialization
|
|||||||
dat.ADDITIONAL_ELEMENTS = additional.ToArray();
|
dat.ADDITIONAL_ELEMENTS = additional.ToArray();
|
||||||
return dat;
|
return dat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Add deserialization of entire MetadataFile
|
||||||
|
#region Internal
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Internal.Machine"/> to <cref="Models.Listrom.Set"/>
|
||||||
|
/// </summary>
|
||||||
|
public static Set? ConvertMachineToListrom(Models.Internal.Machine? item)
|
||||||
|
{
|
||||||
|
if (item == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var set = new 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<Row>();
|
||||||
|
|
||||||
|
var roms = item.Read<Models.Internal.Rom[]>(Models.Internal.Machine.RomKey);
|
||||||
|
if (roms != null)
|
||||||
|
rowItems.AddRange(roms.Select(ConvertToListrom));
|
||||||
|
|
||||||
|
var disks = item.Read<Models.Internal.Disk[]>(Models.Internal.Machine.DiskKey);
|
||||||
|
if (disks != null)
|
||||||
|
rowItems.AddRange(disks.Select(ConvertToListrom));
|
||||||
|
|
||||||
|
set.Row = rowItems.ToArray();
|
||||||
|
return set;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Internal.Disk"/> to <cref="Models.Listrom.Row"/>
|
||||||
|
/// </summary>
|
||||||
|
private static Row? ConvertToListrom(Models.Internal.Disk? item)
|
||||||
|
{
|
||||||
|
if (item == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var row = new Row
|
||||||
|
{
|
||||||
|
Name = item.ReadString(Models.Internal.Disk.NameKey),
|
||||||
|
MD5 = item.ReadString(Models.Internal.Disk.MD5Key),
|
||||||
|
SHA1 = item.ReadString(Models.Internal.Disk.SHA1Key),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item[Models.Internal.Disk.StatusKey] as string == "nodump")
|
||||||
|
row.NoGoodDumpKnown = true;
|
||||||
|
else if (item[Models.Internal.Disk.StatusKey] as string == "baddump")
|
||||||
|
row.Bad = true;
|
||||||
|
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Internal.Rom"/> to <cref="Models.Listrom.Row"/>
|
||||||
|
/// </summary>
|
||||||
|
private static Row? ConvertToListrom(Models.Internal.Rom? item)
|
||||||
|
{
|
||||||
|
if (item == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
var row = new Row
|
||||||
|
{
|
||||||
|
Name = item.ReadString(Models.Internal.Rom.NameKey),
|
||||||
|
Size = item.ReadString(Models.Internal.Rom.SizeKey),
|
||||||
|
CRC = item.ReadString(Models.Internal.Rom.CRCKey),
|
||||||
|
SHA1 = item.ReadString(Models.Internal.Rom.SHA1Key),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item[Models.Internal.Rom.StatusKey] as string == "nodump")
|
||||||
|
row.NoGoodDumpKnown = true;
|
||||||
|
else if (item[Models.Internal.Rom.StatusKey] as string == "baddump")
|
||||||
|
row.Bad = true;
|
||||||
|
|
||||||
|
return row;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@@ -175,5 +176,108 @@ namespace SabreTools.Serialization
|
|||||||
writer.Flush();
|
writer.Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#region Internal
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Listrom.MetadataFile"/> to <cref="Models.Internal.MetadataFile"/>
|
||||||
|
/// </summary>
|
||||||
|
public static Models.Internal.MetadataFile ConvertToInternalModel(MetadataFile item)
|
||||||
|
{
|
||||||
|
var metadataFile = new Models.Internal.MetadataFile
|
||||||
|
{
|
||||||
|
[Models.Internal.MetadataFile.HeaderKey] = ConvertHeaderToInternalModel(item),
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item?.Set != null && item.Set.Any())
|
||||||
|
metadataFile[Models.Internal.MetadataFile.MachineKey] = item.Set.Select(ConvertMachineToInternalModel).ToArray();
|
||||||
|
|
||||||
|
return metadataFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Listrom.MetadataFile"/> to <cref="Header"/>
|
||||||
|
/// </summary>
|
||||||
|
private static Models.Internal.Header ConvertHeaderToInternalModel(MetadataFile item)
|
||||||
|
{
|
||||||
|
var header = new Models.Internal.Header
|
||||||
|
{
|
||||||
|
[Models.Internal.Header.NameKey] = "MAME Listrom",
|
||||||
|
};
|
||||||
|
return header;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Listrom.Set"/> to <cref="Models.Internal.Machine"/>
|
||||||
|
/// </summary>
|
||||||
|
private static Models.Internal.Machine ConvertMachineToInternalModel(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<Models.Internal.DatItem>();
|
||||||
|
foreach (var file in item.Row)
|
||||||
|
{
|
||||||
|
datItems.Add(ConvertToInternalModel(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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Convert from <cref="Models.Listrom.Row"/> to <cref="Models.Internal.DatItem"/>
|
||||||
|
/// </summary>
|
||||||
|
private static Models.Internal.DatItem ConvertToInternalModel(Row item)
|
||||||
|
{
|
||||||
|
if (item.Size == null)
|
||||||
|
{
|
||||||
|
var disk = new Models.Internal.Disk
|
||||||
|
{
|
||||||
|
[Models.Internal.Disk.NameKey] = item.Name,
|
||||||
|
[Models.Internal.Disk.MD5Key] = item.MD5,
|
||||||
|
[Models.Internal.Disk.SHA1Key] = item.SHA1,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item.NoGoodDumpKnown)
|
||||||
|
disk[Models.Internal.Disk.StatusKey] = "nodump";
|
||||||
|
else if (item.Bad)
|
||||||
|
disk[Models.Internal.Disk.StatusKey] = "baddump";
|
||||||
|
|
||||||
|
return disk;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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.SHA1Key] = item.SHA1,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (item.NoGoodDumpKnown)
|
||||||
|
rom[Models.Internal.Rom.StatusKey] = "nodump";
|
||||||
|
else if (item.Bad)
|
||||||
|
rom[Models.Internal.Rom.StatusKey] = "baddump";
|
||||||
|
|
||||||
|
return rom;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user