From 935127219fbcd71f946be85fa6de15fac7f4a9c8 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Wed, 1 May 2024 16:58:54 +0800 Subject: [PATCH] add DISABLE_POOLS_EVENTS define --- src/Consts.cs | 8 +++++++- src/Pools/EcsPool.cs | 16 ++++++++++++++++ src/Pools/EcsPoolBase.cs | 4 ++++ src/Pools/EcsTagPool.cs | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Consts.cs b/src/Consts.cs index 86291a8..189a460 100644 --- a/src/Consts.cs +++ b/src/Consts.cs @@ -22,7 +22,13 @@ public const int MAGIC_PRIME = 314159; /// defs - + + public const bool DISABLE_POOLS_EVENTS = +#if DISABLE_POOLS_EVENTS + true; +#else + false; +#endif public const bool ENABLE_DRAGONECS_DEBUGGER = #if ENABLE_DRAGONECS_DEBUGGER true; diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index b2a307e..c7037a5 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -37,8 +37,10 @@ namespace DCFApixels.DragonECS private IEcsComponentCopy _componentCopyHandler = EcsComponentCopyHandler.instance; private bool _isHasComponentCopyHandler = EcsComponentCopyHandler.isHasHandler; +#if !DISABLE_POOLS_EVENTS private List _listeners = new List(); private int _listenersCachedCount = 0; +#endif private EcsWorld.PoolsMediator _mediator; @@ -91,7 +93,9 @@ namespace DCFApixels.DragonECS } _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); EnableComponent(ref _items[itemIndex]); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnAddAndGet(entityID, _listenersCachedCount); +#endif return ref _items[itemIndex]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -100,7 +104,9 @@ namespace DCFApixels.DragonECS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (!Has(entityID)) { EcsPoolThrowHalper.ThrowNotHaveComponent(entityID); } #endif +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnGet(entityID, _listenersCachedCount); +#endif return ref _items[_mapping[entityID]]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -131,9 +137,13 @@ namespace DCFApixels.DragonECS } _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); EnableComponent(ref _items[itemIndex]); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnAdd(entityID, _listenersCachedCount); +#endif } +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnGet(entityID, _listenersCachedCount); +#endif return ref _items[itemIndex]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -156,7 +166,9 @@ namespace DCFApixels.DragonECS itemIndex = 0; _itemsCount--; _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnDel(entityID, _listenersCachedCount); +#endif } public void TryDel(int entityID) { @@ -191,7 +203,9 @@ namespace DCFApixels.DragonECS DisableComponent(ref _items[itemIndex]); itemIndex = 0; _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnDel(entityID, _listenersCachedCount); +#endif } } #endregion @@ -239,6 +253,7 @@ namespace DCFApixels.DragonECS #endregion #region Listeners +#if !DISABLE_POOLS_EVENTS public void AddListener(IEcsPoolEventListener listener) { if (listener == null) { EcsPoolThrowHalper.ThrowNullListener(); } @@ -253,6 +268,7 @@ namespace DCFApixels.DragonECS _listenersCachedCount--; } } +#endif #endregion #region Enable/Disable/Copy diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index 372e1f2..759fc79 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -156,10 +156,12 @@ namespace DCFApixels.DragonECS void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID); #endregion +#if !DISABLE_POOLS_EVENTS #region Add/Remove Listeners void AddListener(IEcsPoolEventListener listener); void RemoveListener(IEcsPoolEventListener listener); #endregion +#endif } public interface IEcsPool : IEcsReadonlyPool { @@ -218,6 +220,7 @@ namespace DCFApixels.DragonECS /// Called after deleting an entity from the pool void OnDel(int entityID); } +#if !DISABLE_POOLS_EVENTS public static class PoolEventListExtensions { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -268,5 +271,6 @@ namespace DCFApixels.DragonECS for (int i = 0; i < cachedCount; i++) self[i].OnDel(entityID); } } +#endif #endregion } diff --git a/src/Pools/EcsTagPool.cs b/src/Pools/EcsTagPool.cs index 11abac4..9a4d50e 100644 --- a/src/Pools/EcsTagPool.cs +++ b/src/Pools/EcsTagPool.cs @@ -29,8 +29,10 @@ namespace DCFApixels.DragonECS private bool[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID private int _count = 0; +#if !DISABLE_POOLS_EVENTS private List _listeners = new List(); private int _listenersCachedCount = 0; +#endif private T _fakeComponent; private EcsWorld.PoolsMediator _mediator; @@ -86,7 +88,9 @@ namespace DCFApixels.DragonECS _count++; _mapping[entityID] = true; _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnAdd(entityID, _listenersCachedCount); +#endif } public void TryAdd(int entityID) { @@ -108,7 +112,9 @@ namespace DCFApixels.DragonECS _mapping[entityID] = false; _count--; _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnDel(entityID, _listenersCachedCount); +#endif } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void TryDel(int entityID) @@ -166,7 +172,9 @@ namespace DCFApixels.DragonECS { _mapping[entityID] = false; _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); +#if !DISABLE_POOLS_EVENTS _listeners.InvokeOnDel(entityID, _listenersCachedCount); +#endif } } #endregion @@ -237,6 +245,7 @@ namespace DCFApixels.DragonECS #endregion #region Listeners +#if !DISABLE_POOLS_EVENTS public void AddListener(IEcsPoolEventListener listener) { if (listener == null) { EcsPoolThrowHalper.ThrowNullListener(); } @@ -251,6 +260,7 @@ namespace DCFApixels.DragonECS _listenersCachedCount--; } } +#endif #endregion #region IEnumerator - IntelliSense hack