From 13bfffa777598172f82b7fbe3878fdc29ae4cf76 Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Sun, 15 Feb 2026 16:57:16 -0300 Subject: [PATCH] More events moved --- .../ServerSource/Networking/GameServer.cs | 14 ---- .../Items/Components/Signal/Connection.cs | 4 - .../SharedSource/LuaCs/IEvents.cs | 74 ++++++++++++++++++- .../_Services/HarmonyEventPatchesService.cs | 45 ++++++++++- .../_Services/LuaScriptManagementService.cs | 6 ++ 5 files changed, 123 insertions(+), 20 deletions(-) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 4365a0527..90e224ccc 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -339,8 +339,6 @@ namespace Barotrauma.Networking SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient); } - GameMain.LuaCs.Hook.Call("client.connected", newClient); - SendChatMessage($"ServerMessage.JoinedServer~[client]={ClientLogName(newClient)}", ChatMessageType.Server, changeType: PlayerConnectionChangeType.Joined); ServerSettings.ServerDetailsChanged = true; @@ -2303,7 +2301,6 @@ namespace Barotrauma.Networking segmentTable.StartNewSegment(ServerNetSegment.ClientList); outmsg.WriteUInt16(LastClientListUpdateID); - GameMain.LuaCs.Hook.Call("writeClientList", c, outmsg); outmsg.WriteByte((byte)Team1Count); outmsg.WriteByte((byte)Team2Count); @@ -2329,13 +2326,6 @@ namespace Barotrauma.Networking IsOwner = client.Connection == OwnerConnection, IsDownloading = FileSender.ActiveTransfers.Any(t => t.Connection == client.Connection) }; - - var result = GameMain.LuaCs.Hook.Call("writeClientList.modifyTempClientData", c, client, tempClientData, outmsg); - - if (result != null) - { - tempClientData = result.Value; - } outmsg.WriteNetSerializableStruct(tempClientData); outmsg.WritePadBits(); @@ -3725,8 +3715,6 @@ namespace Barotrauma.Networking { if (client == null) return; - GameMain.LuaCs.Hook.Call("client.disconnected", client); - if (client.Character != null) { client.Character.ClientDisconnected = true; @@ -4660,8 +4648,6 @@ namespace Barotrauma.Networking $"No suitable jobs available for {c.Name} (karma {c.Karma}). Assigning a random job: {c.AssignedJob.Prefab.Name}."); } } - - GameMain.LuaCs.Hook.Call("jobsAssigned", unassigned); } public void AssignBotJobs(List bots, CharacterTeamType teamID, bool isPvP) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs index e14929804..deabc428b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/Connection.cs @@ -350,15 +350,11 @@ namespace Barotrauma.Items.Components wire.RegisterSignal(signal, source: this); #endif SendSignalIntoConnection(signal, recipient); - GameMain.LuaCs.Hook.Call("signalReceived", signal, recipient); - GameMain.LuaCs.Hook.Call("signalReceived." + recipient.item.Prefab.Identifier, signal, recipient); } foreach (CircuitBoxConnection connection in CircuitBoxConnections) { connection.ReceiveSignal(signal); - GameMain.LuaCs.Hook.Call("signalReceived", signal, connection.Connection); - GameMain.LuaCs.Hook.Call("signalReceived." + connection.Connection.Item.Prefab.Identifier, signal, connection); } enumeratingWires = false; foreach (var removedWire in removedWires) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs index a392928f7..391ac742f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using Barotrauma.Items.Components; using Barotrauma.LuaCs.Data; using Barotrauma.Networking; @@ -330,6 +331,26 @@ public interface IEventDrawUpdate : IEvent } } +interface IEventSignalReceived : IEvent +{ + void OnSignalReceived(Signal signal, Connection connection); + + static IEventSignalReceived IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventSignalReceived + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnSignalReceived(Signal signal, Connection connection) + { + LuaFuncs[nameof(OnSignalReceived)](signal, connection); + } + } +} + #endregion #region Networking @@ -342,7 +363,7 @@ public interface IEventClientRawNetMessageReceived : IEvent -/// Called when a client connects to the server and has loaded into the lobby. +/// Called when a client connects to the server. /// interface IEventClientConnected : IEvent { @@ -367,6 +388,57 @@ interface IEventClientConnected : IEvent } } } + +/// +/// Called when a client disconnects from the server. +/// +interface IEventClientDisconnected : IEvent +{ + /// + /// Called when a client connects to the server. + /// + /// The connecting client. + void OnClientDisconnected(Client client); + + static IEventClientDisconnected IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventClientDisconnected + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnClientDisconnected(Client client) + { + LuaFuncs[nameof(OnClientDisconnected)](client); + } + } +} + +interface IEventJobsAssigned : IEvent +{ + /// + /// Called when a client connects to the server. + /// + /// The connecting client. + void OnJobsAssigned(IReadOnlyList unassignedClients); + + static IEventJobsAssigned IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventJobsAssigned + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnJobsAssigned(IReadOnlyList unassignedClients) + { + LuaFuncs[nameof(OnJobsAssigned)](unassignedClients); + } + } +} #endif #endregion diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs index 5054bc0ba..e3907ca72 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/HarmonyEventPatchesService.cs @@ -1,4 +1,5 @@ -using Barotrauma.LuaCs; +using Barotrauma.Items.Components; +using Barotrauma.LuaCs; using Barotrauma.LuaCs.Events; using Barotrauma.Networking; using HarmonyLib; @@ -126,6 +127,28 @@ internal class HarmonyEventPatchesService : IService _eventService.PublishEvent(x => x.OnReceivedClientNetMessage(inc, header, sender)); inc.BitPosition -= 8; // rewind so the game can read the message } + + [HarmonyPatch(typeof(GameServer), "OnInitializationComplete"), HarmonyPostfix] + public static void GameServer_OnInitializationComplete_Post(GameServer __instance) + { + Client client = __instance.ConnectedClients.LastOrDefault(); + if (client == null) { return; } + _eventService.PublishEvent(x => x.OnClientConnected(client)); + } + + [HarmonyPatch(typeof(GameServer), nameof(GameServer.DisconnectClient), new Type[] { typeof(Client), typeof(PeerDisconnectPacket) }), HarmonyPrefix] + public static void GameServer_DisconnectClient_Pre(Client client, PeerDisconnectPacket peerDisconnectPacket) + { + if (client == null) { return; } + + _eventService.PublishEvent(x => x.OnClientDisconnected(client)); + } + + [HarmonyPatch(typeof(GameServer), nameof(GameServer.AssignJobs)), HarmonyPostfix] + public static void GameServer_AssignJobs_Post(List unassigned) + { + _eventService.PublishEvent(x => x.OnJobsAssigned(unassigned)); + } #endif [HarmonyPatch(typeof(Character), nameof(Character.Create), new[] { @@ -163,6 +186,26 @@ internal class HarmonyEventPatchesService : IService _eventService.PublishEvent(x => x.OnAfflictionUpdate(__instance, characterHealth, targetLimb, deltaTime)); } + [HarmonyPatch(typeof(Connection), nameof(Connection.SendSignal)), HarmonyPostfix] + public static void Connection_SendSignal_Post(Connection __instance, Signal signal) + { + foreach (var wire in __instance.Wires) + { + Connection recipient = wire.OtherConnection(__instance); + if (recipient == null) { continue; } + if (recipient.Item == __instance.Item || signal.source?.LastSentSignalRecipients.LastOrDefault() == recipient) { continue; } + + _eventService.PublishEvent(x => x.OnSignalReceived(signal, recipient)); + _eventService.Call("signalReceived." + recipient.Item.Prefab.Identifier, signal, recipient); + } + + foreach (CircuitBoxConnection connection in __instance.CircuitBoxConnections) + { + _eventService.PublishEvent(x => x.OnSignalReceived(signal, connection.Connection)); + _eventService.Call("signalReceived." + connection.Connection.Item.Prefab.Identifier, signal, connection.Connection); + } + } + public void Dispose() { IsDisposed = true; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs index 13d339b1e..27ce50979 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs @@ -183,7 +183,13 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService _eventService.RegisterLuaEventAlias("roundEnd", nameof(IEventRoundEnded.OnRoundEnd)); _eventService.RegisterLuaEventAlias("missionsEnded", nameof(IEventMissionsEnded.OnMissionsEnded)); + _eventService.RegisterLuaEventAlias("signalReceived", nameof(IEventSignalReceived.OnSignalReceived)); +#if SERVER + _eventService.RegisterLuaEventAlias("client.connected", nameof(IEventClientConnected.OnClientConnected)); + _eventService.RegisterLuaEventAlias("client.disconnected", nameof(IEventClientDisconnected.OnClientDisconnected)); + _eventService.RegisterLuaEventAlias("jobsAssigned", nameof(IEventJobsAssigned.OnJobsAssigned)); +#endif } private void SetupEnvironment(bool enableSandbox)