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.

235 lines
9.7KB

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