Wire up a whole bunch of stuff on read

This commit is contained in:
Matt Nadareski
2020-08-23 21:10:29 -07:00
parent 79e7446266
commit a1d81a8e5f
13 changed files with 470 additions and 271 deletions

View File

@@ -1083,6 +1083,7 @@ namespace SabreTools.Library.DatFiles
OneRomPerGame();
// If we are removing fields, do that now
// TODO: If this works, why in the hell do I have all of the "GetField" crap?????
if (Header.ExcludeFields != null && Header.ExcludeFields.Any())
RemoveFieldsFromItems();
@@ -1536,8 +1537,8 @@ namespace SabreTools.Library.DatFiles
// If the game has no devices, we continue
if (Items[game][0].Machine.DeviceReferences == null
|| Items[game][0].Machine.DeviceReferences.Count == 0
|| (slotoptions && Items[game][0].Machine.SlotOptions == null)
|| (slotoptions && Items[game][0].Machine.SlotOptions.Count == 0))
|| (slotoptions && Items[game][0].Machine.Slots == null)
|| (slotoptions && Items[game][0].Machine.Slots.Count == 0))
{
continue;
}
@@ -1577,36 +1578,39 @@ namespace SabreTools.Library.DatFiles
// If we're checking slotoptions too
if (slotoptions)
{
// Determine if the game has any slotoptions or not
List<string> slotopts = Items[game][0].Machine.SlotOptions;
List<string> newslotopts = new List<string>();
foreach (string slotopt in slotopts)
// Determine if the game has any slots or not
List<ListXmlSlot> slots = Items[game][0].Machine.Slots;
List<ListXmlSlot> newSlots = new List<ListXmlSlot>();
foreach (ListXmlSlot slot in slots)
{
// If the slotoption doesn't exist then we continue
if (Items[slotopt].Count == 0)
continue;
// Otherwise, copy the items from the slotoption to the current game
DatItem copyFrom = Items[game][0];
List<DatItem> slotItems = Items[slotopt];
foreach (DatItem item in slotItems)
foreach (ListXmlSlotOption slotOption in slot.SlotOptions)
{
DatItem datItem = (DatItem)item.Clone();
newslotopts.AddRange(datItem.Machine.SlotOptions ?? new List<string>());
datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
// If the slotoption doesn't exist then we continue
if (Items[slotOption.Name].Count == 0)
continue;
// Otherwise, copy the items from the slotoption to the current game
DatItem copyFrom = Items[game][0];
List<DatItem> slotItems = Items[slotOption.Name];
foreach (DatItem item in slotItems)
{
foundnew = true;
Items.Add(game, datItem);
DatItem datItem = (DatItem)item.Clone();
newSlots.AddRange(datItem.Machine.Slots ?? new List<ListXmlSlot>());
datItem.CopyMachineInformation(copyFrom);
if (Items[game].Where(i => i.Name.ToLowerInvariant() == datItem.Name.ToLowerInvariant()).Count() == 0)
{
foundnew = true;
Items.Add(game, datItem);
}
}
}
}
// Now that every slotoption is accounted for, add the new list of slotoptions, if they don't already exist
foreach (string slotopt in newslotopts)
// Now that every slotoption is accounted for, add the new list of slots, if they don't already exist
foreach (ListXmlSlot slot in newSlots)
{
if (!Items[game][0].Machine.SlotOptions.Contains(slotopt))
Items[game][0].Machine.SlotOptions.Add(slotopt);
Items[game][0].Machine.Slots.Add(slot);
}
}
}

View File

@@ -550,15 +550,9 @@ namespace SabreTools.Library.DatFiles
}
break;
case "slotoptions":
machine.SlotOptions = new List<string>();
jtr.Read(); // Start Array
while (!sr.EndOfStream && jtr.TokenType != JsonToken.EndArray)
{
machine.SlotOptions.Add(jtr.ReadAsString());
}
break;
// TODO: Add `slot`
case "infos":
machine.Infos = new List<ListXmlInfo>();
jtr.Read(); // Start Array
@@ -654,12 +648,14 @@ namespace SabreTools.Library.DatFiles
if (jtr.TokenType == JsonToken.EndArray)
break;
var sharedFeature = new SoftwareListSharedFeature();
jtr.Read(); // Key
string key = jtr.Value as string;
string value = jtr.ReadAsString();
sharedFeature.Name = jtr.Value as string;
sharedFeature.Value = jtr.ReadAsString();
jtr.Read(); // End object
machine.SharedFeatures.Add(new SoftwareListSharedFeature(key, value));
machine.SharedFeatures.Add(sharedFeature);
}
break;
@@ -850,7 +846,7 @@ namespace SabreTools.Library.DatFiles
datItem.AltName = altName;
datItem.AltTitle = altTitle;
datItem.Original = new OpenMSXOriginal() { Name = original };
datItem.Original = new OpenMSXOriginal() { Content = original };
datItem.OpenMSXSubType = subType;
datItem.OpenMSXType = msxType;
datItem.Remark = remark;
@@ -998,12 +994,14 @@ namespace SabreTools.Library.DatFiles
if (jtr.TokenType == JsonToken.EndArray)
break;
var feature = new SoftwareListFeature();
jtr.Read(); // Key
string key = jtr.Value as string;
string featureValue = jtr.ReadAsString();
feature.Name = jtr.Value as string;
feature.Value = jtr.ReadAsString();
jtr.Read(); // End object
features.Add(new SoftwareListFeature(key, featureValue));
features.Add(feature);
}
break;
@@ -1724,7 +1722,7 @@ namespace SabreTools.Library.DatFiles
break;
}
}
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Devices, Header.ExcludeFields)))
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.DeviceReferences, Header.ExcludeFields)))
{
jtw.WritePropertyName("devices");
jtw.WriteStartArray();
@@ -1735,17 +1733,9 @@ namespace SabreTools.Library.DatFiles
jtw.WriteEndArray();
}
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.SlotOptions, Header.ExcludeFields)))
{
jtw.WritePropertyName("slotoptions");
jtw.WriteStartArray();
foreach (string slotoption in datItem.Machine.SlotOptions)
{
jtw.WriteValue(slotoption);
}
jtw.WriteEndArray();
}
// TODO: Add Field.Slots
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Infos, Header.ExcludeFields)))
{
jtw.WritePropertyName("infos");
@@ -2231,7 +2221,7 @@ namespace SabreTools.Library.DatFiles
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.Original, Header.ExcludeFields)))
{
jtw.WritePropertyName("original");
jtw.WriteValue(datItem.Original.Name);
jtw.WriteValue(datItem.Original.Content);
}
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.OpenMSXSubType, Header.ExcludeFields)))
{

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
@@ -125,10 +126,6 @@ namespace SabreTools.Library.DatFiles
// Otherwise, add what is possible
reader.MoveToContent();
string key = string.Empty;
string temptype = reader.Name;
bool containsItems = false;
// Create a new machine
MachineType machineType = MachineType.NULL;
if (reader.GetAttribute("isbios").AsYesNo() == true)
@@ -143,21 +140,29 @@ namespace SabreTools.Library.DatFiles
Machine machine = new Machine
{
Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("name"),
SourceFile = reader.GetAttribute("sourcefile"),
Runnable = reader.GetAttribute("runnable").AsRunnable(),
Comment = string.Empty,
Description = reader.GetAttribute("name"),
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
RomOf = reader.GetAttribute("romof") ?? string.Empty,
SampleOf = reader.GetAttribute("sampleof") ?? string.Empty,
DeviceReferences = new List<ListXmlDeviceReference>(),
SlotOptions = new List<string>(),
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
SourceFile = reader.GetAttribute("sourcefile"),
Runnable = reader.GetAttribute("runnable").AsRunnable(),
DeviceReferences = new List<ListXmlDeviceReference>(),
Chips = new List<ListXmlChip>(),
Displays = new List<ListXmlDisplay>(),
Sounds = new List<ListXmlSound>(),
Conditions = new List<ListXmlCondition>(),
Inputs = new List<ListXmlInput>(),
DipSwitches = new List<ListXmlDipSwitch>(),
Configurations = new List<ListXmlConfiguration>(),
Slots = new List<ListXmlSlot>(),
};
// Get list for new DatItems
List<DatItem> datItems = new List<DatItem>();
while (!reader.EOF)
{
// We only want elements
@@ -183,9 +188,7 @@ namespace SabreTools.Library.DatFiles
break;
case "biosset":
containsItems = true;
DatItem biosset = new BiosSet
datItems.Add(new BiosSet
{
Name = reader.GetAttribute("name"),
Description = reader.GetAttribute("description"),
@@ -196,20 +199,13 @@ namespace SabreTools.Library.DatFiles
Index = indexId,
Name = filename,
},
};
biosset.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(biosset);
});
reader.Read();
break;
case "rom":
containsItems = true;
DatItem rom = new Rom
datItems.Add(new Rom
{
Name = reader.GetAttribute("name"),
Bios = reader.GetAttribute("bios"),
@@ -234,20 +230,13 @@ namespace SabreTools.Library.DatFiles
Index = indexId,
Name = filename,
},
};
rom.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(rom);
});
reader.Read();
break;
case "disk":
containsItems = true;
DatItem disk = new Disk
datItems.Add(new Disk
{
Name = reader.GetAttribute("name"),
MD5 = reader.GetAttribute("md5"),
@@ -270,18 +259,12 @@ namespace SabreTools.Library.DatFiles
Index = indexId,
Name = filename,
},
};
disk.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(disk);
});
reader.Read();
break;
case "device_ref":
// TODO: Use these device references
var deviceReference = new ListXmlDeviceReference();
deviceReference.Name = reader.GetAttribute("name");
@@ -291,9 +274,7 @@ namespace SabreTools.Library.DatFiles
break;
case "sample":
containsItems = true;
DatItem samplerom = new Sample
datItems.Add(new Sample
{
Name = reader.GetAttribute("name"),
@@ -302,29 +283,24 @@ namespace SabreTools.Library.DatFiles
Index = indexId,
Name = filename,
},
};
samplerom.CopyMachineInformation(machine);
// Now process and add the rom
key = ParseAddHelper(samplerom);
});
reader.Read();
break;
case "chip":
// TODO: Use these chips
var chip = new ListXmlChip();
chip.Name = reader.GetAttribute("name");
chip.Tag = reader.GetAttribute("tag");
chip.Type = reader.GetAttribute("type");
chip.Clock = reader.GetAttribute("clock");
machine.Chips.Add(chip);
reader.Read();
break;
case "display":
// TODO: Use these displays
var display = new ListXmlDisplay();
display.Tag = reader.GetAttribute("tag");
display.Type = reader.GetAttribute("type");
@@ -341,30 +317,33 @@ namespace SabreTools.Library.DatFiles
display.VBend = reader.GetAttribute("vbend");
display.VStart = reader.GetAttribute("vstart");
machine.Displays.Add(display);
reader.Read();
break;
case "sound":
// TODO: Use these sounds
var sound = new ListXmlSound();
sound.Channels = reader.GetAttribute("channels");
machine.Sounds.Add(sound);
reader.Read();
break;
case "condition":
// TODO: Use these conditions
var condition = new ListXmlCondition();
condition.Tag = reader.GetAttribute("tag");
condition.Mask = reader.GetAttribute("mask");
condition.Relation = reader.GetAttribute("relation");
condition.Value = reader.GetAttribute("value");
machine.Conditions.Add(condition);
reader.Read();
break;
case "input":
// TODO: Use these inputs
var input = new ListXmlInput();
input.Service = reader.GetAttribute("service").AsYesNo();
input.Tilt = reader.GetAttribute("tilt").AsYesNo();
@@ -374,12 +353,13 @@ namespace SabreTools.Library.DatFiles
// Now read the internal tags
ReadInput(reader.ReadSubtree(), input);
machine.Inputs.Add(input);
// Skip the input now that we've processed it
reader.Skip();
break;
case "dipswitch":
// TODO: Use these dipswitches
var dipSwitch = new ListXmlDipSwitch();
dipSwitch.Name = reader.GetAttribute("name");
dipSwitch.Tag = reader.GetAttribute("tag");
@@ -388,12 +368,13 @@ namespace SabreTools.Library.DatFiles
// Now read the internal tags
ReadDipSwitch(reader.ReadSubtree(), dipSwitch);
machine.DipSwitches.Add(dipSwitch);
// Skip the dipswitch now that we've processed it
reader.Skip();
break;
case "configuration":
// TODO: Use these configurations
var configuration = new ListXmlConfiguration();
configuration.Name = reader.GetAttribute("name");
configuration.Tag = reader.GetAttribute("tag");
@@ -402,6 +383,8 @@ namespace SabreTools.Library.DatFiles
// Now read the internal tags
ReadConfiguration(reader.ReadSubtree(), configuration);
machine.Configurations.Add(configuration);
// Skip the configuration now that we've processed it
reader.Skip();
break;
@@ -451,6 +434,7 @@ namespace SabreTools.Library.DatFiles
reader.Read();
break;
case "device":
// TODO: Use these devices
var device = new ListXmlDevice();
@@ -468,12 +452,12 @@ namespace SabreTools.Library.DatFiles
break;
case "slot":
// TODO: Use these slots
var slot = new ListXmlSlot();
slot.Name = reader.GetAttribute("name");
// Now read the internal tags
ReadSlot(reader.ReadSubtree(), slot, machine);
machine.Slots.Add(slot);
// Skip the slot now that we've processed it
reader.Skip();
@@ -503,8 +487,18 @@ namespace SabreTools.Library.DatFiles
}
}
// If we found items, copy the machine info and add them
if (datItems.Any())
{
foreach (DatItem datItem in datItems)
{
datItem.CopyMachineInformation(machine);
ParseAddHelper(datItem);
}
}
// If no items were found for this machine, add a Blank placeholder
if (!containsItems)
else
{
Blank blank = new Blank()
{
@@ -558,10 +552,6 @@ namespace SabreTools.Library.DatFiles
slotOption.DeviceName = reader.GetAttribute("devname");
slotOption.Default = reader.GetAttribute("default").AsYesNo();
// TODO: Retire this direct machine setter
if (!machine.SlotOptions.Contains(slotOption.DeviceName))
machine.SlotOptions.Add(slotOption.DeviceName);
slot.SlotOptions.Add(slotOption);
reader.Read();

View File

@@ -250,7 +250,7 @@ namespace SabreTools.Library.DatFiles
case "original":
original = new OpenMSXOriginal();
original.Value = reader.GetAttribute("value").AsYesNo();
original.Name = reader.ReadElementContentAsString();
original.Content = reader.ReadElementContentAsString();
break;
default:
@@ -730,7 +730,7 @@ namespace SabreTools.Library.DatFiles
{
xtw.WriteStartElement("original");
xtw.WriteAttributeString("value", rom.Original.Value == true ? "true" : "false");
xtw.WriteString(rom.Original.Name);
xtw.WriteString(rom.Original.Content);
xtw.WriteEndElement();
}

View File

@@ -389,15 +389,7 @@ namespace SabreTools.Library.DatFiles
break;
case "Machine.SlotOptions":
machine.SlotOptions = new List<string>();
var slotOptions = value.Split(';');
foreach (var slotOption in slotOptions)
{
machine.SlotOptions.Add(slotOption);
}
break;
// TODO: Add Machine.Slot
case "Machine.Infos":
machine.Infos = new List<ListXmlInfo>();
@@ -494,18 +486,20 @@ namespace SabreTools.Library.DatFiles
case "Machine.SharedFeatures":
machine.SharedFeatures = new List<SoftwareListSharedFeature>();
var sharedFeatures = value.Split(';');
foreach (var sharedFeature in sharedFeatures)
foreach (var pair in sharedFeatures)
{
var featurePair = sharedFeature.Split('=');
machine.SharedFeatures.Add(new SoftwareListSharedFeature(featurePair[0], featurePair[1]));
var featurePair = pair.Split('=');
var sharedFeature = new SoftwareListSharedFeature();
sharedFeature.Name = featurePair[0];
sharedFeature.Value = featurePair[1];
machine.SharedFeatures.Add(sharedFeature);
}
break;
case "Machine.DipSwitches":
machine.DipSwitches = new List<ListXmlDipSwitch>();
// TODO: There is no way this would work... Just use empty for now
break;
// TODO: Implement Machine.DipSwitches
#endregion
@@ -577,7 +571,12 @@ namespace SabreTools.Library.DatFiles
foreach (var splitFeature in splitFeatures)
{
var featurePair = splitFeature.Split('=');
features.Add(new SoftwareListFeature(featurePair[0], featurePair[1]));
var feature = new SoftwareListFeature();
feature.Name = featurePair[0];
feature.Value = featurePair[1];
features.Add(feature);
}
break;
@@ -720,7 +719,7 @@ namespace SabreTools.Library.DatFiles
AltName = altName,
AltTitle = altTitle,
Original = new OpenMSXOriginal() { Name = original },
Original = new OpenMSXOriginal() { Content = original },
OpenMSXSubType = subType,
OpenMSXType = msxType,
Remark = remark,
@@ -755,7 +754,7 @@ namespace SabreTools.Library.DatFiles
AltName = altName,
AltTitle = altTitle,
Original = new OpenMSXOriginal() { Name = original },
Original = new OpenMSXOriginal() { Content = original },
OpenMSXSubType = subType,
OpenMSXType = msxType,
Remark = remark,
@@ -793,7 +792,7 @@ namespace SabreTools.Library.DatFiles
AltName = altName,
AltTitle = altTitle,
Original = new OpenMSXOriginal() { Name = original },
Original = new OpenMSXOriginal() { Content = original },
OpenMSXSubType = subType,
OpenMSXType = msxType,
Remark = remark,
@@ -843,7 +842,7 @@ namespace SabreTools.Library.DatFiles
AltName = altName,
AltTitle = altTitle,
Original = new OpenMSXOriginal() { Name = original },
Original = new OpenMSXOriginal() { Content = original },
OpenMSXSubType = subType,
OpenMSXType = msxType,
Remark = remark,
@@ -883,7 +882,7 @@ namespace SabreTools.Library.DatFiles
AltName = altName,
AltTitle = altTitle,
Original = new OpenMSXOriginal() { Name = original },
Original = new OpenMSXOriginal() { Content = original },
OpenMSXSubType = subType,
OpenMSXType = msxType,
Remark = remark,
@@ -937,7 +936,7 @@ namespace SabreTools.Library.DatFiles
AltName = altName,
AltTitle = altTitle,
Original = new OpenMSXOriginal() { Name = original },
Original = new OpenMSXOriginal() { Content = original },
OpenMSXSubType = subType,
OpenMSXType = msxType,
Remark = remark,
@@ -1265,10 +1264,7 @@ namespace SabreTools.Library.DatFiles
case "devices":
return "Machine.Devices";
case "slotoptions":
case "slot options":
case "slot-options":
return "Machine.SlotOptions";
// TODO: Add Machine.Slot
case "infos":
return "Machine.Infos";

View File

@@ -191,7 +191,12 @@ namespace SabreTools.Library.DatFiles
break;
case "sharedfeat":
machine.SharedFeatures.Add(new SoftwareListSharedFeature(reader.GetAttribute("name"), reader.GetAttribute("value")));
var sharedFeature = new SoftwareListSharedFeature();
sharedFeature.Name = reader.GetAttribute("name");
sharedFeature.Value = reader.GetAttribute("value");
machine.SharedFeatures.Add(sharedFeature);
reader.Read();
break;
@@ -284,7 +289,12 @@ namespace SabreTools.Library.DatFiles
break;
case "feature":
features.Add(new SoftwareListFeature(reader.GetAttribute("name"), reader.GetAttribute("value")));
var feature = new SoftwareListFeature();
feature.Name = reader.GetAttribute("name");
feature.Value = reader.GetAttribute("value");
features.Add(feature);
reader.Read();
break;