From 50d0008d03a3c547cef67bd6e16cb8a6c9581cce Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Mon, 22 Jan 2024 04:45:32 -0500 Subject: [PATCH] feat(NetworkClient): Add ReplaceHandler with channel param (#3747) * breaking(NetworkClient): Remove NetworkConnection parameter from ReplaceHandler There is only one connection on client. Aligns with RegisterHandler that takes no NetworkConnection parameter. * feat(NetworkClient): Add ReplaceHandler with channel param --- Assets/Mirror/Core/NetworkClient.cs | 32 ++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/Assets/Mirror/Core/NetworkClient.cs b/Assets/Mirror/Core/NetworkClient.cs index c7699a059..e1362c1a5 100644 --- a/Assets/Mirror/Core/NetworkClient.cs +++ b/Assets/Mirror/Core/NetworkClient.cs @@ -552,14 +552,17 @@ public static void RegisterHandler(Action handler, bool requireAuthen handlers[msgType] = NetworkMessages.WrapHandler((Action)HandlerWrapped, requireAuthentication, exceptionsDisconnect); } - /// Replace a handler for a particular message type. Should require authentication by default. - // RegisterHandler throws a warning (as it should) if a handler is assigned twice - // Use of ReplaceHandler makes it clear the user intended to replace the handler + // Deprecated 2024-01-21 + [Obsolete("Use ReplaceHandler without the NetworkConnection parameter instead. This version is obsolete and will be removed soon.")] public static void ReplaceHandler(Action handler, bool requireAuthentication = true) where T : struct, NetworkMessage { + // we use the same WrapHandler function for server and client. + // so let's wrap it to ignore the NetworkConnection parameter. + // it's not needed on client. it's always NetworkClient.connection. ushort msgType = NetworkMessageId.Id; - handlers[msgType] = NetworkMessages.WrapHandler(handler, requireAuthentication, exceptionsDisconnect); + void HandlerWrapped(NetworkConnection _, T value) => handler(_, value); + handlers[msgType] = NetworkMessages.WrapHandler((Action)HandlerWrapped, requireAuthentication, exceptionsDisconnect); } /// Replace a handler for a particular message type. Should require authentication by default. @@ -568,7 +571,26 @@ public static void ReplaceHandler(Action handler, bool public static void ReplaceHandler(Action handler, bool requireAuthentication = true) where T : struct, NetworkMessage { - ReplaceHandler((NetworkConnection _, T value) => { handler(value); }, requireAuthentication); + // we use the same WrapHandler function for server and client. + // so let's wrap it to ignore the NetworkConnection parameter. + // it's not needed on client. it's always NetworkClient.connection. + ushort msgType = NetworkMessageId.Id; + void HandlerWrapped(NetworkConnection _, T value) => handler(value); + handlers[msgType] = NetworkMessages.WrapHandler((Action)HandlerWrapped, requireAuthentication, exceptionsDisconnect); + } + + /// Replace a handler for a particular message type. Should require authentication by default. This version passes channelId to the handler. + // RegisterHandler throws a warning (as it should) if a handler is assigned twice + // Use of ReplaceHandler makes it clear the user intended to replace the handler + public static void ReplaceHandler(Action handler, bool requireAuthentication = true) + where T : struct, NetworkMessage + { + // we use the same WrapHandler function for server and client. + // so let's wrap it to ignore the NetworkConnection parameter. + // it's not needed on client. it's always NetworkClient.connection. + ushort msgType = NetworkMessageId.Id; + void HandlerWrapped(NetworkConnection _, T value, int channelId) => handler(value, channelId); + handlers[msgType] = NetworkMessages.WrapHandler((Action)HandlerWrapped, requireAuthentication, exceptionsDisconnect); } /// Unregister a message handler of type T.