using System; using System.Collections.Generic; using System.Text; using MoonSharp.Interpreter; using Microsoft.Xna.Framework; using FarseerPhysics.Dynamics; namespace Barotrauma { public static class LuaCustomConverters { public static void RegisterAll() { RegisterAction(); RegisterAction(); RegisterAction(); RegisterAction(); Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func), v => { var function = v.Function; return (Func)((Fixture a, Vector2 b, Vector2 c, float d) => new LuaResult(function.Call(a, b, c, d)).Float()); }); #if CLIENT RegisterAction(); RegisterAction(); { object Call(object function, params object[] arguments) => GameMain.Lua.CallFunction(function, arguments); void RegisterHandler(Func converter) { Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function)); } RegisterHandler(f => (GUIComponent.SecondaryButtonDownHandler)((GUIComponent a1, object a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUIButton.OnClickedHandler)((GUIButton a1, object a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUIButton.OnButtonDownHandler)(() => new LuaResult(Call(f)).Bool())); RegisterHandler(f => (GUIButton.OnPressedHandler)(() => new LuaResult(Call(f)).Bool())); RegisterHandler(f => (GUIColorPicker.OnColorSelectedHandler)((GUIColorPicker a1, Color a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUIDropDown.OnSelectedHandler)((GUIComponent a1, object a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUIListBox.OnSelectedHandler)((GUIComponent a1, object a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUIListBox.OnRearrangedHandler)((GUIListBox a1, object a2) => Call(f, a1, a2))); RegisterHandler(f => (GUIListBox.CheckSelectedHandler)(() => new LuaResult(Call(f)).Object())); RegisterHandler(f => (GUINumberInput.OnValueChangedHandler)((GUINumberInput a1) => Call(f, a1))); RegisterHandler(f => (GUIProgressBar.ProgressGetterHandler)(() => new LuaResult(Call(f)).Float())); RegisterHandler(f => (GUIRadioButtonGroup.RadioButtonGroupDelegate)((GUIRadioButtonGroup a1, int? a2) => Call(f, a1, a2))); RegisterHandler(f => (GUIScrollBar.OnMovedHandler)((GUIScrollBar a1, float a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUIScrollBar.ScrollConversion)((GUIScrollBar a1, float a2) => new LuaResult(Call(f, a1, a2)).Float())); RegisterHandler(f => (GUITextBlock.TextGetterHandler)(() => new LuaResult(Call(f, new object[] { })).String())); RegisterHandler(f => (GUITextBox.OnEnterHandler)((GUITextBox a1, string a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (GUITextBox.OnTextChangedHandler)((GUITextBox a1, string a2) => new LuaResult(Call(f, a1, a2)).Bool())); RegisterHandler(f => (TextBoxEvent)((GUITextBox a1, Microsoft.Xna.Framework.Input.Keys a2) => Call(f, a1, a2))); RegisterHandler(f => (GUITickBox.OnSelectedHandler)((GUITickBox a1) => new LuaResult(Call(f, a1)).Bool())); } #endif Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(Pair), v => { return new Pair((JobPrefab)v.Table.Get(1).ToObject(), (int)v.Table.Get(2).CastToNumber()); }); Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion((Script script, UInt64 v) => { 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() { Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => { var function = v.Function; return (Action)(p => GameMain.Lua.CallFunction(function, p)); }); } public static void RegisterAction() { Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => { var function = v.Function; return (Action)((a1, a2) => GameMain.Lua.CallFunction(function, a1, a2)); }); } public static void RegisterAction() { Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => { var function = v.Function; return (Action)(() => GameMain.Lua.CallFunction(function)); }); } } }