Mirror of Svelto.ECS because we're a fan of it
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

38 líneas
1.4KB

  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Svelto.DataStructures;
  4. namespace Svelto.ECS
  5. {
  6. public static class ComponentTypeMap
  7. {
  8. static readonly FasterDictionary<RefWrapper<Type>, ComponentID> _componentTypeMap = new FasterDictionary<RefWrapper<Type>, ComponentID>();
  9. static readonly FasterDictionary<ComponentID, Type> _reverseComponentTypeMap = new FasterDictionary<ComponentID, Type>();
  10. public static void Add(Type type, ComponentID idData)
  11. {
  12. _componentTypeMap.Add(type, idData);
  13. _reverseComponentTypeMap.Add(idData, type);
  14. }
  15. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  16. public static ComponentID FetchID(Type type)
  17. {
  18. if (_componentTypeMap.TryGetValue(type, out var index) == false)
  19. {
  20. //if warming up is working correctly, this should never happen
  21. var componentType = typeof(ComponentTypeID<>).MakeGenericType(type);
  22. System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(componentType.TypeHandle);
  23. return _componentTypeMap[type];
  24. }
  25. return index;
  26. }
  27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  28. public static Type FetchType(ComponentID id)
  29. {
  30. return _reverseComponentTypeMap[id];
  31. }
  32. }
  33. }