rename EcsSystems to EcsPipeline

This commit is contained in:
Mikhail 2023-03-30 05:32:43 +08:00
parent abbe33e3b5
commit f23ed953d7
5 changed files with 110 additions and 55 deletions

View File

@ -7,11 +7,11 @@ namespace DCFApixels.DragonECS
{
internal class PreInitInjectController
{
private EcsSystems _source;
private EcsPipeline _source;
private InjectSystemBase[] _injectSystems;
private int _injectCount;
public PreInitInjectController(EcsSystems source)
public PreInitInjectController(EcsPipeline source)
{
_injectCount = 0;
_source = source;
@ -102,25 +102,26 @@ namespace DCFApixels.DragonECS
_injectedData = injectedData;
}
public void PreInit(EcsSystems systems)
public void PreInit(EcsPipeline pipeline)
{
if (_injectController == null)
{
_injectController = new PreInitInjectController(systems);
var injectMapRunner = systems.GetRunner<IEcsInject<PreInitInjectController>>();
systems.GetRunner<IEcsPreInitInjectCallbacks>().OnPreInitInjectionBefore();
_injectController = new PreInitInjectController(pipeline);
var injectMapRunner = pipeline.GetRunner<IEcsInject<PreInitInjectController>>();
pipeline.GetRunner<IEcsPreInitInjectCallbacks>().OnPreInitInjectionBefore();
injectMapRunner.Inject(_injectController);
}
var injectRunnerGeneric = systems.GetRunner<IEcsInject<T>>();
var injectRunnerGeneric = pipeline.GetRunner<IEcsInject<T>>();
injectRunnerGeneric.Inject(_injectedData);
if (_injectController.OnInject())
{
_injectController.Destroy();
var injectCallbacksRunner = systems.GetRunner<IEcsPreInitInjectCallbacks>();
var injectCallbacksRunner = pipeline.GetRunner<IEcsPreInitInjectCallbacks>();
injectCallbacksRunner.OnPreInitInjectionAfter();
EcsRunner.Destroy(injectCallbacksRunner);
}
}
@ -134,27 +135,27 @@ namespace DCFApixels.DragonECS
public static class InjectSystemExstensions
{
public static EcsSystems.Builder Inject<T>(this EcsSystems.Builder self, T data)
public static EcsPipeline.Builder Inject<T>(this EcsPipeline.Builder self, T data)
{
self.Add(new InjectSystem<T>(data));
return self;
}
public static EcsSystems.Builder Inject<A, B>(this EcsSystems.Builder self, A a, B b)
public static EcsPipeline.Builder Inject<A, B>(this EcsPipeline.Builder self, A a, B b)
{
self.Inject(a).Inject(b);
return self;
}
public static EcsSystems.Builder Inject<A, B, C, D>(this EcsSystems.Builder self, A a, B b, C c, D d)
public static EcsPipeline.Builder Inject<A, B, C, D>(this EcsPipeline.Builder self, A a, B b, C c, D d)
{
self.Inject(a).Inject(b).Inject(c).Inject(d);
return self;
}
public static EcsSystems.Builder Inject<A, B, C, D, E>(this EcsSystems.Builder self, A a, B b, C c, D d, E e)
public static EcsPipeline.Builder Inject<A, B, C, D, E>(this EcsPipeline.Builder self, A a, B b, C c, D d, E e)
{
self.Inject(a).Inject(b).Inject(c).Inject(d).Inject(e);
return self;
}
public static EcsSystems.Builder Inject<A, B, C, D, E, F>(this EcsSystems.Builder self, A a, B b, C c, D d, E e, F f)
public static EcsPipeline.Builder Inject<A, B, C, D, E, F>(this EcsPipeline.Builder self, A a, B b, C c, D d, E e, F f)
{
self.Inject(a).Inject(b).Inject(c).Inject(d).Inject(e).Inject(f);
return self;

View File

@ -2,19 +2,19 @@
{
public interface IEcsPreInitSystem : IEcsSystem
{
public void PreInit(EcsSystems systems);
public void PreInit(EcsPipeline pipeline);
}
public interface IEcsInitSystem : IEcsSystem
{
public void Init(EcsSystems systems);
public void Init(EcsPipeline pipeline);
}
public interface IEcsRunSystem : IEcsSystem
{
public void Run(EcsSystems systems);
public void Run(EcsPipeline pipeline);
}
public interface IEcsDestroySystem : IEcsSystem
{
public void Destroy(EcsSystems systems);
public void Destroy(EcsPipeline pipeline);
}
public interface IEcsBaseSystem : IEcsInitSystem, IEcsRunSystem, IEcsDestroySystem { }
@ -24,18 +24,18 @@
#if DEBUG
private int[] _targetIds;
#endif
public void PreInit(EcsSystems systems)
public void PreInit(EcsPipeline pipeline)
{
#if DEBUG
for (int i = 0; i < targets.Length; i++)
{
int id = _targetIds[i];
EcsDebug.ProfileMarkBegin(id);
targets[i].PreInit(systems);
targets[i].PreInit(pipeline);
EcsDebug.ProfileMarkEnd(id);
}
#else
foreach (var item in targets) item.PreInit(systems);
foreach (var item in targets) item.PreInit(pipeline);
#endif
}
@ -55,18 +55,18 @@
#if DEBUG
private int[] _targetIds;
#endif
public void Init(EcsSystems systems)
public void Init(EcsPipeline pipeline)
{
#if DEBUG
for (int i = 0; i < targets.Length; i++)
{
int id = _targetIds[i];
EcsDebug.ProfileMarkBegin(id);
targets[i].Init(systems);
targets[i].Init(pipeline);
EcsDebug.ProfileMarkEnd(id);
}
#else
foreach (var item in targets) item.Init(systems);
foreach (var item in targets) item.Init(pipeline);
#endif
}
@ -86,18 +86,18 @@
#if DEBUG
private int[] _targetIds;
#endif
public void Run(EcsSystems systems)
public void Run(EcsPipeline pipeline)
{
#if DEBUG
for (int i = 0; i < targets.Length; i++)
{
int id = _targetIds[i];
EcsDebug.ProfileMarkBegin(id);
targets[i].Run(systems);
targets[i].Run(pipeline);
EcsDebug.ProfileMarkEnd(id);
}
#else
foreach (var item in targets) item.Run(systems);
foreach (var item in targets) item.Run(pipeline);
#endif
}
@ -117,18 +117,18 @@
#if DEBUG
private int[] _targetIds;
#endif
public void Destroy(EcsSystems systems)
public void Destroy(EcsPipeline pipeline)
{
#if DEBUG
for (int i = 0; i < targets.Length; i++)
{
int id = _targetIds[i];
EcsDebug.ProfileMarkBegin(id);
targets[i].Destroy(systems);
targets[i].Destroy(pipeline);
EcsDebug.ProfileMarkEnd(id);
}
#else
foreach (var item in targets) item.Destroy(systems);
foreach (var item in targets) item.Destroy(pipeline);
#endif
}

View File

@ -5,7 +5,7 @@ using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS
{
public sealed class EcsSystems
public sealed class EcsPipeline
{
private IEcsSystem[] _allSystems;
private Dictionary<Type, IEcsRunner> _runners;
@ -24,7 +24,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Constructors
private EcsSystems(IEcsSystem[] systems)
private EcsPipeline(IEcsSystem[] systems)
{
_allSystems = systems;
_runners = new Dictionary<Type, IEcsRunner>();
@ -47,6 +47,11 @@ namespace DCFApixels.DragonECS
_runners.Add(type, result);
return (T)result;
}
internal void OnRunnerDestroy(IEcsRunner runner)
{
_runners.Remove(runner.Interface);
}
#endregion
#region LifeCycle
@ -54,7 +59,7 @@ namespace DCFApixels.DragonECS
{
if(_isInit == true)
{
EcsDebug.Print("[Warning]", $"This {nameof(EcsSystems)} has already been initialized");
EcsDebug.Print("[Warning]", $"This {nameof(EcsPipeline)} has already been initialized");
return;
}
_isInit = true;
@ -81,7 +86,7 @@ namespace DCFApixels.DragonECS
#endif
if (_isDestoryed == true)
{
EcsDebug.Print("[Warning]", $"This {nameof(EcsSystems)} has already been destroyed");
EcsDebug.Print("[Warning]", $"This {nameof(EcsPipeline)} has already been destroyed");
return;
}
_isDestoryed = true;
@ -94,17 +99,17 @@ namespace DCFApixels.DragonECS
private void CheckBeforeInitForMethod(string methodName)
{
if (!_isInit)
throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsSystems)}");
throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsPipeline)}");
}
private void CheckAfterInitForMethod(string methodName)
{
if (_isInit)
throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsSystems)}");
throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsPipeline)}");
}
private void CheckAfterDestroyForMethod(string methodName)
{
if (_isDestoryed)
throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsSystems)}");
throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsPipeline)}");
}
#endif
#endregion
@ -117,6 +122,7 @@ namespace DCFApixels.DragonECS
public class Builder
{
private const int KEYS_CAPACITY = 4;
private HashSet<Type> _uniqueTypes;
private readonly List<object> _blockExecutionOrder;
private readonly Dictionary<object, List<IEcsSystem>> _systems;
private readonly object _basicBlocKey;
@ -125,6 +131,7 @@ namespace DCFApixels.DragonECS
public Builder()
{
_basicBlocKey = "Basic";
_uniqueTypes = new HashSet<Type>();
_blockExecutionOrder = new List<object>(KEYS_CAPACITY);
_systems = new Dictionary<object, List<IEcsSystem>>(KEYS_CAPACITY);
_isBasicBlockDeclared = false;
@ -132,6 +139,17 @@ namespace DCFApixels.DragonECS
}
public Builder Add(IEcsSystem system, object blockKey = null)
{
AddInternal(system, blockKey, false);
return this;
}
public Builder AddUnique(IEcsSystem system, object blockKey = null)
{
AddInternal(system, blockKey, true);
return this;
}
private void AddInternal(IEcsSystem system, object blockKey, bool isUnique)
{
if (blockKey == null) blockKey = _basicBlocKey;
List<IEcsSystem> list;
@ -141,8 +159,9 @@ namespace DCFApixels.DragonECS
list.Add(new SystemsBlockMarkerSystem(blockKey.ToString()));
_systems.Add(blockKey, list);
}
if ((_uniqueTypes.Add(system.GetType()) == false && isUnique))
return;
list.Add(system);
return this;
}
public Builder Add(IEcsModule module)
@ -167,11 +186,11 @@ namespace DCFApixels.DragonECS
return this;
}
public EcsSystems Build()
public EcsPipeline Build()
{
if (_isOnlyBasicBlock)
{
return new EcsSystems(_systems[_basicBlocKey].ToArray());
return new EcsPipeline(_systems[_basicBlocKey].ToArray());
}
if(_isBasicBlockDeclared == false)
@ -196,7 +215,7 @@ namespace DCFApixels.DragonECS
}
}
return new EcsSystems(result.ToArray());
return new EcsPipeline(result.ToArray());
}
}
#endregion
@ -204,16 +223,17 @@ namespace DCFApixels.DragonECS
public interface IEcsModule
{
public void ImportSystems(EcsSystems.Builder builder);
public void ImportSystems(EcsPipeline.Builder builder);
}
public static class EcsSystemsExt
#region Extensions
public static class EcsSystemsExtensions
{
public static bool IsNullOrDestroyed(this EcsSystems self)
public static bool IsNullOrDestroyed(this EcsPipeline self)
{
return self == null || self.IsDestoryed;
}
public static EcsSystems.Builder Add(this EcsSystems.Builder self, IEnumerable<IEcsSystem> range, object blockKey = null)
public static EcsPipeline.Builder Add(this EcsPipeline.Builder self, IEnumerable<IEcsSystem> range, object blockKey = null)
{
foreach (var item in range)
{
@ -221,12 +241,21 @@ namespace DCFApixels.DragonECS
}
return self;
}
public static EcsSystems BuildAndInit(this EcsSystems.Builder self)
public static EcsPipeline.Builder AddUnique(this EcsPipeline.Builder self, IEnumerable<IEcsSystem> range, object blockKey = null)
{
EcsSystems result = self.Build();
foreach (var item in range)
{
self.AddUnique(item, blockKey);
}
return self;
}
public static EcsPipeline BuildAndInit(this EcsPipeline.Builder self)
{
EcsPipeline result = self.Build();
result.Init();
return result;
}
}
#endregion
}

View File

@ -24,10 +24,15 @@ namespace DCFApixels.DragonECS
public interface IEcsSystem { }
public interface IEcsRunner
{
public EcsSystems Source { get; }
public EcsPipeline Source { get; }
public Type Interface { get; }
public IList Targets { get; }
public object Filter { get; }
public bool IsHasFilter { get; }
public bool IsDestroyed { get; }
public bool IsEmpty { get; }
public void Destroy();
}
internal static class EcsRunnerActivator
@ -120,6 +125,10 @@ namespace DCFApixels.DragonECS
}
}
public static class EcsRunner
{
public static void Destroy(object runner) => ((IEcsRunner)runner).Destroy();
}
public abstract class EcsRunner<TInterface> : IEcsSystem, IEcsRunner
where TInterface : IEcsSystem
{
@ -183,7 +192,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Instantiate
private static TInterface Instantiate(EcsSystems source, TInterface[] targets, bool isHasFilter, object filter)
private static TInterface Instantiate(EcsPipeline source, TInterface[] targets, bool isHasFilter, object filter)
{
if (_subclass == null)
EcsRunnerActivator.InitFor<TInterface>();
@ -191,30 +200,34 @@ namespace DCFApixels.DragonECS
var instance = (EcsRunner<TInterface>)Activator.CreateInstance(_subclass);
return (TInterface)(IEcsSystem)instance.Set(source, targets, isHasFilter, filter);
}
public static TInterface Instantiate(EcsSystems source)
public static TInterface Instantiate(EcsPipeline source)
{
return Instantiate(source, FilterSystems(source.AllSystems), false, null);
}
public static TInterface Instantiate(EcsSystems source, object filter)
public static TInterface Instantiate(EcsPipeline source, object filter)
{
return Instantiate(source, FilterSystems(source.AllSystems, filter), true, filter);
}
#endregion
private EcsSystems _source;
private EcsPipeline _source;
protected TInterface[] targets;
private ReadOnlyCollection<TInterface> _targetsSealed;
private object _filter;
private bool _isHasFilter;
private bool _isDestroyed;
#region Properties
public EcsSystems Source => _source;
public EcsPipeline Source => _source;
public Type Interface => typeof(TInterface);
public IList Targets => _targetsSealed;
public object Filter => _filter;
public bool IsHasFilter => _isHasFilter;
public bool IsDestroyed => _isDestroyed;
public bool IsEmpty => targets == null || targets.Length <= 0;
#endregion
private EcsRunner<TInterface> Set(EcsSystems source, TInterface[] targets, bool isHasFilter, object filter)
private EcsRunner<TInterface> Set(EcsPipeline source, TInterface[] targets, bool isHasFilter, object filter)
{
_source = source;
this.targets = targets;
@ -225,8 +238,6 @@ namespace DCFApixels.DragonECS
return this;
}
protected virtual void OnSetup() { }
internal void Rebuild()
{
if(_isHasFilter)
@ -234,6 +245,20 @@ namespace DCFApixels.DragonECS
else
Set(_source, FilterSystems(_source.AllSystems, _filter), _isHasFilter, _filter);
}
public void Destroy()
{
_isDestroyed = true;
_source.OnRunnerDestroy(this);
_source = null;
targets = null;
_targetsSealed = null;
_filter = null;
OnDestroy();
}
protected virtual void OnSetup() { }
protected virtual void OnDestroy() { }
}
#region Extensions