A stable modding interface between Techblox and mods https://mod.exmods.org/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

71 строка
2.4KB

  1. using System;
  2. using HarmonyLib;
  3. using Svelto.ECS;
  4. using Svelto.ECS.Hybrid;
  5. using TechbloxModdingAPI.Blocks.Engines;
  6. using TechbloxModdingAPI.Common.Engines;
  7. using TechbloxModdingAPI.Utility;
  8. using TechbloxModdingAPI.Utility.ECS;
  9. namespace TechbloxModdingAPI.Common;
  10. public class EcsObjectBaseEngine : IApiEngine
  11. {
  12. public void Ready()
  13. {
  14. }
  15. public EntitiesDB entitiesDB { get; set; }
  16. public void Dispose()
  17. {
  18. }
  19. public EntityReference GetEntityReference(EGID egid)
  20. {
  21. return entitiesDB is not null && egid != default ? egid.ToEntityReference(entitiesDB) : EntityReference.Invalid;
  22. }
  23. public EGID GetEgid(EntityReference reference)
  24. {
  25. return entitiesDB is not null && reference.ToEGID(entitiesDB, out var egid) ? egid : default;
  26. }
  27. public OptionalRef<T> GetComponentOptional<T>(EcsObjectBase obj) where T : unmanaged, IEntityComponent
  28. {
  29. return entitiesDB.QueryEntityOptional<T>(obj);
  30. }
  31. public ref T GetComponent<T>(EcsObjectBase obj) where T : unmanaged, IEntityComponent
  32. {
  33. #if DEBUG
  34. if (entitiesDB.Exists<T>(obj.Id) && obj.InitData.Valid)
  35. throw new ArgumentException("The block exists but the init data has not been removed!");
  36. #endif
  37. return ref entitiesDB.QueryEntityOrDefault<T>(obj);
  38. }
  39. public ref T GetViewComponent<T>(EcsObjectBase obj) where T : struct, IEntityViewComponent
  40. {
  41. return ref entitiesDB.QueryEntityOrDefault<T>(obj);
  42. }
  43. public object GetComponent(EcsObjectBase obj, Type type, string name)
  44. {
  45. var opt = AccessTools.Method(typeof(NativeApiExtensions), "QueryEntityOptional",
  46. new[] { typeof(EntitiesDB), typeof(EcsObjectBase), typeof(ExclusiveGroupStruct) }, new[] { type })
  47. .Invoke(null, new object[] { entitiesDB, obj, null });
  48. var str = AccessTools.Property(opt.GetType(), "Value").GetValue(opt);
  49. return AccessTools.Field(str.GetType(), name).GetValue(str);
  50. }
  51. public void SetComponent(EcsObjectBase obj, Type type, string name, object value)
  52. {
  53. var opt = AccessTools.Method(typeof(BlockEngine), "GetBlockInfoOptional", generics: new[] { type })
  54. .Invoke(this, new object[] { obj });
  55. var prop = AccessTools.Property(opt.GetType(), "Value");
  56. var str = prop.GetValue(opt);
  57. AccessTools.Field(str.GetType(), name).SetValue(str, value);
  58. prop.SetValue(opt, str);
  59. }
  60. }