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.

EntityNativeDBExtensions.cs 6.1KB

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