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));
69 RegisterAction<float, Microsoft.Xna.Framework.Graphics.SpriteBatch>();
70 RegisterAction<Microsoft.Xna.Framework.Graphics.SpriteBatch,
float>();
74 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
77 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
79 () => Call(f)?.CastToBool() ??
default));
81 () => Call(f)?.CastToBool() ??
default));
84 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
87 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
90 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
92 (a1, a2) => Call(f, a1, a2)));
94 () => Call(f)?.ToObject() ??
default));
97 (a1) => Call(f, a1)));
99 (a1) => Call(f, a1)));
102 () => (
float)(Call(f)?.CastToNumber() ??
default)));
105 (a1, a2) => Call(f, a1, a2)));
108 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
110 (a1, a2) => (
float)(Call(f, a1, a2)?.CastToNumber() ??
default)));
113 () => Call(f,
new object[0])?.CastToString() ??
default));
116 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
118 (a1, a2) => Call(f, a1, a2)?.CastToBool() ??
default));
120 (a1, a2) => Call(f, a1, a2)));
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)
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)