DragonECS/src/Executors/EcsWhereExecutor.cs

172 lines
5.7 KiB
C#
Raw Normal View History

2024-08-05 11:13:13 +08:00
using DCFApixels.DragonECS.Internal;
2024-08-24 12:29:58 +08:00
using System;
2024-08-05 11:13:13 +08:00
using System.Runtime.CompilerServices;
2024-08-24 12:29:58 +08:00
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
#endif
2023-12-24 15:40:19 +08:00
2024-08-24 12:29:58 +08:00
namespace DCFApixels.DragonECS.Internal
{
2024-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-08-24 12:29:58 +08:00
internal readonly struct EcsWhereExecutorCoreList : IEcsWorldComponent<EcsWhereExecutorCoreList>
2023-12-24 15:40:19 +08:00
{
2024-08-24 12:29:58 +08:00
internal readonly EcsWhereExecutorCore[] _cores;
public EcsWhereExecutorCoreList(EcsWhereExecutorCore[] cores)
{
_cores = cores;
}
public void Init(ref EcsWhereExecutorCoreList component, EcsWorld world)
{
component = new EcsWhereExecutorCoreList(new EcsWhereExecutorCore[64]);
}
public void OnDestroy(ref EcsWhereExecutorCoreList component, EcsWorld world)
{
component = default;
}
}
2023-12-24 15:40:19 +08:00
2024-08-24 12:29:58 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal class EcsWhereExecutorCore
{
private EcsWorld _source;
private EcsMaskIterator _iterator;
private int[] _filteredEntities = new int[32];
private int _filteredEntitiesCount = 0;
private long _lastWorldVersion = 0;
2024-08-23 22:31:43 +08:00
private PoolVersionsChecker _versionsChecker;
2023-12-24 15:40:19 +08:00
#region Properties
2024-08-24 12:29:58 +08:00
public long Version
2023-12-24 15:40:19 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-24 12:29:58 +08:00
get { return _lastWorldVersion; }
2023-12-24 15:40:19 +08:00
}
#endregion
2024-08-24 12:29:58 +08:00
#region Constructors
public EcsWhereExecutorCore(EcsWorld source, EcsAspect aspect)
2023-12-24 15:40:19 +08:00
{
2024-08-24 12:29:58 +08:00
_source = source;
_versionsChecker = new PoolVersionsChecker(aspect.Mask);
2023-12-24 15:40:19 +08:00
}
#endregion
#region Methods
2024-08-23 22:31:43 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void Filter(EcsSpan span)
{
2024-08-24 12:29:58 +08:00
_filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities);
_lastWorldVersion = _source.Version;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute()
{
return ExecuteFor(_source.Entities);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
if (span.WorldID != _source.id) { Throw.Quiery_ArgumentDifferentWorldsException(); }
#endif
_source.ReleaseDelEntityBufferAllAuto();
if (_lastWorldVersion != _source.Version || _versionsChecker.NextEquals() == false)
{
Filter(span);
}
return new EcsSpan(_source.id, _filteredEntities, _filteredEntitiesCount);
2024-08-23 22:31:43 +08:00
}
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public EcsSpan Execute(Comparison<int> comparison)
// {
// return ExecuteFor(_aspect.World.Entities, comparison);
// }
// [MethodImpl(MethodImplOptions.AggressiveInlining)]
// public EcsSpan ExecuteFor(EcsSpan span, Comparison<int> comparison)
// {
//#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
// if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
// if (span.WorldID != WorldID) { Throw.Quiery_ArgumentDifferentWorldsException(); }
//#endif
// World.ReleaseDelEntityBufferAllAuto();
// if (_lastWorldVersion != World.Version)
// {
// Filter(span);
// }
// Array.Sort<int>(_filteredEntities, 0, _filteredEntitiesCount, comparison);
// }
2024-08-24 12:29:58 +08:00
#endregion
}
}
namespace DCFApixels.DragonECS
{
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
public sealed class EcsWhereExecutor<TAspect> : EcsQueryExecutor where TAspect : EcsAspect, new()
{
private TAspect _aspect;
private EcsWhereExecutorCore _core;
#region Properties
public TAspect Aspect
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _aspect; }
}
public sealed override long Version
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _core.Version; }
}
#endregion
2024-08-23 22:31:43 +08:00
2024-08-24 12:29:58 +08:00
#region OnInitialize/OnDestroy
protected sealed override void OnInitialize()
{
_aspect = World.GetAspect<TAspect>();
int maskID = _aspect.Mask.ID;
ref var list = ref World.Get<EcsWhereExecutorCoreList>();
var cores = list._cores;
if (maskID >= list._cores.Length)
{
Array.Resize(ref cores, cores.Length << 1);
list = new EcsWhereExecutorCoreList(cores);
}
ref var coreRef = ref cores[maskID];
if (coreRef == null)
{
coreRef = new EcsWhereExecutorCore(World, _aspect);
}
_core = coreRef;
}
protected sealed override void OnDestroy() { }
#endregion
#region Methods
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-25 23:05:00 +08:00
public EcsSpan Execute()
{
2024-08-24 12:29:58 +08:00
return _core.Execute();
2024-02-25 23:05:00 +08:00
}
2023-12-24 15:40:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span)
2023-12-24 15:40:19 +08:00
{
2024-08-24 12:29:58 +08:00
return _core.ExecuteFor(span);
2023-12-24 15:40:19 +08:00
}
#endregion
}
}