From 5bebf812f16062d84e7c2a801f74823d56669798 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 30 Sep 2024 19:29:19 +0800 Subject: [PATCH] stash --- src/DebugUtils/EcsDebug.cs | 70 +++++++++++++++++++++---------- src/DebugUtils/TypeMeta.cs | 19 ++++++--- src/Internal/ReflectionUtility.cs | 16 +++++-- 3 files changed, 76 insertions(+), 29 deletions(-) diff --git a/src/DebugUtils/EcsDebug.cs b/src/DebugUtils/EcsDebug.cs index 6828d5f..6f8cd45 100644 --- a/src/DebugUtils/EcsDebug.cs +++ b/src/DebugUtils/EcsDebug.cs @@ -2,39 +2,71 @@ using System; using System.Collections; using System.Collections.Generic; -using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { + using static EcsConsts; public readonly struct EcsProfilerMarker { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) public readonly int id; - internal EcsProfilerMarker(int id) { this.id = id; } +#endif + internal EcsProfilerMarker(int id) + { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) + this.id = id; +#endif + } public EcsProfilerMarker(string name) { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) id = DebugService.Instance.RegisterMark(name); +#endif } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Begin() { DebugService.Instance.ProfilerMarkBegin(id); } + public void Begin() + { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) + DebugService.Instance.ProfilerMarkBegin(id); +#endif + } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void End() { DebugService.Instance.ProfilerMarkEnd(id); } + public void End() + { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) + DebugService.Instance.ProfilerMarkEnd(id); +#endif + } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public AutoScope Auto() { return new AutoScope(id); } + public AutoScope Auto() + { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) + return new AutoScope(id); +#else + return default; +#endif + } public readonly ref struct AutoScope { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) private readonly int _id; +#endif [MethodImpl(MethodImplOptions.AggressiveInlining)] public AutoScope(int id) { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) _id = id; DebugService.Instance.ProfilerMarkBegin(id); +#endif } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Dispose() { +#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) DebugService.Instance.ProfilerMarkEnd(_id); +#endif } } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -42,14 +74,10 @@ namespace DCFApixels.DragonECS } [MetaColor(MetaColor.DragonRose)] - [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.DEBUG_GROUP)] - [MetaDescription(EcsConsts.AUTHOR, "Debugging utility. To modify or change the behavior, create a new class inherited from DebugService and set this service using DebugService.Set().")] + [MetaGroup(PACK_GROUP, DEBUG_GROUP)] + [MetaDescription(AUTHOR, "Debugging utility. To modify or change the behavior, create a new class inherited from DebugService and set this service using DebugService.Set().")] public static class EcsDebug { - public const string WARNING_TAG = EcsConsts.DEBUG_WARNING_TAG; - public const string ERROR_TAG = EcsConsts.DEBUG_ERROR_TAG; - public const string PASS_TAG = EcsConsts.DEBUG_PASS_TAG; - public static void Set() where T : DebugService, new() { DebugService.Set(); @@ -271,7 +299,10 @@ namespace DCFApixels.DragonECS } public sealed class DefaultDebugService : DebugService { +#if !UNITY_5_3_OR_NEWER private const string PROFILER_MARKER = "ProfilerMark"; + private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] "; + private readonly struct MarkerData { public readonly Stopwatch Stopwatch; @@ -289,18 +320,14 @@ namespace DCFApixels.DragonECS } } private MarkerData[] _stopwatchs; -#if !UNITY_5_3_OR_NEWER [ThreadStatic] private static char[] _buffer; -#endif public DefaultDebugService() { Console.ForegroundColor = ConsoleColor.White; Console.BackgroundColor = ConsoleColor.Black; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER _stopwatchs = new MarkerData[64]; -#endif } public override void Print(string tag, object v) @@ -337,11 +364,8 @@ namespace DCFApixels.DragonECS Console.ForegroundColor = color; } - private const string PROFILER_MARKER_CACHE = "[" + PROFILER_MARKER + "] "; public override void ProfilerMarkBegin(int id) { -#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) && !UNITY_5_3_OR_NEWER - var color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGray; _stopwatchs[id].Stopwatch.Start(); @@ -351,11 +375,9 @@ namespace DCFApixels.DragonECS Console.WriteLine("> "); Console.ForegroundColor = color; -#endif } public override void ProfilerMarkEnd(int id) { -#if ((DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_DEBUGGER) && !UNITY_5_3_OR_NEWER var color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.DarkGray; _stopwatchs[id].Stopwatch.Stop(); @@ -373,7 +395,6 @@ namespace DCFApixels.DragonECS Console.WriteLine(_buffer, 0, written); Console.ForegroundColor = color; -#endif } protected override void OnDelProfilerMark(int id) @@ -441,5 +462,12 @@ namespace DCFApixels.DragonECS written = bufferLength - zeroPartLength; } +#endif + public override void Break() { } + public override void Print(string tag, object v) { } + public override void ProfilerMarkBegin(int id) { } + public override void ProfilerMarkEnd(int id) { } + protected override void OnDelProfilerMark(int id) { } + protected override void OnNewProfilerMark(int id, string name) { } } } \ No newline at end of file diff --git a/src/DebugUtils/TypeMeta.cs b/src/DebugUtils/TypeMeta.cs index 8197599..0361196 100644 --- a/src/DebugUtils/TypeMeta.cs +++ b/src/DebugUtils/TypeMeta.cs @@ -316,10 +316,19 @@ namespace DCFApixels.DragonECS public static bool IsHasMeta(Type type) { #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED - return (CheckEcsMemener(type) || Attribute.GetCustomAttributes(type, typeof(EcsMetaAttribute), false).Length > 0); + return CheckEcsMemener(type) || Attribute.GetCustomAttributes(type, typeof(EcsMetaAttribute), false).Length > 0; #else EcsDebug.PrintWarning($"Reflection is not available, the {nameof(TypeMeta)}.{nameof(IsHasMeta)} method does not work."); return false; +#endif + } + public static bool IsHasMetaID(Type type) + { +#if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED + return type.HasAttribute(); +#else + EcsDebug.PrintWarning($"Reflection is not available, the {nameof(TypeMeta)}.{nameof(IsHasMetaID)} method does not work."); + return false; #endif } public override string ToString() { return Name; } @@ -394,7 +403,7 @@ namespace DCFApixels.DragonECS public static (string, bool) GetMetaName(Type type) { #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает - bool isCustom = type.TryGetCustomAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false; + bool isCustom = type.TryGetAttribute(out MetaNameAttribute atr) && string.IsNullOrEmpty(atr.name) == false; if (isCustom) { if ((type.IsGenericType && atr.isHideGeneric == false) == false) @@ -426,7 +435,7 @@ namespace DCFApixels.DragonECS public static (MetaColor, bool) GetColor(Type type) { #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает - bool isCustom = type.TryGetCustomAttribute(out MetaColorAttribute atr); + bool isCustom = type.TryGetAttribute(out MetaColorAttribute atr); return (isCustom ? atr.color : AutoColor(type), isCustom); #else EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetColor)} method does not work."); @@ -439,7 +448,7 @@ namespace DCFApixels.DragonECS public static MetaGroup GetGroup(Type type) { #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает - return type.TryGetCustomAttribute(out MetaGroupAttribute atr) ? atr.Data : MetaGroup.Empty; + return type.TryGetAttribute(out MetaGroupAttribute atr) ? atr.Data : MetaGroup.Empty; #else EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetGroup)} method does not work."); return MetaGroup.Empty; @@ -451,7 +460,7 @@ namespace DCFApixels.DragonECS public static MetaDescription GetDescription(Type type) { #if (DEBUG && !DISABLE_DEBUG) || !REFLECTION_DISABLED //в дебажных утилитах REFLECTION_DISABLED только в релизном билде работает - bool isCustom = type.TryGetCustomAttribute(out MetaDescriptionAttribute atr); + bool isCustom = type.TryGetAttribute(out MetaDescriptionAttribute atr); return isCustom ? atr.Data : MetaDescription.Empty; #else EcsDebug.PrintWarning($"Reflection is not available, the {nameof(MetaGenerator)}.{nameof(GetDescription)} method does not work."); diff --git a/src/Internal/ReflectionUtility.cs b/src/Internal/ReflectionUtility.cs index d5f308c..38e2168 100644 --- a/src/Internal/ReflectionUtility.cs +++ b/src/Internal/ReflectionUtility.cs @@ -7,16 +7,26 @@ namespace DCFApixels.DragonECS.Internal internal static class ReflectionUtility { [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static bool TryGetCustomAttribute(this Type self, out T attribute) where T : Attribute + public static bool TryGetAttribute(this MemberInfo 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 + public static bool TryGetAttribute(this MemberInfo self, bool inherit, out T attribute) where T : Attribute { - attribute = self.GetCustomAttribute(); + attribute = self.GetCustomAttribute(inherit); return attribute != null; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool HasAttribute(this MemberInfo self) where T : Attribute + { + return self.GetCustomAttribute() != null; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static bool HasAttribute(this MemberInfo self, bool inherit) where T : Attribute + { + return self.GetCustomAttribute(inherit) != null; + } } }