From a7d9350332a2fbe1e4477ab21969f9581ff84565 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 13 Mar 2025 20:46:51 +0800 Subject: [PATCH] add di for runner --- src/EcsPipeline.cs | 37 +++++++++++++++------------ src/Injections/Graph/InjectionNode.cs | 31 ++++++++++++++++------ src/Injections/Injector.cs | 9 +++++++ src/Injections/Utils/Interfaces.cs | 3 ++- 4 files changed, 54 insertions(+), 26 deletions(-) diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index 89403df..12b9ea5 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -32,7 +32,6 @@ namespace DCFApixels.DragonECS public sealed partial class EcsPipeline { private readonly IConfigContainer _configs; - private Injector.Builder _injectorBuilder; private Injector _injector; private IEcsProcess[] _allSystems; @@ -79,8 +78,15 @@ namespace DCFApixels.DragonECS { _configs = configs; _allSystems = systems; - _injectorBuilder = injectorBuilder; - _injectorBuilder.Inject(this); + injectorBuilder.Inject(this); + + var members = GetProcess(); + for (int i = 0; i < members.Length; i++) + { + members[i].Pipeline = this; + } + + _injector = injectorBuilder.Build(this); } #endregion @@ -130,14 +136,18 @@ namespace DCFApixels.DragonECS { return (TRunner)result; } - TRunner instance = new TRunner(); -#if DEBUG - EcsRunner.CheckRunnerTypeIsValide(runnerType, instance.Interface); + TRunner runnerInstance = new TRunner(); +#if DEBUG && !DISABLE_DEBUG + EcsRunner.CheckRunnerTypeIsValide(runnerType, runnerInstance.Interface); #endif - instance.Init_Internal(this); - _runners.Add(runnerType, instance); - _runners.Add(instance.Interface, instance); - return instance; + runnerInstance.Init_Internal(this); + _runners.Add(runnerType, runnerInstance); + _runners.Add(runnerInstance.Interface, runnerInstance); + Injector.ExtractAllTo(runnerInstance); + + // init after. + Injector.Inject(runnerInstance); + return runnerInstance; } public T GetRunner() where T : IEcsProcess { @@ -178,13 +188,6 @@ namespace DCFApixels.DragonECS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS _initMarker.Begin(); #endif - var members = GetProcess(); - for (int i = 0; i < members.Length; i++) - { - members[i].Pipeline = this; - } - _injector = _injectorBuilder.Build(this); - _injectorBuilder = null; GetRunnerInstance().PreInit(); GetRunnerInstance().Init(); diff --git a/src/Injections/Graph/InjectionNode.cs b/src/Injections/Graph/InjectionNode.cs index 8851f71..968d774 100644 --- a/src/Injections/Graph/InjectionNode.cs +++ b/src/Injections/Graph/InjectionNode.cs @@ -6,16 +6,11 @@ namespace DCFApixels.DragonECS public abstract class InjectionNodeBase { private readonly Type _type; - public Type Type - { - get { return _type; } - } + public Type Type { get { return _type; } } public abstract object CurrentInjectedDependencyRaw { get; } - protected InjectionNodeBase(Type type) - { - _type = type; - } + protected InjectionNodeBase(Type type) { _type = type; } public abstract void Inject(object obj); + public abstract void ExtractTo(object target); public abstract void Init(EcsPipeline pipeline); } } @@ -23,6 +18,7 @@ namespace DCFApixels.DragonECS.Internal { internal sealed class InjectionNode : InjectionNodeBase { + private EcsPipeline _pipeline; private EcsProcess> _process; private T _currentInjectedDependency; public sealed override object CurrentInjectedDependencyRaw @@ -38,6 +34,7 @@ namespace DCFApixels.DragonECS.Internal public InjectionNode() : base(typeof(T)) { } public sealed override void Init(EcsPipeline pipeline) { + _pipeline = pipeline; _process = pipeline.GetProcess>(); } public sealed override void Inject(object raw) @@ -48,6 +45,24 @@ namespace DCFApixels.DragonECS.Internal { _process[i].Inject(obj); } + foreach (var runner in _pipeline.AllRunners) + { + ExtractTo_Internal(runner.Value); + } + } + public sealed override void ExtractTo(object target) + { + if (_currentInjectedDependency == null) { return; } + ExtractTo_Internal(target); + } + private void ExtractTo_Internal(object target) + { + var type = target.GetType(); + var intrfs = type.GetInterfaces(); + if (target is IEcsInject intrf) + { + intrf.Inject(_currentInjectedDependency); + } } } } diff --git a/src/Injections/Injector.cs b/src/Injections/Injector.cs index 3efe4bf..186d485 100644 --- a/src/Injections/Injector.cs +++ b/src/Injections/Injector.cs @@ -62,6 +62,15 @@ namespace DCFApixels.DragonECS } branch.Inject(raw); } + public void ExtractAllTo(object target) + { + if (target is IEcsInjectProcess == false) { return; } + + foreach (var node in _nodes) + { + node.Value.ExtractTo(target); + } + } public T Extract() { return (T)Extract_Internal(typeof(T)); diff --git a/src/Injections/Utils/Interfaces.cs b/src/Injections/Utils/Interfaces.cs index e7bc9ad..07e02e6 100644 --- a/src/Injections/Utils/Interfaces.cs +++ b/src/Injections/Utils/Interfaces.cs @@ -1,11 +1,12 @@ namespace DCFApixels.DragonECS { + public interface IEcsInjectProcess : IEcsProcess { } [MetaName(nameof(Inject))] [MetaColor(MetaColor.DragonRose)] [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.DI_GROUP)] [MetaDescription(EcsConsts.AUTHOR, "The interface of the dependency injection process.")] [MetaID("4C86537C92019AA24383CBF53CBD456C")] - public interface IEcsInject : IEcsProcess + public interface IEcsInject : IEcsInjectProcess { void Inject(T obj); }