From aa80699f5a3bd048eae5671eb052307215ce3abd Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 27 Mar 2023 17:34:12 +0800 Subject: [PATCH] Add debug feature --- src/Debug.meta | 8 + src/{ => Debug}/Attributes.meta | 0 .../Attributes/DebugColorAttribute.cs | 0 .../Attributes/DebugColorAttribute.cs.meta | 0 src/Debug/EcsDebug.cs | 154 ++++++++++++++++++ src/Debug/EcsDebug.cs.meta | 11 ++ src/EcsRunner.cs | 10 ++ 7 files changed, 183 insertions(+) create mode 100644 src/Debug.meta rename src/{ => Debug}/Attributes.meta (100%) rename src/{ => Debug}/Attributes/DebugColorAttribute.cs (100%) rename src/{ => Debug}/Attributes/DebugColorAttribute.cs.meta (100%) create mode 100644 src/Debug/EcsDebug.cs create mode 100644 src/Debug/EcsDebug.cs.meta diff --git a/src/Debug.meta b/src/Debug.meta new file mode 100644 index 0000000..4079cf9 --- /dev/null +++ b/src/Debug.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2121c71030effff4ab087ff83e5cfc95 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Attributes.meta b/src/Debug/Attributes.meta similarity index 100% rename from src/Attributes.meta rename to src/Debug/Attributes.meta diff --git a/src/Attributes/DebugColorAttribute.cs b/src/Debug/Attributes/DebugColorAttribute.cs similarity index 100% rename from src/Attributes/DebugColorAttribute.cs rename to src/Debug/Attributes/DebugColorAttribute.cs diff --git a/src/Attributes/DebugColorAttribute.cs.meta b/src/Debug/Attributes/DebugColorAttribute.cs.meta similarity index 100% rename from src/Attributes/DebugColorAttribute.cs.meta rename to src/Debug/Attributes/DebugColorAttribute.cs.meta diff --git a/src/Debug/EcsDebug.cs b/src/Debug/EcsDebug.cs new file mode 100644 index 0000000..9abb76c --- /dev/null +++ b/src/Debug/EcsDebug.cs @@ -0,0 +1,154 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; + + +namespace DCFApixels.DragonECS +{ + public static class EcsDebug + { + public static void Set() where T : DebugService, new() => DebugService.Set(); + public static void Set(DebugService service) => DebugService.Set(service); + + + public static void Print(object v) => DebugService.Instance.Print(v); + public static void Print(string tag, object v) + { +#if !DISABLE_ECS_DEBUG + DebugService.Instance.Print(tag, v); +#endif + } + public static int RegisterMark(string name) + { +#if !DISABLE_ECS_DEBUG + return DebugService.Instance.RegisterMark(name); +#else + return 0; +#endif + } + public static void DeleteMark(string name) + { +#if !DISABLE_ECS_DEBUG + DebugService.Instance.DeleteMark(name); +#endif + } + public static void ProfileMarkBegin(int id) + { +#if !DISABLE_ECS_DEBUG + DebugService.Instance.ProfileMarkBegin(id); +#endif + } + public static double ProfileMarkEnd(int id) + { +#if !DISABLE_ECS_DEBUG + return DebugService.Instance.ProfileMarkEnd(id); +#else + return 0; +#endif + } + + } + public abstract class DebugService + { + private static DebugService _instance; + + public static DebugService Instance + { + get + { + if (_instance == null) + { + _instance = new DefaultDebugService(); + } + return _instance; + } + } + + public static void Set() where T : DebugService, new() => Set(new T()); + public static void Set(DebugService service) + { + _instance = service; + OnServiceChanged(_instance); + } + + + public static Action OnServiceChanged = delegate { }; + + + private IntDispenser _idDispenser = new IntDispenser(0); + private Dictionary _nameIdTable = new Dictionary(); + + public void Print(object v) => Print(null, v); + public abstract void Print(string tag, object v); + + + public int RegisterMark(string name) + { + int id; + if(!_nameIdTable.TryGetValue(name, out id)) + { + id = _idDispenser.GetFree(); + _nameIdTable.Add(name, id); + } + OnNewMark(id, name); + return id; + } + public void DeleteMark(string name) + { + int id = _nameIdTable[name]; + _nameIdTable.Remove(name); + _idDispenser.Release(id); + OnDelMark(id); + + } + + protected abstract void OnNewMark(int id, string name); + protected abstract void OnDelMark(int id); + + public abstract void ProfileMarkBegin(int id); + public abstract double ProfileMarkEnd(int id); + + // public abstract IEnumerable<> + } + + public sealed class DefaultDebugService : DebugService + { + private Stopwatch[] _stopwatchs; + + public DefaultDebugService() + { +#if !DISABLE_ECS_DEBUG + _stopwatchs = new Stopwatch[64]; +#endif + } + + public override void Print(string tag, object v) + { + Console.WriteLine($"[{tag}] {v}"); + } + + public override void ProfileMarkBegin(int id) + { + _stopwatchs[id].Start(); + } + + public override double ProfileMarkEnd(int id) + { + _stopwatchs[id].Stop(); + var time = _stopwatchs[id].Elapsed; + _stopwatchs[id].Reset(); + return time.TotalSeconds; + } + + protected override void OnDelMark(int id) + { + _stopwatchs[id] = null; + } + + protected override void OnNewMark(int id, string name) + { + if (id >= _stopwatchs.Length) Array.Resize(ref _stopwatchs, _stopwatchs.Length << 1); + _stopwatchs[id] = new Stopwatch(); + } + } +} \ No newline at end of file diff --git a/src/Debug/EcsDebug.cs.meta b/src/Debug/EcsDebug.cs.meta new file mode 100644 index 0000000..e9867aa --- /dev/null +++ b/src/Debug/EcsDebug.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0b40701424b50164b9c76021678c908b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/EcsRunner.cs b/src/EcsRunner.cs index adeb10b..daef3b5 100644 --- a/src/EcsRunner.cs +++ b/src/EcsRunner.cs @@ -27,6 +27,12 @@ namespace DCFApixels.DragonECS } } + [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)] + sealed class MyAttribute : Attribute + { + + } + public interface IEcsSystem { } public interface IEcsRunner { @@ -211,15 +217,19 @@ namespace DCFApixels.DragonECS public IList Targets => _targetsSealed; public object Filter => _filter; public bool IsHasFilter => _isHasFilter; + private EcsRunner Set(TInterface[] targets, bool isHasFilter, object filter) { this.targets = targets; _targetsSealed = new ReadOnlyCollection(targets); _filter = filter; _isHasFilter = isHasFilter; + OnSetup(); return this; } + protected virtual void OnSetup() { } + internal void Rebuild(IEnumerable targets) { if(_isHasFilter)