Skip to content

Commit

Permalink
imp - Cache sub-platform detection
Browse files Browse the repository at this point in the history
---

We've cached the sub-platform detection code for better performance.

---

Type: imp
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Nov 30, 2024
1 parent cb6ab4c commit 1ebee9e
Showing 1 changed file with 63 additions and 23 deletions.
86 changes: 63 additions & 23 deletions SpecProbe.Software/Platform/PlatformHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,24 @@ namespace SpecProbe.Software.Platform
/// </summary>
public static class PlatformHelper
{
private static Platform platform = Platform.Unknown;
private static bool firstTimeMuslDetection = true;
private static bool isMuslLinux = false;
private static bool firstTimeAndroidDetection = true;
private static bool isAndroid = false;
private static bool firstTimeWslDetection = true;
private static bool isWsl = false;

/// <summary>
/// Is this system a Windows system?
/// </summary>
/// <returns>True if running on Windows (Windows 10, Windows 11, etc.). Otherwise, false.</returns>
public static bool IsOnWindows() =>
Environment.OSVersion.Platform == PlatformID.Win32NT;
public static bool IsOnWindows()
{
if (platform == Platform.Windows)
return true;
return Environment.OSVersion.Platform == PlatformID.Win32NT;
}

/// <summary>
/// Is this system a Windows system or a WSL system?
Expand All @@ -49,15 +61,21 @@ public static bool IsOnWindowsOrWsl() =>
/// Is this system a Unix system? True for macOS, too!
/// </summary>
/// <returns>True if running on Unix (Linux, *nix, etc.). Otherwise, false.</returns>
public static bool IsOnUnix() =>
Environment.OSVersion.Platform == PlatformID.Unix;
public static bool IsOnUnix()
{
if (platform == Platform.Linux)
return true;
return Environment.OSVersion.Platform == PlatformID.Unix;
}

/// <summary>
/// Is this system a macOS system?
/// </summary>
/// <returns>True if running on macOS (MacBook, iMac, etc.). Otherwise, false.</returns>
public static bool IsOnMacOS()
{
if (platform == Platform.MacOS)
return true;
if (IsOnUnix())
{
string System = UnameManager.GetUname(UnameTypes.KernelName);
Expand All @@ -73,10 +91,10 @@ public static bool IsOnMacOS()
/// <returns>True if running on Android phones using Termux. Otherwise, false.</returns>
public static bool IsOnAndroid()
{
if (IsOnUnix() && !IsOnMacOS())
return File.Exists("/system/build.prop");
else
return false;
if (IsOnUnix() && !IsOnMacOS() && firstTimeAndroidDetection)
isAndroid = File.Exists("/system/build.prop");
firstTimeAndroidDetection = false;
return isAndroid;
}

/// <summary>
Expand Down Expand Up @@ -107,17 +125,22 @@ public static bool IsOnArm64() =>
/// <returns>True if running on Unix systems that use musl libc. Otherwise, false.</returns>
public static bool IsOnUnixMusl()
{
if (!firstTimeMuslDetection)
return isMuslLinux;
try
{
if (!IsOnUnix() || IsOnMacOS() || IsOnWindows())
return false;
var gnuRel = gnuGetLibcVersion();
return false;
isMuslLinux = false;
}
catch
{
return true;
// Android uses Bionic instead of GNU and MUSL.
isMuslLinux = !IsOnAndroid();
}
firstTimeMuslDetection = false;
return isMuslLinux;
}

/// <summary>
Expand All @@ -126,15 +149,23 @@ public static bool IsOnUnixMusl()
/// <returns>True if running on WSL. Otherwise, false.</returns>
public static bool IsOnUnixWsl()
{
// Now, get a path that allows us to detect WSL using WSLInterop.
if (!firstTimeWslDetection)
return isWsl;

// Get a path that allows us to detect WSL using WSLInterop.
string wslInteropPath = "/proc/sys/fs/binfmt_misc/WSLInterop";
string wslInteropMagic = "4d5a";

// Check to see if we have this file
if (!File.Exists(wslInteropPath))
return false;
string stream = File.ReadAllText(wslInteropPath);
return stream.Contains(wslInteropMagic);
if (File.Exists(wslInteropPath))
{
string stream = File.ReadAllText(wslInteropPath);
isWsl = stream.Contains(wslInteropMagic);
}
else
isWsl = false;
firstTimeWslDetection = false;
return isWsl;
}

/// <summary>
Expand Down Expand Up @@ -196,14 +227,18 @@ public static string GetCurrentGenericRid(bool includeMusl = true) =>
/// <exception cref="PlatformNotSupportedException"></exception>
public static Platform GetPlatform()
{
if (IsOnWindows())
return Platform.Windows;
else if (IsOnMacOS())
return Platform.MacOS;
else if (IsOnUnix())
return Platform.Linux;
else
throw new PlatformNotSupportedException("This operating system is not supported.");
if (platform == Platform.Unknown)
{
if (IsOnWindows())
platform = Platform.Windows;
else if (IsOnMacOS())
platform = Platform.MacOS;
else if (IsOnUnix())
platform = Platform.Linux;
else
throw new PlatformNotSupportedException("This operating system is not supported.");
}
return platform;
}

/// <summary>
Expand Down Expand Up @@ -270,6 +305,11 @@ internal static string ExecuteProcessToString(string File, string Args)
return CommandProcess.StandardOutput.ReadToEnd();
}

static PlatformHelper()
{
GetPlatform();
}

#region Interop
[DllImport("libc", EntryPoint = "gnu_get_libc_version")]
private static extern IntPtr gnuGetLibcVersion();
Expand Down

0 comments on commit 1ebee9e

Please sign in to comment.