diff --git a/SabreTools.Library/DatFiles/Json.cs b/SabreTools.Library/DatFiles/Json.cs index f06091c7..b2ba4257 100644 --- a/SabreTools.Library/DatFiles/Json.cs +++ b/SabreTools.Library/DatFiles/Json.cs @@ -648,7 +648,50 @@ namespace SabreTools.Library.DatFiles break; - #endregion + case "dipswitches": + machine.DipSwitches = new List(); + 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 diff --git a/SabreTools.Library/DatFiles/SeparatedValue.cs b/SabreTools.Library/DatFiles/SeparatedValue.cs index f6da6897..b380827c 100644 --- a/SabreTools.Library/DatFiles/SeparatedValue.cs +++ b/SabreTools.Library/DatFiles/SeparatedValue.cs @@ -476,6 +476,11 @@ namespace SabreTools.Library.DatFiles break; + case "Machine.DipSwitches": + machine.DipSwitches = new List(); + // 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 diff --git a/SabreTools.Library/DatFiles/SoftwareList.cs b/SabreTools.Library/DatFiles/SoftwareList.cs index 0a69788d..59f4409a 100644 --- a/SabreTools.Library/DatFiles/SoftwareList.cs +++ b/SabreTools.Library/DatFiles/SoftwareList.cs @@ -147,6 +147,7 @@ namespace SabreTools.Library.DatFiles CloneOf = reader.GetAttribute("cloneof") ?? string.Empty, Infos = new List(), SharedFeatures = new List(), + DipSwitches = new List(), MachineType = (machineType == MachineType.NULL ? MachineType.None : machineType), }; @@ -247,6 +248,7 @@ namespace SabreTools.Library.DatFiles areaEndinaness; long? areasize = null; var features = new List(); + var dipswitches = new List(); 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 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; } + /// + /// Read DipSwitch DipValues information + /// + /// True if full pathnames are to be kept, false otherwise (default) + private List ReadDipSwitch(XmlReader reader) + { + // If we have an empty trurip, skip it + if (reader == null) + return null; + + // Get list ready + List dipValues = new List(); + + // 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; + } + /// /// Create and open an output file for writing direct from a dictionary /// @@ -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) diff --git a/SabreTools.Library/DatItems/Auxiliary.cs b/SabreTools.Library/DatItems/Auxiliary.cs index 83bb8cc5..0c35f599 100644 --- a/SabreTools.Library/DatItems/Auxiliary.cs +++ b/SabreTools.Library/DatItems/Auxiliary.cs @@ -1,4 +1,6 @@ -/// +using System.Collections.Generic; + +/// /// This holds all of the auxiliary types needed for proper parsing /// namespace SabreTools.Library.DatItems @@ -26,6 +28,42 @@ namespace SabreTools.Library.DatItems #region SoftwareList + /// + /// Represents one SoftwareList dipswitch + /// + public class SoftwareListDipSwitch + { + public string Name { get; set; } + public string Tag { get; set; } + public string Mask { get; set; } + public List Values { get; set; } + + public SoftwareListDipSwitch(string name, string tag, string mask) + { + Name = name; + Tag = tag; + Mask = mask; + Values = new List(); + } + } + + /// + /// Represents one SoftwareList dipswitch + /// + 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; + } + } + /// /// Represents one SoftwareList shared feature object /// diff --git a/SabreTools.Library/DatItems/DatItem.cs b/SabreTools.Library/DatItems/DatItem.cs index e6f4e285..ec0bd2eb 100644 --- a/SabreTools.Library/DatItems/DatItem.cs +++ b/SabreTools.Library/DatItems/DatItem.cs @@ -254,6 +254,7 @@ namespace SabreTools.Library.DatItems // SoftwareList Field.Supported, Field.SharedFeatures, + Field.DipSwitches, }; #endregion diff --git a/SabreTools.Library/DatItems/Enums.cs b/SabreTools.Library/DatItems/Enums.cs index f03fb88b..9d923def 100644 --- a/SabreTools.Library/DatItems/Enums.cs +++ b/SabreTools.Library/DatItems/Enums.cs @@ -91,6 +91,7 @@ namespace SabreTools.Library.DatItems Supported, SharedFeatures, + DipSwitches, #endregion diff --git a/SabreTools.Library/DatItems/Machine.cs b/SabreTools.Library/DatItems/Machine.cs index cf7fc187..4cba6c45 100644 --- a/SabreTools.Library/DatItems/Machine.cs +++ b/SabreTools.Library/DatItems/Machine.cs @@ -257,10 +257,16 @@ namespace SabreTools.Library.DatItems /// /// List of shared feature items /// - /// Also in SoftwareList [JsonProperty("sharedfeat")] public List SharedFeatures { get; set; } = null; + /// + /// List of shared feature items + /// + /// Also in SoftwareList + [JsonProperty("dipswitches")] + public List DipSwitches { get; set; } = null; + #endregion #endregion @@ -414,6 +420,10 @@ namespace SabreTools.Library.DatItems case Field.SharedFeatures: fieldValue = string.Join(";", (SharedFeatures ?? new List()).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(); + + // 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 } diff --git a/SabreTools.Library/Tools/Converters.cs b/SabreTools.Library/Tools/Converters.cs index 677a7bc3..22e41e85 100644 --- a/SabreTools.Library/Tools/Converters.cs +++ b/SabreTools.Library/Tools/Converters.cs @@ -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