From 32444868e1446ac73c055975ac69538952b182d6 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 28 Apr 2024 18:36:14 +0800 Subject: [PATCH] update builtin DI --- src/Injections/Graph/InjectionBranch.cs | 20 +++------ src/Injections/Graph/InjectionNode.cs | 5 ++- src/Injections/Injector.cs | 43 ++++++++++++++++--- src/Injections/Utils/IInjectionBlock.cs | 29 ------------- src/Injections/Utils/Interfaces.cs | 23 ++++++++++ ...ectionBlock.cs.meta => Interfaces.cs.meta} | 0 src/Utils/Exceptions.cs | 7 +++ 7 files changed, 75 insertions(+), 52 deletions(-) delete mode 100644 src/Injections/Utils/IInjectionBlock.cs create mode 100644 src/Injections/Utils/Interfaces.cs rename src/Injections/Utils/{IInjectionBlock.cs.meta => Interfaces.cs.meta} (100%) diff --git a/src/Injections/Graph/InjectionBranch.cs b/src/Injections/Graph/InjectionBranch.cs index 79f7146..458c995 100644 --- a/src/Injections/Graph/InjectionBranch.cs +++ b/src/Injections/Graph/InjectionBranch.cs @@ -1,42 +1,32 @@ using System; -namespace DCFApixels.DragonECS +namespace DCFApixels.DragonECS.Internal { - public class InjectionBranch + internal class InjectionBranch { private readonly Injector _source; private readonly 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(Injector source, Type type, bool isDeclared) + public InjectionBranch(Injector source, Type type) { _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) + if (obj is IInjectionBlock block) { - container.InjectTo(new BlockInjector(_source)); + block.InjectTo(_source); } } public void AddNode(InjectionNodeBase node) diff --git a/src/Injections/Graph/InjectionNode.cs b/src/Injections/Graph/InjectionNode.cs index c287304..fee555d 100644 --- a/src/Injections/Graph/InjectionNode.cs +++ b/src/Injections/Graph/InjectionNode.cs @@ -16,7 +16,10 @@ namespace DCFApixels.DragonECS public abstract void Inject(object obj); public abstract void Init(EcsPipeline pipeline); } - public sealed class InjectionNode : InjectionNodeBase +} +namespace DCFApixels.DragonECS.Internal +{ + internal sealed class InjectionNode : InjectionNodeBase { private EcsProcess> _process; public InjectionNode(Type type) : base(type) { } diff --git a/src/Injections/Injector.cs b/src/Injections/Injector.cs index 117e910..c8b609a 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 @@ -22,8 +23,11 @@ namespace DCFApixels.DragonECS private Dictionary _nodes = new Dictionary(32); private bool _isInit = false; + public EcsPipeline Pipelie { get { return _pipeline; } } + private Injector() { } + #region Inject/AddNode public void Inject(T obj) { object raw = obj; @@ -32,18 +36,43 @@ namespace DCFApixels.DragonECS { if (typeof(T) == type) { - InitNode(new InjectionNode(type)); - branch = new InjectionBranch(this, type, true); + if (_nodes.ContainsKey(type) == false) + { + InitNode(new InjectionNode(type)); + } + branch = new InjectionBranch(this, type); InitBranch(branch); } else { - branch = new InjectionBranch(this, type, false); - InitBranch(branch); + bool hasNode = _nodes.ContainsKey(type); + if (hasNode == false && obj is IInjectionUnit unit) + { + unit.OnInitInjectionBranch(new InjectionBranchIniter(this)); + hasNode = _nodes.ContainsKey(type); + } + if (hasNode) + { + branch = new InjectionBranch(this, type); + InitBranch(branch); + } + else + { + throw new EcsInjectionException($"To create an injection branch, no injection node of {type.Name} was found. To create a node, use the AddNode<{type.Name}>() method directly in the injector or in the implementation of the IInjectionUnit for {type.Name}."); + } } } branch.Inject(raw); } + public void AddNode() + { + Type type = typeof(T); + if (_nodes.ContainsKey(type) == false) + { + InitNode(new InjectionNode(type)); + } + } + #endregion #region Internal private void InitBranch(InjectionBranch branch) @@ -68,7 +97,7 @@ namespace DCFApixels.DragonECS _nodes.Add(node.Type, node); foreach (var item in _branches) { - var type = item.Key; + //var type = item.Key; var branch = item.Value; if (node.Type.IsAssignableFrom(branch.Type)) { @@ -108,7 +137,7 @@ namespace DCFApixels.DragonECS if (IsCanInstantiated(type)) #endif { - InitBranch(new InjectionBranch(this, type, true)); + InitBranch(new InjectionBranch(this, type)); } return true; } diff --git a/src/Injections/Utils/IInjectionBlock.cs b/src/Injections/Utils/IInjectionBlock.cs deleted file mode 100644 index 0293c28..0000000 --- a/src/Injections/Utils/IInjectionBlock.cs +++ /dev/null @@ -1,29 +0,0 @@ -namespace DCFApixels.DragonECS -{ - public readonly struct BlockInjector - { - private readonly Injector _injectionGraph; - public BlockInjector(Injector injectionGraph) - { - _injectionGraph = injectionGraph; - } - public void Inject(T obj) - { - _injectionGraph.Inject(obj); - } - // public void InjectNoBoxing(T data) where T : struct - // { - // _injectionGraph.InjectNoBoxing(data); - // } - //#if !REFLECTION_DISABLED - // public void InjectRaw(object obj) - // { - // _injectionGraph.InjectRaw(obj); - // } - //#endif - } - public interface IInjectionBlock - { - void InjectTo(BlockInjector i); - } -} diff --git a/src/Injections/Utils/Interfaces.cs b/src/Injections/Utils/Interfaces.cs new file mode 100644 index 0000000..51791f6 --- /dev/null +++ b/src/Injections/Utils/Interfaces.cs @@ -0,0 +1,23 @@ +namespace DCFApixels.DragonECS +{ + public interface IInjectionBlock + { + void InjectTo(Injector inj); + } + public readonly struct InjectionBranchIniter + { + private readonly Injector _injector; + public InjectionBranchIniter(Injector injector) + { + _injector = injector; + } + public void AddNode() + { + _injector.AddNode(); + } + } + public interface IInjectionUnit + { + void OnInitInjectionBranch(InjectionBranchIniter initer); + } +} diff --git a/src/Injections/Utils/IInjectionBlock.cs.meta b/src/Injections/Utils/Interfaces.cs.meta similarity index 100% rename from src/Injections/Utils/IInjectionBlock.cs.meta rename to src/Injections/Utils/Interfaces.cs.meta diff --git a/src/Utils/Exceptions.cs b/src/Utils/Exceptions.cs index dd533bf..15c1d04 100644 --- a/src/Utils/Exceptions.cs +++ b/src/Utils/Exceptions.cs @@ -24,6 +24,13 @@ namespace DCFApixels.DragonECS public EcsRunnerImplementationException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } public EcsRunnerImplementationException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } } + [Serializable] + public class EcsInjectionException : Exception + { + public EcsInjectionException() { } + public EcsInjectionException(string message) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message) { } + public EcsInjectionException(string message, Exception inner) : base(EcsConsts.EXCEPTION_MESSAGE_PREFIX + message, inner) { } + } } namespace DCFApixels.DragonECS.Internal