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<Entity>();
18 RegisterAction<float>();
21 RegisterFunc<Fixture, Vector2, Vector2, float, float>();
22 RegisterFunc<AIObjective, bool>();
24 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(
LuaCsAction), v => (
LuaCsAction)(args =>
26 if (v.Function.OwnerScript ==
Lua)
28 CallLuaFunction(v.Function, args);
32 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(
LuaCsFunc), v => (
LuaCsFunc)(args =>
34 if (v.Function.OwnerScript ==
Lua)
36 return CallLuaFunction(v.Function, args);
41 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(LuaCsCompatPatchFunc), v => (LuaCsCompatPatchFunc)((
self, args) =>
43 if (v.Function.OwnerScript ==
Lua)
45 return CallLuaFunction(v.Function, self, args);
50 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(
LuaCsPatchFunc), v => (
LuaCsPatchFunc)((
self, args) =>
52 if (v.Function.OwnerScript ==
Lua)
54 return CallLuaFunction(v.Function, self, args);
60 DynValue Call(
object function, params
object[] arguments) =>
CallLuaFunction(
function, arguments);
61 void RegisterHandler<T>(Func<Closure, T> converter) => Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(T), v => converter(v.Function));
68 RegisterAction<float, Microsoft.Xna.Framework.Graphics.SpriteBatch>();
69 RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch,
float>();
73 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
76 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
78 () => Call(f)?.CastToBool() ??
default));
80 () => Call(f)?.CastToBool() ??
default));
83 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
86 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
89 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
91 (a1, a2) => Call(f, a1, a2)));
93 () => Call(f)?.ToObject() ??
default));
96 (a1) => Call(f, a1)));
98 (a1) => Call(f, a1)));
101 () => (
float)(Call(f)?.CastToNumber() ??
default)));
104 (a1, a2) => Call(f, a1, a2)));
107 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
109 (a1, a2) => (
float)(Call(f, a1, a2)?.CastToNumber() ??
default)));
112 () => Call(f,
new object[0])?.CastToString() ??
default));
115 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
117 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
119 (a1, a2) => Call(f, a1, a2)));
122 (a1) => Call(f, a1)?.CastToBool() ??
default));
127 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(
Pair<JobPrefab, int>), v =>
132 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<ulong>((Script script, ulong v) =>
134 return DynValue.NewString(v.ToString());
137 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.String, typeof(ulong), v =>
139 return ulong.Parse(v.String);
142 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
143 scriptDataType: DataType.UserData,
144 clrDataType: typeof(sbyte),
145 canConvert: luaValue => luaValue.UserData?.Object is
LuaSByte,
146 converter: luaValue => luaValue.UserData.Object is
LuaSByte v
148 :
throw new ScriptRuntimeException(
"use SByte(value) to pass primitive type 'sbyte' to C#"));
149 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
150 scriptDataType: DataType.UserData,
151 clrDataType: typeof(
byte),
152 canConvert: luaValue => luaValue.UserData?.Object is
LuaByte,
153 converter: luaValue => luaValue.UserData.Object is
LuaByte v
155 :
throw new ScriptRuntimeException(
"use Byte(value) to pass primitive type 'byte' to C#"));
156 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
157 scriptDataType: DataType.UserData,
158 clrDataType: typeof(
short),
159 canConvert: luaValue => luaValue.UserData?.Object is
LuaInt16,
160 converter: luaValue => luaValue.UserData.Object is
LuaInt16 v
162 :
throw new ScriptRuntimeException(
"use Int16(value) to pass primitive type 'short' to C#"));
163 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
164 scriptDataType: DataType.UserData,
165 clrDataType: typeof(ushort),
166 canConvert: luaValue => luaValue.UserData?.Object is
LuaUInt16,
167 converter: luaValue => luaValue.UserData.Object is
LuaUInt16 v
169 :
throw new ScriptRuntimeException(
"use UInt16(value) to pass primitive type 'ushort' to C#"));
170 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
171 scriptDataType: DataType.UserData,
172 clrDataType: typeof(
int),
173 canConvert: luaValue => luaValue.UserData?.Object is
LuaInt32,
174 converter: luaValue => luaValue.UserData.Object is
LuaInt32 v
176 :
throw new ScriptRuntimeException(
"use Int32(value) to pass primitive type 'int' to C#"));
177 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
178 scriptDataType: DataType.UserData,
179 clrDataType: typeof(uint),
180 canConvert: luaValue => luaValue.UserData?.Object is
LuaUInt32,
181 converter: luaValue => luaValue.UserData.Object is
LuaUInt32 v
183 :
throw new ScriptRuntimeException(
"use UInt32(value) to pass primitive type 'uint' to C#"));
184 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
185 scriptDataType: DataType.UserData,
186 clrDataType: typeof(
long),
187 canConvert: luaValue => luaValue.UserData?.Object is
LuaInt64,
188 converter: luaValue => luaValue.UserData.Object is
LuaInt64 v
190 :
throw new ScriptRuntimeException(
"use Int64(value) to pass primitive type 'long' to C#"));
191 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
192 scriptDataType: DataType.UserData,
193 clrDataType: typeof(ulong),
194 canConvert: luaValue => luaValue.UserData?.Object is
LuaUInt64,
195 converter: luaValue => luaValue.UserData.Object is
LuaUInt64 v
197 :
throw new ScriptRuntimeException(
"use UInt64(value) to pass primitive type 'ulong' to C#"));
198 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
199 scriptDataType: DataType.UserData,
200 clrDataType: typeof(
float),
201 canConvert: luaValue => luaValue.UserData?.Object is
LuaSingle,
202 converter: luaValue => luaValue.UserData.Object is
LuaSingle v
204 :
throw new ScriptRuntimeException(
"use Single(value) to pass primitive type 'float' to C#"));
205 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
206 scriptDataType: DataType.UserData,
207 clrDataType: typeof(
double),
208 canConvert: luaValue => luaValue.UserData?.Object is
LuaDouble,
209 converter: luaValue => luaValue.UserData.Object is
LuaDouble v
211 :
throw new ScriptRuntimeException(
"use Double(value) to pass primitive type 'double' to C#"));
213 RegisterOption<Character>(DataType.UserData);
214 RegisterOption<AccountId>(DataType.UserData);
215 RegisterOption<ContentPackageId>(DataType.UserData);
216 RegisterOption<SteamId>(DataType.UserData);
217 RegisterOption<DateTime>(DataType.UserData);
218 RegisterOption<BannedPlayer>(DataType.UserData);
219 RegisterOption<Address>(DataType.UserData);
221 RegisterOption<int>(DataType.Number);
223 RegisterEither<Address, AccountId>();
225 RegisterImmutableArray<FactionPrefab.HireableCharacter>();
228 private void RegisterImmutableArray<T>()
230 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Table, typeof(ImmutableArray<T>), v =>
232 return v.ToObject<T[]>().ToImmutableArray();
236 private void RegisterEither<T1, T2>()
238 DynValue convertEitherIntoDynValue(Either<T1, T2> either)
240 if (either.TryGet(out T1 value1))
242 return UserData.Create(value1);
245 if (either.TryGet(out T2 value2))
247 return UserData.Create(value2);
253 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(EitherT<T1, T2>), (Script v,
object obj) =>
255 if (obj is EitherT<T1, T2> either)
257 return convertEitherIntoDynValue(either);
263 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(EitherU<T1, T2>), (Script v,
object obj) =>
265 if (obj is EitherU<T1, T2> either)
267 return convertEitherIntoDynValue(either);
274 private void RegisterOption<T>(DataType dataType)
276 Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(typeof(Option<T>), (Script v,
object obj) =>
278 if (obj is Option<T> option)
280 if (option.TryUnwrap(out T outValue))
282 return UserData.Create(outValue);
289 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(dataType, typeof(Option<T>), v =>
291 return Option<T>.Some(v.ToObject<T>());
294 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Nil, typeof(Option<T>), v =>
296 return Option<T>.None();
300 private void RegisterAction<T>()
302 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T>), v =>
304 var
function = v.Function;
308 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action<T>), v =>
310 var
function = v.Function;
315 private void RegisterAction<T1, T2>()
317 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action<T1, T2>), v =>
319 var
function = v.Function;
320 return (Action<T1, T2>)((a1, a2) =>
CallLuaFunction(
function, a1, a2));
323 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action<T1, T2>), v =>
325 var
function = v.Function;
326 return (Action<T1, T2>)((a1, a2) =>
CallLuaFunction(
function, a1, a2));
330 private void RegisterAction()
332 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
334 var
function = v.Function;
338 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Action), v =>
340 var
function = v.Function;
345 private void RegisterFunc<T1>()
347 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1>), v =>
349 var
function = v.Function;
350 return () =>
function.Call().ToObject<T1>();
353 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Func<T1>), v =>
355 var
function = v.Function;
356 return () =>
function.Call().ToObject<T1>();
360 private void RegisterFunc<T1, T2>()
362 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2>), v =>
364 var
function = v.Function;
365 return (T1 a) =>
function.Call(a).ToObject<T2>();
368 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.ClrFunction, typeof(Func<T1, T2>), v =>
370 var
function = v.Function;
371 return (T1 a) =>
function.Call(a).ToObject<T2>();
375 private void RegisterFunc<T1, T2, T3, T4, T5>()
377 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2, T3, T4, T5>), v =>
379 var
function = v.Function;
380 return (T1 a, T2 b, T3 c, T4 d) =>
function.Call(a, b, c, d).ToObject<T5>();
383 Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Func<T1, T2, T3, T4, T5>), v =>
385 var
function = v.Function;
386 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)
delegate bool OnColorSelectedHandler(GUIColorPicker component, Color color)
delegate bool SecondaryButtonDownHandler(GUIComponent component, object userData)
GUIComponent that can be used to render custom content on the UI
delegate bool OnSelectedHandler(GUIComponent selected, object obj=null)
delegate bool OnSelectedHandler(GUIComponent component, object obj)
delegate object CheckSelectedHandler()
delegate void OnRearrangedHandler(GUIListBox listBox, object obj)
delegate float ProgressGetterHandler()
delegate LocalizedString TextGetterHandler()
delegate bool OnEnterHandler(GUITextBox textBox, string text)
delegate bool OnTextChangedHandler(GUITextBox textBox, string text)
delegate bool OnSelectedHandler(GUITickBox obj)
DynValue CallLuaFunction(object function, params object[] args)
delegate void LuaCsAction(params object[] args)
delegate void TextBoxEvent(GUITextBox sender, Keys key)
delegate DynValue LuaCsPatchFunc(object instance, LuaCsHook.ParameterTable ptable)
delegate object LuaCsFunc(params object[] args)