Mirror of Svelto.ECS because we're a fan of it
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.

161 lines
6.4KB

  1. using System.Runtime.CompilerServices;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. //todo: once using native memory for unmanaged struct will be optional, this will need to be moved under the Native namespace
  5. namespace Svelto.ECS
  6. {
  7. public static class EntityNativeDBExtensions
  8. {
  9. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  10. public static NB<T> QueryEntitiesAndIndex<T>
  11. (this EntitiesDB entitiesDb, EGID entityGID, out uint index) where T : unmanaged, IEntityComponent
  12. {
  13. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
  14. return array;
  15. throw new EntityNotFoundException(entityGID, typeof(T));
  16. }
  17. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  18. public static NB<T> QueryEntitiesAndIndex<T>
  19. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index)
  20. where T : unmanaged, IEntityComponent
  21. {
  22. EGID entityGID = new EGID(id, group);
  23. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
  24. return array;
  25. throw new EntityNotFoundException(entityGID, typeof(T));
  26. }
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static bool TryQueryEntitiesAndIndex<T>
  29. (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> array)
  30. where T : unmanaged, IEntityComponent
  31. {
  32. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out array) == true)
  33. return true;
  34. return false;
  35. }
  36. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  37. public static bool TryQueryEntitiesAndIndex<T>
  38. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index, out NB<T> array)
  39. where T : unmanaged, IEntityComponent
  40. {
  41. if (entitiesDb.QueryEntitiesAndIndexInternal(new EGID(id, group), out index, out array) == true)
  42. return true;
  43. return false;
  44. }
  45. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  46. public static bool TryGetEntity<T>(this EntitiesDB entitiesDb, uint entityID, ExclusiveGroupStruct @group, out T value)
  47. where T : unmanaged, IEntityComponent
  48. {
  49. if (TryQueryEntitiesAndIndex<T>(entitiesDb, entityID, group, out var index, out var array))
  50. {
  51. value = array[index];
  52. return true;
  53. }
  54. value = default;
  55. return false;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static bool TryGetEntity<T>(this EntitiesDB entitiesDb, EGID egid, out T value)
  59. where T : unmanaged, IEntityComponent
  60. {
  61. return TryGetEntity<T>(entitiesDb, egid.entityID, egid.groupID, out value);
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. static bool QueryEntitiesAndIndexInternal<T>
  65. (this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> buffer)
  66. where T : unmanaged, IEntityComponent
  67. {
  68. index = 0;
  69. buffer = default;
  70. if (entitiesDb.SafeQueryEntityDictionary<T>(entityGID.groupID, out var safeDictionary) == false)
  71. return false;
  72. if (safeDictionary.TryFindIndex(entityGID.entityID, out index) == false)
  73. return false;
  74. buffer = (NBInternal<T>) (safeDictionary as ITypeSafeDictionary<T>).GetValues(out _);
  75. return true;
  76. }
  77. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  78. public static ref T QueryEntity<T>
  79. (this EntitiesDB entitiesDb, EGID entityGID) where T : unmanaged, IEntityComponent
  80. {
  81. var array = entitiesDb.QueryEntitiesAndIndex<T>(entityGID, out var index);
  82. return ref array[(int) index];
  83. }
  84. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  85. public static ref T QueryEntity<T>
  86. (this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  87. {
  88. return ref entitiesDb.QueryEntity<T>(new EGID(id, group));
  89. }
  90. /// <summary>
  91. /// Expects that only one entity of type T exists in the group
  92. /// </summary>
  93. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  94. public static ref T QueryUniqueEntity<T>
  95. (this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  96. {
  97. var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
  98. #if DEBUG && !PROFILE_SVELTO
  99. if (entitiescount == 0)
  100. throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
  101. if (entitiescount != 1)
  102. throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
  103. .FastConcat("'"));
  104. #endif
  105. return ref entities[0];
  106. }
  107. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  108. public static NB<T> GetArrayAndEntityIndex<T>
  109. (this EGIDMapper<T> mapper, uint entityID, out uint index) where T : unmanaged, IEntityComponent
  110. {
  111. if (mapper._map.TryFindIndex(entityID, out index))
  112. {
  113. return (NBInternal<T>) mapper._map.GetValues(out _);
  114. }
  115. throw new ECSException("Entity not found");
  116. }
  117. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  118. public static bool TryGetArrayAndEntityIndex<T>
  119. (this EGIDMapper<T> mapper, uint entityID, out uint index, out NB<T> array)
  120. where T : unmanaged, IEntityComponent
  121. {
  122. index = default;
  123. if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
  124. {
  125. array = (NBInternal<T>) mapper._map.GetValues(out _);
  126. return true;
  127. }
  128. array = default;
  129. return false;
  130. }
  131. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  132. public static AllGroupsEnumerable<T1> QueryEntities<T1>(this EntitiesDB db)
  133. where T1 :unmanaged, IEntityComponent
  134. {
  135. return new AllGroupsEnumerable<T1>(db);
  136. }
  137. }
  138. }