update type meta

This commit is contained in:
Mikhail 2024-09-27 21:46:16 +08:00
parent 639cd64cc7
commit f9eaa81f03
3 changed files with 64 additions and 58 deletions

View File

@ -280,6 +280,7 @@ namespace DCFApixels.DragonECS
#region GetTypeMeta #region GetTypeMeta
public static TypeMeta GetTypeMeta(object obj) public static TypeMeta GetTypeMeta(object obj)
{ {
if (obj == null) { return TypeMeta.NullTypeMeta; }
return TypeMeta.Get(GetTypeMetaSource(obj).GetType()); return TypeMeta.Get(GetTypeMetaSource(obj).GetType());
} }
public static TypeMeta GetTypeMeta<T>() public static TypeMeta GetTypeMeta<T>()

View File

@ -18,6 +18,7 @@ namespace DCFApixels.DragonECS
{ {
Throw.ArgumentException($"Аргумент {nameof(id)} не может содержать символ запятой ','"); Throw.ArgumentException($"Аргумент {nameof(id)} не может содержать символ запятой ','");
} }
id = string.Intern(id);
ID = id; ID = id;
} }
} }
@ -25,7 +26,7 @@ namespace DCFApixels.DragonECS
public static class MetaIDUtility public static class MetaIDUtility
{ {
[ThreadStatic] [ThreadStatic]
private static System.Random _randon; private static Random _randon;
[ThreadStatic] [ThreadStatic]
private static byte[] _buffer; private static byte[] _buffer;
[ThreadStatic] [ThreadStatic]
@ -38,7 +39,7 @@ namespace DCFApixels.DragonECS
IntPtr prt = Marshal.AllocHGlobal(1); IntPtr prt = Marshal.AllocHGlobal(1);
long alloc = (long)prt; long alloc = (long)prt;
Marshal.Release(prt); Marshal.Release(prt);
_randon = new System.Random((int)alloc); _randon = new Random((int)alloc);
_buffer = new byte[8]; _buffer = new byte[8];
_isInit = true; _isInit = true;
} }

View File

@ -17,15 +17,14 @@ namespace DCFApixels.DragonECS
MetaDescription Description { get; } MetaDescription Description { get; }
MetaGroup Group { get; } MetaGroup Group { get; }
IReadOnlyList<string> Tags { get; } IReadOnlyList<string> Tags { get; }
//string MetaID { get; }
ITypeMeta BaseMeta { get; } ITypeMeta BaseMeta { get; }
} }
public static class ITypeMetaExstensions public static class ITypeMetaExstensions
{ {
public static TypeMeta FindRootTypeMeta(this ITypeMeta meta) public static TypeMeta FindRootTypeMeta(this ITypeMeta meta)
{ {
ITypeMeta result = null; ITypeMeta result = meta;
while ((result = meta.BaseMeta) != null) { } while (result.BaseMeta != null) { result = meta.BaseMeta; }
return (TypeMeta)result; return (TypeMeta)result;
} }
} }
@ -36,8 +35,14 @@ namespace DCFApixels.DragonECS
[DebuggerTypeProxy(typeof(DebuggerProxy))] [DebuggerTypeProxy(typeof(DebuggerProxy))]
public sealed class TypeMeta : ITypeMeta public sealed class TypeMeta : ITypeMeta
{ {
private static readonly Dictionary<Type, TypeMeta> _metaCache = new Dictionary<Type, TypeMeta>(); private const string NULL_NAME = "NULL";
public static readonly TypeMeta NullTypeMeta;
private static object _lock = new object();
private static readonly Dictionary<Type, TypeMeta> _metaCache = new Dictionary<Type, TypeMeta>();
private static int _increment = 1;
private readonly int _uniqueID;
internal readonly Type _type; internal readonly Type _type;
private bool _isCustomName; private bool _isCustomName;
@ -56,38 +61,31 @@ namespace DCFApixels.DragonECS
private InitFlag _initFlags = InitFlag.None; private InitFlag _initFlags = InitFlag.None;
private static object _lock = new object();
//private EcsMemberType _memberType;
public ITypeMeta BaseMeta
{
get { return null; }
}
private static bool CheckEcsMemener(Type checkedType)
{
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
return checkedType.IsInterface == false && checkedType.IsAbstract == false && typeof(IEcsMember).IsAssignableFrom(checkedType);
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetTags)} method does not work.");
return false;
#endif
}
public static bool IsHasMeta(Type type)
{
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
return (CheckEcsMemener(type) || Attribute.GetCustomAttributes(type, typeof(EcsMetaAttribute), false).Length > 0);
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetTags)} method does not work.");
return false;
#endif
}
#region Constructors #region Constructors
static TypeMeta()
{
NullTypeMeta = new TypeMeta(typeof(void))
{
_isCustomName = false,
_isCustomColor = true,
_isHidden = true,
_name = NULL_NAME,
_typeName = NULL_NAME,
_color = new MetaColor(MetaColor.Black),
_description = new MetaDescription("", NULL_NAME),
_group = new MetaGroup(""),
_tags = Array.Empty<string>(),
_metaID = string.Empty,
_typeCode = EcsTypeCode.Get(typeof(void)),
_initFlags = InitFlag.All,
};
_metaCache.Add(typeof(void), NullTypeMeta);
}
public static TypeMeta Get(Type type) public static TypeMeta Get(Type type)
{ {
lock (_lock) lock (_lock) //TODO посмотреть можно ли тут убрать лок
{ {
if (_metaCache.TryGetValue(type, out TypeMeta result) == false) if (_metaCache.TryGetValue(type, out TypeMeta result) == false)
{ {
@ -99,6 +97,7 @@ namespace DCFApixels.DragonECS
} }
private TypeMeta(Type type) private TypeMeta(Type type)
{ {
_uniqueID = _increment++;
_type = type; _type = type;
} }
#endregion #endregion
@ -110,13 +109,6 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
//#region EcsMemberType
//public EcsMemberType EcsMemberType
//{
// get { return _memberType; }
//}
//#endregion
#region Name #region Name
private void InitName() private void InitName()
{ {
@ -308,21 +300,42 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Other #region Other
public override string ToString() ITypeMeta ITypeMeta.BaseMeta
{ {
return Name; get { return null; }
} }
public override int GetHashCode() private static bool CheckEcsMemener(Type checkedType)
{ {
return _color.GetHashCode() ^ _name[0].GetHashCode() ^ _name[_name.Length - 1].GetHashCode(); #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
return checkedType.IsInterface == false && checkedType.IsAbstract == false && typeof(IEcsMember).IsAssignableFrom(checkedType);
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(TypeMeta)}.{nameof(CheckEcsMemener)} method does not work.");
return false;
#endif
} }
public static bool IsHasMeta(Type type)
{
#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED
return (CheckEcsMemener(type) || Attribute.GetCustomAttributes(type, typeof(EcsMetaAttribute), false).Length > 0);
#else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(TypeMeta)}.{nameof(IsHasMeta)} method does not work.");
return false;
#endif
}
public override string ToString() { return Name; }
/// <returns> Unique ID </returns>
public override int GetHashCode() { return _uniqueID; }
private class DebuggerProxy : ITypeMeta private class DebuggerProxy : ITypeMeta
{ {
private readonly TypeMeta _meta; private readonly TypeMeta _meta;
public ITypeMeta BaseMeta public int UniqueID
{ {
get { return _meta.BaseMeta; } get { return _meta._uniqueID; }
}
ITypeMeta ITypeMeta.BaseMeta
{
get { return null; }
} }
public Type Type public Type Type
{ {
@ -483,7 +496,7 @@ namespace DCFApixels.DragonECS
return atr.ID; return atr.ID;
} }
#else #else
EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetTags)} method does not work."); EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetMetaID)} method does not work.");
return string.Empty; return string.Empty;
#endif #endif
} }
@ -491,13 +504,4 @@ namespace DCFApixels.DragonECS
} }
#endregion #endregion
} }
//public enum EcsMemberType : byte
//{
// Undefined = 0,
//
// Component = 1,
// System = 2,
// Other = 3,
//}
} }