From 1793e991ca8ef7b31a3a66c4158715c11acf936b Mon Sep 17 00:00:00 2001 From: YoshiRulz Date: Wed, 2 Sep 2020 07:47:57 +1000 Subject: [PATCH] Move Windows version check to OSTailoredCode and cleanup --- src/BizHawk.Client.EmuHawk/MainForm.cs | 35 ++++++---------- .../Extensions/StringExtensions.cs | 12 ++++++ src/BizHawk.Common/OSTailoredCode.cs | 41 +++++++++++++++++++ .../StringExtensions/StringExtensionTests.cs | 13 ++++++ 4 files changed, 79 insertions(+), 22 deletions(-) diff --git a/src/BizHawk.Client.EmuHawk/MainForm.cs b/src/BizHawk.Client.EmuHawk/MainForm.cs index d2fc8e4b65..4466588cca 100644 --- a/src/BizHawk.Client.EmuHawk/MainForm.cs +++ b/src/BizHawk.Client.EmuHawk/MainForm.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; -using System.Globalization; using System.IO; using System.Linq; using System.Reflection; @@ -29,6 +28,7 @@ using BizHawk.Client.EmuHawk.ToolExtensions; using BizHawk.Client.EmuHawk.CoreExtensions; using BizHawk.Client.EmuHawk.CustomControls; using BizHawk.Common.PathExtensions; +using BizHawk.Common.StringExtensions; using BizHawk.Emulation.Common.Base_Implementations; using BizHawk.Emulation.Cores.Consoles.NEC.PCE; using BizHawk.Emulation.Cores.Nintendo.SNES9X; @@ -628,30 +628,21 @@ namespace BizHawk.Client.EmuHawk if (!OSTailoredCode.IsUnixHost && !Config.SkipOutdatedOsCheck) { - static string GetRegValue(string key) + var (winVersion, win10Release) = OSTailoredCode.HostWindowsVersion.Value; + var message = winVersion switch { - using var proc = OSTailoredCode.ConstructSubshell("REG", $@"QUERY ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"" /V {key}"); - proc.Start(); - return proc.StandardOutput.ReadToEnd().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; - } - var winVer = float.Parse(GetRegValue("CurrentVersion"), NumberFormatInfo.InvariantInfo); - if (winVer < 6.3f) + OSTailoredCode.WindowsVersion._10 when win10Release < 1809 => $"Quick reminder: version {win10Release} of Windows 10 is no longer supported by Microsoft. EmuHawk will continue to work, but please update to at least 1809 \"Redstone 5\" for increased security.", + OSTailoredCode.WindowsVersion._10 => null, + OSTailoredCode.WindowsVersion._8_1 => null, // still CBB + _ => $"Quick reminder: Windows {winVersion.ToString().RemovePrefix('_').Replace("_", ".")} is no longer supported by Microsoft. EmuHawk will continue to work, but please get a new operating system for increased security (either Windows 8.1, Windows 10, or a GNU+Linux distro)." + }; +#if false + if (message != null) { - // less than is just easier than equals - string message = ($"Quick reminder: Windows {(winVer < 6.2f ? winVer < 6.1f ? winVer < 6.0f ? "XP" : "Vista" : "7" : "8")} is no longer supported by Microsoft. EmuHawk will continue to work, but please get a new operating system for increased security (either Windows 8.1, Windows 10, or a GNU+Linux distro)."); - } - else if (GetRegValue("ProductName").Contains("Windows 10")) - { - var win10version = int.Parse(GetRegValue("ReleaseId")); - if (win10version < 1809) - { - string message = ($"Quick reminder: version {win10version} of Windows 10 is no longer supported by Microsoft. EmuHawk will continue to work, but please update to at least 1809 \"Redstone 5\" for increased security."); - } - } - else - { - // 8.1: can't be bothered writing code for KB installed check, not that I have a Win8.1 machine to test on anyway, so it gets a free pass --yoshi + using var box = new ExceptionBox(message); + box.ShowDialog(); } +#endif } } diff --git a/src/BizHawk.Common/Extensions/StringExtensions.cs b/src/BizHawk.Common/Extensions/StringExtensions.cs index deb8b54bba..b2105959b9 100644 --- a/src/BizHawk.Common/Extensions/StringExtensions.cs +++ b/src/BizHawk.Common/Extensions/StringExtensions.cs @@ -11,6 +11,18 @@ namespace BizHawk.Common.StringExtensions public static bool In(this string str, params string[] options) => options.Any(opt => string.Equals(opt, str, StringComparison.InvariantCultureIgnoreCase)); + /// + /// with the first char removed, or + /// the original if the first char of is not + /// + public static string RemovePrefix(this string str, char prefix) => str.RemovePrefix(prefix, notFoundValue: str); + + /// + /// with the first char removed, or + /// if the first char of is not + /// + public static string RemovePrefix(this string str, char prefix, string notFoundValue) => str.Length != 0 && str[0] == prefix ? str.Substring(1, str.Length - 1) : notFoundValue; + /// /// with the last char removed, or /// the original if the last char of is not diff --git a/src/BizHawk.Common/OSTailoredCode.cs b/src/BizHawk.Common/OSTailoredCode.cs index c3b14dd498..da5e85b14e 100644 --- a/src/BizHawk.Common/OSTailoredCode.cs +++ b/src/BizHawk.Common/OSTailoredCode.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Globalization; using System.Runtime.InteropServices; #if EXE_PROJECT @@ -15,6 +16,36 @@ namespace BizHawk.Common ? SimpleSubshell("uname", "-s", "Can't determine OS") == "Darwin" ? DistinctOS.macOS : DistinctOS.Linux : DistinctOS.Windows; + private static readonly Lazy<(WindowsVersion, int?)?> _HostWindowsVersion = new Lazy<(WindowsVersion, int?)?>(() => + { + static string GetRegValue(string key) + { + using var proc = ConstructSubshell("REG", $@"QUERY ""HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion"" /V {key}"); + proc.Start(); + return proc.StandardOutput.ReadToEnd().Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)[1].Split(new[] { '\t', ' ' }, StringSplitOptions.RemoveEmptyEntries)[2]; + } + if (CurrentOS != DistinctOS.Windows) return null; + var rawWinVer = float.Parse(GetRegValue("CurrentVersion"), NumberFormatInfo.InvariantInfo); // contains '.' even when system-wide decimal separator is ',' + WindowsVersion winVer; // sorry if this elif chain is confusing, I couldn't be bothered writing and testing float equality --yoshi + if (rawWinVer < 6.0f) winVer = WindowsVersion.XP; + else if (rawWinVer < 6.1f) winVer = WindowsVersion.Vista; + else if (rawWinVer < 6.2f) winVer = WindowsVersion._7; + else if (rawWinVer < 6.3f) winVer = WindowsVersion._8; + else + { + // 8.1 and 10 are both version 6.3 + if (GetRegValue("ProductName").Contains("Windows 10")) + { + return (WindowsVersion._10, int.Parse(GetRegValue("ReleaseId"))); + } + // ...else we're on 8.1. Can't be bothered writing code for KB installed check, not that I have a Win8.1 machine to test on anyway, so it gets a free pass --yoshi + winVer = WindowsVersion._8_1; + } + return (winVer, null); + }); + + public static (WindowsVersion Version, int? Win10Release)? HostWindowsVersion => _HostWindowsVersion.Value; + public static readonly bool IsUnixHost = CurrentOS != DistinctOS.Windows; private static readonly Lazy _LinkedLibManager = new Lazy(() => CurrentOS switch @@ -123,6 +154,16 @@ namespace BizHawk.Common Windows } + public enum WindowsVersion + { + XP, + Vista, + _7, + _8, + _8_1, + _10 + } + /// POSIX $0 /// POSIX $* (space-delimited) /// stdout is discarded if false diff --git a/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs b/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs index ba261011cb..faf9ea2d00 100644 --- a/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs +++ b/src/BizHawk.Tests/Common/StringExtensions/StringExtensionTests.cs @@ -17,5 +17,18 @@ namespace BizHawk.Tests.Common.StringExtensions var actual = "hello world".In(strArray); Assert.IsTrue(actual); } + + [TestMethod] + public void TestRemovePrefix() + { + const string abcdef = "abcdef"; + const string qrs = "qrs"; + + Assert.AreEqual("bcdef", abcdef.RemovePrefix('a', qrs)); + Assert.AreEqual(string.Empty, "a".RemovePrefix('a', qrs)); + Assert.AreEqual(qrs, abcdef.RemovePrefix('c', qrs)); + Assert.AreEqual(qrs, abcdef.RemovePrefix('x', qrs)); + Assert.AreEqual(qrs, string.Empty.RemovePrefix('a', qrs)); + } } }