From 12e05cb07eec1af6a77091016be524daf3fff1ff Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Sat, 18 Apr 2026 00:00:00 -0300 Subject: [PATCH] Fix LuaCs net messages received during connection initialization to be read incorrectly, happened because we would reset the BitPosition in our harmony patch which would cause the message to be read incorrectly later --- .../LuaCs/_Services/NetworkingService.cs | 6 ++-- .../LuaCs/_Services/NetworkingService.cs | 18 +++++------- .../SharedSource/LuaCs/IEvents.cs | 29 ++++++++++++++----- .../_Services/HarmonyEventPatchesService.cs | 25 +++++++++++++--- 4 files changed, 54 insertions(+), 24 deletions(-) diff --git a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/_Services/NetworkingService.cs b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/_Services/NetworkingService.cs index 4b7c5d771..fa3b2905e 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/LuaCs/_Services/NetworkingService.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/LuaCs/_Services/NetworkingService.cs @@ -32,11 +32,11 @@ partial class NetworkingService : INetworkingService, IEventServerConnected, IEv } } - public void OnReceivedServerNetMessage(IReadMessage netMessage, ServerPacketHeader serverPacketHeader) + public bool? OnReceivedServerNetMessage(IReadMessage netMessage, ServerPacketHeader serverPacketHeader) { if (serverPacketHeader != ServerHeader) { - return; + return null; } ServerToClient luaCsHeader = (ServerToClient)netMessage.ReadByte(); @@ -55,6 +55,8 @@ partial class NetworkingService : INetworkingService, IEventServerConnected, IEv ReadIds(netMessage); break; } + + return true; } private void SendSyncMessage() diff --git a/Barotrauma/BarotraumaServer/ServerSource/LuaCs/_Services/NetworkingService.cs b/Barotrauma/BarotraumaServer/ServerSource/LuaCs/_Services/NetworkingService.cs index d3cb28157..5bd32a8a1 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/LuaCs/_Services/NetworkingService.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/LuaCs/_Services/NetworkingService.cs @@ -35,11 +35,11 @@ partial class NetworkingService : INetworkingService, IEventClientRawNetMessageR return message; } - public void OnReceivedClientNetMessage(IReadMessage netMessage, ClientPacketHeader clientPacketHeader, NetworkConnection sender) + public bool? OnReceivedClientNetMessage(IReadMessage netMessage, ClientPacketHeader clientPacketHeader, NetworkConnection sender) { if (clientPacketHeader != ClientHeader) { - return; + return null; } Client client = GameMain.Server.ConnectedClients.First(c => c.Connection == sender); @@ -64,6 +64,8 @@ partial class NetworkingService : INetworkingService, IEventClientRawNetMessageR RequestIdSingle(netMessage, client); break; } + + return true; } private void HandleNetMessageId(IReadMessage netMessage, Client client = null) @@ -159,15 +161,11 @@ partial class NetworkingService : INetworkingService, IEventClientRawNetMessageR SendToClient(message, client.Connection, DeliveryMethod.Reliable); - // delay by 1s to allow above id sync to reach the client. - CoroutineManager.Invoke(() => + // TODO: when we move to using GUIDs for everything, this should combined into a single message + foreach (INetworkSyncVar netVar in netVars.Keys) { - // TODO: when we move to using GUIDs for everything, this should combined into a single message - foreach (INetworkSyncVar netVar in netVars.Keys) - { - SendNetVar(netVar, client.Connection); - } - },1f); + SendNetVar(netVar, client.Connection); + } } public void SendToClient(IWriteMessage netMessage, NetworkConnection connection = null, DeliveryMethod deliveryMethod = DeliveryMethod.Reliable) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs index ca0e9498c..ca52dab77 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs @@ -959,7 +959,7 @@ interface IEventInventoryItemSwap : IEvent #if SERVER public interface IEventClientRawNetMessageReceived : IEvent { - void OnReceivedClientNetMessage(IReadMessage netMessage, ClientPacketHeader clientPacketHeader, NetworkConnection sender); + bool? OnReceivedClientNetMessage(IReadMessage netMessage, ClientPacketHeader clientPacketHeader, NetworkConnection sender); static IEventClientRawNetMessageReceived IEvent.GetLuaRunner(IDictionary luaFunc) => new LuaWrapper(luaFunc); @@ -970,15 +970,22 @@ public interface IEventClientRawNetMessageReceived : IEvent c.Connection == sender); - if (client == null) { return; } + if (client == null) { return null; } - LuaFuncs[nameof(OnReceivedClientNetMessage)](netMessage, clientPacketHeader, client); + var result = LuaFuncs[nameof(OnReceivedClientNetMessage)](netMessage, clientPacketHeader, client); + + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) + { + return dynValue.Boolean; + } + + return null; } } } @@ -1069,7 +1076,7 @@ interface IEventJobsAssigned : IEvent public interface IEventServerRawNetMessageReceived : IEvent { - void OnReceivedServerNetMessage(IReadMessage netMessage, ServerPacketHeader serverPacketHeader); + bool? OnReceivedServerNetMessage(IReadMessage netMessage, ServerPacketHeader serverPacketHeader); static IEventServerRawNetMessageReceived IEvent.GetLuaRunner(IDictionary luaFunc) => new LuaWrapper(luaFunc); @@ -1080,9 +1087,15 @@ public interface IEventServerRawNetMessageReceived : IEvent(x => x.OnReceivedServerNetMessage(inc, header)); + bool? skip = null; + _eventService.PublishEvent(x => skip = x.OnReceivedServerNetMessage(inc, header) ?? skip); + + if (skip == true) + { + return false; + } + inc.BitPosition = prevBitPosition; // rewind so the game can read the message + return true; } [HarmonyPatch(typeof(SubEditorScreen), nameof(SubEditorScreen.Select), new Type[] { }), HarmonyPostfix] @@ -177,12 +185,21 @@ internal class HarmonyEventPatchesService : ISystem #elif SERVER [HarmonyPatch(typeof(GameServer), "ReadDataMessage"), HarmonyPrefix] - public static void GameServer_ReadDataMessage_Pre(NetworkConnection sender, IReadMessage inc) + public static bool GameServer_ReadDataMessage_Pre(NetworkConnection sender, IReadMessage inc) { int prevBitPosition = inc.BitPosition; ClientPacketHeader header = (ClientPacketHeader)inc.ReadByte(); - _eventService.PublishEvent(x => x.OnReceivedClientNetMessage(inc, header, sender)); + + bool? skip = null; + _eventService.PublishEvent(x => skip = x.OnReceivedClientNetMessage(inc, header, sender) ?? skip); + + if (skip == true) + { + return false; + } + inc.BitPosition = prevBitPosition; // rewind so the game can read the message + return true; } [HarmonyPatch(typeof(GameServer), "OnInitializationComplete"), HarmonyPostfix]