From 8cb190de080aad56372b1c63d8b4f977f3c81d01 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 31 Aug 2019 17:46:47 -0400 Subject: add Android detection --- src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs | 68 +++++++++++++++++++---- src/SMAPI/GamePlatform.cs | 3 + 2 files changed, 59 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs b/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs index 4f67c00e..6dce5da5 100644 --- a/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs +++ b/src/SMAPI.Toolkit/Utilities/EnvironmentUtility.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; #if SMAPI_FOR_WINDOWS @@ -14,6 +15,9 @@ namespace StardewModdingAPI.Toolkit.Utilities /********* ** 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")] @@ -26,19 +30,10 @@ namespace StardewModdingAPI.Toolkit.Utilities /// Detect the current OS. public static Platform DetectPlatform() { - switch (Environment.OSVersion.Platform) - { - case PlatformID.MacOSX: - return Platform.Mac; + if (EnvironmentUtility.CachedPlatform == null) + EnvironmentUtility.CachedPlatform = EnvironmentUtility.DetectPlatformImpl(); - case PlatformID.Unix: - return EnvironmentUtility.IsRunningMac() - ? Platform.Mac - : Platform.Linux; - - default: - return Platform.Windows; - } + return EnvironmentUtility.CachedPlatform.Value; } @@ -81,6 +76,55 @@ namespace StardewModdingAPI.Toolkit.Utilities /********* ** 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()) + { + process.StartInfo.FileName = "getprop"; + process.StartInfo.Arguments = "ro.build.user"; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; + try + { + process.Start(); + string output = process.StandardOutput.ReadToEnd(); + return !string.IsNullOrEmpty(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 diff --git a/src/SMAPI/GamePlatform.cs b/src/SMAPI/GamePlatform.cs index 174239e0..b64595e4 100644 --- a/src/SMAPI/GamePlatform.cs +++ b/src/SMAPI/GamePlatform.cs @@ -5,6 +5,9 @@ namespace StardewModdingAPI /// The game's platform version. public enum GamePlatform { + /// The Android version of the game. + Android = Platform.Android, + /// The Linux version of the game. Linux = Platform.Linux, -- cgit