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.

EntityDescriptor.cs 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using Svelto.DataStructures;
  2. using Svelto.ECS.Internal;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Svelto.ECS
  6. {
  7. public interface IEntityDescriptor
  8. {
  9. IEntityViewBuilder[] entityViewsToBuild { get; }
  10. }
  11. internal static class EntityDescriptorTemplate<TType> where TType : IEntityDescriptor, new()
  12. {
  13. public static readonly EntityDescriptorInfo Default = new EntityDescriptorInfo(new TType());
  14. }
  15. public class EntityDescriptorInfo
  16. {
  17. readonly internal IEntityViewBuilder[] entityViewsToBuild;
  18. readonly internal RemoveEntityImplementor removeEntityImplementor;
  19. internal readonly string name;
  20. internal EntityDescriptorInfo(IEntityDescriptor descriptor)
  21. {
  22. name = descriptor.ToString();
  23. entityViewsToBuild = descriptor.entityViewsToBuild;
  24. removeEntityImplementor = new RemoveEntityImplementor(entityViewsToBuild);
  25. }
  26. }
  27. }
  28. namespace Svelto.ECS.Internal
  29. {
  30. static class EntityFactory
  31. {
  32. internal static void BuildGroupedEntityViews(int entityID, int groupID,
  33. Dictionary<int, Dictionary<Type, ITypeSafeList>> groupEntityViewsByType,
  34. EntityDescriptorInfo entityViewsToBuildDescriptor,
  35. object[] implementors)
  36. {
  37. Dictionary<Type, ITypeSafeList> groupedEntityViewsTyped;
  38. if (groupEntityViewsByType.TryGetValue(groupID, out groupedEntityViewsTyped) == false)
  39. {
  40. groupedEntityViewsTyped = new Dictionary<Type, ITypeSafeList>();
  41. groupEntityViewsByType.Add(groupID, groupedEntityViewsTyped);
  42. }
  43. var removeEntityImplementor = new RemoveEntityImplementor(entityViewsToBuildDescriptor.entityViewsToBuild, groupID);
  44. InternalBuildEntityViews(entityID, groupedEntityViewsTyped, entityViewsToBuildDescriptor, implementors, removeEntityImplementor);
  45. }
  46. internal static void BuildEntityViews(int entityID,
  47. Dictionary<Type, ITypeSafeList> entityViewsByType,
  48. EntityDescriptorInfo entityViewsToBuildDescriptor,
  49. object[] implementors)
  50. {
  51. var removeEntityImplementor = entityViewsToBuildDescriptor.removeEntityImplementor;
  52. InternalBuildEntityViews(entityID, entityViewsByType, entityViewsToBuildDescriptor, implementors, removeEntityImplementor);
  53. }
  54. private static void InternalBuildEntityViews(int entityID,
  55. Dictionary<Type, ITypeSafeList> entityViewsByType,
  56. EntityDescriptorInfo entityViewsToBuildDescriptor,
  57. object[] implementors, RemoveEntityImplementor removeEntityImplementor)
  58. {
  59. var entityViewsToBuild = entityViewsToBuildDescriptor.entityViewsToBuild;
  60. int count = entityViewsToBuild.Length;
  61. for (int index = 0; index < count; index++)
  62. {
  63. var entityViewBuilder = entityViewsToBuild[index];
  64. var entityViewType = entityViewBuilder.GetEntityViewType();
  65. //only class EntityView will be returned
  66. //struct EntityView cannot be filled so it will be null.
  67. var entityViewObjectToFill =
  68. BuildEntityView(entityID, entityViewsByType, entityViewType, entityViewBuilder);
  69. //the semantic of this code must still be improved
  70. //but only classes can be filled, so I am aware
  71. //it's a EntityView
  72. if (entityViewObjectToFill != null)
  73. {
  74. FillEntityView(entityViewObjectToFill as EntityView, implementors, removeEntityImplementor,
  75. entityViewsToBuildDescriptor.name);
  76. }
  77. }
  78. }
  79. internal static IEntityView BuildEntityView(int entityID, Dictionary<Type, ITypeSafeList> entityViewsByType,
  80. Type entityViewType, IEntityViewBuilder entityViewBuilder)
  81. {
  82. ITypeSafeList entityViewsList;
  83. var entityViewsPoolWillBeCreated =
  84. entityViewsByType.TryGetValue(entityViewType, out entityViewsList) == false;
  85. IEntityView entityViewObjectToFill;
  86. //passing the undefined entityViewsByType inside the entityViewBuilder will allow
  87. //it to be created with the correct type and casted back to the undefined list.
  88. //that's how the list will be eventually of the target type.
  89. entityViewBuilder.BuildEntityViewAndAddToList(ref entityViewsList, entityID, out entityViewObjectToFill);
  90. if (entityViewsPoolWillBeCreated)
  91. entityViewsByType.Add(entityViewType, entityViewsList);
  92. return entityViewObjectToFill as IEntityView;
  93. }
  94. //this is used to avoid newing a dictionary every time, but it's used locally only and it's clearead for each use
  95. #if DEBUG && !PROFILER
  96. static readonly Dictionary<Type, Tuple<object, int>> implementorsByType = new Dictionary<Type, Tuple<object, int>>();
  97. #else
  98. static readonly Dictionary<Type, object> implementorsByType = new Dictionary<Type, object>();
  99. #endif
  100. static void FillEntityView(EntityView entityView, object[] implementors, RemoveEntityImplementor removeEntity,
  101. string entityDescriptorName)
  102. {
  103. for (int index = 0; index < implementors.Length; index++)
  104. {
  105. var implementor = implementors[index];
  106. if (implementor != null)
  107. {
  108. var type = implementor.GetType();
  109. Type[] interfaces;
  110. if (_cachedTypes.TryGetValue(type, out interfaces) == false)
  111. interfaces = _cachedTypes[type] = type.GetInterfaces();
  112. for (int iindex = 0; iindex < interfaces.Length; iindex++)
  113. {
  114. var componentType = interfaces[iindex];
  115. #if DEBUG && !PROFILER
  116. Tuple<object, int> implementorHolder;
  117. if (implementorsByType.TryGetValue(componentType, out implementorHolder) == true)
  118. implementorHolder.item2++;
  119. else
  120. implementorsByType[componentType] = new Tuple<object, int>(implementor, 1);
  121. #else
  122. implementorsByType[componentType] = implementor;
  123. #endif
  124. }
  125. }
  126. #if DEBUG && !PROFILER
  127. else
  128. Utility.Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat(entityView.ToString()));
  129. #endif
  130. }
  131. int count;
  132. //Very efficent way to collect the fields of every EntityViewType
  133. KeyValuePair<Type, Action<EntityView, object>>[] setters =
  134. FasterList<KeyValuePair<Type, Action<EntityView, object>>>.NoVirt.ToArrayFast(entityView.EntityViewBlazingFastReflection, out count);
  135. var removeEntityComponentType = typeof(IRemoveEntityComponent);
  136. for (int i = 0; i < count; i++)
  137. {
  138. var keyValuePair = setters[i];
  139. Type fieldType = keyValuePair.Key;
  140. if (fieldType != removeEntityComponentType)
  141. {
  142. #if DEBUG && !PROFILER
  143. Tuple<object, int> component;
  144. #else
  145. object component;
  146. #endif
  147. if (implementorsByType.TryGetValue(fieldType, out component) == false)
  148. {
  149. Exception e = new Exception(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name + " - EntityView: " +
  150. entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName);
  151. throw e;
  152. }
  153. #if DEBUG && !PROFILER
  154. if (component.item2 > 1)
  155. Utility.Console.LogError(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat(
  156. "Component Type: ", fieldType.Name, " implementor: ",
  157. component.item1.ToString()) + " - EntityView: " +
  158. entityView.GetType().Name + " - EntityDescriptor " + entityDescriptorName);
  159. #endif
  160. #if DEBUG && !PROFILER
  161. keyValuePair.Value(entityView, component.item1);
  162. #else
  163. keyValuePair.Value(entityView, component);
  164. #endif
  165. }
  166. else
  167. {
  168. keyValuePair.Value(entityView, removeEntity);
  169. }
  170. }
  171. implementorsByType.Clear();
  172. }
  173. #if DEBUG && !PROFILER
  174. struct Tuple<T1, T2>
  175. {
  176. public T1 item1;
  177. public T2 item2;
  178. public Tuple(T1 implementor, T2 v)
  179. {
  180. item1 = implementor;
  181. item2 = v;
  182. }
  183. }
  184. #endif
  185. static Dictionary<Type, Type[]> _cachedTypes = new Dictionary<Type, Type[]>();
  186. const string DUPLICATE_IMPLEMENTOR_ERROR =
  187. "<color=orange>Svelto.ECS</color> the same component is implemented with more than one implementor. This is considered an error and MUST be fixed.";
  188. const string NULL_IMPLEMENTOR_ERROR =
  189. "<color=orange>Svelto.ECS</color> Null implementor, please be careful about the implementors passed to avoid performance loss";
  190. const string NOT_FOUND_EXCEPTION = "<color=orange>Svelto.ECS</color> Implementor not found for an EntityView.";
  191. }
  192. }