Wire up partial support for dipswitches

This commit is contained in:
Matt Nadareski
2020-08-21 16:48:56 -07:00
parent 4d7a4373a9
commit 755b6f030b
8 changed files with 242 additions and 12 deletions

View File

@@ -648,7 +648,50 @@ namespace SabreTools.Library.DatFiles
break;
#endregion
case "dipswitches":
machine.DipSwitches = new List<SoftwareListDipSwitch>();
jtr.Read(); // Start Array
while (!sr.EndOfStream)
{
jtr.Read(); // Start object (or end array)
if (jtr.TokenType == JsonToken.EndArray)
break;
jtr.Read(); // Name Key
string name = jtr.ReadAsString();
jtr.Read(); // Tag Key
string tag = jtr.ReadAsString();
jtr.Read(); // Mask Key
string mask = jtr.ReadAsString();
var dip = new SoftwareListDipSwitch(name, tag, mask);
jtr.Read(); // Start dipvalues object
while (!sr.EndOfStream)
{
jtr.Read(); // Start object (or end array)
if (jtr.TokenType == JsonToken.EndArray)
break;
jtr.Read(); // Name Key
string valname = jtr.ReadAsString();
jtr.Read(); // Value Key
string value = jtr.ReadAsString();
jtr.Read(); // Default Key
bool? def = jtr.ReadAsString().AsYesNo();
jtr.Read(); // End object
dip.Values.Add(new SoftwareListDipValue(valname, value, def));
}
jtr.Read(); // End object
machine.DipSwitches.Add(dip);
}
break;
#endregion
default:
break;
@@ -1744,6 +1787,41 @@ namespace SabreTools.Library.DatFiles
jtw.WriteEndArray();
}
if (!string.IsNullOrWhiteSpace(datItem.GetField(Field.DipSwitches, Header.ExcludeFields)))
{
jtw.WritePropertyName("dipswitches");
jtw.WriteStartArray();
foreach (var dip in datItem.Machine.DipSwitches)
{
jtw.WriteStartObject();
jtw.WritePropertyName("name");
jtw.WriteValue(dip.Name);
jtw.WritePropertyName("tag");
jtw.WriteValue(dip.Tag);
jtw.WritePropertyName("mask");
jtw.WriteValue(dip.Mask);
jtw.WriteStartArray();
foreach (SoftwareListDipValue dipval in dip.Values)
{
jtw.WriteStartObject();
jtw.WritePropertyName("name");
jtw.WriteValue(dipval.Name);
jtw.WritePropertyName("value");
jtw.WriteValue(dipval.Value);
jtw.WritePropertyName("default");
jtw.WriteValue(dipval.Default == true ? "yes" : "no");
jtw.WriteEndObject();
}
jtw.WriteEndArray();
// End dipswitch
jtw.WriteEndObject();
}
jtw.WriteEndArray();
}
#endregion

View File

@@ -476,6 +476,11 @@ namespace SabreTools.Library.DatFiles
break;
case "Machine.DipSwitches":
machine.DipSwitches = new List<SoftwareListDipSwitch>();
// TODO: There is no way this would work... Just use empty for now
break;
#endregion
#endregion // Machine
@@ -1248,6 +1253,13 @@ namespace SabreTools.Library.DatFiles
case "shared features":
case "shared-features":
return "Machine.SharedFeatures";
case "dipswitch":
case "dip switch":
case "dip-switch":
case "dipswitches":
case "dip switches":
case "dip-switches":
return "Machine.DipSwitches";
#endregion

View File

@@ -147,6 +147,7 @@ namespace SabreTools.Library.DatFiles
CloneOf = reader.GetAttribute("cloneof") ?? string.Empty,
Infos = new List<ListXmlInfo>(),
SharedFeatures = new List<SoftwareListSharedFeature>(),
DipSwitches = new List<SoftwareListDipSwitch>(),
MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType),
};
@@ -247,6 +248,7 @@ namespace SabreTools.Library.DatFiles
areaEndinaness;
long? areasize = null;
var features = new List<SoftwareListFeature>();
var dipswitches = new List<SoftwareListDipSwitch>();
bool containsItems = false;
while (!reader.EOF)
@@ -331,16 +333,15 @@ namespace SabreTools.Library.DatFiles
break;
case "dipswitch":
// TODO: Read dipswitches
// string dipswitch_name = reader.GetAttribute("name");
// string dipswitch_tag = reader.GetAttribute("tag");
// string dipswitch_mask = reader.GetAttribute("mask");
// For every <dipvalue> element...
// string dipvalue_name = reader.GetAttribute("name");
// string dipvalue_value = reader.GetAttribute("value");
// bool? dipvalue_default = Utilities.GetYesNo(reader.GetAttribute("default")); // (yes|no) "no"
var dip = new SoftwareListDipSwitch(
reader.GetAttribute("name"),
reader.GetAttribute("tag"),
reader.GetAttribute("mask"));
dip.Values = ReadDipSwitch(reader.ReadSubtree());
dipswitches.Add(dip);
// Skip the dipswitch now that we've processed it
reader.Skip();
break;
@@ -563,6 +564,50 @@ namespace SabreTools.Library.DatFiles
return containsItems;
}
/// <summary>
/// Read DipSwitch DipValues information
/// </summary>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
private List<SoftwareListDipValue> ReadDipSwitch(XmlReader reader)
{
// If we have an empty trurip, skip it
if (reader == null)
return null;
// Get list ready
List<SoftwareListDipValue> dipValues = new List<SoftwareListDipValue>();
// Otherwise, add what is possible
reader.MoveToContent();
while (!reader.EOF)
{
// We only want elements
if (reader.NodeType != XmlNodeType.Element)
{
reader.Read();
continue;
}
// Get the information from the dipswitch
switch (reader.Name)
{
case "dipvalue":
dipValues.Add(new SoftwareListDipValue(
reader.GetAttribute("name"),
reader.GetAttribute("value"),
reader.GetAttribute("default").AsYesNo()));
break;
default:
reader.Read();
break;
}
}
return dipValues;
}
/// <summary>
/// Create and open an output file for writing direct from a dictionary
/// </summary>
@@ -785,6 +830,29 @@ namespace SabreTools.Library.DatFiles
}
}
if (!Header.ExcludeFields.Contains(Field.DipSwitches) && datItem.Machine.DipSwitches != null && datItem.Machine.DipSwitches.Count > 0)
{
foreach (SoftwareListDipSwitch dip in datItem.Machine.DipSwitches)
{
xtw.WriteStartElement("dipswitch");
xtw.WriteAttributeString("name", dip.Name);
xtw.WriteAttributeString("tag", dip.Tag);
xtw.WriteAttributeString("mask", dip.Mask);
foreach (SoftwareListDipValue dipval in dip.Values)
{
xtw.WriteStartElement("dipvalue");
xtw.WriteAttributeString("name", dipval.Name);
xtw.WriteAttributeString("value", dipval.Value);
xtw.WriteAttributeString("default", dipval.Default == true ? "yes" : "no");
xtw.WriteEndElement();
}
// End dipswitch
xtw.WriteEndElement();
}
}
xtw.Flush();
}
catch (Exception ex)

View File

@@ -1,4 +1,6 @@
/// <summary>
using System.Collections.Generic;
/// <summary>
/// This holds all of the auxiliary types needed for proper parsing
/// </summary>
namespace SabreTools.Library.DatItems
@@ -26,6 +28,42 @@ namespace SabreTools.Library.DatItems
#region SoftwareList
/// <summary>
/// Represents one SoftwareList dipswitch
/// </summary>
public class SoftwareListDipSwitch
{
public string Name { get; set; }
public string Tag { get; set; }
public string Mask { get; set; }
public List<SoftwareListDipValue> Values { get; set; }
public SoftwareListDipSwitch(string name, string tag, string mask)
{
Name = name;
Tag = tag;
Mask = mask;
Values = new List<SoftwareListDipValue>();
}
}
/// <summary>
/// Represents one SoftwareList dipswitch
/// </summary>
public class SoftwareListDipValue
{
public string Name { get; set; }
public string Value { get; set; }
public bool? Default { get; set; }
public SoftwareListDipValue(string name, string value, bool? def)
{
Name = name;
Value = value;
Default = def;
}
}
/// <summary>
/// Represents one SoftwareList shared feature object
/// </summary>

View File

@@ -254,6 +254,7 @@ namespace SabreTools.Library.DatItems
// SoftwareList
Field.Supported,
Field.SharedFeatures,
Field.DipSwitches,
};
#endregion

View File

@@ -91,6 +91,7 @@ namespace SabreTools.Library.DatItems
Supported,
SharedFeatures,
DipSwitches,
#endregion

View File

@@ -257,10 +257,16 @@ namespace SabreTools.Library.DatItems
/// <summary>
/// List of shared feature items
/// </summary>
/// <remarks>Also in SoftwareList</remarks>
[JsonProperty("sharedfeat")]
public List<SoftwareListSharedFeature> SharedFeatures { get; set; } = null;
/// <summary>
/// List of shared feature items
/// </summary>
/// <remarks>Also in SoftwareList</remarks>
[JsonProperty("dipswitches")]
public List<SoftwareListDipSwitch> DipSwitches { get; set; } = null;
#endregion
#endregion
@@ -414,6 +420,10 @@ namespace SabreTools.Library.DatItems
case Field.SharedFeatures:
fieldValue = string.Join(";", (SharedFeatures ?? new List<SoftwareListSharedFeature>()).Select(i => $"{i.Name}={i.Value}"));
break;
case Field.DipSwitches:
// TODO: There is no possible way this will work... use placeholder for now
fieldValue = "dipswitches";
break;
#endregion
@@ -596,6 +606,14 @@ namespace SabreTools.Library.DatItems
}
}
if (mappings.Keys.Contains(Field.DipSwitches))
{
if (DipSwitches == null)
DipSwitches = new List<SoftwareListDipSwitch>();
// TODO: There's no way this will work... just create the new list for now
}
#endregion
}
@@ -696,6 +714,7 @@ namespace SabreTools.Library.DatItems
Supported = this.Supported,
SharedFeatures = this.SharedFeatures,
DipSwitches = this.DipSwitches,
#endregion
};
@@ -1104,6 +1123,9 @@ namespace SabreTools.Library.DatItems
if (fields.Contains(Field.SharedFeatures))
SharedFeatures = null;
if (fields.Contains(Field.DipSwitches))
DipSwitches = null;
#endregion
}
@@ -1252,6 +1274,9 @@ namespace SabreTools.Library.DatItems
if (fields.Contains(Field.SharedFeatures))
SharedFeatures = machine.SharedFeatures;
if (fields.Contains(Field.DipSwitches))
DipSwitches = machine.DipSwitches;
#endregion
}

View File

@@ -307,6 +307,13 @@ namespace SabreTools.Library.Tools
case "shared features":
case "shared-features":
return Field.SharedFeatures;
case "dipswitch":
case "dip switch":
case "dip-switch":
case "dipswitches":
case "dip switches":
case "dip-switches":
return Field.DipSwitches;
#endregion