A stable modding interface between Techblox and mods https://mod.exmods.org/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.4KB

  1. using System;
  2. using System.Collections.Generic;
  3. using HarmonyLib;
  4. using RobocraftX.Schedulers;
  5. using Svelto.ECS;
  6. using Svelto.ECS.Hybrid;
  7. using Svelto.Tasks;
  8. using Svelto.Tasks.Lean;
  9. using TechbloxModdingAPI.Blocks.Engines;
  10. using TechbloxModdingAPI.Common.Engines;
  11. using TechbloxModdingAPI.Utility;
  12. using TechbloxModdingAPI.Utility.ECS;
  13. namespace TechbloxModdingAPI.Common;
  14. public class EcsObjectBaseEngine : IFactoryEngine, IFunEngine
  15. {
  16. public void Ready()
  17. {
  18. }
  19. public EntitiesDB entitiesDB { get; set; }
  20. public void Dispose()
  21. {
  22. }
  23. public EntityReference GetEntityReference(EGID egid)
  24. {
  25. return entitiesDB is not null && egid != default ? egid.ToEntityReference(entitiesDB) : EntityReference.Invalid;
  26. }
  27. public EGID GetEgid(EntityReference reference)
  28. {
  29. return entitiesDB is not null && reference.ToEGID(entitiesDB, out var egid) ? egid : default;
  30. }
  31. public OptionalRef<T> GetComponentOptional<T>(EcsObjectBase obj) where T : unmanaged, IEntityComponent
  32. {
  33. return entitiesDB.QueryEntityOptional<T>(obj);
  34. }
  35. public ref T GetComponent<T>(EcsObjectBase obj) where T : unmanaged, IEntityComponent
  36. {
  37. #if DEBUG
  38. if (entitiesDB.Exists<T>(obj.Id) && obj.InitData.Valid)
  39. throw new ArgumentException("The block exists but the init data has not been removed!");
  40. #endif
  41. return ref entitiesDB.QueryEntityOrDefault<T>(obj);
  42. }
  43. public ref T GetViewComponent<T>(EcsObjectBase obj) where T : struct, IEntityViewComponent
  44. {
  45. return ref entitiesDB.QueryEntityOrDefault<T>(obj);
  46. }
  47. public object GetComponent(EcsObjectBase obj, Type type, string name)
  48. {
  49. var opt = AccessTools.Method(typeof(NativeApiExtensions), "QueryEntityOptional",
  50. new[] { typeof(EntitiesDB), typeof(EcsObjectBase), typeof(ExclusiveGroupStruct) }, new[] { type })
  51. .Invoke(null, new object[] { entitiesDB, obj, null });
  52. var str = AccessTools.Property(opt.GetType(), "Value").GetValue(opt);
  53. return AccessTools.Field(str.GetType(), name).GetValue(str);
  54. }
  55. public void SetComponent(EcsObjectBase obj, Type type, string name, object value)
  56. {
  57. var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
  58. .Invoke(this, new object[] { obj });
  59. var prop = AccessTools.Property(opt.GetType(), "Value");
  60. var str = prop.GetValue(opt);
  61. AccessTools.Field(str.GetType(), name).SetValue(str, value);
  62. prop.SetValue(opt, str);
  63. }
  64. private readonly Dictionary<EcsObjectBase, Action<EcsObjectBase>> _waitingForSubmission = new();
  65. public void TrackNewEntity(EcsObjectBase obj, Action<EcsObjectBase> done)
  66. {
  67. if (_waitingForSubmission.ContainsKey(obj))
  68. throw new InvalidOperationException("Something has gone horribly wrong here");
  69. _waitingForSubmission.Add(obj, done);
  70. WaitUntilEntitySubmission().RunOn(ClientLean.UIScheduler); // TODO: Pick the right scheduler
  71. }
  72. private IEnumerator<TaskContract> WaitUntilEntitySubmission()
  73. {
  74. // TODO: Get the scheduler instance based on the engine (inject in engine manager)
  75. yield return new WaitForSubmissionEnumerator(FullGameFields._mainGameEnginesRoot.scheduler).Continue();
  76. foreach (var (obj, done) in _waitingForSubmission) done(obj);
  77. }
  78. public IEntityFactory Factory { get; set; }
  79. public IEntityFunctions Functions { get; set; }
  80. }