Skip to content

Commit

Permalink
fix: fixed sending and converting callback keys
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Jun 1, 2024
1 parent 4946a97 commit 4d402af
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 119 deletions.
65 changes: 0 additions & 65 deletions Assets/PlayroomKit/CallbackManager.cs

This file was deleted.

34 changes: 20 additions & 14 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ public class MatchMakingOptions
[DllImport("__Internal")]
private static extern void InsertCoinInternal(
string options,
Action onLaunchCallback,
Action<string> onLaunchCallback,
Action<string> onQuitInternalCallback,
Action onDisconnectCallback,
Action<string> onError);
Action<string> onDisconnectCallback,
Action<string> onError,
string onLaunchCallBackKey,
string onDisconnectCallBackKey
);

[MonoPInvokeCallback(typeof(Action))]
private static void InvokeInsertCoin()
[MonoPInvokeCallback(typeof(Action<string>))]
private static void InvokeInsertCoin(string key)
{
CallbackManager.InvokeCallback();
CallbackManager.InvokeCallback(key);


#if UNITY_WEBGL && !UNITY_EDITOR
Expand All @@ -92,11 +95,12 @@ public static void InsertCoin(InitOptions options = null, Action onLaunchCallBac
if (IsRunningInBrowser())
{
isPlayRoomInitialized = true;


CallbackManager.RegisterCallback(onLaunchCallBack);
CallbackManager.RegisterCallback(onDisconnectCallback);

string onLaunchCallBackKey = CallbackManager.RegisterCallback(onLaunchCallBack, "onLaunchCallBack");
string onDisconnectCallBackKey = CallbackManager.RegisterCallback(onDisconnectCallback, "onDisconnectCallBack");

Debug.Log(onLaunchCallBackKey);

string optionsJson = null;
if (options != null)
Expand All @@ -112,7 +116,9 @@ public static void InsertCoin(InitOptions options = null, Action onLaunchCallBac
#endif
}

InsertCoinInternal(optionsJson, InvokeInsertCoin, __OnQuitInternalHandler, onDisconnectCallbackHandler, InvokeOnErrorInsertCoin);
InsertCoinInternal(
optionsJson, InvokeInsertCoin, __OnQuitInternalHandler, onDisconnectCallbackHandler,
InvokeOnErrorInsertCoin, onLaunchCallBackKey, onDisconnectCallBackKey);
}
else
{
Expand Down Expand Up @@ -228,10 +234,10 @@ private static void __OnPlayerJoinCallbackHandler(string id)
OnPlayerJoinWrapperCallback(id);
}

[MonoPInvokeCallback(typeof(Action))]
private static void onDisconnectCallbackHandler()
[MonoPInvokeCallback(typeof(Action<string>))]
private static void onDisconnectCallbackHandler(string key)
{
CallbackManager.InvokeCallback();
CallbackManager.InvokeCallback(key);
}


Expand Down Expand Up @@ -404,7 +410,7 @@ public static Player Me()


[DllImport("__Internal")]
private static extern void OnDisconnectInternal(Action callback);
private static extern void OnDisconnectInternal(Action<string> callback);


public static void OnDisconnect(Action callback)
Expand Down
58 changes: 58 additions & 0 deletions Assets/PlayroomKit/modules/CallbackManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;

namespace Playroom
{
public static class CallbackManager
{
private static Dictionary<string, Delegate> callbacks = new();

public static string RegisterCallback(Action callback, string key = null)
{
if (string.IsNullOrEmpty(key))
key = GenerateKey();


if (!callbacks.ContainsKey(key))
{
callbacks.Add(key, callback);
}
else
{
callbacks[key] = callback;
}

return key;
}


public static void InvokeCallback(string key)
{
if (callbacks.TryGetValue(key, out Delegate callback))
{
(callback as Action)?.Invoke();
}
else
{
Debug.LogError($"Callback with key {key} not found!");
}
}

private static string GenerateKey()
{
return Guid.NewGuid().ToString();
}



/// <summary>
/// Calls an external method to convert a string associated with a given key, called from the JS side only.
/// </summary>
/// <param name="key">The key associated with the string to be converted.</param>
[DllImport("__Internal")]
private static extern void ConvertString(string key);

}
}
File renamed without changes.
65 changes: 25 additions & 40 deletions Assets/PlayroomKit/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ mergeInto(LibraryManager.library, {
onLaunchCallBack,
onQuitInternalCallback,
onDisconnectCallback,
onError
onError,
onLaunchCallBackKey,
onQuitInternalCallbackKey
) {

onLaunchCallBackKey = UTF8ToString(onLaunchCallBackKey);
onQuitInternalCallbackKey = UTF8ToString(onQuitInternalCallbackKey);

function OnLaunchCallBack() {
dynCall("v", onLaunchCallBack, []);
var key = _ConvertString(onLaunchCallBackKey);
dynCall("vi", onLaunchCallBack, [key]);
}

function OnDisconnectCallback() {
dynCall("v", onDisconnectCallback, []);
var key = _ConvertString(onQuitInternalCallbackKey);
dynCall("vi", onDisconnectCallback, [key]);
}
this.onPlayerJoinCallBacks = {};
var options = optionsJson ? JSON.parse(UTF8ToString(optionsJson)) : {};
Expand Down Expand Up @@ -698,7 +706,7 @@ mergeInto(LibraryManager.library, {
});
},

WaitForStateInternal: function (stateKey, onStateSetCallback) {
WaitForStateInternal: function (state, onStateSetCallback) {
if (!window.Playroom) {
console.error(
"Playroom library is not loaded. Please make sure to call InsertCoin first."
Expand All @@ -707,16 +715,12 @@ mergeInto(LibraryManager.library, {
return;
}

stateKey = UTF8ToString(stateKey);
Playroom.waitForState(stateKey)
state = UTF8ToString(state);
Playroom.waitForState(state)
.then((stateVal) => {

var bufferSize = lengthBytesUTF8(stateKey) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(stateKey, buffer, bufferSize);

stateVal = JSON.stringify(stateVal);

var buffer = _ConvertString(state);

dynCall("vii", onStateSetCallback, [stringToNewUTF8(stateVal), buffer]);
})
Expand Down Expand Up @@ -909,36 +913,17 @@ mergeInto(LibraryManager.library, {
});
},

// Utils for callback manager:
SendKey: function (key) {
if (!window.Playroom) {
console.error(
"Playroom library is not loaded. Please make sure to call InsertCoin first."
);
return;
}
console.log(UTF8ToString(key));
if (!this.callbackIds) {
this.callbackIds = [];
}
this.callbackIds.push(UTF8ToString(key));
},

GetKey: function (key) {
if (!window.Playroom) {
console.error(
"Playroom library is not loaded. Please make sure to call InsertCoin first."
);
return;
}

var foundKey = this.callbackIds.find(function (item) {
return item === UTF8ToString(key);
});

var bufferSize = lengthBytesUTF8(foundKey) + 1;
// UTILS
/**
* Converts a given string into a UTF-8 encoded string and stores it in memory.
*
* @param {string} str - The string to be converted.
* @returns {number} The memory address of the buffer where the converted string is stored.
*/
ConvertString: function (str) {
var bufferSize = lengthBytesUTF8(str) + 1;
var buffer = _malloc(bufferSize);
stringToUTF8(foundKey, buffer, bufferSize);
stringToUTF8(str, buffer, bufferSize);
return buffer;
},
});

0 comments on commit 4d402af

Please sign in to comment.