Skip to content

Commit

Permalink
fix: RPC fix
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jan 3, 2025
1 parent 2f3c477 commit 01a02d0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ public void StartMatchmakingWrapper(Action callback)
StartMatchmakingInternal(callback);
}

public void RpcRegisterWrapper(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null)
public void RpcRegisterWrapper(string name, Action<string, string> rpcRegisterCallback,
string onResponseReturn = null)
{
RpcRegisterInternal(name, rpcRegisterCallback, onResponseReturn);
}
Expand All @@ -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<string, string> rpcRegisterCallback,
string onResponseReturn = null);


[DllImport("__Internal")]
public static extern void RpcRegisterInternal(string name, Action<string> 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)
Expand All @@ -168,7 +173,8 @@ public void KickPlayerWrapper(string playerID, Action onKickCallback = null)
}

// Wrapper for WaitForPlayerStateInternal
public void WaitForPlayerStateWrapper(string playerID, string stateKey, Action<string> onStateSetCallback = null)
public void WaitForPlayerStateWrapper(string playerID, string stateKey,
Action<string> onStateSetCallback = null)
{
WaitForPlayerStateInternal(playerID, stateKey, onStateSetCallback);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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<string> onStateSetCallback = null);
private static extern void WaitForPlayerStateInternal(string playerID, string stateKey,
Action<string> 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);
Expand All @@ -269,7 +282,6 @@ public string GetProfileWrapper(string playerID)

[DllImport("__Internal")]
private static extern string GetProfileByPlayerId(string playerID);

}
}
}
}
43 changes: 38 additions & 5 deletions Assets/PlayroomKit/modules/RPC/RPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,21 @@ public RPC(PlayroomKit playroomKit, IInterop interop)
public void RpcRegister(string name, Action<string, string> rpcRegisterCallback,
string onResponseReturn = null)
{
CallbackManager.RegisterRpcCallback(rpcRegisterCallback, name);
CallbackManager.RegisterCallback(rpcRegisterCallback, name);
_interop.RpcRegisterWrapper(name, InvokeRpcRegisterCallBack, onResponseReturn);
}

public static void RpcRegister2(string name, Action<string> 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);
Expand All @@ -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);
Expand Down Expand Up @@ -113,6 +124,28 @@ protected static void InvokeOnResponseCallback()
}
}

[MonoPInvokeCallback(typeof(Action<string>))]
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<string, string>))]
protected static void InvokeRpcRegisterCallBack(string dataJson, string senderJson)
{
Expand All @@ -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];
Expand Down
20 changes: 14 additions & 6 deletions Assets/PlayroomKit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 12 additions & 9 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}

0 comments on commit 01a02d0

Please sign in to comment.