From 06573d0dcbcfcd5c6295bbad28005fc0bd616ea6 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 25 Feb 2024 18:33:17 +0800 Subject: [PATCH] add custom injection node --- src/EcsPipeline.cs | 23 ++++++++----- src/Injections/Graph/CustomInjectionNode.cs | 36 +++++++++++++++++++++ src/Injections/Graph/InjectionNode.cs | 6 ++-- src/Injections/Injector.cs | 25 +++++++++++++- 4 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 src/Injections/Graph/CustomInjectionNode.cs diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index 260a97c..8b3c714 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -27,7 +27,7 @@ namespace DCFApixels.DragonECS private bool _isDestoryed = false; #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - private EcsProfilerMarker _initBarker = new EcsProfilerMarker("EcsPipeline.Init"); + private EcsProfilerMarker _initMarker = new EcsProfilerMarker("EcsPipeline.Init"); #endif #region Properties @@ -63,6 +63,7 @@ namespace DCFApixels.DragonECS _config = config; _allSystems = systems; _injectorBuilder = injectorBuilder; + _injectorBuilder.Inject(this); } #endregion @@ -138,13 +139,8 @@ namespace DCFApixels.DragonECS return; } #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - _initBarker.Begin(); + _initMarker.Begin(); #endif - EcsProcess members = GetProcess(); - foreach (var member in members) - { - member.Pipeline = this; - } _injector = _injectorBuilder.Build(this); _injectorBuilder = null; @@ -156,7 +152,7 @@ namespace DCFApixels.DragonECS GC.Collect(); #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - _initBarker.End(); + _initMarker.End(); #endif } @@ -223,6 +219,7 @@ namespace DCFApixels.DragonECS _injector.Declare(); _injector.Declare(); _injector.Declare(); + _injector.CustomDeclare(new PipelinePropertyInjectionNode()); _basicLayer = EcsConsts.BASIC_LAYER; Layers = new LayerList(this, _basicLayer); @@ -562,4 +559,14 @@ namespace DCFApixels.DragonECS } } #endregion +} +namespace DCFApixels.DragonECS.Internal +{ + internal sealed class PipelinePropertyInjectionNode : CustomInjectionNode + { + public sealed override void InjectTo(IEcsPipelineMember system, EcsPipeline obj) + { + system.Pipeline = obj; + } + } } \ No newline at end of file diff --git a/src/Injections/Graph/CustomInjectionNode.cs b/src/Injections/Graph/CustomInjectionNode.cs new file mode 100644 index 0000000..ada193c --- /dev/null +++ b/src/Injections/Graph/CustomInjectionNode.cs @@ -0,0 +1,36 @@ +using System; + +namespace DCFApixels.DragonECS +{ + public abstract class CustomInjectionNodeBase : InjectionNodeBase + { + protected CustomInjectionNodeBase(Type type) : base(type) { } + } + public abstract class CustomInjectionNode : CustomInjectionNodeBase + where TProcess : IEcsProcess + { + private EcsProcess _processProperty; + private EcsProcess> _process; + public CustomInjectionNode() : base(typeof(TType)) { } + public sealed override void Init(EcsPipeline pipeline) + { + _processProperty = pipeline.GetProcess(); + _process = pipeline.GetProcess>(); + OnInitialized(pipeline); + } + public sealed override void Inject(object obj) + { + TType target = (TType)obj; + for (int i = 0; i < _process.Length; i++) + { + _process[i].Inject(target); + } + for (int i = 0; i < _processProperty.Length; i++) + { + InjectTo(_processProperty[i], target); + } + } + public virtual void OnInitialized(EcsPipeline pipeline) { } + public abstract void InjectTo(TProcess system, TType obj); + } +} diff --git a/src/Injections/Graph/InjectionNode.cs b/src/Injections/Graph/InjectionNode.cs index a5efc97..a19372a 100644 --- a/src/Injections/Graph/InjectionNode.cs +++ b/src/Injections/Graph/InjectionNode.cs @@ -16,15 +16,15 @@ namespace DCFApixels.DragonECS public abstract void Inject(object obj); public abstract void Init(EcsPipeline pipeline); } - public class InjectionNode : InjectionNodeBase + public sealed class InjectionNode : InjectionNodeBase { private EcsProcess> _process; public InjectionNode(Type type) : base(type) { } - public override void Init(EcsPipeline pipeline) + public sealed override void Init(EcsPipeline pipeline) { _process = pipeline.GetProcess>(); } - public override void Inject(object obj) + public sealed override void Inject(object obj) { for (int i = 0; i < _process.Length; i++) { diff --git a/src/Injections/Injector.cs b/src/Injections/Injector.cs index 3d849cd..9854326 100644 --- a/src/Injections/Injector.cs +++ b/src/Injections/Injector.cs @@ -1,4 +1,5 @@ -using System; +using DCFApixels.DragonECS.Internal; +using System; using System.Collections.Generic; namespace DCFApixels.DragonECS @@ -111,6 +112,23 @@ namespace DCFApixels.DragonECS } return true; } + private bool TryDeclareProperty(CustomInjectionNodeBase injectionProperty) + { + Type type = injectionProperty.Type; + if (_nodes.TryGetValue(type, out InjectionNodeBase oldNode)) + { + Throw.Exception("Already declared"); + } + InitNode(injectionProperty); +#if !REFLECTION_DISABLED + if (IsCanInstantiated(type)) +#endif + { + InitBranch(new InjectionBranch(this, type, true)); + } + return true; + } + public class Builder { private EcsPipeline.Builder _source; @@ -126,6 +144,11 @@ namespace DCFApixels.DragonECS _instance.TryDeclare(); return _source; } + public EcsPipeline.Builder CustomDeclare(CustomInjectionNodeBase injectionProperty) + { + _instance.TryDeclareProperty(injectionProperty); + return _source; + } public EcsPipeline.Builder Inject(T obj) { _initInjections.Add(new InitInject(obj));