More events moved

This commit is contained in:
Evil Factory
2026-02-15 16:57:16 -03:00
parent a50dce8fc2
commit 13bfffa777
5 changed files with 123 additions and 20 deletions
@@ -339,8 +339,6 @@ namespace Barotrauma.Networking
SendConsoleMessage("Granted all permissions to " + newClient.Name + ".", newClient); 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); SendChatMessage($"ServerMessage.JoinedServer~[client]={ClientLogName(newClient)}", ChatMessageType.Server, changeType: PlayerConnectionChangeType.Joined);
ServerSettings.ServerDetailsChanged = true; ServerSettings.ServerDetailsChanged = true;
@@ -2303,7 +2301,6 @@ namespace Barotrauma.Networking
segmentTable.StartNewSegment(ServerNetSegment.ClientList); segmentTable.StartNewSegment(ServerNetSegment.ClientList);
outmsg.WriteUInt16(LastClientListUpdateID); outmsg.WriteUInt16(LastClientListUpdateID);
GameMain.LuaCs.Hook.Call("writeClientList", c, outmsg);
outmsg.WriteByte((byte)Team1Count); outmsg.WriteByte((byte)Team1Count);
outmsg.WriteByte((byte)Team2Count); outmsg.WriteByte((byte)Team2Count);
@@ -2329,13 +2326,6 @@ namespace Barotrauma.Networking
IsOwner = client.Connection == OwnerConnection, IsOwner = client.Connection == OwnerConnection,
IsDownloading = FileSender.ActiveTransfers.Any(t => t.Connection == client.Connection) IsDownloading = FileSender.ActiveTransfers.Any(t => t.Connection == client.Connection)
}; };
var result = GameMain.LuaCs.Hook.Call<TempClient?>("writeClientList.modifyTempClientData", c, client, tempClientData, outmsg);
if (result != null)
{
tempClientData = result.Value;
}
outmsg.WriteNetSerializableStruct(tempClientData); outmsg.WriteNetSerializableStruct(tempClientData);
outmsg.WritePadBits(); outmsg.WritePadBits();
@@ -3725,8 +3715,6 @@ namespace Barotrauma.Networking
{ {
if (client == null) return; if (client == null) return;
GameMain.LuaCs.Hook.Call("client.disconnected", client);
if (client.Character != null) if (client.Character != null)
{ {
client.Character.ClientDisconnected = true; 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}."); $"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<CharacterInfo> bots, CharacterTeamType teamID, bool isPvP) public void AssignBotJobs(List<CharacterInfo> bots, CharacterTeamType teamID, bool isPvP)
@@ -350,15 +350,11 @@ namespace Barotrauma.Items.Components
wire.RegisterSignal(signal, source: this); wire.RegisterSignal(signal, source: this);
#endif #endif
SendSignalIntoConnection(signal, recipient); 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) foreach (CircuitBoxConnection connection in CircuitBoxConnections)
{ {
connection.ReceiveSignal(signal); 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; enumeratingWires = false;
foreach (var removedWire in removedWires) foreach (var removedWire in removedWires)
@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Reflection; using System.Reflection;
using Barotrauma.Items.Components;
using Barotrauma.LuaCs.Data; using Barotrauma.LuaCs.Data;
using Barotrauma.Networking; using Barotrauma.Networking;
@@ -330,6 +331,26 @@ public interface IEventDrawUpdate : IEvent<IEventDrawUpdate>
} }
} }
interface IEventSignalReceived : IEvent<IEventSignalReceived>
{
void OnSignalReceived(Signal signal, Connection connection);
static IEventSignalReceived IEvent<IEventSignalReceived>.GetLuaRunner(IDictionary<string, LuaCsFunc> luaFunc)
=> new LuaWrapper(luaFunc);
public sealed class LuaWrapper : LuaWrapperBase, IEventSignalReceived
{
public LuaWrapper(IDictionary<string, LuaCsFunc> luaFuncs) : base(luaFuncs)
{
}
public void OnSignalReceived(Signal signal, Connection connection)
{
LuaFuncs[nameof(OnSignalReceived)](signal, connection);
}
}
}
#endregion #endregion
#region Networking #region Networking
@@ -342,7 +363,7 @@ public interface IEventClientRawNetMessageReceived : IEvent<IEventClientRawNetMe
} }
/// <summary> /// <summary>
/// Called when a client connects to the server and has loaded into the lobby. /// Called when a client connects to the server.
/// </summary> /// </summary>
interface IEventClientConnected : IEvent<IEventClientConnected> interface IEventClientConnected : IEvent<IEventClientConnected>
{ {
@@ -367,6 +388,57 @@ interface IEventClientConnected : IEvent<IEventClientConnected>
} }
} }
} }
/// <summary>
/// Called when a client disconnects from the server.
/// </summary>
interface IEventClientDisconnected : IEvent<IEventClientDisconnected>
{
/// <summary>
/// Called when a client connects to the server.
/// </summary>
/// <param name="client">The connecting client.</param>
void OnClientDisconnected(Client client);
static IEventClientDisconnected IEvent<IEventClientDisconnected>.GetLuaRunner(IDictionary<string, LuaCsFunc> luaFunc)
=> new LuaWrapper(luaFunc);
public sealed class LuaWrapper : LuaWrapperBase, IEventClientDisconnected
{
public LuaWrapper(IDictionary<string, LuaCsFunc> luaFuncs) : base(luaFuncs)
{
}
public void OnClientDisconnected(Client client)
{
LuaFuncs[nameof(OnClientDisconnected)](client);
}
}
}
interface IEventJobsAssigned : IEvent<IEventJobsAssigned>
{
/// <summary>
/// Called when a client connects to the server.
/// </summary>
/// <param name="client">The connecting client.</param>
void OnJobsAssigned(IReadOnlyList<Client> unassignedClients);
static IEventJobsAssigned IEvent<IEventJobsAssigned>.GetLuaRunner(IDictionary<string, LuaCsFunc> luaFunc)
=> new LuaWrapper(luaFunc);
public sealed class LuaWrapper : LuaWrapperBase, IEventJobsAssigned
{
public LuaWrapper(IDictionary<string, LuaCsFunc> luaFuncs) : base(luaFuncs)
{
}
public void OnJobsAssigned(IReadOnlyList<Client> unassignedClients)
{
LuaFuncs[nameof(OnJobsAssigned)](unassignedClients);
}
}
}
#endif #endif
#endregion #endregion
@@ -1,4 +1,5 @@
using Barotrauma.LuaCs; using Barotrauma.Items.Components;
using Barotrauma.LuaCs;
using Barotrauma.LuaCs.Events; using Barotrauma.LuaCs.Events;
using Barotrauma.Networking; using Barotrauma.Networking;
using HarmonyLib; using HarmonyLib;
@@ -126,6 +127,28 @@ internal class HarmonyEventPatchesService : IService
_eventService.PublishEvent<IEventClientRawNetMessageReceived>(x => x.OnReceivedClientNetMessage(inc, header, sender)); _eventService.PublishEvent<IEventClientRawNetMessageReceived>(x => x.OnReceivedClientNetMessage(inc, header, sender));
inc.BitPosition -= 8; // rewind so the game can read the message 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<IEventClientConnected>(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<IEventClientDisconnected>(x => x.OnClientDisconnected(client));
}
[HarmonyPatch(typeof(GameServer), nameof(GameServer.AssignJobs)), HarmonyPostfix]
public static void GameServer_AssignJobs_Post(List<Client> unassigned)
{
_eventService.PublishEvent<IEventJobsAssigned>(x => x.OnJobsAssigned(unassigned));
}
#endif #endif
[HarmonyPatch(typeof(Character), nameof(Character.Create), new[] { [HarmonyPatch(typeof(Character), nameof(Character.Create), new[] {
@@ -163,6 +186,26 @@ internal class HarmonyEventPatchesService : IService
_eventService.PublishEvent<IEventAfflictionUpdate>(x => x.OnAfflictionUpdate(__instance, characterHealth, targetLimb, deltaTime)); _eventService.PublishEvent<IEventAfflictionUpdate>(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<IEventSignalReceived>(x => x.OnSignalReceived(signal, recipient));
_eventService.Call("signalReceived." + recipient.Item.Prefab.Identifier, signal, recipient);
}
foreach (CircuitBoxConnection connection in __instance.CircuitBoxConnections)
{
_eventService.PublishEvent<IEventSignalReceived>(x => x.OnSignalReceived(signal, connection.Connection));
_eventService.Call("signalReceived." + connection.Connection.Item.Prefab.Identifier, signal, connection.Connection);
}
}
public void Dispose() public void Dispose()
{ {
IsDisposed = true; IsDisposed = true;
@@ -183,7 +183,13 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService
_eventService.RegisterLuaEventAlias<IEventRoundEnded>("roundEnd", nameof(IEventRoundEnded.OnRoundEnd)); _eventService.RegisterLuaEventAlias<IEventRoundEnded>("roundEnd", nameof(IEventRoundEnded.OnRoundEnd));
_eventService.RegisterLuaEventAlias<IEventMissionsEnded>("missionsEnded", nameof(IEventMissionsEnded.OnMissionsEnded)); _eventService.RegisterLuaEventAlias<IEventMissionsEnded>("missionsEnded", nameof(IEventMissionsEnded.OnMissionsEnded));
_eventService.RegisterLuaEventAlias<IEventSignalReceived>("signalReceived", nameof(IEventSignalReceived.OnSignalReceived));
#if SERVER
_eventService.RegisterLuaEventAlias<IEventClientConnected>("client.connected", nameof(IEventClientConnected.OnClientConnected));
_eventService.RegisterLuaEventAlias<IEventClientDisconnected>("client.disconnected", nameof(IEventClientDisconnected.OnClientDisconnected));
_eventService.RegisterLuaEventAlias<IEventJobsAssigned>("jobsAssigned", nameof(IEventJobsAssigned.OnJobsAssigned));
#endif
} }
private void SetupEnvironment(bool enableSandbox) private void SetupEnvironment(bool enableSandbox)