diff --git a/src/Collections/EcsSpan.cs b/src/Collections/EcsSpan.cs index f70910d..d7fc288 100644 --- a/src/Collections/EcsSpan.cs +++ b/src/Collections/EcsSpan.cs @@ -252,8 +252,12 @@ namespace DCFApixels.DragonECS #endregion #region operators + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator ==(EcsLongsSpan left, EcsLongsSpan right) { return left._source == right._source; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool operator !=(EcsLongsSpan left, EcsLongsSpan right) { return left._source != right._source; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator EcsSpan(EcsLongsSpan a) { return a.ToSpan(); } #endregion #region Enumerator @@ -296,8 +300,6 @@ namespace DCFApixels.DragonECS [EditorBrowsable(EditorBrowsableState.Never)] public override int GetHashCode() { throw new NotSupportedException(); } #pragma warning restore CS0809 // Устаревший член переопределяет неустаревший член - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator EcsSpan(EcsLongsSpan a) { return a.ToSpan(); } #endregion } } \ No newline at end of file diff --git a/src/DebugUtils/EcsDebug.cs b/src/DebugUtils/EcsDebug.cs index 172c63a..050f3f4 100644 --- a/src/DebugUtils/EcsDebug.cs +++ b/src/DebugUtils/EcsDebug.cs @@ -370,6 +370,8 @@ namespace DCFApixels.DragonECS private MarkerData[] _stopwatchs = new MarkerData[64]; private char[] _buffer = new char[128]; + private object _lock = new object(); + public DefaultDebugService() { Console.ForegroundColor = ConsoleColor.White; @@ -406,11 +408,14 @@ namespace DCFApixels.DragonECS } public sealed override void Break() { - var color = Console.ForegroundColor; - Console.ForegroundColor = ConsoleColor.Cyan; - Console.WriteLine("Press Enter to сontinue."); - Console.ReadKey(); - Console.ForegroundColor = color; + lock (_lock) + { + var color = Console.ForegroundColor; + Console.ForegroundColor = ConsoleColor.Cyan; + Console.WriteLine("Press Enter to сontinue."); + Console.ForegroundColor = color; + Console.ReadKey(); + } } public sealed override void ProfilerMarkBegin(int id) diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 7589aef..dc11329 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -165,23 +165,6 @@ namespace DCFApixels.DragonECS return _world.GetPoolInstance(); } - public void Include() - { - IncludeImplicit(typeof(T)); - } - public void Exclude() - { - ExcludeImplicit(typeof(T)); - } - public void Include(Type type) - { - IncludeImplicit(type); - } - public void Exclude(Type type) - { - ExcludeImplicit(type); - } - private void IncludeImplicit(Type type) { _maskBuilder.Inc(type); diff --git a/src/EcsMask.cs b/src/EcsMask.cs index 550b64a..94b2c8d 100644 --- a/src/EcsMask.cs +++ b/src/EcsMask.cs @@ -11,11 +11,15 @@ using Unity.IL2CPP.CompilerServices; namespace DCFApixels.DragonECS { + public interface IEcsComponentMask + { + EcsMask ToMask(EcsWorld world); + } #if ENABLE_IL2CPP [Il2CppSetOption (Option.NullChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)] #endif - public sealed class EcsStaticMask : IEquatable + public sealed class EcsStaticMask : IEquatable, IEcsComponentMask { private static ConcurrentDictionary _ids = new ConcurrentDictionary(); private static IdDispenser _idDIspenser = new IdDispenser(nullID: 0); @@ -187,7 +191,7 @@ namespace DCFApixels.DragonECS [Il2CppSetOption(Option.ArrayBoundsChecks, false)] #endif [DebuggerTypeProxy(typeof(DebuggerProxy))] - public sealed class EcsMask : IEquatable + public sealed class EcsMask : IEquatable, IEcsComponentMask { internal readonly int _id; internal readonly short _worldID; @@ -398,6 +402,7 @@ namespace DCFApixels.DragonECS #endregion #region Other + EcsMask IEcsComponentMask.ToMask(EcsWorld world) { return this; } public EcsMaskIterator GetIterator() { if (_iterator == null) @@ -1001,7 +1006,9 @@ namespace DCFApixels.DragonECS private ReadOnlySpan.Enumerator _span; private readonly int[] _entityComponentMasks; + [ThreadStatic] private static EcsMaskChunck* _preSortedIncBuffer; + [ThreadStatic] private static EcsMaskChunck* _preSortedExcBuffer; private UnsafeArray _sortIncChunckBuffer; diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index e550948..ed96818 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -72,6 +72,10 @@ namespace DCFApixels.DragonECS private List _entityListeners = new List(); #region Properties + EcsWorld IEntityStorage.World + { + get { return this; } + } public IConfigContainer Configs { [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Executors/EcsQueryExecutor.cs b/src/Executors/EcsQueryExecutor.cs index c046794..0e7ee76 100644 --- a/src/Executors/EcsQueryExecutor.cs +++ b/src/Executors/EcsQueryExecutor.cs @@ -7,30 +7,18 @@ namespace DCFApixels.DragonECS public partial class EcsWorld { private readonly Dictionary<(Type, object), EcsQueryExecutor> _executorCoures = new Dictionary<(Type, object), EcsQueryExecutor>(256); - public TExecutor GetExecutor(EcsMask mask) + public TExecutor GetExecutor(IEcsComponentMask mask) where TExecutor : EcsQueryExecutor, new() { var coreType = typeof(TExecutor); if (_executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutor core) == false) { core = new TExecutor(); - core.Initialize(this, mask); + core.Initialize(this, mask.ToMask(this)); _executorCoures.Add((coreType, mask), core); } 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) - { - core = new TExecutorCore(); - core.Initialize(this, staticMask.ToMask(this)); - _executorCoures.Add((coreType, staticMask), core); - } - return (TExecutorCore)core; - } } public abstract class EcsQueryExecutor { diff --git a/src/Executors/Queries.cs b/src/Executors/Queries.cs index 98069c6..bce01fb 100644 --- a/src/Executors/Queries.cs +++ b/src/Executors/Queries.cs @@ -5,6 +5,7 @@ namespace DCFApixels.DragonECS { public interface IEntityStorage { + EcsWorld World { get; } EcsSpan ToSpan(); } public static class Queries @@ -14,6 +15,11 @@ namespace DCFApixels.DragonECS where TAspect : EcsAspect, new() where TCollection : IEntityStorage { + if(ReferenceEquals(entities, entities.World)) + { + entities.World.GetQueryCache(out EcsWhereExecutor executor, out aspect); + return executor.Execute(); + } return entities.ToSpan().Where(out aspect); } public static EcsSpan Where(this EcsReadonlyGroup group, out TAspect aspect) @@ -24,38 +30,27 @@ namespace DCFApixels.DragonECS public static EcsSpan Where(this EcsSpan span, out TAspect aspect) where TAspect : EcsAspect, new() { - EcsWorld world = span.World; - world.GetQueryCache(out EcsWhereExecutor executor, out aspect); + span.World.GetQueryCache(out EcsWhereExecutor executor, out aspect); return executor.ExecuteFor(span); } - public static EcsSpan Where(this TCollection entities, EcsStaticMask mask) + + public static EcsSpan Where(this TCollection entities, IEcsComponentMask mask) where TCollection : IEntityStorage { + if (ReferenceEquals(entities, entities.World)) + { + var executor = entities.World.GetExecutor(mask); + return executor.Execute(); + } return entities.ToSpan().Where(mask); } - public static EcsSpan Where(this EcsReadonlyGroup group, EcsStaticMask mask) + public static EcsSpan Where(this EcsReadonlyGroup group, IEcsComponentMask mask) { return group.ToSpan().Where(mask); } - public static EcsSpan Where(this EcsSpan span, EcsStaticMask mask) + public static EcsSpan Where(this EcsSpan span, IEcsComponentMask 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); + var executor = span.World.GetExecutor(mask); return executor.ExecuteFor(span); } #endregion @@ -65,6 +60,11 @@ namespace DCFApixels.DragonECS where TAspect : EcsAspect, new() where TCollection : IEntityStorage { + if (ReferenceEquals(entities, entities.World)) + { + entities.World.GetQueryCache(out EcsWhereExecutor executor, out aspect); + return executor.Execute(comparison); + } return entities.ToSpan().Where(out aspect, comparison); } public static EcsSpan Where(this EcsReadonlyGroup group, out TAspect aspect, Comparison comparison) @@ -75,40 +75,29 @@ namespace DCFApixels.DragonECS public static EcsSpan Where(this EcsSpan span, out TAspect aspect, Comparison comparison) where TAspect : EcsAspect, new() { - EcsWorld world = span.World; - world.GetQueryCache(out EcsWhereExecutor executor, out aspect); + span.World.GetQueryCache(out EcsWhereExecutor executor, out aspect); return executor.ExecuteFor(span, comparison); } - public static EcsSpan Where(this TCollection entities, EcsStaticMask mask, Comparison comparison) + + public static EcsSpan Where(this TCollection entities, IEcsComponentMask mask, Comparison comparison) where TCollection : IEntityStorage { + if (ReferenceEquals(entities, entities.World)) + { + EcsWhereExecutor executor = entities.World.GetExecutor(mask); + return executor.Execute(comparison); + } return entities.ToSpan().Where(mask, comparison); } - public static EcsSpan Where(this EcsReadonlyGroup group, EcsStaticMask mask, Comparison comparison) + public static EcsSpan Where(this EcsReadonlyGroup group, IEcsComponentMask mask, Comparison comparison) { return group.ToSpan().Where(mask, comparison); } - public static EcsSpan Where(this EcsSpan span, EcsStaticMask mask, Comparison comparison) + public static EcsSpan Where(this EcsSpan span, IEcsComponentMask mask, Comparison comparison) { - EcsWorld world = span.World; - var executor = world.GetExecutor(mask); + var executor = span.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 #region WhereToGroup @@ -116,6 +105,11 @@ namespace DCFApixels.DragonECS where TAspect : EcsAspect, new() where TCollection : IEntityStorage { + if (ReferenceEquals(entities, entities.World)) + { + entities.World.GetQueryCache(out EcsWhereToGroupExecutor executor, out aspect); + return executor.Execute(); + } return entities.ToSpan().WhereToGroup(out aspect); } public static EcsReadonlyGroup WhereToGroup(this EcsReadonlyGroup group, out TAspect aspect) @@ -126,39 +120,27 @@ namespace DCFApixels.DragonECS public static EcsReadonlyGroup WhereToGroup(this EcsSpan span, out TAspect aspect) where TAspect : EcsAspect, new() { - EcsWorld world = span.World; - 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); + span.World.GetQueryCache(out EcsWhereToGroupExecutor executor, out aspect); return executor.ExecuteFor(span); } - public static EcsReadonlyGroup WhereToGroup(this TCollection entities, EcsMask mask) + public static EcsReadonlyGroup WhereToGroup(this TCollection entities, IEcsComponentMask mask) where TCollection : IEntityStorage { + if (ReferenceEquals(entities, entities.World)) + { + EcsWhereToGroupExecutor executor = entities.World.GetExecutor(mask); + return executor.Execute(); + } return entities.ToSpan().WhereToGroup(mask); } - public static EcsReadonlyGroup WhereToGroup(this EcsReadonlyGroup group, EcsMask mask) + public static EcsReadonlyGroup WhereToGroup(this EcsReadonlyGroup group, IEcsComponentMask mask) { return group.ToSpan().WhereToGroup(mask); } - public static EcsReadonlyGroup WhereToGroup(this EcsSpan span, EcsMask mask) + public static EcsReadonlyGroup WhereToGroup(this EcsSpan span, IEcsComponentMask mask) { - EcsWorld world = span.World; - var executor = world.GetExecutor(mask); + var executor = span.World.GetExecutor(mask); return executor.ExecuteFor(span); } #endregion