From c332289a0a16b07c6b915d08d66c11931f7dc0b7 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:53:23 +0800 Subject: [PATCH] update injector --- src/Injections/Graph/InjectionBranch.cs | 10 +++++++ src/Injections/Injector.cs | 35 ++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/Injections/Graph/InjectionBranch.cs b/src/Injections/Graph/InjectionBranch.cs index 458c995..1ea6f74 100644 --- a/src/Injections/Graph/InjectionBranch.cs +++ b/src/Injections/Graph/InjectionBranch.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS.Internal { @@ -9,10 +10,18 @@ namespace DCFApixels.DragonECS.Internal private InjectionNodeBase[] _nodes = new InjectionNodeBase[2]; private int _nodesCount = 0; + private object _currentInjectedDependency; + public Type Type { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return _type; } } + public object CurrentInjectedDependency + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get { return _currentInjectedDependency; } + } public InjectionBranch(Injector source, Type type) { _source = source; @@ -20,6 +29,7 @@ namespace DCFApixels.DragonECS.Internal } public void Inject(object obj) { + _currentInjectedDependency = obj; for (int i = 0; i < _nodesCount; i++) { _nodes[i].Inject(obj); diff --git a/src/Injections/Injector.cs b/src/Injections/Injector.cs index ecc0c7d..48c1ae5 100644 --- a/src/Injections/Injector.cs +++ b/src/Injections/Injector.cs @@ -1,6 +1,7 @@ using DCFApixels.DragonECS.Internal; using System; using System.Collections.Generic; +using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { @@ -20,18 +21,28 @@ namespace DCFApixels.DragonECS { void OnInitInjectionComplete(); } - public class Injector + + public interface IInjector + { + void Inject(T obj); + T Extract(); + } + public class Injector : IInjector { private EcsPipeline _pipeline; private Dictionary _branches = new Dictionary(32); private Dictionary _nodes = new Dictionary(32); private bool _isInit = false; - public EcsPipeline Pipelie { get { return _pipeline; } } + public EcsPipeline Pipelie + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get { return _pipeline; } + } private Injector() { } - #region Inject/AddNode + #region Inject/Extract/AddNode public void Inject(T obj) { object raw = obj; @@ -68,6 +79,24 @@ namespace DCFApixels.DragonECS } branch.Inject(raw); } + public T Extract() + { + return (T)Extract_Internal(typeof(T)); + } + private object Extract_Internal(Type type) + { + if (_branches.TryGetValue(type, out InjectionBranch branch)) + { + return branch.CurrentInjectedDependency; + } + return null; + + //if (_nodes.ContainsKey(type)) + //{ + // return null; + //} + //throw new EcsInjectionException($"The injection graph is missing a node for {type.Name} type. To create a node, use the AddNode<{type.Name}>() method directly in the injector or in the implementation of the IInjectionUnit for {type.Name}."); + } public void AddNode() { Type type = typeof(T);