From 7fe950f98b8e57da4c2d97cad005e5fb10c843fe Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Mon, 13 Nov 2023 16:34:34 -0600 Subject: [PATCH] Modernize platform checks (requires .NET Framework 4.7.1+) --- Core/Platform.cs | 68 +++++++++++------------------------------------- 1 file changed, 15 insertions(+), 53 deletions(-) diff --git a/Core/Platform.cs b/Core/Platform.cs index 360f47045c..b9e683a65a 100644 --- a/Core/Platform.cs +++ b/Core/Platform.cs @@ -24,21 +24,20 @@ static Platform() /// Are we on a Mac? /// /// true if is mac; otherwise, false. - public static readonly bool IsMac = IsRunningOnMac(); + public static readonly bool IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX); /// /// Are we on a Unix (including Linux, but *not* Mac) system. - /// Note that Mono thinks Mac is Unix! So we need to negate Mac explicitly. /// /// true if is unix; otherwise, false. - public static readonly bool IsUnix = Environment.OSVersion.Platform == PlatformID.Unix && !IsMac; + public static readonly bool IsUnix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); /// /// Are we on a flavour of Windows? This is implemented internally by checking /// if we're not on Unix or Mac. /// /// true if is windows; otherwise, false. - public static readonly bool IsWindows = !IsUnix && !IsMac; + public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows); /// /// Are we on Mono? @@ -53,40 +52,9 @@ static Platform() /// /// Are we running in an X11 environment? /// - public static readonly bool IsX11 = IsUnix && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISPLAY")); - - - // From https://github.com/mono/monodevelop/blob/master/main/src/core/Mono.Texteditor/Mono.TextEditor/Platform.cs - // Environment.OSVersion.Platform returns Unix for Mac OS X as of Mono v4.0.0: - // https://bugzilla.xamarin.com/show_bug.cgi?id=13345 - // So we have to detect it another way - private static bool IsRunningOnMac() - { - IntPtr buf = IntPtr.Zero; - try - { - buf = Marshal.AllocHGlobal(8192); - // This is a hacktastic way of getting sysname from uname () - if (uname(buf) == 0) - { - string os = Marshal.PtrToStringAnsi(buf); - if (os == "Darwin") - return true; - } - } - catch - { - } - finally - { - if (buf != IntPtr.Zero) - Marshal.FreeHGlobal(buf); - } - return false; - } - - [DllImport("libc")] - private static extern int uname(IntPtr buf); + public static readonly bool IsX11 = + IsUnix + && !string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DISPLAY")); private static bool IsOnMonoFourOrLater() { @@ -94,25 +62,19 @@ private static bool IsOnMonoFourOrLater() return false; // Get Mono's display name and parse the version - Type type = Type.GetType("Mono.Runtime"); string display_name = - (string) type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, null); + (string)Type.GetType("Mono.Runtime") + .GetMethod("GetDisplayName", + BindingFlags.NonPublic | BindingFlags.Static) + .Invoke(null, null); var match = versionMatcher.Match(display_name); - if (match.Success) - { - int majorVersion = Int32.Parse(match.Groups["majorVersion"].Value); - return majorVersion >= 4; - } - else - { - return false; - } + return match.Success + && int.Parse(match.Groups["majorVersion"].Value) >= 4; } - private static readonly Regex versionMatcher = new Regex( - "^\\s*(?\\d+)\\.\\d+\\.\\d+\\s*\\(", - RegexOptions.Compiled - ); + private static readonly Regex versionMatcher = + new Regex("^\\s*(?\\d+)\\.\\d+\\.\\d+\\s*\\(", + RegexOptions.Compiled); } }