Skip to content

Commit

Permalink
Merge pull request #123 from aarijimam/feature/browserbridge-onquit
Browse files Browse the repository at this point in the history
OnQuit for browser mock mode
  • Loading branch information
momintlh authored Nov 22, 2024
2 parents ca0860e + 02d6632 commit 280c702
Show file tree
Hide file tree
Showing 21 changed files with 169 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 1429187865}
m_IndirectSpecularColor: {r: 0.44657844, g: 0.49641222, b: 0.57481694, a: 1}
m_IndirectSpecularColor: {r: 0.44657815, g: 0.49641192, b: 0.5748165, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -210,13 +210,21 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 7325353642485059119, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: enableLogs
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8899696337967424923, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: webDriverDirectory
value: Assets\
value: Assets/
objectReference: {fileID: 0}
- target: {fileID: 8899696337967424923, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: includeJavaScriptFiles.Array.data[0]
value: Assets/PlayroomKit/modules/MockMode/BrowserMode/PlayroomMockBrowserBridge.js
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: mockMode
value: 0
value: 1
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: insertCoinCaller
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class PlayroomKit

private static PlayroomKit _instance;
public static bool IsPlayRoomInitialized;
private static readonly Dictionary<string, Player> Players = new();
internal static readonly Dictionary<string, Player> Players = new();

public enum MockModeSelector
{
Expand Down
14 changes: 14 additions & 0 deletions Assets/PlayroomKit/Prefabs/PlayroomMockManager.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ GameObject:
m_Component:
- component: {fileID: 4611920177403363027}
- component: {fileID: 8987662522597341863}
- component: {fileID: 7325353642485059119}
m_Layer: 0
m_Name: PlayroomMockManager
m_TagString: EditorOnly
Expand Down Expand Up @@ -48,6 +49,19 @@ MonoBehaviour:
m_EditorClassIdentifier:
mockMode: 1
insertCoinCaller: {fileID: 0}
--- !u!114 &7325353642485059119
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1824217503633254899}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 37df37ffbaed24d5f858db19670affdf, type: 3}
m_Name:
m_EditorClassIdentifier:
logToggle: {fileID: 0}
--- !u!1 &5935941862803782869
GameObject:
m_ObjectHideFlags: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ public void CallJs(string jsFunctionName, string callbackName = null, string gam

string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})";
if (isAsync) jsCall = $"await {jsCall}";

DebugLogger.Log(jsCall);

ExecuteJS(jsCall);
}
Expand All @@ -401,7 +403,7 @@ public T CallJs<T>(string jsFunctionName, string callbackName = null, string gam
string jsCall = $"{jsFunctionName}({string.Join(", ", allParams)})";
if (isAsync) jsCall = $"await {jsCall}";

Debug.Log(jsCall);
DebugLogger.Log(jsCall);
return ExecuteJS<T>(jsCall);
}

Expand Down
37 changes: 37 additions & 0 deletions Assets/PlayroomKit/modules/Helpers/DebugLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using UnityEngine;

public static class DebugLogger
{
public static bool enableLogs = true; // Toggle to enable/disable logs
#if UNITY_EDITOR || DEBUG


public static void Log(string message)
{
if (enableLogs)
{
Debug.Log(message);
}
}

public static void LogWarning(string message)
{
if (enableLogs)
{
Debug.LogWarning(message);
}
}

public static void LogError(string message)
{
if (enableLogs)
{
Debug.LogError(message);
}
}
#else
public static void Log(string message) { }
public static void LogWarning(string message) { }
public static void LogError(string message) { }
#endif
}
3 changes: 3 additions & 0 deletions Assets/PlayroomKit/modules/Helpers/DebugLogger.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions Assets/PlayroomKit/modules/Helpers/DebugToggle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using UnityEngine;

public class DebugToggle : MonoBehaviour
{
[SerializeField] private bool enableLogs = false;

private void Start()
{
DebugLogger.enableLogs = enableLogs;
}

private void ToggleLogs(bool isEnabled)
{
DebugLogger.enableLogs = isEnabled;
}
}

11 changes: 11 additions & 0 deletions Assets/PlayroomKit/modules/Helpers/DebugToggle.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions Assets/PlayroomKit/modules/Interfaces/IPlayroomBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using AOT;
using Playroom;
using UnityEngine;

namespace Playroom
{
Expand Down Expand Up @@ -69,6 +70,19 @@ protected static void InvokeCallback(string stateKey, string stateVal)
{
CallbackManager.InvokeCallback(stateKey, stateVal);
}

[MonoPInvokeCallback(typeof(Action<string>))]
internal static void __OnQuitInternalHandler(string playerId)
{
if (Players.TryGetValue(playerId, out Player player))
{
player.InvokePlayerOnQuitCallback();
}
else
{
Debug.LogError("[__OnQuitInternalHandler] Couldn't find player with id " + playerId);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using UBB;
using UnityEngine;

Expand Down Expand Up @@ -91,30 +92,12 @@ public PlayroomKit.Player.Profile GetProfile()
{
string json = _ubb.CallJs<string>("GetProfile", null, null, false, _id);

Debug.Log(json);
DebugLogger.Log("Profile Json: " + json);

var profileData = Helpers.ParseProfile(json);
return profileData;
}

public Action OnQuit(Action<string> callback)
{
string callbackKey = $"OnQuit_{_id}";

GameObject callbackObject = new GameObject(callbackKey);
Debug.Log(callbackKey);

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

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

// TODO: actually call on quit
_ubb.CallJs("OnQuit", null, null, false, callbackKey, _id);

return default;
}

public void Kick(Action onKickCallBack = null)
{
_ubb.CallJs("Kick", null, null, true, _id);
Expand All @@ -132,6 +115,32 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu

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

private List<Action<string>> OnQuitCallbacks = new();

public Action OnQuit(Action<string> callback)
{
OnQuitCallbacks.Add(callback);

void Unsubscribe()
{
OnQuitCallbacks.Remove(callback);
}

return Unsubscribe;
}

public void OnQuitWrapperCallback(string id)
{
if (OnQuitCallbacks != null)
foreach (var callback in OnQuitCallbacks)
callback?.Invoke(id);
}

internal void InvokePlayerOnQuitCallback(string id)
{
OnQuitWrapperCallback(id);
}

#region UTILS

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
}

var gameObjectName = _ubb.GetGameObject("InsertCoin").name;
var devManagerName = _ubb.GetGameObject("devManager").name;
Debug.Log("DevManagerName:" + gameObjectName);
_ubb.CallJs("InsertCoin", onLaunchCallBack.GetMethodInfo().Name, gameObjectName, true, optionsJson);
PlayroomKit.IsPlayRoomInitialized = true;
}
Expand All @@ -49,7 +51,7 @@ public Action OnPlayerJoin(Action<PlayroomKit.Player> onPlayerJoinCallback)

void Unsub()
{
Debug.Log("Unsubscribing from OnPlayerJoin");
DebugLogger.Log("Unsubscribing from OnPlayerJoin");
}

return Unsub;
Expand Down Expand Up @@ -190,7 +192,7 @@ public static void MockOnPlayerJoinWrapper(string playerId)
{
PlayroomKit.IPlayroomBase.OnPlayerJoinWrapperCallback(playerId);
}

#endregion
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
OnPlayerJoin = function (gameObjectName) {
Playroom.onPlayerJoin((player) => {
unityInstance.SendMessage(gameObjectName, "GetPlayerID", player.id);
player.onQuit((state) => {
unityInstance.SendMessage(gameObjectName, "OnQuitPlayer", player.id);
})
});
};

Expand Down Expand Up @@ -210,34 +213,6 @@ Kick = async function (playerID) {
await playerState.kick();
};

OnQuit = function (playerID, callbackKey) {
if (!window.Playroom) {
console.error(
"Playroom library is not loaded. Please make sure to call InsertCoin first."
);
reject("Playroom library not loaded");
return;
}

const players = window._multiplayer.getPlayers();

if (typeof players !== "object" || players === null) {
console.error('The "players" variable is not an object:', players);
return null;
}
const playerState = players[playerID];

if (!playerState) {
console.error("Player with ID", playerID, "not found.");
return null;
}

playerState.onQuit((state) => {
console.log(`${state.id} quit!`);
unityInstance.SendMessage("CallbackManager", "InvokeCallback", callbackKey, state.id);
});
};

ResetPlayersStates = async function (keysToExclude) {
await Playroom.resetPlayersStates(keysToExclude);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void InvokeCallback(string jsonData)

if (callbacks.TryGetValue(key, out var callbackInfo))
{
Debug.LogWarning(
DebugLogger.LogWarning(
$"key: {key}, gameObjectName: {callbackInfo.gameObject.name}, callbackName: {callbackInfo.methodName}");

callbackInfo.gameObject.SendMessage(callbackInfo.methodName, parameter,
Expand Down
4 changes: 2 additions & 2 deletions Assets/PlayroomKit/modules/MockMode/LocalPlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class LocalMockPlayroomService : PlayroomKit.IPlayroomBase

public Action OnPlayerJoin(Action<PlayroomKit.Player> onPlayerJoinCallback)
{
Debug.Log("On Player Join");
DebugLogger.Log("On Player Join");
var testPlayer = PlayroomKit.GetPlayerById(PlayerId);
PlayroomKit.IPlayroomBase.OnPlayerJoinCallbacks.Add(onPlayerJoinCallback);
PlayroomKit.IPlayroomBase.__OnPlayerJoinCallbackHandler(PlayerId);
Expand All @@ -31,7 +31,7 @@ public void InsertCoin(InitOptions options = null, Action onLaunchCallBack = nul
Action onDisconnectCallback = null)
{
PlayroomKit.IsPlayRoomInitialized = true;
Debug.Log("Coin Inserted");
DebugLogger.Log("Coin Inserted");
string optionsJson = null;
if (options != null) optionsJson = Helpers.SerializeInitOptions(options);
onLaunchCallBack?.Invoke();
Expand Down
4 changes: 2 additions & 2 deletions Assets/PlayroomKit/modules/Player/LocalPlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static Dictionary<string, object> GetMockPlayerStates()

public void SetStateHelper<T>(string key, T value, bool reliable = false)
{
Debug.Log($"MockPlayerService setState: {key} => {value}");
DebugLogger.Log($"MockPlayerService setState: {key} => {value}");
if (mockPlayerStatesDictionary.ContainsKey(key))
mockPlayerStatesDictionary[key] = value;
else
Expand Down Expand Up @@ -115,7 +115,7 @@ public void WaitForState(string stateKey, Action<string> onStateSetCallback = nu

string key = $"{stateKey}_{_id}";
CallbackManager.RegisterCallback(onStateSetCallback, key);
Debug.Log($"Callback registered, the key is: {key}");
DebugLogger.Log($"Callback registered, the key is: {key}");
}
}
}
Expand Down
Loading

0 comments on commit 280c702

Please sign in to comment.