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.

EntitiesDB.DescriptorMap.cs 3.9KB

Svelto.ECS 2.9 changes (random order of importance): New Serialization framework more thorough disposing of the EnginesRoot an EnginesRoot reference should never be held, unless it’s a weak reference. The code changed to stick to this rule IReactOnAddAndRemove callbacks are now guaranteed to be called after all the entity structs generated by the same entity have been added and before any is removed. both functions pass the EGID of the analysing entity by parameter now, so that the entity struct won’t need to implement INeedEGID for this sole purpose. The IReactOnSwap MovedFrom method has been removed, it is now redundant. Entities built or removed during the IReactOnAddAndRemove callbacks are now added and removed immediately and not on the next submission like used to happen. This avoid some awkward checks that were previously needed inside engines. EntityStreams can get (optionally) the EGID of the entity published, so that the EntityStruct won’t need an INeedEGID for this sole purpose. Groups are not trimmed anymore when they are emptied to avoid allocations. Removed a bunch of run-time allocations that weren’t supposed to happen in Release and/or when the Profile define is used in editor (for debugging reasons Svelto.ECS may need to use strings at run-time, but Svelto.ECS is allocation zero in Release and when the Profile keyword is used) A improved the DynamicEntityDescriptor and ExtendibleEntityDescriptor code and more notably, introduced the new method ExtendedWith<> to facilitate the writing of modular and reusable entity descriptors. Several minor code design improvements/optimisations
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Svelto.ECS.Serialization;
  5. namespace Svelto.ECS
  6. {
  7. partial class EnginesRoot
  8. {
  9. sealed class SerializationDescriptorMap
  10. {
  11. /// <summary>
  12. /// Here we want to register all the EntityDescriptors that need to be serialized for network play.
  13. ///
  14. /// Remember! This needs to in sync across different clients and server as the values are serialized across
  15. /// the network also want this to not change so we can save to a DB
  16. /// </summary>
  17. internal SerializationDescriptorMap()
  18. {
  19. _descriptors = new Dictionary<uint, ISerializableEntityDescriptor>();
  20. _factories = new Dictionary<uint, IDeserializationFactory>();
  21. Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
  22. foreach (Assembly assembly in assemblies)
  23. {
  24. foreach (Type type in GetTypesSafe(assembly))
  25. {
  26. if (type != null && type.IsClass && type.GetConstructor(Type.EmptyTypes) != null &&
  27. typeof(ISerializableEntityDescriptor).IsAssignableFrom(type))
  28. {
  29. var descriptor = Activator.CreateInstance(type) as ISerializableEntityDescriptor;
  30. RegisterEntityDescriptor(descriptor);
  31. }
  32. }
  33. }
  34. }
  35. static IEnumerable<Type> GetTypesSafe(Assembly assembly)
  36. {
  37. try
  38. {
  39. Type[] types = assembly.GetTypes();
  40. return types;
  41. }
  42. catch (ReflectionTypeLoadException e)
  43. {
  44. return e.Types;
  45. }
  46. }
  47. void RegisterEntityDescriptor(ISerializableEntityDescriptor descriptor)
  48. {
  49. if (descriptor == null)
  50. {
  51. return;
  52. }
  53. uint descriptorHash = descriptor.hash;
  54. #if DEBUG && !PROFILER
  55. if (_descriptors.ContainsKey(descriptorHash))
  56. {
  57. throw new Exception($"Hash Collision of '{descriptor.GetType()}' against " +
  58. $"'{_descriptors[descriptorHash]} ::: {descriptorHash}'");
  59. }
  60. #endif
  61. _descriptors[descriptorHash] = descriptor;
  62. }
  63. public ISerializableEntityDescriptor GetDescriptorFromHash(uint descriptorID)
  64. {
  65. #if DEBUG && !PROFILER
  66. DBC.ECS.Check.Require(_descriptors.ContainsKey(descriptorID),
  67. $"Could not find descriptor with ID '{descriptorID}'!");
  68. #endif
  69. return _descriptors[descriptorID];
  70. }
  71. public IDeserializationFactory GetSerializationFactory(uint descriptorID)
  72. {
  73. return _factories.TryGetValue(descriptorID, out var factory) ? factory : null;
  74. }
  75. public void RegisterSerializationFactory<Descriptor>(IDeserializationFactory deserializationFactory)
  76. where Descriptor : ISerializableEntityDescriptor, new()
  77. {
  78. _factories.Add(SerializationEntityDescriptorTemplate<Descriptor>.hash, deserializationFactory);
  79. }
  80. readonly Dictionary<uint, ISerializableEntityDescriptor> _descriptors;
  81. readonly Dictionary<uint, IDeserializationFactory> _factories;
  82. }
  83. /// <summary>
  84. /// The map of serializable entity hashes to the serializable entity builders (to know the entity structs
  85. /// to serialize)
  86. /// </summary>
  87. SerializationDescriptorMap serializationDescriptorMap { get; } = new SerializationDescriptorMap();
  88. }
  89. }