From a0ed7bee0324a015c63413968b544beabfe9db6a Mon Sep 17 00:00:00 2001 From: Xilophor Date: Sat, 7 Sep 2024 16:14:19 -0400 Subject: [PATCH] Rearranged LNetworkUtils; Added OnNetworkStartCallback --- .../Patches/NetworkManagerPatch.cs | 11 ++++- LethalNetworkAPI/Utils/LNetworkUtils.cs | 45 ++++++++++++------- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/LethalNetworkAPI/Patches/NetworkManagerPatch.cs b/LethalNetworkAPI/Patches/NetworkManagerPatch.cs index 476d828..acd1750 100644 --- a/LethalNetworkAPI/Patches/NetworkManagerPatch.cs +++ b/LethalNetworkAPI/Patches/NetworkManagerPatch.cs @@ -4,6 +4,7 @@ namespace LethalNetworkAPI.Patches; using Internal; using Old.Networking; using Unity.Netcode; +using Utils; [HarmonyPatch(typeof(NetworkManager))] [HarmonyPriority(Priority.HigherThanNormal)] @@ -12,16 +13,22 @@ internal static class NetworkManagerPatch { [HarmonyPatch(nameof(NetworkManager.Initialize))] [HarmonyPostfix] - public static void InitializePatch() + public static void InitializePatch(NetworkManager __instance) { _ = new UnnamedMessageHandler(); _ = new NetworkHandler(); + + __instance.OnServerStarted += LNetworkUtils.InvokeOnNetworkStartCallback; + __instance.OnClientStarted += LNetworkUtils.InvokeOnNetworkStartCallback; } [HarmonyPatch(nameof(NetworkManager.ShutdownInternal))] [HarmonyPrefix] - public static void ShutdownPatch() + public static void ShutdownPatch(NetworkManager __instance) { + __instance.OnServerStarted -= LNetworkUtils.InvokeOnNetworkStartCallback; + __instance.OnClientStarted -= LNetworkUtils.InvokeOnNetworkStartCallback; + UnnamedMessageHandler.Instance?.Dispose(); NetworkHandler.Instance?.Dispose(); } diff --git a/LethalNetworkAPI/Utils/LNetworkUtils.cs b/LethalNetworkAPI/Utils/LNetworkUtils.cs index d63c1d5..fafbed5 100644 --- a/LethalNetworkAPI/Utils/LNetworkUtils.cs +++ b/LethalNetworkAPI/Utils/LNetworkUtils.cs @@ -1,7 +1,9 @@ namespace LethalNetworkAPI.Utils; +using System; using System.Diagnostics; using System.Linq; +using System.Runtime.CompilerServices; using BepInEx; using HarmonyLib; using Unity.Netcode; @@ -9,20 +11,10 @@ namespace LethalNetworkAPI.Utils; public static class LNetworkUtils { /// - /// Get the client's GUID from the player ID. + /// Called when the local client establishes a connection to, or starts up, a server. /// - /// The in-game player ID. - /// The client's NGO GUID. - public static ulong GetClientGuid(int playerId) => - StartOfRound.Instance.allPlayerScripts[playerId].actualClientId; - - /// - /// Get the client's player ID from the client's GUID. - /// - /// The client's NGO GUID. - /// The client's in-game player ID. - public static int GetPlayerId(ulong clientGuid) => - (int)StartOfRound.Instance.allPlayerScripts.First(player => player.actualClientId == clientGuid).playerClientId; + /// Called with bool isServer. + public static event Action OnNetworkStartCallback = delegate { }; /// /// Whether the client is connected to a server. @@ -32,7 +24,7 @@ public static int GetPlayerId(ulong clientGuid) => /// /// Whether the client is the host or server. /// - public static bool IsHostOrServer => IsConnected && (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer); + public static bool IsHostOrServer => NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer; /// /// All connected clients' GUIDs. @@ -63,14 +55,32 @@ public static ulong[] AllConnectedClients /// /// The client to exclude. /// This will be empty if not connected to a server. - public static ulong[] AllConnectedClientsExcept(ulong clientId) => AllConnectedClients.Where(i => i != clientId).ToArray(); + public static ulong[] AllConnectedClientsExcept(ulong clientId) => + AllConnectedClients.Where(i => i != clientId).ToArray(); /// /// All connected clients' GUIDs, except the specified client. /// /// The clients to exclude. /// This will be empty if not connected to a server. - public static ulong[] AllConnectedClientsExcept(params ulong[] clientIds) => AllConnectedClients.Where(i => !clientIds.Contains(i)).ToArray(); + public static ulong[] AllConnectedClientsExcept(params ulong[] clientIds) => + AllConnectedClients.Where(i => !clientIds.Contains(i)).ToArray(); + + /// + /// Get the client's GUID from the player ID. + /// + /// The in-game player ID. + /// The client's NGO GUID. + public static ulong GetClientGuid(int playerId) => + StartOfRound.Instance.allPlayerScripts[playerId].actualClientId; + + /// + /// Get the client's player ID from the client's GUID. + /// + /// The client's NGO GUID. + /// The client's in-game player ID. + public static int GetPlayerId(ulong clientGuid) => + (int)StartOfRound.Instance.allPlayerScripts.First(player => player.actualClientId == clientGuid).playerClientId; internal static string GetModGuid(int frameIndex) { @@ -81,4 +91,7 @@ internal static string GetModGuid(int frameIndex) return MetadataHelper.GetMetadata(pluginType).GUID; } + + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void InvokeOnNetworkStartCallback() => OnNetworkStartCallback?.Invoke(IsHostOrServer); }