From 647af7bdf0b97cbcbc1a38b849c5b589f65fe7b4 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Fri, 17 May 2024 22:31:04 +0500 Subject: [PATCH 1/5] fix: made internal functions private. * Made all non-upstream functions private #72 * Removed unnecessary warning log --- Assets/PlayroomKit/PlayroomKit.cs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 56f6d79..ecfb85a 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -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()) { @@ -1073,10 +1073,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) { @@ -1514,7 +1511,7 @@ public Dictionary GetStateDict(string key) } } - public int GetPlayerStateInt(string key) + private int GetPlayerStateInt(string key) { if (IsRunningInBrowser()) { @@ -1534,7 +1531,7 @@ public int GetPlayerStateInt(string key) } } - public float GetPlayerStateFloat(string key) + private float GetPlayerStateFloat(string key) { if (IsRunningInBrowser()) { @@ -1554,7 +1551,7 @@ public float GetPlayerStateFloat(string key) } } - public string GetPlayerStateString(string key) + private string GetPlayerStateString(string key) { if (IsRunningInBrowser()) { @@ -1574,7 +1571,7 @@ public string GetPlayerStateString(string key) } } - public bool GetPlayerStateBool(string key) + private bool GetPlayerStateBool(string key) { if (IsRunningInBrowser()) { @@ -1687,12 +1684,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 +1809,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()) { From 44d18f08bc3fe9ce48c98f53d0c39a5e74fab84f Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Sat, 18 May 2024 15:30:02 +0500 Subject: [PATCH 2/5] fix: more changes on access level --- Assets/PlayroomKit/PlayroomKit.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index ecfb85a..11eb197 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); } @@ -922,7 +922,7 @@ private static T MockGetState(string key) [DllImport("__Internal")] private static extern void UnsubscribeOnQuitInternal(); - public static void UnsubscribeOnQuit() + private static void UnsubscribeOnQuit() { UnsubscribeOnQuitInternal(); } @@ -1245,7 +1245,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) @@ -1297,7 +1297,7 @@ public class JsonColor public string id; - public static int totalObjects = 0; + private static int totalObjects = 0; public Player(string id) From 79a1d8e81ab745e8ec408d8c1b48ba6c21de0c97 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Sat, 18 May 2024 22:58:37 +0500 Subject: [PATCH 3/5] fix: GetState now supports dictionaries --- Assets/PlayroomKit/PlayroomKit.cs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 11eb197..6bd6899 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -704,6 +704,10 @@ public static T GetState(string key) { return (T)(object)GetStateString(key); } + else if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Dictionary)) + { + return (T)(object)GetStateDict(key); + } else { Debug.LogError($"GetState<{typeof(T)}> is not supported."); @@ -768,7 +772,7 @@ void OnStateSetCallback() [DllImport("__Internal")] private static extern string GetStateDictionaryInternal(string key); - public static Dictionary GetStateDict(string key) + private static Dictionary GetStateDict(string key) { if (IsRunningInBrowser()) { @@ -1073,7 +1077,7 @@ private static void InvokeRpcRegisterCallBack(string dataJson, string senderJson var player = new Player(senderJson); Players.Add(senderJson, player); } - + } catch (Exception ex) { @@ -1473,6 +1477,10 @@ public T GetState(string key) else if (type == typeof(Vector3)) return (T)(object)JsonUtility.FromJson(GetPlayerStateStringById(id, key)); else if (type == typeof(Vector2)) return (T)(object)JsonUtility.FromJson(GetPlayerStateStringById(id, key)); else if (type == typeof(Quaternion)) return (T)(object)JsonUtility.FromJson(GetPlayerStateStringById(id, key)); + else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary)) + { + return (T)(object)GetStateDict(key); + } else throw new NotSupportedException($"Type {typeof(T)} is not supported by GetState"); } else @@ -1490,7 +1498,7 @@ public T GetState(string key) } - public Dictionary GetStateDict(string key) + private Dictionary GetStateDict(string key) { if (IsRunningInBrowser()) { From e237cf8036cb44695269a98578e00a095ebc7b8f Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Sat, 18 May 2024 23:16:36 +0500 Subject: [PATCH 4/5] fix: another function from public 2 private --- Assets/PlayroomKit/PlayroomKit.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index 6bd6899..dad72c0 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -936,7 +936,7 @@ private static void __OnQuitInternalHandler(string playerId) { if (Players.TryGetValue(playerId, out Player player)) { - player.OnQuitWrapperCallback(); + ((IPlayerInteraction)player).InvokeOnQuitWrapperCallback(); } else { @@ -1272,7 +1272,13 @@ private static void InvokeStartMatchmakingCallback() } // Player class - public class Player + + public interface IPlayerInteraction + { + void InvokeOnQuitWrapperCallback(); + } + + public class Player : IPlayerInteraction { [Serializable] @@ -1336,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) @@ -1875,6 +1886,8 @@ private void SetStateHelper(string id, string key, Dictionary valu // Output the JSON string SetPlayerStateDictionary(id, key, jsonString, reliable); } + + } } From 49aebc963de6eb9e4a5d7261ed32e5cfd9f72ff8 Mon Sep 17 00:00:00 2001 From: momintlh <77355191+momintlh@users.noreply.github.com> Date: Sun, 19 May 2024 00:37:52 +0500 Subject: [PATCH 5/5] fix: overloaded GetState for dictionary. --- Assets/PlayroomKit/PlayroomKit.cs | 106 ++++++++++++++++-------------- 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/Assets/PlayroomKit/PlayroomKit.cs b/Assets/PlayroomKit/PlayroomKit.cs index dad72c0..fdd17f0 100644 --- a/Assets/PlayroomKit/PlayroomKit.cs +++ b/Assets/PlayroomKit/PlayroomKit.cs @@ -688,44 +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)) - { - return (T)(object)GetStateBool(key); - } - else if (typeof(T) == typeof(string)) + 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)GetStateString(key); + Debug.LogError($"GetState<{type}> is not supported."); + return default; } - else if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(Dictionary)) + } + else + { + if (isPlayRoomInitialized) { - return (T)(object)GetStateDict(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")] @@ -772,26 +791,7 @@ void OnStateSetCallback() [DllImport("__Internal")] private static extern string GetStateDictionaryInternal(string key); - private 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) @@ -1488,10 +1488,6 @@ public T GetState(string key) else if (type == typeof(Vector3)) return (T)(object)JsonUtility.FromJson(GetPlayerStateStringById(id, key)); else if (type == typeof(Vector2)) return (T)(object)JsonUtility.FromJson(GetPlayerStateStringById(id, key)); else if (type == typeof(Quaternion)) return (T)(object)JsonUtility.FromJson(GetPlayerStateStringById(id, key)); - else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary)) - { - return (T)(object)GetStateDict(key); - } else throw new NotSupportedException($"Type {typeof(T)} is not supported by GetState"); } else @@ -1509,23 +1505,31 @@ public T GetState(string key) } - private 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; } } }