diff --git a/src/EcsWorld.cache.cs b/src/EcsWorld.cache.cs index 086fead..9da136a 100644 --- a/src/EcsWorld.cache.cs +++ b/src/EcsWorld.cache.cs @@ -7,8 +7,8 @@ namespace DCFApixels.DragonECS internal readonly struct PoolCache : IEcsWorldComponent> where T : IEcsPoolImplementation, new() { - public readonly T instance; - public PoolCache(T instance) { this.instance = instance; } + public readonly T Instance; + public PoolCache(T instance) { Instance = instance; } void IEcsWorldComponent>.Init(ref PoolCache component, EcsWorld world) { component = new PoolCache(world.CreatePool()); @@ -21,8 +21,8 @@ namespace DCFApixels.DragonECS internal readonly struct AspectCache : IEcsWorldComponent> where T : EcsAspect, new() { - public readonly T instance; - public AspectCache(T instance) { this.instance = instance; } + public readonly T Instance; + public AspectCache(T instance) { Instance = instance; } void IEcsWorldComponent>.Init(ref AspectCache component, EcsWorld world) { component = new AspectCache(EcsAspect.Builder.New(world)); @@ -32,18 +32,25 @@ namespace DCFApixels.DragonECS component = default; } } - internal readonly struct QueryCache : IEcsWorldComponent> - where T : EcsQueryCache, new() + internal readonly struct QueryCache : IEcsWorldComponent> + where TExecutor : EcsQueryExecutor, new() + where TAspcet : EcsAspect, new() { - public readonly T instance; - public QueryCache(T instance) { this.instance = instance; } - void IEcsWorldComponent>.Init(ref QueryCache component, EcsWorld world) + public readonly TExecutor Executor; + public readonly TAspcet Aspcet; + public QueryCache(TExecutor executor, TAspcet aspcet) { - T instance = new T(); - instance.Initialize(world, world._executorsMediator); - component = new QueryCache(instance); + Executor = executor; + Aspcet = aspcet; } - void IEcsWorldComponent>.OnDestroy(ref QueryCache component, EcsWorld world) + void IEcsWorldComponent>.Init(ref QueryCache component, EcsWorld world) + { + TExecutor instance = new TExecutor(); + TAspcet aspect = world.GetAspect(); + instance.Initialize(world, aspect.Mask); + component = new QueryCache(instance, aspect); + } + void IEcsWorldComponent>.OnDestroy(ref QueryCache component, EcsWorld world) { component = default; } diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index d4db734..e550948 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -165,7 +165,6 @@ namespace DCFApixels.DragonECS _worlds[worldID] = this; _poolsMediator = new PoolsMediator(this); - _executorsMediator = new ExecutorMediator(this); int poolsCapacity = ArrayUtility.NormalizeSizeToPowerOfTwo(config.PoolsCapacity); _pools = new IEcsPoolImplementation[poolsCapacity]; @@ -222,12 +221,16 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public TAspect GetAspect() where TAspect : EcsAspect, new() { - return Get>().instance; + return Get>().Instance; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public TExecutor GetQueryCache() where TExecutor : EcsQueryCache, new() + public void GetQueryCache(out TExecutor executor, out TAspect aspect) + where TExecutor : EcsQueryExecutor, new() + where TAspect : EcsAspect, new() { - return Get>().instance; + ref var cmp = ref Get>(); + executor = cmp.Executor; + aspect = cmp.Aspcet; } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/EcsWorld.pools.cs b/src/EcsWorld.pools.cs index 3e14724..2a87a4a 100644 --- a/src/EcsWorld.pools.cs +++ b/src/EcsWorld.pools.cs @@ -65,7 +65,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public TPool GetPoolInstance() where TPool : IEcsPoolImplementation, new() { - return Get>().instance; + return Get>().Instance; } #if UNITY_2020_3_OR_NEWER [UnityEngine.Scripting.Preserve] @@ -73,7 +73,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public TPool GetPoolInstanceUnchecked() where TPool : IEcsPoolImplementation, new() { - return GetUnchecked>().instance; + return GetUnchecked>().Instance; } #if UNITY_2020_3_OR_NEWER [UnityEngine.Scripting.Preserve] @@ -81,7 +81,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TPool GetPoolInstance(int worldID) where TPool : IEcsPoolImplementation, new() { - return Get>(worldID).instance; + return Get>(worldID).Instance; } #if UNITY_2020_3_OR_NEWER [UnityEngine.Scripting.Preserve] @@ -89,7 +89,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TPool GetPoolInstanceUnchecked(int worldID) where TPool : IEcsPoolImplementation, new() { - return GetUnchecked>(worldID).instance; + return GetUnchecked>(worldID).Instance; } #endregion diff --git a/src/Executors/EcsQueryExecutor.cs b/src/Executors/EcsQueryExecutor.cs index 00a78ea..c046794 100644 --- a/src/Executors/EcsQueryExecutor.cs +++ b/src/Executors/EcsQueryExecutor.cs @@ -7,42 +7,29 @@ namespace DCFApixels.DragonECS public partial class EcsWorld { private readonly Dictionary<(Type, object), EcsQueryExecutor> _executorCoures = new Dictionary<(Type, object), EcsQueryExecutor>(256); - private readonly ExecutorMediator _executorsMediator; - public readonly struct ExecutorMediator + public TExecutor GetExecutor(EcsMask mask) + where TExecutor : EcsQueryExecutor, new() { - public readonly EcsWorld World; - internal ExecutorMediator(EcsWorld world) + var coreType = typeof(TExecutor); + if (_executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutor core) == false) { - if (world == null || world._executorsMediator.World != null) - { - throw new InvalidOperationException(); - } - World = world; + core = new TExecutor(); + core.Initialize(this, mask); + _executorCoures.Add((coreType, mask), core); } - public TExecutorCore GetCore(EcsMask mask) - where TExecutorCore : EcsQueryExecutor, new() + return (TExecutor)core; + } + public TExecutorCore GetExecutor(EcsStaticMask staticMask) + where TExecutorCore : EcsQueryExecutor, new() + { + var coreType = typeof(TExecutorCore); + if (_executorCoures.TryGetValue((coreType, staticMask), out EcsQueryExecutor core) == false) { - var coreType = typeof(TExecutorCore); - if (World._executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutor core) == false) - { - core = new TExecutorCore(); - core.Initialize(World, mask); - World._executorCoures.Add((coreType, mask), core); - } - return (TExecutorCore)core; - } - public TExecutorCore GetCore(EcsStaticMask staticMask) - where TExecutorCore : EcsQueryExecutor, new() - { - var coreType = typeof(TExecutorCore); - if (World._executorCoures.TryGetValue((coreType, staticMask), out EcsQueryExecutor core) == false) - { - core = new TExecutorCore(); - core.Initialize(World, staticMask.ToMask(World)); - World._executorCoures.Add((coreType, staticMask), core); - } - return (TExecutorCore)core; + core = new TExecutorCore(); + core.Initialize(this, staticMask.ToMask(this)); + _executorCoures.Add((coreType, staticMask), core); } + return (TExecutorCore)core; } } public abstract class EcsQueryExecutor @@ -64,6 +51,7 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return _mask; } } + public abstract long Version { get; } internal void Initialize(EcsWorld world, EcsMask mask) { _source = world; @@ -78,40 +66,6 @@ namespace DCFApixels.DragonECS protected abstract void OnInitialize(); protected abstract void OnDestroy(); } - public abstract class EcsQueryCache - { - private EcsWorld _source; - private EcsWorld.ExecutorMediator _mediator; - public short WorldID - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _source.id; } - } - public EcsWorld World - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _source; } - } - protected EcsWorld.ExecutorMediator Mediator - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _mediator; } - } - public abstract long Version { get; } - internal void Initialize(EcsWorld world, EcsWorld.ExecutorMediator mediator) - { - _source = world; - _mediator = mediator; - OnInitialize(); - } - internal void Destroy() - { - OnDestroy(); - _source = null; - } - protected abstract void OnInitialize(); - protected abstract void OnDestroy(); - } public readonly struct PoolVersionsChecker { diff --git a/src/Executors/EcsWhereExecutor.cs b/src/Executors/EcsWhereExecutor.cs index eab1a37..5f3fb77 100644 --- a/src/Executors/EcsWhereExecutor.cs +++ b/src/Executors/EcsWhereExecutor.cs @@ -1,5 +1,4 @@ -using DCFApixels.DragonECS.Internal; -using System; +using System; using System.Runtime.CompilerServices; #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; @@ -25,7 +24,7 @@ namespace DCFApixels.DragonECS.Internal private PoolVersionsChecker _versionsChecker; #region Properties - public long Version + public sealed override long Version { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return _version; } @@ -96,62 +95,4 @@ namespace DCFApixels.DragonECS.Internal } #endregion } -} - -namespace DCFApixels.DragonECS -{ -#if ENABLE_IL2CPP - [Il2CppSetOption(Option.NullChecks, false)] - [Il2CppSetOption(Option.ArrayBoundsChecks, false)] -#endif - public sealed class EcsWhereCache : EcsQueryCache where TAspect : EcsAspect, new() - { - private TAspect _aspect; - private EcsWhereExecutor _executor; - - #region Properties - public TAspect Aspect - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _aspect; } - } - public sealed override long Version - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _executor.Version; } - } - #endregion - - #region OnInitialize/OnDestroy - protected sealed override void OnInitialize() - { - _aspect = World.GetAspect(); - _executor = Mediator.GetCore(_aspect.Mask); - } - protected sealed override void OnDestroy() { } - #endregion - - #region Methods - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan Execute() - { - return _executor.Execute(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan ExecuteFor(EcsSpan span) - { - return _executor.ExecuteFor(span); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan Execute(Comparison comparison) - { - return _executor.Execute(comparison); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan ExecuteFor(EcsSpan span, Comparison comparison) - { - return _executor.ExecuteFor(span, comparison); - } - #endregion - } -} +} \ No newline at end of file diff --git a/src/Executors/EcsWhereToGroupExecutor.cs b/src/Executors/EcsWhereToGroupExecutor.cs index 893e01c..884c268 100644 --- a/src/Executors/EcsWhereToGroupExecutor.cs +++ b/src/Executors/EcsWhereToGroupExecutor.cs @@ -1,5 +1,4 @@ -using DCFApixels.DragonECS.Internal; -using System.Runtime.CompilerServices; +using System.Runtime.CompilerServices; #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; #endif @@ -22,7 +21,7 @@ namespace DCFApixels.DragonECS.Internal private PoolVersionsChecker _versionsChecker; #region Properties - public long Version + public sealed override long Version { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return _version; } @@ -82,55 +81,4 @@ namespace DCFApixels.DragonECS.Internal } #endregion } -} - -namespace DCFApixels.DragonECS -{ -#if ENABLE_IL2CPP - [Il2CppSetOption(Option.NullChecks, false)] - [Il2CppSetOption(Option.ArrayBoundsChecks, false)] -#endif - public sealed class EcsWhereToGroupCache : EcsQueryCache where TAspect : EcsAspect, new() - { - private TAspect _aspect; - private EcsWhereToGroupExecutor _executor; - - #region Properties - public TAspect Aspect - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _aspect; } - } - public sealed override long Version - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _executor.Version; } - } - #endregion - - #region OnInitialize/OnDestroy - protected sealed override void OnInitialize() - { - _aspect = World.GetAspect(); - _executor = Mediator.GetCore(_aspect.Mask); - } - protected sealed override void OnDestroy() - { - _executor.Destroy(); - } - #endregion - - #region Methods - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsReadonlyGroup Execute() - { - return _executor.Execute(); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsReadonlyGroup ExecuteFor(EcsSpan span) - { - return _executor.ExecuteFor(span); - } - #endregion - } -} +} \ No newline at end of file diff --git a/src/Executors/Queries.cs b/src/Executors/Queries.cs index d518e8d..98069c6 100644 --- a/src/Executors/Queries.cs +++ b/src/Executors/Queries.cs @@ -1,4 +1,5 @@ -using System; +using DCFApixels.DragonECS.Internal; +using System; namespace DCFApixels.DragonECS { @@ -24,14 +25,39 @@ namespace DCFApixels.DragonECS where TAspect : EcsAspect, new() { EcsWorld world = span.World; - var executor = world.GetQueryCache>(); - aspect = executor.Aspect; + world.GetQueryCache(out EcsWhereExecutor executor, out aspect); + return executor.ExecuteFor(span); + } + public static EcsSpan Where(this TCollection entities, EcsStaticMask mask) + where TCollection : IEntityStorage + { + return entities.ToSpan().Where(mask); + } + public static EcsSpan Where(this EcsReadonlyGroup group, EcsStaticMask mask) + { + return group.ToSpan().Where(mask); + } + public static EcsSpan Where(this EcsSpan span, EcsStaticMask mask) + { + EcsWorld world = span.World; + var executor = world.GetExecutor(mask); + return executor.ExecuteFor(span); + } + public static EcsSpan Where(this TCollection entities, EcsMask mask) + where TCollection : IEntityStorage + { + return entities.ToSpan().Where(mask); + } + public static EcsSpan Where(this EcsReadonlyGroup group, EcsMask mask) + { + return group.ToSpan().Where(mask); + } + public static EcsSpan Where(this EcsSpan span, EcsMask mask) + { + EcsWorld world = span.World; + var executor = world.GetExecutor(mask); return executor.ExecuteFor(span); } - //public static EcsSpan Where(this TCollection entities, EcsStaticMask mask) - //{ - // - //} #endregion #region Where with sort @@ -50,8 +76,37 @@ namespace DCFApixels.DragonECS where TAspect : EcsAspect, new() { EcsWorld world = span.World; - var executor = world.GetQueryCache>(); - aspect = executor.Aspect; + world.GetQueryCache(out EcsWhereExecutor executor, out aspect); + return executor.ExecuteFor(span, comparison); + } + public static EcsSpan Where(this TCollection entities, EcsStaticMask mask, Comparison comparison) + where TCollection : IEntityStorage + { + return entities.ToSpan().Where(mask, comparison); + } + public static EcsSpan Where(this EcsReadonlyGroup group, EcsStaticMask mask, Comparison comparison) + { + return group.ToSpan().Where(mask, comparison); + } + public static EcsSpan Where(this EcsSpan span, EcsStaticMask mask, Comparison comparison) + { + EcsWorld world = span.World; + var executor = world.GetExecutor(mask); + return executor.ExecuteFor(span); + } + public static EcsSpan Where(this TCollection entities, EcsMask mask, Comparison comparison) + where TCollection : IEntityStorage + { + return entities.ToSpan().Where(mask, comparison); + } + public static EcsSpan Where(this EcsReadonlyGroup group, EcsMask mask, Comparison comparison) + { + return group.ToSpan().Where(mask, comparison); + } + public static EcsSpan Where(this EcsSpan span, EcsMask mask, Comparison comparison) + { + EcsWorld world = span.World; + var executor = world.GetExecutor(mask); return executor.ExecuteFor(span, comparison); } #endregion @@ -72,10 +127,40 @@ namespace DCFApixels.DragonECS where TAspect : EcsAspect, new() { EcsWorld world = span.World; - var executor = world.GetQueryCache>(); - aspect = executor.Aspect; + world.GetQueryCache(out EcsWhereToGroupExecutor executor, out aspect); + return executor.ExecuteFor(span); + } + public static EcsReadonlyGroup WhereToGroup(this TCollection entities, EcsStaticMask mask) + where TCollection : IEntityStorage + { + return entities.ToSpan().WhereToGroup(mask); + } + public static EcsReadonlyGroup WhereToGroup(this EcsReadonlyGroup group, EcsStaticMask mask) + { + return group.ToSpan().WhereToGroup(mask); + } + public static EcsReadonlyGroup WhereToGroup(this EcsSpan span, EcsStaticMask mask) + { + EcsWorld world = span.World; + var executor = world.GetExecutor(mask); + return executor.ExecuteFor(span); + } + + public static EcsReadonlyGroup WhereToGroup(this TCollection entities, EcsMask mask) + where TCollection : IEntityStorage + { + return entities.ToSpan().WhereToGroup(mask); + } + public static EcsReadonlyGroup WhereToGroup(this EcsReadonlyGroup group, EcsMask mask) + { + return group.ToSpan().WhereToGroup(mask); + } + public static EcsReadonlyGroup WhereToGroup(this EcsSpan span, EcsMask mask) + { + EcsWorld world = span.World; + var executor = world.GetExecutor(mask); return executor.ExecuteFor(span); } #endregion } -} +} \ No newline at end of file