DragonECS/src/Utils/EcsTypeCode.cs

47 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2023-11-08 15:15:10 +08:00
using System.Linq;
using System.Runtime.CompilerServices;
2023-11-08 15:15:10 +08:00
namespace DCFApixels.DragonECS.Internal
{
2023-11-08 15:15:10 +08:00
public static class EcsTypeCode
{
2023-11-08 15:15:10 +08:00
private static readonly Dictionary<Type, int> _codes = new Dictionary<Type, int>();
private static int _incremetn = 1;
public static int Count => _codes.Count;
public static int Get(Type type)
{
2023-11-08 15:15:10 +08:00
if (!_codes.TryGetValue(type, out int code))
{
2023-11-08 15:15:10 +08:00
code = _incremetn++;
_codes.Add(type, code);
}
2023-11-08 15:15:10 +08:00
return code;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Get<T>() => EcsTypeCodeCache<T>.code;
public static bool Has(Type type) => _codes.ContainsKey(type);
public static bool Has<T>() => _codes.ContainsKey(typeof(T));
public static IEnumerable<TypeCodeInfo> GetDeclared() => _codes.Select(o => new TypeCodeInfo(o.Key, o.Value));
}
public static class EcsTypeCodeCache<T>
{
public static readonly int code = EcsTypeCode.Get(typeof(T));
}
public struct TypeCodeInfo
{
public Type type;
public int code;
public TypeCodeInfo(Type type, int code)
{
this.type = type;
this.code = code;
}
public override string ToString()
{
return this.AutoToString(false) + "\n\r";
}
}
2023-11-08 15:15:10 +08:00
}