| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System;
- using System.Collections.Concurrent;
- using System.Runtime.InteropServices;
- using UnityEngine;
- using TapSDK.Core.Internal.Log;
- namespace TapSDK.Core
- {
- public class BridgeIOS : IBridge
- {
- private static readonly BridgeIOS SInstance = new BridgeIOS();
- private readonly ConcurrentDictionary<string, Action<Result>> dic;
- public static BridgeIOS GetInstance()
- {
- return SInstance;
- }
- private BridgeIOS()
- {
- dic = new ConcurrentDictionary<string, Action<Result>>();
- }
- private ConcurrentDictionary<string, Action<Result>> GetConcurrentDictionary()
- {
- return dic;
- }
- private delegate void EngineBridgeDelegate(string result);
- [AOT.MonoPInvokeCallbackAttribute(typeof(EngineBridgeDelegate))]
- static void engineBridgeDelegate(string resultJson)
- {
- var result = new Result(resultJson);
- var actionDic = GetInstance().GetConcurrentDictionary();
- Action<Result> action = null;
- if (actionDic != null && actionDic.ContainsKey(result.callbackId))
- {
- action = actionDic[result.callbackId];
- }
- if (action != null)
- {
- action(result);
- if (result.onceTime && BridgeIOS.GetInstance().GetConcurrentDictionary()
- .TryRemove(result.callbackId, out Action<Result> outAction))
- {
- TapLog.Log($"TapSDK resolved current Action:{result.callbackId}");
- }
- }
- }
-
-
- public void Register(string serviceClz, string serviceImp)
- {
- //IOS无需注册
- }
- public void Call(Command command)
- {
- #if UNITY_IOS
- callHandler(command.ToJSON());
- #endif
- }
- public void Call(Command command, Action<Result> action)
- {
- if (!command.withCallback || string.IsNullOrEmpty(command.callbackId)) return;
- if (!dic.ContainsKey(command.callbackId))
- {
- dic.GetOrAdd(command.callbackId, action);
- }
- #if UNITY_IOS
- registerHandler(command.ToJSON(), engineBridgeDelegate);
- #endif
- }
- public string CallWithReturnValue(Command command, Action<Result> action)
- {
- if (command.callbackId != null && !dic.ContainsKey(command.callbackId))
- {
- dic.GetOrAdd(command.callbackId, action);
- }
- #if UNITY_IOS
- if (action == null)
- {
- return callWithReturnValue(command.ToJSON(), null);
- } else {
- return callWithReturnValue(command.ToJSON(), engineBridgeDelegate);
- }
- #else
- return null;
- #endif
- }
- #if UNITY_IOS
- [DllImport("__Internal")]
- private static extern string callWithReturnValue(string command, EngineBridgeDelegate engineBridgeDelegate);
- [DllImport("__Internal")]
- private static extern void callHandler(string command);
- [DllImport("__Internal")]
- private static extern void registerHandler(string command, EngineBridgeDelegate engineBridgeDelegate);
- #endif
- }
- }
|