mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-11-13 00:55:55 +08:00
update injector
This commit is contained in:
parent
61e324097b
commit
c332289a0a
@ -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);
|
||||||
|
|||||||
@ -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);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user