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.

EnginesRoot.Entities.cs 8.6KB

Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. }