diff --git a/src/EcsSystems.cs b/src/EcsSystems.cs index 0c6ceb7..93fc52c 100644 --- a/src/EcsSystems.cs +++ b/src/EcsSystems.cs @@ -14,6 +14,7 @@ namespace DCFApixels.DragonECS private ReadOnlyCollection _allSystemsSealed; private ReadOnlyDictionary _allRunnersSealed; + private bool _isInit; private bool _isDestoryed; #region Properties @@ -31,12 +32,8 @@ namespace DCFApixels.DragonECS _allSystemsSealed = new ReadOnlyCollection(_allSystems); _allRunnersSealed = new ReadOnlyDictionary(_runners); + _isInit = false; _isDestoryed = false; - - GetRunner().PreInit(this); - GetRunner().Init(this); - - _runRunnerCache = GetRunner(); } #endregion @@ -53,11 +50,26 @@ namespace DCFApixels.DragonECS #endregion #region LifeCycle + public void Init() + { + if(_isInit == true) + { + EcsDebug.Print("[Warning]", $"This {nameof(EcsSystems)} has already been initialized"); + return; + } + _isInit = true; + + GetRunner().PreInit(this); + GetRunner().Init(this); + + _runRunnerCache = GetRunner(); + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Run() { #if DEBUG || !DRAGONECS_NO_SANITIZE_CHECKS + CheckBeforeInitForMethod(nameof(Run)); CheckAfterDestroyForMethod(nameof(Run)); #endif _runRunnerCache.Run(this); @@ -65,8 +77,13 @@ namespace DCFApixels.DragonECS public void Destroy() { #if DEBUG || !DRAGONECS_NO_SANITIZE_CHECKS - CheckAfterDestroyForMethod(nameof(Destroy)); + CheckBeforeInitForMethod(nameof(Run)); #endif + if (_isDestoryed == true) + { + EcsDebug.Print("[Warning]", $"This {nameof(EcsSystems)} has already been destroyed"); + return; + } _isDestoryed = true; GetRunner().Destroy(this); } @@ -74,10 +91,20 @@ namespace DCFApixels.DragonECS #region StateChecks #if DEBUG || !DRAGONECS_NO_SANITIZE_CHECKS + private void CheckBeforeInitForMethod(string methodName) + { + if (!_isInit) + throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsSystems)}"); + } + private void CheckAfterInitForMethod(string methodName) + { + if (_isInit) + throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsSystems)}"); + } private void CheckAfterDestroyForMethod(string methodName) { if (_isDestoryed) - throw new MethodAccessException($"It is forbidden to call method {methodName}, after destroying {nameof(EcsSystems)}"); + throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsSystems)}"); } #endif #endregion @@ -191,5 +218,12 @@ namespace DCFApixels.DragonECS } return self; } + public static EcsSystems BuildAndInit(this EcsSystems.Builder self) + { + EcsSystems result = self.Build(); + result.Init(); + return result; + } + } }