refactoring

This commit is contained in:
Mikhail 2023-06-16 16:13:44 +08:00
parent 12df87fe0e
commit fae94c020a
5 changed files with 59 additions and 51 deletions

View File

@ -5,18 +5,15 @@ using System.Reflection;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
internal static class DummyInstance<T>
{
public static T intsance = (T)Activator.CreateInstance(typeof(T));
}
internal class AutoInjectionMap internal class AutoInjectionMap
{ {
private readonly EcsPipeline _source; private readonly EcsPipeline _source;
private Dictionary<Type, List<InjectedPropertyRecord>> _systemProoperties; private Dictionary<Type, List<InjectedPropertyRecord>> _systemProoperties;
private HashSet<Type> _notInjected; private HashSet<Type> _notInjected;
private Type _dummyInstance = typeof(DummyInstance<>);
private bool _isDummyInjected = false; private bool _isDummyInjected = false;
private bool _isPreInitInjectionComplete = false;
public AutoInjectionMap(EcsPipeline source) public AutoInjectionMap(EcsPipeline source)
{ {
_source = source; _source = source;
@ -84,7 +81,9 @@ namespace DCFApixels.DragonECS
} }
public void Inject(Type fieldType, object obj) public void Inject(Type fieldType, object obj)
{ {
_notInjected.Remove(fieldType); if (!_isPreInitInjectionComplete)
_notInjected.Remove(fieldType);
Type baseType = fieldType.BaseType; Type baseType = fieldType.BaseType;
if (baseType != null) if (baseType != null)
Inject(baseType, obj); 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"); EcsDebug.Print(EcsConsts.DEBUG_ERROR_TAG, $"The {systemRecord.Attribute.notNullDummyType} dummy cannot be assigned to the {systemRecord.property.PropertyType.Name} field");
continue; continue;
} }
systemRecord.property.Inject(systemRecord.target, DummyInstance.GetInstance(systemRecord.Attribute.notNullDummyType));
systemRecord.property.Inject(systemRecord.target,
_dummyInstance.MakeGenericType(systemRecord.Attribute.notNullDummyType).GetField("intsance", BindingFlags.Static | BindingFlags.Public).GetValue(null));
} }
} }
WarningMissedInjections(); WarningMissedInjections();
@ -135,6 +132,11 @@ namespace DCFApixels.DragonECS
#endif #endif
} }
public void OnPreInitInjectionComplete()
{
_isPreInitInjectionComplete = true;
}
private readonly struct InjectedPropertyRecord private readonly struct InjectedPropertyRecord
{ {
public readonly IEcsProcess target; public readonly IEcsProcess target;
@ -149,58 +151,29 @@ namespace DCFApixels.DragonECS
} }
[DebugHide, DebugColor(DebugColor.Gray)] [DebugHide, DebugColor(DebugColor.Gray)]
public class AutoInjectSystem : IEcsPreInitProcess, IEcsPreInject, IEcsPreInitInjectProcess public class AutoInjectSystem : IEcsPreInject, IEcsInject<EcsPipeline>, IEcsPreInitInjectProcess
{ {
private EcsPipeline _pipeline; private EcsPipeline _pipeline;
private List<object> _injectQueue = new List<object>(); private List<object> _delayedInjects = new List<object>();
private AutoInjectionMap _autoInjectionMap; private AutoInjectionMap _autoInjectionMap;
private bool _isPreInjectionComplete = false; public void Inject(EcsPipeline obj) => _pipeline = obj;
public void PreInject(object obj) public void PreInject(object obj)
{ {
if (_pipeline == null) _delayedInjects.Add(obj);
{
_injectQueue.Add(obj);
return;
}
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 OnPreInitInjectionBefore() { }
public void OnPreInitInjectionAfter() public void OnPreInitInjectionAfter()
{ {
_isPreInjectionComplete = true; _autoInjectionMap = new AutoInjectionMap(_pipeline);
if (_autoInjectionMap != null)
{
_autoInjectionMap.InjectDummy();
}
ClearUsless();
}
private void ClearUsless() foreach (var obj in _delayedInjects)
{ _autoInjectionMap.Inject(obj.GetType(), obj);
_autoInjectionMap = null; _autoInjectionMap.InjectDummy();
GC.Collect(0); //Ñîáðàòü âñå õëàìîâûå ìèíè êëàññû ñîçäàííå âðåìåííî _autoInjectionMap.OnPreInitInjectionComplete();
_delayedInjects.Clear();
_delayedInjects = null;
GC.Collect(0);
} }
} }

View 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);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6f09b27bf655bd04fa96bf5905d12a45
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: