From 1ebee9e21a9004f530cdc0ab9bcd541fb9536b91 Mon Sep 17 00:00:00 2001 From: Aptivi CEO Date: Sat, 30 Nov 2024 18:20:51 +0300 Subject: [PATCH] imp - Cache sub-platform detection --- We've cached the sub-platform detection code for better performance. --- Type: imp Breaking: False Doc Required: False Backport Required: False Part: 1/1 --- SpecProbe.Software/Platform/PlatformHelper.cs | 86 ++++++++++++++----- 1 file changed, 63 insertions(+), 23 deletions(-) diff --git a/SpecProbe.Software/Platform/PlatformHelper.cs b/SpecProbe.Software/Platform/PlatformHelper.cs index e4a407b..73a0a78 100644 --- a/SpecProbe.Software/Platform/PlatformHelper.cs +++ b/SpecProbe.Software/Platform/PlatformHelper.cs @@ -31,12 +31,24 @@ namespace SpecProbe.Software.Platform /// 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; + /// /// Is this system a Windows system? /// /// True if running on Windows (Windows 10, Windows 11, etc.). Otherwise, false. - 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; + } /// /// Is this system a Windows system or a WSL system? @@ -49,8 +61,12 @@ public static bool IsOnWindowsOrWsl() => /// Is this system a Unix system? True for macOS, too! /// /// True if running on Unix (Linux, *nix, etc.). Otherwise, false. - 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; + } /// /// Is this system a macOS system? @@ -58,6 +74,8 @@ public static bool IsOnUnix() => /// True if running on macOS (MacBook, iMac, etc.). Otherwise, false. public static bool IsOnMacOS() { + if (platform == Platform.MacOS) + return true; if (IsOnUnix()) { string System = UnameManager.GetUname(UnameTypes.KernelName); @@ -73,10 +91,10 @@ public static bool IsOnMacOS() /// True if running on Android phones using Termux. Otherwise, false. 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; } /// @@ -107,17 +125,22 @@ public static bool IsOnArm64() => /// True if running on Unix systems that use musl libc. Otherwise, false. 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; } /// @@ -126,15 +149,23 @@ public static bool IsOnUnixMusl() /// True if running on WSL. Otherwise, false. 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; } /// @@ -196,14 +227,18 @@ public static string GetCurrentGenericRid(bool includeMusl = true) => /// 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; } /// @@ -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();