From d80d4d91a3ddd976aea1b651b46d59ffce246790 Mon Sep 17 00:00:00 2001 From: Roland Firmont Date: Mon, 21 Mar 2022 16:12:29 +0100 Subject: [PATCH] Fix crash with some function hooks Hooking functions that return float, ushort or byte causes a crash as the returned value will not match the expected type. This fix will allow to return LuaFloat, LuaByte and LuaUShort instead. Example hook that would crash without this: ItemComponent.DegreeOfSuccess --- .../SharedSource/Lua/LuaCustomConverters.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs index 87ada48eb..e49ae1a65 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Lua/LuaCustomConverters.cs @@ -69,6 +69,23 @@ namespace Barotrauma { return DynValue.NewString(v.ToString()); }); + + Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.UserData, typeof(object), v => + { + if (v.UserData.Object is LuaByte lbyte) + { + return lbyte.Value; + } + else if (v.UserData.Object is LuaUShort lushort) + { + return lushort.Value; + } + else if (v.UserData.Object is LuaFloat lfloat) + { + return lfloat.Value; + } + return v.UserData.Object; + }); } public static void RegisterAction()