From 625c41c0ea39bb2af37ece7865098cf2f6d38471 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 23 Aug 2020 18:45:54 -0400 Subject: move file for upcoming change --- .../Framework/LowLevelEnvironmentUtility.cs | 170 +++++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs (limited to 'src/SMAPI.Toolkit/Framework') diff --git a/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs b/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs new file mode 100644 index 00000000..1e490448 --- /dev/null +++ b/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs @@ -0,0 +1,170 @@ +using System; +using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +#if SMAPI_FOR_WINDOWS +using System.Management; +#endif +using System.Runtime.InteropServices; + +namespace StardewModdingAPI.Toolkit.Utilities +{ + /// Provides methods for fetching environment information. + public static class EnvironmentUtility + { + /********* + ** Fields + *********/ + /// The cached platform. + private static Platform? CachedPlatform; + + /// Get the OS name from the system uname command. + /// The buffer to fill with the resulting string. + [DllImport("libc")] + static extern int uname(IntPtr buffer); + + + /********* + ** Public methods + *********/ + /// Detect the current OS. + public static Platform DetectPlatform() + { + return EnvironmentUtility.CachedPlatform ??= EnvironmentUtility.DetectPlatformImpl(); + } + + + /// Get the human-readable OS name and version. + /// The current platform. + [SuppressMessage("ReSharper", "EmptyGeneralCatchClause", Justification = "Error suppressed deliberately to fallback to default behaviour.")] + public static string GetFriendlyPlatformName(Platform platform) + { +#if SMAPI_FOR_WINDOWS + try + { + return new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem") + .Get() + .Cast() + .Select(entry => entry.GetPropertyValue("Caption").ToString()) + .FirstOrDefault(); + } + catch { } +#endif + + string name = Environment.OSVersion.ToString(); + switch (platform) + { + case Platform.Android: + name = $"Android {name}"; + break; + + case Platform.Mac: + name = $"MacOS {name}"; + break; + } + return name; + } + + /// Get the name of the Stardew Valley executable. + /// The current platform. + public static string GetExecutableName(Platform platform) + { + return platform == Platform.Windows + ? "Stardew Valley.exe" + : "StardewValley.exe"; + } + + /// Get whether the platform uses Mono. + /// The current platform. + public static bool IsMono(this Platform platform) + { + return platform == Platform.Linux || platform == Platform.Mac; + } + + + /********* + ** Private methods + *********/ + /// Detect the current OS. + private static Platform DetectPlatformImpl() + { + switch (Environment.OSVersion.Platform) + { + case PlatformID.MacOSX: + return Platform.Mac; + + case PlatformID.Unix when EnvironmentUtility.IsRunningAndroid(): + return Platform.Android; + + case PlatformID.Unix when EnvironmentUtility.IsRunningMac(): + return Platform.Mac; + + case PlatformID.Unix: + return Platform.Linux; + + default: + return Platform.Windows; + } + } + + /// Detect whether the code is running on Android. + /// + /// This code is derived from https://stackoverflow.com/a/47521647/262123. It detects Android by calling the + /// getprop system command to check for an Android-specific property. + /// + private static bool IsRunningAndroid() + { + using Process process = new Process + { + StartInfo = + { + FileName = "getprop", + Arguments = "ro.build.user", + RedirectStandardOutput = true, + UseShellExecute = false, + CreateNoWindow = true + } + }; + + try + { + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + return !string.IsNullOrWhiteSpace(output); + } + catch + { + return false; + } + } + + /// Detect whether the code is running on Mac. + /// + /// This code is derived from the Mono project (see System.Windows.Forms/System.Windows.Forms/XplatUI.cs). It detects Mac by calling the + /// uname system command and checking the response, which is always 'Darwin' for MacOS. + /// + private static bool IsRunningMac() + { + IntPtr buffer = IntPtr.Zero; + try + { + buffer = Marshal.AllocHGlobal(8192); + if (EnvironmentUtility.uname(buffer) == 0) + { + string os = Marshal.PtrToStringAnsi(buffer); + return os == "Darwin"; + } + return false; + } + catch + { + return false; // default to Linux + } + finally + { + if (buffer != IntPtr.Zero) + Marshal.FreeHGlobal(buffer); + } + } + } +} -- cgit From 76c926c396092f02441a62937bfc5d437e582e57 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 23 Aug 2020 18:45:04 -0400 Subject: add EarlyConstants for constants needed before external DLLs are loaded --- .../Framework/LowLevelEnvironmentUtility.cs | 69 ++++++++++------------ 1 file changed, 31 insertions(+), 38 deletions(-) (limited to 'src/SMAPI.Toolkit/Framework') diff --git a/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs b/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs index 1e490448..b01d8b21 100644 --- a/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs +++ b/src/SMAPI.Toolkit/Framework/LowLevelEnvironmentUtility.cs @@ -6,18 +6,17 @@ using System.Linq; using System.Management; #endif using System.Runtime.InteropServices; +using StardewModdingAPI.Toolkit.Utilities; -namespace StardewModdingAPI.Toolkit.Utilities +namespace StardewModdingAPI.Toolkit.Framework { - /// Provides methods for fetching environment information. - public static class EnvironmentUtility + /// Provides low-level methods for fetching environment information. + /// This is used by the SMAPI core before the toolkit DLL is available; most code should use instead. + internal static class LowLevelEnvironmentUtility { /********* ** Fields *********/ - /// The cached platform. - private static Platform? CachedPlatform; - /// Get the OS name from the system uname command. /// The buffer to fill with the resulting string. [DllImport("libc")] @@ -28,16 +27,32 @@ namespace StardewModdingAPI.Toolkit.Utilities ** Public methods *********/ /// Detect the current OS. - public static Platform DetectPlatform() + public static string DetectPlatform() { - return EnvironmentUtility.CachedPlatform ??= EnvironmentUtility.DetectPlatformImpl(); + switch (Environment.OSVersion.Platform) + { + case PlatformID.MacOSX: + return nameof(Platform.Mac); + + case PlatformID.Unix when LowLevelEnvironmentUtility.IsRunningAndroid(): + return nameof(Platform.Android); + + case PlatformID.Unix when LowLevelEnvironmentUtility.IsRunningMac(): + return nameof(Platform.Mac); + + case PlatformID.Unix: + return nameof(Platform.Linux); + + default: + return nameof(Platform.Windows); + } } /// Get the human-readable OS name and version. /// The current platform. [SuppressMessage("ReSharper", "EmptyGeneralCatchClause", Justification = "Error suppressed deliberately to fallback to default behaviour.")] - public static string GetFriendlyPlatformName(Platform platform) + public static string GetFriendlyPlatformName(string platform) { #if SMAPI_FOR_WINDOWS try @@ -54,11 +69,11 @@ namespace StardewModdingAPI.Toolkit.Utilities string name = Environment.OSVersion.ToString(); switch (platform) { - case Platform.Android: + case nameof(Platform.Android): name = $"Android {name}"; break; - case Platform.Mac: + case nameof(Platform.Mac): name = $"MacOS {name}"; break; } @@ -67,46 +82,24 @@ namespace StardewModdingAPI.Toolkit.Utilities /// Get the name of the Stardew Valley executable. /// The current platform. - public static string GetExecutableName(Platform platform) + public static string GetExecutableName(string platform) { - return platform == Platform.Windows + return platform == nameof(Platform.Windows) ? "Stardew Valley.exe" : "StardewValley.exe"; } /// Get whether the platform uses Mono. /// The current platform. - public static bool IsMono(this Platform platform) + public static bool IsMono(string platform) { - return platform == Platform.Linux || platform == Platform.Mac; + return platform == nameof(Platform.Linux) || platform == nameof(Platform.Mac); } /********* ** Private methods *********/ - /// Detect the current OS. - private static Platform DetectPlatformImpl() - { - switch (Environment.OSVersion.Platform) - { - case PlatformID.MacOSX: - return Platform.Mac; - - case PlatformID.Unix when EnvironmentUtility.IsRunningAndroid(): - return Platform.Android; - - case PlatformID.Unix when EnvironmentUtility.IsRunningMac(): - return Platform.Mac; - - case PlatformID.Unix: - return Platform.Linux; - - default: - return Platform.Windows; - } - } - /// Detect whether the code is running on Android. /// /// This code is derived from https://stackoverflow.com/a/47521647/262123. It detects Android by calling the @@ -149,7 +142,7 @@ namespace StardewModdingAPI.Toolkit.Utilities try { buffer = Marshal.AllocHGlobal(8192); - if (EnvironmentUtility.uname(buffer) == 0) + if (LowLevelEnvironmentUtility.uname(buffer) == 0) { string os = Marshal.PtrToStringAnsi(buffer); return os == "Darwin"; -- cgit