Mirror of Svelto.ECS because we're a fan of it
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
8.6KB

  1. //#define PARANOID_CHECK
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using Svelto.DataStructures;
  6. using Svelto.ECS.Internal;
  7. namespace Svelto.ECS
  8. {
  9. public partial class EnginesRoot : IDisposable, IUnitTestingInterface
  10. {
  11. ///--------------------------------------------
  12. ///
  13. public IEntityStreamConsumerFactory GenerateConsumerFactory()
  14. {
  15. return new GenericEntityStreamConsumerFactory(this);
  16. }
  17. public IEntityFactory GenerateEntityFactory()
  18. {
  19. return new GenericEntityFactory(this);
  20. }
  21. public IEntityFunctions GenerateEntityFunctions()
  22. {
  23. return new GenericEntityFunctions(this);
  24. }
  25. ///--------------------------------------------
  26. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  27. EntityInitializer BuildEntity(EGID entityID, IComponentBuilder[] componentsToBuild, Type descriptorType,
  28. IEnumerable<object> implementors, string caller)
  29. {
  30. CheckAddEntityID(entityID, descriptorType, caller);
  31. DBC.ECS.Check.Require(entityID.groupID.isInvalid == false,
  32. "invalid group detected, are you using new ExclusiveGroupStruct() instead of new ExclusiveGroup()?");
  33. var reference = _entityLocator.ClaimReference();
  34. _entityLocator.SetReference(reference, entityID);
  35. var dic = EntityFactory.BuildGroupedEntities(entityID, _groupedEntityToAdd, componentsToBuild, implementors
  36. #if DEBUG && !PROFILE_SVELTO
  37. , descriptorType
  38. #endif
  39. );
  40. return new EntityInitializer(entityID, dic, reference);
  41. }
  42. /// <summary>
  43. /// Preallocate memory to avoid the impact to resize arrays when many entities are submitted at once
  44. /// </summary>
  45. void Preallocate(ExclusiveGroupStruct groupID, uint size, IComponentBuilder[] entityComponentsToBuild)
  46. {
  47. void PreallocateEntitiesToAdd()
  48. {
  49. _groupedEntityToAdd.Preallocate(groupID, size, entityComponentsToBuild);
  50. }
  51. void PreallocateDBGroup()
  52. {
  53. var numberOfEntityComponents = entityComponentsToBuild.Length;
  54. FasterDictionary<ComponentID, ITypeSafeDictionary> group = GetOrAddDBGroup(groupID);
  55. for (var index = 0; index < numberOfEntityComponents; index++)
  56. {
  57. var entityComponentBuilder = entityComponentsToBuild[index];
  58. var entityComponentType = entityComponentBuilder.getComponentID;
  59. var dbList = group.GetOrAdd(entityComponentType, () => entityComponentBuilder.CreateDictionary(size));
  60. entityComponentBuilder.Preallocate(dbList, size);
  61. if (_groupsPerEntity.TryGetValue(entityComponentType, out var groupedGroup) == false)
  62. groupedGroup = _groupsPerEntity[entityComponentType] =
  63. new FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary>();
  64. groupedGroup[groupID] = dbList;
  65. }
  66. }
  67. PreallocateDBGroup();
  68. PreallocateEntitiesToAdd();
  69. _entityLocator.PreallocateReferenceMaps(groupID, size);
  70. }
  71. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  72. FasterDictionary<ComponentID, ITypeSafeDictionary> GetDBGroup(ExclusiveGroupStruct fromIdGroupId)
  73. {
  74. if (_groupEntityComponentsDB.TryGetValue(fromIdGroupId,
  75. out FasterDictionary<ComponentID, ITypeSafeDictionary> fromGroup) == false)
  76. throw new ECSException("Group doesn't exist: ".FastConcat(fromIdGroupId.ToName()));
  77. return fromGroup;
  78. }
  79. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  80. FasterDictionary<ComponentID, ITypeSafeDictionary> GetOrAddDBGroup(ExclusiveGroupStruct toGroupId)
  81. {
  82. return _groupEntityComponentsDB.GetOrAdd(toGroupId,
  83. () => new FasterDictionary<ComponentID, ITypeSafeDictionary>());
  84. }
  85. IComponentBuilder[] FindRealComponents<T>(EGID fromEntityGID) where T : IEntityDescriptor, new()
  86. {
  87. var fromGroup = GetDBGroup(fromEntityGID.groupID);
  88. if (fromGroup.TryGetValue(ComponentBuilderUtilities.ENTITY_INFO_COMPONENT_ID,
  89. out var entityInfoDic) //<entity ID, EntityInfoComponent>
  90. && ((ITypeSafeDictionary<EntityInfoComponent>)entityInfoDic).TryGetValue(fromEntityGID.entityID,
  91. out var entityInfo)) //there could be multiple entity descriptors registered in the same group, so it's necessary to check if the entity registered in the group has entityInfoComponent
  92. {
  93. #if PARANOID_CHECK
  94. var hash = new HashSet<IComponentBuilder>(entityInfo.componentsToBuild,
  95. default(ComponentBuilderComparer));
  96. foreach (var component in EntityDescriptorTemplate<T>.descriptor.componentsToBuild)
  97. {
  98. if (hash.Contains(component) == false)
  99. throw new Exception(
  100. $"entityInfo.componentsToBuild must contain all the base components {fromEntityGID}," +
  101. $" missing component {component}");
  102. hash.Remove(component);
  103. }
  104. #endif
  105. return entityInfo.componentsToBuild;
  106. }
  107. return EntityDescriptorTemplate<T>.realDescriptor.componentsToBuild;
  108. }
  109. IComponentBuilder[] FindRealComponents(EGID fromEntityGID, IComponentBuilder[] baseComponents)
  110. {
  111. var fromGroup = GetDBGroup(fromEntityGID.groupID);
  112. if (fromGroup.TryGetValue(ComponentBuilderUtilities.ENTITY_INFO_COMPONENT_ID,
  113. out var entityInfoDic) //<entity ID, EntityInfoComponent>
  114. && ((ITypeSafeDictionary<EntityInfoComponent>)entityInfoDic).TryGetValue(fromEntityGID.entityID,
  115. out var entityInfo)) //there could be multiple entity descriptors registered in the same group, so it's necessary to check if the entity registered in the group has entityInfoComponent
  116. {
  117. #if PARANOID_CHECK
  118. var hash = new HashSet<IComponentBuilder>(entityInfo.componentsToBuild,
  119. default(ComponentBuilderComparer));
  120. foreach (var component in baseComponents)
  121. {
  122. if (hash.Contains(component) == false)
  123. throw new Exception(
  124. $"entityInfo.componentsToBuild must contain all the base components {fromEntityGID}," +
  125. $" missing component {component}");
  126. hash.Remove(component);
  127. }
  128. #endif
  129. return entityInfo.componentsToBuild;
  130. }
  131. return baseComponents;
  132. }
  133. //one datastructure rule them all:
  134. //split by group
  135. //split by type per group. It's possible to get all the entities of a give type T per group thanks
  136. //to the FasterDictionary capabilities OR it's possible to get a specific entityComponent indexed by
  137. //ID. This ID doesn't need to be the EGID, it can be just the entityID
  138. //for each group id, save a dictionary indexed by entity type of entities indexed by id
  139. // group EntityComponentType entityID, EntityComponent
  140. internal readonly FasterDictionary<ExclusiveGroupStruct, FasterDictionary<ComponentID, ITypeSafeDictionary>>
  141. _groupEntityComponentsDB;
  142. //for each entity view type, return the groups (dictionary of entities indexed by entity id) where they are
  143. //found indexed by group id. TypeSafeDictionary are never created, they instead point to the ones hold
  144. //by _groupEntityComponentsDB
  145. // <EntityComponentType <groupID <entityID, EntityComponent>>>
  146. internal readonly FasterDictionary<ComponentID, FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary>>
  147. _groupsPerEntity;
  148. #if SVELTO_LEGACY_FILTERS
  149. //The filters stored for each component and group
  150. internal readonly FasterDictionary<ComponentID, FasterDictionary<ExclusiveGroupStruct, LegacyGroupFilters>>
  151. _groupFilters;
  152. #endif
  153. readonly EntitiesDB _entitiesDB;
  154. EntitiesDB IUnitTestingInterface.entitiesForTesting => _entitiesDB;
  155. }
  156. public interface IUnitTestingInterface
  157. {
  158. EntitiesDB entitiesForTesting { get; }
  159. }
  160. }