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.Engines.cs 15KB

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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.Common;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS.Internal;
  6. using Svelto.ECS.Schedulers;
  7. namespace Svelto.ECS
  8. {
  9. public sealed partial class EnginesRoot
  10. {
  11. static EnginesRoot()
  12. {
  13. GroupHashMap.Init();
  14. SerializationDescriptorMap.Init();
  15. }
  16. /// <summary>
  17. /// Engines root contextualize your engines and entities. You don't need to limit yourself to one EngineRoot
  18. /// as multiple engines root could promote separation of scopes. The EntitySubmissionScheduler checks
  19. /// periodically if new entity must be submitted to the database and the engines. It's an external
  20. /// dependencies to be independent by the running platform as the user can define it.
  21. /// The EntitySubmissionScheduler cannot hold an EnginesRoot reference, that's why
  22. /// it must receive a weak reference of the EnginesRoot callback.
  23. /// </summary>
  24. public EnginesRoot(EntitiesSubmissionScheduler entitiesComponentScheduler)
  25. {
  26. _entitiesOperations = new FasterDictionary<ulong, EntitySubmitOperation>();
  27. _idChecker = new FasterDictionary<ExclusiveGroupStruct, HashSet<uint>>();
  28. _multipleOperationOnSameEGIDChecker = new FasterDictionary<EGID, uint>();
  29. #if UNITY_NATIVE //because of the thread count, ATM this is only for unity
  30. _nativeSwapOperationQueue = new DataStructures.AtomicNativeBags(Allocator.Persistent);
  31. _nativeRemoveOperationQueue = new DataStructures.AtomicNativeBags(Allocator.Persistent);
  32. _nativeAddOperationQueue = new DataStructures.AtomicNativeBags(Allocator.Persistent);
  33. #endif
  34. _serializationDescriptorMap = new SerializationDescriptorMap();
  35. _maxNumberOfOperationsPerFrame = uint.MaxValue;
  36. _reactiveEnginesAddRemove = new FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>>();
  37. _reactiveEnginesAddRemoveOnDispose =
  38. new FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>>();
  39. _reactiveEnginesSwap = new FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>>();
  40. _reactiveEnginesSubmission = new FasterList<IReactOnSubmission>();
  41. _enginesSet = new FasterList<IEngine>();
  42. _enginesTypeSet = new HashSet<Type>();
  43. _disposableEngines = new FasterList<IDisposable>();
  44. _transientEntitiesOperations = new FasterList<EntitySubmitOperation>();
  45. _groupEntityComponentsDB =
  46. new FasterDictionary<ExclusiveGroupStruct, FasterDictionary<RefWrapperType, ITypeSafeDictionary>>();
  47. _groupsPerEntity =
  48. new FasterDictionary<RefWrapperType, FasterDictionary<ExclusiveGroupStruct, ITypeSafeDictionary>>();
  49. _groupedEntityToAdd = new DoubleBufferedEntitiesToAdd();
  50. _entityStreams = EntitiesStreams.Create();
  51. _groupFilters =
  52. new FasterDictionary<RefWrapperType, FasterDictionary<ExclusiveGroupStruct, GroupFilters>>();
  53. _entityLocator.InitEntityReferenceMap();
  54. _entitiesDB = new EntitiesDB(this,_entityLocator);
  55. scheduler = entitiesComponentScheduler;
  56. scheduler.onTick = new EntitiesSubmitter(this);
  57. #if UNITY_NATIVE
  58. AllocateNativeOperations();
  59. #endif
  60. }
  61. public EnginesRoot
  62. (EntitiesSubmissionScheduler entitiesComponentScheduler, bool isDeserializationOnly) :
  63. this(entitiesComponentScheduler)
  64. {
  65. _isDeserializationOnly = isDeserializationOnly;
  66. }
  67. public EntitiesSubmissionScheduler scheduler { get; }
  68. /// <summary>
  69. /// Dispose an EngineRoot once not used anymore, so that all the
  70. /// engines are notified with the entities removed.
  71. /// It's a clean up process.
  72. /// </summary>
  73. public void Dispose()
  74. {
  75. _isDisposing = true;
  76. using (var profiler = new PlatformProfiler("Final Dispose"))
  77. {
  78. //Note: The engines are disposed before the the remove callback to give the chance to behave
  79. //differently if a remove happens as a consequence of a dispose
  80. //The pattern is to implement the IDisposable interface and set a flag in the engine. The
  81. //remove callback will then behave differently according the flag.
  82. foreach (var engine in _disposableEngines)
  83. try
  84. {
  85. if (engine is IDisposingEngine dengine)
  86. dengine.isDisposing = true;
  87. engine.Dispose();
  88. }
  89. catch (Exception e)
  90. {
  91. Console.LogException(e);
  92. }
  93. foreach (var groups in _groupEntityComponentsDB)
  94. foreach (var entityList in groups.Value)
  95. try
  96. {
  97. entityList.Value.ExecuteEnginesRemoveCallbacks(_reactiveEnginesAddRemoveOnDispose, profiler
  98. , groups.Key);
  99. }
  100. catch (Exception e)
  101. {
  102. Console.LogException(e);
  103. }
  104. foreach (var groups in _groupEntityComponentsDB)
  105. foreach (var entityList in groups.Value)
  106. entityList.Value.Dispose();
  107. foreach (var type in _groupFilters)
  108. foreach (var group in type.Value)
  109. group.Value.Dispose();
  110. _groupFilters.Clear();
  111. #if UNITY_NATIVE
  112. _nativeAddOperationQueue.Dispose();
  113. _nativeRemoveOperationQueue.Dispose();
  114. _nativeSwapOperationQueue.Dispose();
  115. #endif
  116. _groupEntityComponentsDB.Clear();
  117. _groupsPerEntity.Clear();
  118. _disposableEngines.Clear();
  119. _enginesSet.Clear();
  120. _enginesTypeSet.Clear();
  121. _reactiveEnginesSwap.Clear();
  122. _reactiveEnginesAddRemove.Clear();
  123. _reactiveEnginesAddRemoveOnDispose.Clear();
  124. _reactiveEnginesSubmission.Clear();
  125. _entitiesOperations.Clear();
  126. _transientEntitiesOperations.Clear();
  127. _groupedEntityToAdd.Dispose();
  128. _entityLocator.DisposeEntityReferenceMap();
  129. _entityStreams.Dispose();
  130. scheduler.Dispose();
  131. }
  132. GC.SuppressFinalize(this);
  133. }
  134. public void AddEngine(IEngine engine)
  135. {
  136. var type = engine.GetType();
  137. var refWrapper = new RefWrapperType(type);
  138. DBC.ECS.Check.Require(engine != null, "Engine to add is invalid or null");
  139. DBC.ECS.Check.Require(
  140. _enginesTypeSet.Contains(refWrapper) == false
  141. || type.ContainsCustomAttribute(typeof(AllowMultipleAttribute)) == true
  142. , "The same engine has been added more than once, if intentional, use [AllowMultiple] class attribute "
  143. .FastConcat(engine.ToString()));
  144. try
  145. {
  146. if (engine is IReactOnAddAndRemove viewEngine)
  147. CheckReactEngineComponents(viewEngine, _reactiveEnginesAddRemove, type.Name);
  148. if (engine is IReactOnDispose viewEngineDispose)
  149. CheckReactEngineComponents(viewEngineDispose, _reactiveEnginesAddRemoveOnDispose, type.Name);
  150. if (engine is IReactOnSwap viewEngineSwap)
  151. CheckReactEngineComponents(viewEngineSwap, _reactiveEnginesSwap, type.Name);
  152. if (engine is IReactOnSubmission submissionEngine)
  153. _reactiveEnginesSubmission.Add(submissionEngine);
  154. _enginesTypeSet.Add(refWrapper);
  155. _enginesSet.Add(engine);
  156. if (engine is IDisposable)
  157. _disposableEngines.Add(engine as IDisposable);
  158. if (engine is IQueryingEntitiesEngine queryableEntityComponentEngine)
  159. {
  160. queryableEntityComponentEngine.entitiesDB = _entitiesDB;
  161. queryableEntityComponentEngine.Ready();
  162. }
  163. }
  164. catch (Exception e)
  165. {
  166. throw new ECSException("Code crashed while adding engine ".FastConcat(engine.GetType().ToString(), " ")
  167. , e);
  168. }
  169. }
  170. void NotifyReactiveEnginesOnSubmission()
  171. {
  172. var enginesCount = _reactiveEnginesSubmission.count;
  173. for (var i = 0; i < enginesCount; i++)
  174. _reactiveEnginesSubmission[i].EntitiesSubmitted();
  175. }
  176. ~EnginesRoot()
  177. {
  178. Console.LogWarning("Engines Root has been garbage collected, don't forget to call Dispose()!");
  179. Dispose();
  180. }
  181. void CheckReactEngineComponents<T>
  182. (T engine, FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>> engines, string typeName)
  183. where T : class, IReactEngine
  184. {
  185. var interfaces = engine.GetType().GetInterfaces();
  186. foreach (var interf in interfaces)
  187. if (interf.IsGenericTypeEx() && typeof(T).IsAssignableFrom(interf))
  188. {
  189. var genericArguments = interf.GetGenericArgumentsEx();
  190. AddEngineToList(engine, genericArguments, engines, typeName);
  191. }
  192. }
  193. static void AddEngineToList<T>
  194. (T engine, Type[] entityComponentTypes
  195. , FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>> engines, string typeName)
  196. where T : class, IReactEngine
  197. {
  198. for (var i = 0; i < entityComponentTypes.Length; i++)
  199. {
  200. var type = entityComponentTypes[i];
  201. if (engines.TryGetValue(new RefWrapperType(type), out var list) == false)
  202. {
  203. list = new FasterList<ReactEngineContainer>();
  204. engines.Add(new RefWrapperType(type), list);
  205. }
  206. list.Add(new ReactEngineContainer(engine, typeName));
  207. }
  208. }
  209. internal bool _isDisposing;
  210. readonly FasterList<IDisposable> _disposableEngines;
  211. readonly FasterList<IEngine> _enginesSet;
  212. readonly HashSet<Type> _enginesTypeSet;
  213. readonly FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>> _reactiveEnginesAddRemove;
  214. readonly FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>> _reactiveEnginesAddRemoveOnDispose;
  215. readonly FasterList<IReactOnSubmission> _reactiveEnginesSubmission;
  216. readonly FasterDictionary<RefWrapperType, FasterList<ReactEngineContainer>> _reactiveEnginesSwap;
  217. public struct EntitiesSubmitter
  218. {
  219. public EntitiesSubmitter(EnginesRoot enginesRoot) : this()
  220. {
  221. _enginesRoot = new Svelto.DataStructures.WeakReference<EnginesRoot>(enginesRoot);
  222. _privateSubmitEntities =
  223. _enginesRoot.Target.SingleSubmission(new PlatformProfiler());
  224. submitEntities = Invoke(); //this must be last to capture all the variables
  225. }
  226. IEnumerator<bool> Invoke()
  227. {
  228. while (true)
  229. {
  230. DBC.ECS.Check.Require(_enginesRoot.IsValid, "ticking an GCed engines root?");
  231. var enginesRootTarget = _enginesRoot.Target;
  232. var entitiesSubmissionScheduler = enginesRootTarget.scheduler;
  233. if (entitiesSubmissionScheduler.paused == false)
  234. {
  235. DBC.ECS.Check.Require(entitiesSubmissionScheduler.isRunning == false
  236. , "A submission started while the previous one was still flushing");
  237. entitiesSubmissionScheduler.isRunning = true;
  238. using (var profiler = new PlatformProfiler("Svelto.ECS - Entities Submission"))
  239. {
  240. var iterations = 0;
  241. var hasEverSubmitted = false;
  242. #if UNITY_NATIVE
  243. enginesRootTarget.FlushNativeOperations(profiler);
  244. #endif
  245. //todo: proper unit test structural changes made as result of add/remove callbacks
  246. while (enginesRootTarget.HasMadeNewStructuralChangesInThisIteration() && iterations++ < 5)
  247. {
  248. hasEverSubmitted = true;
  249. while (true)
  250. {
  251. _privateSubmitEntities.MoveNext();
  252. if (_privateSubmitEntities.Current == true)
  253. {
  254. using (profiler.Yield())
  255. {
  256. yield return true;
  257. }
  258. }
  259. else
  260. break;
  261. }
  262. #if UNITY_NATIVE
  263. if (enginesRootTarget.HasMadeNewStructuralChangesInThisIteration())
  264. enginesRootTarget.FlushNativeOperations(profiler);
  265. #endif
  266. }
  267. #if DEBUG && !PROFILE_SVELTO
  268. if (iterations == 5)
  269. throw new ECSException("possible circular submission detected");
  270. #endif
  271. if (hasEverSubmitted)
  272. enginesRootTarget.NotifyReactiveEnginesOnSubmission();
  273. }
  274. entitiesSubmissionScheduler.isRunning = false;
  275. ++entitiesSubmissionScheduler.iteration;
  276. }
  277. yield return false;
  278. }
  279. }
  280. public uint maxNumberOfOperationsPerFrame
  281. {
  282. set => _enginesRoot.Target._maxNumberOfOperationsPerFrame = value;
  283. }
  284. readonly Svelto.DataStructures.WeakReference<EnginesRoot> _enginesRoot;
  285. internal readonly IEnumerator<bool> submitEntities;
  286. readonly IEnumerator<bool> _privateSubmitEntities;
  287. }
  288. }
  289. }