From caef8d8baee2a24a9e15bcd411ce558ef523f5fc Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Fri, 26 May 2023 03:45:35 +0800 Subject: [PATCH] refactoring --- src/Builtin/InjectSystem.cs | 124 +++++++++++++++--------------------- src/Builtin/Systems.cs | 70 ++++++++++---------- src/Builtin/WorldRunners.cs | 106 ------------------------------ src/Builtin/Worlds.cs | 4 ++ src/EcsWorld.cs | 12 +--- 5 files changed, 93 insertions(+), 223 deletions(-) delete mode 100644 src/Builtin/WorldRunners.cs diff --git a/src/Builtin/InjectSystem.cs b/src/Builtin/InjectSystem.cs index 5fa251f..e387514 100644 --- a/src/Builtin/InjectSystem.cs +++ b/src/Builtin/InjectSystem.cs @@ -5,37 +5,8 @@ using System.Linq; namespace DCFApixels.DragonECS { //TODO развить идею инжектов - //1) добавить расширенный метод инжекта, с 2 джинерик-аргументами, первый базовый тип и второй инжектируемый тип. - //напримере это будет работать так Inject делает инжект объекта типа Foo для систем с IEcsInject или с IEcsInject - //2) добавить контейнер, который автоматически создается, собирает в себя все пре-инжекты и авто-инжектится во все системы. + //добавить контейнер, который автоматически создается, собирает в себя все пре-инжекты и авто-инжектится во все системы. //но это спорная идея - namespace Internal - { - internal class PreInitInjectController - { - private EcsPipeline _source; - private InjectSystemBase[] _injectSystems; - private int _injectCount; - public PreInitInjectController(EcsPipeline source) - { - _injectCount = 0; - _source = source; - _injectSystems = _source.AllSystems.OfType().ToArray(); - } - public bool OnInject() - { - _injectCount++; - return IsInjectionEnd; - } - public bool IsInjectionEnd => _injectCount >= _injectSystems.Length; - public void Destroy() - { - _source = null; - _injectSystems = null; - } - } - } - public interface IEcsPreInject : IEcsSystem { void PreInject(object obj); @@ -52,6 +23,29 @@ namespace DCFApixels.DragonECS namespace Internal { + internal class PreInitInjectController + { + private EcsPipeline _source; + private InjectSystemBase[] _injectSystems; + private int _injectCount; + public bool IsInjectionEnd => _injectCount >= _injectSystems.Length; + public PreInitInjectController(EcsPipeline source) + { + _injectCount = 0; + _source = source; + _injectSystems = _source.AllSystems.OfType().ToArray(); + } + public bool OnInject() + { + _injectCount++; + return IsInjectionEnd; + } + public void Destroy() + { + _source = null; + _injectSystems = null; + } + } [DebugHide, DebugColor(DebugColor.Gray)] public sealed class EcsPreInjectRunner : EcsRunner, IEcsPreInject { @@ -86,55 +80,43 @@ namespace DCFApixels.DragonECS foreach (var item in targets) item.OnPreInitInjectionBefore(); } } - } - - public class InjectSystemBase { } - - [DebugHide, DebugColor(DebugColor.Gray)] - public class InjectSystem : InjectSystemBase, IEcsPreInitProcess, IEcsInject, IEcsPreInitInjectProcess - { - private T _injectedData; - - private PreInitInjectController _injectController; - void IEcsInject.Inject(PreInitInjectController obj) => _injectController = obj; - - public InjectSystem(T injectedData) + public class InjectSystemBase { } + [DebugHide, DebugColor(DebugColor.Gray)] + public class InjectSystem : InjectSystemBase, IEcsPreInitProcess, IEcsInject, IEcsPreInitInjectProcess { - _injectedData = injectedData; - } - - public void PreInit(EcsPipeline pipeline) - { - if (_injectedData == null) - return; - - if (_injectController == null) + private T _injectedData; + private PreInitInjectController _injectController; + void IEcsInject.Inject(PreInitInjectController obj) => _injectController = obj; + public InjectSystem(T injectedData) { - _injectController = new PreInitInjectController(pipeline); - var injectMapRunner = pipeline.GetRunner>(); - pipeline.GetRunner().OnPreInitInjectionBefore(); - injectMapRunner.Inject(_injectController); + _injectedData = injectedData; } - - var injectRunnerGeneric = pipeline.GetRunner>(); - injectRunnerGeneric.Inject(_injectedData); - - if (_injectController.OnInject()) + public void PreInit(EcsPipeline pipeline) { - _injectController.Destroy(); - var injectCallbacksRunner = pipeline.GetRunner(); - injectCallbacksRunner.OnPreInitInjectionAfter(); - EcsRunner.Destroy(injectCallbacksRunner); + if (_injectedData == null) return; + if (_injectController == null) + { + _injectController = new PreInitInjectController(pipeline); + var injectMapRunner = pipeline.GetRunner>(); + pipeline.GetRunner().OnPreInitInjectionBefore(); + injectMapRunner.Inject(_injectController); + } + var injectRunnerGeneric = pipeline.GetRunner>(); + injectRunnerGeneric.Inject(_injectedData); + if (_injectController.OnInject()) + { + _injectController.Destroy(); + var injectCallbacksRunner = pipeline.GetRunner(); + injectCallbacksRunner.OnPreInitInjectionAfter(); + EcsRunner.Destroy(injectCallbacksRunner); + } } - } - public void OnPreInitInjectionBefore() { } - public void OnPreInitInjectionAfter() - { - _injectController = null; + public void OnPreInitInjectionBefore() { } + public void OnPreInitInjectionAfter() => _injectController = null; } } - public static class InjectSystemExstensions + public static class InjectSystemExtensions { public static EcsPipeline.Builder Inject(this EcsPipeline.Builder self, T data) { diff --git a/src/Builtin/Systems.cs b/src/Builtin/Systems.cs index 431afa3..15ff527 100644 --- a/src/Builtin/Systems.cs +++ b/src/Builtin/Systems.cs @@ -1,53 +1,51 @@ using System.Collections.Generic; +using DCFApixels.DragonECS.Internal; namespace DCFApixels.DragonECS { - [DebugHide, DebugColor(DebugColor.Black)] - public class SystemsBlockMarkerSystem : IEcsSystem + namespace Internal { - public readonly string name; - public SystemsBlockMarkerSystem(string name) { this.name = name; } - } - - [DebugHide, DebugColor(DebugColor.Grey)] - public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject - { - private List _worlds = new List(); - public void PreInject(object obj) + [DebugHide, DebugColor(DebugColor.Black)] + public class SystemsBlockMarkerSystem : IEcsSystem { - if (obj is EcsWorld world) - _worlds.Add(world); + public readonly string name; + public SystemsBlockMarkerSystem(string name) => this.name = name; } - public void Run(EcsPipeline pipeline) + [DebugHide, DebugColor(DebugColor.Grey)] + public class DeleteEmptyEntitesSystem : IEcsRunProcess, IEcsPreInject { - foreach (var world in _worlds) - world.DeleteEmptyEntites(); - } - } - - [DebugHide, DebugColor(DebugColor.Grey)] - public class DeleteOneFrameComponentSystem : IEcsRunProcess, IEcsInject - where TWorld : EcsWorld - where TComponent : struct, IEcsComponent - { - private TWorld _world; - public void Inject(TWorld obj) => _world = obj; - private sealed class Subject : EcsSubject - { - public EcsPool pool; - public Subject(Builder b) + private List _worlds = new List(); + public void PreInject(object obj) { - pool = b.Include(); + if (obj is EcsWorld world) + _worlds.Add(world); + } + public void Run(EcsPipeline pipeline) + { + foreach (var world in _worlds) + world.DeleteEmptyEntites(); } } - public void Run(EcsPipeline pipeline) + [DebugHide, DebugColor(DebugColor.Grey)] + public class DeleteOneFrameComponentSystem : IEcsRunProcess, IEcsInject + where TWorld : EcsWorld + where TComponent : struct, IEcsComponent { - foreach (var e in _world.Where(out Subject s)) - s.pool.Del(e); + private sealed class Subject : EcsSubject + { + public EcsPool pool; + public Subject(Builder b) => pool = b.Include(); + } + private TWorld _world; + public void Inject(TWorld obj) => _world = obj; + public void Run(EcsPipeline pipeline) + { + foreach (var e in _world.Where(out Subject s)) + s.pool.Del(e); + } } } - - public static class DeleteOneFrameComponentSystemExt + public static class DeleteOneFrameComponentSystemExtensions { private const string AUTO_DEL_LAYER = nameof(AUTO_DEL_LAYER); public static EcsPipeline.Builder AutoDel(this EcsPipeline.Builder b) diff --git a/src/Builtin/WorldRunners.cs b/src/Builtin/WorldRunners.cs deleted file mode 100644 index 105432b..0000000 --- a/src/Builtin/WorldRunners.cs +++ /dev/null @@ -1,106 +0,0 @@ -using DCFApixels.DragonECS.RunnersCore; - -namespace DCFApixels.DragonECS -{ - public interface IEcsComponentAdd : IEcsSystem - { - void OnComponentAdd(int entityID); - } - public interface IEcsComponentWrite : IEcsSystem - { - void OnComponentWrite(int entityID); - } - public interface IEcsComponentDel : IEcsSystem - { - void OnComponentDel(int entityID); - } - public interface IEcsComponentLifecycle : IEcsComponentAdd, IEcsComponentWrite, IEcsComponentDel { } - - namespace Internal - { - [DebugColor(DebugColor.Orange)] - public sealed class EcsEntityAddComponentRunner : EcsRunner, IEcsComponentAdd - { - public void OnComponentAdd(int entityID) - { - foreach (var item in targets) item.OnComponentAdd(entityID); - } - } - [DebugColor(DebugColor.Orange)] - public sealed class EcsEntityChangeComponentRunner : EcsRunner, IEcsComponentWrite - { - public void OnComponentWrite(int entityID) - { - foreach (var item in targets) item.OnComponentWrite(entityID); - } - } - [DebugColor(DebugColor.Orange)] - public sealed class EcsEntityDelComponentRunner : EcsRunner, IEcsComponentDel - { - public void OnComponentDel(int entityID) - { - foreach (var item in targets) item.OnComponentDel(entityID); - } - } - } - - public interface IEcsEntityCreate : IEcsSystem - { - void OnEntityCreate(int entityID); - } - public interface IEcsEntityDestroy : IEcsSystem - { - void OnEntityDestroy(int entityID); - } - public interface IEcsEntityLifecycle : IEcsEntityCreate, IEcsEntityDestroy { } - - namespace Internal - { - [DebugColor(DebugColor.Orange)] - public sealed class EcsEntityCreateRunner : EcsRunner, IEcsEntityCreate - { - public void OnEntityCreate(int entityID) - { - foreach (var item in targets) item.OnEntityCreate(entityID); - } - } - [DebugColor(DebugColor.Orange)] - public sealed class EcsEntityDestroyRunner : EcsRunner, IEcsEntityDestroy - { - public void OnEntityDestroy(int entityID) - { - foreach (var item in targets) item.OnEntityDestroy(entityID); - } - } - } - - public interface IEcsWorldCreate : IEcsSystem - { - void OnWorldCreate(EcsWorld world); - } - public interface IEcsWorldDestroy : IEcsSystem - { - void OnWorldDestroy(EcsWorld world); - } - public interface IEcsWorldLifecycle : IEcsWorldCreate, IEcsWorldDestroy { } - - namespace Internal - { - [DebugColor(DebugColor.Orange)] - public sealed class EcsWorldCreateRunner : EcsRunner, IEcsWorldCreate - { - public void OnWorldCreate(EcsWorld world) - { - foreach (var item in targets) item.OnWorldCreate(world); - } - } - [DebugColor(DebugColor.Orange)] - public sealed class EcsWorldDestryRunner : EcsRunner, IEcsWorldDestroy - { - public void OnWorldDestroy(EcsWorld world) - { - foreach (var item in targets) item.OnWorldDestroy(world); - } - } - } -} diff --git a/src/Builtin/Worlds.cs b/src/Builtin/Worlds.cs index d92e45e..23f26cb 100644 --- a/src/Builtin/Worlds.cs +++ b/src/Builtin/Worlds.cs @@ -1,5 +1,9 @@ namespace DCFApixels.DragonECS { + internal sealed class EcsNullWorld : EcsWorld + { + public EcsNullWorld() : base(false) { } + } public sealed class EcsDefaultWorld : EcsWorld { } public sealed class EcsEventWorld : EcsWorld { } } diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 752f3ab..6b0b631 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -1,16 +1,10 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; +using DCFApixels.DragonECS.Internal; namespace DCFApixels.DragonECS { - using Internal; - - internal sealed class EcsNullWorld : EcsWorld - { - public EcsNullWorld() : base(false) { } - } - public abstract class EcsWorld { private const short GEN_BITS = 0x7fff; @@ -19,6 +13,7 @@ namespace DCFApixels.DragonECS public static EcsWorld[] Worlds = new EcsWorld[8]; private static IntDispenser _worldIdDispenser = new IntDispenser(0); + public readonly short uniqueID; private int _worldTypeID; @@ -598,9 +593,6 @@ namespace DCFApixels.DragonECS void OnNewEntity(int entityID); void OnDelEntity(int entityID); } - #endregion - - #region Extensions internal static class WorldEventListExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)]