Skip to content

Commit

Permalink
Rearranged LNetworkUtils; Added OnNetworkStartCallback
Browse files Browse the repository at this point in the history
  • Loading branch information
Xilophor committed Sep 7, 2024
1 parent 801a973 commit a0ed7be
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
11 changes: 9 additions & 2 deletions LethalNetworkAPI/Patches/NetworkManagerPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace LethalNetworkAPI.Patches;
using Internal;
using Old.Networking;
using Unity.Netcode;
using Utils;

[HarmonyPatch(typeof(NetworkManager))]
[HarmonyPriority(Priority.HigherThanNormal)]
Expand All @@ -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();
}
Expand Down
45 changes: 29 additions & 16 deletions LethalNetworkAPI/Utils/LNetworkUtils.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
namespace LethalNetworkAPI.Utils;

using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Unity.Netcode;

public static class LNetworkUtils
{
/// <summary>
/// Get the client's GUID from the player ID.
/// Called when the local client establishes a connection to, or starts up, a server.
/// </summary>
/// <param name="playerId">The in-game player ID.</param>
/// <returns>The client's NGO GUID.</returns>
public static ulong GetClientGuid(int playerId) =>
StartOfRound.Instance.allPlayerScripts[playerId].actualClientId;

/// <summary>
/// Get the client's player ID from the client's GUID.
/// </summary>
/// <param name="clientGuid">The client's NGO GUID.</param>
/// <returns>The client's in-game player ID.</returns>
public static int GetPlayerId(ulong clientGuid) =>
(int)StartOfRound.Instance.allPlayerScripts.First(player => player.actualClientId == clientGuid).playerClientId;
/// <remarks>Called with <see cref="bool">bool</see> isServer.</remarks>
public static event Action<bool> OnNetworkStartCallback = delegate { };

/// <summary>
/// Whether the client is connected to a server.
Expand All @@ -32,7 +24,7 @@ public static int GetPlayerId(ulong clientGuid) =>
/// <summary>
/// Whether the client is the host or server.
/// </summary>
public static bool IsHostOrServer => IsConnected && (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer);
public static bool IsHostOrServer => NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer;

/// <summary>
/// All connected clients' GUIDs.
Expand Down Expand Up @@ -63,14 +55,32 @@ public static ulong[] AllConnectedClients
/// </summary>
/// <param name="clientId">The client to exclude.</param>
/// <remarks>This will be empty if not connected to a server.</remarks>
public static ulong[] AllConnectedClientsExcept(ulong clientId) => AllConnectedClients.Where(i => i != clientId).ToArray();
public static ulong[] AllConnectedClientsExcept(ulong clientId) =>
AllConnectedClients.Where(i => i != clientId).ToArray();

/// <summary>
/// All connected clients' GUIDs, except the specified client.
/// </summary>
/// <param name="clientIds">The clients to exclude.</param>
/// <remarks>This will be empty if not connected to a server.</remarks>
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();

/// <summary>
/// Get the client's GUID from the player ID.
/// </summary>
/// <param name="playerId">The in-game player ID.</param>
/// <returns>The client's NGO GUID.</returns>
public static ulong GetClientGuid(int playerId) =>
StartOfRound.Instance.allPlayerScripts[playerId].actualClientId;

/// <summary>
/// Get the client's player ID from the client's GUID.
/// </summary>
/// <param name="clientGuid">The client's NGO GUID.</param>
/// <returns>The client's in-game player ID.</returns>
public static int GetPlayerId(ulong clientGuid) =>
(int)StartOfRound.Instance.allPlayerScripts.First(player => player.actualClientId == clientGuid).playerClientId;

internal static string GetModGuid(int frameIndex)
{
Expand All @@ -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);
}

0 comments on commit a0ed7be

Please sign in to comment.