BridgeIOS.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Runtime.InteropServices;
  4. using UnityEngine;
  5. using TapSDK.Core.Internal.Log;
  6. namespace TapSDK.Core
  7. {
  8. public class BridgeIOS : IBridge
  9. {
  10. private static readonly BridgeIOS SInstance = new BridgeIOS();
  11. private readonly ConcurrentDictionary<string, Action<Result>> dic;
  12. public static BridgeIOS GetInstance()
  13. {
  14. return SInstance;
  15. }
  16. private BridgeIOS()
  17. {
  18. dic = new ConcurrentDictionary<string, Action<Result>>();
  19. }
  20. private ConcurrentDictionary<string, Action<Result>> GetConcurrentDictionary()
  21. {
  22. return dic;
  23. }
  24. private delegate void EngineBridgeDelegate(string result);
  25. [AOT.MonoPInvokeCallbackAttribute(typeof(EngineBridgeDelegate))]
  26. static void engineBridgeDelegate(string resultJson)
  27. {
  28. var result = new Result(resultJson);
  29. var actionDic = GetInstance().GetConcurrentDictionary();
  30. Action<Result> action = null;
  31. if (actionDic != null && actionDic.ContainsKey(result.callbackId))
  32. {
  33. action = actionDic[result.callbackId];
  34. }
  35. if (action != null)
  36. {
  37. action(result);
  38. if (result.onceTime && BridgeIOS.GetInstance().GetConcurrentDictionary()
  39. .TryRemove(result.callbackId, out Action<Result> outAction))
  40. {
  41. TapLog.Log($"TapSDK resolved current Action:{result.callbackId}");
  42. }
  43. }
  44. }
  45. public void Register(string serviceClz, string serviceImp)
  46. {
  47. //IOS无需注册
  48. }
  49. public void Call(Command command)
  50. {
  51. #if UNITY_IOS
  52. callHandler(command.ToJSON());
  53. #endif
  54. }
  55. public void Call(Command command, Action<Result> action)
  56. {
  57. if (!command.withCallback || string.IsNullOrEmpty(command.callbackId)) return;
  58. if (!dic.ContainsKey(command.callbackId))
  59. {
  60. dic.GetOrAdd(command.callbackId, action);
  61. }
  62. #if UNITY_IOS
  63. registerHandler(command.ToJSON(), engineBridgeDelegate);
  64. #endif
  65. }
  66. public string CallWithReturnValue(Command command, Action<Result> action)
  67. {
  68. if (command.callbackId != null && !dic.ContainsKey(command.callbackId))
  69. {
  70. dic.GetOrAdd(command.callbackId, action);
  71. }
  72. #if UNITY_IOS
  73. if (action == null)
  74. {
  75. return callWithReturnValue(command.ToJSON(), null);
  76. } else {
  77. return callWithReturnValue(command.ToJSON(), engineBridgeDelegate);
  78. }
  79. #else
  80. return null;
  81. #endif
  82. }
  83. #if UNITY_IOS
  84. [DllImport("__Internal")]
  85. private static extern string callWithReturnValue(string command, EngineBridgeDelegate engineBridgeDelegate);
  86. [DllImport("__Internal")]
  87. private static extern void callHandler(string command);
  88. [DllImport("__Internal")]
  89. private static extern void registerHandler(string command, EngineBridgeDelegate engineBridgeDelegate);
  90. #endif
  91. }
  92. }