DragonECS/src/Internal/EcsTypeCodeManager.cs

121 lines
3.9 KiB
C#
Raw Normal View History

2025-03-14 16:53:25 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
using System;
using System.Collections.Generic;
2024-11-05 17:16:50 +08:00
using System.Diagnostics;
2023-11-08 15:15:10 +08:00
using System.Linq;
using System.Runtime.CompilerServices;
2024-11-03 18:57:56 +08:00
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
#endif
2023-11-08 15:15:10 +08:00
namespace DCFApixels.DragonECS.Internal
{
2024-04-28 18:36:24 +08:00
//TODO разработать возможность ручного устанавливания ID типам.
2024-11-05 15:54:56 +08:00
//это может быть полезно как детерминированность для сети
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-10-02 22:13:10 +08:00
internal static class EcsTypeCodeManager
{
2024-11-05 17:16:50 +08:00
private static readonly Dictionary<EcsTypeCodeKey, EcsTypeCode> _codes = new Dictionary<EcsTypeCodeKey, 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); }
2024-11-05 17:16:50 +08:00
public static EcsTypeCodeKey FindTypeOfCode(EcsTypeCode typeCode)
2024-10-31 14:46:21 +08:00
{
foreach (var item in _codes)
{
if (item.Value == typeCode)
{
return item.Key;
}
}
return null;
}
2024-02-14 03:04:05 +08:00
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
{
2024-11-05 17:16:50 +08:00
public EcsTypeCodeKey type;
2024-10-02 22:13:10 +08:00
public EcsTypeCode code;
2024-11-05 17:16:50 +08:00
public TypeCodeInfo(EcsTypeCodeKey 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);
}
}
2025-03-21 19:52:34 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
[DebuggerDisplay("{" + nameof(ToString) + "()}")]
internal readonly struct EcsTypeCodeKey : IEquatable<EcsTypeCodeKey>
{
public readonly Type Type;
public readonly string NameKey;
public EcsTypeCodeKey(Type type, string nameKey)
{
Type = type;
NameKey = nameKey;
}
public bool Equals(EcsTypeCodeKey other)
{
return Type == other.Type && NameKey == other.NameKey;
}
public override bool Equals(object obj)
{
return obj is EcsTypeCodeKey other && Equals(other);
}
public override int GetHashCode()
{
return HashCode.Combine(Type, NameKey);
}
public override string ToString()
{
if (string.IsNullOrEmpty(NameKey))
{
return Type.ToString();
}
return $"{Type} {NameKey}";
}
public static implicit operator EcsTypeCodeKey(Type type) { return new EcsTypeCodeKey(type, string.Empty); }
}
2023-11-08 15:15:10 +08:00
}