2024-03-06 21:35:34 +08:00
using DCFApixels.DragonECS.Internal ;
using System ;
2023-06-16 11:53:59 +08:00
using System.Collections.Generic ;
2024-03-09 23:20:29 +08:00
using System.Diagnostics ;
2023-12-20 23:16:57 +08:00
using System.Linq ;
2023-05-23 01:47:28 +08:00
using System.Reflection ;
namespace DCFApixels.DragonECS
{
public static class EcsDebugUtility
{
2023-11-01 02:30:19 +08:00
private const BindingFlags RFL_FLAGS = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
2024-03-06 21:35:34 +08:00
private static readonly Dictionary < Type , TypeMeta > _metaCache = new Dictionary < Type , TypeMeta > ( ) ;
2023-11-01 02:30:19 +08:00
2023-06-16 12:34:54 +08:00
#region GetGenericTypeName
2024-03-06 21:35:34 +08:00
public static string GetGenericTypeFullName < T > ( int maxDepth = 2 )
{
return GetGenericTypeFullName ( typeof ( T ) , maxDepth ) ;
}
public static string GetGenericTypeFullName ( Type type , int maxDepth = 2 )
{
return GetGenericTypeNameInternal ( type , maxDepth , true ) ;
}
public static string GetGenericTypeName < T > ( int maxDepth = 2 )
{
return GetGenericTypeName ( typeof ( T ) , maxDepth ) ;
}
public static string GetGenericTypeName ( Type type , int maxDepth = 2 )
{
return GetGenericTypeNameInternal ( type , maxDepth , false ) ;
}
2023-05-26 04:25:09 +08:00
private static string GetGenericTypeNameInternal ( Type type , int maxDepth , bool isFull )
2023-05-23 01:47:28 +08:00
{
2024-03-06 21:35:34 +08:00
string typeName = isFull ? type . FullName : type . Name ;
2023-05-23 01:47:28 +08:00
if ( ! type . IsGenericType | | maxDepth = = 0 )
2024-03-06 21:35:34 +08:00
{
return typeName ;
}
int genericInfoIndex = typeName . LastIndexOf ( '`' ) ;
if ( genericInfoIndex > 0 )
{
typeName = typeName . Remove ( genericInfoIndex ) ;
}
2023-05-23 01:47:28 +08:00
2024-03-06 21:35:34 +08:00
string genericParams = "" ;
2023-05-23 01:47:28 +08:00
Type [ ] typeParameters = type . GetGenericArguments ( ) ;
for ( int i = 0 ; i < typeParameters . Length ; + + i )
{
2024-03-06 21:35:34 +08:00
//чтобы строка не была слишком длинной, используются сокращенные имена для типов аргументов
string paramTypeName = GetGenericTypeNameInternal ( typeParameters [ i ] , maxDepth - 1 , false ) ;
genericParams + = ( i = = 0 ? paramTypeName : $", {paramTypeName}" ) ;
2023-05-23 01:47:28 +08:00
}
2024-03-06 21:35:34 +08:00
return $"{typeName}<{genericParams}>" ;
2023-05-23 01:47:28 +08:00
}
2023-06-16 12:34:54 +08:00
#endregion
2023-05-23 01:47:28 +08:00
2023-06-22 10:50:47 +08:00
#region AutoToString
2023-06-22 10:58:57 +08:00
/// <summary> slow but automatic conversion of ValueType to string in the format "name(field1, field2... fieldn)" </summary>
2023-06-22 10:50:47 +08:00
public static string AutoToString < T > ( this T self , bool isWriteName = true ) where T : struct
{
return AutoToString ( self , typeof ( T ) , isWriteName ) ;
}
2023-06-22 10:58:57 +08:00
private static string AutoToString ( object target , Type type , bool isWriteName )
2023-06-22 10:50:47 +08:00
{
2024-02-23 18:34:40 +08:00
#if ! REFLECTION_DISABLED
2024-01-18 21:25:26 +08:00
//TODO сделать специальный вывод в виде названий констант для Enum-ов
2024-02-23 18:34:40 +08:00
#pragma warning disable IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.
2023-11-01 02:30:19 +08:00
var fields = type . GetFields ( RFL_FLAGS ) ;
2024-02-23 18:34:40 +08:00
#pragma warning restore IL2070
2023-06-22 10:50:47 +08:00
string [ ] values = new string [ fields . Length ] ;
for ( int i = 0 ; i < fields . Length ; i + + )
2024-02-26 11:54:18 +08:00
{
2023-06-22 11:00:33 +08:00
values [ i ] = ( fields [ i ] . GetValue ( target ) ? ? "NULL" ) . ToString ( ) ;
2024-02-26 11:54:18 +08:00
}
2023-06-22 10:58:57 +08:00
if ( isWriteName )
2024-02-26 11:54:18 +08:00
{
2023-06-22 10:50:47 +08:00
return $"{type.Name}({string.Join(" , ", values)})" ;
2024-02-26 11:54:18 +08:00
}
2023-06-22 10:50:47 +08:00
else
2024-02-26 11:54:18 +08:00
{
2023-06-22 10:50:47 +08:00
return $"({string.Join(" , ", values)})" ;
2024-02-26 11:54:18 +08:00
}
2024-02-26 10:43:37 +08:00
#else
2024-02-23 18:34:40 +08:00
EcsDebug . PrintWarning ( $"Reflection is not available, the {nameof(AutoToString)} method does not work." ) ;
return string . Empty ;
2024-02-26 10:43:37 +08:00
#endif
2023-06-22 10:50:47 +08:00
}
#endregion
2023-06-16 12:34:54 +08:00
#region GetName
2024-03-06 21:35:34 +08:00
public static string GetMetaName ( object obj )
2023-10-31 03:03:13 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( obj ) . Name ;
2023-10-31 03:03:13 +08:00
}
2024-03-06 21:35:34 +08:00
public static string GetMetaName < T > ( )
2023-10-31 03:03:13 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta < T > ( ) . Name ;
2023-10-31 03:03:13 +08:00
}
2024-03-06 21:35:34 +08:00
public static string GetMetaName ( Type type )
2023-06-16 11:53:59 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( type ) . Name ;
}
public static bool TryGetMetaName ( object obj , out string name )
{
TypeMeta meta = GetTypeMeta ( obj ) ;
name = meta . Name ;
return meta . IsCustomName ;
}
public static bool TryGetMetaName < T > ( out string name )
{
TypeMeta meta = GetTypeMeta < T > ( ) ;
name = meta . Name ;
return meta . IsCustomName ;
}
public static bool TryGetMetaName ( Type type , out string name )
{
TypeMeta meta = GetTypeMeta ( type ) ;
name = meta . Name ;
return meta . IsCustomName ;
2023-06-16 11:53:59 +08:00
}
2023-06-16 12:34:54 +08:00
#endregion
2023-05-23 01:47:28 +08:00
2024-03-06 21:35:34 +08:00
#region GetColor
public static MetaColor GetColor ( object obj )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( obj ) . Color ;
2023-11-01 02:30:19 +08:00
}
2024-03-06 21:35:34 +08:00
public static MetaColor GetColor < T > ( )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta < T > ( ) . Color ;
2023-11-01 02:30:19 +08:00
}
2024-03-06 21:35:34 +08:00
public static MetaColor GetColor ( Type type )
2023-06-29 17:57:28 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( type ) . Color ;
}
public static bool TryGetColor ( object obj , out MetaColor color )
{
TypeMeta meta = GetTypeMeta ( obj ) ;
color = meta . Color ;
return meta . IsCustomColor ;
}
public static bool TryGetColor < T > ( out MetaColor color )
{
TypeMeta meta = GetTypeMeta < T > ( ) ;
color = meta . Color ;
return meta . IsCustomColor ;
}
public static bool TryGetColor ( Type type , out MetaColor color )
{
TypeMeta meta = GetTypeMeta ( type ) ;
color = meta . Color ;
return meta . IsCustomColor ;
2023-06-29 17:57:28 +08:00
}
#endregion
2023-06-16 12:34:54 +08:00
#region GetDescription
2024-05-01 13:38:08 +08:00
public static MetaDescription GetDescription ( object obj )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( obj ) . Description ;
2023-11-01 02:30:19 +08:00
}
2024-05-01 13:38:08 +08:00
public static MetaDescription GetDescription < T > ( )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta < T > ( ) . Description ;
2023-11-01 02:30:19 +08:00
}
2024-05-01 13:38:08 +08:00
public static MetaDescription GetDescription ( Type type )
2023-06-16 11:53:59 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( type ) . Description ;
2023-06-16 11:53:59 +08:00
}
2024-05-01 13:38:08 +08:00
public static bool TryGetDescription ( object obj , out MetaDescription description )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta ( obj ) ;
description = meta . Description ;
2024-05-01 13:38:08 +08:00
return description ! = MetaDescription . Empty ;
2023-11-01 02:30:19 +08:00
}
2024-05-01 13:38:08 +08:00
public static bool TryGetDescription < T > ( out MetaDescription description )
2024-02-26 11:54:18 +08:00
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta < T > ( ) ;
description = meta . Description ;
2024-05-01 13:38:08 +08:00
return description ! = MetaDescription . Empty ;
2024-02-26 11:54:18 +08:00
}
2024-05-01 13:38:08 +08:00
public static bool TryGetDescription ( Type type , out MetaDescription description )
2023-05-23 01:47:28 +08:00
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta ( type ) ;
description = meta . Description ;
2024-05-01 13:38:08 +08:00
return description ! = MetaDescription . Empty ;
2024-03-03 00:28:16 +08:00
}
2024-03-06 21:35:34 +08:00
#endregion
#region GetGroup
2024-03-06 23:29:37 +08:00
public static MetaGroup GetGroup ( object obj )
2024-03-03 00:28:16 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( obj ) . Group ;
2023-06-16 11:53:59 +08:00
}
2024-03-06 23:29:37 +08:00
public static MetaGroup GetGroup < T > ( )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta < T > ( ) . Group ;
2023-11-01 02:30:19 +08:00
}
2024-03-06 23:29:37 +08:00
public static MetaGroup GetGroup ( Type type )
2024-02-26 11:54:18 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( type ) . Group ;
2024-02-26 11:54:18 +08:00
}
2024-03-06 21:35:34 +08:00
2024-03-06 23:29:37 +08:00
public static bool TryGetGroup ( object obj , out MetaGroup group )
2023-06-16 11:53:59 +08:00
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta ( obj ) ;
group = meta . Group ;
2024-05-01 13:38:08 +08:00
return group ! = MetaGroup . Empty ;
2024-03-06 21:35:34 +08:00
}
2024-03-06 23:29:37 +08:00
public static bool TryGetGroup < T > ( out MetaGroup group )
2024-03-06 21:35:34 +08:00
{
TypeMeta meta = GetTypeMeta < T > ( ) ;
group = meta . Group ;
2024-05-01 13:38:08 +08:00
return group ! = MetaGroup . Empty ;
2024-03-06 21:35:34 +08:00
}
2024-03-06 23:29:37 +08:00
public static bool TryGetGroup ( Type type , out MetaGroup group )
2024-03-06 21:35:34 +08:00
{
TypeMeta meta = GetTypeMeta ( type ) ;
group = meta . Group ;
2024-05-01 13:38:08 +08:00
return group ! = MetaGroup . Empty ;
2023-12-20 23:16:57 +08:00
}
#endregion
#region GetTags
public static IReadOnlyCollection < string > GetTags ( object obj )
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( obj ) . Tags ;
2023-12-20 23:16:57 +08:00
}
2024-02-26 11:54:18 +08:00
public static IReadOnlyCollection < string > GetTags < T > ( )
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta < T > ( ) . Tags ;
2024-02-26 11:54:18 +08:00
}
2023-12-20 23:16:57 +08:00
public static IReadOnlyCollection < string > GetTags ( Type type )
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( type ) . Tags ;
2023-12-20 23:16:57 +08:00
}
public static bool TryGetTags ( object obj , out IReadOnlyCollection < string > tags )
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta ( obj ) ;
tags = meta . Tags ;
return tags . Count < = 0 ;
2023-12-20 23:16:57 +08:00
}
2024-02-26 11:54:18 +08:00
public static bool TryGetTags < T > ( out IReadOnlyCollection < string > tags )
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta < T > ( ) ;
tags = meta . Tags ;
return tags . Count < = 0 ;
2024-02-26 11:54:18 +08:00
}
2023-12-20 23:16:57 +08:00
public static bool TryGetTags ( Type type , out IReadOnlyCollection < string > tags )
{
2024-03-06 21:35:34 +08:00
TypeMeta meta = GetTypeMeta ( type ) ;
tags = meta . Tags ;
return tags . Count < = 0 ;
2023-05-23 01:47:28 +08:00
}
2023-06-16 11:53:59 +08:00
#endregion
2023-05-31 04:09:55 +08:00
2023-06-16 12:34:54 +08:00
#region IsHidden
2023-11-01 02:30:19 +08:00
public static bool IsHidden ( object obj )
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( obj ) . IsHidden ;
2023-11-01 02:30:19 +08:00
}
2024-02-26 11:54:18 +08:00
public static bool IsHidden < T > ( )
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta < T > ( ) . IsHidden ;
2024-02-26 11:54:18 +08:00
}
public static bool IsHidden ( Type type )
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( type ) . IsHidden ;
2024-02-26 11:54:18 +08:00
}
2023-06-16 12:34:54 +08:00
#endregion
2024-03-06 21:35:34 +08:00
#region GetTypeMeta
public static TypeMeta GetTypeMeta ( object obj )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( GetTypeMetaSource ( obj ) . GetType ( ) ) ;
2023-11-01 02:30:19 +08:00
}
2024-03-06 21:35:34 +08:00
public static TypeMeta GetTypeMeta < T > ( )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
return GetTypeMeta ( typeof ( T ) ) ;
2023-11-01 02:30:19 +08:00
}
2024-03-06 21:35:34 +08:00
public static TypeMeta GetTypeMeta ( Type type )
2023-11-01 02:30:19 +08:00
{
2024-03-06 21:35:34 +08:00
if ( _metaCache . TryGetValue ( type , out TypeMeta result ) = = false )
2024-02-26 11:54:18 +08:00
{
2024-03-06 21:35:34 +08:00
result = new TypeMeta ( type ) ;
2024-02-26 11:54:18 +08:00
_metaCache . Add ( type , result ) ;
}
return result ;
2023-06-29 17:57:28 +08:00
}
#endregion
2024-03-06 21:35:34 +08:00
#region TypeMetaSource
public static bool IsTypeMetaProvided ( object obj )
2023-05-31 04:09:55 +08:00
{
2024-03-06 21:35:34 +08:00
return obj is IEcsTypeMetaProvider ;
2023-05-31 04:09:55 +08:00
}
2024-03-06 21:35:34 +08:00
public static object GetTypeMetaSource ( object obj )
2023-06-16 12:34:54 +08:00
{
2024-03-06 21:35:34 +08:00
return obj is IEcsTypeMetaProvider intr ? intr . MetaSource : obj ;
2023-06-16 12:34:54 +08:00
}
#endregion
2024-02-26 11:54:18 +08:00
}
2024-03-06 23:28:27 +08:00
public interface ITypeMeta
{
2024-05-16 20:30:13 +08:00
Type Type { get ; }
2024-03-07 03:34:24 +08:00
string Name { get ; }
MetaColor Color { get ; }
2024-05-01 13:38:08 +08:00
MetaDescription Description { get ; }
2024-03-07 03:34:24 +08:00
MetaGroup Group { get ; }
IReadOnlyCollection < string > Tags { get ; }
2024-03-06 23:28:27 +08:00
}
2024-03-09 23:20:29 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2024-03-06 23:28:27 +08:00
public sealed class TypeMeta : ITypeMeta
2024-02-26 11:54:18 +08:00
{
internal readonly Type _type ;
2024-03-06 21:35:34 +08:00
private bool _isCustomName ;
private bool _isCustomColor ;
private bool _isHidden ;
private string _name ;
private MetaColor _color ;
2024-05-01 13:38:08 +08:00
private MetaDescription _description ;
2024-03-06 23:29:37 +08:00
private MetaGroup _group ;
2024-03-06 21:35:34 +08:00
private IReadOnlyCollection < string > _tags ;
private int _typeCode ;
2024-02-26 11:54:18 +08:00
private InitFlag _initFlags = InitFlag . None ;
2024-03-06 21:35:34 +08:00
#region Constructors
public TypeMeta ( Type type )
2024-02-26 11:54:18 +08:00
{
_type = type ;
}
2024-03-06 21:35:34 +08:00
#endregion
2024-02-26 11:54:18 +08:00
2024-03-06 21:35:34 +08:00
#region Type
2024-02-26 11:54:18 +08:00
public Type Type
{
get { return _type ; }
}
2024-03-06 21:35:34 +08:00
#endregion
#region Name
private void InitName ( )
{
if ( _initFlags . HasFlag ( InitFlag . Name ) = = false )
{
2024-03-10 01:25:08 +08:00
( _name , _isCustomName ) = MetaGenerator . GetMetaName ( _type ) ;
2024-03-06 21:35:34 +08:00
_initFlags | = InitFlag . Name ;
}
}
public bool IsCustomName
{
get
{
InitName ( ) ;
return _isCustomName ;
}
}
2024-02-26 11:54:18 +08:00
public string Name
{
get
{
2024-03-06 21:35:34 +08:00
InitName ( ) ;
2024-02-26 11:54:18 +08:00
return _name ;
}
}
2024-03-06 21:35:34 +08:00
#endregion
#region Color
private void InitColor ( )
{
if ( _initFlags . HasFlag ( InitFlag . Color ) = = false )
{
2024-03-10 01:25:08 +08:00
( _color , _isCustomColor ) = MetaGenerator . GetColor ( _type ) ;
2024-03-06 21:35:34 +08:00
_initFlags | = InitFlag . Color ;
}
}
public bool IsCustomColor
2024-02-26 11:54:18 +08:00
{
get
{
2024-03-06 21:35:34 +08:00
InitColor ( ) ;
return _isCustomColor ;
2024-02-26 11:54:18 +08:00
}
}
public MetaColor Color
{
get
{
2024-03-06 21:35:34 +08:00
InitColor ( ) ;
2024-02-29 02:37:04 +08:00
return _color ;
2024-02-26 11:54:18 +08:00
}
}
2024-03-06 21:35:34 +08:00
#endregion
#region Description
2024-05-01 13:38:08 +08:00
public MetaDescription Description
2024-02-26 11:54:18 +08:00
{
get
{
2024-02-29 02:37:04 +08:00
if ( _initFlags . HasFlag ( InitFlag . Description ) = = false )
2024-02-26 11:54:18 +08:00
{
2024-03-10 01:25:08 +08:00
_description = MetaGenerator . GetDescription ( _type ) ;
2024-02-26 11:54:18 +08:00
_initFlags | = InitFlag . Description ;
}
return _description ;
}
}
2024-03-06 21:35:34 +08:00
#endregion
#region Group
2024-03-06 23:29:37 +08:00
public MetaGroup Group
2024-02-26 11:54:18 +08:00
{
get
{
2024-03-06 21:35:34 +08:00
if ( _initFlags . HasFlag ( InitFlag . Group ) = = false )
2024-02-26 11:54:18 +08:00
{
2024-03-10 01:25:08 +08:00
_group = MetaGenerator . GetGroup ( _type ) ;
2024-03-06 21:35:34 +08:00
_initFlags | = InitFlag . Group ;
2024-02-26 11:54:18 +08:00
}
2024-03-06 21:35:34 +08:00
return _group ;
}
}
#endregion
#region Tags
private void InitTags ( )
{
if ( _initFlags . HasFlag ( InitFlag . Tags ) = = false )
{
2024-03-10 01:25:08 +08:00
_tags = MetaGenerator . GetTags ( _type ) ;
2024-03-06 21:35:34 +08:00
_initFlags | = InitFlag . Tags ;
_isHidden = _tags . Contains ( MetaTags . HIDDEN ) ;
}
}
public IReadOnlyCollection < string > Tags
{
get
{
InitTags ( ) ;
2024-02-26 11:54:18 +08:00
return _tags ;
}
}
2024-03-06 21:35:34 +08:00
public bool IsHidden
2024-02-26 11:54:18 +08:00
{
get
{
2024-03-06 21:35:34 +08:00
InitTags ( ) ;
return _isHidden ;
}
}
#endregion
#region TypeCode
public int TypeCode
{
get
{
if ( _initFlags . HasFlag ( InitFlag . TypeCode ) = = false )
2024-02-26 11:54:18 +08:00
{
2024-03-06 21:35:34 +08:00
_typeCode = EcsTypeCode . Get ( _type ) ;
_initFlags | = InitFlag . TypeCode ;
2024-02-26 11:54:18 +08:00
}
2024-03-06 21:35:34 +08:00
return _typeCode ;
2024-02-26 11:54:18 +08:00
}
}
2024-03-06 21:35:34 +08:00
#endregion
2024-02-26 11:54:18 +08:00
2024-03-06 21:35:34 +08:00
#region InitializeAll
2024-02-26 11:54:18 +08:00
public void InitializeAll ( )
{
if ( _initFlags = = InitFlag . All )
{
return ;
}
_ = Name ;
_ = Group ;
_ = Color ;
_ = Description ;
_ = Tags ;
2024-03-06 21:35:34 +08:00
_ = TypeCode ;
2024-02-26 11:54:18 +08:00
}
2024-03-06 21:35:34 +08:00
#endregion
2024-02-26 11:54:18 +08:00
2024-03-06 21:35:34 +08:00
#region InitFlag
[Flags]
2024-02-26 11:54:18 +08:00
private enum InitFlag : byte
{
None = 0 ,
Name = 1 < < 0 ,
Group = 1 < < 1 ,
Color = 1 < < 2 ,
Description = 1 < < 3 ,
Tags = 1 < < 4 ,
2024-03-06 21:35:34 +08:00
TypeCode = 1 < < 5 ,
All = Name | Group | Color | Description | Tags | TypeCode
2023-06-29 17:57:28 +08:00
}
2024-03-06 21:35:34 +08:00
#endregion
2024-03-09 23:09:01 +08:00
#region Other
public override string ToString ( )
{
return Name ;
}
2024-03-09 23:20:29 +08:00
private class DebuggerProxy : ITypeMeta
{
private readonly TypeMeta _meta ;
2024-05-16 20:30:13 +08:00
public Type Type
{
get { return _meta . Type ; }
}
2024-03-09 23:20:29 +08:00
public string Name
{
get { return _meta . Name ; }
}
public MetaColor Color
{
get { return _meta . Color ; }
}
2024-05-01 13:38:08 +08:00
public MetaDescription Description
2024-03-09 23:20:29 +08:00
{
get { return _meta . Description ; }
}
public MetaGroup Group
{
get { return _meta . Group ; }
}
public IReadOnlyCollection < string > Tags
{
get { return _meta . Tags ; }
}
public DebuggerProxy ( TypeMeta meta )
{
_meta = meta ;
}
}
2024-03-09 23:09:01 +08:00
#endregion
2024-03-06 21:35:34 +08:00
2024-03-10 01:25:08 +08:00
#region MetaGenerator
private static class MetaGenerator
2024-03-06 21:35:34 +08:00
{
2024-03-10 01:25:08 +08:00
private const int GENERIC_NAME_DEPTH = 3 ;
2024-03-06 21:35:34 +08:00
2024-03-10 01:25:08 +08:00
#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
2024-03-06 21:35:34 +08:00
2024-03-10 01:25:08 +08:00
#region GetColor
private static MetaColor AutoColor ( Type type )
{
2024-05-16 20:30:13 +08:00
return new MetaColor ( type . Name ) . UpContrast ( ) ; //.Desaturate(0.48f) / 1.18f;
2024-03-10 01:25:08 +08:00
}
public static ( MetaColor , bool ) GetColor ( Type type )
{
bool isCustom = type . TryGetCustomAttribute ( out MetaColorAttribute atr ) ;
return ( isCustom ? atr . color : AutoColor ( type ) , isCustom ) ;
}
#endregion
2024-03-06 21:35:34 +08:00
2024-03-10 01:25:08 +08:00
#region GetGroup
public static MetaGroup GetGroup ( Type type )
{
return type . TryGetCustomAttribute ( out MetaGroupAttribute atr ) ? atr . Data : MetaGroup . Empty ;
}
#endregion
2024-03-06 21:35:34 +08:00
2024-03-10 01:25:08 +08:00
#region GetDescription
2024-05-01 13:38:08 +08:00
public static MetaDescription GetDescription ( Type type )
2024-03-10 01:25:08 +08:00
{
bool isCustom = type . TryGetCustomAttribute ( out MetaDescriptionAttribute atr ) ;
2024-05-01 13:38:08 +08:00
return isCustom ? atr . Data : MetaDescription . Empty ;
2024-03-10 01:25:08 +08:00
}
#endregion
#region GetTags
public static IReadOnlyCollection < string > GetTags ( Type type )
{
var atr = type . GetCustomAttribute < MetaTagsAttribute > ( ) ;
return atr ! = null ? atr . Tags : Array . Empty < string > ( ) ;
}
#endregion
2024-03-06 21:35:34 +08:00
}
#endregion
2024-03-10 01:25:08 +08:00
}
2024-03-06 21:35:34 +08:00
2024-03-10 01:25:08 +08:00
public static class TypeMetaDataCachedExtensions
{
public static TypeMeta GetMeta ( this object self )
2024-03-06 21:35:34 +08:00
{
2024-03-10 01:25:08 +08:00
return EcsDebugUtility . GetTypeMeta ( self ) ;
2024-03-06 21:35:34 +08:00
}
2024-03-10 01:25:08 +08:00
public static TypeMeta ToMeta ( this Type self )
2024-03-06 21:35:34 +08:00
{
2024-03-10 01:25:08 +08:00
return EcsDebugUtility . GetTypeMeta ( self ) ;
2024-03-06 21:35:34 +08:00
}
2023-06-29 17:57:28 +08:00
}
2024-02-26 11:54:18 +08:00
}