CBT2.0.2 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.
This commit is contained in:
@@ -1,4 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using MoonSharp.Interpreter;
|
using MoonSharp.Interpreter;
|
||||||
using Microsoft.Xna.Framework;
|
using Microsoft.Xna.Framework;
|
||||||
using FarseerPhysics.Dynamics;
|
using FarseerPhysics.Dynamics;
|
||||||
@@ -226,6 +228,28 @@ namespace Barotrauma
|
|||||||
RegisterEither<Address, AccountId>();
|
RegisterEither<Address, AccountId>();
|
||||||
|
|
||||||
RegisterImmutableArray<FactionPrefab.HireableCharacter>();
|
RegisterImmutableArray<FactionPrefab.HireableCharacter>();
|
||||||
|
|
||||||
|
|
||||||
|
RegisterThreadSafeList<Character, ThreadSafeCharacterList>();
|
||||||
|
RegisterThreadSafeList<WayPoint, ThreadSafeWayPointList>();
|
||||||
|
RegisterThreadSafeList<Submarine, ThreadSafeSubmarineList>();
|
||||||
|
RegisterThreadSafeList<MapEntity, ThreadSafeMapEntityList>();
|
||||||
|
RegisterThreadSafeList<Hull, ThreadSafeHullList>();
|
||||||
|
RegisterThreadSafeList<Gap, ThreadSafeGapList>();
|
||||||
|
RegisterThreadSafeList<Structure, ThreadSafeStructureList>();
|
||||||
|
RegisterThreadSafeList<AITarget, ThreadSafeAITargetList>();
|
||||||
|
RegisterThreadSafeList<PhysicsBody, ThreadSafePhysicsBodyList>();
|
||||||
|
|
||||||
|
RegisterImmutableList<Event>();
|
||||||
|
RegisterImmutableList<EventSet>();
|
||||||
|
RegisterImmutableList<Sprite>();
|
||||||
|
|
||||||
|
RegisterImmutableHashSet<Event>();
|
||||||
|
RegisterImmutableHashSet<Identifier>();
|
||||||
|
RegisterImmutableHashSet<LocationConnection>();
|
||||||
|
|
||||||
|
RegisterImmutableDictionary<Identifier, EventPrefab>();
|
||||||
|
RegisterImmutableDictionary<EventSet, ImmutableList<Event>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RegisterImmutableArray<T>()
|
private void RegisterImmutableArray<T>()
|
||||||
@@ -389,5 +413,103 @@ namespace Barotrauma
|
|||||||
return (T1 a, T2 b, T3 c, T4 d) => function.Call(a, b, c, d).ToObject<T5>();
|
return (T1 a, T2 b, T3 c, T4 d) => function.Call(a, b, c, d).ToObject<T5>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void RegisterThreadSafeList<TItem, TList>() where TList : IEnumerable<TItem>
|
||||||
|
{
|
||||||
|
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
|
||||||
|
typeof(TList),
|
||||||
|
(Script script, object obj) =>
|
||||||
|
{
|
||||||
|
if (obj is IEnumerable<TItem> enumerable)
|
||||||
|
{
|
||||||
|
var table = new Table(script);
|
||||||
|
int i = 1;
|
||||||
|
foreach (var item in enumerable)
|
||||||
|
{
|
||||||
|
table[i++] = DynValue.FromObject(script, item);
|
||||||
|
}
|
||||||
|
return DynValue.NewTable(table);
|
||||||
|
}
|
||||||
|
return DynValue.Nil;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RegisterImmutableList<T>()
|
||||||
|
{
|
||||||
|
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
|
||||||
|
typeof(ImmutableList<T>),
|
||||||
|
(Script script, object obj) =>
|
||||||
|
{
|
||||||
|
if (obj is ImmutableList<T> list)
|
||||||
|
{
|
||||||
|
var table = new Table(script);
|
||||||
|
int i = 1;
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
table[i++] = DynValue.FromObject(script, item);
|
||||||
|
}
|
||||||
|
return DynValue.NewTable(table);
|
||||||
|
}
|
||||||
|
return DynValue.Nil;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Lua table -> ImmutableList<T>
|
||||||
|
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||||
|
DataType.Table,
|
||||||
|
typeof(ImmutableList<T>),
|
||||||
|
v => v.ToObject<T[]>().ToImmutableList()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RegisterImmutableHashSet<T>()
|
||||||
|
{
|
||||||
|
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
|
||||||
|
typeof(ImmutableHashSet<T>),
|
||||||
|
(Script script, object obj) =>
|
||||||
|
{
|
||||||
|
if (obj is ImmutableHashSet<T> set)
|
||||||
|
{
|
||||||
|
var table = new Table(script);
|
||||||
|
int i = 1;
|
||||||
|
foreach (var item in set)
|
||||||
|
{
|
||||||
|
table[i++] = DynValue.FromObject(script, item);
|
||||||
|
}
|
||||||
|
return DynValue.NewTable(table);
|
||||||
|
}
|
||||||
|
return DynValue.Nil;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Lua table -> ImmutableHashSet<T>
|
||||||
|
Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(
|
||||||
|
DataType.Table,
|
||||||
|
typeof(ImmutableHashSet<T>),
|
||||||
|
v => v.ToObject<T[]>().ToImmutableHashSet()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RegisterImmutableDictionary<TKey, TValue>()
|
||||||
|
{
|
||||||
|
Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion(
|
||||||
|
typeof(ImmutableDictionary<TKey, TValue>),
|
||||||
|
(Script script, object obj) =>
|
||||||
|
{
|
||||||
|
if (obj is ImmutableDictionary<TKey, TValue> dict)
|
||||||
|
{
|
||||||
|
var table = new Table(script);
|
||||||
|
foreach (var kvp in dict)
|
||||||
|
{
|
||||||
|
table[DynValue.FromObject(script, kvp.Key)] =
|
||||||
|
DynValue.FromObject(script, kvp.Value);
|
||||||
|
}
|
||||||
|
return DynValue.NewTable(table);
|
||||||
|
}
|
||||||
|
return DynValue.Nil;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user