DragonECS/src/Internal/EcsTypeCodeManager.cs

69 lines
2.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
{
2024-04-28 18:36:24 +08:00
//TODO разработать возможность ручного устанавливания ID типам.
//это нужно для упрощения разработки сетевух
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-10-02 22:13:10 +08:00
internal static class EcsTypeCodeManager
{
2024-10-02 22:13:10 +08:00
private static readonly Dictionary<Type, EcsTypeCode> _codes = new Dictionary<Type, EcsTypeCode>();
2024-02-14 03:04:05 +08:00
private static int _increment = 1;
2024-09-09 18:21:21 +08:00
private static object _lock = new object();
2024-02-14 03:04:05 +08:00
public static int Count
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _codes.Count; }
}
2024-10-02 22:13:10 +08:00
public static EcsTypeCode Get(Type type)
{
2024-09-09 18:21:21 +08:00
lock (_lock)
{
2024-10-02 22:13:10 +08:00
if (!_codes.TryGetValue(type, out EcsTypeCode code))
2024-09-09 18:21:21 +08:00
{
2024-10-02 22:13:10 +08:00
code = (EcsTypeCode)_increment++;
2024-09-09 18:21:21 +08:00
_codes.Add(type, code);
}
return code;
}
2023-11-08 15:15:10 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-10-02 22:13:10 +08:00
public static EcsTypeCode Get<T>() { return EcsTypeCodeCache<T>.code; }
2024-02-14 03:04:05 +08:00
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-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal static class EcsTypeCodeCache<T>
2023-11-08 15:15:10 +08:00
{
2024-10-02 22:13:10 +08:00
public static readonly EcsTypeCode code = EcsTypeCodeManager.Get(typeof(T));
2023-11-08 15:15:10 +08:00
}
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal struct TypeCodeInfo
2023-11-08 15:15:10 +08:00
{
public Type type;
2024-10-02 22:13:10 +08:00
public EcsTypeCode code;
public TypeCodeInfo(Type type, EcsTypeCode code)
2023-11-08 15:15:10 +08:00
{
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
}