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.

75 lines
3.7KB

  1. using Svelto.ECS;
  2. using Svelto.ECS.Hybrid;
  3. namespace TechbloxModdingAPI.Utility.ECS
  4. {
  5. public static class ManagedApiExtensions
  6. {
  7. /// <summary>
  8. /// Attempts to query an entity and returns an optional that contains the result if succeeded.
  9. /// <b>This overload does not take initializer data into account.</b>
  10. /// </summary>
  11. /// <param name="entitiesDB">The entities DB</param>
  12. /// <param name="egid">The EGID to query</param>
  13. /// <typeparam name="T">The component type to query</typeparam>
  14. /// <returns>An optional that contains the result on success or is empty if not found</returns>
  15. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EGID egid)
  16. where T : struct, IEntityViewComponent
  17. {
  18. return entitiesDB.TryQueryEntitiesAndIndex<T>(egid, out uint index, out var array)
  19. ? new OptionalRef<T>(array, index)
  20. : new OptionalRef<T>();
  21. }
  22. /// <summary>
  23. /// Attempts to query an entity and returns the result in an optional reference.
  24. /// </summary>
  25. /// <param name="entitiesDB">The entities DB to query from</param>
  26. /// <param name="obj">The ECS object to query</param>
  27. /// <param name="group">The group of the entity if the object can have multiple</param>
  28. /// <typeparam name="T">The component to query</typeparam>
  29. /// <returns>A reference to the component or a dummy value</returns>
  30. public static OptionalRef<T> QueryEntityOptional<T>(this EntitiesDB entitiesDB, EcsObjectBase obj,
  31. ExclusiveGroupStruct group = default)
  32. where T : struct, IEntityViewComponent
  33. {
  34. EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
  35. var opt = QueryEntityOptional<T>(entitiesDB, id);
  36. return opt ? opt : new OptionalRef<T>(obj, false);
  37. }
  38. /// <summary>
  39. /// Attempts to query an entity and returns the result or a dummy value that can be modified.
  40. /// </summary>
  41. /// <param name="entitiesDB">The entities DB to query from</param>
  42. /// <param name="obj">The ECS object to query</param>
  43. /// <param name="group">The group of the entity if the object can have multiple</param>
  44. /// <typeparam name="T">The component to query</typeparam>
  45. /// <returns>A reference to the component or a dummy value</returns>
  46. public static ref T QueryEntityOrDefault<T>(this EntitiesDB entitiesDB, EcsObjectBase obj,
  47. ExclusiveGroupStruct group = default)
  48. where T : struct, IEntityViewComponent
  49. {
  50. EGID id = group == ExclusiveGroupStruct.Invalid ? obj.Id : new EGID(obj.Id.entityID, group);
  51. var opt = QueryEntityOptional<T>(entitiesDB, id);
  52. if (opt) return ref opt.Get();
  53. if (obj.InitData.Valid) return ref obj.InitData.Initializer(id).GetOrAdd<T>();
  54. return ref opt.Get(); //Default value
  55. }
  56. /// <summary>
  57. /// Query entities as OptionalRefs. The elements always exist, it's just a nice way to encapsulate the data.
  58. /// </summary>
  59. /// <param name="entitiesDB"></param>
  60. /// <param name="group"></param>
  61. /// <param name="select"></param>
  62. /// <typeparam name="T"></typeparam>
  63. /// <typeparam name="TR"></typeparam>
  64. /// <returns></returns>
  65. public static RefCollection<T> QueryEntitiesOptional<T>(this EntitiesDB entitiesDB, ExclusiveGroupStruct group) where T : struct, IEntityViewComponent
  66. {
  67. var (buffer, ids, count) = entitiesDB.QueryEntities<T>(group);
  68. return new RefCollection<T>(count, buffer, ids, group);
  69. }
  70. }
  71. }