From 91992194a9a50ca6406448da805996e4b8532a4f Mon Sep 17 00:00:00 2001 From: Evil Factory <36804725+evilfactory@users.noreply.github.com> Date: Fri, 20 Feb 2026 20:40:06 -0300 Subject: [PATCH] Fix return events not working in Lua --- .../SharedSource/LuaCs/IEvents.cs | 100 +++++++++--------- .../_Services/LuaScriptManagementService.cs | 2 + 2 files changed, 50 insertions(+), 52 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs index 12fb85f04..c12c5669f 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/IEvents.cs @@ -3,6 +3,7 @@ using Barotrauma.LuaCs.Data; using Barotrauma.Networking; using FarseerPhysics.Dynamics; using Microsoft.Xna.Framework; +using MoonSharp.Interpreter; using Steamworks.Ugc; using System; using System.Collections.Generic; @@ -258,10 +259,10 @@ internal interface IEventChatMessage : IEvent 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) + object result = LuaFuncs[nameof(OnChatMessage)](messageText, sender, type, message); + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return true; + return dynValue.Boolean; } return null; @@ -286,9 +287,9 @@ internal interface IEventTryClientChangeName : IEvent 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) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; + return dynValue.Boolean; } return null; @@ -313,9 +314,9 @@ internal interface IEventChangeFallDamage : IEvent 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) + if (result is DynValue dynValue && dynValue.Type == DataType.Number) { - return f; + return (float)dynValue.Number; } return null; @@ -340,9 +341,9 @@ internal interface IEventGapOxygenUpdate : IEvent public bool? OnGapOxygenUpdate(Gap gap, Hull hull1, Hull hull2) { var result = LuaFuncs[nameof(OnGapOxygenUpdate)](gap, hull1, hull2); - if (result is bool b) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; + return dynValue.Boolean; } return null; @@ -367,9 +368,9 @@ internal interface IEventCharacterApplyDamage : IEvent public bool? OnCanUseVoiceRadio(Client sender, Client recipient) { var result = LuaFuncs[nameof(OnCanUseVoiceRadio)](sender, recipient); - if (result is bool b) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; + return dynValue.Boolean; } return null; @@ -475,9 +476,9 @@ internal interface IEventChangeLocalVoiceRange : IEvent 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) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; + return dynValue.Boolean; } return null; @@ -529,9 +530,9 @@ internal interface IEventWifiSignalTransmitted : IEvent public bool? OnItemUsed(Item item, Character user, Limb targetLimb, Entity useTarget) { var result = LuaFuncs[nameof(OnItemUsed)](item, user, targetLimb, useTarget); - if (result is bool b) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; - } - else - { - return null; + return dynValue.Boolean; } + + return null; } } } @@ -817,14 +816,12 @@ interface IEventItemSecondaryUse : IEvent public bool? OnItemSecondaryUsed(Item item, Character user) { var result = LuaFuncs[nameof(OnItemSecondaryUsed)](item, user); - if (result is bool b) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; - } - else - { - return null; + return dynValue.Boolean; } + + return null; } } } @@ -844,15 +841,18 @@ interface IEventCharacterDamageLimb : IEvent public AttackResult? OnCharacterDamageLimb(Character character, Vector2 worldPosition, Limb hitLimb, IEnumerable afflictions, float stun, bool playSound, Vector2 attackImpulse, Character attacker = null, float damageMultiplier = 1, bool allowStacking = true, float penetration = 0f, bool shouldImplode = false) { - var result = LuaFuncs[nameof(OnCharacterDamageLimb)](character, worldPosition, hitLimb, afflictions, stun, playSound, attackImpulse, attacker, damageMultiplier, allowStacking, penetration, shouldImplode); + object result = LuaFuncs[nameof(OnCharacterDamageLimb)](character, worldPosition, hitLimb, afflictions, stun, playSound, attackImpulse, attacker, damageMultiplier, allowStacking, penetration, shouldImplode); + if (result is DynValue dynValue) + { + result = dynValue.ToObject(); + } + if (result is AttackResult attackResult) { return attackResult; } - else - { - return null; - } + + return null; } } } @@ -873,14 +873,12 @@ interface IEventInventoryPutItem : IEvent public bool? OnInventoryPutItem(Inventory inventory, Item item, Character user, int i, bool removeItem) { var result = LuaFuncs[nameof(OnInventoryPutItem)](inventory, item, user, i, removeItem); - if (result is bool b) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; - } - else - { - return null; + return dynValue.Boolean; } + + return null; } } } @@ -901,14 +899,12 @@ interface IEventInventoryItemSwap : IEvent public bool? OnInventoryItemSwap(Inventory inventory, Item item, Character user, int i, bool swapWholeStack) { var result = LuaFuncs[nameof(OnInventoryItemSwap)](inventory, item, user, i, swapWholeStack); - if (result is bool b) + if (result is DynValue dynValue && dynValue.Type == DataType.Boolean) { - return b; - } - else - { - return null; + return dynValue.Boolean; } + + return null; } } } diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs index 6fbb41a35..cceb83a13 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/_Services/LuaScriptManagementService.cs @@ -196,6 +196,8 @@ class LuaScriptManagementService : ILuaScriptManagementService, ILuaDataService _eventService.RegisterLuaEventAlias("changeFallDamage", nameof(IEventChangeFallDamage.OnChangeFallDamage)); + _eventService.RegisterLuaEventAlias("chatMessage", nameof(IEventChatMessage.OnChatMessage)); + _eventService.RegisterLuaEventAlias("canUseVoiceRadio", nameof(IEventCanUseVoiceRadio.OnCanUseVoiceRadio)); _eventService.RegisterLuaEventAlias("changeLocalVoiceRange", nameof(IEventChangeLocalVoiceRange.OnChangeLocalVoiceRange));