Mirror of Svelto.ECS because we're a fan of it
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8KB

  1. using System.Threading;
  2. using Svelto.Common;
  3. using Svelto.DataStructures;
  4. using Svelto.ECS.Internal;
  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, _IInternalEntityComponent
  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. #if UNITY_NATIVE //at the moment I am still considering NativeOperations useful only for Unity
  31. static class EntityComponentID<T>
  32. {
  33. internal static readonly Unity.Burst.SharedStatic<uint> ID =
  34. Unity.Burst.SharedStatic<uint>.GetOrCreate<GlobalTypeID, T>();
  35. }
  36. static class EntityComponentIDMap
  37. {
  38. static readonly Svelto.DataStructures.FasterList<IFiller> TYPE_IDS;
  39. static EntityComponentIDMap()
  40. {
  41. TYPE_IDS = new Svelto.DataStructures.FasterList<IFiller>();
  42. }
  43. internal static void Register<T>(IFiller entityBuilder) where T : struct, _IInternalEntityComponent
  44. {
  45. var location = EntityComponentID<T>.ID.Data = GlobalTypeID.NextID<T>();
  46. TYPE_IDS.AddAt(location, entityBuilder);
  47. }
  48. internal static IFiller GetTypeFromID(uint typeId) { return TYPE_IDS[typeId]; }
  49. }
  50. #endif
  51. }