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.

154 lines
6.1KB

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