From 78731622df8c14a591c8fc3827a6d4c3d53cccc9 Mon Sep 17 00:00:00 2001 From: DCFApixels <99481254+DCFApixels@users.noreply.github.com> Date: Mon, 6 Jan 2025 10:48:39 +0800 Subject: [PATCH] update --- src/Collections/EcsGroup.cs | 3 -- src/DebugUtils/TypeMeta.cs | 2 +- src/EcsMask.cs | 5 ++-- src/EcsRunner.cs | 6 ++-- src/EcsWorld.pools.cs | 31 +++++++++++++------- src/Injections/Injector.cs | 8 +++--- src/Pools/EcsPool.cs | 2 +- src/Pools/EcsPoolBase.cs | 56 +++++++++++++++++++++++++++++++------ 8 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/Collections/EcsGroup.cs b/src/Collections/EcsGroup.cs index 15d056b..7ef02b5 100644 --- a/src/Collections/EcsGroup.cs +++ b/src/Collections/EcsGroup.cs @@ -1399,9 +1399,6 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] private void MarkEntity_Internal(int entityID) { - //throw new NotImplementedException(); - //_sparse[entityID] |= int.MinValue; - _dense[IndexOf(entityID)] |= int.MinValue; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/DebugUtils/TypeMeta.cs b/src/DebugUtils/TypeMeta.cs index 58a981f..b352c1b 100644 --- a/src/DebugUtils/TypeMeta.cs +++ b/src/DebugUtils/TypeMeta.cs @@ -521,7 +521,7 @@ namespace DCFApixels.DragonECS if (type.IsGenericType && type.IsGenericTypeDefinition == false) { 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; } diff --git a/src/EcsMask.cs b/src/EcsMask.cs index 4a0ba6f..ef0355e 100644 --- a/src/EcsMask.cs +++ b/src/EcsMask.cs @@ -440,9 +440,10 @@ namespace DCFApixels.DragonECS public readonly int chunkIndex; 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; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/EcsRunner.cs b/src/EcsRunner.cs index 4df6abf..4f6902e 100644 --- a/src/EcsRunner.cs +++ b/src/EcsRunner.cs @@ -23,7 +23,7 @@ namespace DCFApixels.DragonECS #region CheckRunnerValide 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. Type targetInterface = processInterfaceType; 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)}."); } #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 } @@ -123,7 +123,7 @@ namespace DCFApixels.DragonECS protected virtual void OnSetup() { } #endregion - #region Simple + #region RunHelper public struct RunHelper { private readonly EcsProcess _process; diff --git a/src/EcsWorld.pools.cs b/src/EcsWorld.pools.cs index bd8e13a..8c9e2f0 100644 --- a/src/EcsWorld.pools.cs +++ b/src/EcsWorld.pools.cs @@ -10,9 +10,11 @@ namespace DCFApixels.DragonECS { private SparseArray _poolTypeCode_2_CmpTypeIDs = new SparseArray(); private SparseArray _cmpTypeCode_2_CmpTypeIDs = new SparseArray(); - private int _poolsCount; + internal IEcsPoolImplementation[] _pools; internal PoolSlot[] _poolSlots; + private int _poolsCount; + #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS private int _lockedPoolCount = 0; #endif @@ -154,18 +156,21 @@ namespace DCFApixels.DragonECS } private TPool FindOrAutoCreatePool() where TPool : IEcsPoolImplementation, new() { - int poolTypeCode = (int)EcsTypeCodeManager.Get(); - if (_poolTypeCode_2_CmpTypeIDs.TryGetValue(poolTypeCode, out int cmpTypeID)) + lock (_worldLock) { - var pool = _pools[cmpTypeID]; + int poolTypeCode = (int)EcsTypeCodeManager.Get(); + if (_poolTypeCode_2_CmpTypeIDs.TryGetValue(poolTypeCode, out int cmpTypeID)) + { + var pool = _pools[cmpTypeID]; #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - if ((pool is TPool) == false) { Throw.UndefinedException(); } + if ((pool is TPool) == false) { Throw.UndefinedException(); } #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) { @@ -243,7 +248,8 @@ namespace DCFApixels.DragonECS _pools[componentTypeID] = newPool; newPool.OnInit(this, _poolsMediator, componentTypeID); - //return newPool; + + OnPoolInitialized?.Invoke(newPool); } } #endregion @@ -461,5 +467,10 @@ namespace DCFApixels.DragonECS } } #endregion + + #region Events + public delegate void OnPoolInitializedHandler(IEcsPool pool); + public event OnPoolInitializedHandler OnPoolInitialized; + #endregion } } diff --git a/src/Injections/Injector.cs b/src/Injections/Injector.cs index a8842e9..cf82f7d 100644 --- a/src/Injections/Injector.cs +++ b/src/Injections/Injector.cs @@ -180,9 +180,9 @@ namespace DCFApixels.DragonECS } public EcsPipeline.Builder Inject(T obj) { - if(obj is EcsWorld objWorld) + if (obj is EcsWorld objWorld) { - if(_monoWorld == null) + if (_monoWorld == null) { _monoWorld = objWorld; } @@ -192,11 +192,11 @@ namespace DCFApixels.DragonECS Type objWorldType = objWorld.GetType(); if (monoWorldType != objWorldType) { - if(objWorldType == typeof(EcsWorld)) + if (objWorldType == typeof(EcsWorld)) { // Екземпляр EcsWorld имеет самый больший приоритет. _monoWorld = objWorld; } - if(objWorldType == typeof(EcsDefaultWorld) && + if (objWorldType == typeof(EcsDefaultWorld) && monoWorldType != typeof(EcsWorld)) { // Екземпляр EcsDefaultWorld имеет приоритет больше других типов, но меньше приоритета EcsWorld. _monoWorld = objWorld; diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index ad62937..e37f1d4 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -164,7 +164,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(int entityID) { - return _mapping[entityID] > 0; + return _mapping[entityID] != 0; } public void Del(int entityID) { diff --git a/src/Pools/EcsPoolBase.cs b/src/Pools/EcsPoolBase.cs index de4b7c0..cb2d6ec 100644 --- a/src/Pools/EcsPoolBase.cs +++ b/src/Pools/EcsPoolBase.cs @@ -7,7 +7,7 @@ using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS.PoolsCore { - /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. + /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. public interface IEcsPoolImplementation : IEcsPool { #region Methods @@ -19,37 +19,50 @@ namespace DCFApixels.DragonECS.PoolsCore #endregion } - /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. - /// Component type + /// Only used to implement a custom pool. In other contexts use IEcsPool or IEcsPool. + /// Component type. public interface IEcsPoolImplementation : IEcsPoolImplementation { } + #region 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(int entityID) { throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName()}."); } + [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotHaveComponent(int entityID) { throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName()}."); } + [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowAlreadyHasComponent(Type type, int entityID) { throw new EcsFrameworkException($"Entity({entityID}) already has component {EcsDebugUtility.GetGenericTypeName(type)}."); } + [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNotHaveComponent(Type type, int entityID) { throw new EcsFrameworkException($"Entity({entityID}) has no component {EcsDebugUtility.GetGenericTypeName(type)}."); } + [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowNullListener() { throw new ArgumentNullException("listener is null"); } + [MethodImpl(MethodImplOptions.NoInlining)] public static void ThrowPoolLocked() { throw new EcsFrameworkException("The pool is currently locked and cannot add or remove components."); } } + #endregion } namespace DCFApixels.DragonECS.Internal @@ -63,6 +76,7 @@ namespace DCFApixels.DragonECS.Internal public sealed class EcsNullPool : IEcsPoolImplementation { public static readonly EcsNullPool instance = new EcsNullPool(); + #region Properties int IEcsReadonlyPool.ComponentTypeID { get { return 0; } }//TODO Првоерить что NullComponent всегда имеет id 0 Type IEcsReadonlyPool.ComponentType { get { return typeof(NullComponent); } } @@ -256,7 +270,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void InvokeOnAdd(this List 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)] @@ -282,7 +296,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void InvokeOnGet(this List 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)] @@ -293,16 +307,30 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void InvokeOnDel(this List 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 self, int entityID) + { + for (int i = 0; i < self.Count; i++) { self[i].OnAdd(entityID); } + } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void InvokeOnAdd(this StructList 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 self, int entityID) + { + for (int i = 0; i < self.Count; i++) + { + self[i].OnAdd(entityID); + self[i].OnGet(entityID); + } } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void InvokeOnAddAndGet(this StructList self, int entityID, int cachedCount) @@ -314,14 +342,24 @@ namespace DCFApixels.DragonECS } } [MethodImpl(MethodImplOptions.AggressiveInlining)] + internal static void InvokeOnGet(this StructList self, int entityID) + { + for (int i = 1; i < self.Count; i++) { self[i].OnGet(entityID); } + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void InvokeOnGet(this StructList 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 self, int entityID) + { + for (int i = 0; i < self.Count; i++) { self[i].OnDel(entityID); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static void InvokeOnDel(this StructList 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