Skip to content

Commit

Permalink
Modernize platform checks (requires .NET Framework 4.7.1+)
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Nov 14, 2023
1 parent ce2262a commit 7fe950f
Showing 1 changed file with 15 additions and 53 deletions.
68 changes: 15 additions & 53 deletions Core/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ static Platform()
/// Are we on a Mac?
/// </summary>
/// <value><c>true</c> if is mac; otherwise, <c>false</c>.</value>
public static readonly bool IsMac = IsRunningOnMac();
public static readonly bool IsMac = RuntimeInformation.IsOSPlatform(OSPlatform.OSX);

/// <summary>
/// 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.
/// </summary>
/// <value><c>true</c> if is unix; otherwise, <c>false</c>.</value>
public static readonly bool IsUnix = Environment.OSVersion.Platform == PlatformID.Unix && !IsMac;
public static readonly bool IsUnix = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);

/// <summary>
/// Are we on a flavour of Windows? This is implemented internally by checking
/// if we're not on Unix or Mac.
/// </summary>
/// <value><c>true</c> if is windows; otherwise, <c>false</c>.</value>
public static readonly bool IsWindows = !IsUnix && !IsMac;
public static readonly bool IsWindows = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);

/// <summary>
/// Are we on Mono?
Expand All @@ -53,66 +52,29 @@ static Platform()
/// <summary>
/// Are we running in an X11 environment?
/// </summary>
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()
{
if (!IsMono)
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*(?<majorVersion>\\d+)\\.\\d+\\.\\d+\\s*\\(",
RegexOptions.Compiled
);
private static readonly Regex versionMatcher =
new Regex("^\\s*(?<majorVersion>\\d+)\\.\\d+\\.\\d+\\s*\\(",
RegexOptions.Compiled);
}
}

0 comments on commit 7fe950f

Please sign in to comment.