protected BridgeResponse SyncInvokePlatform(CallPlatformMethod invokeMethod, params JsonData[] args) { var request = BridgeRequest.Create(invokeMethod.ToString(), args); var requestJson = request.ToJson(); BridgeResponse response = null; try { var result = InvokePlatform(requestJson); response = BridgeResponse.Deserialize(result); if (response == null) { DebugUtility.LogError(LoggerTags.Module, "同步调用 -> arg:{0}; 解析错误 -> result:{1}", requestJson, result); response = BridgeResponse.CreateErrorResponse(request.id, BridgeCode.ReturnTypeError); return(response); } else { DebugUtility.Log(LoggerTags.Module, "同步调用 -> arg:{0}; 返回 -> result:{1}", requestJson, result); return(response); } } catch (Exception ex) { DebugUtility.LogError(LoggerTags.Module, "同步调用 -> arg:{0}; 异常 -> Exception:{1}", requestJson, ex); response = BridgeResponse.CreateErrorResponse(request.id, BridgeCode.InvokeParamError); return(response); } }
internal void RequestSupportCmdInfo(BridgeRequest req, BridgeResponse rep) { var json = req.GetArgument(); if (json.IsObject) { if (json.ContainsKey("protocol")) { JsonData protocols = json["protocol"]; if (protocols.IsArray) { JsonData result = new JsonData(); result.SetJsonType(JsonType.Object); var protocolsCount = protocols.Count; for (int i = 0; i < protocolsCount; i++) { var protocol = (string)protocols[i]; if (string.IsNullOrEmpty(protocol) || protocol == "Explorer") { protocol = "Explorer"; var support = CommandFactory.GetCommandSupprtList(typeof(ExploreProtocol)); result[protocol] = LitJsonHelper.CreateArrayJsonData(support); } } rep.SetResult(BridgeCode.Success, result); return; } } } DebugUtility.LogError(LoggerTags.Project, "The first argument is not JSON object, examples: 'protocol': [ 'Explorer' ] "); rep.SetFailureResult(BridgeCode.Failure); }
public void call(string args) { var request = BridgeRequest.Deserialize(args); if (request == null) { DebugUtility.LogError(LoggerTags.Module, "平台调用 -> 异常args:{0}", args); return; } DebugUtility.Log(LoggerTags.Module, "平台调用 -> 传入args:{0}", args); var response = BridgeResponse.CreateDefaultResponse(request.id, request.callbackMethod); var invokeMethod = request.invokeMethod.ToEnum(CallUnityMethod.Unknown); var method = caller.PullListener(invokeMethod); if (method != null) { try { method(request, response); } catch (Exception ex) { DebugUtility.LogException(ex); response.SetFailureResult(BridgeCode.Failure); caller.DefaultCallBackPlatform(response); } } else { DebugUtility.LogError(LoggerTags.Module, "平台调用 -> 方法未监听!method:{0}, id:{1}", request.invokeMethod, request.id); response.SetFailureResult(BridgeCode.NorFunc); caller.DefaultCallBackPlatform(response); } }
public static BridgeRequest Deserialize(string jsonString) { if (string.IsNullOrEmpty(jsonString)) { return(null); } try { var ret = new BridgeRequest(); var json = JsonMapper.ToObject(jsonString); var keys = json.Keys; var id = (int)json["id"]; var invokeMethod = (string)json["func"]; var args = keys.Contains("args") ? json["args"] : string.Empty; var callbackMethod = keys.Contains("callback") ? (string)json["callback"] : string.Empty; ret.id = id; ret.invokeMethod = invokeMethod; ret.args = args; ret.callbackMethod = callbackMethod; return(ret); } catch (Exception ex) { DebugUtility.LogException(ex); return(null); } }
public static BridgeRequest Create(string invokeMethod, params JsonData[] args) { if (requestId >= int.MaxValue) { requestId = 0; } var id = ++requestId; var ret = new BridgeRequest(); ret.id = id; ret.invokeMethod = invokeMethod; ret.args = ToArgs(args); return(ret); }
protected void AsyncInvokePlatform(CallPlatformMethod invokeMethod, CallUnityMethod callbackMethod, Action <BridgeResponse> method, params JsonData[] args) { var request = BridgeRequest.Create(invokeMethod.ToString(), callbackMethod.ToString(), args); var requestJson = request.ToJson(); try { DebugUtility.Log(LoggerTags.Module, "异步调用 -> arg:{0};", requestJson); PushListener(request.callbackMethod, method); InvokePlatform(requestJson); } catch (Exception ex) { DebugUtility.LogError(LoggerTags.Module, "异步调用 -> arg:{0}; 异常 -> Exception:{1}", requestJson, ex); } }
internal void OnRecvMessage(BridgeRequest req, BridgeResponse rep) { var json = req.GetArgument(); string message = ""; if (json.IsObject) { if (json.ContainsKey("message")) { message = json["message"].ToJson(); } } else if (json.IsString) { message = (string)json; } onRecvMessageEvent.SafeInvoke(message); }
internal void Startup(BridgeRequest req, BridgeResponse rep) { var json = req.GetArgument(); string sceneID = ""; if (json.IsObject) { if (json.ContainsKey("sceneID")) { sceneID = json["sceneID"].ToJson(); } } // temp code if (!string.IsNullOrEmpty(sceneID)) { Loki.UI.WindowManager.CloseAll(); UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(sceneID, UnityEngine.SceneManagement.LoadSceneMode.Single).completed += (h) => { Loki.UI.WindowManager.Open <SimulationWindow>(); }; } }
internal void Shutdown(BridgeRequest req, BridgeResponse rep) { }