Mirror of Svelto.ECS because we're a fan of it
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

108 satır
4.3KB

  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. using Svelto.DataStructures;
  5. using Svelto.Utilities;
  6. namespace Svelto.ECS.Serialization
  7. {
  8. public static class DesignatedHash
  9. {
  10. public static readonly Func<byte[], uint> Hash = Murmur3.MurmurHash3_x86_32;
  11. }
  12. public abstract class SerializableEntityDescriptor<TType> : ISerializableEntityDescriptor
  13. where TType : IEntityDescriptor, new()
  14. {
  15. static SerializableEntityDescriptor()
  16. {
  17. IComponentBuilder[] defaultEntities = EntityDescriptorTemplate<TType>.realDescriptor.componentsToBuild;
  18. var hashNameAttribute = Type.GetCustomAttribute<HashNameAttribute>();
  19. if (hashNameAttribute == null)
  20. {
  21. throw new Exception("HashName attribute not found on the serializable type ".FastConcat(Type.FullName));
  22. }
  23. Hash = DesignatedHash.Hash(Encoding.ASCII.GetBytes(hashNameAttribute._name));
  24. var (index, dynamicIndex) = SetupSpecialEntityComponent(defaultEntities, out ComponentsToBuild);
  25. if (index == -1)
  26. {
  27. index = ComponentsToBuild.Length - 1;
  28. }
  29. // Stores the hash of this EntityDescriptor
  30. ComponentsToBuild[index] = new ComponentBuilder<SerializableEntityComponent>(new SerializableEntityComponent
  31. {
  32. descriptorHash = Hash
  33. });
  34. // If the current serializable is an ExtendibleDescriptor, I have to update it.
  35. if (dynamicIndex != -1)
  36. {
  37. ComponentsToBuild[dynamicIndex] = new ComponentBuilder<EntityInfoComponent>(new EntityInfoComponent
  38. {
  39. componentsToBuild = ComponentsToBuild
  40. });
  41. }
  42. /////
  43. var entitiesToSerialize = new FasterList<ISerializableComponentBuilder>();
  44. EntityComponentsToSerializeMap = new FasterDictionary<ComponentID, ISerializableComponentBuilder>();
  45. foreach (IComponentBuilder e in defaultEntities)
  46. {
  47. if (e is ISerializableComponentBuilder serializableEntityBuilder)
  48. {
  49. EntityComponentsToSerializeMap[serializableEntityBuilder.getComponentID] = serializableEntityBuilder;
  50. entitiesToSerialize.Add(serializableEntityBuilder);
  51. }
  52. }
  53. EntitiesToSerialize = entitiesToSerialize.ToArray();
  54. }
  55. static (int indexSerial, int indexDynamic) SetupSpecialEntityComponent
  56. (IComponentBuilder[] defaultEntities, out IComponentBuilder[] componentsToBuild)
  57. {
  58. int length = defaultEntities.Length;
  59. int newLenght = length + 1;
  60. int indexSerial = -1;
  61. int indexDynamic = -1;
  62. for (var i = 0; i < length; ++i)
  63. {
  64. if (defaultEntities[i].GetEntityComponentType() == SerializableStructType)
  65. {
  66. indexSerial = i;
  67. --newLenght;
  68. }
  69. if (defaultEntities[i].GetEntityComponentType() == ComponentBuilderUtilities.ENTITY_INFO_COMPONENT)
  70. {
  71. indexDynamic = i;
  72. }
  73. }
  74. componentsToBuild = new IComponentBuilder[newLenght];
  75. Array.Copy(defaultEntities, 0, componentsToBuild, 0, length);
  76. return (indexSerial, indexDynamic);
  77. }
  78. public IComponentBuilder[] componentsToBuild => ComponentsToBuild;
  79. public uint hash => Hash;
  80. public Type realType => Type;
  81. public ISerializableComponentBuilder[] componentsToSerialize => EntitiesToSerialize;
  82. static readonly IComponentBuilder[] ComponentsToBuild;
  83. static readonly FasterDictionary<ComponentID, ISerializableComponentBuilder> EntityComponentsToSerializeMap;
  84. static readonly ISerializableComponentBuilder[] EntitiesToSerialize;
  85. static readonly uint Hash;
  86. static readonly Type SerializableStructType = typeof(SerializableEntityComponent);
  87. static readonly Type Type = typeof(TType);
  88. }
  89. }