mirror of
https://github.com/DCFApixels/DragonECS-AutoInjections.git
synced 2025-09-18 05:04:35 +08:00
update injections
This commit is contained in:
parent
c5b3c64bb8
commit
9bc699d563
@ -11,19 +11,16 @@ namespace DCFApixels.DragonECS
|
|||||||
internal class AutoInjectionMap
|
internal class AutoInjectionMap
|
||||||
{
|
{
|
||||||
private readonly EcsPipeline _source;
|
private readonly EcsPipeline _source;
|
||||||
|
private Dictionary<Type, List<FieldRecord>> _systems;
|
||||||
private Dictionary<Type, List<FiledRecord>> _systems;
|
|
||||||
private HashSet<Type> _notInjected;
|
private HashSet<Type> _notInjected;
|
||||||
|
|
||||||
private Type _dummyInstance = typeof(DummyInstance<>);
|
private Type _dummyInstance = typeof(DummyInstance<>);
|
||||||
|
|
||||||
private bool _isDummyInjected = false;
|
private bool _isDummyInjected = false;
|
||||||
|
|
||||||
public AutoInjectionMap(EcsPipeline source)
|
public AutoInjectionMap(EcsPipeline source)
|
||||||
{
|
{
|
||||||
_source = source;
|
_source = source;
|
||||||
var allsystems = _source.AllSystems;
|
var allsystems = _source.AllSystems;
|
||||||
_systems = new Dictionary<Type, List<FiledRecord>>();
|
_systems = new Dictionary<Type, List<FieldRecord>>();
|
||||||
_notInjected = new HashSet<Type>();
|
_notInjected = new HashSet<Type>();
|
||||||
foreach (var system in allsystems)
|
foreach (var system in allsystems)
|
||||||
{
|
{
|
||||||
@ -34,27 +31,25 @@ namespace DCFApixels.DragonECS
|
|||||||
if (autoInjectAttribute != null)
|
if (autoInjectAttribute != null)
|
||||||
{
|
{
|
||||||
Type fieldType = field.FieldType;
|
Type fieldType = field.FieldType;
|
||||||
List<FiledRecord> list;
|
List<FieldRecord> list;
|
||||||
if (!_systems.TryGetValue(fieldType, out list))
|
if (!_systems.TryGetValue(fieldType, out list))
|
||||||
{
|
{
|
||||||
list = new List<FiledRecord>();
|
list = new List<FieldRecord>();
|
||||||
_systems.Add(fieldType, list);
|
_systems.Add(fieldType, list);
|
||||||
}
|
}
|
||||||
|
|
||||||
list.Add(new FiledRecord(system, field, autoInjectAttribute));
|
list.Add(new FieldRecord(system, field, autoInjectAttribute));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
foreach (var item in _systems.Keys)
|
foreach (var item in _systems.Keys)
|
||||||
{
|
|
||||||
_notInjected.Add(item);
|
_notInjected.Add(item);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
private static List<FieldInfo> GetAllFieldsFor(Type type)
|
private static List<FieldInfo> GetAllFieldsFor(Type type)
|
||||||
{
|
{
|
||||||
List<FieldInfo> result = new List<FieldInfo>();
|
List<FieldInfo> result = new List<FieldInfo>();
|
||||||
Do(type, result);
|
Do(type, result);
|
||||||
void Do(Type type, List<FieldInfo> result)
|
static void Do(Type type, List<FieldInfo> result)
|
||||||
{
|
{
|
||||||
result.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
|
result.AddRange(type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
|
||||||
if (type.BaseType != null)
|
if (type.BaseType != null)
|
||||||
@ -62,16 +57,17 @@ namespace DCFApixels.DragonECS
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
public void Inject(object obj)
|
public void Inject(Type fieldType, object obj)
|
||||||
{
|
{
|
||||||
_notInjected.Remove(obj.GetType());
|
_notInjected.Remove(obj.GetType());
|
||||||
Type objectType = obj.GetType();
|
Type baseType = fieldType.BaseType;
|
||||||
if(_systems.TryGetValue(objectType, out List<FiledRecord> list))
|
if (baseType != null)
|
||||||
|
Inject(baseType, obj);
|
||||||
|
|
||||||
|
if (_systems.TryGetValue(fieldType, out List<FieldRecord> list))
|
||||||
{
|
{
|
||||||
foreach (var item in list)
|
foreach (var item in list)
|
||||||
{
|
|
||||||
item.field.SetValue(item.target, obj);
|
item.field.SetValue(item.target, obj);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +75,6 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
if (_isDummyInjected)
|
if (_isDummyInjected)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_isDummyInjected = true;
|
_isDummyInjected = true;
|
||||||
foreach (var notInjectedItem in _notInjected)
|
foreach (var notInjectedItem in _notInjected)
|
||||||
{
|
{
|
||||||
@ -117,12 +112,12 @@ namespace DCFApixels.DragonECS
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly struct FiledRecord
|
private readonly struct FieldRecord
|
||||||
{
|
{
|
||||||
public readonly IEcsSystem target;
|
public readonly IEcsSystem target;
|
||||||
public readonly FieldInfo field;
|
public readonly FieldInfo field;
|
||||||
public readonly EcsInjectAttribute attribute;
|
public readonly EcsInjectAttribute attribute;
|
||||||
public FiledRecord(IEcsSystem target, FieldInfo field, EcsInjectAttribute attribute)
|
public FieldRecord(IEcsSystem target, FieldInfo field, EcsInjectAttribute attribute)
|
||||||
{
|
{
|
||||||
this.target = target;
|
this.target = target;
|
||||||
this.field = field;
|
this.field = field;
|
||||||
@ -136,11 +131,8 @@ namespace DCFApixels.DragonECS
|
|||||||
{
|
{
|
||||||
private EcsPipeline _pipeline;
|
private EcsPipeline _pipeline;
|
||||||
private List<object> _injectQueue = new List<object>();
|
private List<object> _injectQueue = new List<object>();
|
||||||
|
|
||||||
private AutoInjectionMap _autoInjectionMap;
|
private AutoInjectionMap _autoInjectionMap;
|
||||||
|
|
||||||
private bool _isPreInjectionComplete = false;
|
private bool _isPreInjectionComplete = false;
|
||||||
|
|
||||||
public void PreInject(object obj)
|
public void PreInject(object obj)
|
||||||
{
|
{
|
||||||
if(_pipeline == null)
|
if(_pipeline == null)
|
||||||
@ -169,7 +161,7 @@ namespace DCFApixels.DragonECS
|
|||||||
|
|
||||||
private void AutoInject(object obj)
|
private void AutoInject(object obj)
|
||||||
{
|
{
|
||||||
_autoInjectionMap.Inject(obj);
|
_autoInjectionMap.Inject(obj.GetType(), obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void OnPreInitInjectionBefore() { }
|
public void OnPreInitInjectionBefore() { }
|
||||||
|
Loading…
Reference in New Issue
Block a user