diff --git a/src/systems.cs b/src/systems.cs index a14cd4a..ad7ebaa 100644 --- a/src/systems.cs +++ b/src/systems.cs @@ -164,7 +164,15 @@ public void Init () { public void Run () { for (int i = 0, iMax = _runSystemsCount; i < iMax; i++) { + +#if DEBUG && LEOECSLITE_PROFILESYSTEMS + UnityEngine.Profiling.Profiler.BeginSample(_runSystems[i].GetType().Name); +#endif _runSystems[i].Run (this); +#if DEBUG && LEOECSLITE_PROFILESYSTEMS + UnityEngine.Profiling.Profiler.EndSample(); +#endif + #if DEBUG && !LEOECSLITE_NO_SANITIZE_CHECKS var worldName = CheckForLeakedEntities (); if (worldName != null) { throw new System.Exception ($"Empty entity detected in world \"{worldName}\" after {_runSystems[i].GetType ().Name}.Run()."); } diff --git a/src/worlds.cs b/src/worlds.cs index dfd35fb..fa4aae1 100644 --- a/src/worlds.cs +++ b/src/worlds.cs @@ -6,6 +6,7 @@ using System; using System.Collections.Generic; +using System.Reflection; using System.Runtime.CompilerServices; #if ENABLE_IL2CPP @@ -256,8 +257,35 @@ public IEcsPool GetPoolById (int typeId) { } [MethodImpl (MethodImplOptions.AggressiveInlining)] - public IEcsPool GetPoolByType (Type type) { - return _poolHashes.TryGetValue (type, out var pool) ? pool : null; + public IEcsPool GetPoolByType (Type poolType) { + + if (_poolHashes.TryGetValue (poolType, out var rawPool)) { + return rawPool; + } + + var genericPoolType = typeof(EcsPool<>); + var constructed = genericPoolType.MakeGenericType(poolType); + + var poolConstructors = constructed.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance); + var pool = poolConstructors[0].Invoke(new object[] + { + this, _poolsCount, _poolDenseSize, Entities.Length, _poolRecycledSize + }) as IEcsPool; + + // var pool = Activator.CreateInstance(constructed) as IEcsPool; + // EcsWorld world, int id, int denseCapacity, int sparseCapacity, int recycledCapacity + // var pool = new EcsPool (this, _poolsCount, _poolDenseSize, Entities.Length, _poolRecycledSize); + _poolHashes[poolType] = pool; + if (_poolsCount == _pools.Length) { + var newSize = _poolsCount << 1; + Array.Resize (ref _pools, newSize); + Array.Resize (ref _filtersByIncludedComponents, newSize); + Array.Resize (ref _filtersByExcludedComponents, newSize); + } + _pools[_poolsCount++] = pool; + return pool; + + // return _poolHashes.TryGetValue (type, out var pool) ? pool : null; } public int GetAllEntities (ref int[] entities) { @@ -494,12 +522,18 @@ void Reset () { } [MethodImpl (MethodImplOptions.AggressiveInlining)] - public Mask Inc () where T : struct { - var poolId = _world.GetPool ().GetId (); + public Mask Inc () where T : struct + { + return IncByType(typeof(T)); + } + + [MethodImpl (MethodImplOptions.AggressiveInlining)] + public Mask IncByType(Type type) { + var poolId = _world.GetPoolByType(type).GetId (); #if DEBUG && !LEOECSLITE_NO_SANITIZE_CHECKS if (_built) { throw new Exception ("Cant change built mask."); } - if (Array.IndexOf (Include, poolId, 0, IncludeCount) != -1) { throw new Exception ($"{typeof (T).Name} already in constraints list."); } - if (Array.IndexOf (Exclude, poolId, 0, ExcludeCount) != -1) { throw new Exception ($"{typeof (T).Name} already in constraints list."); } + if (Array.IndexOf (Include, poolId, 0, IncludeCount) != -1) { throw new Exception ($"{type.Name} already in constraints list."); } + if (Array.IndexOf (Exclude, poolId, 0, ExcludeCount) != -1) { throw new Exception ($"{type.Name} already in constraints list."); } #endif if (IncludeCount == Include.Length) { Array.Resize (ref Include, IncludeCount << 1); } Include[IncludeCount++] = poolId; @@ -511,11 +545,19 @@ public Mask Inc () where T : struct { #endif [MethodImpl (MethodImplOptions.AggressiveInlining)] public Mask Exc () where T : struct { - var poolId = _world.GetPool ().GetId (); + return ExcByType(typeof(T)); + } + +#if UNITY_2020_3_OR_NEWER + [UnityEngine.Scripting.Preserve] +#endif + [MethodImpl (MethodImplOptions.AggressiveInlining)] + public Mask ExcByType(Type type) { + var poolId = _world.GetPoolByType(type).GetId (); #if DEBUG && !LEOECSLITE_NO_SANITIZE_CHECKS if (_built) { throw new Exception ("Cant change built mask."); } - if (Array.IndexOf (Include, poolId, 0, IncludeCount) != -1) { throw new Exception ($"{typeof (T).Name} already in constraints list."); } - if (Array.IndexOf (Exclude, poolId, 0, ExcludeCount) != -1) { throw new Exception ($"{typeof (T).Name} already in constraints list."); } + if (Array.IndexOf (Include, poolId, 0, IncludeCount) != -1) { throw new Exception ($"{type.Name} already in constraints list."); } + if (Array.IndexOf (Exclude, poolId, 0, ExcludeCount) != -1) { throw new Exception ($"{type.Name} already in constraints list."); } #endif if (ExcludeCount == Exclude.Length) { Array.Resize (ref Exclude, ExcludeCount << 1); } Exclude[ExcludeCount++] = poolId;