(Badly) use DeviceReference

This commit is contained in:
Matt Nadareski
2020-08-23 15:42:58 -07:00
parent 2576e22c85
commit 79e7446266
5 changed files with 32 additions and 35 deletions

View File

@@ -1534,8 +1534,8 @@ namespace SabreTools.Library.DatFiles
continue; continue;
// If the game has no devices, we continue // If the game has no devices, we continue
if (Items[game][0].Machine.Devices == null if (Items[game][0].Machine.DeviceReferences == null
|| Items[game][0].Machine.Devices.Count == 0 || Items[game][0].Machine.DeviceReferences.Count == 0
|| (slotoptions && Items[game][0].Machine.SlotOptions == null) || (slotoptions && Items[game][0].Machine.SlotOptions == null)
|| (slotoptions && Items[game][0].Machine.SlotOptions.Count == 0)) || (slotoptions && Items[game][0].Machine.SlotOptions.Count == 0))
{ {
@@ -1543,21 +1543,21 @@ namespace SabreTools.Library.DatFiles
} }
// Determine if the game has any devices or not // Determine if the game has any devices or not
List<string> devices = Items[game][0].Machine.Devices; List<ListXmlDeviceReference> deviceReferences = Items[game][0].Machine.DeviceReferences;
List<string> newdevs = new List<string>(); List<ListXmlDeviceReference> newdevs = new List<ListXmlDeviceReference>();
foreach (string device in devices) foreach (ListXmlDeviceReference deviceReference in deviceReferences)
{ {
// If the device doesn't exist then we continue // If the device doesn't exist then we continue
if (Items[device].Count == 0) if (Items[deviceReference.Name].Count == 0)
continue; continue;
// Otherwise, copy the items from the device to the current game // Otherwise, copy the items from the device to the current game
DatItem copyFrom = Items[game][0]; DatItem copyFrom = Items[game][0];
List<DatItem> devItems = Items[device]; List<DatItem> devItems = Items[deviceReference.Name];
foreach (DatItem item in devItems) foreach (DatItem item in devItems)
{ {
DatItem datItem = (DatItem)item.Clone(); DatItem datItem = (DatItem)item.Clone();
newdevs.AddRange(datItem.Machine.Devices ?? new List<string>()); newdevs.AddRange(datItem.Machine.DeviceReferences ?? new List<ListXmlDeviceReference>());
datItem.CopyMachineInformation(copyFrom); datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0) if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
{ {
@@ -1568,10 +1568,10 @@ namespace SabreTools.Library.DatFiles
} }
// Now that every device is accounted for, add the new list of devices, if they don't already exist // Now that every device is accounted for, add the new list of devices, if they don't already exist
foreach (string device in newdevs) foreach (ListXmlDeviceReference device in newdevs)
{ {
if (!Items[game][0].Machine.Devices.Contains(device)) if (!Items[game][0].Machine.DeviceReferences.Select(d => d.Name).Contains(device.Name))
Items[game][0].Machine.Devices.Add(device); Items[game][0].Machine.DeviceReferences.Add(new ListXmlDeviceReference() { Name = device.Name });
} }
// If we're checking slotoptions too // If we're checking slotoptions too

View File

@@ -542,11 +542,11 @@ namespace SabreTools.Library.DatFiles
machine.Runnable = jtr.ReadAsString().AsRunnable(); machine.Runnable = jtr.ReadAsString().AsRunnable();
break; break;
case "devices": case "devices":
machine.Devices = new List<string>(); machine.DeviceReferences = new List<ListXmlDeviceReference>();
jtr.Read(); // Start Array jtr.Read(); // Start Array
while (!sr.EndOfStream && jtr.TokenType != JsonToken.EndArray) while (!sr.EndOfStream && jtr.TokenType != JsonToken.EndArray)
{ {
machine.Devices.Add(jtr.ReadAsString()); machine.DeviceReferences.Add(new ListXmlDeviceReference() { Name = jtr.ReadAsString() });
} }
break; break;
@@ -1728,9 +1728,9 @@ namespace SabreTools.Library.DatFiles
{ {
jtw.WritePropertyName("devices"); jtw.WritePropertyName("devices");
jtw.WriteStartArray(); jtw.WriteStartArray();
foreach (string device in datItem.Machine.Devices) foreach (ListXmlDeviceReference device in datItem.Machine.DeviceReferences)
{ {
jtw.WriteValue(device); jtw.WriteValue(device.Name);
} }
jtw.WriteEndArray(); jtw.WriteEndArray();

View File

@@ -152,7 +152,7 @@ namespace SabreTools.Library.DatFiles
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty, CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
RomOf = reader.GetAttribute("romof") ?? string.Empty, RomOf = reader.GetAttribute("romof") ?? string.Empty,
SampleOf = reader.GetAttribute("sampleof") ?? string.Empty, SampleOf = reader.GetAttribute("sampleof") ?? string.Empty,
Devices = new List<string>(), DeviceReferences = new List<ListXmlDeviceReference>(),
SlotOptions = new List<string>(), SlotOptions = new List<string>(),
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType), MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
@@ -285,9 +285,7 @@ namespace SabreTools.Library.DatFiles
var deviceReference = new ListXmlDeviceReference(); var deviceReference = new ListXmlDeviceReference();
deviceReference.Name = reader.GetAttribute("name"); deviceReference.Name = reader.GetAttribute("name");
// TODO: Retire this direct machine setter machine.DeviceReferences.Add(deviceReference);
if (!machine.Devices.Contains(deviceReference.Name))
machine.Devices.Add(deviceReference.Name);
reader.Read(); reader.Read();
break; break;

View File

@@ -380,11 +380,11 @@ namespace SabreTools.Library.DatFiles
break; break;
case "Machine.Devices": case "Machine.Devices":
machine.Devices = new List<string>(); machine.DeviceReferences = new List<ListXmlDeviceReference>();
var devices = value.Split(';'); var devices = value.Split(';');
foreach (var device in devices) foreach (var device in devices)
{ {
machine.Devices.Add(device); machine.DeviceReferences.Add(new ListXmlDeviceReference() { Name = device });
} }
break; break;

View File

@@ -152,9 +152,8 @@ namespace SabreTools.Library.DatItems
/// <summary> /// <summary>
/// List of associated device names /// List of associated device names
/// </summary> /// </summary>
/// TODO: Use ListXmlDeviceReference for this...
[JsonProperty("devices")] [JsonProperty("devices")]
public List<string> Devices { get; set; } = null; public List<ListXmlDeviceReference> DeviceReferences { get; set; } = null;
/// <summary> /// <summary>
/// List of slot options /// List of slot options
@@ -382,7 +381,7 @@ namespace SabreTools.Library.DatItems
fieldValue = Runnable.ToString(); fieldValue = Runnable.ToString();
break; break;
case Field.Devices: case Field.Devices:
fieldValue = string.Join(";", Devices ?? new List<string>()); fieldValue = string.Join(";", DeviceReferences ?? new List<ListXmlDeviceReference>());
break; break;
case Field.SlotOptions: case Field.SlotOptions:
fieldValue = string.Join(";", SlotOptions ?? new List<string>()); fieldValue = string.Join(";", SlotOptions ?? new List<string>());
@@ -554,11 +553,11 @@ namespace SabreTools.Library.DatItems
if (mappings.Keys.Contains(Field.Devices)) if (mappings.Keys.Contains(Field.Devices))
{ {
if (Devices == null) if (DeviceReferences == null)
Devices = new List<string>(); DeviceReferences = new List<ListXmlDeviceReference>();
string[] devices = mappings[Field.Devices].Split(';'); var devices = mappings[Field.Devices].Split(';').Select(d => new ListXmlDeviceReference() { Name = d, });
Devices.AddRange(devices); DeviceReferences.AddRange(devices);
} }
if (mappings.Keys.Contains(Field.SlotOptions)) if (mappings.Keys.Contains(Field.SlotOptions))
@@ -738,7 +737,7 @@ namespace SabreTools.Library.DatItems
SourceFile = this.SourceFile, SourceFile = this.SourceFile,
Runnable = this.Runnable, Runnable = this.Runnable,
Devices = this.Devices, DeviceReferences = this.DeviceReferences,
SlotOptions = this.SlotOptions, SlotOptions = this.SlotOptions,
Infos = this.Infos, Infos = this.Infos,
MachineType = this.MachineType, MachineType = this.MachineType,
@@ -933,14 +932,14 @@ namespace SabreTools.Library.DatItems
return false; return false;
// Filter on devices // Filter on devices
if (Devices != null && Devices.Any()) if (DeviceReferences != null && DeviceReferences.Any())
{ {
bool anyPositiveDevice = false; bool anyPositiveDevice = false;
bool anyNegativeDevice = false; bool anyNegativeDevice = false;
foreach (string device in Devices) foreach (ListXmlDeviceReference device in DeviceReferences)
{ {
anyPositiveDevice |= filter.Devices.MatchesPositiveSet(device) != false; anyPositiveDevice |= filter.Devices.MatchesPositiveSet(device.Name) != false;
anyNegativeDevice |= filter.Devices.MatchesNegativeSet(device) == false; anyNegativeDevice |= filter.Devices.MatchesNegativeSet(device.Name) == false;
} }
if (!anyPositiveDevice || anyNegativeDevice) if (!anyPositiveDevice || anyNegativeDevice)
@@ -1154,7 +1153,7 @@ namespace SabreTools.Library.DatItems
Runnable = Runnable.NULL; Runnable = Runnable.NULL;
if (fields.Contains(Field.Devices)) if (fields.Contains(Field.Devices))
Devices = null; DeviceReferences = null;
if (fields.Contains(Field.SlotOptions)) if (fields.Contains(Field.SlotOptions))
SlotOptions = null; SlotOptions = null;
@@ -1318,7 +1317,7 @@ namespace SabreTools.Library.DatItems
Runnable = machine.Runnable; Runnable = machine.Runnable;
if (fields.Contains(Field.Devices)) if (fields.Contains(Field.Devices))
Devices = machine.Devices; DeviceReferences = machine.DeviceReferences;
if (fields.Contains(Field.SlotOptions)) if (fields.Contains(Field.SlotOptions))
SlotOptions = machine.SlotOptions; SlotOptions = machine.SlotOptions;