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.

233 lines
9.5KB

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