DragonECS/src/EcsMask.cs

988 lines
36 KiB
C#
Raw Normal View History

2024-01-07 18:52:54 +08:00
using DCFApixels.DragonECS.Internal;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
2024-10-02 22:13:10 +08:00
#if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices;
#endif
2024-01-07 18:52:54 +08:00
namespace DCFApixels.DragonECS
{
2024-10-10 19:57:19 +08:00
using static EcsMaskIteratorUtility;
2024-10-05 18:05:33 +08:00
public interface IEcsComponentMask
{
EcsMask ToMask(EcsWorld world);
}
2024-10-02 22:13:10 +08:00
#if ENABLE_IL2CPP
2024-04-28 19:43:10 +08:00
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-01-07 18:52:54 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2024-10-05 18:05:33 +08:00
public sealed class EcsMask : IEquatable<EcsMask>, IEcsComponentMask
2024-01-07 18:52:54 +08:00
{
2024-10-31 14:46:21 +08:00
internal readonly EcsStaticMask _staticMask;
2024-04-22 17:20:31 +08:00
internal readonly int _id;
internal readonly short _worldID;
internal readonly EcsMaskChunck[] _incChunckMasks;
internal readonly EcsMaskChunck[] _excChunckMasks;
2024-10-02 22:13:10 +08:00
/// <summary> Sorted </summary>
internal readonly int[] _inc;
/// <summary> Sorted </summary>
internal readonly int[] _exc;
2024-01-07 23:19:18 +08:00
2024-08-23 22:31:43 +08:00
private EcsMaskIterator _iterator;
2024-01-07 23:19:18 +08:00
#region Properties
2024-03-02 21:45:09 +08:00
public int ID
{
2024-10-02 22:13:10 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-22 17:20:31 +08:00
get { return _id; }
2024-03-02 21:45:09 +08:00
}
2024-04-16 12:46:09 +08:00
public short WorldID
2024-03-02 21:45:09 +08:00
{
2024-10-02 22:13:10 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-22 17:20:31 +08:00
get { return _worldID; }
2024-03-02 21:45:09 +08:00
}
public EcsWorld World
{
2024-10-02 22:13:10 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-22 17:20:31 +08:00
get { return EcsWorld.GetWorld(_worldID); }
2024-03-02 21:45:09 +08:00
}
2024-10-02 22:13:10 +08:00
/// <summary> Sorted set including constraints. </summary>
2024-03-02 21:45:09 +08:00
public ReadOnlySpan<int> Inc
{
2024-10-02 22:13:10 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-22 17:20:31 +08:00
get { return _inc; }
2024-03-02 21:45:09 +08:00
}
2024-10-02 22:13:10 +08:00
/// <summary> Sorted set excluding constraints. </summary>
2024-03-02 21:45:09 +08:00
public ReadOnlySpan<int> Exc
{
2024-10-02 22:13:10 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-22 17:20:31 +08:00
get { return _exc; }
2024-03-02 21:45:09 +08:00
}
2024-03-17 10:18:16 +08:00
public bool IsEmpty
{
2024-10-02 22:13:10 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-22 17:20:31 +08:00
get { return _inc.Length == 0 && _exc.Length == 0; }
2024-03-17 10:18:16 +08:00
}
2024-04-18 22:14:50 +08:00
public bool IsBroken
{
2024-04-22 17:20:31 +08:00
get { return (_inc.Length & _exc.Length) == 1 && _inc[0] == _exc[0]; }
2024-04-18 22:14:50 +08:00
}
2024-01-07 23:19:18 +08:00
#endregion
#region Constructors
2024-10-31 14:46:21 +08:00
[Obsolete("")]//TODO написать новый сопсоб создания
2024-10-02 22:13:10 +08:00
public static Builder New(EcsWorld world) { return new Builder(world); }
internal static EcsMask CreateEmpty(int id, short worldID)
2024-04-18 22:14:50 +08:00
{
2024-10-31 14:46:21 +08:00
return new EcsMask(EcsStaticMask.Empty, id, worldID, new int[0], new int[0]);
2024-04-18 22:14:50 +08:00
}
2024-10-02 22:13:10 +08:00
internal static EcsMask CreateBroken(int id, short worldID)
2024-04-18 22:14:50 +08:00
{
2024-10-31 14:46:21 +08:00
return new EcsMask(EcsStaticMask.Broken, id, worldID, new int[1] { 1 }, new int[1] { 1 });
2024-04-18 22:14:50 +08:00
}
2024-10-31 14:46:21 +08:00
private EcsMask(EcsStaticMask staticMask, int id, short worldID, int[] inc, int[] exc)
2024-04-18 22:14:50 +08:00
{
2024-10-31 14:46:21 +08:00
_staticMask = staticMask;
2024-10-02 22:13:10 +08:00
_id = id;
_inc = inc;
_exc = exc;
_worldID = worldID;
2024-01-07 19:32:16 +08:00
2024-04-22 17:20:31 +08:00
_incChunckMasks = MakeMaskChuncsArray(inc);
_excChunckMasks = MakeMaskChuncsArray(exc);
2024-01-07 20:44:11 +08:00
}
private unsafe EcsMaskChunck[] MakeMaskChuncsArray(int[] sortedArray)
{
EcsMaskChunck* buffer = stackalloc EcsMaskChunck[sortedArray.Length];
int resultLength = 0;
for (int i = 0; i < sortedArray.Length;)
2024-01-07 19:32:16 +08:00
{
2024-01-07 20:44:11 +08:00
int chankIndexX = sortedArray[i] >> EcsMaskChunck.DIV_SHIFT;
int maskX = 0;
do
{
EcsMaskChunck bitJ = EcsMaskChunck.FromID(sortedArray[i]);
2024-10-10 19:57:19 +08:00
if (bitJ.chunkIndex != chankIndexX)
2024-01-07 20:44:11 +08:00
{
break;
}
maskX |= bitJ.mask;
i++;
} while (i < sortedArray.Length);
buffer[resultLength++] = new EcsMaskChunck(chankIndexX, maskX);
2024-01-07 19:32:16 +08:00
}
2024-01-07 20:44:11 +08:00
EcsMaskChunck[] result = new EcsMaskChunck[resultLength];
for (int i = 0; i < resultLength; i++)
2024-01-07 19:32:16 +08:00
{
2024-01-07 20:44:11 +08:00
result[i] = buffer[i];
2024-01-07 19:32:16 +08:00
}
2024-01-07 20:44:11 +08:00
return result;
2024-01-07 18:52:54 +08:00
}
2024-01-07 23:19:18 +08:00
#endregion
2024-01-07 18:52:54 +08:00
2024-02-11 14:53:36 +08:00
#region Checks
2024-03-02 21:59:02 +08:00
public bool IsSubmaskOf(EcsMask otherMask)
2024-02-11 14:53:36 +08:00
{
return IsSubmask(otherMask, this);
}
2024-03-02 21:59:02 +08:00
public bool IsSupermaskOf(EcsMask otherMask)
2024-02-11 14:53:36 +08:00
{
return IsSubmask(this, otherMask);
}
2024-03-02 21:59:02 +08:00
public bool IsConflictWith(EcsMask otherMask)
2024-02-11 14:53:36 +08:00
{
2024-04-22 17:20:31 +08:00
return OverlapsArray(_inc, otherMask._exc) || OverlapsArray(_exc, otherMask._inc);
2024-02-11 14:53:36 +08:00
}
private static bool OverlapsArray(int[] l, int[] r)
{
int li = 0;
int ri = 0;
while (li < l.Length && ri < r.Length)
{
if (l[li] == r[ri])
{
return true;
}
else if (l[li] < r[ri])
{
li++;
}
else
{
ri++;
}
}
return false;
}
private static bool IsSubmask(EcsMask super, EcsMask sub)
{
2024-04-22 17:20:31 +08:00
return IsSubarray(sub._inc, super._inc) && IsSuperarray(sub._exc, super._exc);
2024-02-11 14:53:36 +08:00
}
private static bool IsSubarray(int[] super, int[] sub)
{
if (super.Length < sub.Length)
{
return false;
}
int superI = 0;
int subI = 0;
while (superI < super.Length && subI < sub.Length)
{
if (super[superI] == sub[subI])
{
superI++;
}
subI++;
}
return subI == sub.Length;
}
private static bool IsSuperarray(int[] super, int[] sub)
{
if (super.Length < sub.Length)
{
return false;
}
int superI = 0;
int subI = 0;
while (superI < super.Length && subI < sub.Length)
{
if (super[superI] == sub[subI])
{
subI++;
}
superI++;
}
return subI == sub.Length;
}
#endregion
2024-01-07 18:52:54 +08:00
#region Object
2024-03-02 21:45:09 +08:00
public override string ToString()
{
2024-04-22 17:20:31 +08:00
return CreateLogString(_worldID, _inc, _exc);
2024-03-02 21:45:09 +08:00
}
2024-01-07 19:32:16 +08:00
public bool Equals(EcsMask mask)
{
2024-04-22 17:20:31 +08:00
return _id == mask._id && _worldID == mask._worldID;
2024-01-07 19:32:16 +08:00
}
public override bool Equals(object obj)
{
2024-04-22 17:20:31 +08:00
return obj is EcsMask mask && _id == mask._id && Equals(mask);
2024-01-07 19:32:16 +08:00
}
public override int GetHashCode()
{
2024-04-22 17:20:31 +08:00
return unchecked(_id ^ (_worldID * EcsConsts.MAGIC_PRIME));
2024-01-07 19:32:16 +08:00
}
2024-01-07 18:52:54 +08:00
#endregion
2024-08-23 22:31:43 +08:00
#region Other
2024-10-05 18:05:33 +08:00
EcsMask IEcsComponentMask.ToMask(EcsWorld world) { return this; }
2024-08-23 22:31:43 +08:00
public EcsMaskIterator GetIterator()
{
2024-08-24 13:10:50 +08:00
if (_iterator == null)
2024-08-23 22:31:43 +08:00
{
_iterator = new EcsMaskIterator(EcsWorld.GetWorld(_worldID), this);
}
return _iterator;
}
#endregion
2024-01-07 18:52:54 +08:00
#region Debug utils
2024-04-16 12:46:09 +08:00
private static string CreateLogString(short worldID, int[] inc, int[] exc)
2024-01-07 18:52:54 +08:00
{
#if (DEBUG && !DISABLE_DEBUG)
2024-03-02 21:45:09 +08:00
string converter(int o) { return EcsDebugUtility.GetGenericTypeName(EcsWorld.GetWorld(worldID).AllPools[o].ComponentType, 1); }
2024-01-07 18:52:54 +08:00
return $"Inc({string.Join(", ", inc.Select(converter))}) Exc({string.Join(", ", exc.Select(converter))})";
#else
return $"Inc({string.Join(", ", inc)}) Exc({string.Join(", ", exc)})"; // Release optimization
#endif
}
2024-01-07 19:32:16 +08:00
2024-01-07 18:52:54 +08:00
internal class DebuggerProxy
{
2024-04-18 22:14:50 +08:00
private EcsMask _source;
2024-01-07 19:32:16 +08:00
public readonly int ID;
2024-01-07 18:52:54 +08:00
public readonly EcsWorld world;
2024-04-16 12:46:09 +08:00
private readonly short _worldID;
2024-01-07 18:52:54 +08:00
public readonly EcsMaskChunck[] includedChunkMasks;
public readonly EcsMaskChunck[] excludedChunkMasks;
public readonly int[] included;
public readonly int[] excluded;
public readonly Type[] includedTypes;
public readonly Type[] excludedTypes;
2024-04-18 22:14:50 +08:00
public bool IsEmpty { get { return _source.IsEmpty; } }
public bool IsBroken { get { return _source.IsBroken; } }
2024-01-07 18:52:54 +08:00
public DebuggerProxy(EcsMask mask)
{
2024-04-18 22:14:50 +08:00
_source = mask;
2024-04-22 17:20:31 +08:00
ID = mask._id;
world = EcsWorld.GetWorld(mask._worldID);
_worldID = mask._worldID;
includedChunkMasks = mask._incChunckMasks;
excludedChunkMasks = mask._excChunckMasks;
included = mask._inc;
excluded = mask._exc;
2024-03-02 21:45:09 +08:00
Type converter(int o) { return world.GetComponentType(o); }
2024-01-07 18:52:54 +08:00
includedTypes = included.Select(converter).ToArray();
excludedTypes = excluded.Select(converter).ToArray();
}
2024-03-02 21:45:09 +08:00
public override string ToString()
{
return CreateLogString(_worldID, included, excluded);
}
2024-01-07 18:52:54 +08:00
}
#endregion
2024-04-15 01:18:08 +08:00
#region Operators
public static EcsMask operator -(EcsMask a, EcsMask b)
{
return a.World.Get<WorldMaskComponent>().ExceptMask(a, b);
}
2024-10-05 20:59:16 +08:00
public static EcsMask operator -(EcsMask a, IEcsComponentMask b)
{
return a.World.Get<WorldMaskComponent>().ExceptMask(a, b.ToMask(a.World));
}
public static EcsMask operator -(IEcsComponentMask b, EcsMask a)
{
return a.World.Get<WorldMaskComponent>().ExceptMask(b.ToMask(a.World), a);
}
2024-10-05 19:10:27 +08:00
public static EcsMask operator +(EcsMask a, EcsMask b)
{
return a.World.Get<WorldMaskComponent>().CombineMask(a, b);
}
2024-10-05 20:59:16 +08:00
public static EcsMask operator +(EcsMask a, IEcsComponentMask b)
{
return a.World.Get<WorldMaskComponent>().CombineMask(a, b.ToMask(a.World));
}
public static EcsMask operator +(IEcsComponentMask b, EcsMask a)
{
return a.World.Get<WorldMaskComponent>().CombineMask(b.ToMask(a.World), a);
}
2024-10-05 19:10:27 +08:00
public static implicit operator EcsMask((IEcsComponentMask mask, EcsWorld world) a)
{
return a.mask.ToMask(a.world);
}
2024-10-05 19:15:33 +08:00
public static implicit operator EcsMask((EcsWorld world, IEcsComponentMask mask) a)
{
return a.mask.ToMask(a.world);
}
2024-04-15 01:18:08 +08:00
#endregion
#region OpMaskKey
private readonly struct OpMaskKey : IEquatable<OpMaskKey>
{
public readonly int leftMaskID;
public readonly int rightMaskID;
public readonly int operation;
2024-10-05 19:10:27 +08:00
public const int COMBINE_OP = 7;
2024-04-15 01:18:08 +08:00
public const int EXCEPT_OP = 32;
public OpMaskKey(int leftMaskID, int rightMaskID, int operation)
{
this.leftMaskID = leftMaskID;
this.rightMaskID = rightMaskID;
this.operation = operation;
}
public bool Equals(OpMaskKey other)
{
2024-04-28 18:36:24 +08:00
return leftMaskID == other.leftMaskID &&
rightMaskID == other.rightMaskID &&
2024-04-15 01:18:08 +08:00
operation == other.operation;
}
public override int GetHashCode()
{
return leftMaskID ^ (rightMaskID * operation);
}
}
#endregion
2024-10-10 19:57:19 +08:00
#region StaticMask
2024-10-02 22:13:10 +08:00
public static EcsMask FromStatic(EcsWorld world, EcsStaticMask abstractMask)
{
2024-10-31 14:46:21 +08:00
return world.Get<WorldMaskComponent>().ConvertFromStatic(abstractMask);
2024-10-02 22:13:10 +08:00
}
#endregion
2024-01-07 19:32:16 +08:00
#region Builder
2024-01-11 00:48:39 +08:00
private readonly struct WorldMaskComponent : IEcsWorldComponent<WorldMaskComponent>
{
private readonly EcsWorld _world;
2024-04-15 01:18:08 +08:00
private readonly Dictionary<OpMaskKey, EcsMask> _opMasks;
2024-10-31 14:46:21 +08:00
private readonly SparseArray<EcsMask> _staticMasks;
2024-01-11 00:48:39 +08:00
2024-04-18 22:14:50 +08:00
public readonly EcsMask EmptyMask;
public readonly EcsMask BrokenMask;
2024-01-11 00:48:39 +08:00
#region Constructor/Destructor
2024-10-31 14:46:21 +08:00
public WorldMaskComponent(EcsWorld world)
2024-01-11 00:48:39 +08:00
{
_world = world;
2024-10-02 22:13:10 +08:00
_opMasks = new Dictionary<OpMaskKey, EcsMask>(256);
2024-10-31 14:46:21 +08:00
_staticMasks = new SparseArray<EcsMask>(256);
EmptyMask = CreateEmpty(_staticMasks.Count, world.id);
_staticMasks.Add(EmptyMask._staticMask.ID, EmptyMask);
BrokenMask = CreateBroken(_staticMasks.Count, world.id);
_staticMasks.Add(BrokenMask._staticMask.ID, BrokenMask);
2024-01-11 00:48:39 +08:00
}
public void Init(ref WorldMaskComponent component, EcsWorld world)
{
2024-10-31 14:46:21 +08:00
component = new WorldMaskComponent(world);
2024-01-11 00:48:39 +08:00
}
public void OnDestroy(ref WorldMaskComponent component, EcsWorld world)
{
2024-04-15 01:18:08 +08:00
component._opMasks.Clear();
2024-10-31 14:46:21 +08:00
component._staticMasks.Clear();
2024-01-11 00:48:39 +08:00
component = default;
}
#endregion
#region GetMask
2024-10-05 19:10:27 +08:00
internal EcsMask CombineMask(EcsMask a, EcsMask b)
{
int operation = OpMaskKey.COMBINE_OP;
if (_opMasks.TryGetValue(new OpMaskKey(a._id, b._id, operation), out EcsMask result) == false)
{
if (a.IsConflictWith(b))
{
return a.World.Get<WorldMaskComponent>().BrokenMask;
}
2024-10-31 14:46:21 +08:00
result = ConvertFromStatic(EcsStaticMask.New().Combine(a._staticMask).Combine(b._staticMask).Build());
2024-10-05 19:10:27 +08:00
_opMasks.Add(new OpMaskKey(a._id, b._id, operation), result);
}
return result;
}
2024-04-15 01:18:08 +08:00
internal EcsMask ExceptMask(EcsMask a, EcsMask b)
{
int operation = OpMaskKey.EXCEPT_OP;
2024-04-22 17:20:31 +08:00
if (_opMasks.TryGetValue(new OpMaskKey(a._id, b._id, operation), out EcsMask result) == false)
2024-04-15 01:18:08 +08:00
{
if (a.IsConflictWith(b))
{
2024-04-18 22:14:50 +08:00
return a.World.Get<WorldMaskComponent>().BrokenMask;
2024-04-15 01:18:08 +08:00
}
2024-10-31 14:46:21 +08:00
result = ConvertFromStatic(EcsStaticMask.New().Combine(a._staticMask).Except(b._staticMask).Build());
2024-04-22 17:20:31 +08:00
_opMasks.Add(new OpMaskKey(a._id, b._id, operation), result);
2024-04-15 01:18:08 +08:00
}
return result;
}
2024-01-11 00:48:39 +08:00
2024-10-31 14:46:21 +08:00
internal EcsMask ConvertFromStatic(EcsStaticMask staticMask)
2024-01-07 18:52:54 +08:00
{
2024-10-31 14:46:21 +08:00
int[] ConvertTypeCodeToComponentTypeID(ReadOnlySpan<EcsTypeCode> from, EcsWorld world)
2024-01-11 00:48:39 +08:00
{
2024-10-31 14:46:21 +08:00
int[] to = new int[from.Length];
for (int i = 0; i < to.Length; i++)
2024-01-11 00:48:39 +08:00
{
2024-10-31 14:46:21 +08:00
to[i] = world.DeclareOrGetComponentTypeID(from[i]);
2024-01-11 00:48:39 +08:00
}
2024-10-31 14:46:21 +08:00
Array.Sort(to);
return to;
2024-01-11 00:48:39 +08:00
}
2024-10-31 14:46:21 +08:00
if (_staticMasks.TryGetValue(staticMask.ID, out EcsMask result) == false)
2024-01-07 18:52:54 +08:00
{
2024-10-31 14:46:21 +08:00
int[] incs = ConvertTypeCodeToComponentTypeID(staticMask.IncTypeCodes, _world);
int[] excs = ConvertTypeCodeToComponentTypeID(staticMask.ExcTypeCodes, _world);
result = new EcsMask(staticMask, _staticMasks.Count, _world.id, incs, excs);
_staticMasks.Add(staticMask.ID, result);
2024-01-07 18:52:54 +08:00
}
2024-10-31 14:46:21 +08:00
return result;
2024-01-07 18:52:54 +08:00
}
2024-01-11 00:48:39 +08:00
#endregion
2024-01-07 18:52:54 +08:00
}
2024-10-31 14:46:21 +08:00
[Obsolete("")]//TODO написать новый сопсоб создания
public struct Builder
2024-01-07 18:52:54 +08:00
{
2024-10-31 14:46:21 +08:00
private readonly EcsStaticMask.Builder _builder;
2024-01-07 19:32:16 +08:00
private readonly EcsWorld _world;
2024-01-07 18:52:54 +08:00
2024-10-31 14:46:21 +08:00
public Builder(EcsWorld world)
2024-01-07 18:52:54 +08:00
{
_world = world;
2024-10-31 14:46:21 +08:00
_builder = EcsStaticMask.Builder.New();
2024-01-07 18:52:54 +08:00
}
2024-04-15 01:18:08 +08:00
2024-10-31 14:46:21 +08:00
public Builder Include<T>() { return Inc<T>(); }
public Builder Exclude<T>() { return Exc<T>(); }
public Builder Include(Type type) { return Inc(type); }
public Builder Exclude(Type type) { return Exc(type); }
2024-04-15 01:18:08 +08:00
2024-10-31 14:46:21 +08:00
public Builder Inc<T>() { _builder.Inc<T>(); return this; }
public Builder Exc<T>() { _builder.Exc<T>(); return this; }
public Builder Inc(Type type) { _builder.Inc(type); return this; }
public Builder Exc(Type type) { _builder.Exc(type); return this; }
public Builder Inc(EcsTypeCode typeCode) { _builder.Inc(typeCode); return this; }
public Builder Exc(EcsTypeCode typeCode) { _builder.Exc(typeCode); return this; }
public Builder Combine(EcsMask mask) { _builder.Combine(mask._staticMask); return this; }
public Builder Except(EcsMask mask) { _builder.Except(mask._staticMask); return this; }
2024-04-18 22:14:50 +08:00
2024-10-31 14:46:21 +08:00
public Builder Inc(int componentTypeID) { Inc(_world.GetComponentType(componentTypeID)); return this; }
public Builder Exc(int componentTypeID) { Exc(_world.GetComponentType(componentTypeID)); return this; }
2024-01-07 18:52:54 +08:00
2024-10-31 14:46:21 +08:00
public EcsMask Build() { return _world.Get<WorldMaskComponent>().ConvertFromStatic(_builder.Build()); }
2024-04-18 22:14:50 +08:00
}
2024-01-07 19:32:16 +08:00
#endregion
2024-01-07 18:52:54 +08:00
}
2024-01-07 23:19:18 +08:00
#region EcsMaskChunck
2024-01-07 18:52:54 +08:00
[DebuggerTypeProxy(typeof(DebuggerProxy))]
public readonly struct EcsMaskChunck
{
internal const int BITS = 32;
internal const int DIV_SHIFT = 5;
internal const int MOD_MASK = BITS - 1;
2024-10-10 19:57:19 +08:00
public readonly int chunkIndex;
2024-01-07 18:52:54 +08:00
public readonly int mask;
public EcsMaskChunck(int chankIndex, int mask)
{
2024-10-10 19:57:19 +08:00
this.chunkIndex = chankIndex;
2024-01-07 18:52:54 +08:00
this.mask = mask;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsMaskChunck FromID(int id)
{
return new EcsMaskChunck(id >> DIV_SHIFT, 1 << (id & MOD_MASK));
}
public override string ToString()
{
2024-10-10 19:57:19 +08:00
return $"mask({chunkIndex}, {mask}, {BitsUtility.CountBits(mask)})";
2024-01-07 18:52:54 +08:00
}
internal class DebuggerProxy
{
public int chunk;
public uint mask;
public int[] values = Array.Empty<int>();
public string bits;
public DebuggerProxy(EcsMaskChunck maskbits)
{
2024-10-10 19:57:19 +08:00
chunk = maskbits.chunkIndex;
2024-01-07 18:52:54 +08:00
mask = (uint)maskbits.mask;
BitsUtility.GetBitNumbersNoAlloc(mask, ref values);
for (int i = 0; i < values.Length; i++)
{
values[i] += (chunk) << 5;
}
bits = BitsUtility.ToBitsString(mask, '_', 8);
}
}
}
2024-01-07 23:19:18 +08:00
#endregion
2024-08-23 22:31:43 +08:00
2024-08-24 12:29:58 +08:00
#region EcsMaskIterator
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-08-23 22:31:43 +08:00
public class EcsMaskIterator
{
2024-10-10 19:57:19 +08:00
public readonly EcsWorld World;
public readonly EcsMask Mask;
private readonly UnsafeArray<int> _sortIncBuffer;
private readonly UnsafeArray<int> _sortExcBuffer;
private readonly UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer;
private readonly UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer;
private readonly bool _isOnlyInc;
#region Constructors/Finalizator
public unsafe EcsMaskIterator(EcsWorld source, EcsMask mask)
2024-08-23 22:31:43 +08:00
{
2024-10-10 19:57:19 +08:00
World = source;
Mask = mask;
_sortIncBuffer = UnsafeArray<int>.FromArray(mask._inc);
_sortExcBuffer = UnsafeArray<int>.FromArray(mask._exc);
_sortIncChunckBuffer = UnsafeArray<EcsMaskChunck>.FromArray(mask._incChunckMasks);
_sortExcChunckBuffer = UnsafeArray<EcsMaskChunck>.FromArray(mask._excChunckMasks);
_isOnlyInc = _sortExcBuffer.Length <= 0;
2024-08-23 22:31:43 +08:00
}
2024-10-10 19:57:19 +08:00
unsafe ~EcsMaskIterator()
2024-08-23 22:31:43 +08:00
{
2024-10-10 19:57:19 +08:00
_sortIncBuffer.ReadonlyDispose();
_sortExcBuffer.ReadonlyDispose();
_sortIncChunckBuffer.ReadonlyDispose();
_sortExcChunckBuffer.ReadonlyDispose();
2024-08-23 22:31:43 +08:00
}
#endregion
2024-10-10 19:57:19 +08:00
#region IterateTo
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void IterateTo(EcsSpan source, EcsGroup group)
2024-08-23 22:31:43 +08:00
{
2024-10-10 19:57:19 +08:00
if (_isOnlyInc)
2024-08-24 12:29:58 +08:00
{
2024-10-10 19:57:19 +08:00
IterateOnlyInc(source).CopyTo(group);
2024-08-24 12:29:58 +08:00
}
2024-10-10 19:57:19 +08:00
else
2024-08-24 12:29:58 +08:00
{
2024-10-10 19:57:19 +08:00
Iterate(source).CopyTo(group);
2024-08-24 12:29:58 +08:00
}
2024-10-10 19:57:19 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int IterateTo(EcsSpan source, ref int[] array)
{
if (_isOnlyInc)
2024-08-24 12:29:58 +08:00
{
2024-10-10 19:57:19 +08:00
return IterateOnlyInc(source).CopyTo(ref array);
2024-08-24 12:29:58 +08:00
}
2024-10-10 19:57:19 +08:00
else
2024-08-24 12:29:58 +08:00
{
2024-10-10 19:57:19 +08:00
return Iterate(source).CopyTo(ref array);
2024-08-24 12:29:58 +08:00
}
}
#endregion
2024-10-10 19:57:19 +08:00
#region Iterate/Enumerable
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerable Iterate(EcsSpan span) { return new Enumerable(this, span); }
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-08-23 22:31:43 +08:00
public readonly ref struct Enumerable
{
private readonly EcsMaskIterator _iterator;
private readonly EcsSpan _span;
public Enumerable(EcsMaskIterator iterator, EcsSpan span)
{
_iterator = iterator;
_span = span;
}
#region CopyTo
2024-10-10 19:57:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-23 22:31:43 +08:00
public void CopyTo(EcsGroup group)
{
group.Clear();
var enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
group.AddUnchecked(enumerator.Current);
}
}
2024-10-10 19:57:19 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-08-23 22:31:43 +08:00
public int CopyTo(ref int[] array)
{
int count = 0;
var enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
if (array.Length <= count)
{
Array.Resize(ref array, array.Length << 1);
}
array[count++] = enumerator.Current;
}
return count;
}
#endregion
#region Other
2024-10-10 19:57:19 +08:00
public List<int> ToList()
2024-08-23 22:31:43 +08:00
{
List<int> ints = new List<int>();
2024-10-10 19:57:19 +08:00
foreach (var e in this) { ints.Add(e); }
return ints;
2024-08-23 22:31:43 +08:00
}
2024-10-10 19:57:19 +08:00
public override string ToString() { return CollectionUtility.EntitiesToString(ToList(), "it"); }
2024-08-23 22:31:43 +08:00
#endregion
#region Enumerator
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() { return new Enumerator(_span, _iterator); }
2024-10-10 19:57:19 +08:00
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
2024-08-23 22:31:43 +08:00
public unsafe ref struct Enumerator
{
private ReadOnlySpan<int>.Enumerator _span;
2024-10-10 19:57:19 +08:00
private readonly UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer;
private readonly UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer;
2024-08-23 22:31:43 +08:00
2024-10-10 19:57:19 +08:00
private readonly int[] _entityComponentMasks;
2024-08-23 22:31:43 +08:00
private readonly int _entityComponentMaskLengthBitShift;
public unsafe Enumerator(EcsSpan span, EcsMaskIterator iterator)
{
_sortIncChunckBuffer = iterator._sortIncChunckBuffer;
_sortExcChunckBuffer = iterator._sortExcChunckBuffer;
2024-10-10 19:57:19 +08:00
_entityComponentMasks = iterator.World._entityComponentMasks;
2024-08-23 22:31:43 +08:00
_entityComponentMaskLengthBitShift = iterator.World._entityComponentMaskLengthBitShift;
if (iterator.Mask.IsBroken)
{
_span = span.Slice(0, 0).GetEnumerator();
return;
}
#region Sort
UnsafeArray<int> _sortIncBuffer = iterator._sortIncBuffer;
UnsafeArray<int> _sortExcBuffer = iterator._sortExcBuffer;
EcsWorld.PoolSlot[] counts = iterator.World._poolSlots;
2024-10-10 19:57:19 +08:00
int max = _sortIncBuffer.Length > _sortExcBuffer.Length ? _sortIncBuffer.Length : _sortExcBuffer.Length;
2024-08-23 22:31:43 +08:00
2024-10-10 19:57:19 +08:00
EcsMaskChunck* preSortingBuffer;
if (max > STACK_BUFFER_THRESHOLD)
2024-08-23 22:31:43 +08:00
{
2024-10-10 19:57:19 +08:00
preSortingBuffer = TempBuffer<EcsMaskChunck>.Get(max);
}
else
{
EcsMaskChunck* ptr = stackalloc EcsMaskChunck[max];
preSortingBuffer = ptr;
2024-08-23 22:31:43 +08:00
}
if (_sortIncChunckBuffer.Length > 1)
{
2024-10-10 19:57:19 +08:00
var comparer = new IncCountComparer(counts);
UnsafeArraySortHalperX<int>.InsertionSort(_sortIncBuffer.ptr, _sortIncBuffer.Length, ref comparer);
ConvertToChuncks(preSortingBuffer, _sortIncBuffer, _sortIncChunckBuffer);
2024-08-23 22:31:43 +08:00
}
if (_sortIncChunckBuffer.Length > 0 && counts[_sortIncBuffer.ptr[0]].count <= 0)
{
_span = span.Slice(0, 0).GetEnumerator();
return;
}
2024-10-10 19:57:19 +08:00
2024-08-23 22:31:43 +08:00
if (_sortExcChunckBuffer.Length > 1)
{
2024-10-10 19:57:19 +08:00
ExcCountComparer comparer = new ExcCountComparer(counts);
UnsafeArraySortHalperX<int>.InsertionSort(_sortExcBuffer.ptr, _sortExcBuffer.Length, ref comparer);
ConvertToChuncks(preSortingBuffer, _sortExcBuffer, _sortExcChunckBuffer);
2024-08-23 22:31:43 +08:00
}
#endregion
_span = span.GetEnumerator();
}
public int Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _span.Current; }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
while (_span.MoveNext())
{
int chunck = _span.Current << _entityComponentMaskLengthBitShift;
for (int i = 0; i < _sortIncChunckBuffer.Length; i++)
{
var bit = _sortIncChunckBuffer.ptr[i];
2024-10-10 19:57:19 +08:00
if ((_entityComponentMasks[chunck + bit.chunkIndex] & bit.mask) != bit.mask)
2024-08-23 22:31:43 +08:00
{
goto skip;
}
}
for (int i = 0; i < _sortExcChunckBuffer.Length; i++)
{
var bit = _sortExcChunckBuffer.ptr[i];
2024-10-10 19:57:19 +08:00
if ((_entityComponentMasks[chunck + bit.chunkIndex] & bit.mask) != 0)
2024-08-23 22:31:43 +08:00
{
goto skip;
}
}
return true;
skip: continue;
}
return false;
}
}
#endregion
}
#endregion
2024-10-10 19:57:19 +08:00
#region Iterate/Enumerable OnlyInc
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public OnlyIncEnumerable IterateOnlyInc(EcsSpan span) { return new OnlyIncEnumerable(this, span); }
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
public readonly ref struct OnlyIncEnumerable
{
private readonly EcsMaskIterator _iterator;
private readonly EcsSpan _span;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public OnlyIncEnumerable(EcsMaskIterator iterator, EcsSpan span)
{
_iterator = iterator;
_span = span;
}
#region CopyTo
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void CopyTo(EcsGroup group)
{
group.Clear();
var enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
group.AddUnchecked(enumerator.Current);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CopyTo(ref int[] array)
{
int count = 0;
var enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
if (array.Length <= count)
{
Array.Resize(ref array, array.Length << 1);
}
array[count++] = enumerator.Current;
}
return count;
}
#endregion
#region Other
public List<int> ToList()
{
List<int> ints = new List<int>();
foreach (var e in this) { ints.Add(e); }
return ints;
}
public override string ToString() { return CollectionUtility.EntitiesToString(ToList(), "inc_it"); }
#endregion
#region Enumerator
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() { return new Enumerator(_span, _iterator); }
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
public unsafe ref struct Enumerator
{
private ReadOnlySpan<int>.Enumerator _span;
private readonly UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer;
private readonly int[] _entityComponentMasks;
private readonly int _entityComponentMaskLengthBitShift;
public unsafe Enumerator(EcsSpan span, EcsMaskIterator iterator)
{
_sortIncChunckBuffer = iterator._sortIncChunckBuffer;
_entityComponentMasks = iterator.World._entityComponentMasks;
_entityComponentMaskLengthBitShift = iterator.World._entityComponentMaskLengthBitShift;
if (iterator.Mask.IsBroken)
{
_span = span.Slice(0, 0).GetEnumerator();
return;
}
#region Sort
UnsafeArray<int> _sortIncBuffer = iterator._sortIncBuffer;
EcsWorld.PoolSlot[] counts = iterator.World._poolSlots;
int max = _sortIncBuffer.Length;
EcsMaskChunck* preSortingBuffer;
if (max > STACK_BUFFER_THRESHOLD)
{
preSortingBuffer = TempBuffer<EcsMaskChunck>.Get(max);
}
else
{
EcsMaskChunck* ptr = stackalloc EcsMaskChunck[max];
preSortingBuffer = ptr;
}
if (_sortIncChunckBuffer.Length > 1)
{
var comparer = new IncCountComparer(counts);
UnsafeArraySortHalperX<int>.InsertionSort(_sortIncBuffer.ptr, _sortIncBuffer.Length, ref comparer);
ConvertToChuncks(preSortingBuffer, _sortIncBuffer, _sortIncChunckBuffer);
}
if (_sortIncChunckBuffer.Length > 0 && counts[_sortIncBuffer.ptr[0]].count <= 0)
{
_span = span.Slice(0, 0).GetEnumerator();
return;
}
#endregion
_span = span.GetEnumerator();
}
public int Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _span.Current; }
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
while (_span.MoveNext())
{
int chunck = _span.Current << _entityComponentMaskLengthBitShift;
for (int i = 0; i < _sortIncChunckBuffer.Length; i++)
{
var bit = _sortIncChunckBuffer.ptr[i];
if ((_entityComponentMasks[chunck + bit.chunkIndex] & bit.mask) != bit.mask)
{
goto skip;
}
}
return true;
skip: continue;
}
return false;
}
}
#endregion
}
#endregion
}
#endregion
}
namespace DCFApixels.DragonECS.Internal
{
#region EcsMaskIteratorUtility
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal unsafe class EcsMaskIteratorUtility
{
internal const int STACK_BUFFER_THRESHOLD = 256;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void ConvertToChuncks(EcsMaskChunck* ptr, UnsafeArray<int> input, UnsafeArray<EcsMaskChunck> output)
{
for (int i = 0; i < input.Length; i++)
{
ptr[i] = EcsMaskChunck.FromID(input.ptr[i]);
}
for (int inputI = 0, outputI = 0; outputI < output.Length; inputI++, ptr++)
{
int maskX = ptr->mask;
if (maskX == 0) { continue; }
int chunkIndexX = ptr->chunkIndex;
EcsMaskChunck* subptr = ptr;
for (int j = 1; j < input.Length - inputI; j++, subptr++)
{
if (subptr->chunkIndex == chunkIndexX)
{
maskX |= subptr->mask;
*subptr = default;
}
}
output.ptr[outputI] = new EcsMaskChunck(chunkIndexX, maskX);
outputI++;
}
}
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal readonly struct IncCountComparer : IStructComparer<int>
{
public readonly EcsWorld.PoolSlot[] counts;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IncCountComparer(EcsWorld.PoolSlot[] counts)
{
this.counts = counts;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(int a, int b)
{
return counts[a].count - counts[b].count;
}
}
#if ENABLE_IL2CPP
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal readonly struct ExcCountComparer : IStructComparer<int>
{
public readonly EcsWorld.PoolSlot[] counts;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ExcCountComparer(EcsWorld.PoolSlot[] counts)
{
this.counts = counts;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(int a, int b)
{
return counts[b].count - counts[a].count;
}
}
2024-08-23 22:31:43 +08:00
}
2024-08-24 12:29:58 +08:00
#endregion
2024-10-02 22:13:10 +08:00
}