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.

120 lines
6.1KB

  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.ECS;
  4. using Svelto.Tasks;
  5. using Svelto.Tasks.Lean;
  6. using TechbloxModdingAPI.Tasks;
  7. namespace TechbloxModdingAPI.Utility
  8. {
  9. public static class NativeApiExtensions
  10. {
  11. /// <summary>
  12. /// Attempts to query an entity and returns an optional that contains the result if succeeded.
  13. /// <b>This overload does not take initializer data into account.</b>
  14. /// </summary>
  15. /// <param name="entitiesDB">The entities DB</param>
  16. /// <param name="egid">The EGID to query</param>
  17. /// <typeparam name="T">The component type to query</typeparam>
  18. /// <returns>An optional that contains the result on success or is empty if not found</returns>
  19. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EGID egid)
  20. where T : unmanaged, IEntityComponent
  21. {
  22. return entitiesDB.TryQueryEntitiesAndIndex<T>(egid, out uint index, out var array)
  23. ? new OptionalRef<T>(array, index)
  24. : new OptionalRef<T>();
  25. }
  26. /// <summary>
  27. /// Attempts to query an entity and returns the result in an optional reference.
  28. /// </summary>
  29. /// <param name="entitiesDB">The entities DB to query from</param>
  30. /// <param name="obj">The ECS object to query</param>
  31. /// <param name="group">The group of the entity if the object can have multiple</param>
  32. /// <typeparam name="T">The component to query</typeparam>
  33. /// <returns>A reference to the component or a dummy value</returns>
  34. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj,
  35. ExclusiveGroupStruct group = default)
  36. where T : unmanaged, IEntityComponent
  37. {
  38. EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
  39. var opt = QueryEntityOptional<T>(entitiesDB, id);
  40. return opt ? opt : new OptionalRef<T>(obj, true);
  41. }
  42. /// <summary>
  43. /// Attempts to query an entity and returns the result or a dummy value that can be modified.
  44. /// </summary>
  45. /// <param name="entitiesDB">The entities DB to query from</param>
  46. /// <param name="obj">The ECS object to query</param>
  47. /// <param name="group">The group of the entity if the object can have multiple</param>
  48. /// <typeparam name="T">The component to query</typeparam>
  49. /// <returns>A reference to the component or a dummy value</returns>
  50. public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj,
  51. ExclusiveGroupStruct group = default)
  52. where T : unmanaged, IEntityComponent
  53. {
  54. EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
  55. var opt = QueryEntityOptional<T>(entitiesDB, id);
  56. if (opt) return ref opt.Get();
  57. if (obj.InitData.Valid) return ref obj.InitData.Initializer(id).GetOrAdd<T>();
  58. /*if (!obj.InitData.Valid) return ref opt.Get(); //Default value
  59. var init = obj.InitData.Initializer(id);
  60. // Do not create the component if missing, as that can trigger Add() listeners that, in some cases, may be
  61. // invalid if (ab)using the classes in an unusual way - TODO: Check entity descriptor or something
  62. if (init.Has<T>()) return ref init.Get<T>();*/
  63. return ref opt.Get(); //Default value
  64. }
  65. private static readonly Dictionary<Type, (int PublishedCount, HashSet<EGID> Changes)> ChangesToPublish = new();
  66. /// <summary>
  67. /// Publishes an entity change, ignoring duplicate publishes and delaying changes as necessary.
  68. /// It will only publish in the next frame.
  69. /// </summary>
  70. /// <param name="entitiesDB">The entities DB to publish to</param>
  71. /// <param name="id">The ECS object that got changed</param>
  72. /// <param name="limit">Limits how many changes to publish - should be no more than the consumers' capacity that process this component</param>
  73. /// <typeparam name="T">The component that changed</typeparam>
  74. public static void PublishEntityChangeDelayed<T>(this EntitiesDB entitiesDB, EGID id, int limit = 80)
  75. where T : unmanaged, IEntityComponent
  76. {
  77. //TODO: Doesn't seem to help
  78. if (!ChangesToPublish.ContainsKey(typeof(T)))
  79. ChangesToPublish.Add(typeof(T), (0, new HashSet<EGID>()));
  80. var changes = ChangesToPublish[typeof(T)].Changes;
  81. if (changes.Contains(id)) return;
  82. changes.Add(id);
  83. PublishChanges<T>(entitiesDB, id, limit).RunOn(Scheduler.leanRunner);
  84. }
  85. private static IEnumerator<TaskContract> PublishChanges<T>(EntitiesDB entitiesDB, EGID id, int limit)
  86. where T : unmanaged, IEntityComponent
  87. {
  88. yield return Yield.It;
  89. while (ChangesToPublish[typeof(T)].PublishedCount >= limit)
  90. yield return Yield.It;
  91. entitiesDB.PublishEntityChange<T>(id);
  92. var (count, changes) = ChangesToPublish[typeof(T)];
  93. changes.Remove(id);
  94. ChangesToPublish[typeof(T)] = (count + 1, changes);
  95. yield return Yield.It;
  96. ChangesToPublish[typeof(T)] = (0, changes);
  97. }
  98. /// <summary>
  99. /// Query entities as OptionalRefs. The elements always exist, it's just a nice way to encapsulate the data.
  100. /// </summary>
  101. /// <param name="entitiesDB"></param>
  102. /// <param name="group"></param>
  103. /// <param name="select"></param>
  104. /// <typeparam name="T"></typeparam>
  105. /// <typeparam name="TR"></typeparam>
  106. /// <returns></returns>
  107. public static RefCollection<T> QueryEntitiesOptional<T>(this EntitiesDB entitiesDB, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  108. {
  109. var (buffer, ids, count) = entitiesDB.QueryEntities<T>(group);
  110. return new RefCollection<T>(count, buffer, ids, group);
  111. }
  112. }
  113. }