Merge pull request #104 from notpeelz/fix-hook-cs-compat

Fix Hook.Add compatibility with C# mods
This commit is contained in:
Evil Factory
2022-08-20 12:38:44 -03:00
committed by GitHub
4 changed files with 23 additions and 20 deletions
@@ -409,8 +409,13 @@ namespace Barotrauma
() => () =>
{ {
if (getValidArgs == null) return null; if (getValidArgs == null) return null;
return getValidArgs().ToObject<string[][]>(); var validArgs = getValidArgs();
}, isCheat); if (validArgs is DynValue luaValue)
{
return luaValue.ToObject<string[][]>();
}
return (string[][])validArgs;
}, isCheat);
luaAddedCommand.Add(cmd); luaAddedCommand.Add(cmd);
DebugConsole.Commands.Add(cmd); DebugConsole.Commands.Add(cmd);
@@ -6,16 +6,10 @@ using LuaCsCompatPatchFunc = Barotrauma.LuaCsPatch;
namespace Barotrauma namespace Barotrauma
{ {
public delegate DynValue CallLuaFunctionFunc(object function, params object[] args); partial class LuaCsSetup
internal static class LuaCustomConverters
{ {
private static CallLuaFunctionFunc CallLuaFunction; private void RegisterLuaConverters()
public static void Initialize(CallLuaFunctionFunc callLuaFunction)
{ {
CallLuaFunction = callLuaFunction;
RegisterAction<Item>(); RegisterAction<Item>();
RegisterAction<Character>(); RegisterAction<Character>();
RegisterAction<Entity>(); RegisterAction<Entity>();
@@ -194,7 +188,7 @@ namespace Barotrauma
: throw new ScriptRuntimeException("use Double(value) to pass primitive type 'double' to C#")); : throw new ScriptRuntimeException("use Double(value) to pass primitive type 'double' to C#"));
} }
public static void RegisterAction<T>() private void RegisterAction<T>()
{ {
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), v => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), v =>
{ {
@@ -209,7 +203,7 @@ namespace Barotrauma
}); });
} }
public static void RegisterAction<T1, T2>() private void RegisterAction<T1, T2>()
{ {
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T1, T2>), v => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T1, T2>), v =>
{ {
@@ -224,7 +218,7 @@ namespace Barotrauma
}); });
} }
public static void RegisterAction() private void RegisterAction()
{ {
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
{ {
@@ -239,7 +233,7 @@ namespace Barotrauma
}); });
} }
public static void RegisterFunc<T1>() private void RegisterFunc<T1>()
{ {
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1>), v => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1>), v =>
{ {
@@ -254,7 +248,7 @@ namespace Barotrauma
}); });
} }
public static void RegisterFunc<T1, T2>() private void RegisterFunc<T1, T2>()
{ {
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2>), v => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2>), v =>
{ {
@@ -269,7 +263,7 @@ namespace Barotrauma
}); });
} }
public static void RegisterFunc<T1, T2, T3, T4, T5>() private void RegisterFunc<T1, T2, T3, T4, T5>()
{ {
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2, T3, T4, T5>), v => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2, T3, T4, T5>), v =>
{ {
@@ -18,7 +18,7 @@ using Sigil.NonGeneric;
namespace Barotrauma namespace Barotrauma
{ {
public delegate void LuaCsAction(params object[] args); public delegate void LuaCsAction(params object[] args);
public delegate DynValue LuaCsFunc(params object[] args); public delegate object LuaCsFunc(params object[] args);
public delegate DynValue LuaCsPatchFunc(object instance, LuaCsHook.ParameterTable ptable); public delegate DynValue LuaCsPatchFunc(object instance, LuaCsHook.ParameterTable ptable);
internal static class SigilExtensions internal static class SigilExtensions
@@ -777,9 +777,13 @@ namespace Barotrauma
var result = tuple.Item1.func(args); var result = tuple.Item1.func(args);
// TODO(BREAKING): change this to !result.IsVoid() // TODO(BREAKING): change this to !result.IsVoid()
if (result != null && !result.IsNil()) if (result is DynValue luaResult && !luaResult.IsNil())
{ {
lastResult = result.ToObject<T>(); lastResult = luaResult.ToObject<T>();
}
else
{
lastResult = (T)result;
} }
if (luaCs.PerformanceCounter.EnablePerformanceCounter) if (luaCs.PerformanceCounter.EnablePerformanceCounter)
@@ -409,7 +409,7 @@ namespace Barotrauma
LuaScriptLoader = new LuaScriptLoader(); LuaScriptLoader = new LuaScriptLoader();
LuaScriptLoader.ModulePaths = new string[] { }; LuaScriptLoader.ModulePaths = new string[] { };
LuaCustomConverters.Initialize(CallLuaFunction); RegisterLuaConverters();
lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug); lua = new Script(CoreModules.Preset_SoftSandbox | CoreModules.Debug);
lua.Options.DebugPrint = PrintMessage; lua.Options.DebugPrint = PrintMessage;