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,6 +538,71 @@ namespace DCFApixels.DragonECS
} }
} }
#endregion #endregion
#region MetaGenerator
private static class MetaGenerator
{
private const int GENERIC_NAME_DEPTH = 3;
#region GetMetaName
public static (string, bool) GetMetaName(Type type)
{
bool isCustom = type.TryGetCustomAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false;
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
#region GetColor
private static MetaColor AutoColor(Type type)
{
return new MetaColor(type.Name).Desaturate(0.48f) / 1.18f;
}
public static (MetaColor, bool) GetColor(Type type)
{
bool isCustom = type.TryGetCustomAttribute(out MetaColorAttribute atr);
return (isCustom ? atr.color : AutoColor(type), isCustom);
}
#endregion
#region GetGroup
public static MetaGroup GetGroup(Type type)
{
return type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.Data : MetaGroup.Empty;
}
#endregion
#region GetDescription
public static string GetDescription(Type type)
{
bool isCustom = type.TryGetCustomAttribute(out MetaDescriptionAttribute atr);
return isCustom ? atr.description : string.Empty;
}
#endregion
#region GetTags
public static IReadOnlyCollection<string> GetTags(Type type)
{
var atr = type.GetCustomAttribute<MetaTagsAttribute>();
return atr != null ? atr.Tags : Array.Empty<string>();
}
#endregion
}
#endregion
} }
public static class TypeMetaDataCachedExtensions public static class TypeMetaDataCachedExtensions
@ -555,55 +616,4 @@ namespace DCFApixels.DragonECS
return EcsDebugUtility.GetTypeMeta(self); return EcsDebugUtility.GetTypeMeta(self);
} }
} }
}
namespace DCFApixels.DragonECS.Internal
{
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;
return (isCustom ? atr.name : EcsDebugUtility.GetGenericTypeName(type, GENERIC_NAME_DEPTH), isCustom);
}
#endregion
#region GetColor
private static MetaColor AutoColor(Type type)
{
return new MetaColor(type.Name).Desaturate(0.48f) / 1.18f;
}
public static (MetaColor, bool) GetColor(Type type)
{
bool isCustom = type.TryGetCustomAttribute(out MetaColorAttribute atr);
return (isCustom ? atr.color : AutoColor(type), isCustom);
}
#endregion
#region GetGroup
public static MetaGroup GetGroup(Type type)
{
return type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.Data : MetaGroup.Empty;
}
#endregion
#region GetDescription
public static string GetDescription(Type type)
{
bool isCustom = type.TryGetCustomAttribute(out MetaDescriptionAttribute atr);
return isCustom ? atr.description : string.Empty;
}
#endregion
#region GetTags
public static IReadOnlyCollection<string> GetTags(Type type)
{
var atr = type.GetCustomAttribute<MetaTagsAttribute>();
return atr != null ? atr.Tags : Array.Empty<string>();
}
#endregion
}
} }

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;
}
} }
} }