Skip to content

Commit

Permalink
feat: RPC almost
Browse files Browse the repository at this point in the history
  • Loading branch information
momintlh committed Nov 14, 2024
1 parent c55b18b commit dd67958
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public PlayroomKit()
else if (CurrentMockMode == MockModeSelector.Browser)
{
_playroomService = new PlayroomBrowserMockService();
// _rpc = new RPCLocal();
_rpc = new RPCBrowser();
}
#endif
}
Expand Down
1 change: 1 addition & 0 deletions Assets/PlayroomKit/modules/Interfaces/IRPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum RpcMode
OTHERS,
HOST
}

public interface IRPC
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu

#region UTILS

[System.Serializable]
[Serializable]
private class PrimitiveWrapper<T>
{
public T value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,38 +119,35 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu

CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback");

_ubb.CallJs("WaitForState", null, null, true, stateKey, callbackKey);
_ubb.CallJs("WaitForState", callbackKey, null, true, stateKey);
}

public void WaitForPlayerState(string playerID, string stateKey, Action<string> onStateSetCallback = null)
{
string callbackKey = $"WaitForPlayerState_{stateKey}";
GameObject callbackObject = new GameObject(callbackKey);

MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(onStateSetCallback, callbackKey);

invoker.SetCallback(onStateSetCallback, callbackKey);
CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback");

_ubb.CallJs("WaitForPlayerState", null, null, true, playerID, stateKey, callbackKey);
_ubb.CallJs("WaitForPlayerState", callbackKey, null, false, playerID, stateKey);
}

public void ResetStates(string[] keysToExclude = null, Action onStatesReset = null)
{
_ubb.CallJs("ResetStates", null, null, true, keysToExclude);
_ubb.CallJs("ResetStates", null, null, true, keysToExclude ?? Array.Empty<string>());
onStatesReset?.Invoke();
}

public void ResetPlayersStates(string[] keysToExclude = null, Action onStatesReset = null)
{
_ubb.CallJs("ResetPlayersStates", null, null, true, keysToExclude);
_ubb.CallJs("ResetPlayersStates", null, null, true, keysToExclude ?? Array.Empty<string>());
onStatesReset?.Invoke();
}

#endregion



#region Misc

// TODO: will implement after Player is implemented.
Expand All @@ -160,7 +157,7 @@ public void UnsubscribeOnQuit()
}

#endregion

#region Joystick Mehtods

public void CreateJoyStick(JoystickOptions options)
Expand All @@ -177,6 +174,7 @@ public Dpad DpadJoystick()
#endregion

#region Utils

public static void MockOnPlayerJoinWrapper(string playerId)
{
PlayroomKit.IPlayroomBase.OnPlayerJoinWrapperCallback(playerId);
Expand All @@ -185,5 +183,42 @@ public static void MockOnPlayerJoinWrapper(string playerId)
#endregion
}


public class RPCBrowser : PlayroomKit.IRPC
{
private UnityBrowserBridge _ubb;

public RPCBrowser()
{
}

public void RpcRegister(string name, Action<string, string> rpcRegisterCallback, string onResponseReturn = null)
{
if (_ubb)
_ubb = UnityBrowserBridge.Instance;

string callbackKey = $"RpcEvent_{name}";
GameObject callbackObject = new GameObject(callbackKey);

MockCallbackInvoker invoker = callbackObject.AddComponent<MockCallbackInvoker>();
invoker.SetCallback(rpcRegisterCallback, callbackKey);


CallBacksHandlerMock.Instance.RegisterCallbackObject(callbackKey, callbackObject, "ExecuteCallback");
_ubb.CallJs("RpcRegister", callbackKey, null, false, name, onResponseReturn);

}

public void RpcCall(string name, object data, PlayroomKit.RpcMode mode, Action callbackOnResponse = null)
{
_ubb.CallJs("RpcCall", null, null, false, name, data.ToString(), mode.ToString());
}

public void RpcCall(string name, object data, Action callbackOnResponse = null)
{
RpcCall(name, data, PlayroomKit.RpcMode.ALL, callbackOnResponse);
}
}

#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ WaitForPlayerState = async function (playerId, stateKey, onStateSetCallback) {
return null;
}

await Playroom.waitForPlayerState().then((stateVal) => {
// playerState, stateKey, onStateSetCallback
Playroom.waitForPlayerState(playerState, stateKey).then((stateVal) => {
const data = {
key: onStateSetCallback,
parameter: stateVal,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace Playroom
{
public class CallBacksHandlerMock : MonoBehaviour
{
private readonly Dictionary<string, (GameObject gameObject, string methodName)> callbacks = new();

[SerializeField]
private PlayroomkitDevManager manager;

private static CallBacksHandlerMock _instance;

public static CallBacksHandlerMock Instance
Expand All @@ -30,25 +35,20 @@ public static CallBacksHandlerMock Instance

private void Start()
{
var manager = FindObjectOfType<PlayroomkitDevManager>();
gameObject.transform.SetParent(manager.gameObject.transform);
}

private Dictionary<string, (GameObject gameObject, string methodName)> callbacks = new();

public void RegisterCallbackObject(string key, GameObject gameObject, string methodName)
{
if (!callbacks.ContainsKey(key))
{

callbacks.TryAdd(key, (gameObject, methodName));
}
}

public void HandleRPC(string jsonData)
{


var jsonNode = JSON.Parse(jsonData);

string key = jsonNode["key"];
Expand Down
5 changes: 2 additions & 3 deletions Assets/PlayroomKit/modules/MockMode/MockCallbackInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,12 @@ public void SetCallback(Action<string, string> cb, string key)


public void ExecuteCallback(string result)
{
{
StartCoroutine(ExecuteCallbackCoroutine(result));
}

public void ExecuteCallback(string[] result)
{

StartCoroutine(ExecuteCallbackCoroutine(result));
}

Expand All @@ -48,7 +47,7 @@ public void ExecuteCallback()
private IEnumerator ExecuteCallbackCoroutine(string result)
{
yield return new WaitForEndOfFrame();

CallbackManager.InvokeCallback(gameObject.name, result);
Destroy(gameObject);
}
Expand Down
7 changes: 0 additions & 7 deletions Assets/PlayroomKit/modules/RPC/RPC.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AOT;
using SimpleJSON;
using UnityEngine;

Expand All @@ -27,7 +24,6 @@ public RPC(PlayroomKit playroomKit, IInterop interop)
_interop = interop;
}


public void RpcRegister(string name, Action<string, string> rpcRegisterCallback,
string onResponseReturn = null)
{
Expand All @@ -36,7 +32,6 @@ public void RpcRegister(string name, Action<string, string> rpcRegisterCallback,
_interop.RpcRegisterWrapper(name, IRPC.InvokeRpcRegisterCallBack, onResponseReturn);

}


public void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse = null)
{
Expand Down Expand Up @@ -77,8 +72,6 @@ public void RpcCall(string name, object data, Action callbackOnResponse = null)
{
RpcCall(name, data, RpcMode.ALL, callbackOnResponse);
}

}

}
}

0 comments on commit dd67958

Please sign in to comment.