Update TypeMeta & EcsDebugUtility

This commit is contained in:
Mikhail 2024-03-06 21:35:34 +08:00
parent 6f30b5de97
commit d4415c3127
8 changed files with 394 additions and 272 deletions

View File

@ -1,42 +1,58 @@
using System; using DCFApixels.DragonECS.Internal;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public static class EcsDebugUtility public static class EcsDebugUtility
{ {
private const BindingFlags RFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic; private const BindingFlags RFL_FLAGS = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
private static readonly Dictionary<Type, TypeMeta> _metaCache = new Dictionary<Type, TypeMeta>();
#region GetGenericTypeName #region GetGenericTypeName
public static string GetGenericTypeFullName<T>(int maxDepth = 2) => GetGenericTypeFullName(typeof(T), maxDepth); public static string GetGenericTypeFullName<T>(int maxDepth = 2)
public static string GetGenericTypeFullName(Type type, int maxDepth = 2) => GetGenericTypeNameInternal(type, maxDepth, true); {
public static string GetGenericTypeName<T>(int maxDepth = 2) => GetGenericTypeName(typeof(T), maxDepth); return GetGenericTypeFullName(typeof(T), maxDepth);
public static string GetGenericTypeName(Type type, int maxDepth = 2) => GetGenericTypeNameInternal(type, maxDepth, false); }
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);
}
private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull) private static string GetGenericTypeNameInternal(Type type, int maxDepth, bool isFull)
{ {
#if (DEBUG && !DISABLE_DEBUG) #if (DEBUG && !DISABLE_DEBUG)
string result = isFull ? type.FullName : type.Name; string typeName = isFull ? type.FullName : type.Name;
if (!type.IsGenericType || maxDepth == 0) if (!type.IsGenericType || maxDepth == 0)
return result; {
return typeName;
}
int genericInfoIndex = typeName.LastIndexOf('`');
if (genericInfoIndex > 0)
{
typeName = typeName.Remove(genericInfoIndex);
}
int iBacktick = result.IndexOf('`'); string genericParams = "";
if (iBacktick > 0)
result = result.Remove(iBacktick);
result += "<";
Type[] typeParameters = type.GetGenericArguments(); Type[] typeParameters = type.GetGenericArguments();
for (int i = 0; i < typeParameters.Length; ++i) 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 $"{typeName}<{genericParams}>";
return result;
#else //optimization for release build #else //optimization for release build
return type.Name; return isFull ? type.FullName : type.Name;
#endif #endif
} }
#endregion #endregion
@ -75,389 +91,382 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region GetName #region GetName
public static string GetName(object obj, int maxGenericDepth = 2) public static string GetMetaName(object obj)
{ {
return obj is IEcsMetaProvider intr ? return GetTypeMeta(obj).Name;
GetName(intr.MetaSource, maxGenericDepth) :
GetName(type: obj.GetType(), maxGenericDepth);
} }
public static string GetName<T>(int maxGenericDepth = 2) => GetName(typeof(T), maxGenericDepth); public static string GetMetaName<T>()
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)
{ {
return obj is IEcsMetaProvider intr ? return GetTypeMeta<T>().Name;
TryGetCustomName(intr.MetaSource, out name) :
TryGetCustomName(type: obj.GetType(), out name);
} }
public static bool TryGetCustomName<T>(out string name) => TryGetCustomName(type: typeof(T), out name); public static string GetMetaName(Type type)
public static bool TryGetCustomName(Type type, out string name)
{ {
if (type.TryGetCustomAttribute(out MetaNameAttribute atr)) return GetTypeMeta(type).Name;
{
name = atr.name;
return true;
}
name = string.Empty;
return false;
} }
#endregion
#region GetGroup public static bool TryGetMetaName(object obj, out string name)
public static MetaGroup GetGroup(object obj)
{ {
return obj is IEcsMetaProvider intr ? TypeMeta meta = GetTypeMeta(obj);
GetGroup(intr.MetaSource) : name = meta.Name;
GetGroup(type: obj.GetType()); return meta.IsCustomName;
} }
public static MetaGroup GetGroup<T>() => GetGroup(typeof(T)); public static bool TryGetMetaName<T>(out string name)
public static MetaGroup GetGroup(Type type) => type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.GetData() : MetaGroup.Empty;
public static bool TryGetGroup(object obj, out MetaGroup group)
{ {
return obj is IEcsMetaProvider intr ? TypeMeta meta = GetTypeMeta<T>();
TryGetGroup(intr.MetaSource, out group) : name = meta.Name;
TryGetGroup(type: obj.GetType(), out group); return meta.IsCustomName;
} }
public static bool TryGetGroup<T>(out MetaGroup text) => TryGetGroup(typeof(T), out text); public static bool TryGetMetaName(Type type, out string name)
public static bool TryGetGroup(Type type, out MetaGroup group)
{ {
if (type.TryGetCustomAttribute(out MetaGroupAttribute atr)) TypeMeta meta = GetTypeMeta(type);
{ name = meta.Name;
group = atr.GetData(); return meta.IsCustomName;
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<T>() => 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<T>(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;
} }
#endregion #endregion
#region GetColor #region GetColor
public static MetaColor GetColor(object obj) public static MetaColor GetColor(object obj)
{ {
return obj is IEcsMetaProvider intr ? return GetTypeMeta(obj).Color;
GetColor(intr.MetaSource) :
GetColor(type: obj.GetType());
} }
public static MetaColor GetColor<T>() public static MetaColor GetColor<T>()
{ {
return GetColor(typeof(T)); return GetTypeMeta<T>().Color;
} }
public static MetaColor GetColor(Type type) public static MetaColor GetColor(Type type)
{ {
var atr = type.GetCustomAttribute<MetaColorAttribute>(); return GetTypeMeta(type).Color;
return atr != null ? atr.color : AutoColor(type);
}
private static MetaColor AutoColor(Type type)
{
return new MetaColor(type.Name).Desaturate(0.75f) / 1.15f;
} }
public static bool TryGetColor(object obj, out MetaColor color) public static bool TryGetColor(object obj, out MetaColor color)
{ {
return obj is IEcsMetaProvider intr ? TypeMeta meta = GetTypeMeta(obj);
TryGetColor(intr.MetaSource, out color) : color = meta.Color;
TryGetColor(type: obj.GetType(), out color); return meta.IsCustomColor;
} }
public static bool TryGetColor<T>(out MetaColor color) public static bool TryGetColor<T>(out MetaColor color)
{ {
return TryGetColor(typeof(T), out color); TypeMeta meta = GetTypeMeta<T>();
color = meta.Color;
return meta.IsCustomColor;
} }
public static bool TryGetColor(Type type, out MetaColor color) public static bool TryGetColor(Type type, out MetaColor color)
{ {
var atr = type.GetCustomAttribute<MetaColorAttribute>(); TypeMeta meta = GetTypeMeta(type);
if (atr != null) color = meta.Color;
{ return meta.IsCustomColor;
color = atr.color; }
return true; #endregion
}
color = MetaColor.BlackColor; #region GetDescription
return false; public static string GetDescription(object obj)
{
return GetTypeMeta(obj).Description;
}
public static string GetDescription<T>()
{
return GetTypeMeta<T>().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<T>(out string description)
{
TypeMeta meta = GetTypeMeta<T>();
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<T>()
{
return GetTypeMeta<T>().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<T>(out MetaGroup group)
{
TypeMeta meta = GetTypeMeta<T>();
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 #endregion
#region GetTags #region GetTags
public static IReadOnlyCollection<string> GetTags(object obj) public static IReadOnlyCollection<string> GetTags(object obj)
{ {
return obj is IEcsMetaProvider intr ? return GetTypeMeta(obj).Tags;
GetTags(intr.MetaSource) :
GetTags(type: obj.GetType());
} }
public static IReadOnlyCollection<string> GetTags<T>() public static IReadOnlyCollection<string> GetTags<T>()
{ {
return GetTags(typeof(T)); return GetTypeMeta<T>().Tags;
} }
public static IReadOnlyCollection<string> GetTags(Type type) public static IReadOnlyCollection<string> GetTags(Type type)
{ {
var atr = type.GetCustomAttribute<MetaTagsAttribute>(); return GetTypeMeta(type).Tags;
return atr != null ? atr.Tags : Array.Empty<string>();
} }
public static bool TryGetTags(object obj, out IReadOnlyCollection<string> tags) public static bool TryGetTags(object obj, out IReadOnlyCollection<string> tags)
{ {
return obj is IEcsMetaProvider intr ? TypeMeta meta = GetTypeMeta(obj);
TryGetTags(intr.MetaSource, out tags) : tags = meta.Tags;
TryGetTags(type: obj.GetType(), out tags); return tags.Count <= 0;
} }
public static bool TryGetTags<T>(out IReadOnlyCollection<string> tags) public static bool TryGetTags<T>(out IReadOnlyCollection<string> tags)
{ {
return TryGetTags(typeof(T), out tags); TypeMeta meta = GetTypeMeta<T>();
tags = meta.Tags;
return tags.Count <= 0;
} }
public static bool TryGetTags(Type type, out IReadOnlyCollection<string> tags) public static bool TryGetTags(Type type, out IReadOnlyCollection<string> tags)
{ {
var atr = type.GetCustomAttribute<MetaTagsAttribute>(); TypeMeta meta = GetTypeMeta(type);
if (atr != null) tags = meta.Tags;
{ return tags.Count <= 0;
tags = atr.Tags;
return true;
}
tags = Array.Empty<string>();
return false;
} }
#endregion #endregion
#region IsHidden #region IsHidden
public static bool IsHidden(object obj) public static bool IsHidden(object obj)
{ {
return obj is IEcsMetaProvider intr ? return GetTypeMeta(obj).IsHidden;
IsHidden(intr.MetaSource) :
IsHidden(type: obj.GetType());
} }
public static bool IsHidden<T>() public static bool IsHidden<T>()
{ {
return IsHidden(typeof(T)); return GetTypeMeta<T>().IsHidden;
} }
public static bool IsHidden(Type type) public static bool IsHidden(Type type)
{ {
return type.TryGetCustomAttribute(out MetaTagsAttribute atr) && atr.Tags.Contains(MetaTags.HIDDEN); return GetTypeMeta(type).IsHidden;
} }
#endregion #endregion
#region MetaSource #region GetTypeMeta
public static bool IsMetaSourceProvided(object obj) 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<T>()
{ {
return obj is IEcsMetaProvider intr ? intr.MetaSource : obj; return GetTypeMeta(typeof(T));
} }
#endregion public static TypeMeta GetTypeMeta(Type type)
#region GenerateTypeMeta
public static TypeMetaData GenerateTypeMeta(object obj)
{ {
return obj is IEcsMetaProvider intr ? if (_metaCache.TryGetValue(type, out TypeMeta result) == false)
GenerateTypeMeta(intr.MetaSource) :
GenerateTypeMeta(type: obj.GetType());
}
public static TypeMetaData GenerateTypeMeta<T>()
{
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<Type, TypeMetaDataCached> _metaCache = new Dictionary<Type, TypeMetaDataCached>();
public static TypeMetaDataCached GetCachedTypeMeta(object obj)
{
return obj is IEcsMetaProvider intr ?
GetCachedTypeMeta(intr.MetaSource) :
GetCachedTypeMeta(type: obj.GetType());
}
public static TypeMetaDataCached GetCachedTypeMeta<T>()
{
return GetCachedTypeMeta(typeof(T));
}
public static TypeMetaDataCached GetCachedTypeMeta(Type type)
{
if (_metaCache.TryGetValue(type, out TypeMetaDataCached result) == false)
{ {
result = new TypeMetaDataCached(type); result = new TypeMeta(type);
_metaCache.Add(type, result); _metaCache.Add(type, result);
} }
return result; return result;
} }
#endregion #endregion
#region ReflectionExtensions #region TypeMetaSource
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsTypeMetaProvided(object obj)
public static bool TryGetCustomAttribute<T>(this Type self, out T attribute) where T : Attribute
{ {
attribute = self.GetCustomAttribute<T>(); return obj is IEcsTypeMetaProvider;
return attribute != null;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static object GetTypeMetaSource(object obj)
public static bool TryGetCustomAttribute<T>(this MemberInfo self, out T attribute) where T : Attribute
{ {
attribute = self.GetCustomAttribute<T>(); return obj is IEcsTypeMetaProvider intr ? intr.MetaSource : obj;
return attribute != null;
} }
#endregion #endregion
} }
[Serializable] public sealed class TypeMeta
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<string> tags;
public TypeMetaData(Type type, string name, MetaGroup group, MetaColor color, string description, IReadOnlyCollection<string> 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
{ {
internal readonly Type _type; internal readonly Type _type;
internal string _name; private bool _isCustomName;
internal MetaGroup _group; private bool _isCustomColor;
internal MetaColor _color; private bool _isHidden;
internal string _description;
internal IReadOnlyCollection<string> _tags; private string _name;
private MetaColor _color;
private string _description;
private MetaGroup _group;
private IReadOnlyCollection<string> _tags;
private int _typeCode;
private TypeMetaData _typeMetaData = null;
private InitFlag _initFlags = InitFlag.None; private InitFlag _initFlags = InitFlag.None;
public TypeMetaDataCached(Type type) #region Constructors
public TypeMeta(Type type)
{ {
_type = type; _type = type;
} }
#endregion
#region Type
public Type Type public Type Type
{ {
get { return _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 public string Name
{ {
get get
{ {
if (_initFlags.HasFlag(InitFlag.Name) == false) InitName();
{
_name = EcsDebugUtility.GetName(_type);
_initFlags |= InitFlag.Name;
}
return _name; 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 get
{ {
if (_initFlags.HasFlag(InitFlag.Group) == false) InitColor();
{ return _isCustomColor;
_group = EcsDebugUtility.GetGroup(_type);
_initFlags |= InitFlag.Group;
}
return _group;
} }
} }
public MetaColor Color public MetaColor Color
{ {
get get
{ {
if (_initFlags.HasFlag(InitFlag.Color) == false) InitColor();
{
_color = EcsDebugUtility.GetColor(_type);
_initFlags |= InitFlag.Color;
}
return _color; return _color;
} }
} }
#endregion
#region Description
public string Description public string Description
{ {
get get
{ {
if (_initFlags.HasFlag(InitFlag.Description) == false) if (_initFlags.HasFlag(InitFlag.Description) == false)
{ {
_description = EcsDebugUtility.GetDescription(_type); _description = MetaCacheGenerator.GetDescription(_type);
_initFlags |= InitFlag.Description; _initFlags |= InitFlag.Description;
} }
return _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<string> Tags public IReadOnlyCollection<string> Tags
{ {
get get
{ {
if (_initFlags.HasFlag(InitFlag.Tags) == false) InitTags();
{
_tags = EcsDebugUtility.GetTags(_type);
_initFlags |= InitFlag.Tags;
}
return _tags; return _tags;
} }
} }
public TypeMetaData AllData public bool IsHidden
{ {
get get
{ {
if (_typeMetaData == null) InitTags();
{ return _isHidden;
_typeMetaData = new TypeMetaData(
Type,
Name,
Group,
Color,
Description,
Tags);
}
return _typeMetaData;
} }
} }
#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() public void InitializeAll()
{ {
if (_initFlags == InitFlag.All) if (_initFlags == InitFlag.All)
@ -469,8 +478,12 @@ namespace DCFApixels.DragonECS
_ = Color; _ = Color;
_ = Description; _ = Description;
_ = Tags; _ = Tags;
_ = TypeCode;
} }
#endregion
#region InitFlag
[Flags]
private enum InitFlag : byte private enum InitFlag : byte
{ {
None = 0, None = 0,
@ -479,7 +492,73 @@ namespace DCFApixels.DragonECS
Color = 1 << 2, Color = 1 << 2,
Description = 1 << 3, Description = 1 << 3,
Tags = 1 << 4, 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<string> GetTags(Type type)
{
var atr = type.GetCustomAttribute<MetaTagsAttribute>();
return atr != null ? atr.Tags : Array.Empty<string>();
}
#endregion
}
}

View File

@ -1,6 +1,6 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsMetaProvider public interface IEcsTypeMetaProvider
{ {
object MetaSource { get; } object MetaSource { get; }
} }

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 5379b4d037441ed4cb4171648c1453d4 guid: 4a642dc8905124247bf83c9d13d8fb13
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

View File

@ -219,7 +219,8 @@ namespace DCFApixels.DragonECS
byte r = this.r; byte r = this.r;
byte g = this.g; byte g = this.g;
byte b = this.b; 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)); r = (byte)(r + (gray - r) * (1 - t));
g = (byte)(g + (gray - g) * (1 - t)); g = (byte)(g + (gray - g) * (1 - t));
b = (byte)(b + (gray - b) * (1 - t)); b = (byte)(b + (gray - b) * (1 - t));

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
@ -8,18 +9,27 @@ namespace DCFApixels.DragonECS
{ {
public static readonly MetaGroupAttribute Empty = new MetaGroupAttribute(""); public static readonly MetaGroupAttribute Empty = new MetaGroupAttribute("");
public readonly string name; public readonly string name;
public readonly string rootCategory; private string[] path = null;
public IReadOnlyCollection<string> Splited
{
get
{
if (path == null)
{
path = Regex.Split(name, @"[/|\\]");
}
return path;
}
}
public MetaGroupAttribute(string name) public MetaGroupAttribute(string name)
{ {
name = Regex.Replace(name, @"^[/|\\]+|[/|\\]+$", ""); name = Regex.Replace(name, @"^[/|\\]+|[/|\\]+$", "");
rootCategory = Regex.Match(name, @"^(.*?)[/\\]").Groups[1].Value;
this.name = name; 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 public readonly struct MetaGroup
{ {
@ -29,9 +39,9 @@ namespace DCFApixels.DragonECS
{ {
get { return _source.name; } get { return _source.name; }
} }
public string RootCategory public IReadOnlyCollection<string> Splited
{ {
get { return _source.rootCategory; } get { return _source.Splited; }
} }
public bool IsNull public bool IsNull
{ {
@ -41,9 +51,5 @@ namespace DCFApixels.DragonECS
{ {
_source = source; _source = source;
} }
public string[] SplitCategories()
{
return _source.SplitCategories();
}
} }
} }

View File

@ -8,9 +8,12 @@ namespace DCFApixels.DragonECS
public sealed class MetaTagsAttribute : EcsMetaAttribute public sealed class MetaTagsAttribute : EcsMetaAttribute
{ {
private readonly string[] _tags = Array.Empty<string>(); private readonly string[] _tags = Array.Empty<string>();
public IReadOnlyCollection<string> Tags => _tags; public IReadOnlyCollection<string> 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() { }
public MetaTagsAttribute(params string[] tags) public MetaTagsAttribute(params string[] tags)
{ {

View File

@ -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<T>(this Type self, out T attribute) where T : Attribute
{
attribute = self.GetCustomAttribute<T>();
return attribute != null;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool TryGetCustomAttribute<T>(this MemberInfo self, out T attribute) where T : Attribute
{
attribute = self.GetCustomAttribute<T>();
return attribute != null;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bfba14901791dae45bab76154086b299
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: