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.

213 lines
9.2KB

  1. #if SVELTO_LEGACY_FILTERS
  2. using DBC.ECS;
  3. using Svelto.DataStructures;
  4. using Svelto.DataStructures.Native;
  5. using Svelto.ECS.Internal;
  6. namespace Svelto.ECS
  7. {
  8. /// <summary>
  9. /// This feature must be eventually tied to the new ExclusiveGroup that won't allow the use of
  10. /// custom EntitiesID
  11. /// The filters could be updated when entities buffer changes during the submission, while now this process
  12. /// is completely manual.
  13. /// Making this working properly is not in my priorities right now, as I will need to add the new group type
  14. /// AND optimize the submission process to be able to add more overhead
  15. /// </summary>
  16. public partial class EntitiesDB
  17. {
  18. FasterDictionary<ComponentID, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>> _filters =>
  19. _enginesRoot._groupFilters;
  20. public LegacyFilters GetLegacyFilters()
  21. {
  22. return new LegacyFilters(_filters);
  23. }
  24. public readonly struct LegacyFilters
  25. {
  26. public LegacyFilters(
  27. FasterDictionary<ComponentID, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>>
  28. filtersLegacy)
  29. {
  30. _filtersLegacy = filtersLegacy;
  31. }
  32. public ref LegacyFilterGroup CreateOrGetFilterForGroup<T>(int filterID, ExclusiveGroupStruct groupID)
  33. where T : struct, _IInternalEntityComponent
  34. {
  35. var refWrapper = ComponentTypeID<T>.id;
  36. return ref CreateOrGetFilterForGroup(filterID, groupID, refWrapper);
  37. }
  38. public bool HasFiltersForGroup<T>(ExclusiveGroupStruct groupID) where T : struct, _IInternalEntityComponent
  39. {
  40. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary) == false)
  41. return false;
  42. return fasterDictionary.ContainsKey(groupID);
  43. }
  44. public bool HasFilterForGroup<T>(int filterID, ExclusiveGroupStruct groupID)
  45. where T : struct, _IInternalEntityComponent
  46. {
  47. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary) == false)
  48. return false;
  49. if (fasterDictionary.TryGetValue(groupID, out var result))
  50. return result.HasFilter(filterID);
  51. return false;
  52. }
  53. public ref LegacyGroupFilters CreateOrGetFiltersForGroup<T>(ExclusiveGroupStruct groupID)
  54. where T : struct, _IInternalEntityComponent
  55. {
  56. var fasterDictionary = _filtersLegacy.GetOrAdd(ComponentTypeID<T>.id,
  57. () => new FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>());
  58. return ref fasterDictionary.GetOrAdd(groupID,
  59. () => new LegacyGroupFilters(new SharedSveltoDictionaryNative<int, LegacyFilterGroup>(0), groupID));
  60. }
  61. public ref LegacyGroupFilters GetFiltersForGroup<T>(ExclusiveGroupStruct groupID)
  62. where T : struct, _IInternalEntityComponent
  63. {
  64. #if DEBUG && !PROFILE_SVELTO
  65. if (_filtersLegacy.ContainsKey(ComponentTypeID<T>.id) == false)
  66. throw new ECSException($"trying to fetch not existing filters, type {typeof(T)}");
  67. if (_filtersLegacy[ComponentTypeID<T>.id].ContainsKey(groupID) == false)
  68. throw new ECSException(
  69. $"trying to fetch not existing filters, type {typeof(T)} group {groupID.ToName()}");
  70. #endif
  71. return ref _filtersLegacy[ComponentTypeID<T>.id].GetValueByRef(groupID);
  72. }
  73. public ref LegacyFilterGroup GetFilterForGroup<T>(int filterId, ExclusiveGroupStruct groupID)
  74. where T : struct, _IInternalEntityComponent
  75. {
  76. #if DEBUG && !PROFILE_SVELTO
  77. if (_filtersLegacy.ContainsKey(ComponentTypeID<T>.id) == false)
  78. throw new ECSException($"trying to fetch not existing filters, type {typeof(T)}");
  79. if (_filtersLegacy[ComponentTypeID<T>.id].ContainsKey(groupID) == false)
  80. throw new ECSException(
  81. $"trying to fetch not existing filters, type {typeof(T)} group {groupID.ToName()}");
  82. #endif
  83. return ref _filtersLegacy[ComponentTypeID<T>.id][groupID].GetFilter(filterId);
  84. }
  85. public bool TryGetFilterForGroup<T>(int filterId, ExclusiveGroupStruct groupID,
  86. out LegacyFilterGroup groupLegacyFilter) where T : struct, _IInternalEntityComponent
  87. {
  88. groupLegacyFilter = default;
  89. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary) == false)
  90. return false;
  91. if (fasterDictionary.TryGetValue(groupID, out var groupFilters) == false)
  92. return false;
  93. if (groupFilters.TryGetFilter(filterId, out groupLegacyFilter) == false)
  94. return false;
  95. return true;
  96. }
  97. public bool TryGetFiltersForGroup<T>(ExclusiveGroupStruct groupID,
  98. out LegacyGroupFilters legacyGroupFilters) where T : struct, _IInternalEntityComponent
  99. {
  100. legacyGroupFilters = default;
  101. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary) == false)
  102. return false;
  103. return fasterDictionary.TryGetValue(groupID, out legacyGroupFilters);
  104. }
  105. public void ClearFilter<T>(int filterID, ExclusiveGroupStruct exclusiveGroupStruct)
  106. {
  107. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary))
  108. {
  109. Check.Require(fasterDictionary.ContainsKey(exclusiveGroupStruct),
  110. $"trying to clear filter not present in group {exclusiveGroupStruct}");
  111. fasterDictionary[exclusiveGroupStruct].ClearFilter(filterID);
  112. }
  113. }
  114. public void ClearFilters<T>(int filterID)
  115. {
  116. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary))
  117. foreach (var filtersPerGroup in fasterDictionary)
  118. filtersPerGroup.value.ClearFilter(filterID);
  119. }
  120. public void DisposeFilters<T>(ExclusiveGroupStruct exclusiveGroupStruct)
  121. {
  122. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary))
  123. {
  124. fasterDictionary[exclusiveGroupStruct].DisposeFilters();
  125. fasterDictionary.Remove(exclusiveGroupStruct);
  126. }
  127. }
  128. public void DisposeFilters<T>()
  129. {
  130. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary))
  131. foreach (var filtersPerGroup in fasterDictionary)
  132. filtersPerGroup.value.DisposeFilters();
  133. _filtersLegacy.Remove(ComponentTypeID<T>.id);
  134. }
  135. public void DisposeFilterForGroup<T>(int resetFilterID, ExclusiveGroupStruct group)
  136. {
  137. if (_filtersLegacy.TryGetValue(ComponentTypeID<T>.id, out var fasterDictionary))
  138. fasterDictionary[@group].DisposeFilter(resetFilterID);
  139. }
  140. public bool TryRemoveEntityFromFilter<T>(int filtersID, EGID egid) where T : struct, _IInternalEntityComponent
  141. {
  142. if (TryGetFilterForGroup<T>(filtersID, egid.groupID, out var filter))
  143. return filter.TryRemove(egid.entityID);
  144. return false;
  145. }
  146. public void RemoveEntityFromFilter<T>(int filtersID, EGID egid) where T : struct, _IInternalEntityComponent
  147. {
  148. ref var filter = ref GetFilterForGroup<T>(filtersID, egid.groupID);
  149. filter.Remove(egid.entityID);
  150. }
  151. public bool AddEntityToFilter<N>(int filtersID, EGID egid, N mapper) where N : IEGIDMapper
  152. {
  153. ref var filter =
  154. ref CreateOrGetFilterForGroup(filtersID, egid.groupID, new RefWrapperType(mapper.entityType));
  155. return filter.Add(egid.entityID, mapper);
  156. }
  157. internal ref LegacyFilterGroup CreateOrGetFilterForGroup(int filterID, ExclusiveGroupStruct groupID,
  158. RefWrapperType refWrapper)
  159. {
  160. var fasterDictionary = _filtersLegacy.GetOrAdd(refWrapper,
  161. () => new FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>());
  162. var filters = fasterDictionary.GetOrAdd(groupID,
  163. (ref ExclusiveGroupStruct gid) =>
  164. new LegacyGroupFilters(new SharedSveltoDictionaryNative<int, LegacyFilterGroup>(0), gid),
  165. ref groupID);
  166. return ref filters.CreateOrGetFilter(filterID);
  167. }
  168. readonly FasterDictionary<ComponentID, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>>
  169. _filtersLegacy;
  170. }
  171. }
  172. }
  173. #endif