2 using MoonSharp.Interpreter;
3 using Microsoft.Xna.Framework;
4 using FarseerPhysics.Dynamics;
5 using LuaCsCompatPatchFunc =
Barotrauma.LuaCsPatch;
7 using System.Collections.Immutable;
13 private void RegisterLuaConverters()
15 RegisterAction<Item>();
16 RegisterAction<Character>();
17 RegisterAction<Character, Character>();
18 RegisterAction<Entity>();
19 RegisterAction<float>();
22 RegisterFunc<Fixture, Vector2, Vector2, float, float>();
23 RegisterFunc<AIObjective, bool>();
25 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(
LuaCsAction), v => (
LuaCsAction)(args =>
27 if (v.Function.OwnerScript ==
Lua)
29 CallLuaFunction(v.Function, args);
33 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(
LuaCsFunc), v => (
LuaCsFunc)(args =>
35 if (v.Function.OwnerScript ==
Lua)
37 return CallLuaFunction(v.Function, args);
42 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsCompatPatchFunc), v => (LuaCsCompatPatchFunc)((
self, args) =>
44 if (v.Function.OwnerScript ==
Lua)
46 return CallLuaFunction(v.Function, self, args);
51 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(
LuaCsPatchFunc), v => (
LuaCsPatchFunc)((
self, args) =>
53 if (v.Function.OwnerScript ==
Lua)
55 return CallLuaFunction(v.Function, self, args);
61 DynValue Call(
object function, params
object[] arguments) =>
CallLuaFunction(
function, arguments);
62 void RegisterHandler<T>(Func<Closure, T> converter) => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function));
68 RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch, GUICustomComponent>();
69 RegisterAction<float, Microsoft.Xna.Framework.Graphics.SpriteBatch>();
70 RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch,
float>();
73 RegisterHandler(f => (GUIComponent.SecondaryButtonDownHandler)(
74 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
76 RegisterHandler(f => (GUIButton.OnClickedHandler)(
77 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
78 RegisterHandler(f => (GUIButton.OnButtonDownHandler)(
79 () => Call(f)?.CastToBool() ??
default));
80 RegisterHandler(f => (GUIButton.OnPressedHandler)(
81 () => Call(f)?.CastToBool() ??
default));
83 RegisterHandler(f => (GUIColorPicker.OnColorSelectedHandler)(
84 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
86 RegisterHandler(f => (GUIDropDown.OnSelectedHandler)(
87 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
89 RegisterHandler(f => (GUIListBox.OnSelectedHandler)(
90 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
91 RegisterHandler(f => (GUIListBox.OnRearrangedHandler)(
92 (a1, a2) => Call(f, a1, a2)));
93 RegisterHandler(f => (GUIListBox.CheckSelectedHandler)(
94 () => Call(f)?.ToObject() ??
default));
96 RegisterHandler(f => (GUINumberInput.OnValueEnteredHandler)(
97 (a1) => Call(f, a1)));
98 RegisterHandler(f => (GUINumberInput.OnValueChangedHandler)(
99 (a1) => Call(f, a1)));
101 RegisterHandler(f => (GUIProgressBar.ProgressGetterHandler)(
102 () => (
float)(Call(f)?.CastToNumber() ??
default)));
104 RegisterHandler(f => (GUIRadioButtonGroup.RadioButtonGroupDelegate)(
105 (a1, a2) => Call(f, a1, a2)));
107 RegisterHandler(f => (GUIScrollBar.OnMovedHandler)(
108 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
109 RegisterHandler(f => (GUIScrollBar.ScrollConversion)(
110 (a1, a2) => (
float)(Call(f, a1, a2)?.CastToNumber() ??
default)));
112 RegisterHandler(f => (GUITextBlock.TextGetterHandler)(
113 () => Call(f,
new object[0])?.CastToString() ??
default));
115 RegisterHandler(f => (GUITextBox.OnEnterHandler)(
116 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
117 RegisterHandler(f => (GUITextBox.OnTextChangedHandler)(
118 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
119 RegisterHandler(f => (TextBoxEvent)(
120 (a1, a2) => Call(f, a1, a2)));
122 RegisterHandler(f => (GUITickBox.OnSelectedHandler)(
123 (a1) => Call(f, a1)?.CastToBool() ??
default));
128 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(
Pair<JobPrefab, int>), v =>
133 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<ulong>((Script script, ulong v) =>
135 return DynValue.NewString(v.ToString());
138 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.String, typeof(ulong), v =>
140 return ulong.Parse(v.String);
143 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
144 scriptDataType: DataType.UserData,
145 clrDataType: typeof(sbyte),
146 canConvert: luaValue => luaValue.UserData?.Object is
LuaSByte,
147 converter: luaValue => luaValue.UserData.Object is
LuaSByte v
149 :
throw new ScriptRuntimeException(
"use SByte(value) to pass primitive type 'sbyte' to C#"));
150 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
151 scriptDataType: DataType.UserData,
152 clrDataType: typeof(
byte),
153 canConvert: luaValue => luaValue.UserData?.Object is
LuaByte,
154 converter: luaValue => luaValue.UserData.Object is
LuaByte v
156 :
throw new ScriptRuntimeException(
"use Byte(value) to pass primitive type 'byte' to C#"));
157 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
158 scriptDataType: DataType.UserData,
159 clrDataType: typeof(
short),
160 canConvert: luaValue => luaValue.UserData?.Object is
LuaInt16,
161 converter: luaValue => luaValue.UserData.Object is
LuaInt16 v
163 :
throw new ScriptRuntimeException(
"use Int16(value) to pass primitive type 'short' to C#"));
164 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
165 scriptDataType: DataType.UserData,
166 clrDataType: typeof(ushort),
167 canConvert: luaValue => luaValue.UserData?.Object is
LuaUInt16,
168 converter: luaValue => luaValue.UserData.Object is
LuaUInt16 v
170 :
throw new ScriptRuntimeException(
"use UInt16(value) to pass primitive type 'ushort' to C#"));
171 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
172 scriptDataType: DataType.UserData,
173 clrDataType: typeof(
int),
174 canConvert: luaValue => luaValue.UserData?.Object is
LuaInt32,
175 converter: luaValue => luaValue.UserData.Object is
LuaInt32 v
177 :
throw new ScriptRuntimeException(
"use Int32(value) to pass primitive type 'int' to C#"));
178 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
179 scriptDataType: DataType.UserData,
180 clrDataType: typeof(uint),
181 canConvert: luaValue => luaValue.UserData?.Object is
LuaUInt32,
182 converter: luaValue => luaValue.UserData.Object is
LuaUInt32 v
184 :
throw new ScriptRuntimeException(
"use UInt32(value) to pass primitive type 'uint' to C#"));
185 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
186 scriptDataType: DataType.UserData,
187 clrDataType: typeof(
long),
188 canConvert: luaValue => luaValue.UserData?.Object is
LuaInt64,
189 converter: luaValue => luaValue.UserData.Object is
LuaInt64 v
191 :
throw new ScriptRuntimeException(
"use Int64(value) to pass primitive type 'long' to C#"));
192 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
193 scriptDataType: DataType.UserData,
194 clrDataType: typeof(ulong),
195 canConvert: luaValue => luaValue.UserData?.Object is
LuaUInt64,
196 converter: luaValue => luaValue.UserData.Object is
LuaUInt64 v
198 :
throw new ScriptRuntimeException(
"use UInt64(value) to pass primitive type 'ulong' to C#"));
199 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
200 scriptDataType: DataType.UserData,
201 clrDataType: typeof(
float),
202 canConvert: luaValue => luaValue.UserData?.Object is
LuaSingle,
203 converter: luaValue => luaValue.UserData.Object is
LuaSingle v
205 :
throw new ScriptRuntimeException(
"use Single(value) to pass primitive type 'float' to C#"));
206 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
207 scriptDataType: DataType.UserData,
208 clrDataType: typeof(
double),
209 canConvert: luaValue => luaValue.UserData?.Object is
LuaDouble,
210 converter: luaValue => luaValue.UserData.Object is
LuaDouble v
212 :
throw new ScriptRuntimeException(
"use Double(value) to pass primitive type 'double' to C#"));
214 RegisterOption<Character>(DataType.UserData);
215 RegisterOption<AccountId>(DataType.UserData);
216 RegisterOption<ContentPackageId>(DataType.UserData);
217 RegisterOption<SteamId>(DataType.UserData);
218 RegisterOption<DateTime>(DataType.UserData);
219 RegisterOption<BannedPlayer>(DataType.UserData);
220 RegisterOption<Address>(DataType.UserData);
222 RegisterOption<int>(DataType.Number);
224 RegisterEither<Address, AccountId>();
226 RegisterImmutableArray<FactionPrefab.HireableCharacter>();
229 private void RegisterImmutableArray<T>()
231 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(ImmutableArray<T>), v =>
233 return v.ToObject<T[]>().ToImmutableArray();
237 private void RegisterEither<T1, T2>()
239 DynValue convertEitherIntoDynValue(Either<T1, T2> either)
241 if (either.TryGet(out T1 value1))
243 return UserData.Create(value1);
246 if (either.TryGet(out T2 value2))
248 return UserData.Create(value2);
254 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(EitherT<T1, T2>), (Script v,
object obj) =>
256 if (obj is EitherT<T1, T2> either)
258 return convertEitherIntoDynValue(either);
264 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(EitherU<T1, T2>), (Script v,
object obj) =>
266 if (obj is EitherU<T1, T2> either)
268 return convertEitherIntoDynValue(either);
275 private void RegisterOption<T>(DataType dataType)
277 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(Option<T>), (Script v,
object obj) =>
279 if (obj is Option<T> option)
281 if (option.TryUnwrap(out T outValue))
283 return UserData.Create(outValue);
290 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(dataType, typeof(Option<T>), v =>
292 return Option<T>.Some(v.ToObject<T>());
295 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Nil, typeof(Option<T>), v =>
297 return Option<T>.None();
301 private void RegisterAction<T>()
303 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), v =>
305 var
function = v.Function;
309 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action<T>), v =>
311 var
function = v.Function;
316 private void RegisterAction<T1, T2>()
318 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T1, T2>), v =>
320 var
function = v.Function;
321 return (Action<T1, T2>)((a1, a2) =>
CallLuaFunction(
function, a1, a2));
324 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action<T1, T2>), v =>
326 var
function = v.Function;
327 return (Action<T1, T2>)((a1, a2) =>
CallLuaFunction(
function, a1, a2));
331 private void RegisterAction()
333 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
335 var
function = v.Function;
339 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action), v =>
341 var
function = v.Function;
346 private void RegisterFunc<T1>()
348 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1>), v =>
350 var
function = v.Function;
351 return () =>
function.Call().ToObject<T1>();
354 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Func<T1>), v =>
356 var
function = v.Function;
357 return () =>
function.Call().ToObject<T1>();
361 private void RegisterFunc<T1, T2>()
363 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2>), v =>
365 var
function = v.Function;
366 return (T1 a) =>
function.Call(a).ToObject<T2>();
369 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Func<T1, T2>), v =>
371 var
function = v.Function;
372 return (T1 a) =>
function.Call(a).ToObject<T2>();
376 private void RegisterFunc<T1, T2, T3, T4, T5>()
378 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2, T3, T4, T5>), v =>
380 var
function = v.Function;
381 return (T1 a, T2 b, T3 c, T4 d) =>
function.Call(a, b, c, d).ToObject<T5>();
384 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2, T3, T4, T5>), v =>
386 var
function = v.Function;
387 return (T1 a, T2 b, T3 c, T4 d) =>
function.Call(a, b, c, d).ToObject<T5>();
delegate void OnDeathHandler(Character character, CauseOfDeath causeOfDeath)
delegate void OnAttackedHandler(Character attacker, AttackResult attackResult)
DynValue CallLuaFunction(object function, params object[] args)
delegate void LuaCsAction(params object[] args)
delegate DynValue LuaCsPatchFunc(object instance, LuaCsHook.ParameterTable ptable)
delegate object LuaCsFunc(params object[] args)