From 6bbd3ca80d4cd84fbf8d700f373fcbd686088ad7 Mon Sep 17 00:00:00 2001 From: adelikat Date: Thu, 3 Jul 2014 16:43:39 +0000 Subject: [PATCH] Hex and Watch Boxes - prevent user from pasting invalid input, also if they do manage to get the textbox into an invalid state, interpret the value as 0 rather than throwing an exception --- .../CustomControls/HexTextBox.cs | 4 +- .../tools/HexEditor/HexEditor.cs | 2 +- .../tools/Watch/WatchValueBox.cs | 81 ++++++- BizHawk.Common/Extensions/StringExtensions.cs | 207 ++++++++++++++++-- 4 files changed, 258 insertions(+), 36 deletions(-) diff --git a/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs b/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs index 616ebeabf6..54b9713632 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/HexTextBox.cs @@ -213,7 +213,7 @@ namespace BizHawk.Client.EmuHawk protected override void OnTextChanged(EventArgs e) { - if (string.IsNullOrWhiteSpace(Text)) + if (string.IsNullOrWhiteSpace(Text) || !Text.IsHex()) { ResetText(); } @@ -223,7 +223,7 @@ namespace BizHawk.Client.EmuHawk public int? ToRawInt() { - if (string.IsNullOrWhiteSpace(Text)) + if (string.IsNullOrWhiteSpace(Text) || !Text.IsHex()) { if (Nullable) { diff --git a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs index 626880185c..cdaf19b88d 100644 --- a/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs +++ b/BizHawk.Client.EmuHawk/tools/HexEditor/HexEditor.cs @@ -1377,7 +1377,7 @@ namespace BizHawk.Client.EmuHawk if (data != null && data.GetDataPresent(DataFormats.Text)) { var clipboardRaw = (string)data.GetData(DataFormats.Text); - var hex = clipboardRaw.DoHexString(); + var hex = clipboardRaw.OnlyHex(); var numBytes = hex.Length / 2; for (var i = 0; i < numBytes; i++) diff --git a/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs b/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs index 2a969dc2ea..d4758485be 100644 --- a/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs +++ b/BizHawk.Client.EmuHawk/tools/Watch/WatchValueBox.cs @@ -533,6 +533,31 @@ namespace BizHawk.Client.EmuHawk { ResetText(); } + + switch (_type) + { + case Watch.DisplayType.Signed: + Text = Text.OnlySigned(); + break; + case Watch.DisplayType.Unsigned: + Text = Text.OnlyUnsigned(); + break; + case Watch.DisplayType.Binary: + Text = Text.OnlyBinary(); + break; + case Watch.DisplayType.Hex: + Text = Text.OnlyHex(); + break; + case Watch.DisplayType.FixedPoint_12_4: + case Watch.DisplayType.FixedPoint_20_12: + Text = Text.OnlyFixedPoint(); + break; + case Watch.DisplayType.Float: + Text = Text.OnlyFloat(); + break; + } + + base.OnTextChanged(e); } public int? ToRawInt() @@ -549,24 +574,60 @@ namespace BizHawk.Client.EmuHawk switch (_type) { - default: case Watch.DisplayType.Signed: - return int.Parse(Text); + if (Text.IsSigned()) + { + return int.Parse(Text); + } + + break; case Watch.DisplayType.Unsigned: - return (int)uint.Parse(Text); + if (Text.IsUnsigned()) + { + return (int)uint.Parse(Text); + } + + break; case Watch.DisplayType.Binary: - return Convert.ToInt32(Text, 2); + if (Text.IsBinary()) + { + return Convert.ToInt32(Text, 2); + } + + break; case Watch.DisplayType.Hex: - return int.Parse(Text, NumberStyles.HexNumber); + if (Text.IsHex()) + { + return int.Parse(Text, NumberStyles.HexNumber); + } + + break; case Watch.DisplayType.FixedPoint_12_4: - return (int)(double.Parse(Text) * 16.0); + if (Text.IsFixedPoint()) + { + return (int)(double.Parse(Text) * 16.0); + } + + break; case Watch.DisplayType.FixedPoint_20_12: - return (int)(double.Parse(Text) * 4096.0); + if (Text.IsFixedPoint()) + { + return (int)(double.Parse(Text) * 4096.0); + } + + break; case Watch.DisplayType.Float: - float val = float.Parse(Text); - var bytes = BitConverter.GetBytes(val); - return BitConverter.ToInt32(bytes, 0); + if (Text.IsFloat()) + { + float val = float.Parse(Text); + var bytes = BitConverter.GetBytes(val); + return BitConverter.ToInt32(bytes, 0); + } + + break; } + + return 0; } public void SetFromRawInt(int? val) diff --git a/BizHawk.Common/Extensions/StringExtensions.cs b/BizHawk.Common/Extensions/StringExtensions.cs index 76299aadfa..fe56405c15 100644 --- a/BizHawk.Common/Extensions/StringExtensions.cs +++ b/BizHawk.Common/Extensions/StringExtensions.cs @@ -149,29 +149,6 @@ namespace BizHawk.Common.StringExtensions return char.ToUpper(c) >= 'A' && char.ToUpper(c) <= 'F'; } - /// - /// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase - /// - public static string DoHexString(this string raw) - { - if (raw == null) - { - return string.Empty; - } - - var output = new StringBuilder(); - - foreach (var chr in raw) - { - if (IsHex(chr)) - { - output.Append(char.ToUpper(chr)); - } - } - - return output.ToString(); - } - /// /// Validates all chars are 0 or 1 /// @@ -238,6 +215,190 @@ namespace BizHawk.Common.StringExtensions return c.IsFixedPoint() || c == '-'; } + /// + /// Takes any string and removes any value that is not a valid binary value (0 or 1) + /// + public static string OnlyBinary(this string raw) + { + if (raw == null) + { + return string.Empty; + } + + var output = new StringBuilder(); + + foreach (var chr in raw) + { + if (IsBinary(chr)) + { + output.Append(chr); + } + } + + return output.ToString(); + } + + /// + /// Takes any string and removes any value that is not a valid unsigned integer value (0-9) + /// + public static string OnlyUnsigned(this string raw) + { + if (raw == null) + { + return string.Empty; + } + + var output = new StringBuilder(); + + foreach (var chr in raw) + { + if (IsUnsigned(chr)) + { + output.Append(chr); + } + } + + return output.ToString(); + } + + /// + /// Takes any string and removes any value that is not a valid unsigned integer value (0-9 or -) + /// Note: a "-" will only be kept if it is the first digit + /// + public static string OnlySigned(this string raw) + { + if (raw == null) + { + return string.Empty; + } + + var output = new StringBuilder(); + + int count = 0; + foreach (var chr in raw) + { + if (count == 0 && chr == '-') + { + output.Append(chr); + } + else if (IsUnsigned(chr)) + { + + output.Append(chr); + } + + count++; + } + + return output.ToString(); + } + + /// + /// Takes any string and removes any value that is not a valid hex value (0-9, a-f, A-F), returns the remaining characters in uppercase + /// + public static string OnlyHex(this string raw) + { + if (raw == null) + { + return string.Empty; + } + + var output = new StringBuilder(); + + foreach (var chr in raw) + { + if (IsHex(chr)) + { + output.Append(char.ToUpper(chr)); + } + } + + return output.ToString(); + } + + /// + /// Takes any string and removes any value that is not a fixed point value (0-9 or .) + /// Note: only the first occurance of a . will be kept + /// + public static string OnlyFixedPoint(this string raw) + { + if (raw == null) + { + return string.Empty; + } + + var output = new StringBuilder(); + + bool usedDot = false; + foreach (var chr in raw) + { + if (chr == '.') + { + if (usedDot) + { + continue; + } + else + { + usedDot = true; + } + } + + if (IsFixedPoint(chr)) + { + output.Append(chr); + } + } + + return output.ToString(); + } + + /// + /// Takes any string and removes any value that is not a float point value (0-9, -, or .) + /// Note: - is only valid as the first character, and only the first occurance of a . will be kept + /// + public static string OnlyFloat(this string raw) + { + if (raw == null) + { + return string.Empty; + } + + var output = new StringBuilder(); + + bool usedDot = false; + int count = 0; + foreach (var chr in raw) + { + if (count == 0 && chr == '-') + { + output.Append(chr); + } + else + { + if (chr == '.') + { + if (usedDot) + { + continue; + } + else + { + usedDot = true; + } + } + + if (IsFixedPoint(chr)) + { + output.Append(chr); + } + } + } + + count++; + return output.ToString(); + } + #endregion } }