Skip to content

Commit a47de17

Browse files
committed
Add Lua converters for thread-safe and immutable collections
Implemented custom Lua converters for various thread-safe lists, ImmutableList, ImmutableHashSet, and ImmutableDictionary types. This allows seamless conversion between C# collections and Lua tables, improving Lua scripting integration with these collection types.
1 parent 9474f76 commit a47de17

1 file changed

Lines changed: 124 additions & 2 deletions

File tree

Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaConverters.cs

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using MoonSharp.Interpreter;
35
using Microsoft.Xna.Framework;
46
using FarseerPhysics.Dynamics;
@@ -132,7 +134,7 @@ private void RegisterLuaConverters()
132134
return new Pair<JobPrefab, int>((JobPrefab)v.Table.Get(1).ToObject(), (int)v.Table.Get(2).CastToNumber());
133135
});
134136

135-
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<ulong>((Script script, ulong v) =>
137+
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<ulong>((Script script, ulong v) =>
136138
{
137139
return DynValue.NewString(v.ToString());
138140
});
@@ -226,6 +228,28 @@ private void RegisterLuaConverters()
226228
RegisterEither<Address, AccountId>();
227229

228230
RegisterImmutableArray<FactionPrefab.HireableCharacter>();
231+
232+
233+
RegisterThreadSafeList<Character, ThreadSafeCharacterList>();
234+
RegisterThreadSafeList<WayPoint, ThreadSafeWayPointList>();
235+
RegisterThreadSafeList<Submarine, ThreadSafeSubmarineList>();
236+
RegisterThreadSafeList<MapEntity, ThreadSafeMapEntityList>();
237+
RegisterThreadSafeList<Hull, ThreadSafeHullList>();
238+
RegisterThreadSafeList<Gap, ThreadSafeGapList>();
239+
RegisterThreadSafeList<Structure, ThreadSafeStructureList>();
240+
RegisterThreadSafeList<AITarget, ThreadSafeAITargetList>();
241+
RegisterThreadSafeList<PhysicsBody, ThreadSafePhysicsBodyList>();
242+
243+
RegisterImmutableList<Event>();
244+
RegisterImmutableList<EventSet>();
245+
RegisterImmutableList<Sprite>();
246+
247+
RegisterImmutableHashSet<Event>();
248+
RegisterImmutableHashSet<Identifier>();
249+
RegisterImmutableHashSet<LocationConnection>();
250+
251+
RegisterImmutableDictionary<Identifier, EventPrefab>();
252+
RegisterImmutableDictionary<EventSet, ImmutableList<Event>>();
229253
}
230254

231255
private void RegisterImmutableArray<T>()
@@ -332,7 +356,7 @@ private void RegisterAction<T1, T2>()
332356

333357
private void RegisterAction()
334358
{
335-
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
359+
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v =>
336360
{
337361
var function = v.Function;
338362
return (Action)(() => CallLuaFunction(function));
@@ -389,5 +413,103 @@ private void RegisterFunc<T1, T2, T3, T4, T5>()
389413
return (T1 a, T2 b, T3 c, T4 d) => function.Call(a, b, c, d).ToObject<T5>();
390414
});
391415
}
416+
417+
private void RegisterThreadSafeList<TItem, TList>() where TList : IEnumerable<TItem>
418+
{
419+
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
420+
typeof(TList),
421+
(Script script, object obj) =>
422+
{
423+
if (obj is IEnumerable<TItem> enumerable)
424+
{
425+
var table = new Table(script);
426+
int i = 1;
427+
foreach (var item in enumerable)
428+
{
429+
table[i++] = DynValue.FromObject(script, item);
430+
}
431+
return DynValue.NewTable(table);
432+
}
433+
return DynValue.Nil;
434+
}
435+
);
436+
}
437+
438+
private void RegisterImmutableList<T>()
439+
{
440+
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
441+
typeof(ImmutableList<T>),
442+
(Script script, object obj) =>
443+
{
444+
if (obj is ImmutableList<T> list)
445+
{
446+
var table = new Table(script);
447+
int i = 1;
448+
foreach (var item in list)
449+
{
450+
table[i++] = DynValue.FromObject(script, item);
451+
}
452+
return DynValue.NewTable(table);
453+
}
454+
return DynValue.Nil;
455+
}
456+
);
457+
458+
// Lua table -> ImmutableList<T>
459+
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
460+
DataType.Table,
461+
typeof(ImmutableList<T>),
462+
v => v.ToObject<T[]>().ToImmutableList()
463+
);
464+
}
465+
466+
private void RegisterImmutableHashSet<T>()
467+
{
468+
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
469+
typeof(ImmutableHashSet<T>),
470+
(Script script, object obj) =>
471+
{
472+
if (obj is ImmutableHashSet<T> set)
473+
{
474+
var table = new Table(script);
475+
int i = 1;
476+
foreach (var item in set)
477+
{
478+
table[i++] = DynValue.FromObject(script, item);
479+
}
480+
return DynValue.NewTable(table);
481+
}
482+
return DynValue.Nil;
483+
}
484+
);
485+
486+
// Lua table -> ImmutableHashSet<T>
487+
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
488+
DataType.Table,
489+
typeof(ImmutableHashSet<T>),
490+
v => v.ToObject<T[]>().ToImmutableHashSet()
491+
);
492+
}
493+
494+
private void RegisterImmutableDictionary<TKey, TValue>()
495+
{
496+
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
497+
typeof(ImmutableDictionary<TKey, TValue>),
498+
(Script script, object obj) =>
499+
{
500+
if (obj is ImmutableDictionary<TKey, TValue> dict)
501+
{
502+
var table = new Table(script);
503+
foreach (var kvp in dict)
504+
{
505+
table[DynValue.FromObject(script, kvp.Key)] =
506+
DynValue.FromObject(script, kvp.Value);
507+
}
508+
return DynValue.NewTable(table);
509+
}
510+
return DynValue.Nil;
511+
}
512+
);
513+
}
392514
}
393515
}

0 commit comments

Comments
 (0)