|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
2 | 4 | using MoonSharp.Interpreter; |
3 | 5 | using Microsoft.Xna.Framework; |
4 | 6 | using FarseerPhysics.Dynamics; |
@@ -132,7 +134,7 @@ private void RegisterLuaConverters() |
132 | 134 | return new Pair<JobPrefab, int>((JobPrefab)v.Table.Get(1).ToObject(), (int)v.Table.Get(2).CastToNumber()); |
133 | 135 | }); |
134 | 136 |
|
135 | | - Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<ulong>((Script script, ulong v) => |
| 137 | + Script.GlobalOptions.CustomConverters.SetClrToScriptCustomConversion<ulong>((Script script, ulong v) => |
136 | 138 | { |
137 | 139 | return DynValue.NewString(v.ToString()); |
138 | 140 | }); |
@@ -226,6 +228,28 @@ private void RegisterLuaConverters() |
226 | 228 | RegisterEither<Address, AccountId>(); |
227 | 229 |
|
228 | 230 | 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>>(); |
229 | 253 | } |
230 | 254 |
|
231 | 255 | private void RegisterImmutableArray<T>() |
@@ -332,7 +356,7 @@ private void RegisterAction<T1, T2>() |
332 | 356 |
|
333 | 357 | private void RegisterAction() |
334 | 358 | { |
335 | | - Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => |
| 359 | + Script.GlobalOptions.CustomConverters.SetScriptToClrCustomConversion(DataType.Function, typeof(Action), v => |
336 | 360 | { |
337 | 361 | var function = v.Function; |
338 | 362 | return (Action)(() => CallLuaFunction(function)); |
@@ -389,5 +413,103 @@ private void RegisterFunc<T1, T2, T3, T4, T5>() |
389 | 413 | return (T1 a, T2 b, T3 c, T4 d) => function.Call(a, b, c, d).ToObject<T5>(); |
390 | 414 | }); |
391 | 415 | } |
| 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 | + } |
392 | 514 | } |
393 | 515 | } |
0 commit comments