From d4415c312740b2733ab8a34e800a79e006fd5176 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Wed, 6 Mar 2024 21:35:34 +0800 Subject: [PATCH] Update TypeMeta & EcsDebugUtility --- src/Debug/EcsDebugUtility.cs | 591 ++++++++++-------- ...etaProvider.cs => IEcsTypeMetaProvider.cs} | 2 +- ...r.cs.meta => IEcsTypeMetaProvider.cs.meta} | 2 +- .../MetaAttributes/MetaColorAttribute.cs | 3 +- .../MetaAttributes/MetaGroupAttribute.cs | 28 +- src/Debug/MetaAttributes/MetaTagsAttribute.cs | 7 +- src/Internal/ReflectionUtility.cs | 22 + src/Internal/ReflectionUtility.cs.meta | 11 + 8 files changed, 394 insertions(+), 272 deletions(-) rename src/Debug/Interfaces/{IEcsMetaProvider.cs => IEcsTypeMetaProvider.cs} (66%) rename src/Debug/Interfaces/{IEcsMetaProvider.cs.meta => IEcsTypeMetaProvider.cs.meta} (83%) create mode 100644 src/Internal/ReflectionUtility.cs create mode 100644 src/Internal/ReflectionUtility.cs.meta diff --git a/src/Debug/EcsDebugUtility.cs b/src/Debug/EcsDebugUtility.cs index 9da8d0b..e16726c 100644 --- a/src/Debug/EcsDebugUtility.cs +++ b/src/Debug/EcsDebugUtility.cs @@ -1,42 +1,58 @@ -using System; +using DCFApixels.DragonECS.Internal; +using System; using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { public static class EcsDebugUtility { private const BindingFlags RFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; + private static readonly Dictionary _metaCache = new Dictionary(); #region GetGenericTypeName - public static string GetGenericTypeFullName(int maxDepth = 2) => GetGenericTypeFullName(typeof(T), maxDepth); - public static string GetGenericTypeFullName(Type type, int maxDepth = 2) => GetGenericTypeNameInternal(type, maxDepth, true); - public static string GetGenericTypeName(int maxDepth = 2) => GetGenericTypeName(typeof(T), maxDepth); - public static string GetGenericTypeName(Type type, int maxDepth = 2) => GetGenericTypeNameInternal(type, maxDepth, false); + public static string GetGenericTypeFullName(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(int maxDepth = 2) + { + return GetGenericTypeName(typeof(T), maxDepth); + } + public static string GetGenericTypeName(Type type, int maxDepth = 2) + { + return GetGenericTypeNameInternal(type, maxDepth, false); + } private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull) { #if (DEBUG && !DISABLE_DEBUG) - string result = isFull ? type.FullName : type.Name; + string typeName = isFull ? type.FullName : type.Name; if (!type.IsGenericType || maxDepth == 0) - return result; + { + return typeName; + } + int genericInfoIndex = typeName.LastIndexOf('`'); + if (genericInfoIndex > 0) + { + typeName = typeName.Remove(genericInfoIndex); + } - int iBacktick = result.IndexOf('`'); - if (iBacktick > 0) - result = result.Remove(iBacktick); - - result += "<"; + string genericParams = ""; Type[] typeParameters = type.GetGenericArguments(); for (int i = 0; i < typeParameters.Length; ++i) { - string typeParamName = GetGenericTypeNameInternal(typeParameters[i], maxDepth - 1, false);//чтобы строка не была слишком длинной, используются сокращенные имена для типов аргументов - result += (i == 0 ? typeParamName : "," + typeParamName); + //чтобы строка не была слишком длинной, используются сокращенные имена для типов аргументов + string paramTypeName = GetGenericTypeNameInternal(typeParameters[i], maxDepth - 1, false); + genericParams += (i == 0 ? paramTypeName : $", {paramTypeName}"); } - result += ">"; - return result; + return $"{typeName}<{genericParams}>"; #else //optimization for release build - return type.Name; + return isFull ? type.FullName : type.Name; #endif } #endregion @@ -75,389 +91,382 @@ namespace DCFApixels.DragonECS #endregion #region GetName - public static string GetName(object obj, int maxGenericDepth = 2) + public static string GetMetaName(object obj) { - return obj is IEcsMetaProvider intr ? - GetName(intr.MetaSource, maxGenericDepth) : - GetName(type: obj.GetType(), maxGenericDepth); + return GetTypeMeta(obj).Name; } - public static string GetName(int maxGenericDepth = 2) => GetName(typeof(T), maxGenericDepth); - public static string GetName(Type type, int maxGenericDepth = 2) => type.TryGetCustomAttribute(out MetaNameAttribute atr) ? atr.name : GetGenericTypeName(type, maxGenericDepth); - public static bool TryGetCustomName(object obj, out string name) + public static string GetMetaName() { - return obj is IEcsMetaProvider intr ? - TryGetCustomName(intr.MetaSource, out name) : - TryGetCustomName(type: obj.GetType(), out name); + return GetTypeMeta().Name; } - public static bool TryGetCustomName(out string name) => TryGetCustomName(type: typeof(T), out name); - public static bool TryGetCustomName(Type type, out string name) + public static string GetMetaName(Type type) { - if (type.TryGetCustomAttribute(out MetaNameAttribute atr)) - { - name = atr.name; - return true; - } - name = string.Empty; - return false; + return GetTypeMeta(type).Name; } - #endregion - #region GetGroup - public static MetaGroup GetGroup(object obj) + public static bool TryGetMetaName(object obj, out string name) { - return obj is IEcsMetaProvider intr ? - GetGroup(intr.MetaSource) : - GetGroup(type: obj.GetType()); + TypeMeta meta = GetTypeMeta(obj); + name = meta.Name; + return meta.IsCustomName; } - public static MetaGroup GetGroup() => GetGroup(typeof(T)); - public static MetaGroup GetGroup(Type type) => type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.GetData() : MetaGroup.Empty; - public static bool TryGetGroup(object obj, out MetaGroup group) + public static bool TryGetMetaName(out string name) { - return obj is IEcsMetaProvider intr ? - TryGetGroup(intr.MetaSource, out group) : - TryGetGroup(type: obj.GetType(), out group); + TypeMeta meta = GetTypeMeta(); + name = meta.Name; + return meta.IsCustomName; } - public static bool TryGetGroup(out MetaGroup text) => TryGetGroup(typeof(T), out text); - public static bool TryGetGroup(Type type, out MetaGroup group) + public static bool TryGetMetaName(Type type, out string name) { - if (type.TryGetCustomAttribute(out MetaGroupAttribute atr)) - { - group = atr.GetData(); - return true; - } - group = MetaGroup.Empty; - return false; - } - #endregion - - #region GetDescription - public static string GetDescription(object obj) - { - return obj is IEcsMetaProvider intr ? - GetDescription(intr.MetaSource) : - GetDescription(type: obj.GetType()); - } - public static string GetDescription() => GetDescription(typeof(T)); - public static string GetDescription(Type type) => type.TryGetCustomAttribute(out MetaDescriptionAttribute atr) ? atr.description : string.Empty; - public static bool TryGetDescription(object obj, out string text) - { - return obj is IEcsMetaProvider intr ? - TryGetDescription(intr.MetaSource, out text) : - TryGetDescription(type: obj.GetType(), out text); - } - public static bool TryGetDescription(out string text) => TryGetDescription(typeof(T), out text); - public static bool TryGetDescription(Type type, out string text) - { - if (type.TryGetCustomAttribute(out MetaDescriptionAttribute atr)) - { - text = atr.description; - return true; - } - text = string.Empty; - return false; + TypeMeta meta = GetTypeMeta(type); + name = meta.Name; + return meta.IsCustomName; } #endregion #region GetColor public static MetaColor GetColor(object obj) { - return obj is IEcsMetaProvider intr ? - GetColor(intr.MetaSource) : - GetColor(type: obj.GetType()); + return GetTypeMeta(obj).Color; } public static MetaColor GetColor() { - return GetColor(typeof(T)); + return GetTypeMeta().Color; } public static MetaColor GetColor(Type type) { - var atr = type.GetCustomAttribute(); - return atr != null ? atr.color : AutoColor(type); - } - private static MetaColor AutoColor(Type type) - { - return new MetaColor(type.Name).Desaturate(0.75f) / 1.15f; + return GetTypeMeta(type).Color; } + public static bool TryGetColor(object obj, out MetaColor color) { - return obj is IEcsMetaProvider intr ? - TryGetColor(intr.MetaSource, out color) : - TryGetColor(type: obj.GetType(), out color); + TypeMeta meta = GetTypeMeta(obj); + color = meta.Color; + return meta.IsCustomColor; } public static bool TryGetColor(out MetaColor color) { - return TryGetColor(typeof(T), out color); + TypeMeta meta = GetTypeMeta(); + color = meta.Color; + return meta.IsCustomColor; } public static bool TryGetColor(Type type, out MetaColor color) { - var atr = type.GetCustomAttribute(); - if (atr != null) - { - color = atr.color; - return true; - } - color = MetaColor.BlackColor; - return false; + TypeMeta meta = GetTypeMeta(type); + color = meta.Color; + return meta.IsCustomColor; + } + #endregion + + #region GetDescription + public static string GetDescription(object obj) + { + return GetTypeMeta(obj).Description; + } + public static string GetDescription() + { + return GetTypeMeta().Description; + } + public static string GetDescription(Type type) + { + return GetTypeMeta(type).Description; + } + + public static bool TryGetDescription(object obj, out string description) + { + TypeMeta meta = GetTypeMeta(obj); + description = meta.Description; + return string.IsNullOrEmpty(description); + } + public static bool TryGetDescription(out string description) + { + TypeMeta meta = GetTypeMeta(); + description = meta.Description; + return string.IsNullOrEmpty(description); + } + public static bool TryGetDescription(Type type, out string description) + { + TypeMeta meta = GetTypeMeta(type); + description = meta.Description; + return string.IsNullOrEmpty(description); + } + #endregion + + #region GetGroup + public static MetaGroup GetGroup(object obj) + { + return GetTypeMeta(obj).Group; + } + public static MetaGroup GetGroup() + { + return GetTypeMeta().Group; + } + public static MetaGroup GetGroup(Type type) + { + return GetTypeMeta(type).Group; + } + + public static bool TryGetGroup(object obj, out MetaGroup group) + { + TypeMeta meta = GetTypeMeta(obj); + group = meta.Group; + return group.IsNull; + } + public static bool TryGetGroup(out MetaGroup group) + { + TypeMeta meta = GetTypeMeta(); + group = meta.Group; + return group.IsNull; + } + public static bool TryGetGroup(Type type, out MetaGroup group) + { + TypeMeta meta = GetTypeMeta(type); + group = meta.Group; + return group.IsNull; } #endregion #region GetTags public static IReadOnlyCollection GetTags(object obj) { - return obj is IEcsMetaProvider intr ? - GetTags(intr.MetaSource) : - GetTags(type: obj.GetType()); + return GetTypeMeta(obj).Tags; } public static IReadOnlyCollection GetTags() { - return GetTags(typeof(T)); + return GetTypeMeta().Tags; } public static IReadOnlyCollection GetTags(Type type) { - var atr = type.GetCustomAttribute(); - return atr != null ? atr.Tags : Array.Empty(); + return GetTypeMeta(type).Tags; } public static bool TryGetTags(object obj, out IReadOnlyCollection tags) { - return obj is IEcsMetaProvider intr ? - TryGetTags(intr.MetaSource, out tags) : - TryGetTags(type: obj.GetType(), out tags); + TypeMeta meta = GetTypeMeta(obj); + tags = meta.Tags; + return tags.Count <= 0; } public static bool TryGetTags(out IReadOnlyCollection tags) { - return TryGetTags(typeof(T), out tags); + TypeMeta meta = GetTypeMeta(); + tags = meta.Tags; + return tags.Count <= 0; } public static bool TryGetTags(Type type, out IReadOnlyCollection tags) { - var atr = type.GetCustomAttribute(); - if (atr != null) - { - tags = atr.Tags; - return true; - } - tags = Array.Empty(); - return false; + TypeMeta meta = GetTypeMeta(type); + tags = meta.Tags; + return tags.Count <= 0; } #endregion #region IsHidden public static bool IsHidden(object obj) { - return obj is IEcsMetaProvider intr ? - IsHidden(intr.MetaSource) : - IsHidden(type: obj.GetType()); + return GetTypeMeta(obj).IsHidden; } public static bool IsHidden() { - return IsHidden(typeof(T)); + return GetTypeMeta().IsHidden; } public static bool IsHidden(Type type) { - return type.TryGetCustomAttribute(out MetaTagsAttribute atr) && atr.Tags.Contains(MetaTags.HIDDEN); + return GetTypeMeta(type).IsHidden; } #endregion - #region MetaSource - public static bool IsMetaSourceProvided(object obj) + #region GetTypeMeta + public static TypeMeta GetTypeMeta(object obj) { - return obj is IEcsMetaProvider; + return GetTypeMeta(GetTypeMetaSource(obj).GetType()); } - public static object GetMetaSource(object obj) + public static TypeMeta GetTypeMeta() { - return obj is IEcsMetaProvider intr ? intr.MetaSource : obj; + return GetTypeMeta(typeof(T)); } - #endregion - - #region GenerateTypeMeta - public static TypeMetaData GenerateTypeMeta(object obj) + public static TypeMeta GetTypeMeta(Type type) { - return obj is IEcsMetaProvider intr ? - GenerateTypeMeta(intr.MetaSource) : - GenerateTypeMeta(type: obj.GetType()); - } - public static TypeMetaData GenerateTypeMeta() - { - return GenerateTypeMeta(typeof(T)); - } - public static TypeMetaData GenerateTypeMeta(Type type) - { - return new TypeMetaData( - type, - GetName(type), - GetGroup(type), - GetColor(type), - GetDescription(type), - GetTags(type)); - } - #endregion - - #region GetCachedTypeMeta - private static readonly Dictionary _metaCache = new Dictionary(); - public static TypeMetaDataCached GetCachedTypeMeta(object obj) - { - return obj is IEcsMetaProvider intr ? - GetCachedTypeMeta(intr.MetaSource) : - GetCachedTypeMeta(type: obj.GetType()); - } - public static TypeMetaDataCached GetCachedTypeMeta() - { - return GetCachedTypeMeta(typeof(T)); - } - public static TypeMetaDataCached GetCachedTypeMeta(Type type) - { - if (_metaCache.TryGetValue(type, out TypeMetaDataCached result) == false) + if (_metaCache.TryGetValue(type, out TypeMeta result) == false) { - result = new TypeMetaDataCached(type); + result = new TypeMeta(type); _metaCache.Add(type, result); } return result; } #endregion - #region ReflectionExtensions - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool TryGetCustomAttribute(this Type self, out T attribute) where T : Attribute + #region TypeMetaSource + public static bool IsTypeMetaProvided(object obj) { - attribute = self.GetCustomAttribute(); - return attribute != null; + return obj is IEcsTypeMetaProvider; } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool TryGetCustomAttribute(this MemberInfo self, out T attribute) where T : Attribute + public static object GetTypeMetaSource(object obj) { - attribute = self.GetCustomAttribute(); - return attribute != null; + return obj is IEcsTypeMetaProvider intr ? intr.MetaSource : obj; } #endregion + } - [Serializable] - public sealed class TypeMetaData - { - public readonly Type type; - public readonly string name; - public readonly MetaGroup group; - public readonly MetaColor color; - public readonly string description; - public readonly IReadOnlyCollection tags; - public TypeMetaData(Type type, string name, MetaGroup group, MetaColor color, string description, IReadOnlyCollection tags) - { - this.type = type; - this.name = name; - this.group = group; - this.color = color; - this.description = description; - this.tags = tags; - } - } - - public static class TypeMetaDataCachedExtensions - { - public static TypeMetaDataCached GetMeta(this object self) - { - return EcsDebugUtility.GetCachedTypeMeta(self); - } - } - public class TypeMetaDataCached + public sealed class TypeMeta { internal readonly Type _type; - internal string _name; - internal MetaGroup _group; - internal MetaColor _color; - internal string _description; - internal IReadOnlyCollection _tags; + private bool _isCustomName; + private bool _isCustomColor; + private bool _isHidden; + + private string _name; + private MetaColor _color; + private string _description; + private MetaGroup _group; + private IReadOnlyCollection _tags; + private int _typeCode; - private TypeMetaData _typeMetaData = null; private InitFlag _initFlags = InitFlag.None; - public TypeMetaDataCached(Type type) + #region Constructors + public TypeMeta(Type type) { _type = type; } + #endregion + #region Type public Type Type { get { return _type; } } + #endregion + + #region Name + private void InitName() + { + if (_initFlags.HasFlag(InitFlag.Name) == false) + { + (_name, _isCustomName) = MetaCacheGenerator.GetName(_type); + _initFlags |= InitFlag.Name; + } + } + public bool IsCustomName + { + get + { + InitName(); + return _isCustomName; + } + } public string Name { get { - if (_initFlags.HasFlag(InitFlag.Name) == false) - { - _name = EcsDebugUtility.GetName(_type); - _initFlags |= InitFlag.Name; - } + InitName(); return _name; } } - public MetaGroup Group + #endregion + + #region Color + private void InitColor() + { + if (_initFlags.HasFlag(InitFlag.Color) == false) + { + (_color, _isCustomColor) = MetaCacheGenerator.GetColor(_type); + _initFlags |= InitFlag.Color; + } + } + public bool IsCustomColor { get { - if (_initFlags.HasFlag(InitFlag.Group) == false) - { - _group = EcsDebugUtility.GetGroup(_type); - _initFlags |= InitFlag.Group; - } - return _group; + InitColor(); + return _isCustomColor; } } public MetaColor Color { get { - if (_initFlags.HasFlag(InitFlag.Color) == false) - { - _color = EcsDebugUtility.GetColor(_type); - _initFlags |= InitFlag.Color; - } + InitColor(); return _color; } } + #endregion + + #region Description public string Description { get { if (_initFlags.HasFlag(InitFlag.Description) == false) { - _description = EcsDebugUtility.GetDescription(_type); + _description = MetaCacheGenerator.GetDescription(_type); _initFlags |= InitFlag.Description; } return _description; } } + #endregion + + #region Group + public MetaGroup Group + { + get + { + if (_initFlags.HasFlag(InitFlag.Group) == false) + { + _group = MetaCacheGenerator.GetGroup(_type); + _initFlags |= InitFlag.Group; + } + return _group; + } + } + #endregion + + #region Tags + private void InitTags() + { + if (_initFlags.HasFlag(InitFlag.Tags) == false) + { + _tags = MetaCacheGenerator.GetTags(_type); + _initFlags |= InitFlag.Tags; + _isHidden = _tags.Contains(MetaTags.HIDDEN); + } + } public IReadOnlyCollection Tags { get { - if (_initFlags.HasFlag(InitFlag.Tags) == false) - { - _tags = EcsDebugUtility.GetTags(_type); - _initFlags |= InitFlag.Tags; - } + InitTags(); return _tags; } } - public TypeMetaData AllData + public bool IsHidden { get { - if (_typeMetaData == null) - { - _typeMetaData = new TypeMetaData( - Type, - Name, - Group, - Color, - Description, - Tags); - } - return _typeMetaData; + InitTags(); + return _isHidden; } } + #endregion + #region TypeCode + public int TypeCode + { + get + { + if (_initFlags.HasFlag(InitFlag.TypeCode) == false) + { + _typeCode = EcsTypeCode.Get(_type); + _initFlags |= InitFlag.TypeCode; + } + return _typeCode; + } + } + #endregion + + #region InitializeAll public void InitializeAll() { if (_initFlags == InitFlag.All) @@ -469,8 +478,12 @@ namespace DCFApixels.DragonECS _ = Color; _ = Description; _ = Tags; + _ = TypeCode; } + #endregion + #region InitFlag + [Flags] private enum InitFlag : byte { None = 0, @@ -479,7 +492,73 @@ namespace DCFApixels.DragonECS Color = 1 << 2, Description = 1 << 3, Tags = 1 << 4, - All = Name | Group | Color | Description | Tags + TypeCode = 1 << 5, + + All = Name | Group | Color | Description | Tags | TypeCode + } + #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); } } +} + +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.GetData() : 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 GetTags(Type type) + { + var atr = type.GetCustomAttribute(); + return atr != null ? atr.Tags : Array.Empty(); + } + #endregion + } } \ No newline at end of file diff --git a/src/Debug/Interfaces/IEcsMetaProvider.cs b/src/Debug/Interfaces/IEcsTypeMetaProvider.cs similarity index 66% rename from src/Debug/Interfaces/IEcsMetaProvider.cs rename to src/Debug/Interfaces/IEcsTypeMetaProvider.cs index a7bc11a..c3315d1 100644 --- a/src/Debug/Interfaces/IEcsMetaProvider.cs +++ b/src/Debug/Interfaces/IEcsTypeMetaProvider.cs @@ -1,6 +1,6 @@ namespace DCFApixels.DragonECS { - public interface IEcsMetaProvider + public interface IEcsTypeMetaProvider { object MetaSource { get; } } diff --git a/src/Debug/Interfaces/IEcsMetaProvider.cs.meta b/src/Debug/Interfaces/IEcsTypeMetaProvider.cs.meta similarity index 83% rename from src/Debug/Interfaces/IEcsMetaProvider.cs.meta rename to src/Debug/Interfaces/IEcsTypeMetaProvider.cs.meta index b99941e..5ae3193 100644 --- a/src/Debug/Interfaces/IEcsMetaProvider.cs.meta +++ b/src/Debug/Interfaces/IEcsTypeMetaProvider.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 5379b4d037441ed4cb4171648c1453d4 +guid: 4a642dc8905124247bf83c9d13d8fb13 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/src/Debug/MetaAttributes/MetaColorAttribute.cs b/src/Debug/MetaAttributes/MetaColorAttribute.cs index fc97416..c570468 100644 --- a/src/Debug/MetaAttributes/MetaColorAttribute.cs +++ b/src/Debug/MetaAttributes/MetaColorAttribute.cs @@ -219,7 +219,8 @@ namespace DCFApixels.DragonECS byte r = this.r; byte g = this.g; byte b = this.b; - byte gray = (byte)(r * 0.299 + g * 0.587 + b * 0.114); + //byte gray = (byte)(r * 0.299 + g * 0.587 + b * 0.114); + byte gray = (byte)(r * 0.333333 + g * 0.333333 + b * 0.333333); r = (byte)(r + (gray - r) * (1 - t)); g = (byte)(g + (gray - g) * (1 - t)); b = (byte)(b + (gray - b) * (1 - t)); diff --git a/src/Debug/MetaAttributes/MetaGroupAttribute.cs b/src/Debug/MetaAttributes/MetaGroupAttribute.cs index c143cd8..8b39ee9 100644 --- a/src/Debug/MetaAttributes/MetaGroupAttribute.cs +++ b/src/Debug/MetaAttributes/MetaGroupAttribute.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text.RegularExpressions; namespace DCFApixels.DragonECS @@ -8,18 +9,27 @@ namespace DCFApixels.DragonECS { public static readonly MetaGroupAttribute Empty = new MetaGroupAttribute(""); public readonly string name; - public readonly string rootCategory; + private string[] path = null; + public IReadOnlyCollection Splited + { + get + { + if (path == null) + { + path = Regex.Split(name, @"[/|\\]"); + } + return path; + } + } public MetaGroupAttribute(string name) { name = Regex.Replace(name, @"^[/|\\]+|[/|\\]+$", ""); - rootCategory = Regex.Match(name, @"^(.*?)[/\\]").Groups[1].Value; this.name = name; } - public string[] SplitCategories() + public MetaGroup GetData() { - return Regex.Split(name, @"[/|\\]"); + return new MetaGroup(this); } - public MetaGroup GetData() => new MetaGroup(this); } public readonly struct MetaGroup { @@ -29,9 +39,9 @@ namespace DCFApixels.DragonECS { get { return _source.name; } } - public string RootCategory + public IReadOnlyCollection Splited { - get { return _source.rootCategory; } + get { return _source.Splited; } } public bool IsNull { @@ -41,9 +51,5 @@ namespace DCFApixels.DragonECS { _source = source; } - public string[] SplitCategories() - { - return _source.SplitCategories(); - } } } diff --git a/src/Debug/MetaAttributes/MetaTagsAttribute.cs b/src/Debug/MetaAttributes/MetaTagsAttribute.cs index 6d13570..3922c77 100644 --- a/src/Debug/MetaAttributes/MetaTagsAttribute.cs +++ b/src/Debug/MetaAttributes/MetaTagsAttribute.cs @@ -8,9 +8,12 @@ namespace DCFApixels.DragonECS public sealed class MetaTagsAttribute : EcsMetaAttribute { private readonly string[] _tags = Array.Empty(); - public IReadOnlyCollection Tags => _tags; + public IReadOnlyCollection Tags + { + get { return _tags; } + } - [Obsolete("With empty parameters, this attribute makes no sense.", true)] + [Obsolete("With empty parameters, this attribute makes no sense.")] public MetaTagsAttribute() { } public MetaTagsAttribute(params string[] tags) { diff --git a/src/Internal/ReflectionUtility.cs b/src/Internal/ReflectionUtility.cs new file mode 100644 index 0000000..d5f308c --- /dev/null +++ b/src/Internal/ReflectionUtility.cs @@ -0,0 +1,22 @@ +using System; +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace DCFApixels.DragonECS.Internal +{ + internal static class ReflectionUtility + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool TryGetCustomAttribute(this Type self, out T attribute) where T : Attribute + { + attribute = self.GetCustomAttribute(); + return attribute != null; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool TryGetCustomAttribute(this MemberInfo self, out T attribute) where T : Attribute + { + attribute = self.GetCustomAttribute(); + return attribute != null; + } + } +} diff --git a/src/Internal/ReflectionUtility.cs.meta b/src/Internal/ReflectionUtility.cs.meta new file mode 100644 index 0000000..e28abcd --- /dev/null +++ b/src/Internal/ReflectionUtility.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bfba14901791dae45bab76154086b299 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: