aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-07 20:37:02 +0100
committerGravatar Jan Tattermusch <jtattermusch@google.com>2018-03-07 20:37:02 +0100
commitd35471afd5244b89dddb4ade684ffde975a5ca03 (patch)
tree342b0ce80e621b7c6f558255a876622b9ace71d0 /src
parentb988e396d29ca6e9d04b45a97efe8827a15ae8e5 (diff)
detect when running on Unity iOS
Diffstat (limited to 'src')
-rw-r--r--src/csharp/Grpc.Core/Internal/NativeExtension.cs10
-rw-r--r--src/csharp/Grpc.Core/Internal/PlatformApis.cs13
2 files changed, 21 insertions, 2 deletions
diff --git a/src/csharp/Grpc.Core/Internal/NativeExtension.cs b/src/csharp/Grpc.Core/Internal/NativeExtension.cs
index 1b99c97956..333f677781 100644
--- a/src/csharp/Grpc.Core/Internal/NativeExtension.cs
+++ b/src/csharp/Grpc.Core/Internal/NativeExtension.cs
@@ -114,8 +114,14 @@ namespace Grpc.Core.Internal
/// </summary>
private static NativeMethods LoadNativeMethodsUnity()
{
- // TODO(jtattermusch): use NativeMethods.DllImportsFromStaticLib for iOS.
- return new NativeMethods(new NativeMethods.DllImportsFromSharedLib());
+ switch (PlatformApis.GetUnityRuntimePlatform())
+ {
+ case "IPhonePlayer":
+ return new NativeMethods(new NativeMethods.DllImportsFromStaticLib());
+ default:
+ // most other platforms load unity plugins as a shared library
+ return new NativeMethods(new NativeMethods.DllImportsFromSharedLib());
+ }
}
private static string GetAssemblyPath()
diff --git a/src/csharp/Grpc.Core/Internal/PlatformApis.cs b/src/csharp/Grpc.Core/Internal/PlatformApis.cs
index 9456b30df8..ce99ba4d77 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
{
@@ -99,6 +100,18 @@ 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.");
+ return Type.GetType("UnityEngine.Application, UnityEngine").GetProperty("platform").GetValue(null).ToString();
+ }
+
[DllImport("libc")]
static extern int uname(IntPtr buf);