update TypeMeta

This commit is contained in:
Mikhail 2024-03-10 01:25:08 +08:00
parent 9e70cabd1a
commit bcbb7c917a
3 changed files with 80 additions and 62 deletions

View File

@ -31,7 +31,6 @@ namespace DCFApixels.DragonECS
} }
private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull) private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull)
{ {
#if (DEBUG && !DISABLE_DEBUG)
string typeName = isFull ? type.FullName : type.Name; string typeName = isFull ? type.FullName : type.Name;
if (!type.IsGenericType || maxDepth == 0) if (!type.IsGenericType || maxDepth == 0)
{ {
@ -52,9 +51,6 @@ namespace DCFApixels.DragonECS
genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}"); genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}");
} }
return $"{typeName}<{genericParams}>"; return $"{typeName}<{genericParams}>";
#else //optimization for release build
return isFull ? type.FullName : type.Name;
#endif
} }
#endregion #endregion
@ -354,7 +350,7 @@ namespace DCFApixels.DragonECS
{ {
if (_initFlags.HasFlag(InitFlag.Name) == false) if (_initFlags.HasFlag(InitFlag.Name) == false)
{ {
(_name, _isCustomName) = MetaCacheGenerator.GetName(_type); (_name, _isCustomName) = MetaGenerator.GetMetaName(_type);
_initFlags |= InitFlag.Name; _initFlags |= InitFlag.Name;
} }
} }
@ -381,7 +377,7 @@ namespace DCFApixels.DragonECS
{ {
if (_initFlags.HasFlag(InitFlag.Color) == false) if (_initFlags.HasFlag(InitFlag.Color) == false)
{ {
(_color, _isCustomColor) = MetaCacheGenerator.GetColor(_type); (_color, _isCustomColor) = MetaGenerator.GetColor(_type);
_initFlags |= InitFlag.Color; _initFlags |= InitFlag.Color;
} }
} }
@ -410,7 +406,7 @@ namespace DCFApixels.DragonECS
{ {
if (_initFlags.HasFlag(InitFlag.Description) == false) if (_initFlags.HasFlag(InitFlag.Description) == false)
{ {
_description = MetaCacheGenerator.GetDescription(_type); _description = MetaGenerator.GetDescription(_type);
_initFlags |= InitFlag.Description; _initFlags |= InitFlag.Description;
} }
return _description; return _description;
@ -425,7 +421,7 @@ namespace DCFApixels.DragonECS
{ {
if (_initFlags.HasFlag(InitFlag.Group) == false) if (_initFlags.HasFlag(InitFlag.Group) == false)
{ {
_group = MetaCacheGenerator.GetGroup(_type); _group = MetaGenerator.GetGroup(_type);
_initFlags |= InitFlag.Group; _initFlags |= InitFlag.Group;
} }
return _group; return _group;
@ -438,7 +434,7 @@ namespace DCFApixels.DragonECS
{ {
if (_initFlags.HasFlag(InitFlag.Tags) == false) if (_initFlags.HasFlag(InitFlag.Tags) == false)
{ {
_tags = MetaCacheGenerator.GetTags(_type); _tags = MetaGenerator.GetTags(_type);
_initFlags |= InitFlag.Tags; _initFlags |= InitFlag.Tags;
_isHidden = _tags.Contains(MetaTags.HIDDEN); _isHidden = _tags.Contains(MetaTags.HIDDEN);
} }
@ -542,32 +538,32 @@ namespace DCFApixels.DragonECS
} }
} }
#endregion #endregion
}
public static class TypeMetaDataCachedExtensions #region MetaGenerator
private static class MetaGenerator
{ {
public static TypeMeta GetMeta(this object self) private const int GENERIC_NAME_DEPTH = 3;
{
return EcsDebugUtility.GetTypeMeta(self);
}
public static TypeMeta ToMeta(this Type self)
{
return EcsDebugUtility.GetTypeMeta(self);
}
}
}
namespace DCFApixels.DragonECS.Internal #region GetMetaName
{ public static (string, bool) GetMetaName(Type type)
internal static class MetaCacheGenerator
{
private const int GENERIC_NAME_DEPTH = 2;
#region GetName
public static (string, bool) GetName(Type type)
{ {
bool isCustom = type.TryGetCustomAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false; bool isCustom = type.TryGetCustomAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false;
return (isCustom ? atr.name : EcsDebugUtility.GetGenericTypeName(type, GENERIC_NAME_DEPTH), isCustom); if (isCustom)
{
if ((type.IsGenericType && atr.isHideGeneric == false) == false)
{
return (atr.name, isCustom);
}
string genericParams = "";
Type[] typeParameters = type.GetGenericArguments();
for (int i = 0; i < typeParameters.Length; ++i)
{
string paramTypeName = EcsDebugUtility.GetGenericTypeName(typeParameters[i], GENERIC_NAME_DEPTH);
genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}");
}
return ($"{atr.name}<{genericParams}>", isCustom);
}
return (EcsDebugUtility.GetGenericTypeName(type, GENERIC_NAME_DEPTH), isCustom);
} }
#endregion #endregion
@ -606,4 +602,18 @@ namespace DCFApixels.DragonECS.Internal
} }
#endregion #endregion
} }
#endregion
}
public static class TypeMetaDataCachedExtensions
{
public static TypeMeta GetMeta(this object self)
{
return EcsDebugUtility.GetTypeMeta(self);
}
public static TypeMeta ToMeta(this Type self)
{
return EcsDebugUtility.GetTypeMeta(self);
}
}
} }

View File

@ -6,6 +6,9 @@ namespace DCFApixels.DragonECS
public sealed class MetaDescriptionAttribute : EcsMetaAttribute public sealed class MetaDescriptionAttribute : EcsMetaAttribute
{ {
public readonly string description; public readonly string description;
public MetaDescriptionAttribute(string description) => this.description = description; public MetaDescriptionAttribute(string description)
{
this.description = description;
}
} }
} }

View File

@ -6,6 +6,11 @@ namespace DCFApixels.DragonECS
public sealed class MetaNameAttribute : EcsMetaAttribute public sealed class MetaNameAttribute : EcsMetaAttribute
{ {
public readonly string name; public readonly string name;
public MetaNameAttribute(string name) => this.name = name; public readonly bool isHideGeneric;
public MetaNameAttribute(string name, bool isHideGeneric = false)
{
this.name = name;
this.isHideGeneric = isHideGeneric;
}
} }
} }