add Android detection

This commit is contained in:
Jesse Plamondon-Willard 2019-08-31 17:46:47 -04:00
parent e02372bba9
commit 8cb190de08
No known key found for this signature in database
GPG Key ID: CF8B1456B3E29F49
3 changed files with 60 additions and 12 deletions

View File

@ -52,6 +52,7 @@ These changes have not been released yet.
* Added fields and methods: `IContentPack.HasFile`, `Context.IsGameLaunched`, and `SemanticVersion.TryParse`.
* Added separate `LogNetworkTraffic` option to make verbose logging less overwhelmingly verbose.
* Added asset propagation for critter textures and `DayTimeMoneyBox` buttons.
* `Constants.TargetPlatform` now returns `Android` when playing on an Android device.
* The installer now recognises custom game paths stored in `stardewvalley.targets`, if any.
* Trace logs for a broken mod now list all detected issues (instead of the first one).
* Trace logs when loading mods are now more clear.

View File

@ -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
*********/
/// <summary>The cached platform.</summary>
private static Platform? CachedPlatform;
/// <summary>Get the OS name from the system uname command.</summary>
/// <param name="buffer">The buffer to fill with the resulting string.</param>
[DllImport("libc")]
@ -26,19 +30,10 @@ namespace StardewModdingAPI.Toolkit.Utilities
/// <summary>Detect the current OS.</summary>
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
*********/
/// <summary>Detect the current OS.</summary>
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;
}
}
/// <summary>Detect whether the code is running on Android.</summary>
/// <remarks>
/// This code is derived from https://stackoverflow.com/a/47521647/262123. It detects Android by calling the
/// <c>getprop</c> system command to check for an Android-specific property.
/// </remarks>
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;
}
}
}
/// <summary>Detect whether the code is running on Mac.</summary>
/// <remarks>
/// This code is derived from the Mono project (see System.Windows.Forms/System.Windows.Forms/XplatUI.cs). It detects Mac by calling the

View File

@ -5,6 +5,9 @@ namespace StardewModdingAPI
/// <summary>The game's platform version.</summary>
public enum GamePlatform
{
/// <summary>The Android version of the game.</summary>
Android = Platform.Android,
/// <summary>The Linux version of the game.</summary>
Linux = Platform.Linux,