update injector

This commit is contained in:
Mikhail 2024-08-20 11:53:23 +08:00
parent 61e324097b
commit c332289a0a
2 changed files with 42 additions and 3 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS.Internal namespace DCFApixels.DragonECS.Internal
{ {
@ -9,10 +10,18 @@ namespace DCFApixels.DragonECS.Internal
private InjectionNodeBase[] _nodes = new InjectionNodeBase[2]; private InjectionNodeBase[] _nodes = new InjectionNodeBase[2];
private int _nodesCount = 0; private int _nodesCount = 0;
private object _currentInjectedDependency;
public Type Type public Type Type
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _type; } get { return _type; }
} }
public object CurrentInjectedDependency
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _currentInjectedDependency; }
}
public InjectionBranch(Injector source, Type type) public InjectionBranch(Injector source, Type type)
{ {
_source = source; _source = source;
@ -20,6 +29,7 @@ namespace DCFApixels.DragonECS.Internal
} }
public void Inject(object obj) public void Inject(object obj)
{ {
_currentInjectedDependency = obj;
for (int i = 0; i < _nodesCount; i++) for (int i = 0; i < _nodesCount; i++)
{ {
_nodes[i].Inject(obj); _nodes[i].Inject(obj);

View File

@ -1,6 +1,7 @@
using DCFApixels.DragonECS.Internal; using DCFApixels.DragonECS.Internal;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
@ -20,18 +21,28 @@ namespace DCFApixels.DragonECS
{ {
void OnInitInjectionComplete(); void OnInitInjectionComplete();
} }
public class Injector
public interface IInjector
{
void Inject<T>(T obj);
T Extract<T>();
}
public class Injector : IInjector
{ {
private EcsPipeline _pipeline; private EcsPipeline _pipeline;
private Dictionary<Type, InjectionBranch> _branches = new Dictionary<Type, InjectionBranch>(32); private Dictionary<Type, InjectionBranch> _branches = new Dictionary<Type, InjectionBranch>(32);
private Dictionary<Type, InjectionNodeBase> _nodes = new Dictionary<Type, InjectionNodeBase>(32); private Dictionary<Type, InjectionNodeBase> _nodes = new Dictionary<Type, InjectionNodeBase>(32);
private bool _isInit = false; private bool _isInit = false;
public EcsPipeline Pipelie { get { return _pipeline; } } public EcsPipeline Pipelie
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _pipeline; }
}
private Injector() { } private Injector() { }
#region Inject/AddNode #region Inject/Extract/AddNode
public void Inject<T>(T obj) public void Inject<T>(T obj)
{ {
object raw = obj; object raw = obj;
@ -68,6 +79,24 @@ namespace DCFApixels.DragonECS
} }
branch.Inject(raw); branch.Inject(raw);
} }
public T Extract<T>()
{
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<T>() public void AddNode<T>()
{ {
Type type = typeof(T); Type type = typeof(T);