mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 01:44:35 +08:00
separating di from core
This commit is contained in:
parent
830a83c0d0
commit
53259a92db
@ -1,66 +0,0 @@
|
||||
using DCFApixels.DragonECS.DI.Internal;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public static partial class EcsPipelineExtensions
|
||||
{
|
||||
public static void Inject<T>(this EcsPipeline self, T data)
|
||||
{
|
||||
self.GetRunner<IEcsInject<T>>().Inject(data);
|
||||
}
|
||||
}
|
||||
public static partial class EcsPipelineBuilderExtensions
|
||||
{
|
||||
public static EcsPipeline.Builder AddInjectionGraph(this EcsPipeline.Builder self, InjectionGraph graph)
|
||||
{
|
||||
self.Config.Set(InjectionGraph.CONFIG_NAME, graph);
|
||||
return self;
|
||||
}
|
||||
public static EcsPipeline.Builder GetInjectionGraph(this EcsPipeline.Builder self, out InjectionGraph graph)
|
||||
{
|
||||
graph = self.Config.Get<InjectionGraph>(InjectionGraph.CONFIG_NAME);
|
||||
return self;
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T>(this EcsPipeline.Builder self, T data)
|
||||
{
|
||||
if (data == null)
|
||||
{
|
||||
Throw.ArgumentNull();
|
||||
}
|
||||
self.Add(new InitInjectionSystem<T>(data));
|
||||
if (data is IEcsModule module)
|
||||
{
|
||||
self.AddModule(module);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1>(this EcsPipeline.Builder self, T0 d0, T1 d1)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1);
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1, T2>(this EcsPipeline.Builder self, T0 d0, T1 d1, T2 d2)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1).Inject(d2);
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1, T2, T3>(this EcsPipeline.Builder self, T0 d0, T1 d1, T2 d2, T3 d3)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1).Inject(d2).Inject(d3);
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1, T2, T3, T4>(this EcsPipeline.Builder self, T0 d0, T1 d1, T2 d2, T3 d3, T4 d4)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1).Inject(d2).Inject(d3).Inject(d4);
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1, T2, T3, T4, T5>(this EcsPipeline.Builder self, T0 d0, T1 d1, T2 d2, T3 d3, T4 d4, T5 f)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1).Inject(d2).Inject(d3).Inject(d4).Inject(f);
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1, T2, T3, T4, T5, T6>(this EcsPipeline.Builder self, T0 d0, T1 d1, T2 d2, T3 d3, T4 d4, T5 f, T6 d6)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1).Inject(d2).Inject(d3).Inject(d4).Inject(f).Inject(d6);
|
||||
}
|
||||
public static EcsPipeline.Builder Inject<T0, T1, T2, T3, T4, T5, T6, T7>(this EcsPipeline.Builder self, T0 d0, T1 d1, T2 d2, T3 d3, T4 d4, T5 f, T6 d6, T7 d7)
|
||||
{
|
||||
return self.Inject(d0).Inject(d1).Inject(d2).Inject(d3).Inject(d4).Inject(f).Inject(d6).Inject(d7);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace DCFApixels.DragonECS.DI.Internal
|
||||
{
|
||||
internal class Throw
|
||||
{
|
||||
public static void ArgumentNull()
|
||||
{
|
||||
throw new ArgumentNullException();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public readonly struct Injector
|
||||
{
|
||||
private readonly InjectionGraph _injectionGraph;
|
||||
public Injector(InjectionGraph injectionGraph)
|
||||
{
|
||||
_injectionGraph = injectionGraph;
|
||||
}
|
||||
public void Inject<T>(T obj)
|
||||
{
|
||||
_injectionGraph.Inject(obj);
|
||||
}
|
||||
public void InjectNoBoxing<T>(T data)
|
||||
{
|
||||
_injectionGraph.InjectNoBoxing(data);
|
||||
}
|
||||
#if !REFLECTION_DISABLED
|
||||
public void InjectRaw(object obj)
|
||||
{
|
||||
_injectionGraph.InjectRaw(obj);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
public interface IInjectionBlock
|
||||
{
|
||||
void InjectTo(Injector i);
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
namespace DCFApixels.DragonECS.DI.Internal
|
||||
{
|
||||
public abstract class InitInjectSystemBase : IEcsSystem { }
|
||||
|
||||
[MetaTags(MetaTags.HIDDEN)]
|
||||
[MetaColor(MetaColor.Gray)]
|
||||
public class InitInjectionSystem<T> : InitInjectSystemBase, IEcsPipelineMember, IEcsInject<InitInjectController>, IEcsPreInitInjectProcess
|
||||
{
|
||||
private EcsPipeline _pipeline;
|
||||
public EcsPipeline Pipeline
|
||||
{
|
||||
get { return Pipeline; }
|
||||
set
|
||||
{
|
||||
_pipeline = value;
|
||||
|
||||
if (_injectedData == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (_injectController == null)
|
||||
{
|
||||
_pipeline.Config.Get<InjectionGraph>(InjectionGraph.CONFIG_NAME).Init(_pipeline);
|
||||
var injectPipelineRunner = _pipeline.GetRunner<IEcsInject<EcsPipeline>>();
|
||||
injectPipelineRunner.Inject(_pipeline);
|
||||
EcsRunner.Destroy(injectPipelineRunner);
|
||||
|
||||
_injectController = new InitInjectController(_pipeline);
|
||||
var injectMapRunner = _pipeline.GetRunner<IEcsInject<InitInjectController>>();
|
||||
_pipeline.GetRunner<IEcsPreInitInjectProcess>().OnPreInitInjectionBefore(_pipeline);
|
||||
injectMapRunner.Inject(_injectController);
|
||||
EcsRunner.Destroy(injectMapRunner);
|
||||
}
|
||||
var injectRunnerGeneric = _pipeline.GetRunner<IEcsInject<T>>();
|
||||
injectRunnerGeneric.Inject(_injectedData);
|
||||
if (_injectController.OnInject())
|
||||
{
|
||||
_injectController.Destroy();
|
||||
var injectCallbacksRunner = _pipeline.GetRunner<IEcsPreInitInjectProcess>();
|
||||
injectCallbacksRunner.OnPreInitInjectionAfter();
|
||||
EcsRunner.Destroy(injectCallbacksRunner);
|
||||
}
|
||||
_injectedData = default;
|
||||
}
|
||||
}
|
||||
|
||||
private InitInjectController _injectController;
|
||||
void IEcsInject<InitInjectController>.Inject(InitInjectController obj) { _injectController = obj; }
|
||||
|
||||
private T _injectedData;
|
||||
internal InitInjectionSystem(T injectedData)
|
||||
{
|
||||
if (injectedData == null)
|
||||
{
|
||||
Throw.ArgumentNull();
|
||||
}
|
||||
_injectedData = injectedData;
|
||||
}
|
||||
void IEcsPreInitInjectProcess.OnPreInitInjectionBefore(EcsPipeline pipeline) { }
|
||||
void IEcsPreInitInjectProcess.OnPreInitInjectionAfter() { _injectController = null; }
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
using DCFApixels.DragonECS.DI.Internal;
|
||||
using DCFApixels.DragonECS.RunnersCore;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
[MetaName(nameof(Inject))]
|
||||
[BindWithEcsRunner(typeof(EcsInjectRunner<>))]
|
||||
public interface IEcsInject<T> : IEcsSystem
|
||||
{
|
||||
void Inject(T obj);
|
||||
}
|
||||
[MetaName("PreInitInject")]
|
||||
[BindWithEcsRunner(typeof(EcsInitInjectProcessRunner))]
|
||||
public interface IEcsPreInitInjectProcess : IEcsSystem
|
||||
{
|
||||
void OnPreInitInjectionBefore(EcsPipeline pipeline);
|
||||
void OnPreInitInjectionAfter();
|
||||
}
|
||||
}
|
||||
namespace DCFApixels.DragonECS.DI.Internal
|
||||
{
|
||||
internal class InitInjectController
|
||||
{
|
||||
private EcsPipeline _source;
|
||||
private EcsProcess<InitInjectSystemBase> _injectSystems;
|
||||
private int _injectCount;
|
||||
public bool IsInjectionEnd
|
||||
{
|
||||
get { return _injectCount >= _injectSystems.Length; }
|
||||
}
|
||||
public InitInjectController(EcsPipeline source)
|
||||
{
|
||||
_injectCount = 0;
|
||||
_source = source;
|
||||
_injectSystems = _source.GetProcess<InitInjectSystemBase>();
|
||||
}
|
||||
public bool OnInject()
|
||||
{
|
||||
_injectCount++;
|
||||
return IsInjectionEnd;
|
||||
}
|
||||
public void Destroy()
|
||||
{
|
||||
_source = null;
|
||||
_injectSystems = EcsProcess<InitInjectSystemBase>.Empty;
|
||||
}
|
||||
}
|
||||
[MetaTags(MetaTags.HIDDEN)]
|
||||
[MetaColor(MetaColor.Gray)]
|
||||
public sealed class EcsInjectRunner<T> : EcsRunner<IEcsInject<T>>, IEcsInject<T>
|
||||
{
|
||||
private InjectionGraph _injectionGraph;
|
||||
void IEcsInject<T>.Inject(T obj)
|
||||
{
|
||||
_injectionGraph.Inject(obj);
|
||||
}
|
||||
protected override void OnSetup()
|
||||
{
|
||||
_injectionGraph = Pipeline.Config.Get<InjectionGraph>(InjectionGraph.CONFIG_NAME);
|
||||
}
|
||||
}
|
||||
[MetaTags(MetaTags.HIDDEN)]
|
||||
[MetaColor(MetaColor.Gray)]
|
||||
public sealed class EcsInitInjectProcessRunner : EcsRunner<IEcsPreInitInjectProcess>, IEcsPreInitInjectProcess
|
||||
{
|
||||
public void OnPreInitInjectionAfter()
|
||||
{
|
||||
foreach (var item in Process) item.OnPreInitInjectionAfter();
|
||||
}
|
||||
public void OnPreInitInjectionBefore(EcsPipeline pipeline)
|
||||
{
|
||||
foreach (var item in Process) item.OnPreInitInjectionBefore(pipeline);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 0404e70f4ccc54a41b3da9268ff287ad
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -1,65 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public class InjectionBranch
|
||||
{
|
||||
private InjectionGraph _source;
|
||||
private Type _type;
|
||||
private InjectionNodeBase[] _nodes = new InjectionNodeBase[2];
|
||||
private int _nodesCount = 0;
|
||||
private bool _isDeclared = false;
|
||||
|
||||
public Type Type
|
||||
{
|
||||
get { return _type; }
|
||||
}
|
||||
public bool IsDeclared
|
||||
{
|
||||
get { return _isDeclared; }
|
||||
}
|
||||
public InjectionBranch(InjectionGraph source, Type type, bool isDeclared)
|
||||
{
|
||||
_source = source;
|
||||
_isDeclared = isDeclared;
|
||||
_type = type;
|
||||
}
|
||||
public void SetDeclaredTrue()
|
||||
{
|
||||
_isDeclared = true;
|
||||
}
|
||||
public void Inject(object obj)
|
||||
{
|
||||
for (int i = 0; i < _nodesCount; i++)
|
||||
{
|
||||
_nodes[i].Inject(obj);
|
||||
}
|
||||
if (obj is IInjectionBlock container)
|
||||
{
|
||||
container.InjectTo(new Injector(_source));
|
||||
}
|
||||
}
|
||||
public void AddNode(InjectionNodeBase node)
|
||||
{
|
||||
if (_nodesCount >= _nodes.Length)
|
||||
{
|
||||
Array.Resize(ref _nodes, (_nodes.Length << 1) + 1);
|
||||
}
|
||||
_nodes[_nodesCount++] = node;
|
||||
}
|
||||
public void Trim()
|
||||
{
|
||||
if (_nodesCount <= 0)
|
||||
{
|
||||
_nodes = Array.Empty<InjectionNodeBase>();
|
||||
return;
|
||||
}
|
||||
|
||||
InjectionNodeBase[] newNodes = new InjectionNodeBase[_nodesCount];
|
||||
for (int i = 0; i < newNodes.Length; i++)
|
||||
{
|
||||
newNodes[i] = _nodes[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public class InjectionGraph
|
||||
{
|
||||
internal const string CONFIG_NAME = "DCFApixels.DragonECS.DI:" + nameof(InjectionGraph);
|
||||
|
||||
private EcsPipeline _pipeline;
|
||||
private Dictionary<Type, InjectionBranch> _branches = new Dictionary<Type, InjectionBranch>(32);
|
||||
private Dictionary<Type, InjectionNodeBase> _nodes = new Dictionary<Type, InjectionNodeBase>(32);
|
||||
private bool _isInit = false;
|
||||
|
||||
public InjectionGraph()
|
||||
{
|
||||
Declare<object>();
|
||||
Declare<EcsWorld>();
|
||||
}
|
||||
public void Init(EcsPipeline pipeline)
|
||||
{
|
||||
if (_isInit)
|
||||
{
|
||||
throw new Exception("Already initialized");
|
||||
}
|
||||
_pipeline = pipeline;
|
||||
foreach (var node in _nodes.Values)
|
||||
{
|
||||
node.Init(pipeline);
|
||||
}
|
||||
_isInit = true;
|
||||
}
|
||||
public bool TryDeclare<T>()
|
||||
{
|
||||
Type type = typeof(T);
|
||||
if (_nodes.ContainsKey(type))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
InitNode(new InjectionNode<T>(type));
|
||||
if (IsCanInstantiated(type))
|
||||
{
|
||||
InitBranch(new InjectionBranch(this, type, true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public void Declare<T>()
|
||||
{
|
||||
if (TryDeclare<T>() == false)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
}
|
||||
public void InjectNoBoxing<T>(T data)
|
||||
{
|
||||
_pipeline.GetRunner<IEcsInject<T>>().Inject(data);
|
||||
}
|
||||
public void Inject<T>(T obj)
|
||||
{
|
||||
Type type = typeof(T);
|
||||
#if DEBUG
|
||||
if (obj.GetType() != type)
|
||||
{
|
||||
throw new ArgumentException();
|
||||
}
|
||||
if (IsCanInstantiated(type) == false)
|
||||
{
|
||||
throw new Exception();
|
||||
}
|
||||
#endif
|
||||
if (_branches.TryGetValue(type, out InjectionBranch branch) == false)
|
||||
{
|
||||
InitNode(new InjectionNode<T>(type));
|
||||
branch = new InjectionBranch(this, type, true);
|
||||
InitBranch(branch);
|
||||
}
|
||||
branch.Inject(obj);
|
||||
}
|
||||
|
||||
#if !REFLECTION_DISABLED
|
||||
public void InjectRaw(object obj)
|
||||
{
|
||||
Type type = obj.GetType();
|
||||
if (_branches.TryGetValue(type, out InjectionBranch branch) == false)
|
||||
{
|
||||
branch = new InjectionBranch(this, type, false);
|
||||
InitBranch(branch);
|
||||
}
|
||||
branch.Inject(obj);
|
||||
}
|
||||
#endif
|
||||
|
||||
private void InitBranch(InjectionBranch branch)
|
||||
{
|
||||
_branches.Add(branch.Type, branch);
|
||||
foreach (var (type, node) in _nodes)
|
||||
{
|
||||
if (branch.Type.IsAssignableTo(type))
|
||||
{
|
||||
branch.AddNode(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
private void InitNode(InjectionNodeBase node)
|
||||
{
|
||||
if (_pipeline != null)
|
||||
{
|
||||
node.Init(_pipeline);
|
||||
}
|
||||
_nodes.Add(node.Type, node);
|
||||
foreach (var (type, branch) in _branches)
|
||||
{
|
||||
if (branch.Type.IsAssignableTo(type))
|
||||
{
|
||||
branch.AddNode(node);
|
||||
}
|
||||
}
|
||||
}
|
||||
private bool IsCanInstantiated(Type type)
|
||||
{
|
||||
return !type.IsAbstract && !type.IsInterface;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public abstract class InjectionNodeBase
|
||||
{
|
||||
private readonly Type _type;
|
||||
public Type Type
|
||||
{
|
||||
get { return _type; }
|
||||
}
|
||||
protected InjectionNodeBase(Type type)
|
||||
{
|
||||
_type = type;
|
||||
}
|
||||
public abstract void Inject(object obj);
|
||||
public abstract void Init(EcsPipeline pipeline);
|
||||
}
|
||||
public class InjectionNode<T> : InjectionNodeBase
|
||||
{
|
||||
private EcsProcess<IEcsInject<T>> _process;
|
||||
public InjectionNode(Type type) : base(type) { }
|
||||
public override void Init(EcsPipeline pipeline)
|
||||
{
|
||||
_process = pipeline.GetProcess<IEcsInject<T>>();
|
||||
}
|
||||
public override void Inject(object obj)
|
||||
{
|
||||
for (int i = 0; i < _process.Length; i++)
|
||||
{
|
||||
_process[i].Inject((T)obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
public sealed class EcsDefaultWorld : EcsWorld
|
||||
{
|
||||
public EcsDefaultWorld(IEcsWorldConfig config = null, short worldID = -1) : base(config, worldID) { }
|
||||
}
|
||||
public sealed class EcsEventWorld : EcsWorld
|
||||
{
|
||||
public EcsEventWorld(IEcsWorldConfig config = null, short worldID = -1) : base(config, worldID) { }
|
||||
}
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 76bb6fc0896102347bbd36ed235e45f7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user