mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-19 02:24:37 +08:00
463 lines
17 KiB
C#
463 lines
17 KiB
C#
#if DISABLE_DEBUG
|
||
#undef DEBUG
|
||
#endif
|
||
using DCFApixels.DragonECS.Core;
|
||
using DCFApixels.DragonECS.Internal;
|
||
using DCFApixels.DragonECS.PoolsCore;
|
||
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Diagnostics;
|
||
using System.Runtime.CompilerServices;
|
||
#if ENABLE_IL2CPP
|
||
using Unity.IL2CPP.CompilerServices;
|
||
#endif
|
||
|
||
namespace DCFApixels.DragonECS
|
||
{
|
||
/// <summary>Standard component</summary>
|
||
[MetaColor(MetaColor.DragonRose)]
|
||
[MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)]
|
||
[MetaDescription(EcsConsts.AUTHOR, "Standard component.")]
|
||
[MetaID("84D2537C9201D6F6B92FEC1C8883A07A")]
|
||
public interface IEcsComponent : IEcsMember { }
|
||
|
||
/// <summary>Pool for IEcsComponent components</summary>
|
||
#if ENABLE_IL2CPP
|
||
[Il2CppSetOption(Option.NullChecks, false)]
|
||
#endif
|
||
[MetaColor(MetaColor.DragonRose)]
|
||
[MetaGroup(EcsConsts.PACK_GROUP, EcsConsts.POOLS_GROUP)]
|
||
[MetaDescription(EcsConsts.AUTHOR, "Pool for IEcsComponent components.")]
|
||
[MetaID("C501547C9201A4B03FC25632E4FAAFD7")]
|
||
[DebuggerDisplay("Count: {Count} Type: {ComponentType}")]
|
||
public sealed class EcsPool<T> : IEcsPoolImplementation<T>, IEcsStructPool<T>, IEntityStorage, IEnumerable<T> //IEnumerable<T> - IntelliSense hack
|
||
where T : struct, IEcsComponent
|
||
{
|
||
private EcsWorld _source;
|
||
private int _componentTypeID;
|
||
private EcsMaskChunck _maskBit;
|
||
|
||
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
|
||
private T[] _items; //dense
|
||
private int _itemsCount = 0;
|
||
private int _capacity = 0;
|
||
|
||
private int[] _sparseEntities;
|
||
private int[] _denseEntitiesDelayed;
|
||
private int _denseEntitiesDelayedCount = 0;
|
||
private bool _isDenseEntitiesDelayedValid = false;
|
||
|
||
private readonly IEcsComponentLifecycle<T> _componentLifecycleHandler = EcsComponentResetHandler<T>.instance;
|
||
private readonly bool _isHasComponentLifecycleHandler = EcsComponentResetHandler<T>.isHasHandler;
|
||
private readonly IEcsComponentCopy<T> _componentCopyHandler = EcsComponentCopyHandler<T>.instance;
|
||
private readonly bool _isHasComponentCopyHandler = EcsComponentCopyHandler<T>.isHasHandler;
|
||
|
||
#if !DISABLE_POOLS_EVENTS
|
||
private readonly StructList<IEcsPoolEventListener> _listeners = new StructList<IEcsPoolEventListener>(2);
|
||
private int _listenersCachedCount = 0;
|
||
#endif
|
||
private bool _isLocked;
|
||
|
||
private EcsWorld.PoolsMediator _mediator;
|
||
|
||
#region Properites
|
||
public int Count
|
||
{
|
||
get { return _itemsCount; }
|
||
}
|
||
public int Capacity
|
||
{
|
||
get { return _items.Length; }
|
||
}
|
||
public int ComponentTypeID
|
||
{
|
||
get { return _componentTypeID; }
|
||
}
|
||
public Type ComponentType
|
||
{
|
||
get { return typeof(T); }
|
||
}
|
||
public EcsWorld World
|
||
{
|
||
get { return _source; }
|
||
}
|
||
public bool IsReadOnly
|
||
{
|
||
get { return false; }
|
||
}
|
||
#endregion
|
||
|
||
#region Methods
|
||
public ref T Add(int entityID)
|
||
{
|
||
ref int itemIndex = ref _mapping[entityID];
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (itemIndex > 0) { EcsPoolThrowHelper.ThrowAlreadyHasComponent<T>(entityID); }
|
||
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
|
||
#endif
|
||
itemIndex = GetFreeItemIndex(entityID);
|
||
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
|
||
ref T result = ref _items[itemIndex];
|
||
_sparseEntities[itemIndex] = entityID;
|
||
EnableComponent(ref result);
|
||
#if !DISABLE_POOLS_EVENTS
|
||
_listeners.InvokeOnAddAndGet(entityID, _listenersCachedCount);
|
||
#endif
|
||
return ref result;
|
||
}
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public ref T Get(int entityID)
|
||
{
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (!Has(entityID)) { EcsPoolThrowHelper.ThrowNotHaveComponent<T>(entityID); }
|
||
#endif
|
||
#if !DISABLE_POOLS_EVENTS
|
||
_listeners.InvokeOnGet(entityID, _listenersCachedCount);
|
||
#endif
|
||
return ref _items[_mapping[entityID]];
|
||
}
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public ref readonly T Read(int entityID)
|
||
{
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (!Has(entityID)) { EcsPoolThrowHelper.ThrowNotHaveComponent<T>(entityID); }
|
||
#endif
|
||
return ref _items[_mapping[entityID]];
|
||
}
|
||
public ref T TryAddOrGet(int entityID)
|
||
{
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
|
||
#endif
|
||
ref int itemIndex = ref _mapping[entityID];
|
||
if (itemIndex <= 0)
|
||
{ //Add block
|
||
itemIndex = GetFreeItemIndex(entityID);
|
||
_mediator.RegisterComponent(entityID, _componentTypeID, _maskBit);
|
||
_sparseEntities[itemIndex] = entityID;
|
||
EnableComponent(ref _items[itemIndex]);
|
||
#if !DISABLE_POOLS_EVENTS
|
||
_listeners.InvokeOnAdd(entityID, _listenersCachedCount);
|
||
#endif
|
||
} //Add block end
|
||
#if !DISABLE_POOLS_EVENTS
|
||
_listeners.InvokeOnGet(entityID, _listenersCachedCount);
|
||
#endif
|
||
return ref _items[itemIndex];
|
||
}
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public bool Has(int entityID)
|
||
{
|
||
return _mapping[entityID] != 0;
|
||
}
|
||
public void Del(int entityID)
|
||
{
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
|
||
#endif
|
||
ref int itemIndex = ref _mapping[entityID];
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (itemIndex <= 0) { EcsPoolThrowHelper.ThrowNotHaveComponent<T>(entityID); }
|
||
#endif
|
||
DisableComponent(ref _items[itemIndex]);
|
||
_sparseEntities[itemIndex] = 0;
|
||
itemIndex = 0;
|
||
_itemsCount--;
|
||
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
|
||
_isDenseEntitiesDelayedValid = false;
|
||
#if !DISABLE_POOLS_EVENTS
|
||
_listeners.InvokeOnDel(entityID, _listenersCachedCount);
|
||
#endif
|
||
}
|
||
public void TryDel(int entityID)
|
||
{
|
||
if (Has(entityID))
|
||
{
|
||
Del(entityID);
|
||
}
|
||
}
|
||
public void Copy(int fromEntityID, int toEntityID)
|
||
{
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (!Has(fromEntityID)) { EcsPoolThrowHelper.ThrowNotHaveComponent<T>(fromEntityID); }
|
||
#endif
|
||
CopyComponent(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)) { EcsPoolThrowHelper.ThrowNotHaveComponent<T>(fromEntityID); }
|
||
#endif
|
||
CopyComponent(ref Get(fromEntityID), ref toWorld.GetPool<T>().TryAddOrGet(toEntityID));
|
||
}
|
||
|
||
public void ClearAll()
|
||
{
|
||
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
|
||
if (_isLocked) { EcsPoolThrowHelper.ThrowPoolLocked(); }
|
||
#endif
|
||
if (_itemsCount <= 0) { return; }
|
||
_itemsCount = 0;
|
||
var span = _source.Where(out SingleAspect<T> _);
|
||
foreach (var entityID in span)
|
||
{
|
||
ref int itemIndex = ref _mapping[entityID];
|
||
DisableComponent(ref _items[itemIndex]);
|
||
itemIndex = 0;
|
||
_mediator.UnregisterComponent(entityID, _componentTypeID, _maskBit);
|
||
#if !DISABLE_POOLS_EVENTS
|
||
_listeners.InvokeOnDel(entityID, _listenersCachedCount);
|
||
#endif
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region Callbacks
|
||
void IEcsPoolImplementation.OnInit(EcsWorld world, EcsWorld.PoolsMediator mediator, int componentTypeID)
|
||
{
|
||
#if DEBUG
|
||
AllowedInWorldsAttribute.CheckAllows<T>(world);
|
||
#endif
|
||
|
||
_source = world;
|
||
_mediator = mediator;
|
||
_componentTypeID = componentTypeID;
|
||
_maskBit = EcsMaskChunck.FromID(componentTypeID);
|
||
|
||
_mapping = new int[world.Capacity];
|
||
Resize(ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.GetWorldConfigOrDefault().PoolComponentsCapacity));
|
||
//_capacity = ArrayUtility.NormalizeSizeToPowerOfTwo(world.Configs.GetWorldConfigOrDefault().PoolComponentsCapacity);
|
||
//_items = new T[_capacity];
|
||
//_sparseEntities = new int[_capacity];
|
||
//_denseEntitiesDelayed = new int[_capacity];
|
||
//for (int i = 0; i < _capacity; i++)
|
||
//{// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> free index-<2D> <20><><EFBFBD><EFBFBD> _denseEntitiesDelayed <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 0, <20><> free index <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
// _denseEntitiesDelayed[i] = i;
|
||
//}
|
||
}
|
||
void IEcsPoolImplementation.OnWorldResize(int newSize)
|
||
{
|
||
Array.Resize(ref _mapping, newSize);
|
||
}
|
||
void IEcsPoolImplementation.OnWorldDestroy() { }
|
||
void IEcsPoolImplementation.OnReleaseDelEntityBuffer(ReadOnlySpan<int> buffer)
|
||
{
|
||
if (_itemsCount <= 0)
|
||
{
|
||
return;
|
||
}
|
||
foreach (var entityID in buffer)
|
||
{
|
||
TryDel(entityID);
|
||
}
|
||
}
|
||
void IEcsPoolImplementation.OnLockedChanged_Debug(bool locked) { _isLocked = locked; }
|
||
#endregion
|
||
|
||
#region Other
|
||
void IEcsPool.AddEmpty(int entityID) { Add(entityID); }
|
||
void IEcsPool.AddRaw(int entityID, object dataRaw)
|
||
{
|
||
Add(entityID) = dataRaw == null ? default : (T)dataRaw;
|
||
}
|
||
object IEcsReadonlyPool.GetRaw(int entityID) { return Get(entityID); }
|
||
void IEcsPool.SetRaw(int entityID, object dataRaw)
|
||
{
|
||
Get(entityID) = dataRaw == null ? default : (T)dataRaw;
|
||
}
|
||
|
||
|
||
public EcsSpan ToSpan()
|
||
{
|
||
UpdateDenseEntities();
|
||
return new EcsSpan(_source.ID, _denseEntitiesDelayed, 1, _itemsCount);
|
||
}
|
||
private bool IsDenseEntitiesDelayedValid()
|
||
{
|
||
return _isDenseEntitiesDelayedValid;//_itemsCount == _denseEntitiesDelayedCount;
|
||
}
|
||
private void UpdateDenseEntities()
|
||
{
|
||
//if (IsDenseEntitiesDelayedValid()) { return; }
|
||
_denseEntitiesDelayedCount = 0;
|
||
for (int i = 0, jRight = _itemsCount + 1; i < _capacity; i++)
|
||
{
|
||
if (_sparseEntities[i] == 0 && i != 0)
|
||
{
|
||
_denseEntitiesDelayed[jRight] = i;
|
||
jRight++;
|
||
}
|
||
else
|
||
{
|
||
_denseEntitiesDelayed[_denseEntitiesDelayedCount++] = _sparseEntities[i];
|
||
}
|
||
}
|
||
_isDenseEntitiesDelayedValid = true;
|
||
}
|
||
private int GetFreeItemIndex(int entityID)
|
||
{
|
||
//if (_denseEntitiesDelayedCount >= _capacity - 1)
|
||
{
|
||
UpdateDenseEntities();
|
||
}
|
||
|
||
if (_itemsCount >= _capacity - 1)
|
||
{
|
||
Resize(_items.Length << 1);
|
||
}
|
||
|
||
int result = _denseEntitiesDelayed[_denseEntitiesDelayedCount];
|
||
_denseEntitiesDelayed[_denseEntitiesDelayedCount] = entityID;
|
||
_itemsCount++;
|
||
_denseEntitiesDelayedCount++;
|
||
if(result == 0)
|
||
{
|
||
|
||
}
|
||
return result;
|
||
}
|
||
private void CheckOrUpsize()
|
||
{
|
||
if (_itemsCount < _capacity) { return; }
|
||
Resize(_items.Length << 1);
|
||
}
|
||
private void Resize(int newSize)
|
||
{
|
||
if (newSize <= _capacity) { return; }
|
||
ArrayUtility.ResizeOrCreate(ref _sparseEntities, newSize);
|
||
ArrayUtility.ResizeOrCreate(ref _items, newSize);
|
||
_denseEntitiesDelayed = new int[newSize];
|
||
_denseEntitiesDelayedCount = 0;
|
||
_capacity = newSize;
|
||
UpdateDenseEntities();
|
||
}
|
||
#endregion
|
||
|
||
#region Listeners
|
||
#if !DISABLE_POOLS_EVENTS
|
||
public void AddListener(IEcsPoolEventListener listener)
|
||
{
|
||
if (listener == null) { EcsPoolThrowHelper.ThrowNullListener(); }
|
||
_listeners.Add(listener);
|
||
_listenersCachedCount++;
|
||
}
|
||
public void RemoveListener(IEcsPoolEventListener listener)
|
||
{
|
||
if (listener == null) { EcsPoolThrowHelper.ThrowNullListener(); }
|
||
if (_listeners.RemoveWithOrder(listener))
|
||
{
|
||
_listenersCachedCount--;
|
||
}
|
||
}
|
||
#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<T> IEnumerable<T>.GetEnumerator() { throw new NotImplementedException(); }
|
||
IEnumerator IEnumerable.GetEnumerator() { throw new NotImplementedException(); }
|
||
#endregion
|
||
|
||
#region MarkersConverter
|
||
public static implicit operator EcsPool<T>(IncludeMarker a) { return a.GetInstance<EcsPool<T>>(); }
|
||
public static implicit operator EcsPool<T>(ExcludeMarker a) { return a.GetInstance<EcsPool<T>>(); }
|
||
public static implicit operator EcsPool<T>(OptionalMarker a) { return a.GetInstance<EcsPool<T>>(); }
|
||
public static implicit operator EcsPool<T>(EcsWorld.GetPoolInstanceMarker a) { return a.GetInstance<EcsPool<T>>(); }
|
||
#endregion
|
||
}
|
||
|
||
public static class EcsPoolExtensions
|
||
{
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> GetPool<TComponent>(this EcsWorld self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.GetPoolInstance<EcsPool<TComponent>>();
|
||
}
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> GetPoolUnchecked<TComponent>(this EcsWorld self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.GetPoolInstanceUnchecked<EcsPool<TComponent>>();
|
||
}
|
||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> Inc<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.IncludePool<EcsPool<TComponent>>();
|
||
}
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> Exc<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.ExcludePool<EcsPool<TComponent>>();
|
||
}
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> Opt<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.OptionalPool<EcsPool<TComponent>>();
|
||
}
|
||
|
||
#region Obsolete
|
||
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Inc) + "<T>()")]
|
||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> Include<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.IncludePool<EcsPool<TComponent>>();
|
||
}
|
||
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Exc) + "<T>()")]
|
||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> Exclude<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.ExcludePool<EcsPool<TComponent>>();
|
||
}
|
||
[Obsolete("Use " + nameof(EcsAspect) + "." + nameof(EcsAspect.Builder) + "." + nameof(Opt) + "<T>()")]
|
||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||
public static EcsPool<TComponent> Optional<TComponent>(this EcsAspect.Builder self) where TComponent : struct, IEcsComponent
|
||
{
|
||
return self.OptionalPool<EcsPool<TComponent>>();
|
||
}
|
||
#endregion
|
||
}
|
||
}
|