From f23ed953d7a4cb3a0d9ec015c808e3ae369d1795 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 30 Mar 2023 05:32:43 +0800 Subject: [PATCH] rename EcsSystems to EcsPipeline --- src/Builtin/InjectSystem.cs | 27 ++++---- src/Builtin/Runners.cs | 32 +++++----- src/{EcsSystems.cs => EcsPipeline.cs} | 63 ++++++++++++++----- ...EcsSystems.cs.meta => EcsPipeline.cs.meta} | 0 src/EcsRunner.cs | 43 ++++++++++--- 5 files changed, 110 insertions(+), 55 deletions(-) rename src/{EcsSystems.cs => EcsPipeline.cs} (75%) rename src/{EcsSystems.cs.meta => EcsPipeline.cs.meta} (100%) diff --git a/src/Builtin/InjectSystem.cs b/src/Builtin/InjectSystem.cs index c714ca6..df1a21b 100644 --- a/src/Builtin/InjectSystem.cs +++ b/src/Builtin/InjectSystem.cs @@ -7,11 +7,11 @@ namespace DCFApixels.DragonECS { internal class PreInitInjectController { - private EcsSystems _source; + private EcsPipeline _source; private InjectSystemBase[] _injectSystems; private int _injectCount; - public PreInitInjectController(EcsSystems source) + public PreInitInjectController(EcsPipeline source) { _injectCount = 0; _source = source; @@ -102,25 +102,26 @@ namespace DCFApixels.DragonECS _injectedData = injectedData; } - public void PreInit(EcsSystems systems) + public void PreInit(EcsPipeline pipeline) { if (_injectController == null) { - _injectController = new PreInitInjectController(systems); - var injectMapRunner = systems.GetRunner>(); - systems.GetRunner().OnPreInitInjectionBefore(); + _injectController = new PreInitInjectController(pipeline); + var injectMapRunner = pipeline.GetRunner>(); + pipeline.GetRunner().OnPreInitInjectionBefore(); injectMapRunner.Inject(_injectController); } - var injectRunnerGeneric = systems.GetRunner>(); + var injectRunnerGeneric = pipeline.GetRunner>(); injectRunnerGeneric.Inject(_injectedData); if (_injectController.OnInject()) { _injectController.Destroy(); - var injectCallbacksRunner = systems.GetRunner(); + var injectCallbacksRunner = pipeline.GetRunner(); injectCallbacksRunner.OnPreInitInjectionAfter(); + EcsRunner.Destroy(injectCallbacksRunner); } } @@ -134,27 +135,27 @@ namespace DCFApixels.DragonECS public static class InjectSystemExstensions { - public static EcsSystems.Builder Inject(this EcsSystems.Builder self, T data) + public static EcsPipeline.Builder Inject(this EcsPipeline.Builder self, T data) { self.Add(new InjectSystem(data)); return self; } - public static EcsSystems.Builder Inject(this EcsSystems.Builder self, A a, B b) + public static EcsPipeline.Builder Inject(this EcsPipeline.Builder self, A a, B b) { self.Inject(a).Inject(b); return self; } - public static EcsSystems.Builder Inject(this EcsSystems.Builder self, A a, B b, C c, D d) + public static EcsPipeline.Builder Inject(this EcsPipeline.Builder self, A a, B b, C c, D d) { self.Inject(a).Inject(b).Inject(c).Inject(d); return self; } - public static EcsSystems.Builder Inject(this EcsSystems.Builder self, A a, B b, C c, D d, E e) + public static EcsPipeline.Builder Inject(this EcsPipeline.Builder self, A a, B b, C c, D d, E e) { self.Inject(a).Inject(b).Inject(c).Inject(d).Inject(e); return self; } - public static EcsSystems.Builder Inject(this EcsSystems.Builder self, A a, B b, C c, D d, E e, F f) + public static EcsPipeline.Builder Inject(this EcsPipeline.Builder self, A a, B b, C c, D d, E e, F f) { self.Inject(a).Inject(b).Inject(c).Inject(d).Inject(e).Inject(f); return self; diff --git a/src/Builtin/Runners.cs b/src/Builtin/Runners.cs index 9b371a5..d4db422 100644 --- a/src/Builtin/Runners.cs +++ b/src/Builtin/Runners.cs @@ -2,19 +2,19 @@ { public interface IEcsPreInitSystem : IEcsSystem { - public void PreInit(EcsSystems systems); + public void PreInit(EcsPipeline pipeline); } public interface IEcsInitSystem : IEcsSystem { - public void Init(EcsSystems systems); + public void Init(EcsPipeline pipeline); } public interface IEcsRunSystem : IEcsSystem { - public void Run(EcsSystems systems); + public void Run(EcsPipeline pipeline); } public interface IEcsDestroySystem : IEcsSystem { - public void Destroy(EcsSystems systems); + public void Destroy(EcsPipeline pipeline); } public interface IEcsBaseSystem : IEcsInitSystem, IEcsRunSystem, IEcsDestroySystem { } @@ -24,18 +24,18 @@ #if DEBUG private int[] _targetIds; #endif - public void PreInit(EcsSystems systems) + public void PreInit(EcsPipeline pipeline) { #if DEBUG for (int i = 0; i < targets.Length; i++) { int id = _targetIds[i]; EcsDebug.ProfileMarkBegin(id); - targets[i].PreInit(systems); + targets[i].PreInit(pipeline); EcsDebug.ProfileMarkEnd(id); } #else - foreach (var item in targets) item.PreInit(systems); + foreach (var item in targets) item.PreInit(pipeline); #endif } @@ -55,18 +55,18 @@ #if DEBUG private int[] _targetIds; #endif - public void Init(EcsSystems systems) + public void Init(EcsPipeline pipeline) { #if DEBUG for (int i = 0; i < targets.Length; i++) { int id = _targetIds[i]; EcsDebug.ProfileMarkBegin(id); - targets[i].Init(systems); + targets[i].Init(pipeline); EcsDebug.ProfileMarkEnd(id); } #else - foreach (var item in targets) item.Init(systems); + foreach (var item in targets) item.Init(pipeline); #endif } @@ -86,18 +86,18 @@ #if DEBUG private int[] _targetIds; #endif - public void Run(EcsSystems systems) + public void Run(EcsPipeline pipeline) { #if DEBUG for (int i = 0; i < targets.Length; i++) { int id = _targetIds[i]; EcsDebug.ProfileMarkBegin(id); - targets[i].Run(systems); + targets[i].Run(pipeline); EcsDebug.ProfileMarkEnd(id); } #else - foreach (var item in targets) item.Run(systems); + foreach (var item in targets) item.Run(pipeline); #endif } @@ -117,18 +117,18 @@ #if DEBUG private int[] _targetIds; #endif - public void Destroy(EcsSystems systems) + public void Destroy(EcsPipeline pipeline) { #if DEBUG for (int i = 0; i < targets.Length; i++) { int id = _targetIds[i]; EcsDebug.ProfileMarkBegin(id); - targets[i].Destroy(systems); + targets[i].Destroy(pipeline); EcsDebug.ProfileMarkEnd(id); } #else - foreach (var item in targets) item.Destroy(systems); + foreach (var item in targets) item.Destroy(pipeline); #endif } diff --git a/src/EcsSystems.cs b/src/EcsPipeline.cs similarity index 75% rename from src/EcsSystems.cs rename to src/EcsPipeline.cs index 5c43b59..2b94331 100644 --- a/src/EcsSystems.cs +++ b/src/EcsPipeline.cs @@ -5,7 +5,7 @@ using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { - public sealed class EcsSystems + public sealed class EcsPipeline { private IEcsSystem[] _allSystems; private Dictionary _runners; @@ -24,7 +24,7 @@ namespace DCFApixels.DragonECS #endregion #region Constructors - private EcsSystems(IEcsSystem[] systems) + private EcsPipeline(IEcsSystem[] systems) { _allSystems = systems; _runners = new Dictionary(); @@ -47,6 +47,11 @@ namespace DCFApixels.DragonECS _runners.Add(type, result); return (T)result; } + + internal void OnRunnerDestroy(IEcsRunner runner) + { + _runners.Remove(runner.Interface); + } #endregion #region LifeCycle @@ -54,7 +59,7 @@ namespace DCFApixels.DragonECS { if(_isInit == true) { - EcsDebug.Print("[Warning]", $"This {nameof(EcsSystems)} has already been initialized"); + EcsDebug.Print("[Warning]", $"This {nameof(EcsPipeline)} has already been initialized"); return; } _isInit = true; @@ -81,7 +86,7 @@ namespace DCFApixels.DragonECS #endif if (_isDestoryed == true) { - EcsDebug.Print("[Warning]", $"This {nameof(EcsSystems)} has already been destroyed"); + EcsDebug.Print("[Warning]", $"This {nameof(EcsPipeline)} has already been destroyed"); return; } _isDestoryed = true; @@ -94,17 +99,17 @@ namespace DCFApixels.DragonECS private void CheckBeforeInitForMethod(string methodName) { if (!_isInit) - throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsSystems)}"); + throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsPipeline)}"); } private void CheckAfterInitForMethod(string methodName) { if (_isInit) - throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsSystems)}"); + throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsPipeline)}"); } private void CheckAfterDestroyForMethod(string methodName) { if (_isDestoryed) - throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsSystems)}"); + throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsPipeline)}"); } #endif #endregion @@ -117,6 +122,7 @@ namespace DCFApixels.DragonECS public class Builder { private const int KEYS_CAPACITY = 4; + private HashSet _uniqueTypes; private readonly List _blockExecutionOrder; private readonly Dictionary> _systems; private readonly object _basicBlocKey; @@ -125,6 +131,7 @@ namespace DCFApixels.DragonECS public Builder() { _basicBlocKey = "Basic"; + _uniqueTypes = new HashSet(); _blockExecutionOrder = new List(KEYS_CAPACITY); _systems = new Dictionary>(KEYS_CAPACITY); _isBasicBlockDeclared = false; @@ -132,6 +139,17 @@ namespace DCFApixels.DragonECS } public Builder Add(IEcsSystem system, object blockKey = null) + { + AddInternal(system, blockKey, false); + return this; + } + public Builder AddUnique(IEcsSystem system, object blockKey = null) + { + AddInternal(system, blockKey, true); + return this; + } + + private void AddInternal(IEcsSystem system, object blockKey, bool isUnique) { if (blockKey == null) blockKey = _basicBlocKey; List list; @@ -141,8 +159,9 @@ namespace DCFApixels.DragonECS list.Add(new SystemsBlockMarkerSystem(blockKey.ToString())); _systems.Add(blockKey, list); } + if ((_uniqueTypes.Add(system.GetType()) == false && isUnique)) + return; list.Add(system); - return this; } public Builder Add(IEcsModule module) @@ -167,11 +186,11 @@ namespace DCFApixels.DragonECS return this; } - public EcsSystems Build() + public EcsPipeline Build() { if (_isOnlyBasicBlock) { - return new EcsSystems(_systems[_basicBlocKey].ToArray()); + return new EcsPipeline(_systems[_basicBlocKey].ToArray()); } if(_isBasicBlockDeclared == false) @@ -196,7 +215,7 @@ namespace DCFApixels.DragonECS } } - return new EcsSystems(result.ToArray()); + return new EcsPipeline(result.ToArray()); } } #endregion @@ -204,16 +223,17 @@ namespace DCFApixels.DragonECS public interface IEcsModule { - public void ImportSystems(EcsSystems.Builder builder); + public void ImportSystems(EcsPipeline.Builder builder); } - public static class EcsSystemsExt + #region Extensions + public static class EcsSystemsExtensions { - public static bool IsNullOrDestroyed(this EcsSystems self) + public static bool IsNullOrDestroyed(this EcsPipeline self) { return self == null || self.IsDestoryed; } - public static EcsSystems.Builder Add(this EcsSystems.Builder self, IEnumerable range, object blockKey = null) + public static EcsPipeline.Builder Add(this EcsPipeline.Builder self, IEnumerable range, object blockKey = null) { foreach (var item in range) { @@ -221,12 +241,21 @@ namespace DCFApixels.DragonECS } return self; } - public static EcsSystems BuildAndInit(this EcsSystems.Builder self) + public static EcsPipeline.Builder AddUnique(this EcsPipeline.Builder self, IEnumerable range, object blockKey = null) { - EcsSystems result = self.Build(); + foreach (var item in range) + { + self.AddUnique(item, blockKey); + } + return self; + } + public static EcsPipeline BuildAndInit(this EcsPipeline.Builder self) + { + EcsPipeline result = self.Build(); result.Init(); return result; } } + #endregion } diff --git a/src/EcsSystems.cs.meta b/src/EcsPipeline.cs.meta similarity index 100% rename from src/EcsSystems.cs.meta rename to src/EcsPipeline.cs.meta diff --git a/src/EcsRunner.cs b/src/EcsRunner.cs index 08405d7..6233e42 100644 --- a/src/EcsRunner.cs +++ b/src/EcsRunner.cs @@ -24,10 +24,15 @@ namespace DCFApixels.DragonECS public interface IEcsSystem { } public interface IEcsRunner { - public EcsSystems Source { get; } + public EcsPipeline Source { get; } + public Type Interface { get; } public IList Targets { get; } public object Filter { get; } public bool IsHasFilter { get; } + public bool IsDestroyed { get; } + public bool IsEmpty { get; } + + public void Destroy(); } internal static class EcsRunnerActivator @@ -120,6 +125,10 @@ namespace DCFApixels.DragonECS } } + public static class EcsRunner + { + public static void Destroy(object runner) => ((IEcsRunner)runner).Destroy(); + } public abstract class EcsRunner : IEcsSystem, IEcsRunner where TInterface : IEcsSystem { @@ -183,7 +192,7 @@ namespace DCFApixels.DragonECS #endregion #region Instantiate - private static TInterface Instantiate(EcsSystems source, TInterface[] targets, bool isHasFilter, object filter) + private static TInterface Instantiate(EcsPipeline source, TInterface[] targets, bool isHasFilter, object filter) { if (_subclass == null) EcsRunnerActivator.InitFor(); @@ -191,30 +200,34 @@ namespace DCFApixels.DragonECS var instance = (EcsRunner)Activator.CreateInstance(_subclass); return (TInterface)(IEcsSystem)instance.Set(source, targets, isHasFilter, filter); } - public static TInterface Instantiate(EcsSystems source) + public static TInterface Instantiate(EcsPipeline source) { return Instantiate(source, FilterSystems(source.AllSystems), false, null); } - public static TInterface Instantiate(EcsSystems source, object filter) + public static TInterface Instantiate(EcsPipeline source, object filter) { return Instantiate(source, FilterSystems(source.AllSystems, filter), true, filter); } #endregion - private EcsSystems _source; + private EcsPipeline _source; protected TInterface[] targets; private ReadOnlyCollection _targetsSealed; private object _filter; private bool _isHasFilter; + private bool _isDestroyed; #region Properties - public EcsSystems Source => _source; + public EcsPipeline Source => _source; + public Type Interface => typeof(TInterface); public IList Targets => _targetsSealed; public object Filter => _filter; public bool IsHasFilter => _isHasFilter; + public bool IsDestroyed => _isDestroyed; + public bool IsEmpty => targets == null || targets.Length <= 0; #endregion - private EcsRunner Set(EcsSystems source, TInterface[] targets, bool isHasFilter, object filter) + private EcsRunner Set(EcsPipeline source, TInterface[] targets, bool isHasFilter, object filter) { _source = source; this.targets = targets; @@ -225,8 +238,6 @@ namespace DCFApixels.DragonECS return this; } - protected virtual void OnSetup() { } - internal void Rebuild() { if(_isHasFilter) @@ -234,6 +245,20 @@ namespace DCFApixels.DragonECS else Set(_source, FilterSystems(_source.AllSystems, _filter), _isHasFilter, _filter); } + + public void Destroy() + { + _isDestroyed = true; + _source.OnRunnerDestroy(this); + _source = null; + targets = null; + _targetsSealed = null; + _filter = null; + OnDestroy(); + } + + protected virtual void OnSetup() { } + protected virtual void OnDestroy() { } } #region Extensions