diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs index db93b88dd..ab7952c3e 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/ChatMessage.cs @@ -117,19 +117,13 @@ namespace Barotrauma.Networking return; } - var should = GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type }); + var should = new LuaResult(GameMain.Lua.hook.Call("chatMessage", new object[] { txt, c, type })); - if(should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } + if (should.Bool()) + { + return; + } if (type == ChatMessageType.Order) { diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs index e5446a2cc..1261712aa 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/GameServer.cs @@ -3112,18 +3112,11 @@ namespace Barotrauma.Networking var hookChatMsg = ChatMessage.Create(senderName, message, (ChatMessageType)type, senderCharacter, senderClient, changeType); - var should = GameMain.Lua.hook.Call("modifyChatMessage", new object[] { hookChatMsg, senderRadio }); + var should = new LuaResult(GameMain.Lua.hook.Call("modifyChatMessage", new object[] { hookChatMsg, senderRadio })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } + if (should.Bool()) + return; + //check which clients can receive the message and apply distance effects foreach (Client client in ConnectedClients) diff --git a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs index 0267d5375..db3ad09b8 100644 --- a/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs +++ b/Barotrauma/BarotraumaServer/ServerSource/Networking/Voip/VoipServer.cs @@ -94,29 +94,20 @@ namespace Barotrauma.Networking ChatMessage.CanUseRadio(sender.Character, out WifiComponent senderRadio) && ChatMessage.CanUseRadio(recipient.Character, out WifiComponent recipientRadio)) { - var should = GameMain.Lua.hook.Call("canUseVoiceRadio", new object[] { sender, recipient }); + var should = new LuaResult(GameMain.Lua.hook.Call("canUseVoiceRadio", new object[] { sender, recipient })); - if (should != null) - { - if (should is DynValue dyn) - { - return dyn.CastToBool(); - } - } + if (!should.IsNull() && should.Bool()) + return should.Bool(); if (recipientRadio.CanReceive(senderRadio)) { return true; } } - var should2 = GameMain.Lua.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient }); + var should2 = new LuaResult(GameMain.Lua.hook.Call("changeLocalVoiceRange", new object[] { sender, recipient })); float range = 1.0f; - if (should2 != null) - { - if (should2 is DynValue dyn) - { - range = (float)dyn.CastToNumber(); - } - } + if (!should2.IsNull()) + range = should2.Float(); + //otherwise do a distance check return ChatMessage.GetGarbleAmount(recipient.Character, sender.Character, ChatMessage.SpeakRange) < range; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs index 56805d478..158101565 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Animation/Ragdoll.cs @@ -712,18 +712,12 @@ namespace Barotrauma float impactDamage = Math.Min((impact - ImpactTolerance) * ImpactDamageMultiplayer, character.MaxVitality * MaxImpactDamage); -#if SERVER + var should = new LuaResult(GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity })); - var should = GameMain.Lua.hook.Call("changeFallDamage", new object[] { impactDamage, character, impactPos, velocity }); - - if (should != null) - { - if (should is DynValue dyn) - { - impactDamage = (float)dyn.CastToNumber(); - } + if (!should.IsNull()) + { + impactDamage = should.Float(); } -#endif character.LastDamageSource = null; character.AddDamage(impactPos, AfflictionPrefab.ImpactDamage.Instantiate(impactDamage).ToEnumerable(), 0.0f, true); diff --git a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs index cf84816b9..bfa7d0906 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Characters/Health/CharacterHealth.cs @@ -409,20 +409,11 @@ namespace Barotrauma { if (targetLimb == null) { -#if SERVER - var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction }); + var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction })); + + if (should.Bool()) + return; - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif //if a limb-specific affliction is applied to no specific limb, apply to all limbs foreach (LimbHealth limbHealth in limbHealths) @@ -433,40 +424,20 @@ namespace Barotrauma } else { -#if SERVER - var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb }); + var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction, targetLimb })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; AddLimbAffliction(targetLimb, affliction); } } else { -#if SERVER - var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction }); + var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, affliction })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; AddAffliction(affliction); } @@ -544,20 +515,10 @@ namespace Barotrauma return; } -#if SERVER - var should = GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb }); + var should = new LuaResult(GameMain.Lua.hook.Call("afflictionApplied", new object[] { this, attackResult, hitLimb })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; foreach (Affliction newAffliction in attackResult.Afflictions) { diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs index 2ffa431bf..9c1544481 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Components/Signal/WifiComponent.cs @@ -186,20 +186,10 @@ namespace Barotrauma.Items.Components var senderComponent = signal.source?.GetComponent(); if (senderComponent != null && !CanReceive(senderComponent)) { return; } -#if SERVER - var should = GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat }); + var should = new LuaResult(GameMain.Lua.hook.Call("wifiSignalTransmitted", new object[] { this, signal, sentFromChat })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; bool chatMsgSent = false; diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index dd901bc0a..67d1b297b 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -2225,20 +2225,10 @@ namespace Barotrauma if (condition == 0.0f) { return; } -#if SERVER - var should = GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb }); + var should = new LuaResult(GameMain.Lua.hook.Call("itemUse", new object[] { this, character, targetLimb })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; bool remove = false; @@ -2273,20 +2263,11 @@ namespace Barotrauma { if (condition == 0.0f) { return; } -#if SERVER - var should = GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character}); + var should = new LuaResult(GameMain.Lua.hook.Call("itemSecondaryUse", new object[] { this, character})); + + if (should.Bool()) + return; - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif bool remove = false; @@ -2319,20 +2300,10 @@ namespace Barotrauma public void ApplyTreatment(Character user, Character character, Limb targetLimb) { -#if SERVER - var should = GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb }); + var should = new LuaResult(GameMain.Lua.hook.Call("itemApplyTreatment", new object[] { this, user, character, targetLimb })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; //can't apply treatment to dead characters if (character.IsDead) return; @@ -2390,20 +2361,10 @@ namespace Barotrauma public void Drop(Character dropper, bool createNetworkEvent = true) { -#if SERVER - var should = GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper}); + var should = new LuaResult(GameMain.Lua.hook.Call("itemDrop", new object[] { this, dropper})); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; if (createNetworkEvent) { @@ -2455,20 +2416,10 @@ namespace Barotrauma public void Equip(Character character) { -#if SERVER - var should = GameMain.Lua.hook.Call("itemEquip", new object[] { this, character}); + var should = new LuaResult(GameMain.Lua.hook.Call("itemEquip", new object[] { this, character})); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; if (Removed) { @@ -2481,20 +2432,10 @@ namespace Barotrauma public void Unequip(Character character) { -#if SERVER - var should = GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character }); + var should = new LuaResult(GameMain.Lua.hook.Call("itemUnequip", new object[] { this, character })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; foreach (ItemComponent ic in components) { ic.Unequip(character); } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs index 4853bd332..66100cd21 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaClasses.cs @@ -559,8 +559,12 @@ namespace Barotrauma try { if (hf.function is Closure) - lastResult = env.lua.Call(hf.function, args); - // else if (hf.function is NLua.LuaFunction luaFunction) + { + var result = env.lua.Call(hf.function, args); + if (result.CastToBool() != false) + lastResult = result; + } + //else if (hf.function is NLua.LuaFunction luaFunction) // lastResult = luaFunction.Call(args); } catch (Exception e) @@ -572,5 +576,60 @@ namespace Barotrauma return lastResult; } } + + } + + public class LuaResult + { + object result; + public LuaResult(object arg) + { + result = arg; + } + + public bool IsNull() + { + if (result == null) + return true; + + if (result is DynValue dynValue) + return dynValue.IsNil(); + + return false; + } + + public bool Bool() + { + if (result is DynValue dynValue) + { + return dynValue.CastToBool(); + } + + return false; + } + + public float Float() + { + if (result is DynValue dynValue) + { + var num = dynValue.CastToNumber(); + if (num == null) { return 0f; } + return (float)num; + } + + return 0f; + } + + public double Double() + { + if (result is DynValue dynValue) + { + var num = dynValue.CastToNumber(); + if (num == null) { return 0f; } + return (double)num; + } + + return 0f; + } } } \ No newline at end of file diff --git a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs index 5ae3be0df..5d9dc228d 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Map/Gap.cs @@ -650,20 +650,10 @@ namespace Barotrauma if (Math.Max(hull1.WorldSurface + hull1.WaveY[hull1.WaveY.Length - 1], hull2.WorldSurface + hull2.WaveY[0]) > WorldRect.Y) { return; } } -#if SERVER - var should = GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 }); + var should = new LuaResult(GameMain.Lua.hook.Call("gapOxygenUpdate", new object[] { this, hull1, hull2 })); - if (should != null) - { - if (should is DynValue dyn) - { - if (dyn.CastToBool()) - { - return; - } - } - } -#endif + if (should.Bool()) + return; float totalOxygen = hull1.Oxygen + hull2.Oxygen; float totalVolume = (hull1.Volume + hull2.Volume);