2023-06-27 05:09:41 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2023-11-08 15:15:10 +08:00
|
|
|
|
using System.Linq;
|
2023-07-04 00:00:25 +08:00
|
|
|
|
using System.Runtime.CompilerServices;
|
2023-06-27 05:09:41 +08:00
|
|
|
|
|
2023-11-08 15:15:10 +08:00
|
|
|
|
namespace DCFApixels.DragonECS.Internal
|
2023-06-27 05:09:41 +08:00
|
|
|
|
{
|
2024-02-22 15:39:37 +08:00
|
|
|
|
internal static class EcsTypeCode
|
2023-06-27 05:09:41 +08:00
|
|
|
|
{
|
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-06-27 05:09:41 +08:00
|
|
|
|
{
|
2023-11-08 15:15:10 +08:00
|
|
|
|
if (!_codes.TryGetValue(type, out int code))
|
2023-06-27 05:09:41 +08:00
|
|
|
|
{
|
2024-02-14 03:04:05 +08:00
|
|
|
|
code = _increment++;
|
2023-11-08 15:15:10 +08:00
|
|
|
|
_codes.Add(type, code);
|
2023-06-29 00:56:26 +08:00
|
|
|
|
}
|
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
|
|
|
|
}
|
2024-02-22 15:39:37 +08:00
|
|
|
|
internal static class EcsTypeCodeCache<T>
|
2023-11-08 15:15:10 +08:00
|
|
|
|
{
|
|
|
|
|
public static readonly int code = EcsTypeCode.Get(typeof(T));
|
|
|
|
|
}
|
2024-02-22 15:39:37 +08:00
|
|
|
|
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-06-27 05:09:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-08 15:15:10 +08:00
|
|
|
|
}
|