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.6KB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Svelto.DataStructures;
  5. using Svelto.ECS.Internal;
  6. namespace Svelto.ECS
  7. {
  8. public class EntityDescriptor
  9. {
  10. protected EntityDescriptor()
  11. {}
  12. /// <summary>
  13. /// if you want to avoid allocation in run-time, you can prebuild
  14. /// EntityDescriptors and use them to build entities at different
  15. /// times
  16. /// </summary>
  17. protected EntityDescriptor(INodeBuilder[] nodesToBuild)
  18. {
  19. _nodesToBuild = new FasterList<INodeBuilder>(nodesToBuild);
  20. }
  21. protected EntityDescriptor(INodeBuilder[] nodesToBuild,
  22. params object[] componentsImplementor):this(nodesToBuild)
  23. {
  24. ProcessImplementors(componentsImplementor);
  25. }
  26. public void AddImplementors(params object[] componentsImplementor)
  27. {
  28. ProcessImplementors(componentsImplementor);
  29. }
  30. public void AddNodes(params INodeBuilder[] nodesWithID)
  31. {
  32. _nodesToBuild.AddRange(nodesWithID);
  33. }
  34. internal void BuildGroupedNodes
  35. (int entityID, int groupID,
  36. Dictionary<Type, Dictionary<int, ITypeSafeList>> groupNodes,
  37. ref BuildNodeCallbackStruct callBackstruct)
  38. {
  39. for (int index = 0; index < _nodesToBuild.Count; index++)
  40. {
  41. var nodeBuilder = _nodesToBuild[index];
  42. var nodeType = nodeBuilder.GetNodeType();
  43. Dictionary<int, ITypeSafeList> groupedNodesTyped;
  44. if (groupNodes.TryGetValue(nodeType, out groupedNodesTyped) == false)
  45. {
  46. groupedNodesTyped = new Dictionary<int, ITypeSafeList>();
  47. groupNodes.Add(nodeType, groupedNodesTyped);
  48. };
  49. ITypeSafeList nodes;
  50. var mustAdd = groupedNodesTyped.TryGetValue(groupID, out nodes) == false;
  51. var node = nodeBuilder.BuildAndAddToList(ref nodes, entityID);
  52. if (mustAdd)
  53. groupedNodesTyped[groupID] = nodes;
  54. if (node != null && nodeBuilder.reflects != FillNodeMode.None)
  55. {
  56. node = FillNode(node, nodeBuilder.reflects);
  57. SetupImplementors(ref callBackstruct, nodes);
  58. }
  59. /* var groupNode = node as IGroupedNode;
  60. if (groupNode != null)
  61. groupNode.groupID = groupID;*/
  62. }
  63. }
  64. internal void BuildNodes(int entityID,
  65. Dictionary<Type, ITypeSafeList> nodesToAdd,
  66. ref BuildNodeCallbackStruct callBackstruct)
  67. {
  68. for (int index = 0; index < _nodesToBuild.Count; index++)
  69. {
  70. var nodeBuilder = _nodesToBuild[index];
  71. var nodeType = nodeBuilder.GetNodeType();
  72. ITypeSafeList nodes;
  73. var mustAdd = nodesToAdd.TryGetValue(nodeType, out nodes) == false;
  74. var node = nodeBuilder.BuildAndAddToList(ref nodes, entityID);
  75. if (mustAdd)
  76. nodesToAdd[nodeType] = nodes;
  77. if (node != null && nodeBuilder.reflects != FillNodeMode.None)
  78. {
  79. FillNode(node, nodeBuilder.reflects);
  80. SetupImplementors(ref callBackstruct, nodes);
  81. }
  82. }
  83. }
  84. void ProcessImplementors(object[] implementors)
  85. {
  86. for (int index = 0; index < implementors.Length; index++)
  87. {
  88. var implementor = implementors[index];
  89. if (implementor != null)
  90. {
  91. if (implementor is IRemoveEntityComponent)
  92. _removingImplementors.Add(new DataStructures.WeakReference<IRemoveEntityComponent>(implementor as IRemoveEntityComponent));
  93. if (implementor is IDisableEntityComponent)
  94. _disablingImplementors.Add(new DataStructures.WeakReference<IDisableEntityComponent>(implementor as IDisableEntityComponent));
  95. if (implementor is IEnableEntityComponent)
  96. _enablingImplementors.Add(new DataStructures.WeakReference<IEnableEntityComponent>(implementor as IEnableEntityComponent));
  97. var interfaces = implementor.GetType().GetInterfaces();
  98. var weakReference = new DataStructures.WeakReference<object>(implementor);
  99. for (int iindex = 0; iindex < interfaces.Length; iindex++)
  100. {
  101. var componentType = interfaces[iindex];
  102. _implementorsByType[componentType] = weakReference;
  103. #if DEBUG && !PROFILER
  104. if (_implementorCounterByType.ContainsKey(componentType) == false)
  105. _implementorCounterByType[componentType] = 1;
  106. else
  107. _implementorCounterByType[componentType]++;
  108. #endif
  109. }
  110. }
  111. #if DEBUG && !PROFILER
  112. else
  113. Utility.Console.LogError(NULL_IMPLEMENTOR_ERROR.FastConcat(ToString()));
  114. #endif
  115. }
  116. }
  117. void SetupImplementors(
  118. ref BuildNodeCallbackStruct callBackstruct,
  119. ITypeSafeList nodes)
  120. {
  121. var RemoveEntity = callBackstruct._internalRemove;
  122. var DisableEntity = callBackstruct._internalDisable;
  123. var EnableEntity = callBackstruct._internalEnable;
  124. Action removeEntityAction = () => { RemoveEntity(nodes); nodes.Clear(); };
  125. Action disableEntityAction = () => DisableEntity(nodes);
  126. Action enableEntityAction = () => EnableEntity(nodes);
  127. int removingImplementorsCount = _removingImplementors.Count;
  128. for (int index = 0; index < removingImplementorsCount; index++)
  129. _removingImplementors[index].Target.removeEntity = removeEntityAction;
  130. int disablingImplementorsCount = _disablingImplementors.Count;
  131. for (int index = 0; index < disablingImplementorsCount; index++)
  132. _disablingImplementors[index].Target.disableEntity = disableEntityAction;
  133. int enablingImplementorsCount = _enablingImplementors.Count;
  134. for (int index = 0; index < enablingImplementorsCount; index++)
  135. _enablingImplementors[index].Target.enableEntity = enableEntityAction;
  136. }
  137. TNode FillNode<TNode>(TNode node, FillNodeMode mode) where TNode : INode
  138. {
  139. var fields = node.GetType().GetFields(BindingFlags.Public |
  140. BindingFlags.Instance);
  141. for (int i = fields.Length - 1; i >= 0; --i)
  142. {
  143. var field = fields[i];
  144. Type fieldType = field.FieldType;
  145. DataStructures.WeakReference<object> component;
  146. if (_implementorsByType.TryGetValue(fieldType, out component) == false)
  147. {
  148. if (mode == FillNodeMode.Strict)
  149. {
  150. Exception e =
  151. new Exception(NOT_FOUND_EXCEPTION +
  152. field.FieldType.Name + " - Node: " + node.GetType().Name +
  153. " - EntityDescriptor " + this);
  154. throw e;
  155. }
  156. }
  157. else
  158. field.SetValue(node, component.Target);
  159. #if DEBUG && !PROFILER
  160. {
  161. if (_implementorCounterByType[fieldType] != 1)
  162. {
  163. Utility.Console.LogError(
  164. DUPLICATE_IMPLEMENTOR_ERROR.FastConcat("component: ", fieldType.ToString(),
  165. " implementor: ", component.Target.ToString()));
  166. }
  167. }
  168. #endif
  169. }
  170. return node;
  171. }
  172. readonly FasterList<DataStructures.WeakReference<IDisableEntityComponent>> _disablingImplementors = new FasterList<DataStructures.WeakReference<IDisableEntityComponent>>();
  173. readonly FasterList<DataStructures.WeakReference<IRemoveEntityComponent>> _removingImplementors = new FasterList<DataStructures.WeakReference<IRemoveEntityComponent>>();
  174. readonly FasterList<DataStructures.WeakReference<IEnableEntityComponent>> _enablingImplementors = new FasterList<DataStructures.WeakReference<IEnableEntityComponent>>();
  175. readonly Dictionary<Type, DataStructures.WeakReference<object>> _implementorsByType = new Dictionary<Type, DataStructures.WeakReference<object>>();
  176. #if DEBUG && !PROFILER
  177. readonly Dictionary<Type, int> _implementorCounterByType = new Dictionary<Type, int>();
  178. #endif
  179. readonly FasterList<INodeBuilder> _nodesToBuild;
  180. const string DUPLICATE_IMPLEMENTOR_ERROR = "the same component is implemented with more than one implementor. This is considered an error and MUST be fixed. ";
  181. const string NULL_IMPLEMENTOR_ERROR = "Null implementor, are you using a wild GetComponents<Monobehaviour> to fetch it? ";
  182. const string NOT_FOUND_EXCEPTION = "Svelto.ECS: Implementor not found for a Node. Implementor Type: ";
  183. }
  184. }