Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/systems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()."); }
Expand Down
60 changes: 51 additions & 9 deletions src/worlds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;

#if ENABLE_IL2CPP
Expand Down Expand Up @@ -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<T> (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) {
Expand Down Expand Up @@ -494,12 +522,18 @@ void Reset () {
}

[MethodImpl (MethodImplOptions.AggressiveInlining)]
public Mask Inc<T> () where T : struct {
var poolId = _world.GetPool<T> ().GetId ();
public Mask Inc<T> () 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;
Expand All @@ -511,11 +545,19 @@ public Mask Inc<T> () where T : struct {
#endif
[MethodImpl (MethodImplOptions.AggressiveInlining)]
public Mask Exc<T> () where T : struct {
var poolId = _world.GetPool<T> ().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;
Expand Down