diff --git a/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs b/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs index de8764f..120bf6c 100644 --- a/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs +++ b/Assets/PlayroomKit/modules/Helpers/InternalFunctions/InterlopWrapper.cs @@ -137,7 +137,8 @@ public void StartMatchmakingWrapper(Action callback) StartMatchmakingInternal(callback); } - public void RpcRegisterWrapper(string name, Action rpcRegisterCallback, string onResponseReturn = null) + public void RpcRegisterWrapper(string name, Action rpcRegisterCallback, + string onResponseReturn = null) { RpcRegisterInternal(name, rpcRegisterCallback, onResponseReturn); } @@ -146,20 +147,24 @@ public void RpcCallWrapper(string name, string data, RpcMode mode, Action callba { RpcCallInternal(name, data, mode, callbackOnResponse); } - - + + //Internal Functions [DllImport("__Internal")] private static extern void RpcRegisterInternal(string name, Action rpcRegisterCallback, string onResponseReturn = null); - + + [DllImport("__Internal")] + public static extern void RpcRegisterInternal(string name, Action rpcRegisterCallback, + string onResponseReturn = null); + [DllImport("__Internal")] private extern static void RpcCallInternal(string name, string data, RpcMode mode, Action callbackOnResponse); - + // - - + + //Player // Wrapper for KickInternal public void KickPlayerWrapper(string playerID, Action onKickCallback = null) @@ -168,7 +173,8 @@ public void KickPlayerWrapper(string playerID, Action onKickCallback = null) } // Wrapper for WaitForPlayerStateInternal - public void WaitForPlayerStateWrapper(string playerID, string stateKey, Action onStateSetCallback = null) + public void WaitForPlayerStateWrapper(string playerID, string stateKey, + Action onStateSetCallback = null) { WaitForPlayerStateInternal(playerID, stateKey, onStateSetCallback); } @@ -192,7 +198,8 @@ public void SetPlayerStateBoolWrapper(string playerID, string key, bool value, b } // Wrapper for SetPlayerStateDictionary - public void SetPlayerStateDictionaryWrapper(string playerID, string key, string jsonValues, bool reliable = false) + public void SetPlayerStateDictionaryWrapper(string playerID, string key, string jsonValues, + bool reliable = false) { SetPlayerStateDictionary(playerID, key, jsonValues, reliable); } @@ -238,22 +245,28 @@ public string GetProfileWrapper(string playerID) private static extern void KickInternal(string playerID, Action onKickCallback = null); [DllImport("__Internal")] - private static extern void WaitForPlayerStateInternal(string playerID, string stateKey, Action onStateSetCallback = null); + private static extern void WaitForPlayerStateInternal(string playerID, string stateKey, + Action onStateSetCallback = null); [DllImport("__Internal")] - private static extern void SetPlayerStateByPlayerId(string playerID, string key, int value, bool reliable = false); + private static extern void SetPlayerStateByPlayerId(string playerID, string key, int value, + bool reliable = false); [DllImport("__Internal")] - private static extern void SetPlayerStateFloatByPlayerId(string playerID, string key, string value, bool reliable = false); + private static extern void SetPlayerStateFloatByPlayerId(string playerID, string key, string value, + bool reliable = false); [DllImport("__Internal")] - private static extern void SetPlayerStateByPlayerId(string playerID, string key, bool value, bool reliable = false); + private static extern void SetPlayerStateByPlayerId(string playerID, string key, bool value, + bool reliable = false); [DllImport("__Internal")] - private static extern void SetPlayerStateDictionary(string playerID, string key, string jsonValues, bool reliable = false); + private static extern void SetPlayerStateDictionary(string playerID, string key, string jsonValues, + bool reliable = false); [DllImport("__Internal")] - private static extern void SetPlayerStateStringById(string playerID, string key, string value, bool reliable = false); + private static extern void SetPlayerStateStringById(string playerID, string key, string value, + bool reliable = false); [DllImport("__Internal")] private static extern int GetPlayerStateIntById(string playerID, string key); @@ -269,7 +282,6 @@ public string GetProfileWrapper(string playerID) [DllImport("__Internal")] private static extern string GetProfileByPlayerId(string playerID); - } } -} +} \ No newline at end of file diff --git a/Assets/PlayroomKit/modules/RPC/RPC.cs b/Assets/PlayroomKit/modules/RPC/RPC.cs index 3a9923b..cf7c852 100644 --- a/Assets/PlayroomKit/modules/RPC/RPC.cs +++ b/Assets/PlayroomKit/modules/RPC/RPC.cs @@ -36,10 +36,21 @@ public RPC(PlayroomKit playroomKit, IInterop interop) public void RpcRegister(string name, Action rpcRegisterCallback, string onResponseReturn = null) { - CallbackManager.RegisterRpcCallback(rpcRegisterCallback, name); + CallbackManager.RegisterCallback(rpcRegisterCallback, name); _interop.RpcRegisterWrapper(name, InvokeRpcRegisterCallBack, onResponseReturn); } + public static void RpcRegister2(string name, Action rpcRegisterCallback, + string onResponseReturn = null) + { + // CallbackManager.RegisterRpcCallback(rpcRegisterCallback, name); + + CallbackManager.RegisterCallback(rpcRegisterCallback, name); + PlayroomKitInterop.RpcRegisterInternal(name, RpcRegisterCallBackHandler, onResponseReturn); + + // _interop.RpcRegisterWrapper(name, InvokeRpcRegisterCallBack, onResponseReturn); + } + public void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse = null) { string jsonData = ConvertToJson(data); @@ -66,9 +77,9 @@ public void RpcCall(string name, object data, RpcMode mode, Action callbackOnRes string jsonString = jsonArray.ToString(); /* - This is required to sync the rpc events between all players, without this players won't know which event has been called. - Update: This fix works fine for now, but there might be a better way. - this is a temporary fix, RPCs need to be handled within JSLIB for better control. + This is required to sync the rpc events between all players, without this players won't know which event has been called. + Update: This fix works fine for now, but there might be a better way. + this is a temporary fix, RPCs need to be handled within JSLIB for better control. */ _playroomKit.SetState("rpcCalledEventName", jsonString, reliable: true); _interop.RpcCallWrapper(name, jsonData, mode, InvokeOnResponseCallback); @@ -113,6 +124,28 @@ protected static void InvokeOnResponseCallback() } } + [MonoPInvokeCallback(typeof(Action))] + protected static void RpcRegisterCallBackHandler(string combinedData) + { + try + { + Debug.Log($"Data: {combinedData}"); + + JSONNode jsonNode = JSON.Parse(combinedData); + + string eventName = jsonNode["eventName"]; + string stringData = jsonNode["data"]; + string senderID = jsonNode["senderId"]; + + Debug.LogWarning($"RPC Register Callback: {eventName} - {stringData} - {senderID}"); + CallbackManager.InvokeCallback(eventName, stringData); + } + catch (Exception ex) + { + Debug.LogError("Error: " + ex.Message); + } + } + [MonoPInvokeCallback(typeof(Action))] protected static void InvokeRpcRegisterCallBack(string dataJson, string senderJson) { @@ -139,7 +172,7 @@ protected static void InvokeRpcRegisterCallBack(string dataJson, string senderJs string item = node.Value; updatedRpcCalledEvents.Add(item); } - + for (var i = 0; i < updatedRpcCalledEvents.Count; i++) { string name = updatedRpcCalledEvents[i]; diff --git a/Assets/PlayroomKit/src/index.js b/Assets/PlayroomKit/src/index.js index 821b592..53bb4db 100644 --- a/Assets/PlayroomKit/src/index.js +++ b/Assets/PlayroomKit/src/index.js @@ -843,17 +843,25 @@ mergeInto(LibraryManager.library, { return; } + + var n = UTF8ToString(name) + console.log(n) + onResponseReturn = UTF8ToString(onResponseReturn); + this.rpcEvents.push(name) function registerCallback(data, sender) { - var dataJson = JSON.stringify(data); + var combinedData = { + data: data, + senderId: sender.id, + // eventName : n + }; - var id = sender.id; - var bufferSize = lengthBytesUTF8(id) + 1; - var buffer = _malloc(bufferSize); - stringToUTF8(id, buffer, bufferSize); + var dataJson = JSON.stringify(combinedData); + + console.log(`[JS] dataJson: ${dataJson}`); - {{{ makeDynCall('vii', 'callback') }}}(stringToNewUTF8(dataJson), buffer) + {{{ makeDynCall('vi', 'callback') }}}(stringToNewUTF8(dataJson)); return onResponseReturn; } diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index f1ac85b..cad5e44 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -20,26 +20,29 @@ void Start() discord = true }, () => { - kit.RpcRegister("A", A); - kit.RpcRegister("B", B); + PlayroomKit.RPC.RpcRegister2("A", A); + PlayroomKit.RPC.RpcRegister2("B", B); }); } private void Update() { if (Input.GetMouseButtonDown(0)) - kit.RpcCall("A", "", PlayroomKit.RpcMode.ALL); - else if (Input.GetMouseButtonDown(1)) - kit.RpcCall("B", "", PlayroomKit.RpcMode.ALL); + kit.RpcCall("A", 1, PlayroomKit.RpcMode.ALL); + + if (Input.GetMouseButtonDown(1)) + kit.RpcCall("B", 2, PlayroomKit.RpcMode.ALL); + + } - private void A(string data, string senderID) + private void A(string data) { - Debug.Log("A"); + Debug.Log($"[Unity] A data: {data}"); } - private void B(string data, string senderID) + private void B(string data) { - Debug.Log("B"); + Debug.Log($"[Unity] B data: {data}"); } } \ No newline at end of file