diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 56f6d79..fdd17f0 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -285,7 +285,7 @@ void Unsubscribe() } } - public static void UnsubscribeOnPlayerJoin(string CallbackID) + private static void UnsubscribeOnPlayerJoin(string CallbackID) { UnsubscribeOnPlayerJoinInternal(CallbackID); } @@ -595,7 +595,7 @@ public static void SetState(string key, Dictionary values, bool [DllImport("__Internal")] private static extern string GetStateStringInternal(string key); - public static string GetStateString(string key) + private static string GetStateString(string key) { if (IsRunningInBrowser()) { @@ -618,7 +618,7 @@ public static string GetStateString(string key) [DllImport("__Internal")] private static extern int GetStateIntInternal(string key); - public static int GetStateInt(string key) + private static int GetStateInt(string key) { if (IsRunningInBrowser()) { @@ -641,7 +641,7 @@ public static int GetStateInt(string key) [DllImport("__Internal")] private static extern float GetStateFloatInternal(string key); - public static float GetStateFloat(string key) + private static float GetStateFloat(string key) { if (IsRunningInBrowser()) { @@ -661,7 +661,7 @@ public static float GetStateFloat(string key) } } - public static bool GetStateBool(string key) + private static bool GetStateBool(string key) { if (IsRunningInBrowser()) { @@ -688,40 +688,63 @@ public static T GetState(string key) { if (IsRunningInBrowser()) { - if (typeof(T) == typeof(int)) - { - return (T)(object)GetStateInt(key); - } - else if (typeof(T) == typeof(float)) - { - return (T)(object)GetStateFloat(key); - } - else if (typeof(T) == typeof(bool)) + Type type = typeof(T); + if (type == typeof(int)) return (T)(object)GetStateInt(key); + else if (type == typeof(float)) return (T)(object)GetStateFloat(key); + else if (type == typeof(bool)) return (T)(object)GetStateBool(key); + else if (type == typeof(string)) return (T)(object)GetStateString(key); + else if (type == typeof(Vector2)) return JsonUtility.FromJson(GetStateString(key)); + else if (type == typeof(Vector3)) return JsonUtility.FromJson(GetStateString(key)); + else if (type == typeof(Vector4)) return JsonUtility.FromJson(GetStateString(key)); + else if (type == typeof(Quaternion)) return JsonUtility.FromJson(GetStateString(key)); + else { - return (T)(object)GetStateBool(key); + Debug.LogError($"GetState<{type}> is not supported."); + return default; } - else if (typeof(T) == typeof(string)) + } + else + { + if (isPlayRoomInitialized) { - return (T)(object)GetStateString(key); + return MockGetState(key); } else { - Debug.LogError($"GetState<{typeof(T)}> is not supported."); + Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); return default; } } + } + + + public static Dictionary GetState(string key, bool isReturnDictionary = false) + { + if (IsRunningInBrowser() && isReturnDictionary) + { + var jsonString = GetStateDictionaryInternal(key); + return ParseJsonToDictionary(jsonString); + } else { - if (!isPlayRoomInitialized) + if (isPlayRoomInitialized) { - Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); - return default; + if (isReturnDictionary) + { + return MockGetState>(key); + } + else + { + return default; + } } else { - return MockGetState(key); + Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); + return default; } } + } [DllImport("__Internal")] @@ -768,26 +791,7 @@ void OnStateSetCallback() [DllImport("__Internal")] private static extern string GetStateDictionaryInternal(string key); - public static Dictionary GetStateDict(string key) - { - if (IsRunningInBrowser()) - { - var jsonString = GetStateDictionaryInternal(key); - return ParseJsonToDictionary(jsonString); - } - else - { - if (!isPlayRoomInitialized) - { - Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); - return default; - } - else - { - return MockGetState>(key); - } - } - } + // Utils: private static void SetStateHelper(string key, Dictionary values, bool reliable = false) @@ -922,7 +926,7 @@ private static T MockGetState(string key) [DllImport("__Internal")] private static extern void UnsubscribeOnQuitInternal(); - public static void UnsubscribeOnQuit() + private static void UnsubscribeOnQuit() { UnsubscribeOnQuitInternal(); } @@ -932,7 +936,7 @@ private static void __OnQuitInternalHandler(string playerId) { if (Players.TryGetValue(playerId, out Player player)) { - player.OnQuitWrapperCallback(); + ((IPlayerInteraction)player).InvokeOnQuitWrapperCallback(); } else { @@ -1073,10 +1077,7 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson var player = new Player(senderJson); Players.Add(senderJson, player); } - else - { - Debug.LogWarning($"Players dictionary already has a player with ID: {senderJson}!"); - } + } catch (Exception ex) { @@ -1248,7 +1249,7 @@ private static string ConvertComplexToJson(object data) } [DllImport("__Internal")] - public static extern void StartMatchmakingInternal(Action callback); + private static extern void StartMatchmakingInternal(Action callback); static Action startMatchmakingCallback = null; public static void StartMatchmaking(Action callback = null) @@ -1271,7 +1272,13 @@ private static void InvokeStartMatchmakingCallback() } // Player class - public class Player + + public interface IPlayerInteraction + { + void InvokeOnQuitWrapperCallback(); + } + + public class Player : IPlayerInteraction { [Serializable] @@ -1300,7 +1307,7 @@ public class JsonColor public string id; - public static int totalObjects = 0; + private static int totalObjects = 0; public Player(string id) @@ -1335,13 +1342,18 @@ private void OnQuitDefaultCallback() } [MonoPInvokeCallback(typeof(Action))] - public void OnQuitWrapperCallback() + private void OnQuitWrapperCallback() { if (OnQuitCallbacks != null) foreach (var callback in OnQuitCallbacks) callback?.Invoke(id); } + void IPlayerInteraction.InvokeOnQuitWrapperCallback() + { + OnQuitWrapperCallback(); + } + public Action OnQuit(Action callback) { if (!isPlayRoomInitialized) @@ -1493,28 +1505,36 @@ public T GetState(string key) } - public Dictionary GetStateDict(string key) + public Dictionary GetState(string key, bool isReturnDictionary = false) { - if (IsRunningInBrowser()) + + if (IsRunningInBrowser() && isReturnDictionary) { var jsonString = GetPlayerStateDictionary(id, key); return ParseJsonToDictionary(jsonString); } else { - if (!isPlayRoomInitialized) + if (isPlayRoomInitialized) { - Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); - return default; + if (isReturnDictionary) + { + return MockGetState>(key); + } + else + { + return default; + } } else { - return MockGetState>(key); + Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin."); + return default; } } } - public int GetPlayerStateInt(string key) + private int GetPlayerStateInt(string key) { if (IsRunningInBrowser()) { @@ -1534,7 +1554,7 @@ public int GetPlayerStateInt(string key) } } - public float GetPlayerStateFloat(string key) + private float GetPlayerStateFloat(string key) { if (IsRunningInBrowser()) { @@ -1554,7 +1574,7 @@ public float GetPlayerStateFloat(string key) } } - public string GetPlayerStateString(string key) + private string GetPlayerStateString(string key) { if (IsRunningInBrowser()) { @@ -1574,7 +1594,7 @@ public string GetPlayerStateString(string key) } } - public bool GetPlayerStateBool(string key) + private bool GetPlayerStateBool(string key) { if (IsRunningInBrowser()) { @@ -1687,12 +1707,6 @@ public void SetState(string key, Dictionary values, bool reliabl } } - public Dictionary GetStateFloat(string id, string key) - { - var jsonString = GetPlayerStateDictionary(id, key); - return ParseJsonToDictionary(jsonString); - } - public void WaitForState(string StateKey, Action onStateSetCallback = null) { if (IsRunningInBrowser()) @@ -1818,7 +1832,7 @@ private static void InvokeKickCallBack() private static extern void WaitForPlayerStateInternal(string playerID, string stateKey, Action onStateSetCallback = null); - public static bool GetPlayerStateBoolById(string id, string key) + private static bool GetPlayerStateBoolById(string id, string key) { if (IsRunningInBrowser()) { @@ -1876,6 +1890,8 @@ private void SetStateHelper(string id, string key, Dictionary valu // Output the JSON string SetPlayerStateDictionary(id, key, jsonString, reliable); } + + } }