DragonECS/src/Debug/EcsDebugUtility.cs

65 lines
2.8 KiB
C#
Raw Normal View History

2023-05-23 01:47:28 +08:00
using System;
using System.Reflection;
namespace DCFApixels.DragonECS
{
public static class EcsDebugUtility
{
2023-05-26 04:25:09 +08:00
public static string GetGenericTypeFullName<T>(int maxDepth = 2) => GetGenericTypeFullName(typeof(T), maxDepth);
public static string GetGenericTypeFullName(Type type, int maxDepth = 2) => GetGenericTypeNameInternal(type, maxDepth, true);
2023-05-23 01:47:28 +08:00
public static string GetGenericTypeName<T>(int maxDepth = 2) => GetGenericTypeName(typeof(T), maxDepth);
2023-05-26 04:25:09 +08:00
public static string GetGenericTypeName(Type type, int maxDepth = 2) => GetGenericTypeNameInternal(type, maxDepth, false);
private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull)
2023-05-23 01:47:28 +08:00
{
#if (DEBUG && !DISABLE_DEBUG)
2023-05-26 04:25:09 +08:00
string friendlyName = isFull ? type.FullName : type.Name;
2023-05-23 01:47:28 +08:00
if (!type.IsGenericType || maxDepth == 0)
return friendlyName;
int iBacktick = friendlyName.IndexOf('`');
if (iBacktick > 0)
friendlyName = friendlyName.Remove(iBacktick);
friendlyName += "<";
Type[] typeParameters = type.GetGenericArguments();
for (int i = 0; i < typeParameters.Length; ++i)
{
2023-05-26 04:25:09 +08:00
string typeParamName = GetGenericTypeNameInternal(typeParameters[i], maxDepth - 1, false);//чтобы строка не была слишком длинной, используются сокращенные имена для типов аргументов
2023-05-23 01:47:28 +08:00
friendlyName += (i == 0 ? typeParamName : "," + typeParamName);
}
friendlyName += ">";
return friendlyName;
2023-05-23 02:11:00 +08:00
#else //optimization for release build
2023-05-23 01:47:28 +08:00
return type.Name;
#endif
}
public static string GetName<T>() => GetName(typeof(T));
public static string GetName(Type type)
{
var atr = type.GetCustomAttribute<DebugNameAttribute>();
return atr != null ? atr.name : GetGenericTypeName(type);
}
public static string GetDescription<T>() => GetDescription(typeof(T));
public static string GetDescription(Type type)
{
var atr = type.GetCustomAttribute<DebugDescriptionAttribute>();
return atr != null ? atr.description : string.Empty;
}
public static (byte, byte, byte) GetColorRGB<T>() => GetColorRGB(typeof(T));
public static (byte, byte, byte) GetColorRGB(Type type)
{
var atr = type.GetCustomAttribute<DebugColorAttribute>();
return atr != null ? (atr.r, atr.g, atr.b) : ((byte)255, (byte)255, (byte)255);
}
2023-05-31 04:09:55 +08:00
public static bool IsHidden<T>() => IsHidden(typeof(T));
public static bool IsHidden(Type type)
{
return type.GetCustomAttribute<DebugHideAttribute>() != null;
}
2023-05-23 01:47:28 +08:00
}
}