DragonECS/src/Executors/EcsWhereExecutor.cs

157 lines
5.3 KiB
C#
Raw Normal View History

2025-03-14 16:53:25 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
using DCFApixels.DragonECS.Core;
2024-11-05 15:50:03 +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-11-07 08:22:10 +08:00
internal sealed class EcsWhereExecutor : MaskQueryExecutor
2023-12-24 15:40:19 +08:00
{
2024-08-24 12:29:58 +08:00
private EcsMaskIterator _iterator;
2024-11-05 15:50:03 +08:00
2024-08-24 21:02:18 +08:00
private int[] _filteredAllEntities = new int[32];
private int _filteredAllEntitiesCount = 0;
private int[] _filteredEntities = null;
2024-08-24 12:29:58 +08:00
private int _filteredEntitiesCount = 0;
2024-11-05 15:50:03 +08:00
private long _version;
2024-10-11 18:28:46 +08:00
private WorldStateVersionsChecker _versionsChecker;
2023-12-24 15:40:19 +08:00
2024-11-17 21:43:50 +08:00
public bool _isDestroyed = false;
2023-12-24 15:40:19 +08:00
#region Properties
2024-10-03 08:20:22 +08:00
public sealed override long Version
2023-12-24 15:40:19 +08:00
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-24 21:02:18 +08:00
get { return _version; }
2023-12-24 15:40:19 +08:00
}
2024-11-05 15:50:03 +08:00
public sealed override bool IsCached
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _versionsChecker.Check(); }
}
2024-11-07 08:22:10 +08:00
public sealed override int LastCachedCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _filteredAllEntitiesCount; }
}
2023-12-24 15:40:19 +08:00
#endregion
2024-08-24 13:10:50 +08:00
#region OnInitialize/OnDestroy
protected sealed override void OnInitialize()
2023-12-24 15:40:19 +08:00
{
2024-10-11 18:28:46 +08:00
_versionsChecker = new WorldStateVersionsChecker(Mask);
2024-08-24 13:10:50 +08:00
_iterator = Mask.GetIterator();
2023-12-24 15:40:19 +08:00
}
2024-10-11 18:28:46 +08:00
protected sealed override void OnDestroy()
{
2024-11-17 21:43:50 +08:00
if (_isDestroyed) { return; }
_isDestroyed = true;
2024-10-11 18:28:46 +08:00
_versionsChecker.Dispose();
}
2023-12-24 15:40:19 +08:00
#endregion
#region Methods
2024-08-23 22:31:43 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-24 21:02:18 +08:00
private void Execute_Iternal()
2024-08-23 22:31:43 +08:00
{
2024-08-24 21:02:18 +08:00
World.ReleaseDelEntityBufferAllAuto();
2024-10-11 18:28:46 +08:00
if (_versionsChecker.CheckAndNext() == false)
2024-08-24 21:02:18 +08:00
{
_version++;
2024-10-10 19:57:19 +08:00
_filteredAllEntitiesCount = _iterator.IterateTo(World.Entities, ref _filteredAllEntities);
2024-08-24 21:02:18 +08:00
}
2024-08-24 12:29:58 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-24 21:02:18 +08:00
private void ExecuteFor_Iternal(EcsSpan span)
2024-08-24 12:29:58 +08:00
{
#if DEBUG || DRAGONECS_STABILITY_MODE
2024-08-24 12:29:58 +08:00
if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
2024-10-31 16:27:53 +08:00
if (span.WorldID != World.ID) { Throw.Quiery_ArgumentDifferentWorldsException(); }
2024-08-24 12:29:58 +08:00
#endif
2024-08-24 21:02:18 +08:00
if (_filteredEntities == null)
2024-08-24 12:29:58 +08:00
{
2024-08-24 21:02:18 +08:00
_filteredEntities = new int[32];
2024-08-24 12:29:58 +08:00
}
2024-10-10 19:57:19 +08:00
_filteredEntitiesCount = _iterator.IterateTo(span, ref _filteredEntities);
2024-08-24 21:02:18 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute()
{
Execute_Iternal();
2025-04-04 10:22:36 +08:00
#if DEBUG || DRAGONECS_DEEP_DEBUG
var newSpan = new EcsSpan(World.ID, _filteredAllEntities, _filteredAllEntitiesCount);
2025-04-04 14:40:53 +08:00
using (EcsGroup group = EcsGroup.New(World))
2025-04-04 10:22:36 +08:00
{
2025-04-04 14:40:53 +08:00
foreach (var e in World.Entities)
{
if (World.IsMatchesMask(Mask, e))
{
group.Add(e);
}
}
if (group.SetEquals(newSpan) == false)
2025-04-04 10:22:36 +08:00
{
2025-04-04 14:40:53 +08:00
int[] array = new int[_filteredAllEntities.Length];
var count = _iterator.IterateTo(World.Entities, ref array);
EcsDebug.PrintError(newSpan.ToString() + "\r\n" + group.ToSpan().ToString());
2025-04-04 10:22:36 +08:00
Throw.DeepDebugException();
}
}
#endif
2024-10-31 16:27:53 +08:00
return new EcsSpan(World.ID, _filteredAllEntities, _filteredAllEntitiesCount);
2024-08-24 21:02:18 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span)
{
if (span.IsSourceEntities)
{
return Execute();
}
2024-08-24 21:02:18 +08:00
ExecuteFor_Iternal(span);
2025-04-04 10:22:36 +08:00
#if DEBUG || DRAGONECS_DEEP_DEBUG
var newSpan = new EcsSpan(World.ID, _filteredEntities, _filteredEntitiesCount);
foreach (var e in newSpan)
{
if (World.IsMatchesMask(Mask, e) == false)
{
Throw.DeepDebugException();
}
}
#endif
2024-10-31 16:27:53 +08:00
return new EcsSpan(World.ID, _filteredEntities, _filteredEntitiesCount);
2024-08-23 22:31:43 +08:00
}
2024-08-26 11:08:42 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute(Comparison<int> comparison)
{
Execute_Iternal();
ArraySortHalperX<int>.Sort(_filteredAllEntities, comparison, _filteredAllEntitiesCount);
2024-10-31 16:27:53 +08:00
return new EcsSpan(World.ID, _filteredAllEntities, _filteredAllEntitiesCount);
2024-08-26 11:08:42 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span, Comparison<int> comparison)
{
if (span.IsSourceEntities)
{
return Execute(comparison);
}
2024-08-26 11:08:42 +08:00
ExecuteFor_Iternal(span);
ArraySortHalperX<int>.Sort(_filteredEntities, comparison, _filteredEntitiesCount);
2024-10-31 16:27:53 +08:00
return new EcsSpan(World.ID, _filteredEntities, _filteredEntitiesCount);
2024-08-26 11:08:42 +08:00
}
2024-08-24 12:29:58 +08:00
#endregion
}
2024-10-03 08:20:22 +08:00
}