refactoring

This commit is contained in:
Mikhail 2024-10-05 18:05:33 +08:00
parent f9449f441f
commit e361fe98c5
7 changed files with 77 additions and 106 deletions

View File

@ -252,8 +252,12 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region operators #region operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(EcsLongsSpan left, EcsLongsSpan right) { return left._source == right._source; } 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; } 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 #endregion
#region Enumerator #region Enumerator
@ -296,8 +300,6 @@ namespace DCFApixels.DragonECS
[EditorBrowsable(EditorBrowsableState.Never)] [EditorBrowsable(EditorBrowsableState.Never)]
public override int GetHashCode() { throw new NotSupportedException(); } public override int GetHashCode() { throw new NotSupportedException(); }
#pragma warning restore CS0809 // Устаревший член переопределяет неустаревший член #pragma warning restore CS0809 // Устаревший член переопределяет неустаревший член
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator EcsSpan(EcsLongsSpan a) { return a.ToSpan(); }
#endregion #endregion
} }
} }

View File

@ -370,6 +370,8 @@ namespace DCFApixels.DragonECS
private MarkerData[] _stopwatchs = new MarkerData[64]; private MarkerData[] _stopwatchs = new MarkerData[64];
private char[] _buffer = new char[128]; private char[] _buffer = new char[128];
private object _lock = new object();
public DefaultDebugService() public DefaultDebugService()
{ {
Console.ForegroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.White;
@ -406,11 +408,14 @@ namespace DCFApixels.DragonECS
} }
public sealed override void Break() public sealed override void Break()
{ {
var color = Console.ForegroundColor; lock (_lock)
Console.ForegroundColor = ConsoleColor.Cyan; {
Console.WriteLine("Press Enter to сontinue."); var color = Console.ForegroundColor;
Console.ReadKey(); Console.ForegroundColor = ConsoleColor.Cyan;
Console.ForegroundColor = color; Console.WriteLine("Press Enter to сontinue.");
Console.ForegroundColor = color;
Console.ReadKey();
}
} }
public sealed override void ProfilerMarkBegin(int id) public sealed override void ProfilerMarkBegin(int id)

View File

@ -165,23 +165,6 @@ namespace DCFApixels.DragonECS
return _world.GetPoolInstance<TPool>(); return _world.GetPoolInstance<TPool>();
} }
public void Include<T>()
{
IncludeImplicit(typeof(T));
}
public void Exclude<T>()
{
ExcludeImplicit(typeof(T));
}
public void Include(Type type)
{
IncludeImplicit(type);
}
public void Exclude(Type type)
{
ExcludeImplicit(type);
}
private void IncludeImplicit(Type type) private void IncludeImplicit(Type type)
{ {
_maskBuilder.Inc(type); _maskBuilder.Inc(type);

View File

@ -11,11 +11,15 @@ using Unity.IL2CPP.CompilerServices;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {
public interface IEcsComponentMask
{
EcsMask ToMask(EcsWorld world);
}
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)] [Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
public sealed class EcsStaticMask : IEquatable<EcsStaticMask> public sealed class EcsStaticMask : IEquatable<EcsStaticMask>, IEcsComponentMask
{ {
private static ConcurrentDictionary<Key, EcsStaticMask> _ids = new ConcurrentDictionary<Key, EcsStaticMask>(); private static ConcurrentDictionary<Key, EcsStaticMask> _ids = new ConcurrentDictionary<Key, EcsStaticMask>();
private static IdDispenser _idDIspenser = new IdDispenser(nullID: 0); private static IdDispenser _idDIspenser = new IdDispenser(nullID: 0);
@ -187,7 +191,7 @@ namespace DCFApixels.DragonECS
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
[DebuggerTypeProxy(typeof(DebuggerProxy))] [DebuggerTypeProxy(typeof(DebuggerProxy))]
public sealed class EcsMask : IEquatable<EcsMask> public sealed class EcsMask : IEquatable<EcsMask>, IEcsComponentMask
{ {
internal readonly int _id; internal readonly int _id;
internal readonly short _worldID; internal readonly short _worldID;
@ -398,6 +402,7 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Other #region Other
EcsMask IEcsComponentMask.ToMask(EcsWorld world) { return this; }
public EcsMaskIterator GetIterator() public EcsMaskIterator GetIterator()
{ {
if (_iterator == null) if (_iterator == null)
@ -1001,7 +1006,9 @@ namespace DCFApixels.DragonECS
private ReadOnlySpan<int>.Enumerator _span; private ReadOnlySpan<int>.Enumerator _span;
private readonly int[] _entityComponentMasks; private readonly int[] _entityComponentMasks;
[ThreadStatic]
private static EcsMaskChunck* _preSortedIncBuffer; private static EcsMaskChunck* _preSortedIncBuffer;
[ThreadStatic]
private static EcsMaskChunck* _preSortedExcBuffer; private static EcsMaskChunck* _preSortedExcBuffer;
private UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer; private UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer;

View File

@ -72,6 +72,10 @@ namespace DCFApixels.DragonECS
private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>(); private List<IEcsEntityEventListener> _entityListeners = new List<IEcsEntityEventListener>();
#region Properties #region Properties
EcsWorld IEntityStorage.World
{
get { return this; }
}
public IConfigContainer Configs public IConfigContainer Configs
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -7,30 +7,18 @@ namespace DCFApixels.DragonECS
public partial class EcsWorld public partial class EcsWorld
{ {
private readonly Dictionary<(Type, object), EcsQueryExecutor> _executorCoures = new Dictionary<(Type, object), EcsQueryExecutor>(256); private readonly Dictionary<(Type, object), EcsQueryExecutor> _executorCoures = new Dictionary<(Type, object), EcsQueryExecutor>(256);
public TExecutor GetExecutor<TExecutor>(EcsMask mask) public TExecutor GetExecutor<TExecutor>(IEcsComponentMask mask)
where TExecutor : EcsQueryExecutor, new() where TExecutor : EcsQueryExecutor, new()
{ {
var coreType = typeof(TExecutor); var coreType = typeof(TExecutor);
if (_executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutor core) == false) if (_executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutor core) == false)
{ {
core = new TExecutor(); core = new TExecutor();
core.Initialize(this, mask); core.Initialize(this, mask.ToMask(this));
_executorCoures.Add((coreType, mask), core); _executorCoures.Add((coreType, mask), core);
} }
return (TExecutor)core; return (TExecutor)core;
} }
public TExecutorCore GetExecutor<TExecutorCore>(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 public abstract class EcsQueryExecutor
{ {

View File

@ -5,6 +5,7 @@ namespace DCFApixels.DragonECS
{ {
public interface IEntityStorage public interface IEntityStorage
{ {
EcsWorld World { get; }
EcsSpan ToSpan(); EcsSpan ToSpan();
} }
public static class Queries public static class Queries
@ -14,6 +15,11 @@ namespace DCFApixels.DragonECS
where TAspect : EcsAspect, new() where TAspect : EcsAspect, new()
where TCollection : IEntityStorage 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); return entities.ToSpan().Where(out aspect);
} }
public static EcsSpan Where<TAspect>(this EcsReadonlyGroup group, out TAspect aspect) public static EcsSpan Where<TAspect>(this EcsReadonlyGroup group, out TAspect aspect)
@ -24,38 +30,27 @@ namespace DCFApixels.DragonECS
public static EcsSpan Where<TAspect>(this EcsSpan span, out TAspect aspect) public static EcsSpan Where<TAspect>(this EcsSpan span, out TAspect aspect)
where TAspect : EcsAspect, new() where TAspect : EcsAspect, new()
{ {
EcsWorld world = span.World; span.World.GetQueryCache(out EcsWhereExecutor executor, out aspect);
world.GetQueryCache(out EcsWhereExecutor executor, out aspect);
return executor.ExecuteFor(span); return executor.ExecuteFor(span);
} }
public static EcsSpan Where<TCollection>(this TCollection entities, EcsStaticMask mask)
public static EcsSpan Where<TCollection>(this TCollection entities, IEcsComponentMask mask)
where TCollection : IEntityStorage where TCollection : IEntityStorage
{ {
if (ReferenceEquals(entities, entities.World))
{
var executor = entities.World.GetExecutor<EcsWhereExecutor>(mask);
return executor.Execute();
}
return entities.ToSpan().Where(mask); 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); 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 = span.World.GetExecutor<EcsWhereExecutor>(mask);
var executor = world.GetExecutor<EcsWhereExecutor>(mask);
return executor.ExecuteFor(span);
}
public static EcsSpan Where<TCollection>(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<EcsWhereExecutor>(mask);
return executor.ExecuteFor(span); return executor.ExecuteFor(span);
} }
#endregion #endregion
@ -65,6 +60,11 @@ namespace DCFApixels.DragonECS
where TAspect : EcsAspect, new() where TAspect : EcsAspect, new()
where TCollection : IEntityStorage 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); return entities.ToSpan().Where(out aspect, comparison);
} }
public static EcsSpan Where<TAspect>(this EcsReadonlyGroup group, out TAspect aspect, Comparison<int> comparison) public static EcsSpan Where<TAspect>(this EcsReadonlyGroup group, out TAspect aspect, Comparison<int> comparison)
@ -75,40 +75,29 @@ namespace DCFApixels.DragonECS
public static EcsSpan Where<TAspect>(this EcsSpan span, out TAspect aspect, Comparison<int> comparison) public static EcsSpan Where<TAspect>(this EcsSpan span, out TAspect aspect, Comparison<int> comparison)
where TAspect : EcsAspect, new() where TAspect : EcsAspect, new()
{ {
EcsWorld world = span.World; span.World.GetQueryCache(out EcsWhereExecutor executor, out aspect);
world.GetQueryCache(out EcsWhereExecutor executor, out aspect);
return executor.ExecuteFor(span, comparison); return executor.ExecuteFor(span, comparison);
} }
public static EcsSpan Where<TCollection>(this TCollection entities, EcsStaticMask mask, Comparison<int> comparison)
public static EcsSpan Where<TCollection>(this TCollection entities, IEcsComponentMask mask, Comparison<int> comparison)
where TCollection : IEntityStorage where TCollection : IEntityStorage
{ {
if (ReferenceEquals(entities, entities.World))
{
EcsWhereExecutor executor = entities.World.GetExecutor<EcsWhereExecutor>(mask);
return executor.Execute(comparison);
}
return entities.ToSpan().Where(mask, comparison); return entities.ToSpan().Where(mask, comparison);
} }
public static EcsSpan Where(this EcsReadonlyGroup group, EcsStaticMask mask, Comparison<int> comparison) public static EcsSpan Where(this EcsReadonlyGroup group, IEcsComponentMask mask, Comparison<int> comparison)
{ {
return group.ToSpan().Where(mask, comparison); return group.ToSpan().Where(mask, comparison);
} }
public static EcsSpan Where(this EcsSpan span, EcsStaticMask mask, Comparison<int> comparison) public static EcsSpan Where(this EcsSpan span, IEcsComponentMask mask, Comparison<int> comparison)
{ {
EcsWorld world = span.World; var executor = span.World.GetExecutor<EcsWhereExecutor>(mask);
var executor = world.GetExecutor<EcsWhereExecutor>(mask);
return executor.ExecuteFor(span); return executor.ExecuteFor(span);
} }
public static EcsSpan Where<TCollection>(this TCollection entities, EcsMask mask, Comparison<int> comparison)
where TCollection : IEntityStorage
{
return entities.ToSpan().Where(mask, comparison);
}
public static EcsSpan Where(this EcsReadonlyGroup group, EcsMask mask, Comparison<int> comparison)
{
return group.ToSpan().Where(mask, comparison);
}
public static EcsSpan Where(this EcsSpan span, EcsMask mask, Comparison<int> comparison)
{
EcsWorld world = span.World;
var executor = world.GetExecutor<EcsWhereExecutor>(mask);
return executor.ExecuteFor(span, comparison);
}
#endregion #endregion
#region WhereToGroup #region WhereToGroup
@ -116,6 +105,11 @@ namespace DCFApixels.DragonECS
where TAspect : EcsAspect, new() where TAspect : EcsAspect, new()
where TCollection : IEntityStorage 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); return entities.ToSpan().WhereToGroup(out aspect);
} }
public static EcsReadonlyGroup WhereToGroup<TAspect>(this EcsReadonlyGroup group, out TAspect aspect) public static EcsReadonlyGroup WhereToGroup<TAspect>(this EcsReadonlyGroup group, out TAspect aspect)
@ -126,39 +120,27 @@ namespace DCFApixels.DragonECS
public static EcsReadonlyGroup WhereToGroup<TAspect>(this EcsSpan span, out TAspect aspect) public static EcsReadonlyGroup WhereToGroup<TAspect>(this EcsSpan span, out TAspect aspect)
where TAspect : EcsAspect, new() where TAspect : EcsAspect, new()
{ {
EcsWorld world = span.World; span.World.GetQueryCache(out EcsWhereToGroupExecutor executor, out aspect);
world.GetQueryCache(out EcsWhereToGroupExecutor executor, out aspect);
return executor.ExecuteFor(span);
}
public static EcsReadonlyGroup WhereToGroup<TCollection>(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<EcsWhereToGroupExecutor>(mask);
return executor.ExecuteFor(span); return executor.ExecuteFor(span);
} }
public static EcsReadonlyGroup WhereToGroup<TCollection>(this TCollection entities, EcsMask mask) public static EcsReadonlyGroup WhereToGroup<TCollection>(this TCollection entities, IEcsComponentMask mask)
where TCollection : IEntityStorage where TCollection : IEntityStorage
{ {
if (ReferenceEquals(entities, entities.World))
{
EcsWhereToGroupExecutor executor = entities.World.GetExecutor<EcsWhereToGroupExecutor>(mask);
return executor.Execute();
}
return entities.ToSpan().WhereToGroup(mask); 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); 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 = span.World.GetExecutor<EcsWhereToGroupExecutor>(mask);
var executor = world.GetExecutor<EcsWhereToGroupExecutor>(mask);
return executor.ExecuteFor(span); return executor.ExecuteFor(span);
} }
#endregion #endregion