Skip to content

Commit 984b198

Browse files
authored
Merge pull request #258 from perpetualKid/master
Update to #172, #173 and #177 Validating WindowSize input https://bugs.launchpad.net/or/+bug/1875887
2 parents 5e34233 + 940ae85 commit 984b198

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

Source/Menu/Options.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using System.Globalization;
2222
using System.IO;
2323
using System.Linq;
24+
using System.Text.RegularExpressions;
2425
using System.Windows.Forms;
2526
using GNU.Gettext;
2627
using GNU.Gettext.WinForms;
@@ -461,7 +462,7 @@ void buttonOK_Click(object sender, EventArgs e)
461462
Settings.DistantMountainsViewingDistance = (int)numericDistantMountainsViewingDistance.Value * 1000;
462463
Settings.ViewingFOV = (int)numericViewingFOV.Value;
463464
Settings.WorldObjectDensity = (int)numericWorldObjectDensity.Value;
464-
Settings.WindowSize = GetValidWindowSize(comboWindowSize);
465+
Settings.WindowSize = GetValidWindowSize(comboWindowSize.Text);
465466

466467
Settings.DayAmbientLight = (int)trackDayAmbientLight.Value;
467468
Settings.DoubleWire = checkDoubleWire.Checked;
@@ -541,14 +542,13 @@ void buttonOK_Click(object sender, EventArgs e)
541542
/// <summary>
542543
/// Returns user's [width]x[height] if expression is valid and values are sane, else returns previous value of setting.
543544
/// </summary>
544-
private string GetValidWindowSize(ComboBox comboWindowSize)
545+
private string GetValidWindowSize(string text)
545546
{
546-
// "1024X780" instead of "1024x780" then "Start" resulted in an immediate return to the Menu with no OpenRailsLog.txt and a baffled user.
547-
var sizeArray = comboWindowSize.Text.ToLower().Replace(" ", "").Split('x');
548-
if (sizeArray.Count() == 2)
549-
if (int.TryParse(sizeArray[0], out int width) && int.TryParse(sizeArray[1], out int height))
550-
if ((100 < width && width < 10000) && (100 < height && height < 100000)) // sanity check
551-
return $"{width}x{height}";
547+
var match = Regex.Match(text, @"^\s*([1-9]\d{2,3})\s*[Xx]\s*([1-9]\d{2,3})\s*$");//capturing 2 groups of 3-4digits, separated by X or x, ignoring whitespace in beginning/end and in between
548+
if (match.Success)
549+
{
550+
return $"{match.Groups[1]}x{match.Groups[2]}";
551+
}
552552
return Settings.WindowSize; // i.e. no change or message. Just ignore non-numeric entries
553553
}
554554

@@ -760,4 +760,4 @@ private void checkPerformanceTuner_Click(object sender, EventArgs e)
760760
}
761761

762762
}
763-
}
763+
}

0 commit comments

Comments
 (0)