Skip to content

Commit

Permalink
fix: multiple callbacks for same rpc
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jan 3, 2025
1 parent 088fcf4 commit 76cd7b9
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/Tests/Editor/PlayroomKitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void HandleShoot(string data, string caller)
}

_playroomKit.RpcRegister("Shoot", HandleShoot, "You shot!");
_interop.Received(1).RpcRegisterWrapper("Shoot", Arg.Any<Action<string, string>>(), "You shot!");
_interop.Received(1).RpcRegisterWrapper("Shoot", Arg.Any<Action<string>>(), "You shot!");
}

[Test]
Expand Down
16 changes: 12 additions & 4 deletions Assets/PlayroomKit/modules/Helpers/CallbackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ namespace Playroom
public static class CallbackManager
{
private static Dictionary<string, Delegate> callbacks = new();
private static readonly HashSet<string> RegisteredEvents = new();

private static Dictionary<string, List<Action<string, string>>> RpcCallBacks = new();

public static string RegisterRpcCallback(Action<string, string> callback, string key = null)
public static void RegisterRpcCallback(Action<string, string> callback, string key = null)
{
if (string.IsNullOrEmpty(key))
key = GenerateKey();
Expand All @@ -24,8 +25,6 @@ public static string RegisterRpcCallback(Action<string, string> callback, string
{
RpcCallBacks.Add(key, new List<Action<string, string>> { callback });
}

return key;
}

public static void InvokeRpcRegisterCallBack(string name, string data, string sender)
Expand All @@ -34,7 +33,7 @@ public static void InvokeRpcRegisterCallBack(string name, string data, string se
{
for (var i = 0; i < callbacks.Count; i++)
{
var callback = callbacks[i];
Action<string, string> callback = callbacks[i];
callback?.Invoke(data, sender);
}
}
Expand All @@ -45,6 +44,15 @@ public static void InvokeRpcRegisterCallBack(string name, string data, string se
}
}

public static bool IsEventRegistered(string eventName)
{
return RegisteredEvents.Contains(eventName);
}

public static void MarkEventAsRegistered(string eventName)
{
RegisteredEvents.Add(eventName);
}

public static string RegisterCallback(Delegate callback, string key = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void InsertCoinWrapper(string options,

void StartMatchmakingWrapper(Action callback);

void RpcRegisterWrapper(string name, Action<string, string> rpcRegisterCallback,
void RpcRegisterWrapper(string name, Action<string> rpcRegisterCallback,
string onResponseReturn = null);

void RpcCallWrapper(string name, string data, RpcMode mode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public void StartMatchmakingWrapper(Action callback)
StartMatchmakingInternal(callback);
}

public void RpcRegisterWrapper(string name, Action<string, string> rpcRegisterCallback,
public void RpcRegisterWrapper(string name, Action<string> rpcRegisterCallback,
string onResponseReturn = null)
{
RpcRegisterInternal(name, rpcRegisterCallback, onResponseReturn);
Expand All @@ -160,7 +160,7 @@ private static extern void RpcRegisterInternal(string name, Action<string, strin
string onResponseReturn = null);

[DllImport("__Internal")]
public static extern void RpcRegisterInternal(string name, Action<string> rpcRegisterCallback,
private static extern void RpcRegisterInternal(string name, Action<string> rpcRegisterCallback,
string onResponseReturn = null);

[DllImport("__Internal")]
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/modules/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public void InvokePlayerOnQuitCallback()
#endif

#if UNITY_EDITOR
else if (_playerService is BrowserMockPlayerService playerService2)
if (_playerService is BrowserMockPlayerService playerService2)
{
playerService2.InvokePlayerOnQuitCallback(id);
}
Expand Down
3 changes: 0 additions & 3 deletions Assets/PlayroomKit/modules/PlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System;
using System.Collections.Generic;


namespace Playroom
{
public partial class PlayroomKit
Expand Down Expand Up @@ -89,13 +88,11 @@ public void TransferHost(string playerId)
_interop.TransferHostWrapper(playerId);
}


public string GetRoomCode()
{
return _interop.GetRoomCodeWrapper();
}


public void StartMatchmaking(Action callback = null)
{
CallbackManager.RegisterCallback(callback, "matchMakingStarted");
Expand Down
12 changes: 8 additions & 4 deletions Assets/PlayroomKit/modules/RPC/RPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,13 @@ public RPC(PlayroomKit playroomKit, IInterop interop)
public void RpcRegister(string name, Action<string, string> rpcRegisterCallback,
string onResponseReturn = null)
{
Debug.Log(rpcRegisterCallback.GetMethodInfo().Name);
CallbackManager.RegisterCallback(rpcRegisterCallback, name);
PlayroomKitInterop.RpcRegisterInternal(name, RpcRegisterCallBackHandler, onResponseReturn);
CallbackManager.RegisterRpcCallback(rpcRegisterCallback, name);

if (!CallbackManager.IsEventRegistered(name))
{
_interop.RpcRegisterWrapper(name, RpcRegisterCallBackHandler, onResponseReturn);
CallbackManager.MarkEventAsRegistered(name);
}
}

public void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse = null)
Expand Down Expand Up @@ -112,7 +116,7 @@ protected static void RpcRegisterCallBackHandler(string combinedData)
string stringData = jsonNode["data"];
string senderID = jsonNode["senderId"];

CallbackManager.InvokeCallback(eventName, stringData, senderID);
CallbackManager.InvokeRpcRegisterCallBack(eventName, stringData, senderID);
}
catch (Exception ex)
{
Expand Down
33 changes: 20 additions & 13 deletions Assets/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,41 +8,48 @@ public class GameManager : MonoBehaviour
private void Start()
{
_kit = new();

_kit.InsertCoin(new InitOptions()
{
gameId = "[my game id]",
maxPlayersPerRoom = 8,
discord = true
}, () =>
{
// same event multiple RPCs
_kit.RpcRegister("A", A);
_kit.RpcRegister("B", B);
_kit.RpcRegister("A", A2);

// RPC Call From Another RPC
_kit.RpcRegister("host", (data, sender) =>
{
Debug.Log("Host RPC CALLED");
_kit.RpcCall("client", 1, PlayroomKit.RpcMode.ALL);
}
);
_kit.RpcRegister("client", (data, sender) =>
{
Debug.Log("client rpc called");
});
});
}

private void Update()
{
if (Input.GetMouseButtonDown(0))
_kit.RpcCall("A", 1, PlayroomKit.RpcMode.HOST);

if (Input.GetMouseButtonDown(1))
Debug.LogWarning(_kit.IsHost());
_kit.RpcCall("host", 1, PlayroomKit.RpcMode.HOST);
}

private void A(string data, string sender)
{
Debug.Log($"[Unity] A called only on HOST {data} and {sender}");

_kit.TransferHost(sender);

_kit.RpcCall("B", 2, PlayroomKit.RpcMode.OTHERS);
Debug.Log($"[Unity] A called");
}

private void B(string data, string sender)
private void A2(string data, string sender)
{
Debug.Log($"[Unity] B called on ALL data: {data} and {sender}");


Debug.LogWarning("[Unity] A2 data: " + data.ToString());
}
}

0 comments on commit 76cd7b9

Please sign in to comment.