This commit is contained in:
DCFApixels 2025-01-06 10:48:39 +08:00
parent bca69fb167
commit 78731622df
8 changed files with 80 additions and 33 deletions

View File

@ -1399,9 +1399,6 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
private void MarkEntity_Internal(int entityID) private void MarkEntity_Internal(int entityID)
{ {
//throw new NotImplementedException();
//_sparse[entityID] |= int.MinValue;
_dense[IndexOf(entityID)] |= int.MinValue; _dense[IndexOf(entityID)] |= int.MinValue;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -521,7 +521,7 @@ namespace DCFApixels.DragonECS
if (type.IsGenericType && type.IsGenericTypeDefinition == false) if (type.IsGenericType && type.IsGenericTypeDefinition == false)
{ {
var metaIds = type.GetGenericArguments().Select(o => GetMetaID(o)); var metaIds = type.GetGenericArguments().Select(o => GetMetaID(o));
if(metaIds.Any(o => string.IsNullOrEmpty(o))) if (metaIds.Any(o => string.IsNullOrEmpty(o)))
{ {
id = string.Empty; id = string.Empty;
} }

View File

@ -440,9 +440,10 @@ namespace DCFApixels.DragonECS
public readonly int chunkIndex; public readonly int chunkIndex;
public readonly int mask; public readonly int mask;
public EcsMaskChunck(int chankIndex, int mask) [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsMaskChunck(int chunkIndex, int mask)
{ {
this.chunkIndex = chankIndex; this.chunkIndex = chunkIndex;
this.mask = mask; this.mask = mask;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -23,7 +23,7 @@ namespace DCFApixels.DragonECS
#region CheckRunnerValide #region CheckRunnerValide
public static void CheckRunnerTypeIsValide(Type runnerType, Type processInterfaceType) public static void CheckRunnerTypeIsValide(Type runnerType, Type processInterfaceType)
{ {
#region DEBUG #if DEBUG
#pragma warning disable IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations. #pragma warning disable IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.
Type targetInterface = processInterfaceType; Type targetInterface = processInterfaceType;
if (runnerType.IsAbstract || runnerType.IsInterface) if (runnerType.IsAbstract || runnerType.IsInterface)
@ -54,7 +54,7 @@ namespace DCFApixels.DragonECS
throw new EcsRunnerImplementationException($"Runner {GetGenericTypeFullName(runnerType, 1)} does not implement interface {GetGenericTypeFullName(baseTypeArgument, 1)}."); throw new EcsRunnerImplementationException($"Runner {GetGenericTypeFullName(runnerType, 1)} does not implement interface {GetGenericTypeFullName(baseTypeArgument, 1)}.");
} }
#pragma warning restore IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations. #pragma warning restore IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.
#endregion #endif
} }
#endregion #endregion
} }
@ -123,7 +123,7 @@ namespace DCFApixels.DragonECS
protected virtual void OnSetup() { } protected virtual void OnSetup() { }
#endregion #endregion
#region Simple #region RunHelper
public struct RunHelper public struct RunHelper
{ {
private readonly EcsProcess<TProcess> _process; private readonly EcsProcess<TProcess> _process;

View File

@ -10,9 +10,11 @@ namespace DCFApixels.DragonECS
{ {
private SparseArray<int> _poolTypeCode_2_CmpTypeIDs = new SparseArray<int>(); private SparseArray<int> _poolTypeCode_2_CmpTypeIDs = new SparseArray<int>();
private SparseArray<int> _cmpTypeCode_2_CmpTypeIDs = new SparseArray<int>(); private SparseArray<int> _cmpTypeCode_2_CmpTypeIDs = new SparseArray<int>();
private int _poolsCount;
internal IEcsPoolImplementation[] _pools; internal IEcsPoolImplementation[] _pools;
internal PoolSlot[] _poolSlots; internal PoolSlot[] _poolSlots;
private int _poolsCount;
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
private int _lockedPoolCount = 0; private int _lockedPoolCount = 0;
#endif #endif
@ -154,18 +156,21 @@ namespace DCFApixels.DragonECS
} }
private TPool FindOrAutoCreatePool<TPool>() where TPool : IEcsPoolImplementation, new() private TPool FindOrAutoCreatePool<TPool>() where TPool : IEcsPoolImplementation, new()
{ {
int poolTypeCode = (int)EcsTypeCodeManager.Get<TPool>(); lock (_worldLock)
if (_poolTypeCode_2_CmpTypeIDs.TryGetValue(poolTypeCode, out int cmpTypeID))
{ {
var pool = _pools[cmpTypeID]; int poolTypeCode = (int)EcsTypeCodeManager.Get<TPool>();
if (_poolTypeCode_2_CmpTypeIDs.TryGetValue(poolTypeCode, out int cmpTypeID))
{
var pool = _pools[cmpTypeID];
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if ((pool is TPool) == false) { Throw.UndefinedException(); } if ((pool is TPool) == false) { Throw.UndefinedException(); }
#endif #endif
return (TPool)pool; return (TPool)pool;
}
TPool newPool = new TPool();
InitPool_Internal(newPool);
return newPool;
} }
TPool newPool = new TPool();
InitPool_Internal(newPool);
return newPool;
} }
private void InitPool_Internal(IEcsPoolImplementation newPool) private void InitPool_Internal(IEcsPoolImplementation newPool)
{ {
@ -243,7 +248,8 @@ namespace DCFApixels.DragonECS
_pools[componentTypeID] = newPool; _pools[componentTypeID] = newPool;
newPool.OnInit(this, _poolsMediator, componentTypeID); newPool.OnInit(this, _poolsMediator, componentTypeID);
//return newPool;
OnPoolInitialized?.Invoke(newPool);
} }
} }
#endregion #endregion
@ -461,5 +467,10 @@ namespace DCFApixels.DragonECS
} }
} }
#endregion #endregion
#region Events
public delegate void OnPoolInitializedHandler(IEcsPool pool);
public event OnPoolInitializedHandler OnPoolInitialized;
#endregion
} }
} }

View File

@ -180,9 +180,9 @@ namespace DCFApixels.DragonECS
} }
public EcsPipeline.Builder Inject<T>(T obj) public EcsPipeline.Builder Inject<T>(T obj)
{ {
if(obj is EcsWorld objWorld) if (obj is EcsWorld objWorld)
{ {
if(_monoWorld == null) if (_monoWorld == null)
{ {
_monoWorld = objWorld; _monoWorld = objWorld;
} }
@ -192,11 +192,11 @@ namespace DCFApixels.DragonECS
Type objWorldType = objWorld.GetType(); Type objWorldType = objWorld.GetType();
if (monoWorldType != objWorldType) if (monoWorldType != objWorldType)
{ {
if(objWorldType == typeof(EcsWorld)) if (objWorldType == typeof(EcsWorld))
{ // Екземпляр EcsWorld имеет самый больший приоритет. { // Екземпляр EcsWorld имеет самый больший приоритет.
_monoWorld = objWorld; _monoWorld = objWorld;
} }
if(objWorldType == typeof(EcsDefaultWorld) && if (objWorldType == typeof(EcsDefaultWorld) &&
monoWorldType != typeof(EcsWorld)) monoWorldType != typeof(EcsWorld))
{ // Екземпляр EcsDefaultWorld имеет приоритет больше других типов, но меньше приоритета EcsWorld. { // Екземпляр EcsDefaultWorld имеет приоритет больше других типов, но меньше приоритета EcsWorld.
_monoWorld = objWorld; _monoWorld = objWorld;

View File

@ -164,7 +164,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Has(int entityID) public bool Has(int entityID)
{ {
return _mapping[entityID] > 0; return _mapping[entityID] != 0;
} }
public void Del(int entityID) public void Del(int entityID)
{ {

View File

@ -7,7 +7,7 @@ using System.Runtime.CompilerServices;
namespace DCFApixels.DragonECS.PoolsCore namespace DCFApixels.DragonECS.PoolsCore
{ {
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary> /// <summary> Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>. </summary>
public interface IEcsPoolImplementation : IEcsPool public interface IEcsPoolImplementation : IEcsPool
{ {
#region Methods #region Methods
@ -19,37 +19,50 @@ namespace DCFApixels.DragonECS.PoolsCore
#endregion #endregion
} }
/// <summary>Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>.</summary> /// <summary> Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool<T>. </summary>
/// <typeparam name="T">Component type</typeparam> /// <typeparam name="T"> Component type. </typeparam>
public interface IEcsPoolImplementation<T> : IEcsPoolImplementation { } public interface IEcsPoolImplementation<T> : IEcsPoolImplementation { }
#region EcsPoolThrowHelper
public static class EcsPoolThrowHelper public static class EcsPoolThrowHelper
{ {
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowDifferentTypes()
{
throw new EcsFrameworkException($"The component instance type and the pool component type are different.");
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowAlreadyHasComponent<T>(int entityID) public static void ThrowAlreadyHasComponent<T>(int entityID)
{ {
throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName<T>()}."); throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName<T>()}.");
} }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowNotHaveComponent<T>(int entityID) public static void ThrowNotHaveComponent<T>(int entityID)
{ {
throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName<T>()}."); throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName<T>()}.");
} }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowAlreadyHasComponent(Type type, int entityID) public static void ThrowAlreadyHasComponent(Type type, int entityID)
{ {
throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName(type)}."); throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName(type)}.");
} }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowNotHaveComponent(Type type, int entityID) public static void ThrowNotHaveComponent(Type type, int entityID)
{ {
throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName(type)}."); throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName(type)}.");
} }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowNullListener() public static void ThrowNullListener()
{ {
throw new ArgumentNullException("listener is null"); throw new ArgumentNullException("listener is null");
} }
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowPoolLocked() public static void ThrowPoolLocked()
{ {
throw new EcsFrameworkException("The pool is currently locked and cannot add or remove components."); throw new EcsFrameworkException("The pool is currently locked and cannot add or remove components.");
} }
} }
#endregion
} }
namespace DCFApixels.DragonECS.Internal namespace DCFApixels.DragonECS.Internal
@ -63,6 +76,7 @@ namespace DCFApixels.DragonECS.Internal
public sealed class EcsNullPool : IEcsPoolImplementation<NullComponent> public sealed class EcsNullPool : IEcsPoolImplementation<NullComponent>
{ {
public static readonly EcsNullPool instance = new EcsNullPool(); public static readonly EcsNullPool instance = new EcsNullPool();
#region Properties #region Properties
int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0 int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0
Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } } Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } }
@ -256,7 +270,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnAdd(this List<IEcsPoolEventListener> self, int entityID, int cachedCount) public static void InvokeOnAdd(this List<IEcsPoolEventListener> self, int entityID, int cachedCount)
{ {
for (int i = 0; i < cachedCount; i++) self[i].OnAdd(entityID); for (int i = 0; i < cachedCount; i++) { self[i].OnAdd(entityID); }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -282,7 +296,7 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnGet(this List<IEcsPoolEventListener> self, int entityID, int cachedCount) internal static void InvokeOnGet(this List<IEcsPoolEventListener> self, int entityID, int cachedCount)
{ {
for (int i = 1; i < cachedCount; i++) self[i].OnGet(entityID); for (int i = 1; i < cachedCount; i++) { self[i].OnGet(entityID); }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -293,16 +307,30 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void InvokeOnDel(this List<IEcsPoolEventListener> self, int entityID, int cachedCount) public static void InvokeOnDel(this List<IEcsPoolEventListener> self, int entityID, int cachedCount)
{ {
for (int i = 0; i < cachedCount; i++) self[i].OnDel(entityID); for (int i = 0; i < cachedCount; i++) { self[i].OnDel(entityID); }
} }
// //
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnAdd(this StructList<IEcsPoolEventListener> self, int entityID)
{
for (int i = 0; i < self.Count; i++) { self[i].OnAdd(entityID); }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnAdd(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount) internal static void InvokeOnAdd(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount)
{ {
for (int i = 0; i < cachedCount; i++) self[i].OnAdd(entityID); for (int i = 0; i < cachedCount; i++) { self[i].OnAdd(entityID); }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnAddAndGet(this StructList<IEcsPoolEventListener> self, int entityID)
{
for (int i = 0; i < self.Count; i++)
{
self[i].OnAdd(entityID);
self[i].OnGet(entityID);
}
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnAddAndGet(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount) internal static void InvokeOnAddAndGet(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount)
@ -314,14 +342,24 @@ namespace DCFApixels.DragonECS
} }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnGet(this StructList<IEcsPoolEventListener> self, int entityID)
{
for (int i = 1; i < self.Count; i++) { self[i].OnGet(entityID); }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnGet(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount) internal static void InvokeOnGet(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount)
{ {
for (int i = 1; i < cachedCount; i++) self[i].OnGet(entityID); for (int i = 1; i < cachedCount; i++) { self[i].OnGet(entityID); }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnDel(this StructList<IEcsPoolEventListener> self, int entityID)
{
for (int i = 0; i < self.Count; i++) { self[i].OnDel(entityID); }
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void InvokeOnDel(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount) internal static void InvokeOnDel(this StructList<IEcsPoolEventListener> self, int entityID, int cachedCount)
{ {
for (int i = 0; i < cachedCount; i++) self[i].OnDel(entityID); for (int i = 0; i < cachedCount; i++) { self[i].OnDel(entityID); }
} }
} }
#endif #endif