From 45c32279de23a47f6522edc030eec171fc67d754 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Wed, 29 Jan 2025 00:12:01 +1100 Subject: [PATCH] Update OperatingSystemPolyfill.cs --- src/Polyfill/OperatingSystemPolyfill.cs | 67 ++++++++++++------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/Polyfill/OperatingSystemPolyfill.cs b/src/Polyfill/OperatingSystemPolyfill.cs index 155ec5f5..4395895c 100644 --- a/src/Polyfill/OperatingSystemPolyfill.cs +++ b/src/Polyfill/OperatingSystemPolyfill.cs @@ -21,33 +21,6 @@ static class OperatingSystemPolyfill { #if !NET5_0_OR_GREATER - static Version GetFreeBSDVersion() - { - if (!IsFreeBSD()) - { - return Environment.OSVersion.Version; - } - - var version = Environment.OSVersion.VersionString - .Replace("Unix", string.Empty).Replace("FreeBSD", string.Empty) - .Replace("-release", string.Empty).Replace(" ", string.Empty); - - return Version.Parse(version); - } - - static Version GetAndroidVersion() - { - if (!IsAndroid()) - { - return Environment.OSVersion.Version; - } - - var result = RunProcess("getprop", "ro.build.version.release") - .Replace(" ", string.Empty); - - return Version.Parse(result); - } - static string RunProcess(string name, string arguments) { using var process = new Process() @@ -244,12 +217,27 @@ public static bool IsFreeBSD() => /// The version revision number. /// true if the current application is running on a FreeBSD version that is at least what was specified in the parameters; false otherwise. //Link: https://learn.microsoft.com/en-us/dotnet/api/system.operatingsystem.isfreebsdversionatleast - public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0) => + public static bool IsFreeBSDVersionAtLeast(int major, int minor, int build = 0, int revision = 0) + { #if NET5_0_OR_GREATER - OperatingSystem.IsFreeBSDVersionAtLeast(major, minor, build, revision); + return OperatingSystem.IsFreeBSDVersionAtLeast(major, minor, build, revision); #else - GetFreeBSDVersion() >= new Version(major, minor, build, revision); + if (!IsFreeBSD()) + { + return false; + } + + var versionString = Environment.OSVersion.VersionString + .Replace("Unix", string.Empty) + .Replace("FreeBSD", string.Empty) + .Replace("-release", string.Empty) + .Replace(" ", string.Empty); + + var version = Version.Parse(versionString); + + return version >= new Version(major, minor, build, revision); #endif + } /// /// Indicates whether the current application is running on iOS or MacCatalyst. @@ -346,13 +334,24 @@ public static bool IsAndroid() /// The revision release number. /// true if the current application is running on an Android version that is at least what was specified in the parameters; false otherwise. //Link: https://learn.microsoft.com/en-us/dotnet/api/system.operatingsystem.isandroidversionatleast - public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0) => + public static bool IsAndroidVersionAtLeast(int major, int minor = 0, int build = 0, int revision = 0) + { #if NET5_0_OR_GREATER - OperatingSystem.IsAndroidVersionAtLeast(major, minor, build, revision); + return OperatingSystem.IsAndroidVersionAtLeast(major, minor, build, revision); #else - IsAndroid() && - GetAndroidVersion() >= new Version(major, minor, build, revision); + if (!IsAndroid()) + { + return false; + } + + var result = RunProcess("getprop", "ro.build.version.release") + .Replace(" ", string.Empty); + + var version = Version.Parse(result); + + return version >= new Version(major, minor, build, revision); #endif + } /// /// Indicates whether the current application is running on watchOS.