diff --git a/LethalNetworkAPI/Event/LNetworkEvent.cs b/LethalNetworkAPI/Event/LNetworkEvent.cs
index 831cbd8..393716d 100644
--- a/LethalNetworkAPI/Event/LNetworkEvent.cs
+++ b/LethalNetworkAPI/Event/LNetworkEvent.cs
@@ -1,6 +1,6 @@
namespace LethalNetworkAPI;
///
-/// Internal Class
+/// Internal class.
///
-public abstract class LNetworkEvent(string identifier) : LethalNetwork($"evt.{identifier}");
\ No newline at end of file
+public abstract class LNetworkEvent(string identifier) : LethalNetwork($"evt.{identifier}", "Event");
\ No newline at end of file
diff --git a/LethalNetworkAPI/Event/LethalClientEvent.cs b/LethalNetworkAPI/Event/LethalClientEvent.cs
index 72f30cb..5aabf3b 100644
--- a/LethalNetworkAPI/Event/LethalClientEvent.cs
+++ b/LethalNetworkAPI/Event/LethalClientEvent.cs
@@ -2,16 +2,22 @@
namespace LethalNetworkAPI;
-public class LethalClientEvent : LNetworkEvent
+public sealed class LethalClientEvent : LNetworkEvent
{
#region Public Constructors
///
/// Create a new network event for clients.
///
+ ///
/// () An identifier for the event.
- /// Opt. (Action) The method to run when an event is received from the server.
- /// Opt. (Action<ulong>) The method to run when an event is received from another client.
+ ///
+ /// Opt. (Action)
+ /// The method to run when an event is received from the server.
+ ///
+ /// Opt. (Action<ulong>)
+ /// The method to run when an event is received from another client.
+ ///
/// Identifiers are specific to a per-mod basis.
public LethalClientEvent(string identifier,
Action? onReceived = null,
@@ -25,7 +31,8 @@ public LethalClientEvent(string identifier,
OnReceivedFromClient += onReceivedFromClient;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"NetworkEvent with identifier \"{Identifier}\" has been created.");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"NetworkEvent with identifier \"{Identifier}\" has been created.");
#endif
}
@@ -45,9 +52,16 @@ public void InvokeServer()
///
/// Invoke event to all clients.
///
- /// Opt. () If the local client event should be invoked.
- /// Opt. () If the local client should wait for a server response before invoking the event.
- /// will only be considered if is set to true.
+ ///
+ /// Opt. ()
+ /// If the local client event should be invoked.
+ ///
+ /// Opt. ()
+ /// If the local client should wait for a server response before
+ /// invoking the event.
+ ///
+ /// will only be considered
+ /// if is set to true.
public void InvokeAllClients(bool includeLocalClient = true, bool waitForServerResponse = false)
{
if (IsNetworkHandlerNull()) return;
@@ -59,7 +73,9 @@ public void InvokeAllClients(bool includeLocalClient = true, bool waitForServerR
OnReceivedFromClient?.Invoke(NetworkManager.Singleton.LocalClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to invoke Event to All Clients {includeLocalClient} with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to invoke Event to All Clients {includeLocalClient}" +
+ $" with identifier: {Identifier}");
#endif
}
@@ -78,7 +94,8 @@ public void InvokeAllClientsSynced()
ReceiveClientEvent(Identifier, NetworkManager.Singleton.LocalClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to invoke Synced Event to Other Clients with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to invoke Synced Event to Other Clients with identifier: {Identifier}");
#endif
}
@@ -91,6 +108,7 @@ public void InvokeAllClientsSynced()
///
/// The callback to invoke when an event is received from another client.
///
+ ///
/// The origin client ID.
public event Action? OnReceivedFromClient;
@@ -108,7 +126,8 @@ private void ReceiveClientEvent(string identifier, ulong originatorClientId)
OnReceivedFromClient?.Invoke(originatorClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received event with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received event with identifier: {Identifier}");
#endif
}
@@ -118,10 +137,12 @@ private void ReceiveSyncedClientEvent(string identifier, double time, ulong orig
var timeToWait = time - NetworkManager.Singleton.ServerTime.Time;
- NetworkHandler.Instance.StartCoroutine(WaitAndInvokeEvent((float)timeToWait, originatorClientId));
+ NetworkHandler.Instance.StartCoroutine(
+ WaitAndInvokeEvent((float)timeToWait, originatorClientId));
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received synced event with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received synced event with identifier: {Identifier}");
#endif
}
@@ -133,7 +154,8 @@ private IEnumerator WaitAndInvokeEvent(float timeToWait, ulong originatorClientI
ReceiveClientEvent(Identifier, originatorClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Invoked event with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Invoked event with identifier: {Identifier}");
#endif
}
diff --git a/LethalNetworkAPI/Event/LethalServerEvent.cs b/LethalNetworkAPI/Event/LethalServerEvent.cs
index c6e6caa..738cc79 100644
--- a/LethalNetworkAPI/Event/LethalServerEvent.cs
+++ b/LethalNetworkAPI/Event/LethalServerEvent.cs
@@ -1,18 +1,21 @@
using System.Collections;
using System.Collections.Generic;
-using Unity.Collections;
namespace LethalNetworkAPI;
-public class LethalServerEvent : LNetworkEvent
+public sealed class LethalServerEvent : LNetworkEvent
{
#region Public Constructors
///
/// Create a new network event for the server.
///
+ ///
/// () An identifier for the event.
- /// Opt. (Action<ulong>) The method to run when an event is received from a client.
+ ///
+ /// Opt. (Action<ulong>)
+ /// The method to run when an event is received from a client.
+ ///
/// Identifiers are specific to a per-mod basis.
public LethalServerEvent(string identifier, Action? onReceived = null) : base(identifier)
{
@@ -29,6 +32,7 @@ public LethalServerEvent(string identifier, Action? onReceived = null) :
///
/// Invoke event to a specific client.
///
+ ///
/// (ulong) The client to invoke the event to.
public void InvokeClient(ulong clientId)
{
@@ -38,14 +42,18 @@ public void InvokeClient(ulong clientId)
clientRpcParams: GenerateClientParams(clientId));
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to invoke Event to Client {clientId} with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to invoke Event to Client {clientId}" +
+ $" with identifier: {Identifier}");
#endif
}
///
/// Invoke event to specific clients.
///
- /// (IEnumerable<ulong>) The clients to invoke the event to.
+ ///
+ /// (IEnumerable<ulong>)
+ /// The clients to invoke the event to.
public void InvokeClients(IEnumerable clientIds)
{
if (IsNetworkHandlerNull() || !IsHostOrServer()) return;
@@ -54,14 +62,18 @@ public void InvokeClients(IEnumerable clientIds)
clientRpcParams: GenerateClientParams(clientIds));
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to invoke Event to Clients {clientIds} with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to invoke Event to Clients {clientIds}" +
+ $" with identifier: {Identifier}");
#endif
}
///
/// Invoke event to all clients.
///
- /// Opt. () Whether the host client should receive as well.
+ ///
+ /// Opt. ()
+ /// Whether the host client should receive as well.
public void InvokeAllClients(bool receiveOnHost = true)
{
if (IsNetworkHandlerNull() || !IsHostOrServer()) return;
@@ -73,7 +85,9 @@ public void InvokeAllClients(bool receiveOnHost = true)
clientRpcParams: GenerateClientParamsExceptHost());
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to invoke Event to All Clients {receiveOnHost} with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to invoke Event to All Clients {receiveOnHost}" +
+ $" with identifier: {Identifier}");
#endif
}
@@ -81,6 +95,7 @@ public void InvokeAllClients(bool receiveOnHost = true)
///
/// The callback to invoke when an event is received by the server.
///
+ ///
/// The origin client ID.
public event Action? OnReceived;
@@ -95,7 +110,8 @@ private void ReceiveServerEvent(string identifier, ulong originClientId)
OnReceived?.Invoke(originClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received event with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received event with identifier: {Identifier}");
#endif
}
@@ -111,7 +127,8 @@ private void ReceiveSyncedServerEvent(string identifier, double time, ulong orig
NetworkHandler.Instance.StartCoroutine(WaitAndInvokeEvent((float)timeToWait, originatorClientId));
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received synced event with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received synced event with identifier: {Identifier}");
#endif
}
@@ -123,7 +140,8 @@ private IEnumerator WaitAndInvokeEvent(float timeToWait, ulong clientId)
OnReceived?.Invoke(clientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Invoked event with identifier: {Identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Invoked event with identifier: {Identifier}");
#endif
}
diff --git a/LethalNetworkAPI/LethalNetwork.cs b/LethalNetworkAPI/LethalNetwork.cs
index 4bfe709..6ba73fe 100644
--- a/LethalNetworkAPI/LethalNetwork.cs
+++ b/LethalNetworkAPI/LethalNetwork.cs
@@ -7,9 +7,16 @@
namespace LethalNetworkAPI;
+///
+/// Internal class.
+///
public abstract class LethalNetwork
{
- protected LethalNetwork(string identifier, int frameIndex = 3)
+
+ internal readonly string Identifier = null!;
+ private readonly string _networkType = null!;
+
+ protected LethalNetwork(string identifier, string networkType, int frameIndex = 3)
{
try
{
@@ -18,14 +25,15 @@ protected LethalNetwork(string identifier, int frameIndex = 3)
var pluginType = AccessTools.GetTypesFromAssembly(assembly).First(type => type.GetCustomAttributes(typeof(BepInPlugin), false).Any());
Identifier = $"{MetadataHelper.GetMetadata(pluginType).GUID}.{identifier}";
+ _networkType = networkType;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"LethalNetwork with identifier \"{Identifier}\" has been created.");
+ LethalNetworkAPIPlugin.Logger.LogDebug($"LethalNetwork {_networkType} with identifier \"{Identifier}\" has been created.");
#endif
}
catch (Exception e)
{
- LethalNetworkAPIPlugin.Logger.LogError($"Unable to find plugin info for calling mod with identifier {identifier}. Are you using BepInEx? \n Stacktrace: {e}");
+ LethalNetworkAPIPlugin.Logger.LogError(string.Format(TextDefinitions.UnableToFindGuid, _networkType.ToLower(), Identifier, e));
}
}
@@ -37,7 +45,7 @@ protected bool IsNetworkHandlerNull(bool log = true)
if (NetworkHandler.Instance != null) return false;
if (log) LethalNetworkAPIPlugin.Logger.LogError(string.Format(
- TextDefinitions.NotInLobbyEvent, Identifier));
+ TextDefinitions.NotInLobbyEvent, _networkType.ToLower(), Identifier));
return true;
}
@@ -45,11 +53,11 @@ protected bool IsNetworkHandlerNull(bool log = true)
protected bool IsHostOrServer(bool log = true)
{
if (NetworkManager.Singleton == null) return false;
-
if (NetworkManager.Singleton.IsHost || NetworkManager.Singleton.IsServer) return true;
if (log) LethalNetworkAPIPlugin.Logger.LogError(string.Format(
- TextDefinitions.NotServerInfo, NetworkManager.Singleton.LocalClientId, Identifier));
+ TextDefinitions.NotServerInfo, NetworkManager.Singleton.LocalClientId, _networkType, Identifier));
+
return false;
}
@@ -59,7 +67,8 @@ private bool DoClientsExist(IEnumerable clientIds, bool log = true)
if (clientIds.Any()) return true;
if (log) LethalNetworkAPIPlugin.Logger.LogError(string.Format(
- TextDefinitions.TargetClientsNotConnected, clientIds, Identifier));
+ TextDefinitions.TargetClientsNotConnected, clientIds, _networkType, Identifier));
+
return false;
}
@@ -103,6 +112,4 @@ private ClientRpcParams GenerateClientParams(IEnumerable clientIds, bool
protected ClientRpcParams GenerateClientParamsExceptHost() => GenerateClientParams([], true);
#endregion
-
- internal readonly string Identifier = null!;
}
\ No newline at end of file
diff --git a/LethalNetworkAPI/LethalNetworkExtensions.cs b/LethalNetworkAPI/LethalNetworkExtensions.cs
index b90f58d..3c1c6ab 100644
--- a/LethalNetworkAPI/LethalNetworkExtensions.cs
+++ b/LethalNetworkAPI/LethalNetworkExtensions.cs
@@ -1,5 +1,4 @@
using GameNetcodeStuff;
-using LethalNetworkAPI;
namespace LethalNetworkAPI;
@@ -64,12 +63,12 @@ public static ulong GetClientId(this PlayerControllerB player)
/// () The network variable.
/// The variable is set to only allow writing by the object's owner client. In order to sync on all clients, the host must also run this method on the same GameObject with the same identifier.
public static LethalNetworkVariable? GetNetworkVariable(this GameObject gameObject, string identifier, bool serverOwned = false) => gameObject.NetworkVariable(identifier, serverOwned);
-
- internal static LethalNetworkVariable? NetworkVariable(this GameObject gameObject, string identifier, bool serverOwned)
+
+ private static LethalNetworkVariable? NetworkVariable(this GameObject gameObject, string identifier, bool serverOwned)
{
if (gameObject.TryGetComponent(out NetworkObject networkObjectComp) == false)
{
- LethalNetworkAPIPlugin.Logger.LogError(TextDefinitions.UnableToLocateNetworkObjectComponent);
+ LethalNetworkAPIPlugin.Logger.LogError(string.Format(TextDefinitions.UnableToLocateNetworkObjectComponent, identifier));
return null;
}
@@ -81,7 +80,7 @@ public static ulong GetClientId(this PlayerControllerB player)
return networkVariable;
networkVariable = new LethalNetworkVariable($"{identifier}.{networkObjectComp.GlobalObjectIdHash}", networkObjectComp, serverOwned, 3);
- NetworkHandler.Instance!.ObjectNetworkVariableList.Add(networkVariable);
+ NetworkHandler.Instance.ObjectNetworkVariableList.Add(networkVariable);
return networkVariable;
}
diff --git a/LethalNetworkAPI/Message/LNetworkMessage.cs b/LethalNetworkAPI/Message/LNetworkMessage.cs
index f0e2aaf..b9f3d36 100644
--- a/LethalNetworkAPI/Message/LNetworkMessage.cs
+++ b/LethalNetworkAPI/Message/LNetworkMessage.cs
@@ -1,6 +1,6 @@
namespace LethalNetworkAPI;
///
-/// Internal Class
+/// Internal class.
///
-public abstract class LNetworkMessage(string identifier) : LethalNetwork($"msg.{identifier}");
\ No newline at end of file
+public abstract class LNetworkMessage(string identifier) : LethalNetwork($"msg.{identifier}", "Message");
\ No newline at end of file
diff --git a/LethalNetworkAPI/Message/LethalClientMessage.cs b/LethalNetworkAPI/Message/LethalClientMessage.cs
index 08f2765..c8e9ef8 100644
--- a/LethalNetworkAPI/Message/LethalClientMessage.cs
+++ b/LethalNetworkAPI/Message/LethalClientMessage.cs
@@ -2,17 +2,23 @@
namespace LethalNetworkAPI;
-/// The serializable data type of the message.
-public class LethalClientMessage : LNetworkMessage
+/// The serializable data type of the message.
+public sealed class LethalClientMessage : LNetworkMessage
{
#region Constructor
///
/// Create a new network message for clients.
///
+ ///
/// () An identifier for the variable.
- /// Opt. (Action<TData>) The method to run when a message is received from the server.
- /// Opt. (Action<TData, ulong>) The method to run when a message is received from another client.
+ ///
+ /// Opt. (Action<TData>)
+ /// The method to run when a message is received from the server.
+ ///
+ /// Opt. (Action<TData, ulong>)
+ /// The method to run when a message is received from another client.
+ ///
/// Identifiers are specific to a per-mod basis.
public LethalClientMessage(string identifier,
Action? onReceived = null,
@@ -42,10 +48,18 @@ public void SendServer(TData data)
///
/// Send data to the server/host.
///
+ ///
/// () The data to send.
- /// Opt. () If the local client event should be invoked.
- /// Opt. () If the local client should wait for a server response before invoking the event.
- /// will only be considered if is set to true.
+ ///
+ /// Opt. ()
+ /// If the local client event should be invoked.
+ ///
+ /// Opt. ()
+ /// If the local client should wait for a server response before
+ /// invoking the event.
+ ///
+ /// will only be considered
+ /// if is set to true.
public void SendAllClients(TData data, bool includeLocalClient = true, bool waitForServerResponse = false)
{
if (IsNetworkHandlerNull()) return;
@@ -57,7 +71,8 @@ public void SendAllClients(TData data, bool includeLocalClient = true, bool wait
OnReceivedFromClient?.Invoke(data, NetworkManager.Singleton.LocalClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to Send Message to Server with data: {data}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to Send Message to Server with data: {data}");
#endif
}
@@ -65,6 +80,7 @@ public void SendAllClients(TData data, bool includeLocalClient = true, bool wait
///
/// The callback to invoke when a message is received from the server.
///
+ ///
/// The received data.
public event Action? OnReceived;
@@ -72,7 +88,9 @@ public void SendAllClients(TData data, bool includeLocalClient = true, bool wait
///
/// The callback to invoke when a message is received from another client.
///
+ ///
/// The received data.
+ ///
/// The origin client ID.
public event Action? OnReceivedFromClient;
@@ -88,7 +106,8 @@ private void ReceiveMessage(string identifier, byte[] data, ulong originatorClie
OnReceivedFromClient?.Invoke(LethalNetworkSerializer.Deserialize(data), originatorClient);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received data: {data}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received data: {data}");
#endif
}
}
\ No newline at end of file
diff --git a/LethalNetworkAPI/Message/LethalServerMessage.cs b/LethalNetworkAPI/Message/LethalServerMessage.cs
index d7d7c5e..b018b0a 100644
--- a/LethalNetworkAPI/Message/LethalServerMessage.cs
+++ b/LethalNetworkAPI/Message/LethalServerMessage.cs
@@ -4,7 +4,7 @@
namespace LethalNetworkAPI;
-/// The serializable data type of the message.
+/// The serializable data type of the message.
public class LethalServerMessage : LNetworkMessage
{
#region Constructor
@@ -12,8 +12,12 @@ public class LethalServerMessage : LNetworkMessage
///
/// Create a new network message for the server.
///
+ ///
/// () An identifier for the variable.
- /// Opt. (Action<TData>) The method to run when a message is received from a client.
+ ///
+ /// Opt. (Action<TData>)
+ /// The method to run when a message is received from a client.
+ ///
/// Identifiers are specific to a per-mod basis.
public LethalServerMessage(string identifier, Action? onReceived = null) : base(identifier)
{
@@ -29,7 +33,9 @@ public LethalServerMessage(string identifier, Action? onReceived =
///
/// Send data to a specified client.
///
+ ///
/// () The data to send.
+ ///
/// (ulong) The client to send the data to.
public void SendClient(TData data, ulong clientId)
{
@@ -42,8 +48,11 @@ public void SendClient(TData data, ulong clientId)
///
/// Send data to the specified clients.
///
+ ///
/// () The data to send.
- /// (IEnumerable<ulong>) The clients to send the data to.
+ ///
+ /// (IEnumerable<ulong>)
+ /// The clients to send the data to.
public void SendClients(TData data, IEnumerable clientIds)
{
if (IsNetworkHandlerNull() || !IsHostOrServer()) return;
@@ -55,7 +64,9 @@ public void SendClients(TData data, IEnumerable clientIds)
///
/// Send data to all clients.
///
+ ///
/// () The data to send.
+ ///
/// Opt. () Whether the host client should receive as well.
public void SendAllClients(TData data, bool receiveOnHost = true)
{
@@ -68,7 +79,8 @@ public void SendAllClients(TData data, bool receiveOnHost = true)
clientRpcParams: GenerateClientParamsExceptHost());
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Attempted to Send Message to All Clients with data: {data}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Attempted to Send Message to All Clients with data: {data}");
#endif
}
@@ -76,7 +88,9 @@ public void SendAllClients(TData data, bool receiveOnHost = true)
///
/// The callback to invoke when a message is received.
///
+ /// s
/// The received data.
+ ///
/// The origin client ID.
public event Action? OnReceived;
@@ -89,7 +103,8 @@ private void ReceiveServerMessage(string identifier, byte[] data, ulong originCl
OnReceived?.Invoke(LethalNetworkSerializer.Deserialize(data), originClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received data: {data}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received data: {data}");
#endif
}
}
\ No newline at end of file
diff --git a/LethalNetworkAPI/Networking/NetworkHandler.cs b/LethalNetworkAPI/Networking/NetworkHandler.cs
index 99bef1f..1b80d11 100644
--- a/LethalNetworkAPI/Networking/NetworkHandler.cs
+++ b/LethalNetworkAPI/Networking/NetworkHandler.cs
@@ -1,9 +1,7 @@
-// ReSharper disable MemberCanBeMadeStatic.Global
-
using System.Collections.Generic;
-using HarmonyLib;
-using LethalNetworkAPI;
using Unity.Collections;
+// ReSharper disable MemberCanBeMadeStatic.Global
+// ReSharper disable MemberCanBeMadeStatic.Local
namespace LethalNetworkAPI.Networking;
@@ -40,22 +38,25 @@ public override void OnDestroy()
NetworkDespawn?.Invoke();
}
- private void OnClientConnectedCallback(ulong client)
- {
+ private void OnClientConnectedCallback(ulong client) =>
OnPlayerJoin?.Invoke(client);
- }
#region Messages
[ServerRpc(RequireOwnership = false)]
- internal void MessageServerRpc(string identifier, byte[] data, bool toOtherClients = false, bool sendToOriginator = false, ServerRpcParams serverRpcParams = default)
+ internal void MessageServerRpc(string identifier,
+ byte[] data,
+ bool toOtherClients = false,
+ bool sendToOriginator = false,
+ ServerRpcParams serverRpcParams = default)
{
if (!toOtherClients)
OnServerMessage?.Invoke(identifier, data, serverRpcParams.Receive.SenderClientId);
else if (!sendToOriginator)
{
var clientIds = new NativeArray(NetworkManager.Singleton.ConnectedClientsIds
- .Where(i => i != serverRpcParams.Receive.SenderClientId).ToArray(), Allocator.Persistent);
+ .Where(i => i != serverRpcParams.Receive.SenderClientId).ToArray(),
+ Allocator.Persistent);
if (!clientIds.Any()) return;
MessageClientRpc(identifier, data, serverRpcParams.Receive.SenderClientId,
@@ -63,13 +64,27 @@ internal void MessageServerRpc(string identifier, byte[] data, bool toOtherClien
}
else
MessageClientRpc(identifier, data, serverRpcParams.Receive.SenderClientId);
+
+#if DEBUG
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received event \"{identifier}\" from a client.");
+#endif
}
[ClientRpc]
- internal void MessageClientRpc(string identifier, byte[] data, ulong originatorClient = 99999, ClientRpcParams clientRpcParams = default)
+ internal void MessageClientRpc(string identifier,
+ byte[] data,
+ ulong originatorClient = 99999,
+ ClientRpcParams clientRpcParams = default)
{
OnClientMessage?.Invoke(identifier, data, originatorClient);
clientRpcParams.Send.TargetClientIdsNativeArray?.Dispose();
+
+#if DEBUG
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received message \"{identifier}\" from server with originator: " +
+ $"{(originatorClient == 99999 ? "server" : originatorClient)}");
+#endif
}
#endregion Messages
@@ -77,14 +92,18 @@ internal void MessageClientRpc(string identifier, byte[] data, ulong originatorC
#region Events
[ServerRpc(RequireOwnership = false)]
- internal void EventServerRpc(string identifier, bool toOtherClients = false, bool sendToOriginator = false, ServerRpcParams serverRpcParams = default)
+ internal void EventServerRpc(string identifier,
+ bool toOtherClients = false,
+ bool sendToOriginator = false,
+ ServerRpcParams serverRpcParams = default)
{
if (!toOtherClients)
OnServerEvent?.Invoke(identifier, serverRpcParams.Receive.SenderClientId);
else if (!sendToOriginator)
{
var clientIds = new NativeArray(NetworkManager.Singleton.ConnectedClientsIds
- .Where(i => i != serverRpcParams.Receive.SenderClientId).ToArray(), Allocator.Persistent);
+ .Where(i => i != serverRpcParams.Receive.SenderClientId).ToArray(),
+ Allocator.Persistent);
if (!clientIds.Any()) return;
EventClientRpc(identifier, serverRpcParams.Receive.SenderClientId,
@@ -92,40 +111,54 @@ internal void EventServerRpc(string identifier, bool toOtherClients = false, boo
}
else
EventClientRpc(identifier, serverRpcParams.Receive.SenderClientId);
+
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received server data: {identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received event \"{identifier}\" from a client.");
#endif
}
[ClientRpc]
- internal void EventClientRpc(string identifier, ulong originatorId = 99999, ClientRpcParams clientRpcParams = default)
+ internal void EventClientRpc(string identifier,
+ ulong originatorClient = 99999,
+ ClientRpcParams clientRpcParams = default)
{
- OnClientEvent?.Invoke(identifier, originatorId);
+ OnClientEvent?.Invoke(identifier, originatorClient);
clientRpcParams.Send.TargetClientIdsNativeArray?.Dispose();
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received event with identifier: {identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received event \"{identifier}\" from server with originator: " +
+ $"{(originatorClient == 99999 ? "server" : originatorClient)}");
#endif
}
[ServerRpc(RequireOwnership = false)]
- internal void SyncedEventServerRpc(string identifier, double time, ServerRpcParams serverRpcParams = default)
+ internal void SyncedEventServerRpc(string identifier,
+ double time,
+ ServerRpcParams serverRpcParams = default)
{
OnSyncedServerEvent?.Invoke(identifier, time, serverRpcParams.Receive.SenderClientId);
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received server data: {identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received synced event \"{identifier}\" from a client.");
#endif
}
[ClientRpc]
- internal void SyncedEventClientRpc(string identifier, double time, ulong originatorClient, ClientRpcParams clientRpcParams = default)
+ internal void SyncedEventClientRpc(string identifier,
+ double time,
+ ulong originatorClient,
+ ClientRpcParams clientRpcParams = default)
{
OnSyncedClientEvent?.Invoke(identifier, time, originatorClient);
clientRpcParams.Send.TargetClientIdsNativeArray?.Dispose();
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received event with identifier: {identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received synced event \"{identifier}\" from server with originator: " +
+ $"{(originatorClient == 99999 ? "server" : originatorClient)}");
#endif
}
@@ -134,36 +167,53 @@ internal void SyncedEventClientRpc(string identifier, double time, ulong origina
#region Variables
[ServerRpc(RequireOwnership = false)]
- internal void UpdateVariableServerRpc(string identifier, byte[] data, ServerRpcParams serverRpcParams = default)
+ internal void UpdateVariableServerRpc(string identifier,
+ byte[] data,
+ ServerRpcParams serverRpcParams = default)
{
- if (serverRpcParams.Receive.SenderClientId != NetworkManager.ServerClientId) OnVariableUpdate?.Invoke(identifier, data);
+ if (serverRpcParams.Receive.SenderClientId != NetworkManager.ServerClientId)
+ OnVariableUpdate?.Invoke(identifier, data);
- var clientIds = new NativeArray(NetworkManager.Singleton.ConnectedClientsIds.Where(i => i != serverRpcParams.Receive.SenderClientId).ToArray(), Allocator.Persistent);
+ var clientIds = new NativeArray(NetworkManager.Singleton.ConnectedClientsIds
+ .Where(i => i != serverRpcParams.Receive.SenderClientId).ToArray(),
+ Allocator.Persistent);
if (!clientIds.Any()) return;
- UpdateVariableClientRpc(identifier, data, new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIdsNativeArray = clientIds } });
+ UpdateVariableClientRpc(identifier, data,
+ new ClientRpcParams { Send = new ClientRpcSendParams { TargetClientIdsNativeArray = clientIds } });
+
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received variable with identifier: {identifier}; data: {data}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received variable with identifier \"{identifier}\" from a client.");
#endif
}
[ClientRpc]
- internal void UpdateVariableClientRpc(string identifier, byte[] data, ClientRpcParams clientRpcParams = default)
+ internal void UpdateVariableClientRpc(string identifier,
+ byte[] data,
+ ClientRpcParams clientRpcParams = default)
{
OnVariableUpdate?.Invoke(identifier, data);
clientRpcParams.Send.TargetClientIdsNativeArray?.Dispose();
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Received variable with identifier: {identifier}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Received variable with identifier \"{identifier}\" from the server.");
#endif
}
[ServerRpc(RequireOwnership = false)]
- internal void GetVariableValueServerRpc(string identifier, ServerRpcParams serverRpcParams = default)
+ internal void GetVariableValueServerRpc(string identifier,
+ ServerRpcParams serverRpcParams = default)
{
GetVariableValue?.Invoke(identifier, serverRpcParams.Receive.SenderClientId);
+
+#if DEBUG
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Requesting variable data with identifier \"{identifier}\" from the server.");
+#endif
}
#endregion
diff --git a/LethalNetworkAPI/TextDefinitions.cs b/LethalNetworkAPI/TextDefinitions.cs
index e6691f7..8a31e08 100644
--- a/LethalNetworkAPI/TextDefinitions.cs
+++ b/LethalNetworkAPI/TextDefinitions.cs
@@ -3,23 +3,26 @@ namespace LethalNetworkAPI;
internal static class TextDefinitions
{
internal const string NotInLobbyMessage =
- "Unable to send the message with identifier \"{0}\" and data {{{1}}}. Is the player in a lobby?";
+ "Unable to send the {0} with identifier \"{1}\" and data {{{2}}}. Is the player in a lobby?";
+
+ internal const string UnableToFindGuid =
+ "Unable to find plugin info for calling mod for {0} with identifier \"{1}\". Are you using BepInEx? \n Stacktrace: {2}";
internal const string NotServerInfo =
- "The client {0} cannot use server methods. Identifier: \"{1}\"";
+ "The client {0} cannot use server methods. {1} Identifier: \"{2}\"";
internal const string NotInLobbyEvent =
- "Unable to invoke the event with identifier \"{0}\". Is the player in a lobby?";
+ "Unable to invoke the {1} with identifier \"{0}\". Is the player in a lobby?";
internal const string NetworkHandlerDoesNotExist =
"The NetworkHandler does not exist. This shouldn't occur!";
internal const string TargetClientNotConnected =
- "The specified client {0} is not connected. Identifier: \"{1}\"";
+ "The specified client {0} is not connected. {1} Identifier: \"{2}\"";
internal const string TargetClientsNotConnected =
- "None of the specified clients {0} are connected. Identifier: \"{1}\"";
+ "None of the specified clients {0} are connected. {1} Identifier: \"{2}\"";
internal const string UnableToLocateNetworkObjectComponent =
- "Unable to find the network object component. Are you adding variable to a network object?";
+ "Unable to find the network object component. Are you adding variable \"{0}\" to a network object?";
}
\ No newline at end of file
diff --git a/LethalNetworkAPI/Variable/LethalNetworkVariable.cs b/LethalNetworkAPI/Variable/LethalNetworkVariable.cs
index 1660b62..18692bd 100644
--- a/LethalNetworkAPI/Variable/LethalNetworkVariable.cs
+++ b/LethalNetworkAPI/Variable/LethalNetworkVariable.cs
@@ -1,28 +1,33 @@
-using System.Diagnostics;
-using BepInEx;
-using HarmonyLib;
using LethalNetworkAPI.Serializable;
-using Unity.Collections;
namespace LethalNetworkAPI;
internal interface ILethalNetVar; // To allow lists of any variable type
/// The serializable data type of the message.
-public class LethalNetworkVariable : LethalNetwork, ILethalNetVar
+public sealed class LethalNetworkVariable : LethalNetwork, ILethalNetVar
{
#region Constructors
+ // ReSharper disable once UnusedMember.Global
///
/// Create a new server-owned network variable, unless otherwise specified with [PublicNetworkVariable].
///
+ ///
/// () An identifier for the variable.
+ ///
/// Identifiers are specific to a per-mod basis. MUST be used outside of patches.
public LethalNetworkVariable(string identifier) : this(identifier, null, true, 2) { }
- internal LethalNetworkVariable(string identifier, NetworkObject? owner, bool serverOwned, int frameIndex) : base(identifier, frameIndex + 1)
+ internal LethalNetworkVariable(string identifier,
+ NetworkObject? owner,
+ bool serverOwned,
+ int frameIndex)
+ : base(identifier,
+ "Variable",
+ frameIndex + 1)
{
- _ownerObject = (!serverOwned) ? owner : null;
+ _ownerObject = !serverOwned ? owner : null;
NetworkHandler.OnVariableUpdate += ReceiveUpdate;
NetworkHandler.NetworkTick += OnNetworkTick;
@@ -34,7 +39,8 @@ internal LethalNetworkVariable(string identifier, NetworkObject? owner, bool ser
NetworkManager.Singleton.OnServerStopped += ClearSubscriptions;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Adding PlayerJoin Listener on NetworkSpawn");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ "Adding PlayerJoin Listener on NetworkSpawn");
#endif
};
@@ -50,7 +56,9 @@ internal LethalNetworkVariable(string identifier, NetworkObject? owner, bool ser
clientRpcParams: GenerateClientParams(clientId));
};
- if (typeof(LethalNetworkVariable).GetCustomAttributes(typeof(PublicNetworkVariableAttribute), true).Any())
+ if (typeof(LethalNetworkVariable)
+ .GetCustomAttributes(typeof(PublicNetworkVariableAttribute), true)
+ .Any())
_public = true;
if (IsNetworkHandlerNull(false)) return;
@@ -60,7 +68,8 @@ internal LethalNetworkVariable(string identifier, NetworkObject? owner, bool ser
// Send variable data when a player joins (if variable is created during playtime (in lobby))
NetworkHandler.OnPlayerJoin += OnPlayerJoin;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Adding PlayerJoin Listener on Variable Creation");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ "Adding PlayerJoin Listener on Variable Creation");
#endif
}
else
@@ -94,7 +103,8 @@ _ownerObject is null ||
_isDirty = true;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"New Value: ({typeof(TData).FullName}) {_value}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"New Value: ({typeof(TData).FullName}) {_value}");
#endif
OnValueChanged?.Invoke(_value);
@@ -105,7 +115,9 @@ _ownerObject is null ||
///
/// The callback to invoke when the variable's value changes.
///
+ ///
/// The received data.
+ ///
/// Invoked when changed locally and on the network.
public event Action? OnValueChanged;
@@ -118,7 +130,8 @@ private void OnPlayerJoin(ulong clientId)
if (IsNetworkHandlerNull() || !IsHostOrServer()) return;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Player Joined! Sending {Identifier}'s value.");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"Player Joined! Sending {Identifier}'s value.");
#endif
NetworkHandler.Instance!.UpdateVariableClientRpc(Identifier,
@@ -132,7 +145,8 @@ private void ClearSubscriptions(bool something)
NetworkManager.Singleton.OnServerStopped -= ClearSubscriptions;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"Cleared Subscriptions!");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ "Cleared Subscriptions!");
#endif
}
@@ -141,7 +155,8 @@ private void SendUpdate()
if (IsNetworkHandlerNull()) return;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"New Value: ({typeof(TData).FullName}) {_value}; {LethalNetworkSerializer.Serialize(_value)}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"New Value: ({typeof(TData).FullName}) {_value}");
#endif
NetworkHandler.Instance!.UpdateVariableServerRpc(Identifier,
@@ -159,7 +174,8 @@ private void ReceiveUpdate(string identifier, byte[] data)
_value = newValue;
#if DEBUG
- LethalNetworkAPIPlugin.Logger.LogDebug($"New Value: ({typeof(TData).FullName}) {newValue}");
+ LethalNetworkAPIPlugin.Logger.LogDebug(
+ $"New Value: ({typeof(TData).FullName}) {newValue}");
#endif
OnValueChanged?.Invoke(newValue);