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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System.Runtime.CompilerServices;
  2. using Svelto.DataStructures;
  3. using Svelto.ECS.Internal;
  4. namespace Svelto.ECS
  5. {
  6. public static class EntityNativeDBExtensions
  7. {
  8. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  9. public static AllGroupsEnumerable<T1> QueryEntities<T1>(this EntitiesDB db)
  10. where T1 :struct, IEntityComponent
  11. {
  12. return new AllGroupsEnumerable<T1>(db);
  13. }
  14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  15. public static NB<T> QueryEntitiesAndIndex<T>(this EntitiesDB entitiesDb, EGID entityGID, out uint index) where T : unmanaged, IEntityComponent
  16. {
  17. if (entitiesDb.QueryEntitiesAndIndexInternal(entityGID, out index, out NB<T> array) == true)
  18. return array;
  19. throw new EntityNotFoundException(entityGID, typeof(T));
  20. }
  21. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  22. public static NB<T> QueryEntitiesAndIndex<T>(this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group, out uint index) 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>(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>(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. static bool QueryEntitiesAndIndexInternal<T>(this EntitiesDB entitiesDb, EGID entityGID, out uint index, out NB<T> buffer) where T : unmanaged, IEntityComponent
  47. {
  48. index = 0;
  49. buffer = default;
  50. if (entitiesDb.SafeQueryEntityDictionary<T>(entityGID.groupID, out var safeDictionary) == false)
  51. return false;
  52. if (safeDictionary.TryFindIndex(entityGID.entityID, out index) == false)
  53. return false;
  54. buffer = (NB<T>) (safeDictionary as ITypeSafeDictionary<T>).GetValues(out _);
  55. return true;
  56. }
  57. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  58. public static ref T QueryEntity<T>(this EntitiesDB entitiesDb, EGID entityGID) where T : unmanaged, IEntityComponent
  59. {
  60. var array = entitiesDb.QueryEntitiesAndIndex<T>(entityGID, out var index);
  61. return ref array[(int) index];
  62. }
  63. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  64. public static ref T QueryEntity<T>(this EntitiesDB entitiesDb, uint id, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  65. {
  66. return ref entitiesDb.QueryEntity<T>(new EGID(id, group));
  67. }
  68. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static ref T QueryUniqueEntity<T>(this EntitiesDB entitiesDb, ExclusiveGroupStruct group) where T : unmanaged, IEntityComponent
  70. {
  71. var (entities, entitiescount) = entitiesDb.QueryEntities<T>(@group);
  72. #if DEBUG && !PROFILE_SVELTO
  73. if (entitiescount == 0)
  74. throw new ECSException("Unique entity not found '".FastConcat(typeof(T).ToString()).FastConcat("'"));
  75. if (entitiescount != 1)
  76. throw new ECSException("Unique entities must be unique! '".FastConcat(typeof(T).ToString())
  77. .FastConcat("'"));
  78. #endif
  79. return ref entities[0];
  80. }
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static NB<T> GetArrayAndEntityIndex<T>(this EGIDMapper<T> mapper, uint entityID, out uint index) where T : unmanaged, IEntityComponent
  83. {
  84. if (mapper._map.TryFindIndex(entityID, out index))
  85. {
  86. return (NB<T>) mapper._map.GetValues(out _);
  87. }
  88. throw new ECSException("Entity not found");
  89. }
  90. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. public static bool TryGetArrayAndEntityIndex<T>(this EGIDMapper<T> mapper, uint entityID, out uint index, out NB<T> array) where T : unmanaged, IEntityComponent
  92. {
  93. index = default;
  94. if (mapper._map != null && mapper._map.TryFindIndex(entityID, out index))
  95. {
  96. array = (NB<T>) mapper._map.GetValues(out _);
  97. return true;
  98. }
  99. array = default;
  100. return false;
  101. }
  102. }
  103. }