From 9a6010efdbbec549fabc1699d415d18d4ff6a900 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 26 Jun 2023 02:53:55 +0800 Subject: [PATCH] update exceptions --- src/Builtin/InjectSystem.cs | 1 - src/EcsAspect.cs | 5 +- src/EcsGroup.cs | 8 +- src/EcsPipeline.cs | 18 ++--- src/EcsWorld.cs | 1 - src/Pools/EcsPool.cs | 13 ++- src/Pools/EcsPoolBase.cs | 5 +- src/Pools/EcsTagPool.cs | 17 ++-- src/Utils/Exceptions.cs | 156 ++++++++++++++++-------------------- src/entlong.cs | 2 +- 10 files changed, 100 insertions(+), 126 deletions(-) diff --git a/src/Builtin/InjectSystem.cs b/src/Builtin/InjectSystem.cs index 03c6a7c..fdac12e 100644 --- a/src/Builtin/InjectSystem.cs +++ b/src/Builtin/InjectSystem.cs @@ -2,7 +2,6 @@ using DCFApixels.DragonECS.RunnersCore; using System; using System.Linq; -using static DCFApixels.DragonECS.EcsThrowHalper; namespace DCFApixels.DragonECS { diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 0ff7038..adb37b3 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -1,12 +1,11 @@ -using System; +using DCFApixels.DragonECS.Internal; +using System; using System.Collections.Generic; -using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Text; -using static DCFApixels.DragonECS.EcsThrowHalper; namespace DCFApixels.DragonECS { diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 5886d20..cd8e834 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -1,13 +1,11 @@ -using System; -using System.Linq; +using DCFApixels.DragonECS.Internal; +using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; +using System.Linq; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS -using static DCFApixels.DragonECS.EcsThrowHalper; -#endif namespace DCFApixels.DragonECS { diff --git a/src/EcsPipeline.cs b/src/EcsPipeline.cs index a06e1c5..6c39ded 100644 --- a/src/EcsPipeline.cs +++ b/src/EcsPipeline.cs @@ -6,21 +6,20 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.CompilerServices; -using static DCFApixels.DragonECS.EcsThrowHalper; namespace DCFApixels.DragonECS { public sealed class EcsPipeline { private IEcsProcess[] _allSystems; - private Dictionary _runners; + private Dictionary _runners = new Dictionary(); private IEcsRunProcess _runRunnerCache; private ReadOnlyCollection _allSystemsSealed; private ReadOnlyDictionary _allRunnersSealed; - private bool _isInit; - private bool _isDestoryed; + private bool _isInit = false; + private bool _isDestoryed = false; #region Properties public ReadOnlyCollection AllSystems => _allSystemsSealed; @@ -33,13 +32,8 @@ namespace DCFApixels.DragonECS private EcsPipeline(IEcsProcess[] systems) { _allSystems = systems; - _runners = new Dictionary(); - _allSystemsSealed = new ReadOnlyCollection(_allSystems); _allRunnersSealed = new ReadOnlyDictionary(_runners); - - _isInit = false; - _isDestoryed = false; } #endregion @@ -87,7 +81,7 @@ namespace DCFApixels.DragonECS public void Run() { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (_isInit) Throw.Pipeline_MethodCalledBeforeInitialisation(nameof(Run)); + if (!_isInit) Throw.Pipeline_MethodCalledBeforeInitialisation(nameof(Run)); if (_isDestoryed) Throw.Pipeline_MethodCalledAfterDestruction(nameof(Run)); #endif _runRunnerCache.Run(this); @@ -95,9 +89,9 @@ namespace DCFApixels.DragonECS public void Destroy() { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (_isInit) Throw.Pipeline_MethodCalledBeforeInitialisation(nameof(Destroy)); + if (!_isInit) Throw.Pipeline_MethodCalledBeforeInitialisation(nameof(Destroy)); #endif - if (_isDestoryed == true) + if (_isDestoryed) { EcsDebug.PrintWarning($"This {nameof(EcsPipeline)} has already been destroyed"); return; diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index e062288..dc9f5d6 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -3,7 +3,6 @@ using DCFApixels.DragonECS.Utils; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; -using static DCFApixels.DragonECS.EcsThrowHalper; namespace DCFApixels.DragonECS { diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index 131b2ff..ac14c63 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -2,7 +2,6 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; -using static DCFApixels.DragonECS.EcsPoolThrowHalper; namespace DCFApixels.DragonECS { @@ -53,7 +52,7 @@ namespace DCFApixels.DragonECS { ref int itemIndex = ref _mapping[entityID]; #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (itemIndex > 0) ThrowAlreadyHasComponent(entityID); + if (itemIndex > 0) EcsPoolThrowHalper.ThrowAlreadyHasComponent(entityID); #endif if (_recycledItemsCount > 0) { @@ -74,7 +73,7 @@ namespace DCFApixels.DragonECS public ref T Get(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif _listeners.InvokeOnGet(entityID); return ref _items[_mapping[entityID]]; @@ -83,7 +82,7 @@ namespace DCFApixels.DragonECS public ref readonly T Read(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif return ref _items[_mapping[entityID]]; } @@ -117,7 +116,7 @@ namespace DCFApixels.DragonECS public void Del(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif ref int itemIndex = ref _mapping[entityID]; _componentResetHandler.Reset(ref _items[itemIndex]); @@ -136,14 +135,14 @@ namespace DCFApixels.DragonECS public void Copy(int fromEntityID, int toEntityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) ThrowNotHaveComponent(fromEntityID); + if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(fromEntityID); #endif _componentCopyHandler.Copy(ref Get(fromEntityID), ref TryAddOrGet(toEntityID)); } public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) ThrowNotHaveComponent(fromEntityID); + if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(fromEntityID); #endif _componentCopyHandler.Copy(ref Get(fromEntityID), ref toWorld.GetPool().TryAddOrGet(toEntityID)); } diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index f07681f..6237eda 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -1,4 +1,5 @@ using DCFApixels.DragonECS.Internal; +using DCFApixels.DragonECS.Internal; using System; using System.Collections.Generic; using System.Runtime.CompilerServices; @@ -57,11 +58,11 @@ namespace DCFApixels.DragonECS { public static void ThrowAlreadyHasComponent(int entityID) { - throw new EcsFrameworkException($"Entity({entityID}) already has component {typeof(T).Name}."); + throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName()}."); } public static void ThrowNotHaveComponent(int entityID) { - throw new EcsFrameworkException($"Entity({entityID}) has no component {typeof(T).Name}."); + throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName()}."); } } public static class IEcsPoolImplementationExtensions diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index a435a19..69f45a2 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -2,7 +2,6 @@ using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; -using static DCFApixels.DragonECS.EcsPoolThrowHalper; namespace DCFApixels.DragonECS { @@ -42,7 +41,7 @@ namespace DCFApixels.DragonECS public void Add(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (Has(entityID)) ThrowAlreadyHasComponent(entityID); + if (Has(entityID)) EcsPoolThrowHalper.ThrowAlreadyHasComponent(entityID); #endif _count++; _mapping[entityID] = true; @@ -67,7 +66,7 @@ namespace DCFApixels.DragonECS public void Del(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif _mapping[entityID] = false; _count--; @@ -81,14 +80,14 @@ namespace DCFApixels.DragonECS public void Copy(int fromEntityID, int toEntityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) ThrowNotHaveComponent(fromEntityID); + if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(fromEntityID); #endif TryAdd(toEntityID); } public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(fromEntityID)) ThrowNotHaveComponent(fromEntityID); + if (!Has(fromEntityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(fromEntityID); #endif toWorld.GetPool().TryAdd(toEntityID); } @@ -137,14 +136,14 @@ namespace DCFApixels.DragonECS ref readonly T IEcsStructPool.Read(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif return ref _fakeComponent; } ref T IEcsStructPool.Get(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif return ref _fakeComponent; } @@ -152,14 +151,14 @@ namespace DCFApixels.DragonECS object IEcsPool.GetRaw(int entityID) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif return _fakeComponent; } void IEcsPool.SetRaw(int entityID, object dataRaw) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if (!Has(entityID)) ThrowNotHaveComponent(entityID); + if (!Has(entityID)) EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); #endif } #endregion diff --git a/src/Utils/Exceptions.cs b/src/Utils/Exceptions.cs index 7cd999d..ed4e955 100644 --- a/src/Utils/Exceptions.cs +++ b/src/Utils/Exceptions.cs @@ -4,98 +4,84 @@ using System.Runtime.Serialization; namespace DCFApixels.DragonECS { - public class EcsThrowHalper + namespace Internal { - public static readonly EcsThrowHalper Throw = new EcsThrowHalper(); - private EcsThrowHalper() { } - } + internal static class Throw + { + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void ArgumentNull() + { + throw new ArgumentNullException(); + } - public static class EcsThrowHalper_Core - { - [MethodImpl(MethodImplOptions.NoInlining)] - public static void Pool_AlreadyHasComponent(this EcsThrowHalper _, int entityID) - { - throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName()}."); - } - [MethodImpl(MethodImplOptions.NoInlining)] - public static void Pool_NotHaveComponent(this EcsThrowHalper _, int entityID) - { - throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName()}."); - } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void ConstraintIsAlreadyContainedInMask(Type type) + { + throw new EcsFrameworkException($"The {EcsDebugUtility.GetGenericTypeName(type)} constraint is already contained in the mask."); + } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void ArgumentNull(this EcsThrowHalper _) - { - throw new ArgumentNullException(); - } + //[MethodImpl(MethodImplOptions.NoInlining)] + //public static void ArgumentDifferentWorldsException() + //{ + // throw new ArgumentException("The groups belong to different worlds."); + //} + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void ArgumentOutOfRange() + { + throw new ArgumentOutOfRangeException($"index is less than 0 or is equal to or greater than Count."); + } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void ConstraintIsAlreadyContainedInMask(this EcsThrowHalper _, Type type) - { - throw new EcsFrameworkException($"The {EcsDebugUtility.GetGenericTypeName(type)} constraint is already contained in the mask."); - } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Group_AlreadyContains(int entityID) + { + throw new EcsFrameworkException($"This group already contains entity {entityID}."); + } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Group_DoesNotContain(int entityID) + { + throw new EcsFrameworkException($"This group does not contain entity {entityID}."); + } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Group_ArgumentDifferentWorldsException() + { + throw new ArgumentException("The groups belong to different worlds."); + } - //[MethodImpl(MethodImplOptions.NoInlining)] - //public static void ArgumentDifferentWorldsException() - //{ - // throw new ArgumentException("The groups belong to different worlds."); - //} - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void ArgumentOutOfRange(this EcsThrowHalper _) - { - throw new ArgumentOutOfRangeException($"index is less than 0 or is equal to or greater than Count."); - } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Pipeline_MethodCalledAfterInitialisation(string methodName) + { + throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsPipeline)}."); + } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Pipeline_MethodCalledBeforeInitialisation(string methodName) + { + throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsPipeline)}."); + } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Pipeline_MethodCalledAfterDestruction(string methodName) + { + throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsPipeline)}."); + } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Group_AlreadyContains(this EcsThrowHalper _, int entityID) - { - throw new EcsFrameworkException($"This group already contains entity {entityID}."); - } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Group_DoesNotContain(this EcsThrowHalper _, int entityID) - { - throw new EcsFrameworkException($"This group does not contain entity {entityID}."); - } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Group_ArgumentDifferentWorldsException(this EcsThrowHalper _) - { - throw new ArgumentException("The groups belong to different worlds."); - } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void World_InvalidIncrementComponentsBalance() + { + throw new MethodAccessException("Invalid increment components balance."); + } + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void World_GroupDoesNotBelongWorld() + { + throw new MethodAccessException("The Group does not belong in this world."); + } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Pipeline_MethodCalledAfterInitialisation(this EcsThrowHalper _, string methodName) - { - throw new MethodAccessException($"It is forbidden to call {methodName}, after initialization {nameof(EcsPipeline)}."); - } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Pipeline_MethodCalledBeforeInitialisation(this EcsThrowHalper _, string methodName) - { - throw new MethodAccessException($"It is forbidden to call {methodName}, before initialization {nameof(EcsPipeline)}."); - } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Pipeline_MethodCalledAfterDestruction(this EcsThrowHalper _, string methodName) - { - throw new MethodAccessException($"It is forbidden to call {methodName}, after destroying {nameof(EcsPipeline)}."); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void World_InvalidIncrementComponentsBalance(this EcsThrowHalper _) - { - throw new MethodAccessException("Invalid increment components balance."); - } - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void World_GroupDoesNotBelongWorld(this EcsThrowHalper _) - { - throw new MethodAccessException("The Group does not belong in this world."); - } - - [MethodImpl(MethodImplOptions.NoInlining)] - internal static void Ent_ThrowIsNotAlive(this EcsThrowHalper _, entlong entity) - { - if (entity.IsNull) - throw new EcsFrameworkException($"The {entity} is null."); - else - throw new EcsFrameworkException($"The {entity} is not alive."); + [MethodImpl(MethodImplOptions.NoInlining)] + internal static void Ent_ThrowIsNotAlive(entlong entity) + { + if (entity.IsNull) + throw new EcsFrameworkException($"The {entity} is null."); + else + throw new EcsFrameworkException($"The {entity} is not alive."); + } } } diff --git a/src/entlong.cs b/src/entlong.cs index ff49d76..8f54c97 100644 --- a/src/entlong.cs +++ b/src/entlong.cs @@ -1,10 +1,10 @@ #pragma warning disable IDE1006 +using DCFApixels.DragonECS.Internal; using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using static DCFApixels.DragonECS.EcsThrowHalper; namespace DCFApixels.DragonECS {