using System; using System.Runtime.CompilerServices; using Svelto.DataStructures; namespace Svelto.ECS { public static class ComponentTypeMap { static readonly FasterDictionary, ComponentID> _componentTypeMap = new FasterDictionary, ComponentID>(); static readonly FasterDictionary _reverseComponentTypeMap = new FasterDictionary(); public static void Add(Type type, ComponentID idData) { _componentTypeMap.Add(type, idData); _reverseComponentTypeMap.Add(idData, type); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ComponentID FetchID(Type type) { if (_componentTypeMap.TryGetValue(type, out var index) == false) { //if warming up is working correctly, this should never happen var componentType = typeof(ComponentTypeID<>).MakeGenericType(type); System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(componentType.TypeHandle); return _componentTypeMap[type]; } return index; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Type FetchType(ComponentID id) { return _reverseComponentTypeMap[id]; } } }