diff --git a/Assets/PlayroomKit/Headers.cs b/Assets/PlayroomKit/Headers.cs new file mode 100644 index 0000000..cdf723b --- /dev/null +++ b/Assets/PlayroomKit/Headers.cs @@ -0,0 +1,90 @@ +using System; +using System.Runtime.InteropServices; + + +namespace Playroom +{ + public partial class PlayroomKit + { + [DllImport("__Internal")] + private static extern void InsertCoinInternal(string options, + Action onLaunchCallback, + Action onQuitInternalCallback, + Action onDisconnectCallback, + Action onError, + string onLaunchCallBackKey, + string onDisconnectCallBackKey); + + [DllImport("__Internal")] + private static extern string OnPlayerJoinInternal(Action callback); + + [DllImport("__Internal")] + private static extern void UnsubscribeOnPlayerJoinInternal(string id); + + [DllImport("__Internal")] + private static extern bool IsHostInternal(); + + [DllImport("__Internal")] + private static extern bool IsStreamModeInternal(); + + [DllImport("__Internal")] + private static extern string MyPlayerInternal(); + + [DllImport("__Internal")] + public static extern string GetRoomCode(); + + [DllImport("__Internal")] + private static extern void OnDisconnectInternal(Action callback); + + [DllImport("__Internal")] + private static extern void SetStateString(string key, string value, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateInternal(string key, int value, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateInternal(string key, bool value, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateFloatInternal(string key, string floatAsString, bool reliable = false); + + [DllImport("__Internal")] + private static extern void SetStateDictionary(string key, string jsonValues, bool reliable = false); + + [DllImport("__Internal")] + private static extern string GetStateStringInternal(string key); + + [DllImport("__Internal")] + private static extern int GetStateIntInternal(string key); + + [DllImport("__Internal")] + private static extern float GetStateFloatInternal(string key); + + [DllImport("__Internal")] + private static extern string GetStateDictionaryInternal(string key); + + [DllImport("__Internal")] + private static extern void WaitForStateInternal(string stateKey, Action onStateSetCallback); + + [DllImport("__Internal")] + private static extern void WaitForPlayerStateInternal(string playerID, string StateKey, Action onStateSetCallback); + + [DllImport("__Internal")] + private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null); + + [DllImport("__Internal")] + private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null); + + [DllImport("__Internal")] + private static extern void UnsubscribeOnQuitInternal(); + + [DllImport("__Internal")] + private static extern void CreateJoystickInternal(string joyStickOptionsJson); + + [DllImport("__Internal")] + private static extern string DpadJoystickInternal(); + + [DllImport("__Internal")] + private static extern void StartMatchmakingInternal(Action callback); + } +} \ No newline at end of file diff --git a/Assets/PlayroomKit/Headers.cs.meta b/Assets/PlayroomKit/Headers.cs.meta new file mode 100644 index 0000000..38ae076 --- /dev/null +++ b/Assets/PlayroomKit/Headers.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9a9b84c597b4c2c47b6c9c762060b126 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PlayroomKit/MockMode.cs b/Assets/PlayroomKit/MockMode.cs new file mode 100644 index 0000000..5a62ee9 --- /dev/null +++ b/Assets/PlayroomKit/MockMode.cs @@ -0,0 +1,90 @@ +using System; +using System.Buffers.Text; +using System.Collections; +using System.Collections.Generic; +using Unity.VisualScripting; +using UnityEngine; + +namespace Playroom +{ + public partial class PlayroomKit + { + private const string PlayerId = "mockplayerID123"; + private static bool mockIsStreamMode; + + /// + /// This is private, instead of public, to prevent tampering in Mock Mode. + /// Reason: In Mock Mode, only a single player can be tested. + /// Ref: https://docs.joinplayroom.com/usage/unity#mock-mode + /// + private static Dictionary MockDictionary = new(); + + + + + private static void MockInsertCoin(InitOptions options, Action onLaunchCallBack) + { + isPlayRoomInitialized = true; + + Debug.Log("Coin Inserted"); + + string optionsJson = null; + if (options != null) optionsJson = SerializeInitOptions(options); + onLaunchCallBack?.Invoke(); + } + + private static void MockSetState(string key, object value) + { + if (MockDictionary.ContainsKey(key)) + MockDictionary[key] = value; + else + MockDictionary.Add(key, value); + } + + private static T MockGetState(string key) + { + if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue) + { + return typedValue; + } + else + { + Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}"); + return default; + } + } + + private static Dictionary callback, string response)> mockRegisterCallbacks = new(); + private static Dictionary mockResponseCallbacks = new(); + + private static void MockRpcRegister(string name, Action rpcRegisterCallback, string onResponseReturn = null) + { + mockRegisterCallbacks.TryAdd(name, (rpcRegisterCallback, onResponseReturn)); + } + + private static void MockRpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) + { + mockResponseCallbacks.TryAdd(name, callbackOnResponse); + + string stringData = Convert.ToString(data); + var player = MyPlayer(); + + if (mockRegisterCallbacks.TryGetValue(name, out var responseHandler)) + { + responseHandler.callback?.Invoke(stringData, player.id); + + if (!string.IsNullOrEmpty(responseHandler.response)) + { + Debug.Log($"Response received: {responseHandler.response}"); + } + } + + if (mockResponseCallbacks.TryGetValue(name, out var callback)) + { + callback?.Invoke(); + } + } + + } + +} \ No newline at end of file diff --git a/Assets/PlayroomKit/MockMode.cs.meta b/Assets/PlayroomKit/MockMode.cs.meta new file mode 100644 index 0000000..38e8799 --- /dev/null +++ b/Assets/PlayroomKit/MockMode.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2caf136702f132c4fa146a9a7ae15f81 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 891c182..88e874a 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -14,19 +14,6 @@ public partial class PlayroomKit { private static bool isPlayRoomInitialized; - /// - /// Required Mock Mode: - /// - private const string PlayerId = "mockPlayer"; - private static bool mockIsStreamMode; - - /* - This is private, instead of public, to prevent tampering in Mock Mode. - Reason: In Mock Mode, only a single player can be tested. - Ref: https://docs.joinplayroom.com/usage/unity#mock-mode - */ - private static Dictionary MockDictionary = new(); - private static readonly Dictionary Players = new(); [Serializable] @@ -55,17 +42,6 @@ public class MatchMakingOptions public int waitBeforeCreatingNewRoom = 5000; } - [DllImport("__Internal")] - private static extern void InsertCoinInternal( - string options, - Action onLaunchCallback, - Action onQuitInternalCallback, - Action onDisconnectCallback, - Action onError, - string onLaunchCallBackKey, - string onDisconnectCallBackKey - ); - [MonoPInvokeCallback(typeof(Action))] private static void InvokeInsertCoin(string key) { @@ -119,16 +95,12 @@ public static void InsertCoin(InitOptions options = null, Action onLaunchCallBac } else { - isPlayRoomInitialized = true; - - Debug.Log("Coin Inserted"); - - string optionsJson = null; - if (options != null) optionsJson = SerializeInitOptions(options); - onLaunchCallBack?.Invoke(); + MockInsertCoin(options, onLaunchCallBack); } } + + private static string SerializeInitOptions(InitOptions options) { if (options == null) return null; @@ -216,11 +188,7 @@ private static JSONNode ConvertValueToJSON(object value) } } - [DllImport("__Internal")] - private static extern string OnPlayerJoinInternal(Action callback); - [DllImport("__Internal")] - private static extern void UnsubscribeOnPlayerJoinInternal(string id); private static List> OnPlayerJoinCallbacks = new(); @@ -327,8 +295,7 @@ public static Player GetPlayer(string playerId) } - [DllImport("__Internal")] - private static extern bool IsHostInternal(); + public static bool IsHost() { @@ -350,8 +317,7 @@ public static bool IsHost() } } - [DllImport("__Internal")] - private static extern bool IsStreamModeInternal(); + public static bool IsStreamMode() { @@ -373,8 +339,7 @@ public static bool IsStreamMode() } } - [DllImport("__Internal")] - private static extern string MyPlayerInternal(); + public static Player MyPlayer() { @@ -402,12 +367,7 @@ public static Player Me() return MyPlayer(); } - [DllImport("__Internal")] - public static extern string GetRoomCode(); - - [DllImport("__Internal")] - private static extern void OnDisconnectInternal(Action callback); public static void OnDisconnect(Action callback) @@ -417,19 +377,6 @@ public static void OnDisconnect(Action callback) } - [DllImport("__Internal")] - private static extern void SetStateString(string key, string value, bool reliable = false); - - [DllImport("__Internal")] - private static extern void SetStateInternal(string key, int value, bool reliable = false); - - - [DllImport("__Internal")] - private static extern void SetStateInternal(string key, bool value, bool reliable = false); - - [DllImport("__Internal")] - private static extern void SetStateFloatInternal(string key, string floatAsString, bool reliable = false); - public static void SetState(string key, string value, bool reliable = false) { if (IsRunningInBrowser()) @@ -444,7 +391,6 @@ public static void SetState(string key, string value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -464,7 +410,6 @@ public static void SetState(string key, int value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -485,7 +430,6 @@ public static void SetState(string key, float value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -505,14 +449,12 @@ public static void SetState(string key, bool value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } } - [DllImport("__Internal")] - private static extern void SetStateDictionary(string key, string jsonValues, bool reliable = false); + public static void SetState(string key, Dictionary values, bool reliable = false) @@ -529,7 +471,6 @@ public static void SetState(string key, Dictionary values, bool rel } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -549,7 +490,6 @@ public static void SetState(string key, Dictionary values, bool r } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -569,7 +509,6 @@ public static void SetState(string key, Dictionary values, bool re } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -589,7 +528,6 @@ public static void SetState(string key, Dictionary values, bool } else { - Debug.Log($"State Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -597,8 +535,7 @@ public static void SetState(string key, Dictionary values, bool // GETTERS - [DllImport("__Internal")] - private static extern string GetStateStringInternal(string key); + private static string GetStateString(string key) { @@ -620,8 +557,7 @@ private static string GetStateString(string key) } } - [DllImport("__Internal")] - private static extern int GetStateIntInternal(string key); + private static int GetStateInt(string key) { @@ -643,8 +579,7 @@ private static int GetStateInt(string key) } } - [DllImport("__Internal")] - private static extern float GetStateFloatInternal(string key); + private static float GetStateFloat(string key) { @@ -752,8 +687,7 @@ public static Dictionary GetState(string key, bool isReturnDiction } - [DllImport("__Internal")] - private static extern void WaitForStateInternal(string stateKey, Action onStateSetCallback); + [MonoPInvokeCallback(typeof(Action))] @@ -774,8 +708,7 @@ public static void WaitForState(string stateKey, Action onStateSetCallba - [DllImport("__Internal")] - private static extern void WaitForPlayerStateInternal(string playerID, string StateKey, Action onStateSetCallback); + Action Callback = null; public void WaitForPlayerState(string playerID, string StateKey, Action onStateSetCallback = null) @@ -794,11 +727,6 @@ void OnStateSetCallback() } - [DllImport("__Internal")] - private static extern string GetStateDictionaryInternal(string key); - - - // Utils: private static void SetStateHelper(string key, Dictionary values, bool reliable = false) { @@ -844,8 +772,6 @@ private static Dictionary ParseJsonToDictionary(string jsonString) return dictionary; } - [DllImport("__Internal")] - private static extern void ResetStatesInternal(string keysToExclude = null, Action OnStatesReset = null); private static Action onstatesReset; private static Action onplayersStatesReset; @@ -873,8 +799,7 @@ private static void InvokePlayersResetCallBack() } - [DllImport("__Internal")] - private static extern void ResetPlayersStatesInternal(string keysToExclude, Action OnPlayersStatesReset = null); + public static void ResetPlayersStates(string[] keysToExclude = null, Action OnStatesReset = null) { @@ -908,29 +833,7 @@ public static bool IsRunningInBrowser() #endif } - private static void MockSetState(string key, object value) - { - if (MockDictionary.ContainsKey(key)) - MockDictionary[key] = value; - else - MockDictionary.Add(key, value); - } - - private static T MockGetState(string key) - { - if (MockDictionary.TryGetValue(key, out var value) && value is T typedValue) - { - return typedValue; - } - else - { - Debug.LogWarning($"No {key} in States or value is not of type {typeof(T)}"); - return default; - } - } - [DllImport("__Internal")] - private static extern void UnsubscribeOnQuitInternal(); private static void UnsubscribeOnQuit() { @@ -952,18 +855,12 @@ private static void __OnQuitInternalHandler(string playerId) // Joystick - [DllImport("__Internal")] - private static extern void CreateJoystickInternal(string joyStickOptionsJson); - public static void CreateJoyStick(JoystickOptions options) { var jsonStr = ConvertJoystickOptionsToJson(options); CreateJoystickInternal(jsonStr); } - [DllImport("__Internal")] - private static extern string DpadJoystickInternal(); - public static Dpad DpadJoystick() { var jsonString = DpadJoystickInternal(); @@ -1021,7 +918,7 @@ public class JoystickOptions public ZoneOptions zones = null; } - [System.Serializable] + [Serializable] public class ButtonOptions { public string id = null; @@ -1038,18 +935,13 @@ public class ZoneOptions } - [System.Serializable] + [Serializable] public class Dpad { public string x; public string y; } - - - [DllImport("__Internal")] - private static extern void StartMatchmakingInternal(Action callback); - static Action startMatchmakingCallback = null; public static void StartMatchmaking(Action callback = null) { diff --git a/Assets/PlayroomKit/modules/CallbackManager.cs b/Assets/PlayroomKit/modules/CallbackManager.cs index 206009b..fc8e9cb 100644 --- a/Assets/PlayroomKit/modules/CallbackManager.cs +++ b/Assets/PlayroomKit/modules/CallbackManager.cs @@ -14,7 +14,6 @@ public static string RegisterCallback(Delegate callback, string key = null) if (string.IsNullOrEmpty(key)) key = GenerateKey(); - if (!callbacks.ContainsKey(key)) { callbacks.Add(key, callback); @@ -34,7 +33,7 @@ public static void InvokeCallback(string key, params string[] args) if (callback is Action action && args.Length == 0) action?.Invoke(); else if (callback is Action stringAction && args.Length == 1) stringAction?.Invoke(args[0]); else if (callback is Action doubleStringAction && args.Length == 2) doubleStringAction?.Invoke(args[0], args[1]); - else Debug.LogError($"Callback with key {key} is of unsupported type or incorrect number of arguments!"); + else Debug.LogError($"Callback with key {key} is of unsupported type or incorrect number of arguments: {args[0]}!"); } else { diff --git a/Assets/PlayroomKit/modules/Player.cs b/Assets/PlayroomKit/modules/Player.cs index a0a815f..48d24ed 100644 --- a/Assets/PlayroomKit/modules/Player.cs +++ b/Assets/PlayroomKit/modules/Player.cs @@ -212,7 +212,6 @@ public void SetState(string key, object value, bool reliable = false) } else { - Debug.Log($"State Set! Key: {key}, Value: {value}"); MockSetState(key, value); } } @@ -383,7 +382,6 @@ public void SetState(string key, Dictionary values, bool reliable = } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -403,7 +401,6 @@ public void SetState(string key, Dictionary values, bool reliable } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -423,7 +420,6 @@ public void SetState(string key, Dictionary values, bool reliable } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -443,7 +439,6 @@ public void SetState(string key, Dictionary values, bool reliabl } else { - Debug.Log($"PlayerState Set! Key: {key}, Value: {values}"); MockSetState(key, values); } } @@ -531,7 +526,7 @@ public Profile GetProfile() var testProfile = new Profile() { color = color1, - name = "CoolPlayTest", + name = "MockPlayer", jsonColor = mockJsonColor, photo = "testPhoto" diff --git a/Assets/PlayroomKit/modules/RPC.cs b/Assets/PlayroomKit/modules/RPC.cs index 9555f8d..0d9b6b3 100644 --- a/Assets/PlayroomKit/modules/RPC.cs +++ b/Assets/PlayroomKit/modules/RPC.cs @@ -32,7 +32,7 @@ public static void RpcRegister(string name, Action rpcRegisterCa } else { - Debug.LogError("[Mock Mode] RPC is currently not supported in Mock Mode!\n Please build the project to test RPC."); + MockRpcRegister(name, rpcRegisterCallback, onResponseReturn); } } @@ -79,7 +79,6 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson public static void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse) { - if (IsRunningInBrowser()) { string jsonData = ConvertToJson(data); @@ -111,7 +110,7 @@ public static void RpcCall(string name, object data, RpcMode mode, Action callba } else { - Debug.LogError("[Mock Mode] RPC Calls are not supported in Mock Mode! yet.\nPlease make a build to test RPC."); + MockRpcCall(name, data, mode, callbackOnResponse); } } diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 86dec15..74be0b0 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -55,18 +55,13 @@ private void Initialize() void Start() { PlayroomKit.RpcRegister("ShootBullet", HandleScoreUpdate, "You shot a bullet!"); - PlayroomKit.WaitForState("posX", PosX); - PlayroomKit.WaitForState("posY", PosY); + PlayroomKit.RpcRegister("Siu", Siu, "siuu"); } - private static void PosX(string pos) + private void Siu(string arg1, string arg2) { - Debug.Log($"pos X state is ready! {pos}"); - } - - private static void PosY(string pos) - { - Debug.Log($"pos Y state2 is ready! {pos}"); + var player = PlayroomKit.GetPlayer(arg2); + Debug.Log($"Caller: {arg2}, Player Name: {player?.GetProfile().name}, Data: {arg1}"); } void HandleScoreUpdate(string data, string caller) @@ -74,14 +69,11 @@ void HandleScoreUpdate(string data, string caller) var player = PlayroomKit.GetPlayer(caller); Debug.Log($"Caller: {caller}, Player Name: {player?.GetProfile().name}, Data: {data}"); - if (PlayerDict.TryGetValue(caller, out GameObject playerObj)) { - var playerController = playerObj.GetComponent(); if (playerController != null) { - playerController.scoreText.text = $"Score: {data}"; } else @@ -109,6 +101,15 @@ private void Update() ShootBullet(index); + + if (Input.GetKeyDown(KeyCode.W)) + { + PlayroomKit.RpcCall("Siu", 69, () => + { + Debug.LogWarning("hheeh"); + }); + } + if (Input.GetKeyDown(KeyCode.R) && PlayroomKit.IsHost()) { PlayroomKit.ResetStates(null, () =>