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.

EnginesRootSubmission.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  5. #if EXPERIMENTAL
  6. using Svelto.ECS.Experimental;
  7. using Svelto.ECS.Experimental.Internal;
  8. #endif
  9. #if ENGINE_PROFILER_ENABLED && UNITY_EDITOR
  10. using Svelto.ECS.Profiler;
  11. #endif
  12. namespace Svelto.ECS
  13. {
  14. public partial class EnginesRoot : IDisposable
  15. {
  16. void SubmitEntityViews()
  17. {
  18. bool newEntityViewsHaveBeenAddedWhileIterating =
  19. _metaEntityViewsToAdd.current.Count > 0
  20. || _entityViewsToAdd.current.Count > 0
  21. || _groupedEntityViewsToAdd.current.Count > 0;
  22. int numberOfReenteringLoops = 0;
  23. while (newEntityViewsHaveBeenAddedWhileIterating)
  24. {
  25. //use other as source from now on
  26. //current will be use to write new entityViews
  27. _entityViewsToAdd.Swap();
  28. _metaEntityViewsToAdd.Swap();
  29. _groupedEntityViewsToAdd.Swap();
  30. if (_entityViewsToAdd.other.Count > 0)
  31. AddEntityViewsToTheDBAndSuitableEngines(_entityViewsToAdd.other, _entityViewsDB);
  32. if (_metaEntityViewsToAdd.other.Count > 0)
  33. AddEntityViewsToTheDBAndSuitableEngines(_metaEntityViewsToAdd.other, _metaEntityViewsDB);
  34. if (_groupedEntityViewsToAdd.other.Count > 0)
  35. AddGroupEntityViewsToTheDBAndSuitableEngines(_groupedEntityViewsToAdd.other, _groupEntityViewsDB, _entityViewsDB);
  36. //other can be cleared now
  37. _entityViewsToAdd.other.Clear();
  38. _metaEntityViewsToAdd.other.Clear();
  39. _groupedEntityViewsToAdd.other.Clear();
  40. //has current new entityViews?
  41. newEntityViewsHaveBeenAddedWhileIterating =
  42. _metaEntityViewsToAdd.current.Count > 0
  43. || _entityViewsToAdd.current.Count > 0
  44. || _groupedEntityViewsToAdd.current.Count > 0;
  45. if (numberOfReenteringLoops > 5)
  46. throw new Exception("possible infinite loop found creating Entities inside IEntityViewsEngine Add method, please consider building entities outside IEntityViewsEngine Add method");
  47. numberOfReenteringLoops++;
  48. }
  49. }
  50. void AddEntityViewsToTheDBAndSuitableEngines(Dictionary<Type, ITypeSafeList> entityViewsToAdd,
  51. Dictionary<Type, ITypeSafeList> entityViewsDB)
  52. {
  53. foreach (var entityViewList in entityViewsToAdd)
  54. {
  55. AddEntityViewToDB(entityViewsDB, entityViewList);
  56. if (entityViewList.Value.isQueryiableEntityView)
  57. {
  58. AddEntityViewToEntityViewsDictionary(_entityViewsDBdic, entityViewList.Value, entityViewList.Key);
  59. }
  60. }
  61. foreach (var entityViewList in entityViewsToAdd)
  62. {
  63. if (entityViewList.Value.isQueryiableEntityView)
  64. {
  65. AddEntityViewToTheSuitableEngines(_entityViewEngines, entityViewList.Value,
  66. entityViewList.Key);
  67. }
  68. }
  69. }
  70. void AddGroupEntityViewsToTheDBAndSuitableEngines(Dictionary<int, Dictionary<Type, ITypeSafeList>> groupedEntityViewsToAdd,
  71. Dictionary<int, Dictionary<Type, ITypeSafeList>> groupEntityViewsDB,
  72. Dictionary<Type, ITypeSafeList> entityViewsDB)
  73. {
  74. foreach (var group in groupedEntityViewsToAdd)
  75. {
  76. AddEntityViewsToTheDBAndSuitableEngines(group.Value, entityViewsDB);
  77. AddEntityViewsToGroupDB(groupEntityViewsDB, @group);
  78. }
  79. }
  80. static void AddEntityViewsToGroupDB(Dictionary<int, Dictionary<Type, ITypeSafeList>> groupEntityViewsDB,
  81. KeyValuePair<int, Dictionary<Type, ITypeSafeList>> @group)
  82. {
  83. Dictionary<Type, ITypeSafeList> groupedEntityViewsByType;
  84. if (groupEntityViewsDB.TryGetValue(@group.Key, out groupedEntityViewsByType) == false)
  85. groupedEntityViewsByType = groupEntityViewsDB[@group.Key] = new Dictionary<Type, ITypeSafeList>();
  86. foreach (var entityView in @group.Value)
  87. {
  88. groupedEntityViewsByType.Add(entityView.Key, entityView.Value);
  89. }
  90. }
  91. static void AddEntityViewToDB(Dictionary<Type, ITypeSafeList> entityViewsDB, KeyValuePair<Type, ITypeSafeList> entityViewList)
  92. {
  93. ITypeSafeList dbList;
  94. if (entityViewsDB.TryGetValue(entityViewList.Key, out dbList) == false)
  95. dbList = entityViewsDB[entityViewList.Key] = entityViewList.Value.Create();
  96. dbList.AddRange(entityViewList.Value);
  97. }
  98. static void AddEntityViewToEntityViewsDictionary(Dictionary<Type, ITypeSafeDictionary> entityViewsDBdic,
  99. ITypeSafeList entityViews, Type entityViewType)
  100. {
  101. ITypeSafeDictionary entityViewsDic;
  102. if (entityViewsDBdic.TryGetValue(entityViewType, out entityViewsDic) == false)
  103. entityViewsDic = entityViewsDBdic[entityViewType] = entityViews.CreateIndexedDictionary();
  104. entityViewsDic.FillWithIndexedEntityViews(entityViews);
  105. }
  106. static void AddEntityViewToTheSuitableEngines(Dictionary<Type, FasterList<IHandleEntityViewEngine>> entityViewEngines, ITypeSafeList entityViewsList, Type entityViewType)
  107. {
  108. FasterList<IHandleEntityViewEngine> enginesForEntityView;
  109. if (entityViewEngines.TryGetValue(entityViewType, out enginesForEntityView))
  110. {
  111. int viewsCount;
  112. var entityViews = entityViewsList.ToArrayFast(out viewsCount);
  113. for (int i = 0; i < viewsCount; i++)
  114. {
  115. int count;
  116. var fastList = FasterList<IHandleEntityViewEngine>.NoVirt.ToArrayFast(enginesForEntityView, out count);
  117. IEntityView entityView = entityViews[i];
  118. for (int j = 0; j < count; j++)
  119. {
  120. #if ENGINE_PROFILER_ENABLED && UNITY_EDITOR
  121. EngineProfiler.MonitorAddDuration(_addEntityViewToEngine, fastList[j], entityView);
  122. #else
  123. fastList[j].Add(entityView);
  124. #endif
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }