add custom injection node

This commit is contained in:
Mikhail 2024-02-25 18:33:17 +08:00
parent 6164d2f0d3
commit 06573d0dcb
4 changed files with 78 additions and 12 deletions

View File

@ -27,7 +27,7 @@ namespace DCFApixels.DragonECS
private bool _isDestoryed = false; private bool _isDestoryed = false;
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
private EcsProfilerMarker _initBarker = new EcsProfilerMarker("EcsPipeline.Init"); private EcsProfilerMarker _initMarker = new EcsProfilerMarker("EcsPipeline.Init");
#endif #endif
#region Properties #region Properties
@ -63,6 +63,7 @@ namespace DCFApixels.DragonECS
_config = config; _config = config;
_allSystems = systems; _allSystems = systems;
_injectorBuilder = injectorBuilder; _injectorBuilder = injectorBuilder;
_injectorBuilder.Inject(this);
} }
#endregion #endregion
@ -138,13 +139,8 @@ namespace DCFApixels.DragonECS
return; return;
} }
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_initBarker.Begin(); _initMarker.Begin();
#endif #endif
EcsProcess<IEcsPipelineMember> members = GetProcess<IEcsPipelineMember>();
foreach (var member in members)
{
member.Pipeline = this;
}
_injector = _injectorBuilder.Build(this); _injector = _injectorBuilder.Build(this);
_injectorBuilder = null; _injectorBuilder = null;
@ -156,7 +152,7 @@ namespace DCFApixels.DragonECS
GC.Collect(); GC.Collect();
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
_initBarker.End(); _initMarker.End();
#endif #endif
} }
@ -223,6 +219,7 @@ namespace DCFApixels.DragonECS
_injector.Declare<object>(); _injector.Declare<object>();
_injector.Declare<EcsWorld>(); _injector.Declare<EcsWorld>();
_injector.Declare<EcsAspect>(); _injector.Declare<EcsAspect>();
_injector.CustomDeclare(new PipelinePropertyInjectionNode());
_basicLayer = EcsConsts.BASIC_LAYER; _basicLayer = EcsConsts.BASIC_LAYER;
Layers = new LayerList(this, _basicLayer); Layers = new LayerList(this, _basicLayer);
@ -562,4 +559,14 @@ namespace DCFApixels.DragonECS
} }
} }
#endregion #endregion
}
namespace DCFApixels.DragonECS.Internal
{
internal sealed class PipelinePropertyInjectionNode : CustomInjectionNode<IEcsPipelineMember, EcsPipeline>
{
public sealed override void InjectTo(IEcsPipelineMember system, EcsPipeline obj)
{
system.Pipeline = obj;
}
}
} }

View File

@ -0,0 +1,36 @@
using System;
namespace DCFApixels.DragonECS
{
public abstract class CustomInjectionNodeBase : InjectionNodeBase
{
protected CustomInjectionNodeBase(Type type) : base(type) { }
}
public abstract class CustomInjectionNode<TProcess, TType> : CustomInjectionNodeBase
where TProcess : IEcsProcess
{
private EcsProcess<TProcess> _processProperty;
private EcsProcess<IEcsInject<TType>> _process;
public CustomInjectionNode() : base(typeof(TType)) { }
public sealed override void Init(EcsPipeline pipeline)
{
_processProperty = pipeline.GetProcess<TProcess>();
_process = pipeline.GetProcess<IEcsInject<TType>>();
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);
}
}

View File

@ -16,15 +16,15 @@ namespace DCFApixels.DragonECS
public abstract void Inject(object obj); public abstract void Inject(object obj);
public abstract void Init(EcsPipeline pipeline); public abstract void Init(EcsPipeline pipeline);
} }
public class InjectionNode<T> : InjectionNodeBase public sealed class InjectionNode<T> : InjectionNodeBase
{ {
private EcsProcess<IEcsInject<T>> _process; private EcsProcess<IEcsInject<T>> _process;
public InjectionNode(Type type) : base(type) { } public InjectionNode(Type type) : base(type) { }
public override void Init(EcsPipeline pipeline) public sealed override void Init(EcsPipeline pipeline)
{ {
_process = pipeline.GetProcess<IEcsInject<T>>(); _process = pipeline.GetProcess<IEcsInject<T>>();
} }
public override void Inject(object obj) public sealed override void Inject(object obj)
{ {
for (int i = 0; i < _process.Length; i++) for (int i = 0; i < _process.Length; i++)
{ {

View File

@ -1,4 +1,5 @@
using System; using DCFApixels.DragonECS.Internal;
using System;
using System.Collections.Generic; using System.Collections.Generic;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
@ -111,6 +112,23 @@ namespace DCFApixels.DragonECS
} }
return true; 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 public class Builder
{ {
private EcsPipeline.Builder _source; private EcsPipeline.Builder _source;
@ -126,6 +144,11 @@ namespace DCFApixels.DragonECS
_instance.TryDeclare<T>(); _instance.TryDeclare<T>();
return _source; return _source;
} }
public EcsPipeline.Builder CustomDeclare(CustomInjectionNodeBase injectionProperty)
{
_instance.TryDeclareProperty(injectionProperty);
return _source;
}
public EcsPipeline.Builder Inject<T>(T obj) public EcsPipeline.Builder Inject<T>(T obj)
{ {
_initInjections.Add(new InitInject<T>(obj)); _initInjections.Add(new InitInject<T>(obj));