add di for runner

This commit is contained in:
DCFApixels 2025-03-13 20:46:51 +08:00
parent 0f41895093
commit a7d9350332
4 changed files with 54 additions and 26 deletions

View File

@ -32,7 +32,6 @@ namespace DCFApixels.DragonECS
public sealed partial class EcsPipeline public sealed partial class EcsPipeline
{ {
private readonly IConfigContainer _configs; private readonly IConfigContainer _configs;
private Injector.Builder _injectorBuilder;
private Injector _injector; private Injector _injector;
private IEcsProcess[] _allSystems; private IEcsProcess[] _allSystems;
@ -79,8 +78,15 @@ namespace DCFApixels.DragonECS
{ {
_configs = configs; _configs = configs;
_allSystems = systems; _allSystems = systems;
_injectorBuilder = injectorBuilder; injectorBuilder.Inject(this);
_injectorBuilder.Inject(this);
var members = GetProcess<IEcsPipelineMember>();
for (int i = 0; i < members.Length; i++)
{
members[i].Pipeline = this;
}
_injector = injectorBuilder.Build(this);
} }
#endregion #endregion
@ -130,14 +136,18 @@ namespace DCFApixels.DragonECS
{ {
return (TRunner)result; return (TRunner)result;
} }
TRunner instance = new TRunner(); TRunner runnerInstance = new TRunner();
#if DEBUG #if DEBUG && !DISABLE_DEBUG
EcsRunner.CheckRunnerTypeIsValide(runnerType, instance.Interface); EcsRunner.CheckRunnerTypeIsValide(runnerType, runnerInstance.Interface);
#endif #endif
instance.Init_Internal(this); runnerInstance.Init_Internal(this);
_runners.Add(runnerType, instance); _runners.Add(runnerType, runnerInstance);
_runners.Add(instance.Interface, instance); _runners.Add(runnerInstance.Interface, runnerInstance);
return instance; Injector.ExtractAllTo(runnerInstance);
// init after.
Injector.Inject(runnerInstance);
return runnerInstance;
} }
public T GetRunner<T>() where T : IEcsProcess public T GetRunner<T>() where T : IEcsProcess
{ {
@ -178,13 +188,6 @@ namespace DCFApixels.DragonECS
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_initMarker.Begin(); _initMarker.Begin();
#endif #endif
var members = GetProcess<IEcsPipelineMember>();
for (int i = 0; i < members.Length; i++)
{
members[i].Pipeline = this;
}
_injector = _injectorBuilder.Build(this);
_injectorBuilder = null;
GetRunnerInstance<EcsPreInitRunner>().PreInit(); GetRunnerInstance<EcsPreInitRunner>().PreInit();
GetRunnerInstance<EcsInitRunner>().Init(); GetRunnerInstance<EcsInitRunner>().Init();

View File

@ -6,16 +6,11 @@ namespace DCFApixels.DragonECS
public abstract class InjectionNodeBase public abstract class InjectionNodeBase
{ {
private readonly Type _type; private readonly Type _type;
public Type Type public Type Type { get { return _type; } }
{
get { return _type; }
}
public abstract object CurrentInjectedDependencyRaw { get; } public abstract object CurrentInjectedDependencyRaw { get; }
protected InjectionNodeBase(Type type) protected InjectionNodeBase(Type type) { _type = type; }
{
_type = type;
}
public abstract void Inject(object obj); public abstract void Inject(object obj);
public abstract void ExtractTo(object target);
public abstract void Init(EcsPipeline pipeline); public abstract void Init(EcsPipeline pipeline);
} }
} }
@ -23,6 +18,7 @@ namespace DCFApixels.DragonECS.Internal
{ {
internal sealed class InjectionNode<T> : InjectionNodeBase internal sealed class InjectionNode<T> : InjectionNodeBase
{ {
private EcsPipeline _pipeline;
private EcsProcess<IEcsInject<T>> _process; private EcsProcess<IEcsInject<T>> _process;
private T _currentInjectedDependency; private T _currentInjectedDependency;
public sealed override object CurrentInjectedDependencyRaw public sealed override object CurrentInjectedDependencyRaw
@ -38,6 +34,7 @@ namespace DCFApixels.DragonECS.Internal
public InjectionNode() : base(typeof(T)) { } public InjectionNode() : base(typeof(T)) { }
public sealed override void Init(EcsPipeline pipeline) public sealed override void Init(EcsPipeline pipeline)
{ {
_pipeline = pipeline;
_process = pipeline.GetProcess<IEcsInject<T>>(); _process = pipeline.GetProcess<IEcsInject<T>>();
} }
public sealed override void Inject(object raw) public sealed override void Inject(object raw)
@ -48,6 +45,24 @@ namespace DCFApixels.DragonECS.Internal
{ {
_process[i].Inject(obj); _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<T> intrf)
{
intrf.Inject(_currentInjectedDependency);
}
} }
} }
} }

View File

@ -62,6 +62,15 @@ namespace DCFApixels.DragonECS
} }
branch.Inject(raw); 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<T>() public T Extract<T>()
{ {
return (T)Extract_Internal(typeof(T)); return (T)Extract_Internal(typeof(T));

View File

@ -1,11 +1,12 @@
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsInjectProcess : IEcsProcess { }
[MetaName(nameof(Inject))] [MetaName(nameof(Inject))]
[MetaColor(MetaColor.DragonRose)] [MetaColor(MetaColor.DragonRose)]
[MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.DI_GROUP)] [MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.DI_GROUP)]
[MetaDescription(EcsConsts.AUTHOR, "The interface of the dependency injection process.")] [MetaDescription(EcsConsts.AUTHOR, "The interface of the dependency injection process.")]
[MetaID("4C86537C92019AA24383CBF53CBD456C")] [MetaID("4C86537C92019AA24383CBF53CBD456C")]
public interface IEcsInject<T> : IEcsProcess public interface IEcsInject<T> : IEcsInjectProcess
{ {
void Inject(T obj); void Inject(T obj);
} }