From 7c618598402a5516289926652c25f083b679ad35 Mon Sep 17 00:00:00 2001 From: Eero Date: Tue, 30 Dec 2025 03:14:27 +0800 Subject: [PATCH] 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. --- .../SharedSource/LuaCs/Lua/LuaConverters.cs | 126 +++++++++++++++++- 1 file changed, 124 insertions(+), 2 deletions(-) diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaConverters.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaConverters.cs index e2c038ebe..4b7845e85 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaConverters.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaConverters.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using MoonSharp.Interpreter; using Microsoft.Xna.Framework; using FarseerPhysics.Dynamics; @@ -132,7 +134,7 @@ namespace Barotrauma return new Pair((JobPrefab)v.Table.Get(1).ToObject(), (int)v.Table.Get(2).CastToNumber()); }); - Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion((Script script, ulong v) => + Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion((Script script, ulong v) => { return DynValue.NewString(v.ToString()); }); @@ -226,6 +228,28 @@ namespace Barotrauma RegisterEither(); RegisterImmutableArray(); + + + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + RegisterThreadSafeList(); + + RegisterImmutableList(); + RegisterImmutableList(); + RegisterImmutableList(); + + RegisterImmutableHashSet(); + RegisterImmutableHashSet(); + RegisterImmutableHashSet(); + + RegisterImmutableDictionary(); + RegisterImmutableDictionary>(); } private void RegisterImmutableArray() @@ -332,7 +356,7 @@ namespace Barotrauma private void RegisterAction() { - Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => + Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => { var function = v.Function; return (Action)(() => CallLuaFunction(function)); @@ -389,5 +413,103 @@ namespace Barotrauma return (T1 a, T2 b, T3 c, T4 d) => function.Call(a, b, c, d).ToObject(); }); } + + private void RegisterThreadSafeList() where TList : IEnumerable + { + Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion( + typeof(TList), + (Script script, object obj) => + { + if (obj is IEnumerable 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() + { + Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion( + typeof(ImmutableList), + (Script script, object obj) => + { + if (obj is ImmutableList 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 + Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion( + DataType.Table, + typeof(ImmutableList), + v => v.ToObject().ToImmutableList() + ); + } + + private void RegisterImmutableHashSet() + { + Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion( + typeof(ImmutableHashSet), + (Script script, object obj) => + { + if (obj is ImmutableHashSet 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 + Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion( + DataType.Table, + typeof(ImmutableHashSet), + v => v.ToObject().ToImmutableHashSet() + ); + } + + private void RegisterImmutableDictionary() + { + Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion( + typeof(ImmutableDictionary), + (Script script, object obj) => + { + if (obj is ImmutableDictionary 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; + } + ); + } } }