asadm / playroom-unity

9 stars 1 forks source link

[Bug] InvokeOnResponseCallback crashes after latest update #53

Closed SaadBazaz closed 2 months ago

SaadBazaz commented 2 months ago

In PlayroomKit.cs:1074 there's this InvokeOnResponseCallback function where you iterate through RpcEventNames while removing elements from it, which makes my RPCs throw errors and crash the game. Although it was always like this it started crashing to me only after the latest update. 🤷‍♂️

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeOnResponseCallback()
{
    foreach (var name in RpcEventNames)
    {
        try
        {
            if (OnResponseCallbacks.TryGetValue(name, out List<Action> callbacks))
            {
                foreach (var callback in callbacks)
                {
                    callback?.Invoke();
                }

                RpcEventNames.Remove(name);
                OnResponseCallbacks.Remove(name);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError($"C#: Error in Invoking callback for RPC event name: '{name}': {ex.Message}");
        }
    }
}

Link to original discussion: https://discord.com/channels/997752993598419044/1128151935669764096/1229920566304182455

Notes: Awaiting stack trace.

ZhengYiHu commented 2 months ago

Stack trace of the crash:

image
Sandbox.framework.js:50580 Found NO interfaces on host .

Sandbox.framework.js:50580 Called RPC Shoot

Sandbox.framework.js:50580 Received RPC Shoot

Sandbox.framework.js:50580 Eclipse57 shoots with following damage: 50

Sandbox.framework.js:52348 Response received:  You shot a bullet!
Sandbox.framework.js:50580 Called Shoot Callback

Sandbox.framework.js:52807 Uncaught 32608064

___resumeException @ Sandbox.framework.js:52807
$ReversePInvokeWrapper_PlayroomKit_InvokeOnResponseCallback_m7BC47267B59E5D8D1A98E90377EE28C6A1B580A3 @ Sandbox.wasm:0xc06df8
$dynCall_v @ Sandbox.wasm:0x1440dc6
(anonymous) @ Sandbox.framework.js:49291
dynCallLegacy @ Sandbox.framework.js:49568
dynCall @ Sandbox.framework.js:49583
onResponseCallback @ Sandbox.framework.js:52349
resolve @ Sandbox.framework.js:15564
rpcResponseHandle @ Sandbox.framework.js:15574
(anonymous) @ Sandbox.framework.js:15444
ZhengYiHu commented 2 months ago

For reference, this is all the code in my sandbox project. It just uses the template shown in the RPC documentation.

public class PlayroomTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var options = new PlayroomKit.InitOptions();
        options.gameId = "************";
        options.discord = true;
        Playroom.PlayroomKit.InsertCoin(options, () =>
        {
            Debug.Log("Registered RPC Shoot");
            PlayroomKit.RpcRegister("Shoot", CallBackFunction, "You shot a bullet!");
        });
    }

    void CallBackFunction(string data, string senderId)
    {
        Debug.Log("Received RPC Shoot");
        var player = Playroom.PlayroomKit.GetPlayer(senderId);
        Debug.Log($"{player.GetProfile().name} shoots with following damage: {data}");
    }

    public void CallShoot()
    {
        Debug.Log("Called RPC Shoot");
        var damage = 50f;
        PlayroomKit.RpcCall("Shoot", damage,PlayroomKit.RpcMode.ALL, () => {
            Debug.Log("Called Shoot Callback");
        });

    }
}