DragonECS/src/Internal/EcsTypeCode.cs

52 lines
1.8 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
{
2024-04-28 18:36:24 +08:00
//TODO разработать возможность ручного устанавливания ID типам.
//это нужно для упрощения разработки сетевух
internal static class EcsTypeCode
{
2023-11-08 15:15:10 +08:00
private static readonly Dictionary<Type, int> _codes = new Dictionary<Type, int>();
2024-02-14 03:04:05 +08:00
private static int _increment = 1;
public static int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _codes.Count; }
}
2023-11-08 15:15:10 +08:00
public static int Get(Type type)
{
2023-11-08 15:15:10 +08:00
if (!_codes.TryGetValue(type, out int code))
{
2024-02-14 03:04:05 +08:00
code = _increment++;
2023-11-08 15:15:10 +08:00
_codes.Add(type, code);
}
2023-11-08 15:15:10 +08:00
return code;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-14 03:04:05 +08:00
public static int Get<T>() { return EcsTypeCodeCache<T>.code; }
public static bool Has(Type type) { return _codes.ContainsKey(type); }
public static bool Has<T>() { return _codes.ContainsKey(typeof(T)); }
public static IEnumerable<TypeCodeInfo> GetDeclaredTypes() { return _codes.Select(o => new TypeCodeInfo(o.Key, o.Value)); }
2023-11-08 15:15:10 +08:00
}
internal static class EcsTypeCodeCache<T>
2023-11-08 15:15:10 +08:00
{
public static readonly int code = EcsTypeCode.Get(typeof(T));
}
internal struct TypeCodeInfo
2023-11-08 15:15:10 +08:00
{
public Type type;
public int code;
public TypeCodeInfo(Type type, int code)
{
this.type = type;
this.code = code;
}
public override string ToString()
{
2024-02-14 03:04:05 +08:00
return this.AutoToString(false);
}
}
2023-11-08 15:15:10 +08:00
}