Remove LuaResult
This commit is contained in:
@@ -367,13 +367,11 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs = null, bool isCheat = false)
|
public void AddCommand(string name, string help, LuaCsAction onExecute, LuaCsFunc getValidArgs = null, bool isCheat = false)
|
||||||
{
|
{
|
||||||
var cmd = new DebugConsole.Command(name, help, (string[] arg1) => { onExecute(new object[] { arg1 }); },
|
var cmd = new DebugConsole.Command(name, help, (string[] arg1) => onExecute(new object[] { arg1 }),
|
||||||
() =>
|
() =>
|
||||||
{
|
{
|
||||||
if (getValidArgs == null) { return null; }
|
if (getValidArgs == null) return null;
|
||||||
var obj = getValidArgs();
|
return getValidArgs().ToObject<string[][]>();
|
||||||
if (obj is LuaResult lr) { return lr.DynValue().ToObject<string[][]>(); }
|
|
||||||
return (string[][])obj;
|
|
||||||
}, isCheat);
|
}, isCheat);
|
||||||
|
|
||||||
luaAddedCommand.Add(cmd);
|
luaAddedCommand.Add(cmd);
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using MoonSharp.Interpreter;
|
|
||||||
using Microsoft.Xna.Framework;
|
|
||||||
using Barotrauma.Networking;
|
|
||||||
using Barotrauma.Items.Components;
|
|
||||||
using System.IO;
|
|
||||||
using System.Net;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Xml.Linq;
|
|
||||||
using FarseerPhysics.Dynamics;
|
|
||||||
using System.Reflection;
|
|
||||||
using HarmonyLib;
|
|
||||||
using MoonSharp.Interpreter.Interop;
|
|
||||||
using System.Diagnostics;
|
|
||||||
|
|
||||||
namespace Barotrauma
|
|
||||||
{
|
|
||||||
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.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
public double Double()
|
|
||||||
{
|
|
||||||
if (result is DynValue dynValue)
|
|
||||||
{
|
|
||||||
var num = dynValue.CastToNumber();
|
|
||||||
if (num == null) { return 0f; }
|
|
||||||
return num.Value;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0f;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string String()
|
|
||||||
{
|
|
||||||
if (result is DynValue dynValue)
|
|
||||||
{
|
|
||||||
var str = dynValue.CastToString();
|
|
||||||
if (str == null) { return ""; }
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Object()
|
|
||||||
{
|
|
||||||
if (result is DynValue dynValue)
|
|
||||||
{
|
|
||||||
return dynValue.ToObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public DynValue DynValue()
|
|
||||||
{
|
|
||||||
if (result is DynValue dynValue)
|
|
||||||
{
|
|
||||||
return dynValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -21,42 +21,50 @@ namespace Barotrauma
|
|||||||
RegisterFunc<Fixture, Vector2, Vector2, float, float>();
|
RegisterFunc<Fixture, Vector2, Vector2, float, float>();
|
||||||
RegisterFunc<AIObjective, bool>();
|
RegisterFunc<AIObjective, bool>();
|
||||||
|
|
||||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsAction), v => (LuaCsAction)( args => GameMain.LuaCs.CallLuaFunction(v.Function, args) ));
|
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsFunc), v => (LuaCsFunc)( args => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, args)) ));
|
DataType.Function,
|
||||||
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsPatch), v => (LuaCsPatch)( (self, args) => new LuaResult(GameMain.LuaCs.CallLuaFunction(v.Function, self, args)) ));
|
typeof(LuaCsAction),
|
||||||
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(LuaResult), (Script s, object v) => (v as LuaResult).DynValue());
|
v => (LuaCsAction)(args => GameMain.LuaCs.CallLuaFunction(v.Function, args)));
|
||||||
|
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||||
|
DataType.Function,
|
||||||
|
typeof(LuaCsFunc),
|
||||||
|
v => (LuaCsFunc)(args => GameMain.LuaCs.CallLuaFunction(v.Function, args)));
|
||||||
|
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||||
|
DataType.Function,
|
||||||
|
typeof(LuaCsPatch),
|
||||||
|
v => (LuaCsPatch)((self, args) => GameMain.LuaCs.CallLuaFunction(v.Function, self, args)));
|
||||||
|
|
||||||
#if CLIENT
|
#if CLIENT
|
||||||
RegisterAction<float>();
|
RegisterAction<float>();
|
||||||
RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch, float>();
|
RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch, float>();
|
||||||
|
|
||||||
{
|
{
|
||||||
object Call(object function, params object[] arguments) => GameMain.LuaCs.CallLuaFunction(function, arguments);
|
DynValue Call(object function, params object[] arguments) => GameMain.LuaCs.CallLuaFunction(function, arguments);
|
||||||
void RegisterHandler<T>(Func<Closure, T> converter)
|
void RegisterHandler<T>(Func<Closure, T> converter)
|
||||||
=> Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function));
|
=> Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIComponent.SecondaryButtonDownHandler)(
|
RegisterHandler(f => (GUIComponent.SecondaryButtonDownHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIButton.OnClickedHandler)(
|
RegisterHandler(f => (GUIButton.OnClickedHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
RegisterHandler(f => (GUIButton.OnButtonDownHandler)(
|
RegisterHandler(f => (GUIButton.OnButtonDownHandler)(
|
||||||
() => new LuaResult(Call(f)).Bool()));
|
() => Call(f).CastToBool()));
|
||||||
RegisterHandler(f => (GUIButton.OnPressedHandler)(
|
RegisterHandler(f => (GUIButton.OnPressedHandler)(
|
||||||
() => new LuaResult(Call(f)).Bool()));
|
() => Call(f).CastToBool()));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIColorPicker.OnColorSelectedHandler)(
|
RegisterHandler(f => (GUIColorPicker.OnColorSelectedHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIDropDown.OnSelectedHandler)(
|
RegisterHandler(f => (GUIDropDown.OnSelectedHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIListBox.OnSelectedHandler)(
|
RegisterHandler(f => (GUIListBox.OnSelectedHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
RegisterHandler(f => (GUIListBox.OnRearrangedHandler)(
|
RegisterHandler(f => (GUIListBox.OnRearrangedHandler)(
|
||||||
(a1, a2) => Call(f, a1, a2)));
|
(a1, a2) => Call(f, a1, a2)));
|
||||||
RegisterHandler(f => (GUIListBox.CheckSelectedHandler)(
|
RegisterHandler(f => (GUIListBox.CheckSelectedHandler)(
|
||||||
() => new LuaResult(Call(f)).Object()));
|
() => Call(f).ToObject()));
|
||||||
|
|
||||||
RegisterHandler(f => (GUINumberInput.OnValueEnteredHandler)(
|
RegisterHandler(f => (GUINumberInput.OnValueEnteredHandler)(
|
||||||
(a1) => Call(f, a1)));
|
(a1) => Call(f, a1)));
|
||||||
@@ -64,28 +72,28 @@ namespace Barotrauma
|
|||||||
(a1) => Call(f, a1)));
|
(a1) => Call(f, a1)));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIProgressBar.ProgressGetterHandler)(
|
RegisterHandler(f => (GUIProgressBar.ProgressGetterHandler)(
|
||||||
() => new LuaResult(Call(f)).Float()));
|
() => (float)(Call(f).CastToNumber() ?? 0)));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIRadioButtonGroup.RadioButtonGroupDelegate)(
|
RegisterHandler(f => (GUIRadioButtonGroup.RadioButtonGroupDelegate)(
|
||||||
(a1, a2) => Call(f, a1, a2)));
|
(a1, a2) => Call(f, a1, a2)));
|
||||||
|
|
||||||
RegisterHandler(f => (GUIScrollBar.OnMovedHandler)(
|
RegisterHandler(f => (GUIScrollBar.OnMovedHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
RegisterHandler(f => (GUIScrollBar.ScrollConversion)(
|
RegisterHandler(f => (GUIScrollBar.ScrollConversion)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Float()));
|
(a1, a2) => (float)(Call(f, a1, a2).CastToNumber() ?? 0)));
|
||||||
|
|
||||||
RegisterHandler(f => (GUITextBlock.TextGetterHandler)(
|
RegisterHandler(f => (GUITextBlock.TextGetterHandler)(
|
||||||
() => new LuaResult(Call(f, new object[] { })).String()));
|
() => Call(f, new object[0]).CastToString()));
|
||||||
|
|
||||||
RegisterHandler(f => (GUITextBox.OnEnterHandler)(
|
RegisterHandler(f => (GUITextBox.OnEnterHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
RegisterHandler(f => (GUITextBox.OnTextChangedHandler)(
|
RegisterHandler(f => (GUITextBox.OnTextChangedHandler)(
|
||||||
(a1, a2) => new LuaResult(Call(f, a1, a2)).Bool()));
|
(a1, a2) => Call(f, a1, a2).CastToBool()));
|
||||||
RegisterHandler(f => (TextBoxEvent)(
|
RegisterHandler(f => (TextBoxEvent)(
|
||||||
(a1, a2) => Call(f, a1, a2)));
|
(a1, a2) => Call(f, a1, a2)));
|
||||||
|
|
||||||
RegisterHandler(f => (GUITickBox.OnSelectedHandler)(
|
RegisterHandler(f => (GUITickBox.OnSelectedHandler)(
|
||||||
(a1) => new LuaResult(Call(f, a1)).Bool()));
|
(a1) => Call(f, a1).CastToBool()));
|
||||||
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ using System.Diagnostics;
|
|||||||
namespace Barotrauma
|
namespace Barotrauma
|
||||||
{
|
{
|
||||||
public delegate void LuaCsAction(params object[] args);
|
public delegate void LuaCsAction(params object[] args);
|
||||||
public delegate object LuaCsFunc(params object[] args);
|
public delegate DynValue LuaCsFunc(params object[] args);
|
||||||
public delegate object LuaCsPatch(object self, Dictionary<string, object> args);
|
public delegate object LuaCsPatch(object self, Dictionary<string, object> args);
|
||||||
|
|
||||||
public partial class LuaCsHook
|
public partial class LuaCsHook
|
||||||
@@ -157,18 +157,18 @@ namespace Barotrauma
|
|||||||
[MoonSharpHidden]
|
[MoonSharpHidden]
|
||||||
public T Call<T>(string name, params object[] args)
|
public T Call<T>(string name, params object[] args)
|
||||||
{
|
{
|
||||||
if (GameMain.LuaCs == null) { return default(T); }
|
if (GameMain.LuaCs == null) return default; // FIXME: should this throw an exception?
|
||||||
if (name == null) { throw new ScriptRuntimeException("Hook.Call: name must not be null."); }
|
if (name == null) throw new ArgumentNullException(name);
|
||||||
if (args == null) { args = new object[] { }; }
|
if (args == null) args = new object[0];
|
||||||
|
|
||||||
name = name.ToLower();
|
name = name.ToLower();
|
||||||
|
|
||||||
if (!hookFunctions.ContainsKey(name))
|
if (!hookFunctions.ContainsKey(name))
|
||||||
{
|
{
|
||||||
return default(T);
|
return default;
|
||||||
}
|
}
|
||||||
|
|
||||||
T lastResult = default(T);
|
T lastResult = default;
|
||||||
|
|
||||||
if (!hookFunctions.ContainsKey(name))
|
if (!hookFunctions.ContainsKey(name))
|
||||||
{
|
{
|
||||||
@@ -192,30 +192,9 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
|
|
||||||
var result = tuple.Item1.func(args);
|
var result = tuple.Item1.func(args);
|
||||||
if (result != null)
|
if (result != null && !result.IsNil())
|
||||||
{
|
{
|
||||||
if (typeof(object) != typeof(T))
|
lastResult = result.ToObject<T>();
|
||||||
{
|
|
||||||
if (result is LuaResult lRes)
|
|
||||||
{
|
|
||||||
if (!lRes.IsNull()) { lastResult = lRes.DynValue().ToObject<T>(); }
|
|
||||||
}
|
|
||||||
else if (result is T cRes && cRes != null)
|
|
||||||
{
|
|
||||||
lastResult = cRes;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (result is LuaResult lRes)
|
|
||||||
{
|
|
||||||
if (!lRes.IsNull()) { lastResult = (T)(object)lRes.DynValue(); }
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
lastResult = (T)result;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
|
if (GameMain.LuaCs.PerformanceCounter.EnablePerformanceCounter)
|
||||||
@@ -239,10 +218,14 @@ namespace Barotrauma
|
|||||||
|
|
||||||
return lastResult;
|
return lastResult;
|
||||||
}
|
}
|
||||||
public object Call(string name, params object[] args) => Call<object>(name, args);
|
|
||||||
|
|
||||||
|
public object Call(string name, params object[] args)
|
||||||
|
{
|
||||||
|
if (name == null) throw new ScriptRuntimeException("Hook.Call: name must not be null.");
|
||||||
|
return Call<object>(name, args);
|
||||||
|
}
|
||||||
|
|
||||||
private static bool PatchPrefix(MethodBase __originalMethod, object[] __args, object __instance)
|
private static bool PatchPrefix(MethodBase __originalMethod, object[] __args, object __instance)
|
||||||
{
|
{
|
||||||
ExecutePatch(__originalMethod, __args, __instance, out object result, HookMethodType.Before);
|
ExecutePatch(__originalMethod, __args, __instance, out object result, HookMethodType.Before);
|
||||||
return result == null;
|
return result == null;
|
||||||
@@ -415,31 +398,22 @@ namespace Barotrauma
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
object[] args = new object[] { __instance }.Concat(__args).ToArray();
|
var args = Enumerable.Empty<object>()
|
||||||
object _result = tuple.Item2(args);
|
.Concat(__args)
|
||||||
|
.Prepend(__instance)
|
||||||
|
.ToArray();
|
||||||
|
var _result = tuple.Item2(args);
|
||||||
|
|
||||||
if (_result == null)
|
if (_result != null && !_result.IsNil())
|
||||||
{
|
{
|
||||||
continue;
|
if (__originalMethod is MethodInfo mi && mi.ReturnType != typeof(void))
|
||||||
}
|
|
||||||
|
|
||||||
if (_result is LuaResult res)
|
|
||||||
{
|
|
||||||
if (!res.IsNull())
|
|
||||||
{
|
{
|
||||||
if (__originalMethod is MethodInfo mi && mi.ReturnType != typeof(void))
|
result = _result.ToObject(mi.ReturnType);
|
||||||
{
|
}
|
||||||
result = res.DynValue().ToObject(mi.ReturnType);
|
else
|
||||||
}
|
{
|
||||||
else
|
result = _result.ToObject();
|
||||||
{
|
|
||||||
result = res.DynValue().ToObject();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = _result;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,17 +58,17 @@ namespace Barotrauma
|
|||||||
var _result = tuple.Item2(__instance, args);
|
var _result = tuple.Item2(__instance, args);
|
||||||
if (_result != null)
|
if (_result != null)
|
||||||
{
|
{
|
||||||
if (_result is LuaResult res)
|
if (_result is DynValue res)
|
||||||
{
|
{
|
||||||
if (!res.IsNull())
|
if (!res.IsNil())
|
||||||
{
|
{
|
||||||
if (__originalMethod is MethodInfo mi && mi.ReturnType != typeof(void))
|
if (__originalMethod is MethodInfo mi && mi.ReturnType != typeof(void))
|
||||||
{
|
{
|
||||||
result = res.DynValue().ToObject(mi.ReturnType);
|
result = res.ToObject(mi.ReturnType);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = res.DynValue().ToObject();
|
result = res.ToObject();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -284,7 +284,7 @@ namespace Barotrauma
|
|||||||
return lua.LoadFile(file, globalContext, codeStringFriendly);
|
return lua.LoadFile(file, globalContext, codeStringFriendly);
|
||||||
}
|
}
|
||||||
|
|
||||||
public object CallLuaFunction(object function, params object[] arguments)
|
public DynValue CallLuaFunction(object function, params object[] arguments)
|
||||||
{
|
{
|
||||||
lock (lua)
|
lock (lua)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user