aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/Grpc.Core/Internal/PlatformApis.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/csharp/Grpc.Core/Internal/PlatformApis.cs')
-rw-r--r--src/csharp/Grpc.Core/Internal/PlatformApis.cs28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/csharp/Grpc.Core/Internal/PlatformApis.cs b/src/csharp/Grpc.Core/Internal/PlatformApis.cs
index 6bb4242479..b90fbccb2b 100644
--- a/src/csharp/Grpc.Core/Internal/PlatformApis.cs
+++ b/src/csharp/Grpc.Core/Internal/PlatformApis.cs
@@ -23,6 +23,7 @@ using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
+using Grpc.Core.Utils;
namespace Grpc.Core.Internal
{
@@ -31,11 +32,13 @@ namespace Grpc.Core.Internal
/// </summary>
internal static class PlatformApis
{
+ const string UnityEngineApplicationClassName = "UnityEngine.Application, UnityEngine";
static readonly bool isLinux;
static readonly bool isMacOSX;
static readonly bool isWindows;
static readonly bool isMono;
static readonly bool isNetCore;
+ static readonly bool isUnity;
static PlatformApis()
{
@@ -54,6 +57,7 @@ namespace Grpc.Core.Internal
isNetCore = false;
#endif
isMono = Type.GetType("Mono.Runtime") != null;
+ isUnity = Type.GetType(UnityEngineApplicationClassName) != null;
}
public static bool IsLinux
@@ -77,6 +81,14 @@ namespace Grpc.Core.Internal
}
/// <summary>
+ /// true if running on Unity platform.
+ /// </summary>
+ public static bool IsUnity
+ {
+ get { return isUnity; }
+ }
+
+ /// <summary>
/// true if running on .NET Core (CoreCLR), false otherwise.
/// </summary>
public static bool IsNetCore
@@ -89,6 +101,22 @@ namespace Grpc.Core.Internal
get { return IntPtr.Size == 8; }
}
+ /// <summary>
+ /// Returns <c>UnityEngine.Application.platform</c> as a string.
+ /// See https://docs.unity3d.com/ScriptReference/Application-platform.html for possible values.
+ /// Value is obtained via reflection to avoid compile-time dependency on Unity.
+ /// This method should only be called if <c>IsUnity</c> is <c>true</c>.
+ /// </summary>
+ public static string GetUnityRuntimePlatform()
+ {
+ GrpcPreconditions.CheckState(IsUnity, "Not running on Unity.");
+#if NETSTANDARD1_5
+ return Type.GetType(UnityEngineApplicationClassName).GetTypeInfo().GetProperty("platform").GetValue(null).ToString();
+#else
+ return Type.GetType(UnityEngineApplicationClassName).GetProperty("platform").GetValue(null).ToString();
+#endif
+ }
+
[DllImport("libc")]
static extern int uname(IntPtr buf);