Move Windows version check to OSTailoredCode and cleanup

This commit is contained in:
YoshiRulz 2020-09-02 07:47:57 +10:00
parent dbddcfe665
commit 1793e991ca
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
4 changed files with 79 additions and 22 deletions

View File

@ -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
}
}

View File

@ -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));
/// <returns>
/// <paramref name="str"/> with the first char removed, or
/// the original <paramref name="str"/> if the first char of <paramref name="str"/> is not <paramref name="prefix"/>
/// </returns>
public static string RemovePrefix(this string str, char prefix) => str.RemovePrefix(prefix, notFoundValue: str);
/// <returns>
/// <paramref name="str"/> with the first char removed, or
/// <paramref name="notFoundValue"/> if the first char of <paramref name="str"/> is not <paramref name="prefix"/>
/// </returns>
public static string RemovePrefix(this string str, char prefix, string notFoundValue) => str.Length != 0 && str[0] == prefix ? str.Substring(1, str.Length - 1) : notFoundValue;
/// <returns>
/// <paramref name="str"/> with the last char removed, or
/// the original <paramref name="str"/> if the last char of <paramref name="str"/> is not <paramref name="suffix"/>

View File

@ -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<ILinkedLibManager> _LinkedLibManager = new Lazy<ILinkedLibManager>(() => CurrentOS switch
@ -123,6 +154,16 @@ namespace BizHawk.Common
Windows
}
public enum WindowsVersion
{
XP,
Vista,
_7,
_8,
_8_1,
_10
}
/// <param name="cmd">POSIX <c>$0</c></param>
/// <param name="args">POSIX <c>$*</c> (space-delimited)</param>
/// <param name="checkStdout">stdout is discarded if false</param>

View File

@ -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));
}
}
}