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.

70 lines
1.8KB

  1. using System.Threading;
  2. using Svelto.Common;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.DataStructures;
  5. namespace Svelto.ECS
  6. {
  7. public class GlobalTypeID
  8. {
  9. internal static uint NextID<T>() { return (uint) (Interlocked.Increment(ref value) - 1); }
  10. static GlobalTypeID() { value = 0; }
  11. static int value;
  12. }
  13. interface IFiller
  14. {
  15. void FillFromByteArray(EntityInitializer init, NativeBag buffer);
  16. }
  17. class Filler<T> : IFiller where T : struct, IEntityComponent
  18. {
  19. static Filler()
  20. {
  21. DBC.ECS.Check.Require(TypeCache<T>.isUnmanaged == true, "invalid type used");
  22. }
  23. //it's an internal interface
  24. public void FillFromByteArray(EntityInitializer init, NativeBag buffer)
  25. {
  26. var component = buffer.Dequeue<T>();
  27. init.Init(component);
  28. }
  29. }
  30. static class EntityComponentID<T>
  31. {
  32. #if UNITY_NATIVE
  33. internal static readonly Unity.Burst.SharedStatic<uint> ID =
  34. Unity.Burst.SharedStatic<uint>.GetOrCreate<GlobalTypeID, T>();
  35. #else
  36. internal struct SharedStatic
  37. {
  38. public uint Data;
  39. }
  40. internal static SharedStatic ID;
  41. #endif
  42. }
  43. static class EntityComponentIDMap
  44. {
  45. static readonly FasterList<IFiller> TYPE_IDS;
  46. static EntityComponentIDMap()
  47. {
  48. TYPE_IDS = new FasterList<IFiller>();
  49. }
  50. internal static void Register<T>(IFiller entityBuilder) where T : struct, IEntityComponent
  51. {
  52. var location = EntityComponentID<T>.ID.Data = GlobalTypeID.NextID<T>();
  53. TYPE_IDS.AddAt(location, entityBuilder);
  54. }
  55. internal static IFiller GetTypeFromID(uint typeId) { return TYPE_IDS[typeId]; }
  56. }
  57. }