diff --git a/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs b/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs index deb4201bf..a8e5e33bf 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/GUI/ChatBox.cs @@ -1,5 +1,6 @@ using Barotrauma.Extensions; using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Events; using Barotrauma.Networking; using Microsoft.Xna.Framework; using System; @@ -413,7 +414,8 @@ namespace Barotrauma { if (GameMain.IsSingleplayer) { - var should = GameMain.LuaCs.Hook.Call("chatMessage", message.Text, message.SenderClient, message.Type, message); + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChatMessage(message.Text, message.SenderClient, message.Type, message) ?? should); if (should != null && should.Value) { return; } } diff --git a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs index 98d00c624..3111861d4 100644 --- a/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/ClientSource/Networking/GameClient.cs @@ -3003,7 +3003,8 @@ namespace Barotrauma.Networking public override void AddChatMessage(ChatMessage message) { - var should = GameMain.LuaCs.Hook.Call("chatMessage", message.Text, message.SenderClient, message.Type, message); + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChatMessage(message.Text, message.SenderClient, message.Type, message) ?? should); if (should != null && should.Value) { return; } if (string.IsNullOrEmpty(message.Text)) { return; } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs b/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs index 560dd223e..8aa2c1809 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Characters/CharacterNetworking.cs @@ -320,11 +320,7 @@ namespace Barotrauma if (TalentTree.IsViableTalentForCharacter(this, prefab.Identifier, talentSelection)) { - bool? should = GameMain.LuaCs.Hook.Call("character.updateTalent", this, prefab, c); - if (should == null) - { - GiveTalent(prefab.Identifier); - } + GiveTalent(prefab.Identifier); talentSelection.Add(prefab.Identifier); } } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs index c76385bf7..83898e882 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs @@ -1,6 +1,8 @@ -using System; -using System.Text; +using Barotrauma.LuaCs.Events; using MoonSharp.Interpreter; +using MoonSharp.VsCodeDebugger.SDK; +using System; +using System.Text; namespace Barotrauma.Networking { @@ -86,12 +88,9 @@ namespace Barotrauma.Networking HandleSpamFilter(c, txt, out bool flaggedAsSpam, similarityMultiplier); if (flaggedAsSpam) { return; } - var should = GameMain.LuaCs.Hook.Call("chatMessage", txt, c, type); - - if (should != null && should.Value) - { - return; - } + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChatMessage(txt, c, type, ChatMessage.Create(c.Name, txt, type, null, c)) ?? should); + if (should != null && should.Value) { return; } if (type == ChatMessageType.Order) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index 90e224ccc..89a92c077 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -3511,7 +3511,8 @@ namespace Barotrauma.Networking return false; } - var result = GameMain.LuaCs.Hook.Call("tryChangeClientName", c, newName, newJob, newTeam); + bool? result = null; + GameMain.LuaCs.EventService.PublishEvent(x => result = x.OnTryClienChangeName(c, newName, newJob, newTeam) ?? result); if (result != null) { @@ -3968,15 +3969,7 @@ namespace Barotrauma.Networking //send to chat-linked wifi components Signal s = new Signal(message, sender: senderCharacter, source: senderRadio.Item); senderRadio.TransmitSignal(s, sentFromChat: true); - } - - var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType); - - var should = GameMain.LuaCs.Hook.Call("modifyChatMessage", hookChatMsg, senderRadio); - - if (should != null && should.Value) - return; - + } //check which clients can receive the message and apply distance effects foreach (Client client in ConnectedClients) @@ -4778,7 +4771,7 @@ namespace Barotrauma.Networking { if (GameMain.Server == null || !GameMain.Server.ServerSettings.SaveServerLogs) { return; } - GameMain.LuaCs?.Hook?.Call("serverLog", line, messageType); + GameMain.LuaCs?.EventService.PublishEvent(x => x.OnServerLog(line, messageType)); GameMain.Server.ServerSettings.ServerLog.WriteLine(line, messageType); diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs index e5cf2c975..b9ea8b6eb 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/LidgrenServerPeer.cs @@ -187,16 +187,7 @@ namespace Barotrauma.Networking { if (netServer == null) { return; } - var skipDeny = false; - { - var result = GameMain.LuaCs.Hook.Call("lidgren.handleConnection", inc); - if (result != null) { - if (result.Value) skipDeny = true; - else return; - } - } - - if (!skipDeny && connectedClients.Count >= serverSettings.MaxPlayers) + if (connectedClients.Count >= serverSettings.MaxPlayers) { inc.SenderConnection.Deny(PeerDisconnectPacket.WithReason(DisconnectReason.ServerFull).ToLidgrenStringRepresentation()); return; diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs index 3e1a11c33..0003f1ef8 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Primitives/Peers/Server/ServerPeer.cs @@ -257,12 +257,7 @@ namespace Barotrauma.Networking protected void UpdatePendingClient(PendingClient pendingClient) { - var skipRemove = false; - var result = GameMain.LuaCs.Hook.Call("handlePendingClient", pendingClient); - - if (result != null) skipRemove = result.Value; - - if (!skipRemove && connectedClients.Count >= serverSettings.MaxPlayers) + if (connectedClients.Count >= serverSettings.MaxPlayers) { RemovePendingClient(pendingClient, PeerDisconnectPacket.WithReason(DisconnectReason.ServerFull)); } diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs index 5349d2ea4..a28f2701b 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs @@ -1,7 +1,10 @@ using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Events; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; +using static Barotrauma.CharacterHealth; +using static Barotrauma.MedicalClinic; namespace Barotrauma.Networking { @@ -96,7 +99,8 @@ namespace Barotrauma.Networking ChatMessage.CanUseRadio(sender.Character, out WifiComponent senderRadio) && (recipientSpectating || ChatMessage.CanUseRadio(recipient.Character, out recipientRadio))) { - var canUse = GameMain.LuaCs.Hook.Call("canUseVoiceRadio", new object[] { sender, recipient }); + bool? canUse = null; + GameMain.LuaCs.EventService.PublishEvent(x => canUse = x.OnCanUseVoiceRadio(sender, recipient) ?? canUse); if (canUse != null) { @@ -116,7 +120,8 @@ namespace Barotrauma.Networking } } - float range = GameMain.LuaCs.Hook.Call("changeLocalVoiceRange", sender, recipient) ?? 1.0f; + float range = 1.0f; + GameMain.LuaCs.EventService.PublishEvent(x => range = x.OnChangeLocalVoiceRange(sender, recipient) ?? range); if (recipientSpectating) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs index fb8754e55..3c64fb44c 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/HumanoidAnimController.cs @@ -1,5 +1,6 @@ using Barotrauma.Extensions; using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Events; using Barotrauma.Networking; using FarseerPhysics; using Microsoft.Xna.Framework; @@ -1305,11 +1306,11 @@ namespace Barotrauma //increase oxygen and clamp it above zero // -> the character should be revived if there are no major afflictions in addition to lack of oxygen target.Oxygen = Math.Max(target.Oxygen + 10.0f, 10.0f); - GameMain.LuaCs.Hook.Call("human.CPRSuccess", this); + GameMain.LuaCs.EventService.PublishEvent(x => x.OnCharacterCPRSuccess(this)); } else { - GameMain.LuaCs.Hook.Call("human.CPRFailed", this); + GameMain.LuaCs.EventService.PublishEvent(x => x.OnCharacterCPRFailed(this)); } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs index 5b7d36724..7517e00f4 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs @@ -1,17 +1,18 @@ -using Barotrauma.Networking; +using Barotrauma.Extensions; +using Barotrauma.LuaCs.Events; +using Barotrauma.Networking; using FarseerPhysics; using FarseerPhysics.Dynamics; using FarseerPhysics.Dynamics.Contacts; using FarseerPhysics.Dynamics.Joints; using Microsoft.Xna.Framework; +using MoonSharp.Interpreter; using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; -using Barotrauma.Extensions; -using LimbParams = Barotrauma.RagdollParams.LimbParams; using JointParams = Barotrauma.RagdollParams.JointParams; -using MoonSharp.Interpreter; +using LimbParams = Barotrauma.RagdollParams.LimbParams; namespace Barotrauma { @@ -857,7 +858,8 @@ namespace Barotrauma float impactDamage = GetImpactDamage(impact, impactTolerance); - var should = GameMain.LuaCs.Hook.Call("changeFallDamage", impactDamage, character, impactPos, velocity); + float? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnChangeFallDamage(impactDamage, character, impactPos, velocity) ?? should); if (should != null) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs index 31abf6789..7974edd57 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/Afflictions/AfflictionHusk.cs @@ -4,6 +4,7 @@ using System.Xml.Linq; using System; using Barotrauma.Extensions; using Microsoft.Xna.Framework; +using Barotrauma.LuaCs.Events; namespace Barotrauma { @@ -343,7 +344,7 @@ namespace Barotrauma if (client != null) { GameMain.Server.SetClientCharacter(client, husk); - GameMain.LuaCs.Hook.Call("husk.clientControlHusk", new object[] { client, husk }); + GameMain.LuaCs.EventService.PublishEvent(x => x.OnClientControlHusk(client, husk)); } #else if (!character.IsRemotelyControlled && character == Character.Controlled) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs index 2c2fcad91..2ddc7d8f9 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs @@ -1,17 +1,19 @@ using Barotrauma.Abilities; +using Barotrauma.Abilities; using Barotrauma.Extensions; +using Barotrauma.Extensions; +using Barotrauma.LuaCs.Events; +using Barotrauma.Networking; using Barotrauma.Networking; using Microsoft.Xna.Framework; +using MoonSharp.Interpreter; using System; using System.Collections.Generic; using System.Globalization; +using System.Globalization; using System.Linq; using System.Xml.Linq; -using Barotrauma.Networking; -using Barotrauma.Extensions; -using System.Globalization; -using MoonSharp.Interpreter; -using Barotrauma.Abilities; +using static OneOf.Types.TrueFalseOrNull; namespace Barotrauma { @@ -655,7 +657,8 @@ namespace Barotrauma return; } - var should = GameMain.LuaCs.Hook.Call("character.applyDamage", this, attackResult, hitLimb, allowStacking); + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnCharacterApplyDamage(this, attackResult, hitLimb, allowStacking) ?? should); if (should != null && should.Value) { return; } foreach (Affliction newAffliction in attackResult.Afflictions) @@ -826,10 +829,9 @@ namespace Barotrauma if (newAffliction.Prefab.TargetSpecies.Any() && newAffliction.Prefab.TargetSpecies.None(s => s == Character.SpeciesName)) { return; } if (Character.Params.Health.ImmunityIdentifiers.Contains(newAffliction.Identifier)) { return; } - var should = GameMain.LuaCs.Hook.Call("character.applyAffliction", this, limbHealth, newAffliction, allowStacking); - - if (should != null && should.Value) - return; + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnCharacterApplyAffliction(this, limbHealth, newAffliction, allowStacking) ?? should); + if (should != null && should.Value) { return; } Affliction existingAffliction = null; foreach ((Affliction affliction, LimbHealth value) in afflictions) diff --git a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs index b29059ecb..9e625b50a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/GameSession/GameSession.cs @@ -1046,9 +1046,6 @@ namespace Barotrauma /// public static ImmutableHashSet GetSessionCrewCharacters(CharacterType type) { - var result = GameMain.LuaCs.Hook.Call("getSessionCrewCharacters", type); - if (result != null) return ImmutableHashSet.Create(result); - if (GameMain.GameSession?.CrewManager is not { } crewManager) { return ImmutableHashSet.Empty; } IEnumerable players; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs index d4d45f120..fe27ceb15 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Holdable/MeleeWeapon.cs @@ -1,4 +1,5 @@ -using FarseerPhysics; +using Barotrauma.LuaCs.Events; +using FarseerPhysics; using FarseerPhysics.Dynamics; using FarseerPhysics.Dynamics.Contacts; using Microsoft.Xna.Framework; @@ -435,7 +436,7 @@ namespace Barotrauma.Items.Components Structure targetStructure = target.UserData as Structure ?? targetFixture.UserData as Structure; Item targetItem = target.UserData is Holdable h ? h.Item : target.UserData as Item ?? targetFixture.UserData as Item; Entity targetEntity = targetCharacter ?? targetStructure ?? targetItem ?? target.UserData as Entity; - GameMain.LuaCs.Hook.Call("meleeWeapon.handleImpact", this, target); + GameMain.LuaCs.EventService.PublishEvent(x => x.OnMeleeWeaponHandleImpact(this, target)); if (Attack != null) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs index cd08a541a..be847f047 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Machines/Deconstructor.cs @@ -1,11 +1,13 @@ using Barotrauma.Abilities; using Barotrauma.Extensions; +using Barotrauma.LuaCs.Events; using Barotrauma.Networking; using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Xml.Linq; +using static OneOf.Types.TrueFalseOrNull; namespace Barotrauma.Items.Components { @@ -332,8 +334,9 @@ namespace Barotrauma.Items.Components GameAnalyticsManager.AddDesignEvent("ItemDeconstructed:" + (GameMain.GameSession?.GameMode?.Preset.Identifier.Value ?? "none") + ":" + targetItem.Prefab.Identifier); } - bool? result = GameMain.LuaCs.Hook.Call("item.deconstructed", targetItem, this, user, allowRemove); - if (result == true) { return; } + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnItemDeconstructed(targetItem, this, user, allowRemove) ?? should); + if (should == true) { return; } if (targetItem.AllowDeconstruct && allowRemove) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs index e59ab6599..7f8b4c75b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs @@ -1,10 +1,13 @@ -using Barotrauma.Networking; +using Barotrauma.LuaCs.Events; +using Barotrauma.Networking; using Microsoft.Xna.Framework; using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Xml.Linq; +using static Barotrauma.CharacterHealth; +using static Barotrauma.MedicalClinic; namespace Barotrauma.Items.Components { @@ -228,8 +231,8 @@ namespace Barotrauma.Items.Components public void TransmitSignal(Signal signal, bool sentFromChat) { - var should = GameMain.LuaCs.Hook.Call("wifiSignalTransmitted", this, signal, sentFromChat); - + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnWifiSignalTransmitted(this, signal, sentFromChat) ?? should); if (should != null && should.Value) { return; } bool chatMsgSent = false; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index f74a46541..8c700265a 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -1,20 +1,23 @@ -using Barotrauma.Items.Components; +using Barotrauma.Abilities; +using Barotrauma.Extensions; +using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Events; +using Barotrauma.MapCreatures.Behavior; using Barotrauma.Networking; using FarseerPhysics; using FarseerPhysics.Dynamics; using FarseerPhysics.Dynamics.Contacts; using Microsoft.Xna.Framework; +using MoonSharp.Interpreter; using System; using System.Collections.Concurrent; using System.Collections.Generic; +using System.Collections.Immutable; using System.Globalization; using System.Linq; using System.Xml.Linq; -using Barotrauma.Extensions; -using Barotrauma.MapCreatures.Behavior; -using MoonSharp.Interpreter; -using System.Collections.Immutable; -using Barotrauma.Abilities; +using static Barotrauma.CharacterHealth; +using static Barotrauma.MedicalClinic; #if CLIENT using Microsoft.Xna.Framework.Graphics; @@ -3891,9 +3894,9 @@ namespace Barotrauma } } - var result = GameMain.LuaCs.Hook.Call("item.readPropertyChange", this, property, parentObject, allowEditing, sender); - if (result != null && result.Value) - return; + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnItemReadPropertyChange(this, property, parentObject, allowEditing, sender) ?? should); + if (should != null && should.Value) { return; } Type type = property.PropertyType; string logValue = ""; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs index 662d7742f..12fb85f04 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs @@ -1,6 +1,7 @@ using Barotrauma.Items.Components; using Barotrauma.LuaCs.Data; using Barotrauma.Networking; +using FarseerPhysics.Dynamics; using Microsoft.Xna.Framework; using Steamworks.Ugc; using System; @@ -136,6 +137,408 @@ internal interface IEventCharacterCreated : IEvent } } +// TODO: harmony-fy +internal interface IEventHumanCPRSuccess : IEvent +{ + void OnCharacterCPRSuccess(HumanoidAnimController animController); + + static IEventHumanCPRSuccess IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventHumanCPRSuccess + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnCharacterCPRSuccess(HumanoidAnimController animController) + { + LuaFuncs[nameof(OnCharacterCPRSuccess)](animController); + } + } +} + +// TODO: harmony-fy +internal interface IEventHumanCPRFailed : IEvent +{ + void OnCharacterCPRFailed(HumanoidAnimController animController); + + static IEventHumanCPRFailed IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventHumanCPRFailed + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnCharacterCPRFailed(HumanoidAnimController animController) + { + LuaFuncs[nameof(OnCharacterCPRFailed)](animController); + } + } +} + +// TODO: harmony-fy +internal interface IEventClientControlHusk : IEvent +{ + void OnClientControlHusk(Client client, Character husk); + + static IEventClientControlHusk IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventClientControlHusk + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnClientControlHusk(Client client, Character husk) + { + LuaFuncs[nameof(OnClientControlHusk)](client, husk); + } + } +} + +// TODO: harmony-fy +internal interface IEventMeleeWeaponHandleImpact : IEvent +{ + void OnMeleeWeaponHandleImpact(MeleeWeapon meleeWeapon, Body target); + + static IEventMeleeWeaponHandleImpact IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventMeleeWeaponHandleImpact + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnMeleeWeaponHandleImpact(MeleeWeapon meleeWeapon, Body target) + { + LuaFuncs[nameof(OnMeleeWeaponHandleImpact)](meleeWeapon, target); + } + } +} + +// TODO: harmony-fy +internal interface IEventServerLog : IEvent +{ + void OnServerLog(string line, ServerLog.MessageType messageType); + + static IEventServerLog IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventServerLog + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public void OnServerLog(string line, ServerLog.MessageType messageType) + { + LuaFuncs[nameof(OnServerLog)](line, messageType); + } + } +} + +// TODO: harmony-fy +internal interface IEventChatMessage : IEvent +{ + bool? OnChatMessage(string messageText, Client sender, ChatMessageType type, ChatMessage message); + + static IEventChatMessage IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventChatMessage + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnChatMessage(string messageText, Client sender, ChatMessageType type, ChatMessage message) + { + var result = LuaFuncs[nameof(OnChatMessage)](messageText, sender, type, message); + if (result is bool b && b) + { + return true; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventTryClientChangeName : IEvent +{ + bool? OnTryClienChangeName(Client client, string newName, Identifier newJob, CharacterTeamType newTeam); + + static IEventTryClientChangeName IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventTryClientChangeName + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnTryClienChangeName(Client client, string newName, Identifier newJob, CharacterTeamType newTeam) + { + var result = LuaFuncs[nameof(OnTryClienChangeName)](client, newName, newJob, newTeam); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventChangeFallDamage : IEvent +{ + float? OnChangeFallDamage(float impactDamage, Character character, Vector2 impactPos, Vector2 velocity); + + static IEventChangeFallDamage IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventChangeFallDamage + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public float? OnChangeFallDamage(float impactDamage, Character character, Vector2 impactPos, Vector2 velocity) + { + var result = LuaFuncs[nameof(OnChangeFallDamage)](impactDamage, character, impactPos, velocity); + if (result is float f) + { + return f; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventGapOxygenUpdate : IEvent +{ + bool? OnGapOxygenUpdate(Gap gap, Hull hull1, Hull hull2); + + static IEventGapOxygenUpdate IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventGapOxygenUpdate + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnGapOxygenUpdate(Gap gap, Hull hull1, Hull hull2) + { + var result = LuaFuncs[nameof(OnGapOxygenUpdate)](gap, hull1, hull2); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventCharacterApplyDamage : IEvent +{ + bool? OnCharacterApplyDamage(CharacterHealth characterHealth, AttackResult attackResult, Limb hitLimb, bool allowStacking); + + static IEventCharacterApplyDamage IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventCharacterApplyDamage + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnCharacterApplyDamage(CharacterHealth characterHealth, AttackResult attackResult, Limb hitLimb, bool allowStacking) + { + var result = LuaFuncs[nameof(OnCharacterApplyDamage)](characterHealth, attackResult, hitLimb, allowStacking); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventCharacterApplyAffliction : IEvent +{ + bool? OnCharacterApplyAffliction(CharacterHealth characterHealth, CharacterHealth.LimbHealth limbHealth, Affliction newAffliction, bool allowStacking); + + static IEventCharacterApplyAffliction IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventCharacterApplyAffliction + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnCharacterApplyAffliction(CharacterHealth characterHealth, CharacterHealth.LimbHealth limbHealth, Affliction newAffliction, bool allowStacking) + { + var result = LuaFuncs[nameof(OnCharacterApplyAffliction)](characterHealth, limbHealth, newAffliction, allowStacking); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventItemReadPropertyChange : IEvent +{ + bool? OnItemReadPropertyChange(Item item, SerializableProperty property, object parentObject, bool allowEditing, Client sender); + + static IEventItemReadPropertyChange IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventItemReadPropertyChange + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnItemReadPropertyChange(Item item, SerializableProperty property, object parentObject, bool allowEditing, Client sender) + { + var result = LuaFuncs[nameof(OnItemReadPropertyChange)](item, property, parentObject, allowEditing, sender); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventCanUseVoiceRadio : IEvent +{ + bool? OnCanUseVoiceRadio(Client sender, Client recipient); + + static IEventCanUseVoiceRadio IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventCanUseVoiceRadio + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnCanUseVoiceRadio(Client sender, Client recipient) + { + var result = LuaFuncs[nameof(OnCanUseVoiceRadio)](sender, recipient); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventChangeLocalVoiceRange : IEvent +{ + float? OnChangeLocalVoiceRange(Client sender, Client recipient); + + static IEventChangeLocalVoiceRange IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventChangeLocalVoiceRange + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public float? OnChangeLocalVoiceRange(Client sender, Client recipient) + { + var result = LuaFuncs[nameof(OnChangeLocalVoiceRange)](sender, recipient); + if (result is float f) + { + return f; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventItemDeconstructed : IEvent +{ + bool? OnItemDeconstructed(Item item, Deconstructor deconstructor, Character user, bool allowRemove); + + static IEventItemDeconstructed IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventItemDeconstructed + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnItemDeconstructed(Item item, Deconstructor deconstructor, Character user, bool allowRemove) + { + var result = LuaFuncs[nameof(OnItemDeconstructed)](item, deconstructor, user, allowRemove); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + +// TODO: harmony-fy +internal interface IEventWifiSignalTransmitted : IEvent +{ + bool? OnWifiSignalTransmitted(WifiComponent wifiComponent, Signal signal, bool sentFromChat); + + static IEventWifiSignalTransmitted IEvent.GetLuaRunner(IDictionary luaFunc) + => new LuaWrapper(luaFunc); + + public sealed class LuaWrapper : LuaWrapperBase, IEventWifiSignalTransmitted + { + public LuaWrapper(IDictionary luaFuncs) : base(luaFuncs) + { + } + + public bool? OnWifiSignalTransmitted(WifiComponent wifiComponent, Signal signal, bool sentFromChat) + { + var result = LuaFuncs[nameof(OnWifiSignalTransmitted)](wifiComponent, signal, sentFromChat); + if (result is bool b) + { + return b; + } + + return null; + } + } +} + internal interface IEventCharacterDeath : IEvent { void OnCharacterDeath(Character character, Affliction causeOfDeathAffliction, CauseOfDeathType causeOfDeathType); @@ -156,29 +559,6 @@ internal interface IEventCharacterDeath : IEvent } } -/* -internal interface IEventHumanCPRFailed : IEvent -{ - void OnHumanCPRFailed(Character character); - static IEventHumanCPRFailed IEvent.GetLuaRunner(IDictionary luaFunc) => new - { - IsLuaRunner = Return.Arguments(() => true), - OnHumanCPRFailed = ReturnVoid.Arguments((Character character) => luaFunc[nameof(OnHumanCPRFailed)](character)) - }.ActLike(); -} - - -internal interface IEventHumanCPRSuccess : IEvent -{ - void OnHumanCPRSuccess(Character character); - static IEventHumanCPRSuccess IEvent.GetLuaRunner(IDictionary luaFunc) => new - { - IsLuaRunner = Return.Arguments(() => true), - OnHumanCPRSuccess = ReturnVoid.Arguments((Character character) => luaFunc[nameof(OnHumanCPRSuccess)](character)) - }.ActLike(); -} -*/ - public interface IEventKeyUpdate : IEvent { void OnKeyUpdate(double deltaTime); diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs index cbd773c6d..305950efe 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LoggerService.cs @@ -1,4 +1,5 @@ -using Barotrauma.Networking; +using Barotrauma.LuaCs.Events; +using Barotrauma.Networking; using FluentResults; using HarmonyLib; using Microsoft.Xna.Framework; @@ -59,7 +60,7 @@ public partial class LoggerService : ILoggerService if (!_isInsideLogCall) { _isInsideLogCall = true; - GameMain.LuaCs?.Hook?.Call("serverLog", logMessage, log.MessageType); + GameMain.LuaCs?.EventService.PublishEvent(x => x.OnServerLog(logMessage, log.MessageType)); _isInsideLogCall = false; } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs index d0267f7d3..6fbb41a35 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs @@ -179,6 +179,25 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService _eventService.RegisterLuaEventAlias("character.death", nameof(IEventCharacterDeath.OnCharacterDeath)); _eventService.RegisterLuaEventAlias("character.damageLimb", nameof(IEventCharacterDamageLimb.OnCharacterDamageLimb)); _eventService.RegisterLuaEventAlias("character.giveJobItems", nameof(IEventGiveCharacterJobItems.OnGiveCharacterJobItems)); + _eventService.RegisterLuaEventAlias("character.CPRSuccess", nameof(IEventHumanCPRSuccess.OnCharacterCPRSuccess)); + _eventService.RegisterLuaEventAlias("character.CPRFailed", nameof(IEventHumanCPRFailed.OnCharacterCPRFailed)); + _eventService.RegisterLuaEventAlias("character.applyDamage", nameof(IEventCharacterApplyDamage.OnCharacterApplyDamage)); + _eventService.RegisterLuaEventAlias("character.applyAffliction", nameof(IEventCharacterApplyAffliction.OnCharacterApplyAffliction)); + + _eventService.RegisterLuaEventAlias("gapOxygenUpdate", nameof(IEventGapOxygenUpdate.OnGapOxygenUpdate)); + + _eventService.RegisterLuaEventAlias("husk.clientControlHusk", nameof(IEventClientControlHusk.OnClientControlHusk)); + + _eventService.RegisterLuaEventAlias("meleeWeapon.handleImpact", nameof(IEventMeleeWeaponHandleImpact.OnMeleeWeaponHandleImpact)); + + _eventService.RegisterLuaEventAlias("serverLog", nameof(IEventServerLog.OnServerLog)); + + _eventService.RegisterLuaEventAlias("tryChangeClientName", nameof(IEventTryClientChangeName.OnTryClienChangeName)); + + _eventService.RegisterLuaEventAlias("changeFallDamage", nameof(IEventChangeFallDamage.OnChangeFallDamage)); + + _eventService.RegisterLuaEventAlias("canUseVoiceRadio", nameof(IEventCanUseVoiceRadio.OnCanUseVoiceRadio)); + _eventService.RegisterLuaEventAlias("changeLocalVoiceRange", nameof(IEventChangeLocalVoiceRange.OnChangeLocalVoiceRange)); _eventService.RegisterLuaEventAlias("roundStart", nameof(IEventRoundStarted.OnRoundStart)); _eventService.RegisterLuaEventAlias("roundEnd", nameof(IEventRoundEnded.OnRoundEnd)); @@ -190,6 +209,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService _eventService.RegisterLuaEventAlias("item.removed", nameof(IEventItemRemoved.OnItemRemoved)); _eventService.RegisterLuaEventAlias("item.use", nameof(IEventItemUse.OnItemUsed)); _eventService.RegisterLuaEventAlias("item.secondaryUse", nameof(IEventItemSecondaryUse.OnItemSecondaryUsed)); + _eventService.RegisterLuaEventAlias("item.readPropertyChange", nameof(IEventItemReadPropertyChange.OnItemReadPropertyChange)); + _eventService.RegisterLuaEventAlias("item.deconstructed", nameof(IEventItemDeconstructed.OnItemDeconstructed)); _eventService.RegisterLuaEventAlias("inventoryPutItem", nameof(IEventInventoryPutItem.OnInventoryPutItem)); _eventService.RegisterLuaEventAlias("inventoryItemSwap", nameof(IEventInventoryItemSwap.OnInventoryItemSwap)); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs index 94b8bd03e..cdf46bbda 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs @@ -1,13 +1,15 @@ using Barotrauma.Extensions; using Barotrauma.Items.Components; +using Barotrauma.LuaCs.Events; +using Barotrauma.Networking; using FarseerPhysics; using FarseerPhysics.Dynamics; using Microsoft.Xna.Framework; +using MoonSharp.Interpreter; using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; -using MoonSharp.Interpreter; namespace Barotrauma { @@ -883,8 +885,8 @@ namespace Barotrauma if (Math.Max(hull1.WorldSurface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.WorldSurface + hull2.WaveY[0]) > WorldRect.Y) { return; } } - var should = GameMain.LuaCs.Hook.Call("gapOxygenUpdate", this, hull1, hull2); - + bool? should = null; + GameMain.LuaCs.EventService.PublishEvent(x => should = x.OnGapOxygenUpdate(this, hull1, hull2) ?? should); if (should != null && should.Value) return; float totalOxygen = hull1.Oxygen + hull2.Oxygen; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Networking/RespawnManager.cs b/Barotrauma/BarotraumaShared/SharedSource/Networking/RespawnManager.cs index 8f67b919d..a9d9ab0d6 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Networking/RespawnManager.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Networking/RespawnManager.cs @@ -213,9 +213,6 @@ namespace Barotrauma.Networking public void Update(float deltaTime) { - var result = GameMain.LuaCs.Hook.Call("respawnManager.update"); - if (result != null && result.Value) { return; } - foreach (var teamSpecificState in teamSpecificStates.Values) { if (RespawnShuttles.None())