diff --git a/SabreTools.Library/DatFiles/Listxml.cs b/SabreTools.Library/DatFiles/Listxml.cs index e0defb1c..c09d7e69 100644 --- a/SabreTools.Library/DatFiles/Listxml.cs +++ b/SabreTools.Library/DatFiles/Listxml.cs @@ -699,56 +699,20 @@ namespace SabreTools.Library.DatFiles case "control": var control = new Control { - ControlType = reader.GetAttribute("type"), - RegButtons = reader.GetAttribute("regbuttons"), + ControlType = reader.GetAttribute("type").AsControlType(), + Player = Sanitizer.CleanLong(reader.GetAttribute("player")), + Buttons = Sanitizer.CleanLong(reader.GetAttribute("buttons")), + RequiredButtons = Sanitizer.CleanLong(reader.GetAttribute("reqbuttons")), + Minimum = Sanitizer.CleanLong(reader.GetAttribute("minimum")), + Maximum = Sanitizer.CleanLong(reader.GetAttribute("maximum")), + Sensitivity = Sanitizer.CleanLong(reader.GetAttribute("sensitivity")), + KeyDelta = Sanitizer.CleanLong(reader.GetAttribute("keydelta")), Reverse = reader.GetAttribute("reverse").AsYesNo(), Ways = reader.GetAttribute("ways"), Ways2 = reader.GetAttribute("ways2"), Ways3 = reader.GetAttribute("ways3"), }; - // Set the player - if (reader.GetAttribute("player") != null) - { - if (Int64.TryParse(reader.GetAttribute("player"), out long player)) - control.Player = player; - } - - // Set the buttons - if (reader.GetAttribute("buttons") != null) - { - if (Int64.TryParse(reader.GetAttribute("buttons"), out long buttons)) - control.Buttons = buttons; - } - - // Set the minimum - if (reader.GetAttribute("minimum") != null) - { - if (Int64.TryParse(reader.GetAttribute("minimum"), out long minimum)) - control.Minimum = minimum; - } - - // Set the maximum - if (reader.GetAttribute("maximum") != null) - { - if (Int64.TryParse(reader.GetAttribute("maximum"), out long maximum)) - control.Maximum = maximum; - } - - // Set the sensitivity - if (reader.GetAttribute("sensitivity") != null) - { - if (Int64.TryParse(reader.GetAttribute("sensitivity"), out long sensitivity)) - control.Sensitivity = sensitivity; - } - - // Set the keydelta - if (reader.GetAttribute("keydelta") != null) - { - if (Int64.TryParse(reader.GetAttribute("keydelta"), out long keyDelta)) - control.KeyDelta = keyDelta; - } - input.Controls.Add(control); reader.Read(); @@ -1608,10 +1572,10 @@ namespace SabreTools.Library.DatFiles foreach (var control in input.Controls) { xtw.WriteStartElement("control"); - xtw.WriteOptionalAttributeString("type", control.ControlType); + xtw.WriteOptionalAttributeString("type", control.ControlType.FromControlType()); xtw.WriteOptionalAttributeString("player", control.Player?.ToString()); xtw.WriteOptionalAttributeString("buttons", control.Buttons?.ToString()); - xtw.WriteOptionalAttributeString("regbuttons", control.RegButtons); + xtw.WriteOptionalAttributeString("reqbuttons", control.RequiredButtons?.ToString()); xtw.WriteOptionalAttributeString("minimum", control.Minimum?.ToString()); xtw.WriteOptionalAttributeString("maximum", control.Maximum?.ToString()); xtw.WriteOptionalAttributeString("sensitivity", control.Sensitivity?.ToString()); diff --git a/SabreTools.Library/DatFiles/OfflineList.cs b/SabreTools.Library/DatFiles/OfflineList.cs index 32e81d14..94317f3c 100644 --- a/SabreTools.Library/DatFiles/OfflineList.cs +++ b/SabreTools.Library/DatFiles/OfflineList.cs @@ -466,7 +466,7 @@ namespace SabreTools.Library.DatFiles { // Prepare all internal variables string releaseNumber = string.Empty, duplicateid; - long size = -1; + long? size = null; List datItems = new List(); Machine machine = new Machine(); @@ -510,9 +510,7 @@ namespace SabreTools.Library.DatFiles break; case "romsize": - if (!Int64.TryParse(reader.ReadElementContentAsString(), out size)) - size = -1; - + size = Sanitizer.CleanLong(reader.ReadElementContentAsString()); break; case "publisher": diff --git a/SabreTools.Library/DatFiles/RomCenter.cs b/SabreTools.Library/DatFiles/RomCenter.cs index 673ba0a9..23c15a7b 100644 --- a/SabreTools.Library/DatFiles/RomCenter.cs +++ b/SabreTools.Library/DatFiles/RomCenter.cs @@ -7,6 +7,7 @@ using System.Text; using SabreTools.Library.Data; using SabreTools.Library.DatItems; using SabreTools.Library.IO; +using SabreTools.Library.Tools; namespace SabreTools.Library.DatFiles { @@ -328,15 +329,10 @@ namespace SabreTools.Library.DatFiles 9 - merge name */ string[] rominfo = line.Split('¬'); - - // Try getting the size separately - if (!Int64.TryParse(rominfo[7], out long size)) - size = 0; - Rom rom = new Rom { Name = rominfo[5], - Size = size, + Size = Sanitizer.CleanLong(rominfo[7]), CRC = rominfo[6], ItemStatus = ItemStatus.None, diff --git a/SabreTools.Library/DatFiles/SabreDat.cs b/SabreTools.Library/DatFiles/SabreDat.cs index dc372fc0..edb8809b 100644 --- a/SabreTools.Library/DatFiles/SabreDat.cs +++ b/SabreTools.Library/DatFiles/SabreDat.cs @@ -400,6 +400,7 @@ namespace SabreTools.Library.DatFiles Name = reader.GetAttribute("name"), Tag = reader.GetAttribute("tag"), ChipType = reader.GetAttribute("chiptype").AsChipType(), + Clock = Sanitizer.CleanLong(reader.GetAttribute("clock")), Source = new Source { @@ -408,13 +409,6 @@ namespace SabreTools.Library.DatFiles }, }; - // Set the clock - if (reader.GetAttribute("clock") != null) - { - if (Int64.TryParse(reader.GetAttribute("clock"), out long clock)) - (datItem as Chip).Clock = clock; - } - break; case "configuration": @@ -1504,10 +1498,10 @@ namespace SabreTools.Library.DatFiles foreach (var control in input.Controls) { xtw.WriteStartElement("control"); - xtw.WriteOptionalAttributeString("type", control.ControlType); + xtw.WriteOptionalAttributeString("type", control.ControlType.FromControlType()); xtw.WriteOptionalAttributeString("player", control.Player?.ToString()); xtw.WriteOptionalAttributeString("buttons", control.Buttons?.ToString()); - xtw.WriteOptionalAttributeString("regbuttons", control.RegButtons); + xtw.WriteOptionalAttributeString("reqbuttons", control.RequiredButtons?.ToString()); xtw.WriteOptionalAttributeString("minimum", control.Minimum?.ToString()); xtw.WriteOptionalAttributeString("maximum", control.Maximum?.ToString()); xtw.WriteOptionalAttributeString("sensitivity", control.Sensitivity?.ToString()); diff --git a/SabreTools.Library/DatFiles/SoftwareList.cs b/SabreTools.Library/DatFiles/SoftwareList.cs index 84ad216f..39e6868f 100644 --- a/SabreTools.Library/DatFiles/SoftwareList.cs +++ b/SabreTools.Library/DatFiles/SoftwareList.cs @@ -255,23 +255,11 @@ namespace SabreTools.Library.DatFiles var dataArea = new DataArea { Name = reader.GetAttribute("name"), + Size = Sanitizer.CleanLong(reader.GetAttribute("size")), + Width = Sanitizer.CleanLong(reader.GetAttribute("width")), Endianness = reader.GetAttribute("endianness").AsEndianness(), }; - // Set the size - if (reader.GetAttribute("size") != null) - { - if (Int64.TryParse(reader.GetAttribute("width"), out long size)) - dataArea.Size = size; - } - - // Set the width - if (reader.GetAttribute("width") != null) - { - if (Int64.TryParse(reader.GetAttribute("width"), out long width)) - dataArea.Width = width; - } - List roms = ReadDataArea(reader.ReadSubtree(), dataArea); // If we got valid roms, add them to the list diff --git a/SabreTools.Library/DatItems/Chip.cs b/SabreTools.Library/DatItems/Chip.cs index 5e63f97b..860c81cf 100644 --- a/SabreTools.Library/DatItems/Chip.cs +++ b/SabreTools.Library/DatItems/Chip.cs @@ -76,10 +76,7 @@ namespace SabreTools.Library.DatItems ChipType = mappings[Field.DatItem_ChipType].AsChipType(); if (mappings.Keys.Contains(Field.DatItem_Clock)) - { - if (Int64.TryParse(mappings[Field.DatItem_Clock], out long clock)) - Clock = clock; - } + Clock = Sanitizer.CleanLong(mappings[Field.DatItem_Clock]); } #endregion @@ -198,9 +195,11 @@ namespace SabreTools.Library.DatItems return false; // DatItem_Clock - if (filter.DatItem_Clock.MatchesPositive(null, Clock) == false) + if (filter.DatItem_Clock.MatchesNeutral(null, Clock) == false) return false; - if (filter.DatItem_Clock.MatchesNegative(null, Clock) == true) + else if (filter.DatItem_Clock.MatchesPositive(null, Clock) == false) + return false; + else if (filter.DatItem_Clock.MatchesNegative(null, Clock) == false) return false; return true; diff --git a/SabreTools.Library/DatItems/Control.cs b/SabreTools.Library/DatItems/Control.cs index d0ea2b10..4acdc252 100644 --- a/SabreTools.Library/DatItems/Control.cs +++ b/SabreTools.Library/DatItems/Control.cs @@ -17,56 +17,55 @@ namespace SabreTools.Library.DatItems #region Fields /// - /// Control type + /// General type of input /// [JsonProperty("type", DefaultValueHandling = DefaultValueHandling.Ignore)] - public string ControlType { get; set; } + public ControlType ControlType { get; set; } /// - /// Player ID + /// Player which the input belongs to /// [JsonProperty("player", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? Player { get; set; } /// - /// Button count + /// Total number of buttons /// [JsonProperty("buttons", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? Buttons { get; set; } /// - /// Regular button count + /// Total number of non-optional buttons /// - /// Couldn't find this used in newest ListXML - [JsonProperty("regbuttons", DefaultValueHandling = DefaultValueHandling.Ignore)] - public string RegButtons { get; set; } // TODO: Int32? + [JsonProperty("reqbuttons", DefaultValueHandling = DefaultValueHandling.Ignore)] + public long? RequiredButtons { get; set; } /// - /// Minimum value + /// Analog minimum value /// [JsonProperty("minimum", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? Minimum { get; set; } /// - /// Maximum value + /// Analog maximum value /// [JsonProperty("maximum", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? Maximum { get; set; } /// - /// Sensitivity value + /// Default analog sensitivity /// [JsonProperty("sensitivity", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? Sensitivity { get; set; } /// - /// Keypress delta + /// Default analog keydelta /// [JsonProperty("keydelta", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? KeyDelta { get; set; } /// - /// Determines if the control is reversed + /// Default analog reverse setting /// [JsonProperty("reverse", DefaultValueHandling = DefaultValueHandling.Ignore)] public bool? Reverse { get; set; } @@ -104,46 +103,28 @@ namespace SabreTools.Library.DatItems // Handle Control-specific fields if (mappings.Keys.Contains(Field.DatItem_Control_Type)) - ControlType = mappings[Field.DatItem_Control_Type]; + ControlType = mappings[Field.DatItem_Control_Type].AsControlType(); if (mappings.Keys.Contains(Field.DatItem_Control_Player)) - { - if (Int64.TryParse(mappings[Field.DatItem_Control_Player], out long player)) - Player = player; - } + Player = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Player]); if (mappings.Keys.Contains(Field.DatItem_Control_Buttons)) - { - if (Int64.TryParse(mappings[Field.DatItem_Control_Buttons], out long buttons)) - Buttons = buttons; - } + Buttons = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Buttons]); - if (mappings.Keys.Contains(Field.DatItem_Control_RegButtons)) - RegButtons = mappings[Field.DatItem_Control_RegButtons]; + if (mappings.Keys.Contains(Field.DatItem_Control_RequiredButtons)) + RequiredButtons = Sanitizer.CleanLong(mappings[Field.DatItem_Control_RequiredButtons]); if (mappings.Keys.Contains(Field.DatItem_Control_Minimum)) - { - if (Int64.TryParse(mappings[Field.DatItem_Control_Minimum], out long minimum)) - Minimum = minimum; - } + Minimum = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Minimum]); if (mappings.Keys.Contains(Field.DatItem_Control_Maximum)) - { - if (Int64.TryParse(mappings[Field.DatItem_Control_Maximum], out long maximum)) - Maximum = maximum; - } + Maximum = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Maximum]); if (mappings.Keys.Contains(Field.DatItem_Control_Sensitivity)) - { - if (Int64.TryParse(mappings[Field.DatItem_Control_Sensitivity], out long sensitivity)) - Sensitivity = sensitivity; - } + Sensitivity = Sanitizer.CleanLong(mappings[Field.DatItem_Control_Sensitivity]); if (mappings.Keys.Contains(Field.DatItem_Control_KeyDelta)) - { - if (Int64.TryParse(mappings[Field.DatItem_Control_KeyDelta], out long keyDelta)) - KeyDelta = keyDelta; - } + KeyDelta = Sanitizer.CleanLong(mappings[Field.DatItem_Control_KeyDelta]); if (mappings.Keys.Contains(Field.DatItem_Control_Reverse)) Reverse = mappings[Field.DatItem_Control_Reverse].AsYesNo(); @@ -188,7 +169,7 @@ namespace SabreTools.Library.DatItems ControlType = this.ControlType, Player = this.Player, Buttons = this.Buttons, - RegButtons = this.RegButtons, + RequiredButtons = this.RequiredButtons, Minimum = this.Minimum, Maximum = this.Maximum, Sensitivity = this.Sensitivity, @@ -217,7 +198,7 @@ namespace SabreTools.Library.DatItems return (ControlType == newOther.ControlType && Player == newOther.Player && Buttons == newOther.Buttons - && RegButtons == newOther.RegButtons + && RequiredButtons == newOther.RequiredButtons && Minimum == newOther.Minimum && Maximum == newOther.Maximum && Sensitivity == newOther.Sensitivity @@ -244,51 +225,65 @@ namespace SabreTools.Library.DatItems return false; // Filter on control type - if (filter.DatItem_Control_Type.MatchesPositiveSet(ControlType) == false) + if (filter.DatItem_Control_Type.MatchesPositive(ControlType.NULL, ControlType) == false) return false; - if (filter.DatItem_Control_Type.MatchesNegativeSet(ControlType) == true) + if (filter.DatItem_Control_Type.MatchesNegative(ControlType.NULL, ControlType) == true) return false; - // Filter on display type - if (filter.DatItem_Control_Player.MatchesPositive(null, Player) == false) + // Filter on player + if (filter.DatItem_Control_Player.MatchesNeutral(null, Player) == false) return false; - if (filter.DatItem_Control_Player.MatchesNegative(null, Player) == true) + else if (filter.DatItem_Control_Player.MatchesPositive(null, Player) == false) + return false; + else if (filter.DatItem_Control_Player.MatchesNegative(null, Player) == false) return false; // Filter on buttons - if (filter.DatItem_Control_Buttons.MatchesPositive(null, Buttons) == false) + if (filter.DatItem_Control_Buttons.MatchesNeutral(null, Buttons) == false) return false; - if (filter.DatItem_Control_Buttons.MatchesNegative(null, Buttons) == true) + else if (filter.DatItem_Control_Buttons.MatchesPositive(null, Buttons) == false) + return false; + else if (filter.DatItem_Control_Buttons.MatchesNegative(null, Buttons) == false) return false; - // Filter on regbuttons - if (filter.DatItem_Control_RegButtons.MatchesPositiveSet(RegButtons) == false) + // Filter on reqbuttons + if (filter.DatItem_Control_ReqButtons.MatchesNeutral(null, RequiredButtons) == false) return false; - if (filter.DatItem_Control_RegButtons.MatchesNegativeSet(RegButtons) == true) + else if (filter.DatItem_Control_ReqButtons.MatchesPositive(null, RequiredButtons) == false) + return false; + else if (filter.DatItem_Control_ReqButtons.MatchesNegative(null, RequiredButtons) == false) return false; // Filter on minimum - if (filter.DatItem_Control_Minimum.MatchesPositive(null, Minimum) == false) + if (filter.DatItem_Control_Minimum.MatchesNeutral(null, Minimum) == false) return false; - if (filter.DatItem_Control_Minimum.MatchesNegative(null, Minimum) == true) + else if (filter.DatItem_Control_Minimum.MatchesPositive(null, Minimum) == false) + return false; + else if (filter.DatItem_Control_Minimum.MatchesNegative(null, Minimum) == false) return false; // Filter on maximum - if (filter.DatItem_Control_Maximum.MatchesPositive(null, Maximum) == false) + if (filter.DatItem_Control_Maximum.MatchesNeutral(null, Maximum) == false) return false; - if (filter.DatItem_Control_Maximum.MatchesNegative(null, Maximum) == true) + else if (filter.DatItem_Control_Maximum.MatchesPositive(null, Maximum) == false) + return false; + else if (filter.DatItem_Control_Maximum.MatchesNegative(null, Maximum) == false) return false; // Filter on sensitivity - if (filter.DatItem_Control_Sensitivity.MatchesPositive(null, Sensitivity) == false) + if (filter.DatItem_Control_Sensitivity.MatchesNeutral(null, Sensitivity) == false) return false; - if (filter.DatItem_Control_Sensitivity.MatchesNegative(null, Sensitivity) == true) + else if (filter.DatItem_Control_Sensitivity.MatchesPositive(null, Sensitivity) == false) + return false; + else if (filter.DatItem_Control_Sensitivity.MatchesNegative(null, Sensitivity) == false) return false; // Filter on keydelta - if (filter.DatItem_Control_KeyDelta.MatchesPositive(null, KeyDelta) == false) + if (filter.DatItem_Control_KeyDelta.MatchesNeutral(null, KeyDelta) == false) return false; - if (filter.DatItem_Control_KeyDelta.MatchesNegative(null, KeyDelta) == true) + else if (filter.DatItem_Control_KeyDelta.MatchesPositive(null, KeyDelta) == false) + return false; + else if (filter.DatItem_Control_KeyDelta.MatchesNegative(null, KeyDelta) == false) return false; // Filter on reverse @@ -327,7 +322,7 @@ namespace SabreTools.Library.DatItems // Remove the fields if (fields.Contains(Field.DatItem_Control_Type)) - ControlType = null; + ControlType = ControlType.NULL; if (fields.Contains(Field.DatItem_Control_Player)) Player = null; @@ -335,8 +330,8 @@ namespace SabreTools.Library.DatItems if (fields.Contains(Field.DatItem_Control_Buttons)) Buttons = null; - if (fields.Contains(Field.DatItem_Control_RegButtons)) - RegButtons = null; + if (fields.Contains(Field.DatItem_Control_RequiredButtons)) + RequiredButtons = null; if (fields.Contains(Field.DatItem_Control_Minimum)) Minimum = null; @@ -394,8 +389,8 @@ namespace SabreTools.Library.DatItems if (fields.Contains(Field.DatItem_Control_Buttons)) Buttons = newItem.Buttons; - if (fields.Contains(Field.DatItem_Control_RegButtons)) - RegButtons = newItem.RegButtons; + if (fields.Contains(Field.DatItem_Control_RequiredButtons)) + RequiredButtons = newItem.RequiredButtons; if (fields.Contains(Field.DatItem_Control_Minimum)) Minimum = newItem.Minimum; diff --git a/SabreTools.Library/DatItems/DatItem.cs b/SabreTools.Library/DatItems/DatItem.cs index c327377d..4e8a8c73 100644 --- a/SabreTools.Library/DatItems/DatItem.cs +++ b/SabreTools.Library/DatItems/DatItem.cs @@ -152,7 +152,7 @@ namespace SabreTools.Library.DatItems Field.DatItem_Control_Type, Field.DatItem_Control_Player, Field.DatItem_Control_Buttons, - Field.DatItem_Control_RegButtons, + Field.DatItem_Control_RequiredButtons, Field.DatItem_Control_Minimum, Field.DatItem_Control_Maximum, Field.DatItem_Control_Sensitivity, diff --git a/SabreTools.Library/DatItems/DataArea.cs b/SabreTools.Library/DatItems/DataArea.cs index 0c8370fb..1fd455b4 100644 --- a/SabreTools.Library/DatItems/DataArea.cs +++ b/SabreTools.Library/DatItems/DataArea.cs @@ -69,16 +69,10 @@ namespace SabreTools.Library.DatItems Name = mappings[Field.DatItem_AreaName]; if (mappings.Keys.Contains(Field.DatItem_AreaSize)) - { - if (Int64.TryParse(mappings[Field.DatItem_AreaSize], out long areaSize)) - Size = areaSize; - } + Size = Sanitizer.CleanLong(mappings[Field.DatItem_AreaSize]); if (mappings.Keys.Contains(Field.DatItem_AreaWidth)) - { - if (Int64.TryParse(mappings[Field.DatItem_AreaWidth], out long areaWidth)) - Width = areaWidth; - } + Width = Sanitizer.CleanLong(mappings[Field.DatItem_AreaWidth]); if (mappings.Keys.Contains(Field.DatItem_AreaEndianness)) Endianness = mappings[Field.DatItem_AreaEndianness].AsEndianness(); @@ -196,9 +190,11 @@ namespace SabreTools.Library.DatItems return false; // Filter on area byte width - if (filter.DatItem_AreaWidth.MatchesPositive(null, Width) == false) + if (filter.DatItem_AreaWidth.MatchesNeutral(null, Width) == false) return false; - if (filter.DatItem_AreaWidth.MatchesNegative(null, Width) == true) + else if (filter.DatItem_AreaWidth.MatchesPositive(null, Width) == false) + return false; + else if (filter.DatItem_AreaWidth.MatchesNegative(null, Width) == false) return false; // Filter on area endianness diff --git a/SabreTools.Library/DatItems/Display.cs b/SabreTools.Library/DatItems/Display.cs index 294d133c..905aa00a 100644 --- a/SabreTools.Library/DatItems/Display.cs +++ b/SabreTools.Library/DatItems/Display.cs @@ -123,25 +123,16 @@ namespace SabreTools.Library.DatItems DisplayType = mappings[Field.DatItem_DisplayType].AsDisplayType(); if (mappings.Keys.Contains(Field.DatItem_Rotate)) - { - if (Int64.TryParse(mappings[Field.DatItem_Rotate], out long rotate)) - Rotate = rotate; - } + Rotate = Sanitizer.CleanLong(mappings[Field.DatItem_Rotate]); if (mappings.Keys.Contains(Field.DatItem_FlipX)) FlipX = mappings[Field.DatItem_FlipX].AsYesNo(); if (mappings.Keys.Contains(Field.DatItem_Width)) - { - if (Int64.TryParse(mappings[Field.DatItem_Width], out long width)) - Width = width; - } + Width = Sanitizer.CleanLong(mappings[Field.DatItem_Width]); if (mappings.Keys.Contains(Field.DatItem_Height)) - { - if (Int64.TryParse(mappings[Field.DatItem_Height], out long height)) - Height = height; - } + Height = Sanitizer.CleanLong(mappings[Field.DatItem_Height]); if (mappings.Keys.Contains(Field.DatItem_Refresh)) { @@ -150,46 +141,25 @@ namespace SabreTools.Library.DatItems } if (mappings.Keys.Contains(Field.DatItem_PixClock)) - { - if (Int64.TryParse(mappings[Field.DatItem_PixClock], out long pixClock)) - PixClock = pixClock; - } + PixClock = Sanitizer.CleanLong(mappings[Field.DatItem_PixClock]); if (mappings.Keys.Contains(Field.DatItem_HTotal)) - { - if (Int64.TryParse(mappings[Field.DatItem_HTotal], out long hTotal)) - HTotal = hTotal; - } + HTotal = Sanitizer.CleanLong(mappings[Field.DatItem_HTotal]); if (mappings.Keys.Contains(Field.DatItem_HBEnd)) - { - if (Int64.TryParse(mappings[Field.DatItem_HBEnd], out long hbEnd)) - HBEnd = hbEnd; - } + HBEnd = Sanitizer.CleanLong(mappings[Field.DatItem_HBEnd]); if (mappings.Keys.Contains(Field.DatItem_HBStart)) - { - if (Int64.TryParse(mappings[Field.DatItem_HBStart], out long hbStart)) - HBStart = hbStart; - } + HBStart = Sanitizer.CleanLong(mappings[Field.DatItem_HBStart]); if (mappings.Keys.Contains(Field.DatItem_VTotal)) - { - if (Int64.TryParse(mappings[Field.DatItem_VTotal], out long vTotal)) - VTotal = vTotal; - } + VTotal = Sanitizer.CleanLong(mappings[Field.DatItem_VTotal]); if (mappings.Keys.Contains(Field.DatItem_VBEnd)) - { - if (Int64.TryParse(mappings[Field.DatItem_VBEnd], out long vbEnd)) - VBEnd = vbEnd; - } + VBEnd = Sanitizer.CleanLong(mappings[Field.DatItem_VBEnd]); if (mappings.Keys.Contains(Field.DatItem_VBStart)) - { - if (Int64.TryParse(mappings[Field.DatItem_VBStart], out long vbStart)) - VBStart = vbStart; - } + VBStart = Sanitizer.CleanLong(mappings[Field.DatItem_VBStart]); } #endregion @@ -294,9 +264,11 @@ namespace SabreTools.Library.DatItems return false; // Filter on rotation - if (filter.DatItem_Rotate.MatchesPositive(null, Rotate) == false) + if (filter.DatItem_Rotate.MatchesNeutral(null, Rotate) == false) return false; - if (filter.DatItem_Rotate.MatchesNegative(null, Rotate) == true) + else if (filter.DatItem_Rotate.MatchesPositive(null, Rotate) == false) + return false; + else if (filter.DatItem_Rotate.MatchesNegative(null, Rotate) == false) return false; // Filter on flipx @@ -304,63 +276,83 @@ namespace SabreTools.Library.DatItems return false; // Filter on width - if (filter.DatItem_Width.MatchesPositiveSet(Width) == false) + if (filter.DatItem_Width.MatchesNeutral(null, Width) == false) return false; - if (filter.DatItem_Width.MatchesNegativeSet(Width) == true) + else if (filter.DatItem_Width.MatchesPositive(null, Width) == false) + return false; + else if (filter.DatItem_Width.MatchesNegative(null, Width) == false) return false; // Filter on height - if (filter.DatItem_Height.MatchesPositiveSet(Height) == false) + if (filter.DatItem_Height.MatchesNeutral(null, Height) == false) return false; - if (filter.DatItem_Height.MatchesNegativeSet(Height) == true) + else if (filter.DatItem_Height.MatchesPositive(null, Height) == false) + return false; + else if (filter.DatItem_Height.MatchesNegative(null, Height) == false) return false; // Filter on refresh - if (filter.DatItem_Refresh.MatchesPositive(null, Refresh) == false) + if (filter.DatItem_Refresh.MatchesNeutral(null, Refresh) == false) return false; - if (filter.DatItem_Refresh.MatchesNegative(null, Refresh) == true) + else if (filter.DatItem_Refresh.MatchesPositive(null, Refresh) == false) + return false; + else if (filter.DatItem_Refresh.MatchesNegative(null, Refresh) == false) return false; // Filter on pixclock - if (filter.DatItem_PixClock.MatchesPositive(null, PixClock) == false) + if (filter.DatItem_PixClock.MatchesNeutral(null, PixClock) == false) return false; - if (filter.DatItem_PixClock.MatchesNegative(null, PixClock) == true) + else if (filter.DatItem_PixClock.MatchesPositive(null, PixClock) == false) + return false; + else if (filter.DatItem_PixClock.MatchesNegative(null, PixClock) == false) return false; // Filter on htotal - if (filter.DatItem_HTotal.MatchesPositive(null, HTotal) == false) + if (filter.DatItem_HTotal.MatchesNeutral(null, HTotal) == false) return false; - if (filter.DatItem_HTotal.MatchesNegative(null, HTotal) == true) + else if (filter.DatItem_HTotal.MatchesPositive(null, HTotal) == false) + return false; + else if (filter.DatItem_HTotal.MatchesNegative(null, HTotal) == false) return false; // Filter on hbend - if (filter.DatItem_HBEnd.MatchesPositive(null, HBEnd) == false) + if (filter.DatItem_HBEnd.MatchesNeutral(null, HBEnd) == false) return false; - if (filter.DatItem_HBEnd.MatchesNegative(null, HBEnd) == true) + else if (filter.DatItem_HBEnd.MatchesPositive(null, HBEnd) == false) + return false; + else if (filter.DatItem_HBEnd.MatchesNegative(null, HBEnd) == false) return false; // Filter on hbstart - if (filter.DatItem_HBStart.MatchesPositive(null, HBStart) == false) + if (filter.DatItem_HBStart.MatchesNeutral(null, HBStart) == false) return false; - if (filter.DatItem_HBStart.MatchesNegative(null, HBStart) == true) + else if (filter.DatItem_HBStart.MatchesPositive(null, HBStart) == false) + return false; + else if (filter.DatItem_HBStart.MatchesNegative(null, HBStart) == false) return false; // Filter on vtotal - if (filter.DatItem_VTotal.MatchesPositive(null, VTotal) == false) + if (filter.DatItem_VTotal.MatchesNeutral(null, VTotal) == false) return false; - if (filter.DatItem_VTotal.MatchesNegative(null, VTotal) == true) + else if (filter.DatItem_VTotal.MatchesPositive(null, VTotal) == false) + return false; + else if (filter.DatItem_VTotal.MatchesNegative(null, VTotal) == false) return false; // Filter on vbend - if (filter.DatItem_VBEnd.MatchesPositive(null, VBEnd) == false) + if (filter.DatItem_VBEnd.MatchesNeutral(null, VBEnd) == false) return false; - if (filter.DatItem_VBEnd.MatchesNegative(null, VBEnd) == true) + else if (filter.DatItem_VBEnd.MatchesPositive(null, VBEnd) == false) + return false; + else if (filter.DatItem_VBEnd.MatchesNegative(null, VBEnd) == false) return false; // Filter on vbstart - if (filter.DatItem_VBStart.MatchesPositive(null, VBStart) == false) + if (filter.DatItem_VBStart.MatchesNeutral(null, VBStart) == false) return false; - if (filter.DatItem_VBStart.MatchesNegative(null, VBStart) == true) + else if (filter.DatItem_VBStart.MatchesPositive(null, VBStart) == false) + return false; + else if (filter.DatItem_VBStart.MatchesNegative(null, VBStart) == false) return false; return true; diff --git a/SabreTools.Library/DatItems/Enums.cs b/SabreTools.Library/DatItems/Enums.cs index dac62fe5..48b51988 100644 --- a/SabreTools.Library/DatItems/Enums.cs +++ b/SabreTools.Library/DatItems/Enums.cs @@ -17,6 +17,34 @@ namespace SabreTools.Library.DatItems Audio = 1 << 1, } + /// + /// Determine the control type + /// + [Flags] + public enum ControlType + { + /// + /// This is a fake flag that is used for filter only + /// + NULL = 0, + + Joy = 1 << 0, + Stick = 1 << 1, + Paddle = 1 << 2, + Pedal = 1 << 3, + Lightgun = 1 << 4, + Positional = 1 << 5, + Dial = 1 << 6, + Trackball = 1 << 7, + Mouse = 1 << 8, + OnlyButtons = 1 << 9, + Keypad = 1 << 10, + Keyboard = 1 << 11, + Mahjong = 1 << 12, + Hanafuda = 1 << 13, + Gambling = 1 << 14, + } + /// /// Determine the display type /// @@ -338,7 +366,7 @@ namespace SabreTools.Library.DatItems DatItem_Control_Type, DatItem_Control_Player, DatItem_Control_Buttons, - DatItem_Control_RegButtons, + DatItem_Control_RequiredButtons, DatItem_Control_Minimum, DatItem_Control_Maximum, DatItem_Control_Sensitivity, diff --git a/SabreTools.Library/DatItems/Input.cs b/SabreTools.Library/DatItems/Input.cs index 133dd50b..8615864d 100644 --- a/SabreTools.Library/DatItems/Input.cs +++ b/SabreTools.Library/DatItems/Input.cs @@ -67,16 +67,10 @@ namespace SabreTools.Library.DatItems Tilt = mappings[Field.DatItem_Tilt].AsYesNo(); if (mappings.Keys.Contains(Field.DatItem_Players)) - { - if (Int64.TryParse(mappings[Field.DatItem_Players], out long players)) - Players = players; - } + Players = Sanitizer.CleanLong(mappings[Field.DatItem_Players]); if (mappings.Keys.Contains(Field.DatItem_Coins)) - { - if (Int64.TryParse(mappings[Field.DatItem_Coins], out long coins)) - Coins = coins; - } + Coins = Sanitizer.CleanLong(mappings[Field.DatItem_Coins]); if (Controls != null) { diff --git a/SabreTools.Library/DatItems/Rom.cs b/SabreTools.Library/DatItems/Rom.cs index 575edb25..f7f2fb80 100644 --- a/SabreTools.Library/DatItems/Rom.cs +++ b/SabreTools.Library/DatItems/Rom.cs @@ -297,10 +297,7 @@ namespace SabreTools.Library.DatItems Bios = mappings[Field.DatItem_Bios]; if (mappings.Keys.Contains(Field.DatItem_Size)) - { - if (Int64.TryParse(mappings[Field.DatItem_Size], out long size)) - Size = size; - } + Size = Sanitizer.CleanLong(mappings[Field.DatItem_Size]); if (mappings.Keys.Contains(Field.DatItem_CRC)) CRC = mappings[Field.DatItem_CRC]; diff --git a/SabreTools.Library/DatItems/Sound.cs b/SabreTools.Library/DatItems/Sound.cs index dc2da704..fef12d88 100644 --- a/SabreTools.Library/DatItems/Sound.cs +++ b/SabreTools.Library/DatItems/Sound.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using SabreTools.Library.Filtering; +using SabreTools.Library.Tools; using Newtonsoft.Json; namespace SabreTools.Library.DatItems @@ -16,7 +17,7 @@ namespace SabreTools.Library.DatItems #region Fields /// - /// Number of channels + /// Number of speakers or channels /// [JsonProperty("channels", DefaultValueHandling = DefaultValueHandling.Ignore)] public long? Channels { get; set; } @@ -36,10 +37,7 @@ namespace SabreTools.Library.DatItems // Handle Sound-specific fields if (mappings.Keys.Contains(Field.DatItem_Channels)) - { - if (Int64.TryParse(mappings[Field.DatItem_Channels], out long channels)) - Channels = channels; - } + Channels = Sanitizer.CleanLong(mappings[Field.DatItem_Channels]); } #endregion diff --git a/SabreTools.Library/Filtering/Filter.cs b/SabreTools.Library/Filtering/Filter.cs index d645ee65..4a742dcf 100644 --- a/SabreTools.Library/Filtering/Filter.cs +++ b/SabreTools.Library/Filtering/Filter.cs @@ -169,10 +169,10 @@ namespace SabreTools.Library.Filtering public FilterItem DatItem_Condition_Value { get; private set; } = new FilterItem(); // Control - public FilterItem DatItem_Control_Type { get; private set; } = new FilterItem(); + public FilterItem DatItem_Control_Type { get; private set; } = new FilterItem() { Positive = ControlType.NULL, Negative = ControlType.NULL }; public FilterItem DatItem_Control_Player { get; private set; } = new FilterItem() { Positive = null, Negative = null, Neutral = null }; public FilterItem DatItem_Control_Buttons { get; private set; } = new FilterItem() { Positive = null, Negative = null, Neutral = null }; - public FilterItem DatItem_Control_RegButtons { get; private set; } = new FilterItem(); + public FilterItem DatItem_Control_ReqButtons { get; private set; } = new FilterItem() { Positive = null, Negative = null, Neutral = null }; public FilterItem DatItem_Control_Minimum { get; private set; } = new FilterItem() { Positive = null, Negative = null, Neutral = null }; public FilterItem DatItem_Control_Maximum { get; private set; } = new FilterItem() { Positive = null, Negative = null, Neutral = null }; public FilterItem DatItem_Control_Sensitivity { get; private set; } = new FilterItem() { Positive = null, Negative = null, Neutral = null }; @@ -742,7 +742,11 @@ namespace SabreTools.Library.Filtering // Control case Field.DatItem_Control_Type: - SetStringFilter(DatItem_Control_Type, value, negate); + + if (negate) + DatItem_Control_Type.Negative |= value.AsControlType(); + else + DatItem_Control_Type.Positive |= value.AsControlType(); break; case Field.DatItem_Control_Player: @@ -753,8 +757,8 @@ namespace SabreTools.Library.Filtering SetLongFilter(DatItem_Control_Buttons, value, negate); break; - case Field.DatItem_Control_RegButtons: - SetStringFilter(DatItem_Control_RegButtons, value, negate); + case Field.DatItem_Control_RequiredButtons: + SetLongFilter(DatItem_Control_ReqButtons, value, negate); break; case Field.DatItem_Control_Minimum: diff --git a/SabreTools.Library/Tools/Converters.cs b/SabreTools.Library/Tools/Converters.cs index 51461fc0..fc2fc183 100644 --- a/SabreTools.Library/Tools/Converters.cs +++ b/SabreTools.Library/Tools/Converters.cs @@ -95,6 +95,72 @@ namespace SabreTools.Library.Tools #endif } + /// + /// Get ControlType value from input string + /// + /// String to get value from + /// ControlType value corresponding to the string + public static ControlType AsControlType(this string controlType) + { +#if NET_FRAMEWORK + switch (controlType?.ToLowerInvariant()) + { + case "joy": + return ControlType.Joy; + case "stick": + return ControlType.Stick; + case "paddle": + return ControlType.Paddle; + case "pedal": + return ControlType.Pedal; + case "lightgun": + return ControlType.Lightgun; + case "positional": + return ControlType.Positional; + case "dial": + return ControlType.Dial; + case "trackball": + return ControlType.Trackball; + case "mouse": + return ControlType.Mouse; + case "only_buttons": + return ControlType.OnlyButtons; + case "keypad": + return ControlType.Keypad; + case "keyboard": + return ControlType.Keyboard; + case "mahjong": + return ControlType.Mahjong; + case "hanafuda": + return ControlType.Hanafuda; + case "gambling": + return ControlType.Gambling; + default: + return ControlType.NULL; + } +#else + return controlType?.ToLowerInvariant() switch + { + "joy" => ControlType.Joy, + "stick" => ControlType.Stick, + "paddle" => ControlType.Paddle, + "pedal" => ControlType.Pedal, + "lightgun" => ControlType.Lightgun, + "positional" => ControlType.Positional, + "dial" => ControlType.Dial, + "trackball" => ControlType.Trackball, + "mouse" => ControlType.Mouse, + "only_buttons" => ControlType.OnlyButtons, + "keypad" => ControlType.Keypad, + "keyboard" => ControlType.Keyboard, + "mahjong" => ControlType.Mahjong, + "hanafuda" => ControlType.Hanafuda, + "gambling" => ControlType.Gambling, + _ => ControlType.NULL, + }; +#endif + } + /// /// Get DatFormat value from input string /// @@ -956,8 +1022,8 @@ namespace SabreTools.Library.Tools case "control_buttons": return Field.DatItem_Control_Buttons; - case "control_regbuttons": - return Field.DatItem_Control_RegButtons; + case "control_reqbuttons": + return Field.DatItem_Control_RequiredButtons; case "control_minimum": return Field.DatItem_Control_Minimum; @@ -2254,6 +2320,72 @@ namespace SabreTools.Library.Tools #endif } + /// + /// Get string value from input ControlType + /// + /// ControlType to get value from + /// String value corresponding to the ControlType + public static string FromControlType(this ControlType controlType) + { +#if NET_FRAMEWORK + switch (controlType) + { + case ControlType.Joy: + return "joy"; + case ControlType.Stick: + return "stick"; + case ControlType.Paddle: + return "paddle"; + case ControlType.Pedal: + return "pedal"; + case ControlType.Lightgun: + return "lightgun"; + case ControlType.Positional: + return "positional"; + case ControlType.Dial: + return "dial"; + case ControlType.Trackball: + return "trackball"; + case ControlType.Mouse: + return "mouse"; + case ControlType.OnlyButtons: + return "only_buttons"; + case ControlType.Keypad: + return "keypad"; + case ControlType.Keyboard: + return "keyboard"; + case ControlType.Mahjong: + return "mahjong"; + case ControlType.Hanafuda: + return "hanafuda"; + case ControlType.Gambling: + return "gambling"; + default: + return null; + } +#else + return controlType switch + { + ControlType.Joy => "joy", + ControlType.Stick => "stick", + ControlType.Paddle => "paddle", + ControlType.Pedal => "pedal", + ControlType.Lightgun => "lightgun", + ControlType.Positional => "positional", + ControlType.Dial => "dial", + ControlType.Trackball => "trackball", + ControlType.Mouse => "mouse", + ControlType.OnlyButtons => "only_buttons", + ControlType.Keypad => "keypad", + ControlType.Keyboard => "keyboard", + ControlType.Mahjong => "mahjong", + ControlType.Hanafuda => "hanafuda", + ControlType.Gambling => "gambling", + _ => null, + }; +#endif + } + /// /// Get string value from input DisplayType ///