Skip to content

Commit

Permalink
Merge pull request #73 from asadm/protection-level-changes
Browse files Browse the repository at this point in the history
Non-Upstream Functions Private
  • Loading branch information
momintlh authored May 18, 2024
2 parents e84d1c3 + 49aebc9 commit a94ef01
Showing 1 changed file with 85 additions and 69 deletions.
154 changes: 85 additions & 69 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void Unsubscribe()
}
}

public static void UnsubscribeOnPlayerJoin(string CallbackID)
private static void UnsubscribeOnPlayerJoin(string CallbackID)
{
UnsubscribeOnPlayerJoinInternal(CallbackID);
}
Expand Down Expand Up @@ -595,7 +595,7 @@ public static void SetState(string key, Dictionary<string, string> 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())
{
Expand All @@ -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())
{
Expand All @@ -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())
{
Expand All @@ -661,7 +661,7 @@ public static float GetStateFloat(string key)
}
}

public static bool GetStateBool(string key)
private static bool GetStateBool(string key)
{
if (IsRunningInBrowser())
{
Expand All @@ -688,40 +688,63 @@ public static T GetState<T>(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<T>(GetStateString(key));
else if (type == typeof(Vector3)) return JsonUtility.FromJson<T>(GetStateString(key));
else if (type == typeof(Vector4)) return JsonUtility.FromJson<T>(GetStateString(key));
else if (type == typeof(Quaternion)) return JsonUtility.FromJson<T>(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<T>(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<string, T> GetState<T>(string key, bool isReturnDictionary = false)
{
if (IsRunningInBrowser() && isReturnDictionary)
{
var jsonString = GetStateDictionaryInternal(key);
return ParseJsonToDictionary<T>(jsonString);
}
else
{
if (!isPlayRoomInitialized)
if (isPlayRoomInitialized)
{
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return default;
if (isReturnDictionary)
{
return MockGetState<Dictionary<string, T>>(key);
}
else
{
return default;
}
}
else
{
return MockGetState<T>(key);
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return default;
}
}

}

[DllImport("__Internal")]
Expand Down Expand Up @@ -768,26 +791,7 @@ void OnStateSetCallback()
[DllImport("__Internal")]
private static extern string GetStateDictionaryInternal(string key);

public static Dictionary<string, T> GetStateDict<T>(string key)
{
if (IsRunningInBrowser())
{
var jsonString = GetStateDictionaryInternal(key);
return ParseJsonToDictionary<T>(jsonString);
}
else
{
if (!isPlayRoomInitialized)
{
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return default;
}
else
{
return MockGetState<Dictionary<string, T>>(key);
}
}
}


// Utils:
private static void SetStateHelper<T>(string key, Dictionary<string, T> values, bool reliable = false)
Expand Down Expand Up @@ -922,7 +926,7 @@ private static T MockGetState<T>(string key)
[DllImport("__Internal")]
private static extern void UnsubscribeOnQuitInternal();

public static void UnsubscribeOnQuit()
private static void UnsubscribeOnQuit()
{
UnsubscribeOnQuitInternal();
}
Expand All @@ -932,7 +936,7 @@ private static void __OnQuitInternalHandler(string playerId)
{
if (Players.TryGetValue(playerId, out Player player))
{
player.OnQuitWrapperCallback();
((IPlayerInteraction)player).InvokeOnQuitWrapperCallback();
}
else
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand All @@ -1271,7 +1272,13 @@ private static void InvokeStartMatchmakingCallback()
}

// Player class
public class Player

public interface IPlayerInteraction
{
void InvokeOnQuitWrapperCallback();
}

public class Player : IPlayerInteraction
{

[Serializable]
Expand Down Expand Up @@ -1300,7 +1307,7 @@ public class JsonColor


public string id;
public static int totalObjects = 0;
private static int totalObjects = 0;


public Player(string id)
Expand Down Expand Up @@ -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<string> callback)
{
if (!isPlayRoomInitialized)
Expand Down Expand Up @@ -1493,28 +1505,36 @@ public T GetState<T>(string key)
}


public Dictionary<string, T> GetStateDict<T>(string key)
public Dictionary<string, T> GetState<T>(string key, bool isReturnDictionary = false)
{
if (IsRunningInBrowser())

if (IsRunningInBrowser() && isReturnDictionary)
{
var jsonString = GetPlayerStateDictionary(id, key);
return ParseJsonToDictionary<T>(jsonString);
}
else
{
if (!isPlayRoomInitialized)
if (isPlayRoomInitialized)
{
Debug.LogError("[Mock Mode] Playroom not initialized yet! Please call InsertCoin.");
return default;
if (isReturnDictionary)
{
return MockGetState<Dictionary<string, T>>(key);
}
else
{
return default;
}
}
else
{
return MockGetState<Dictionary<string, T>>(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())
{
Expand All @@ -1534,7 +1554,7 @@ public int GetPlayerStateInt(string key)
}
}

public float GetPlayerStateFloat(string key)
private float GetPlayerStateFloat(string key)
{
if (IsRunningInBrowser())
{
Expand All @@ -1554,7 +1574,7 @@ public float GetPlayerStateFloat(string key)
}
}

public string GetPlayerStateString(string key)
private string GetPlayerStateString(string key)
{
if (IsRunningInBrowser())
{
Expand All @@ -1574,7 +1594,7 @@ public string GetPlayerStateString(string key)
}
}

public bool GetPlayerStateBool(string key)
private bool GetPlayerStateBool(string key)
{
if (IsRunningInBrowser())
{
Expand Down Expand Up @@ -1687,12 +1707,6 @@ public void SetState(string key, Dictionary<string, string> values, bool reliabl
}
}

public Dictionary<string, float> GetStateFloat(string id, string key)
{
var jsonString = GetPlayerStateDictionary(id, key);
return ParseJsonToDictionary<float>(jsonString);
}

public void WaitForState(string StateKey, Action onStateSetCallback = null)
{
if (IsRunningInBrowser())
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -1876,6 +1890,8 @@ private void SetStateHelper<T>(string id, string key, Dictionary<string, T> valu
// Output the JSON string
SetPlayerStateDictionary(id, key, jsonString, reliable);
}


}
}

Expand Down

0 comments on commit a94ef01

Please sign in to comment.