mirror of
https://github.com/DCFApixels/DragonECS-AutoInjections.git
synced 2025-09-18 05:04:35 +08:00
refactoring
This commit is contained in:
parent
12df87fe0e
commit
fae94c020a
@ -5,18 +5,15 @@ using System.Reflection;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
internal static class DummyInstance<T>
|
||||
{
|
||||
public static T intsance = (T)Activator.CreateInstance(typeof(T));
|
||||
}
|
||||
internal class AutoInjectionMap
|
||||
{
|
||||
private readonly EcsPipeline _source;
|
||||
private Dictionary<Type, List<InjectedPropertyRecord>> _systemProoperties;
|
||||
private HashSet<Type> _notInjected;
|
||||
private Type _dummyInstance = typeof(DummyInstance<>);
|
||||
private bool _isDummyInjected = false;
|
||||
|
||||
private bool _isPreInitInjectionComplete = false;
|
||||
|
||||
public AutoInjectionMap(EcsPipeline source)
|
||||
{
|
||||
_source = source;
|
||||
@ -84,7 +81,9 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
public void Inject(Type fieldType, object obj)
|
||||
{
|
||||
if (!_isPreInitInjectionComplete)
|
||||
_notInjected.Remove(fieldType);
|
||||
|
||||
Type baseType = fieldType.BaseType;
|
||||
if (baseType != null)
|
||||
Inject(baseType, obj);
|
||||
@ -114,9 +113,7 @@ namespace DCFApixels.DragonECS
|
||||
EcsDebug.Print(EcsConsts.DEBUG_ERROR_TAG, $"The {systemRecord.Attribute.notNullDummyType} dummy cannot be assigned to the {systemRecord.property.PropertyType.Name} field");
|
||||
continue;
|
||||
}
|
||||
|
||||
systemRecord.property.Inject(systemRecord.target,
|
||||
_dummyInstance.MakeGenericType(systemRecord.Attribute.notNullDummyType).GetField("intsance", BindingFlags.Static | BindingFlags.Public).GetValue(null));
|
||||
systemRecord.property.Inject(systemRecord.target, DummyInstance.GetInstance(systemRecord.Attribute.notNullDummyType));
|
||||
}
|
||||
}
|
||||
WarningMissedInjections();
|
||||
@ -135,6 +132,11 @@ namespace DCFApixels.DragonECS
|
||||
#endif
|
||||
}
|
||||
|
||||
public void OnPreInitInjectionComplete()
|
||||
{
|
||||
_isPreInitInjectionComplete = true;
|
||||
}
|
||||
|
||||
private readonly struct InjectedPropertyRecord
|
||||
{
|
||||
public readonly IEcsProcess target;
|
||||
@ -149,58 +151,29 @@ namespace DCFApixels.DragonECS
|
||||
}
|
||||
|
||||
[DebugHide, DebugColor(DebugColor.Gray)]
|
||||
public class AutoInjectSystem : IEcsPreInitProcess, IEcsPreInject, IEcsPreInitInjectProcess
|
||||
public class AutoInjectSystem : IEcsPreInject, IEcsInject<EcsPipeline>, IEcsPreInitInjectProcess
|
||||
{
|
||||
private EcsPipeline _pipeline;
|
||||
private List<object> _injectQueue = new List<object>();
|
||||
private List<object> _delayedInjects = new List<object>();
|
||||
private AutoInjectionMap _autoInjectionMap;
|
||||
private bool _isPreInjectionComplete = false;
|
||||
public void Inject(EcsPipeline obj) => _pipeline = obj;
|
||||
public void PreInject(object obj)
|
||||
{
|
||||
if (_pipeline == null)
|
||||
{
|
||||
_injectQueue.Add(obj);
|
||||
return;
|
||||
_delayedInjects.Add(obj);
|
||||
}
|
||||
AutoInject(obj);
|
||||
}
|
||||
public void PreInit(EcsPipeline pipeline)
|
||||
{
|
||||
_pipeline = pipeline;
|
||||
_autoInjectionMap = new AutoInjectionMap(_pipeline);
|
||||
|
||||
foreach (var obj in _injectQueue)
|
||||
{
|
||||
AutoInject(obj);
|
||||
}
|
||||
_injectQueue.Clear();
|
||||
_injectQueue = null;
|
||||
if (_isPreInjectionComplete)
|
||||
{
|
||||
_autoInjectionMap.InjectDummy();
|
||||
}
|
||||
}
|
||||
|
||||
private void AutoInject(object obj)
|
||||
{
|
||||
_autoInjectionMap.Inject(obj.GetType(), obj);
|
||||
}
|
||||
|
||||
public void OnPreInitInjectionBefore() { }
|
||||
public void OnPreInitInjectionAfter()
|
||||
{
|
||||
_isPreInjectionComplete = true;
|
||||
if (_autoInjectionMap != null)
|
||||
{
|
||||
_autoInjectionMap.InjectDummy();
|
||||
}
|
||||
ClearUsless();
|
||||
}
|
||||
_autoInjectionMap = new AutoInjectionMap(_pipeline);
|
||||
|
||||
private void ClearUsless()
|
||||
{
|
||||
_autoInjectionMap = null;
|
||||
GC.Collect(0); //Ñîáðàòü âñå õëàìîâûå ìèíè êëàññû ñîçäàííå âðåìåííî
|
||||
foreach (var obj in _delayedInjects)
|
||||
_autoInjectionMap.Inject(obj.GetType(), obj);
|
||||
_autoInjectionMap.InjectDummy();
|
||||
_autoInjectionMap.OnPreInitInjectionComplete();
|
||||
|
||||
_delayedInjects.Clear();
|
||||
_delayedInjects = null;
|
||||
GC.Collect(0);
|
||||
}
|
||||
}
|
||||
|
||||
|
24
src/Utils/DummyInstance.cs
Normal file
24
src/Utils/DummyInstance.cs
Normal file
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace DCFApixels.DragonECS
|
||||
{
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
#endif
|
||||
internal static class DummyInstance<T>
|
||||
{
|
||||
#if UNITY_2020_3_OR_NEWER
|
||||
[UnityEngine.Scripting.Preserve]
|
||||
#endif
|
||||
public static T intsance = (T)Activator.CreateInstance(typeof(T));
|
||||
}
|
||||
internal static class DummyInstance
|
||||
{
|
||||
private static Type _dummyInstance = typeof(DummyInstance<>);
|
||||
public static object GetInstance(Type type)
|
||||
{
|
||||
return _dummyInstance.MakeGenericType(type).GetField("intsance", BindingFlags.Static | BindingFlags.Public).GetValue(null);
|
||||
}
|
||||
}
|
||||
}
|
11
src/Utils/DummyInstance.cs.meta
Normal file
11
src/Utils/DummyInstance.cs.meta
Normal file
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6f09b27bf655bd04fa96bf5905d12a45
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue
Block a user