diff --git a/src/Pools/EcsValuePool.cs b/src/Pools/EcsValuePool.cs index 675e95c..e22d420 100644 --- a/src/Pools/EcsValuePool.cs +++ b/src/Pools/EcsValuePool.cs @@ -47,7 +47,8 @@ namespace DCFApixels.DragonECS public sealed unsafe class EcsValuePool : IEcsPoolImplementation, IEcsStructPool, IEnumerable //IEnumerable - IntelliSense hack where T : unmanaged, IEcsValueComponent { - private EcsWorld _source; + private short _worldID; + private EcsWorld _world; private int _componentTypeID; private EcsMaskChunck _maskBit; @@ -60,10 +61,10 @@ namespace DCFApixels.DragonECS public int _recycledItemsCount; private readonly EcsValuePoolSharedStore* _sharedStore; - private readonly IEcsComponentLifecycle _componentLifecycleHandler = EcsComponentLifecycleHandler.instance; - private readonly bool _isHasComponentLifecycleHandler = EcsComponentLifecycleHandler.isHasHandler; - private readonly IEcsComponentCopy _componentCopyHandler = EcsComponentCopyHandler.instance; - private readonly bool _isHasComponentCopyHandler = EcsComponentCopyHandler.isHasHandler; + private readonly IEcsComponentLifecycle _customLifecycle = EcsComponentLifecycle.CustomHandler; + private readonly bool _isCustomLifecycle = EcsComponentLifecycle.IsCustom; + private readonly IEcsComponentCopy _customCopy = EcsComponentCopy.CustomHandler; + private readonly bool _isCustomCopy = EcsComponentCopy.IsCustom; private bool _isLocked; @@ -88,7 +89,7 @@ namespace DCFApixels.DragonECS } public EcsWorld World { - get { return _source; } + get { return _world; } } public bool IsReadOnly { @@ -106,14 +107,14 @@ namespace DCFApixels.DragonECS { if (_sharedStore == null) { - _sharedStore = MemoryAllocator.Alloc(1).As(); + _sharedStore = MemoryAllocator.Alloc(1).Ptr; } } public EcsValuePool(int capacity, int recycledCapacity = -1) { if (_sharedStore == null) { - _sharedStore = MemoryAllocator.Alloc(1).As(); + _sharedStore = MemoryAllocator.Alloc(1).Ptr; } capacity = ArrayUtility.NextPow2(capacity); if (recycledCapacity < 0) @@ -121,31 +122,31 @@ namespace DCFApixels.DragonECS recycledCapacity = capacity / 2; } _itemsLength = capacity; - _items = MemoryAllocator.Alloc(_itemsLength).As(); + _items = MemoryAllocator.Alloc(_itemsLength).Ptr; _sharedStore->_items = _items; _recycledItemsLength = recycledCapacity; - _recycledItems = MemoryAllocator.Alloc(_recycledItemsLength).As(); + _recycledItems = MemoryAllocator.Alloc(_recycledItemsLength).Ptr; } void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID) { - _source = world; + _world = world; _mediator = mediator; _componentTypeID = componentTypeID; _maskBit = EcsMaskChunck.FromID(componentTypeID); - _mapping = MemoryAllocator.Alloc(world.Capacity).As(); + _mapping = MemoryAllocator.Alloc(world.Capacity).Ptr; _sharedStore->_mapping = _mapping; var worldConfig = world.Configs.GetWorldConfigOrDefault(); if (_items == null) { _itemsLength = ArrayUtility.NextPow2(worldConfig.PoolComponentsCapacity); - _items = MemoryAllocator.Alloc(_itemsLength).As(); + _items = MemoryAllocator.Alloc(_itemsLength).Ptr; _sharedStore->_items = _items; } if (_recycledItems == null) { _recycledItemsLength = worldConfig.PoolRecycledComponentsCapacity; - _recycledItems = MemoryAllocator.Alloc(_recycledItemsLength).As(); + _recycledItems = MemoryAllocator.Alloc(_recycledItemsLength).Ptr; } } void IEcsPoolImplementation.OnWorldDestroy() { } @@ -162,13 +163,13 @@ namespace DCFApixels.DragonECS { ref int itemIndex = ref _mapping[entityID]; #if DEBUG - if (entityID == EcsConsts.NULL_ENTITY_ID) { Throw.Ent_ThrowIsNotAlive(_source, entityID); } - if (_source.IsUsed(entityID) == false) { Throw.Ent_ThrowIsNotAlive(_source, entityID); } + if (entityID == EcsConsts.NULL_ENTITY_ID) { Throw.Ent_ThrowIsNotAlive(_world, entityID); } + if (_world.IsUsed(entityID) == false) { Throw.Ent_ThrowIsNotAlive(_world, entityID); } if (itemIndex > 0) { EcsPoolThrowHelper.ThrowAlreadyHasComponent(entityID); } if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); } #elif DRAGONECS_STABILITY_MODE if (itemIndex > 0) { return ref Get(entityID); } - if (_isLocked | _source.IsUsed(entityID) == false) { return ref _items[0]; } + if (_isLocked | _world.IsUsed(entityID) == false) { return ref _items[0]; } #endif if (_recycledItemsCount > 0) { @@ -181,13 +182,13 @@ namespace DCFApixels.DragonECS if (itemIndex >= _itemsLength) { _itemsLength = ArrayUtility.NextPow2(_itemsLength << 1); - _items = MemoryAllocator.Realloc(_items, _itemsLength).As(); + _items = MemoryAllocator.Realloc(_items, _itemsLength).Ptr; _sharedStore->_items = _items; } } _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); ref T result = ref ((T*)_items)[itemIndex]; - EnableComponent(ref result); + EcsComponentLifecycle.OnAdd(_isCustomLifecycle, _customLifecycle, ref result, _worldID, entityID); return ref result; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -209,7 +210,7 @@ namespace DCFApixels.DragonECS public ref T TryAddOrGet(int entityID) { #if DEBUG - if (entityID == EcsConsts.NULL_ENTITY_ID) { Throw.Ent_ThrowIsNotAlive(_source, entityID); } + if (entityID == EcsConsts.NULL_ENTITY_ID) { Throw.Ent_ThrowIsNotAlive(_world, entityID); } #endif ref int itemIndex = ref _mapping[entityID]; if (itemIndex <= 0) @@ -230,12 +231,12 @@ namespace DCFApixels.DragonECS if (itemIndex >= _itemsLength) { _itemsLength = ArrayUtility.NextPow2(_itemsLength << 1); - _items = MemoryAllocator.Realloc(_items, _itemsLength).As(); + _items = MemoryAllocator.Realloc(_items, _itemsLength).Ptr; _sharedStore->_items = _items; } } _mediator.RegisterComponent(entityID, _componentTypeID, _maskBit); - EnableComponent(ref ((T*)_items)[itemIndex]); + EcsComponentLifecycle.OnAdd(_isCustomLifecycle, _customLifecycle, ref ((T*)_items)[itemIndex], _worldID, entityID); } //Add block end return ref ((T*)_items)[itemIndex]; } @@ -248,18 +249,18 @@ namespace DCFApixels.DragonECS { ref int itemIndex = ref _mapping[entityID]; #if DEBUG - if (entityID == EcsConsts.NULL_ENTITY_ID) { Throw.Ent_ThrowIsNotAlive(_source, entityID); } + if (entityID == EcsConsts.NULL_ENTITY_ID) { Throw.Ent_ThrowIsNotAlive(_world, entityID); } if (itemIndex <= 0) { EcsPoolThrowHelper.ThrowNotHaveComponent(entityID); } if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); } #elif DRAGONECS_STABILITY_MODE if (itemIndex <= 0) { return; } if (_isLocked) { return; } #endif - DisableComponent(ref ((T*)_items)[itemIndex]); + EcsComponentLifecycle.OnDel( _isCustomLifecycle, _customLifecycle, ref ((T*)_items)[itemIndex], _worldID, entityID); if (_recycledItemsCount >= _recycledItemsLength) { _recycledItemsLength = ArrayUtility.NextPow2(_recycledItemsLength << 1); - _recycledItems = MemoryAllocator.Realloc(_recycledItems, _recycledItemsLength).As(); + _recycledItems = MemoryAllocator.Realloc(_recycledItems, _recycledItemsLength).Ptr; } _recycledItems[_recycledItemsCount++] = itemIndex; itemIndex = 0; @@ -280,7 +281,7 @@ namespace DCFApixels.DragonECS #elif DRAGONECS_STABILITY_MODE if (!Has(fromEntityID)) { return; } #endif - CopyComponent(ref Get(fromEntityID), ref TryAddOrGet(toEntityID)); + EcsComponentCopy.Copy(_isCustomCopy, _customCopy, ref Get(fromEntityID), ref TryAddOrGet(toEntityID)); } public void Copy(int fromEntityID, EcsWorld toWorld, int toEntityID) { @@ -289,7 +290,7 @@ namespace DCFApixels.DragonECS #elif DRAGONECS_STABILITY_MODE if (!Has(fromEntityID)) { return; } #endif - CopyComponent(ref Get(fromEntityID), ref toWorld.GetPool().TryAddOrGet(toEntityID)); + EcsComponentCopy.Copy(_isCustomCopy, _customCopy, ref Get(fromEntityID), ref toWorld.GetPool().TryAddOrGet(toEntityID)); } public void ClearAll() @@ -301,11 +302,11 @@ namespace DCFApixels.DragonECS #endif _recycledItemsCount = 0; // спереди чтобы обнулялось, так как Del не обнуляет if (_itemsCount <= 0) { return; } - var span = _source.Where(out SinglePoolAspect> _); + var span = _world.Where(out SinglePoolAspect> _); foreach (var entityID in span) { ref int itemIndex = ref _mapping[entityID]; - DisableComponent(ref ((T*)_items)[itemIndex]); + EcsComponentLifecycle.OnDel(_isCustomLifecycle, _customLifecycle, ref ((T*)_items)[itemIndex], _worldID, entityID); itemIndex = 0; _mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit); } @@ -317,7 +318,7 @@ namespace DCFApixels.DragonECS #region Callbacks void IEcsPoolImplementation.OnWorldResize(int newSize) { - _mapping = MemoryAllocator.Realloc(_mapping, newSize).As(); + _mapping = MemoryAllocator.Realloc(_mapping, newSize).Ptr; _sharedStore->_mapping = _mapping; } void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan buffer) @@ -360,45 +361,6 @@ namespace DCFApixels.DragonECS #endif #endregion - #region Enable/Disable/Copy - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void EnableComponent(ref T component) - { - if (_isHasComponentLifecycleHandler) - { - _componentLifecycleHandler.Enable(ref component); - } - else - { - component = default; - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void DisableComponent(ref T component) - { - if (_isHasComponentLifecycleHandler) - { - _componentLifecycleHandler.Disable(ref component); - } - else - { - component = default; - } - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void CopyComponent(ref T from, ref T to) - { - if (_isHasComponentCopyHandler) - { - _componentCopyHandler.Copy(ref from, ref to); - } - else - { - to = from; - } - } - #endregion - #region IEnumerator - IntelliSense hack IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); } IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }