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.

EntityViewUtility.cs 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using Svelto.DataStructures;
  4. using Svelto.Utilities;
  5. namespace Svelto.ECS
  6. {
  7. #if DEBUG && !PROFILE_SVELTO
  8. struct ECSTuple<T1, T2>
  9. {
  10. public readonly T1 instance;
  11. public T2 numberOfImplementations;
  12. public ECSTuple(T1 implementor, T2 v)
  13. {
  14. instance = implementor;
  15. numberOfImplementations = v;
  16. }
  17. }
  18. #endif
  19. static class EntityComponentUtility
  20. {
  21. const string DUPLICATE_IMPLEMENTOR_ERROR =
  22. "<color=teal>Svelto.ECS</color> the same component is implemented with more than one implementor. This is "
  23. + "considered an error and MUST be fixed. ";
  24. const string NULL_IMPLEMENTOR_ERROR =
  25. "<color=teal>Svelto.ECS</color> Null implementor, please be careful about the implementors passed to avoid "
  26. + "performance loss ";
  27. const string NOT_FOUND_EXCEPTION =
  28. "<color=teal>Svelto.ECS</color> Implementor not found for an EntityComponent. ";
  29. public static void FillEntityComponent<T>
  30. (this IComponentBuilder componentBuilder, ref T entityComponent
  31. , FasterList<KeyValuePair<Type, FastInvokeActionCast<T>>> entityComponentBlazingFastReflection
  32. , IEnumerable<object> implementors
  33. #if DEBUG && !PROFILE_SVELTO
  34. ,Dictionary<Type, ECSTuple<object, int>> implementorsByType
  35. #else
  36. , Dictionary<Type, object> implementorsByType
  37. #endif
  38. , Dictionary<Type, Type[]> cachedTypeInterfaces)
  39. {
  40. //efficient way to collect the fields of every EntityComponentType
  41. var setters = FasterList<KeyValuePair<Type, FastInvokeActionCast<T>>>.NoVirt.ToArrayFast(
  42. entityComponentBlazingFastReflection, out var count);
  43. //todo this should happen once per T, not once per Build<T>
  44. if (implementors != null)
  45. {
  46. foreach (var implementor in implementors)
  47. {
  48. if (implementor != null)
  49. {
  50. var type = implementor.GetType();
  51. if (cachedTypeInterfaces.TryGetValue(type, out var interfaces) == false)
  52. interfaces = cachedTypeInterfaces[type] = type.GetInterfacesEx();
  53. for (var iindex = 0; iindex < interfaces.Length; iindex++)
  54. {
  55. var componentType = interfaces[iindex];
  56. #if DEBUG && !PROFILE_SVELTO
  57. if (implementorsByType.TryGetValue(componentType, out var implementorData))
  58. {
  59. implementorData.numberOfImplementations++;
  60. implementorsByType[componentType] = implementorData;
  61. }
  62. else
  63. implementorsByType[componentType] = new ECSTuple<object, int>(implementor, 1);
  64. #else
  65. implementorsByType[componentType] = implementor;
  66. #endif
  67. }
  68. }
  69. #if DEBUG && !PROFILE_SVELTO
  70. else
  71. {
  72. Console.Log(NULL_IMPLEMENTOR_ERROR.FastConcat(" entityComponent "
  73. , componentBuilder
  74. .GetEntityComponentType().ToString()));
  75. }
  76. #endif
  77. }
  78. }
  79. for (var i = 0; i < count; i++)
  80. {
  81. var fieldSetter = setters[i];
  82. var fieldType = fieldSetter.Key;
  83. #if DEBUG && !PROFILE_SVELTO
  84. ECSTuple<object, int> implementor;
  85. #else
  86. object implementor;
  87. #endif
  88. if (implementorsByType.TryGetValue(fieldType, out implementor) == false)
  89. {
  90. var e = new ECSException(NOT_FOUND_EXCEPTION + " Component Type: " + fieldType.Name
  91. + " - EntityComponent: " + componentBuilder.GetEntityComponentType().Name);
  92. throw e;
  93. }
  94. #if DEBUG && !PROFILE_SVELTO
  95. if (implementor.numberOfImplementations > 1)
  96. throw new ECSException(DUPLICATE_IMPLEMENTOR_ERROR.FastConcat(
  97. "Component Type: ", fieldType.Name, " implementor: ", implementor.instance.ToString()) +
  98. " - EntityComponent: " + componentBuilder.GetEntityComponentType().Name);
  99. #endif
  100. #if DEBUG && !PROFILE_SVELTO
  101. fieldSetter.Value(ref entityComponent, implementor.instance);
  102. #else
  103. fieldSetter.Value(ref entityComponent, implementor);
  104. #endif
  105. }
  106. implementorsByType.Clear();
  107. }
  108. }
  109. }