Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Onquit Fix for build. #121

Merged
merged 12 commits into from
Nov 22, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: mockMode
value: 1
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8987662522597341863, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: insertCoinCaller
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,9 @@ private void Update()

for (var i = 0; i < players.Count; i++)
{
if (players[i] != null)
if (players[i] != null && PlayerDict.TryGetValue(players[i].id, out GameObject playerObj))
{
Debug.Log("Getting state of: " + players[i].id);
var pos = players[i].GetState<Vector3>("pos");

Debug.Log(pos);
Expand Down Expand Up @@ -176,7 +177,6 @@ public void AddPlayer(PlayroomKit.Player player)
/// <summary>
/// Remove player from the game, called when the player leaves / closes the game.
/// </summary>
[MonoPInvokeCallback(typeof(Action<string>))]
private static void RemovePlayer(string playerID)
{
if (PlayerDict.TryGetValue(playerID, out GameObject player))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@ private void Start()
matchmaking = false,
discord = true,
gameId = "ii4pV1wfceCjjLvRoo3O",
roomCode = roomCode,
roomCode = "roomCode",
}, () =>
{
_playroomKit.OnPlayerJoin(AddPlayer);

_playroomKit.RpcRegister("one", ((data, player) => { Debug.LogWarning("One Event Called"); }));

_playroomKit.RpcRegister("two", ((data, player) => { Debug.LogWarning("two Event Called"); }));
_playroomKit.RpcRegister("one", ((data, player) => { Debug.LogWarning("One Event Called With a diff callback"); }));


_playroomKit.RpcRegister("one",
((data, player) => { Debug.LogWarning("One Event Called With a diff callback"); }));
}, () => { Debug.Log("OnDisconnect callback"); });
}

Expand All @@ -68,7 +68,7 @@ private void Update()

for (var i = 0; i < players.Count; i++)
{
if (players[i] != null)
if (players[i] != null && PlayerDict.TryGetValue(players[i].id, out GameObject playerObj))
{
var pos = players[i].GetState<Vector3>("move");
var rotate = players[i].GetState<Quaternion>("angle");
Expand Down Expand Up @@ -103,16 +103,17 @@ private void AddPlayer(PlayroomKit.Player player)
player.OnQuit(RemovePlayer);
}

// FIX:
[MonoPInvokeCallback(typeof(Action<string>))]
private static void RemovePlayer(string playerID)
{
Debug.Log("Here?");
if (PlayerDict.TryGetValue(playerID, out var player))
{
PlayerDict.Remove(playerID);
playerGameObjects.Remove(player);
Destroy(player);

foreach (var (key, value) in PlayerDict) Debug.Log($"player {key} is still in the room");

Debug.Log(playerID + " has left the room!");
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ public void ShootLaser()
var index = players.IndexOf(myPlayer);
score = playerGameObjects[index].GetComponent<Laser>().ShootLaser(score);
_playroomKit.RpcCall("ShootLaser", score, PlayroomKit.RpcMode.ALL,
() => { logsText.text = "ShootLaser RPC Called"; });
() =>
{
Debug.Log("ShootLaser RPC Called");
logsText.text = "ShootLaser RPC Called"; });
}

public void GetRoomCode()
Expand Down
4 changes: 4 additions & 0 deletions Assets/PlayroomKit/Examples/package-showcase/showcase.unity
Original file line number Diff line number Diff line change
Expand Up @@ -7767,6 +7767,10 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 8899696337967424923, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: httpServerPort
value: 6510
objectReference: {fileID: 0}
- target: {fileID: 8899696337967424923, guid: 1b30fa3a265114a45a7e23165a20aecc, type: 3}
propertyPath: webDriverDirectory
value: Assets\
Expand Down
43 changes: 25 additions & 18 deletions Assets/PlayroomKit/PlayroomKit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ public PlayroomKit()
#if !UNITY_EDITOR
_playroomService = new PlayroomBuildService(new PlayroomKitInterop());
_rpc = new RPC(this);

#elif UNITY_EDITOR

if (CurrentMockMode == MockModeSelector.Local)
{
_playroomService = new LocalMockPlayroomService();
_rpc = new RPCLocal();
}
else if (CurrentMockMode == MockModeSelector.Browser)

switch (CurrentMockMode)
{
_playroomService = new BrowserMockService();
_rpc = new BrowserMockRPC();
case MockModeSelector.Local:
Debug.Log("Starting playroom in Local Mock Mode");
_playroomService = new LocalMockPlayroomService();
_rpc = new RPCLocal();
break;

case MockModeSelector.Browser:
Debug.Log("Starting playroom in Browser Mock Mode");
_playroomService = new BrowserMockService();
_rpc = new BrowserMockRPC();
break;
default:
_playroomService = new LocalMockPlayroomService();
_rpc = new RPCLocal();
break;
}
#endif
}
Expand Down Expand Up @@ -89,14 +97,15 @@ public Player GetPlayer(string playerId)
else
{
#if UNITY_EDITOR
if (CurrentMockMode == MockModeSelector.Local)
{
player = new Player(playerId, new Player.LocalPlayerService(playerId));
}
else if (CurrentMockMode == MockModeSelector.Browser)
switch (CurrentMockMode)
{
player = new Player(playerId,
new BrowserMockPlayerService(UnityBrowserBridge.Instance, playerId));
case MockModeSelector.Local:
player = new Player(playerId, new Player.LocalPlayerService(playerId));
break;
case MockModeSelector.Browser:
player = new Player(playerId,
new BrowserMockPlayerService(UnityBrowserBridge.Instance, playerId));
break;
}
#else
player = new Player(playerId, new Player.PlayerService(playerId));
Expand Down Expand Up @@ -301,7 +310,5 @@ private void UnsubscribeOnQuit()
{
_playroomService.UnsubscribeOnQuit();
}

// DI END
}
}
13 changes: 1 addition & 12 deletions Assets/PlayroomKit/modules/Helpers/CallbackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Playroom
public static class CallbackManager
{
private static Dictionary<string, Delegate> callbacks = new();

public static string RegisterCallback(Delegate callback, string key = null)
{
if (string.IsNullOrEmpty(key))
Expand Down Expand Up @@ -38,18 +38,7 @@ public static void InvokeCallback(string key, params string[] args)
Debug.LogError(
$"Callback with key {key} is of unsupported type or incorrect number of arguments: {args[0]}!");
}
else
{
Debug.LogWarning(
$"Callback with key {key} not found!, maybe register the callback or call the correct playroom function?");
}
}

public static bool CheckCallback(string key)
{
return callbacks.TryGetValue(key, out Delegate callback);
}


private static string GenerateKey()
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/modules/Player/LocalPlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Profile GetProfile()

public Action OnQuit(Action<string> callback)
{
Debug.Log($"OnQuit is not implemented for local");
Debug.Log($"OnQuit is not supported in Local Mock Mode.");
return null;
}

Expand Down
17 changes: 17 additions & 0 deletions Assets/PlayroomKit/modules/Player/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,23 @@ public void WaitForState(string StateKey, Action<string> onStateSetCallback = nu
_playerService.WaitForState(StateKey, onStateSetCallback);
}


public void InvokePlayerOnQuitCallback()
{
// Attempt the cast and throw an exception if it fails
if ((_playerService is PlayerService playerService))
{
playerService.InvokePlayerOnQuitCallback(id);
}
else
{
Debug.LogWarning("InvokePlayerOnQuitCallback is only supported o build");
}

// Invoke the method on the casted object

}

//DI END


Expand Down
12 changes: 10 additions & 2 deletions Assets/PlayroomKit/modules/Player/PlayerService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.CompilerServices;
using UnityEngine;
using System.Runtime.InteropServices;
using AOT;
Expand Down Expand Up @@ -139,6 +140,11 @@ void Unsubscribe()
return Unsubscribe;
}

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


public void Kick(Action onKickCallBack = null)
{
Expand Down Expand Up @@ -167,14 +173,14 @@ private static void InvokeKickCallBack()
}

[MonoPInvokeCallback(typeof(Action))]
private void OnQuitWrapperCallback(string id)
public void OnQuitWrapperCallback(string id)
{
if (OnQuitCallbacks != null)
foreach (var callback in OnQuitCallbacks)
callback?.Invoke(id);
}

void InvokeOnQuitWrapperCallback(string id)
public void InvokeOnQuitWrapperCallback(string id)
{
OnQuitWrapperCallback(id);
}
Expand All @@ -186,6 +192,8 @@ private bool GetPlayerStateBoolById(string key)
stateValue == 0 ? false :
throw new InvalidOperationException($"GetStateBool: {key} is not a bool");
}


}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/PlayroomKit/modules/PlayroomService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private static void __OnQuitInternalHandler(string playerId)
{
if (Players.TryGetValue(playerId, out Player player))
{
throw new NotImplementedException(); //implement OnQuitWrapperCallback;
player.InvokePlayerOnQuitCallback();
}
else
{
Expand Down
1 change: 0 additions & 1 deletion Assets/PlayroomKit/modules/PlayroomkitDevManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ private void OnValidate()
private void UpdateMockMode()
{
PlayroomKit.CurrentMockMode = mockMode;

}

/// <summary>
Expand Down
8 changes: 1 addition & 7 deletions Assets/PlayroomKit/modules/RPC/RPC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public void RpcRegister(string name, Action<string, string> rpcRegisterCallback,

public void RpcCall(string name, object data, RpcMode mode, Action callbackOnResponse = null)
{
if (CallbackManager.CheckCallback(name))
{
string jsonData = ConvertToJson(data);

if (OnResponseCallbacks.ContainsKey(name))
Expand Down Expand Up @@ -74,11 +72,7 @@ public void RpcCall(string name, object data, RpcMode mode, Action callbackOnRes
*/
_playroomKit.SetState("rpcCalledEventName", jsonString, reliable: true);
_interop.RpcCallWrapper(name, jsonData, mode, InvokeOnResponseCallback);
}
else
{
Debug.LogError("RPC is not registered!, register RPC first.");
}

}

public void RpcCall(string name, object data, Action callbackOnResponse = null)
Expand Down
6 changes: 0 additions & 6 deletions ProjectSettings/EditorBuildSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ EditorBuildSettings:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Scenes:
- enabled: 0
path: Assets/Scenes/Lobby.unity
guid: 27215d4ccddeb6d49af03911dd97f9b1
- enabled: 0
path: Assets/PlayroomKit/Examples/2d-platformer/2d-platformer.unity
guid: cf82eec4c654cf44b89cec2a91afebd5
- enabled: 0
path: Assets/PlayroomKit/Examples/discord-activity/discord-activity.unity
guid: bb7ef8e4a67f3fb46b78e5d95a6858a7
- enabled: 0
path: Assets/Scenes/topdown.unity
guid: 490247da3cf48784ab657a183ba059d1
- enabled: 1
path: Assets/PlayroomKit/Examples/package-showcase/showcase.unity
guid: 85c9af77ae006408ab24500b953a9035
Expand Down