From 73090da11a1cbda39eff10262a28d8de4e09f524 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 12 Mar 2023 02:48:51 +0800 Subject: [PATCH] fixes --- src/Builtin/InjectProcessor.cs | 2 +- src/EcsSession.cs | 21 +++++++++++++++------ src/React/EcsRunner.cs | 9 +++++++-- test/Startup.cs | 2 +- test/TestSystem.cs | 10 +++++----- 5 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/Builtin/InjectProcessor.cs b/src/Builtin/InjectProcessor.cs index 7137658..cb887de 100644 --- a/src/Builtin/InjectProcessor.cs +++ b/src/Builtin/InjectProcessor.cs @@ -10,7 +10,7 @@ namespace DCFApixels.DragonECS { public void Inject(T obj); } - public class InjectRunner : EcsRunner>, IEcsInject + public sealed class InjectRunner : EcsRunner>, IEcsInject { void IEcsInject.Inject(T obj) { diff --git a/src/EcsSession.cs b/src/EcsSession.cs index 253d2b6..63f580c 100644 --- a/src/EcsSession.cs +++ b/src/EcsSession.cs @@ -40,23 +40,30 @@ namespace DCFApixels.DragonECS private int _id; - private List _allSystems; + private readonly List _allSystems; private ReadOnlyCollection _allSystemsSealed; private bool _isInit = false; private bool _isDestoryed = false; - private Dictionary _runners; + private readonly Dictionary _runners; private IEcsRunSystem _runRunnerCache; - private EcsWorldMap _worldMap = new EcsWorldMap(); + private readonly EcsWorldMap _worldMap; #region Properties public ReadOnlyCollection AllProcessors => _allSystemsSealed; #endregion + public EcsSession() + { + _allSystems = new List(128); + _runners = new Dictionary(); + _worldMap = new EcsWorldMap(); + } + #region React Runners/Messengers public T GetRunner() where T : IEcsProcessor { @@ -89,7 +96,7 @@ namespace DCFApixels.DragonECS #endregion #region LifeCycle - public void Init() + public EcsSession Init() { CheckInitForMethod(nameof(Init)); _worldMap.Build(); @@ -102,6 +109,8 @@ namespace DCFApixels.DragonECS GetRunner().Init(this); _runRunnerCache = GetRunner(); + + return this; } public void Run() { @@ -111,7 +120,7 @@ namespace DCFApixels.DragonECS } public void Destroy() { - CheckDestroyForMethod(nameof(Run)); + CheckDestroyForMethod(nameof(Destroy)); _isDestoryed = true; GetRunner().Destroy(this); @@ -126,7 +135,7 @@ namespace DCFApixels.DragonECS } private void CheckDestroyForMethod(string methodName) { - if (_isInit) + if (_isDestoryed) throw new MethodAccessException($"Запрещено вызывать метод {methodName}, после уничтожения {nameof(EcsSession)}"); } #endregion diff --git a/src/React/EcsRunner.cs b/src/React/EcsRunner.cs index c986a6d..0ead8cf 100644 --- a/src/React/EcsRunner.cs +++ b/src/React/EcsRunner.cs @@ -59,7 +59,7 @@ namespace DCFApixels.DragonECS _runnerTypes = new Dictionary(); foreach (var item in newRunnerTypes) { - Type intrf = item.GetInterfaces()[2]; //TODO доработать это место. Во-первых убрать магическое число 2, во-вторых сделать так чтоб брался только наследованный интерфейс, а не все + Type intrf = item.GetInterfaces().Where(o => o != typeof(IEcsRunner) && o != typeof(IEcsProcessor)).First(); //TODO оптимизировать это место _runnerTypes.Add(intrf.GUID, item); } @@ -87,7 +87,12 @@ namespace DCFApixels.DragonECS public static void InitFor() where TInterface : IEcsProcessor { Type interfaceType = typeof(TInterface); - Guid interfaceGuid = interfaceType.GUID; + Type nonGenericInterfaceType = interfaceType; + if (nonGenericInterfaceType.IsGenericType) + { + nonGenericInterfaceType = nonGenericInterfaceType.GetGenericTypeDefinition(); + } + Guid interfaceGuid = nonGenericInterfaceType.GUID; if (!_runnerTypes.TryGetValue(interfaceGuid, out Type runnerType)) { diff --git a/test/Startup.cs b/test/Startup.cs index 50fa5e2..1730c2d 100644 --- a/test/Startup.cs +++ b/test/Startup.cs @@ -13,7 +13,7 @@ namespace DCFApixels.DragonECS private void Start() { - _ecsSession + _ecsSession = new EcsSession() .Add(new TestSystem()) .Inject(_data) .Init(); diff --git a/test/TestSystem.cs b/test/TestSystem.cs index e5c3182..50b9456 100644 --- a/test/TestSystem.cs +++ b/test/TestSystem.cs @@ -3,26 +3,26 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using UnityEngine; namespace DCFApixels.DragonECS { - public class TestSystem : - IEcsInject, - IEcsSimpleCycleSystem + public class TestSystem : IEcsInject, IEcsSimpleCycleSystem { private SharedData _sharedData; public void Inject(SharedData obj) => _sharedData = obj; - - public void Init(EcsSession session) { + Debug.Log("Init"); } public void Run(EcsSession session) { + Debug.Log("Run"); } public void Destroy(EcsSession session) { + Debug.Log("Destroy"); } } }