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 10KB

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