diff --git a/QSB/API/IQSBAPI.cs b/QSB/API/IQSBAPI.cs index 9afe6d28..4f396d86 100644 --- a/QSB/API/IQSBAPI.cs +++ b/QSB/API/IQSBAPI.cs @@ -38,15 +38,37 @@ public interface IQSBAPI uint[] GetPlayerIDs(); /// - /// Invoked when a player joins the game. + /// Invoked when any player (local or remote) joins the game. /// UnityEvent OnPlayerJoin(); /// - /// Invoked when a player leaves the game. + /// Invoked when any player (local or remote) leaves the game. /// UnityEvent OnPlayerLeave(); + /// + /// Invoked when the local client joins a game, + /// either through connecting to an external game or hosting their own game. + /// + UnityEvent OnLocalJoin(); + + /// + /// Invoked when a client (but not the local player) joins the game. + /// + UnityEvent OnPeerJoin(); + + /// + /// Invoked when the local client leaves a game, + /// either through disconnecting from an external game or shutting down the hosted server. + /// + UnityEvent OnLocalLeave(); + + /// + /// Invoked when a client (but not the local player) leaves the game. + /// + UnityEvent OnPeerLeave(); + /// /// Sets some arbitrary data for a given player. /// diff --git a/QSB/API/QSBAPI.cs b/QSB/API/QSBAPI.cs index 793eacae..9e330177 100644 --- a/QSB/API/QSBAPI.cs +++ b/QSB/API/QSBAPI.cs @@ -24,9 +24,14 @@ public class QSBAPI : IQSBAPI public uint[] GetPlayerIDs() => QSBPlayerManager.PlayerList.Select(x => x.PlayerId).ToArray(); public UnityEvent OnPlayerJoin() => QSBAPIEvents.OnPlayerJoinEvent; - public UnityEvent OnPlayerLeave() => QSBAPIEvents.OnPlayerLeaveEvent; + public UnityEvent OnLocalJoin() => QSBAPIEvents.OnLocalJoinEvent; + public UnityEvent OnLocalLeave() => QSBAPIEvents.OnLocalLeaveEvent; + + public UnityEvent OnPeerJoin() => QSBAPIEvents.OnPeerJoinEvent; + public UnityEvent OnPeerLeave() => QSBAPIEvents.OnPeerLeaveEvent; + public void SetCustomData(uint playerId, string key, T data) => QSBPlayerManager.GetPlayer(playerId).SetCustomData(key, data); public T GetCustomData(uint playerId, string key) => QSBPlayerManager.GetPlayer(playerId).GetCustomData(key); @@ -41,11 +46,43 @@ internal static class QSBAPIEvents { static QSBAPIEvents() { - QSBPlayerManager.OnAddPlayer += player => OnPlayerJoinEvent.Invoke(player.PlayerId); - QSBPlayerManager.OnRemovePlayer += player => OnPlayerLeaveEvent.Invoke(player.PlayerId); + QSBPlayerManager.OnAddPlayer += (player) => + { + OnPlayerJoinEvent.Invoke(player.PlayerId); + + if (player.IsLocalPlayer) + { + OnLocalJoinEvent.Invoke(); + } + else + { + OnPeerJoinEvent.Invoke(player.PlayerId); + } + }; + + QSBPlayerManager.OnRemovePlayer += (player) => + { + OnPlayerLeaveEvent.Invoke(player.PlayerId); + + if (player.IsLocalPlayer) + { + OnLocalLeaveEvent.Invoke(); + } + else + { + OnPeerLeaveEvent.Invoke(player.PlayerId); + } + }; } internal class PlayerEvent : UnityEvent { } - internal static PlayerEvent OnPlayerJoinEvent = new PlayerEvent(); - internal static PlayerEvent OnPlayerLeaveEvent = new PlayerEvent(); + + internal static PlayerEvent OnPlayerJoinEvent = new(); + internal static PlayerEvent OnPlayerLeaveEvent = new(); + + internal static UnityEvent OnLocalJoinEvent = new(); + internal static UnityEvent OnLocalLeaveEvent = new(); + + internal static PlayerEvent OnPeerJoinEvent = new(); + internal static PlayerEvent OnPeerLeaveEvent = new(); }