From d35471afd5244b89dddb4ade684ffde975a5ca03 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Wed, 7 Mar 2018 20:37:02 +0100 Subject: [PATCH] detect when running on Unity iOS --- src/csharp/Grpc.Core/Internal/NativeExtension.cs | 10 ++++++++-- src/csharp/Grpc.Core/Internal/PlatformApis.cs | 13 +++++++++++++ 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 1b99c979561..333f677781f 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 /// 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 9456b30df89..ce99ba4d773 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; } } + /// + /// Returns UnityEngine.Application.platform 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 IsUnity is true. + /// + 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);