Mirror of Svelto.ECS because we're a fan of it
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

39 строки
1.4KB

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