DragonECS/src/EcsMask.cs

1141 lines
41 KiB
C#
Raw Normal View History

2024-01-07 18:52:54 +08:00
using DCFApixels.DragonECS.Internal;
using System;
2024-10-02 22:13:10 +08:00
using System.Collections.Concurrent;
2024-01-07 18:52:54 +08:00
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-04-28 19:43:10 +08:00
#if ENABLE_IL2CPP
2024-10-02 22:13:10 +08:00
[Il2CppSetOption (Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
public sealed class EcsStaticMask : IEquatable<EcsStaticMask>
{
private static ConcurrentDictionary<Key, EcsStaticMask> _ids = new ConcurrentDictionary<Key, EcsStaticMask>();
private static IdDispenser _idDIspenser = new IdDispenser(nullID: 0);
private static object _lock = new object();
private readonly int _id;
/// <summary> Sorted </summary>
private readonly EcsTypeCode[] _inc;
/// <summary> Sorted </summary>
private readonly EcsTypeCode[] _exc;
#region Properties
public int ID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _id; }
}
/// <summary> Sorted set including constraints presented as global type codes. </summary>
public ReadOnlySpan<EcsTypeCode> IncTypeCodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _inc; }
}
/// <summary> Sorted set excluding constraints presented as global type codes. </summary>
public ReadOnlySpan<EcsTypeCode> ExcTypeCodes
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _exc; }
}
#endregion
#region Constrcutors
public static Builder New() { return new Builder(); }
public static Builder Inc<T>() { return new Builder().Inc<T>(); }
public static Builder Exc<T>() { return new Builder().Exc<T>(); }
private EcsStaticMask(int id, Key key)
{
_id = id;
_inc = key.inc;
_exc = key.exc;
}
#endregion
#region Methods
public EcsMask ToMask(EcsWorld world)
{
return EcsMask.FromStatic(world, this);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(EcsStaticMask other) { return _id == other._id; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() { return _id; }
public override bool Equals(object obj) { return Equals((EcsStaticMask)obj); }
public override string ToString() { return $"s_mask({_id})"; }
#endregion
#region Builder
private readonly struct Key : IEquatable<Key>
{
public readonly EcsTypeCode[] inc;
public readonly EcsTypeCode[] exc;
public readonly int hash;
#region Constructors
public Key(EcsTypeCode[] inc, EcsTypeCode[] exc)
{
this.inc = inc;
this.exc = exc;
unchecked
{
hash = inc.Length + exc.Length;
for (int i = 0, iMax = inc.Length; i < iMax; i++)
{
hash = hash * EcsConsts.MAGIC_PRIME + (int)inc[i];
}
for (int i = 0, iMax = exc.Length; i < iMax; i++)
{
hash = hash * EcsConsts.MAGIC_PRIME - (int)exc[i];
}
}
}
#endregion
#region Object
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Key other)
{
if (inc.Length != other.inc.Length) { return false; }
if (exc.Length != other.exc.Length) { return false; }
for (int i = 0; i < inc.Length; i++)
{
if (inc[i] != other.inc[i]) { return false; }
}
for (int i = 0; i < exc.Length; i++)
{
if (exc[i] != other.exc[i]) { return false; }
}
return true;
}
public override bool Equals(object obj) { return Equals((Key)obj); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() { return hash; }
#endregion
}
public class Builder
{
private readonly HashSet<EcsTypeCode> _inc = new HashSet<EcsTypeCode>();
private readonly HashSet<EcsTypeCode> _exc = new HashSet<EcsTypeCode>();
#region Constrcutors
internal Builder() { }
#endregion
#region Include/Exclude/Combine
public Builder Inc<T>() { return Inc(EcsTypeCodeManager.Get<T>()); }
public Builder Exc<T>() { return Exc(EcsTypeCodeManager.Get<T>()); }
public Builder Inc(Type type) { return Inc(EcsTypeCodeManager.Get(type)); }
public Builder Exc(Type type) { return Exc(EcsTypeCodeManager.Get(type)); }
public Builder Inc(EcsTypeCode typeCode)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (_inc.Contains(typeCode) || _exc.Contains(typeCode)) { Throw.ConstraintIsAlreadyContainedInMask(); }
#endif
_inc.Add(typeCode);
return this;
}
public Builder Exc(EcsTypeCode typeCode)
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (_inc.Contains(typeCode) || _exc.Contains(typeCode)) { Throw.ConstraintIsAlreadyContainedInMask(); }
#endif
_exc.Add(typeCode);
return this;
}
#endregion
#region Build
public EcsStaticMask Build()
{
HashSet<EcsTypeCode> combinedIncs = _inc;
HashSet<EcsTypeCode> combinedExcs = _exc;
var inc = combinedIncs.ToArray();
Array.Sort(inc);
var exc = combinedExcs.ToArray();
Array.Sort(exc);
var key = new Key(inc, exc);
if (_ids.TryGetValue(key, out EcsStaticMask result) == false)
{
lock (_lock)
{
if (_ids.TryGetValue(key, out result) == false)
{
result = new EcsStaticMask(_idDIspenser.UseFree(), key);
_ids[key] = result;
}
}
}
return result;
}
#endregion
}
#endregion
}
#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-01-07 19:32:16 +08:00
public sealed class EcsMask : IEquatable<EcsMask>
2024-01-07 18:52:54 +08:00
{
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-02 22:13:10 +08:00
public static Builder New(EcsWorld world) { return new Builder(world); }
internal static EcsMask Create(int id, short worldID, int[] inc, int[] exc)
2024-01-07 18:52:54 +08:00
{
#if DEBUG
CheckConstraints(inc, exc);
#endif
2024-04-18 22:14:50 +08:00
return new EcsMask(id, worldID, inc, exc);
}
2024-10-02 22:13:10 +08:00
internal static EcsMask CreateEmpty(int id, short worldID)
2024-04-18 22:14:50 +08:00
{
return new EcsMask(id, worldID, new int[0], new int[0]);
}
2024-10-02 22:13:10 +08:00
internal static EcsMask CreateBroken(int id, short worldID)
2024-04-18 22:14:50 +08:00
{
return new EcsMask(id, worldID, new int[1] { 1 }, new int[1] { 1 });
}
private EcsMask(int id, short worldID, int[] inc, int[] exc)
{
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]);
if (bitJ.chankIndex != chankIndexX)
{
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
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
#if DEBUG
private static HashSet<int> _dummyHashSet = new HashSet<int>();
2024-04-18 22:14:50 +08:00
private static void CheckConstraints(int[] inc, int[] exc)
2024-01-07 18:52:54 +08:00
{
lock (_dummyHashSet)
{
2024-03-02 21:45:09 +08:00
if (CheckRepeats(inc)) { throw new EcsFrameworkException("The values in the Include constraints are repeated."); }
if (CheckRepeats(exc)) { throw new EcsFrameworkException("The values in the Exclude constraints are repeated."); }
2024-01-07 18:52:54 +08:00
_dummyHashSet.Clear();
_dummyHashSet.UnionWith(inc);
2024-03-02 21:45:09 +08:00
if (_dummyHashSet.Overlaps(exc)) { throw new EcsFrameworkException("Conflicting Include and Exclude constraints."); }
2024-01-07 18:52:54 +08:00
}
}
2024-04-18 22:14:50 +08:00
private static bool CheckRepeats(int[] array)
2024-01-07 18:52:54 +08:00
{
_dummyHashSet.Clear();
foreach (var item in array)
{
2024-03-02 21:45:09 +08:00
if (_dummyHashSet.Contains(item))
{
return true;
}
2024-01-07 18:52:54 +08:00
_dummyHashSet.Add(item);
}
return false;
}
#endif
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);
}
#endregion
#region OpMaskKey
private readonly struct OpMaskKey : IEquatable<OpMaskKey>
{
public readonly int leftMaskID;
public readonly int rightMaskID;
public readonly int operation;
public const int UNION_OP = 7;
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-02 22:13:10 +08:00
#region AbstractMask
public static EcsMask FromStatic(EcsWorld world, EcsStaticMask abstractMask)
{
return world.Get<WorldMaskComponent>().ConvertFromAbstract(abstractMask);
}
#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;
private readonly Dictionary<Key, EcsMask> _masks;
2024-04-15 01:18:08 +08:00
private readonly Dictionary<OpMaskKey, EcsMask> _opMasks;
2024-10-02 22:13:10 +08:00
private readonly SparseArray<EcsMask> _abstractMasks;
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-02 22:13:10 +08:00
public WorldMaskComponent(EcsWorld world, Dictionary<Key, EcsMask> masks, EcsMask emptyMask, EcsMask brokenMask)
2024-01-11 00:48:39 +08:00
{
_world = world;
_masks = masks;
2024-10-02 22:13:10 +08:00
_opMasks = new Dictionary<OpMaskKey, EcsMask>(256);
_abstractMasks = new SparseArray<EcsMask>(256);
2024-04-18 22:14:50 +08:00
EmptyMask = emptyMask;
BrokenMask = brokenMask;
2024-01-11 00:48:39 +08:00
}
public void Init(ref WorldMaskComponent component, EcsWorld world)
{
2024-04-18 22:14:50 +08:00
var masks = new Dictionary<Key, EcsMask>(256);
2024-10-02 22:13:10 +08:00
EcsMask emptyMask = CreateEmpty(0, world.id);
EcsMask brokenMask = CreateBroken(1, world.id);
2024-04-22 17:20:31 +08:00
masks.Add(new Key(emptyMask._inc, emptyMask._exc), emptyMask);
masks.Add(new Key(brokenMask._inc, brokenMask._exc), brokenMask);
2024-10-02 22:13:10 +08:00
component = new WorldMaskComponent(world, masks, emptyMask, brokenMask);
2024-01-11 00:48:39 +08:00
}
public void OnDestroy(ref WorldMaskComponent component, EcsWorld world)
{
component._masks.Clear();
2024-04-15 01:18:08 +08:00
component._opMasks.Clear();
2024-10-02 22:13:10 +08:00
component._abstractMasks.Clear();
2024-01-11 00:48:39 +08:00
component = default;
}
#endregion
#region GetMask
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-04-18 22:21:15 +08:00
result = New(a.World).Combine(a).Except(b).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-10-02 22:13:10 +08:00
internal EcsMask ConvertFromAbstract(EcsStaticMask abstractMask)
{
if (_abstractMasks.TryGetValue(abstractMask.ID, out EcsMask result) == false)
{
var b = New(_world);
foreach (var typeCode in abstractMask.IncTypeCodes)
{
b.Inc(_world.DeclareOrGetComponentTypeID(typeCode));
}
foreach (var typeCode in abstractMask.ExcTypeCodes)
{
b.Exc(_world.DeclareOrGetComponentTypeID(typeCode));
}
result = b.Build();
_abstractMasks.Add(abstractMask.ID, result);
}
return result;
}
2024-01-11 00:48:39 +08:00
internal EcsMask GetMask(Key maskKey)
{
2024-10-02 22:13:10 +08:00
if (_masks.TryGetValue(maskKey, out EcsMask result) == false)
2024-01-11 00:48:39 +08:00
{
2024-10-02 22:13:10 +08:00
result = Create(_masks.Count, _world.id, maskKey.inc, maskKey.exc);
2024-01-11 00:48:39 +08:00
_masks.Add(maskKey, result);
}
return result;
}
#endregion
}
private readonly struct Key : IEquatable<Key>
2024-01-07 18:52:54 +08:00
{
public readonly int[] inc;
public readonly int[] exc;
public readonly int hash;
2024-01-11 00:48:39 +08:00
#region Constructors
public Key(int[] inc, int[] exc)
2024-01-07 18:52:54 +08:00
{
this.inc = inc;
this.exc = exc;
2024-01-11 00:48:39 +08:00
unchecked
{
hash = inc.Length + exc.Length;
for (int i = 0, iMax = inc.Length; i < iMax; i++)
{
hash = hash * EcsConsts.MAGIC_PRIME + inc[i];
}
for (int i = 0, iMax = exc.Length; i < iMax; i++)
{
hash = hash * EcsConsts.MAGIC_PRIME - exc[i];
}
}
2024-01-07 18:52:54 +08:00
}
2024-01-11 00:48:39 +08:00
#endregion
#region Object
2024-01-07 18:52:54 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-01-11 00:48:39 +08:00
public bool Equals(Key other)
2024-01-07 18:52:54 +08:00
{
2024-10-02 22:13:10 +08:00
if (inc.Length != other.inc.Length) { return false; }
if (exc.Length != other.exc.Length) { return false; }
2024-01-07 18:52:54 +08:00
for (int i = 0; i < inc.Length; i++)
{
2024-10-02 22:13:10 +08:00
if (inc[i] != other.inc[i]) { return false; }
2024-01-07 18:52:54 +08:00
}
for (int i = 0; i < exc.Length; i++)
{
2024-10-02 22:13:10 +08:00
if (exc[i] != other.exc[i]) { return false; }
2024-01-07 18:52:54 +08:00
}
return true;
}
2024-10-02 22:13:10 +08:00
public override bool Equals(object obj) { return Equals((Key)obj); }
2024-01-07 18:52:54 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-09-07 17:18:35 +08:00
public override int GetHashCode() { return hash; }
2024-01-11 00:48:39 +08:00
#endregion
2024-01-07 18:52:54 +08:00
}
public class Builder
{
2024-01-07 19:32:16 +08:00
private readonly EcsWorld _world;
private readonly HashSet<int> _inc = new HashSet<int>();
private readonly HashSet<int> _exc = new HashSet<int>();
2024-04-18 22:14:50 +08:00
private readonly List<Combined> _combineds = new List<Combined>();
private readonly List<Excepted> _excepteds = new List<Excepted>();
2024-01-07 18:52:54 +08:00
2024-01-11 00:48:39 +08:00
#region Constrcutors
2024-01-07 18:52:54 +08:00
internal Builder(EcsWorld world)
{
_world = world;
}
2024-01-11 00:48:39 +08:00
#endregion
2024-01-07 18:52:54 +08:00
2024-10-02 22:13:10 +08:00
#region Inc/Exc/Combine
2024-10-05 10:19:52 +08:00
[Obsolete("Use Inc<T>()")] public Builder Include<T>() { return Inc<T>(); }
[Obsolete("Use Exc<T>()")] public Builder Exclude<T>() { return Exc<T>(); }
2024-10-02 22:13:10 +08:00
[Obsolete("Use Inc(type)")] public Builder Include(Type type) { return Inc(type); }
[Obsolete("Use Exc(type)")] public Builder Exclude(Type type) { return Exc(type); }
public Builder Inc<T>() { return Inc(_world.GetComponentTypeID<T>()); }
public Builder Exc<T>() { return Exc(_world.GetComponentTypeID<T>()); }
public Builder Inc(Type type) { return Inc(_world.GetComponentTypeID(type)); }
public Builder Exc(Type type) { return Exc(_world.GetComponentTypeID(type)); }
2024-04-15 01:18:08 +08:00
2024-10-02 22:13:10 +08:00
public Builder Inc(int componentTypeID)
2024-04-15 01:18:08 +08:00
{
2024-01-07 18:52:54 +08:00
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-10-02 22:13:10 +08:00
if (_inc.Contains(componentTypeID) || _exc.Contains(componentTypeID)) { Throw.ConstraintIsAlreadyContainedInMask(_world.GetComponentType(componentTypeID)); }
2024-01-07 18:52:54 +08:00
#endif
2024-10-02 22:13:10 +08:00
_inc.Add(componentTypeID);
2024-01-11 00:48:39 +08:00
return this;
2024-01-07 18:52:54 +08:00
}
2024-10-02 22:13:10 +08:00
public Builder Exc(int componentTypeID)
2024-01-07 18:52:54 +08:00
{
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
2024-10-02 22:13:10 +08:00
if (_inc.Contains(componentTypeID) || _exc.Contains(componentTypeID)) { Throw.ConstraintIsAlreadyContainedInMask(_world.GetComponentType(componentTypeID)); }
2024-01-07 18:52:54 +08:00
#endif
2024-10-02 22:13:10 +08:00
_exc.Add(componentTypeID);
2024-01-11 00:48:39 +08:00
return this;
2024-01-07 18:52:54 +08:00
}
2024-04-15 01:18:08 +08:00
2024-01-11 00:48:39 +08:00
public Builder Combine(EcsMask mask, int order = 0)
2024-01-07 18:52:54 +08:00
{
2024-04-18 22:14:50 +08:00
_combineds.Add(new Combined(mask, order));
return this;
}
public Builder Except(EcsMask mask, int order = 0)
{
_excepteds.Add(new Excepted(mask, order));
2024-01-11 00:48:39 +08:00
return this;
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-01-11 00:48:39 +08:00
#region Build
2024-01-07 18:52:54 +08:00
public EcsMask Build()
{
HashSet<int> combinedInc;
HashSet<int> combinedExc;
2024-04-18 22:14:50 +08:00
if (_combineds.Count > 0)
2024-01-07 18:52:54 +08:00
{
combinedInc = new HashSet<int>();
combinedExc = new HashSet<int>();
2024-04-18 22:14:50 +08:00
_combineds.Sort((a, b) => a.order - b.order);
foreach (var item in _combineds)
2024-01-07 18:52:54 +08:00
{
EcsMask submask = item.mask;
2024-04-22 17:20:31 +08:00
combinedInc.ExceptWith(submask._exc);//удаляю конфликтующие ограничения
combinedExc.ExceptWith(submask._inc);//удаляю конфликтующие ограничения
combinedInc.UnionWith(submask._inc);
combinedExc.UnionWith(submask._exc);
2024-01-07 18:52:54 +08:00
}
combinedInc.ExceptWith(_exc);//удаляю конфликтующие ограничения
combinedExc.ExceptWith(_inc);//удаляю конфликтующие ограничения
combinedInc.UnionWith(_inc);
combinedExc.UnionWith(_exc);
}
else
{
combinedInc = _inc;
combinedExc = _exc;
}
2024-04-18 22:14:50 +08:00
if (_excepteds.Count > 0)
{
foreach (var item in _excepteds)
{
2024-04-28 18:36:24 +08:00
if (combinedInc.Overlaps(item.mask._exc) || combinedExc.Overlaps(item.mask._inc))
2024-04-18 22:14:50 +08:00
{
_combineds.Clear();
_excepteds.Clear();
return _world.Get<WorldMaskComponent>().BrokenMask;
}
2024-04-22 17:20:31 +08:00
combinedInc.ExceptWith(item.mask._inc);
combinedExc.ExceptWith(item.mask._exc);
2024-04-18 22:14:50 +08:00
}
}
2024-01-07 18:52:54 +08:00
var inc = combinedInc.ToArray();
Array.Sort(inc);
var exc = combinedExc.ToArray();
Array.Sort(exc);
2024-04-18 22:14:50 +08:00
_combineds.Clear();
_excepteds.Clear();
2024-01-11 00:48:39 +08:00
return _world.Get<WorldMaskComponent>().GetMask(new Key(inc, exc));
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-01-07 19:32:16 +08:00
2024-01-07 18:52:54 +08:00
private readonly struct Combined
{
public readonly EcsMask mask;
public readonly int order;
public Combined(EcsMask mask, int order)
{
this.mask = mask;
this.order = order;
}
}
2024-04-18 22:14:50 +08:00
private readonly struct Excepted
{
public readonly EcsMask mask;
public readonly int order;
public Excepted(EcsMask mask, int order)
{
this.mask = mask;
this.order = order;
}
}
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;
public readonly int chankIndex;
public readonly int mask;
public EcsMaskChunck(int chankIndex, int mask)
{
this.chankIndex = chankIndex;
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()
{
return $"mask({chankIndex}, {mask}, {BitsUtility.CountBits(mask)})";
}
internal class DebuggerProxy
{
public int chunk;
public uint mask;
public int[] values = Array.Empty<int>();
public string bits;
public DebuggerProxy(EcsMaskChunck maskbits)
{
chunk = maskbits.chankIndex;
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
{
#region CountStructComparers
private readonly struct IncCountComparer : IStructComparer<int>
{
public readonly EcsWorld.PoolSlot[] counts;
public IncCountComparer(EcsWorld.PoolSlot[] counts)
{
this.counts = counts;
}
public int Compare(int a, int b)
{
return counts[a].count - counts[b].count;
}
}
private readonly struct ExcCountComparer : IStructComparer<int>
{
public readonly EcsWorld.PoolSlot[] counts;
public ExcCountComparer(EcsWorld.PoolSlot[] counts)
{
this.counts = counts;
}
public int Compare(int a, int b)
{
return counts[b].count - counts[a].count;
}
}
#endregion
internal EcsWorld _source;
internal EcsMask _mask;
private UnsafeArray<int> _sortIncBuffer;
private UnsafeArray<int> _sortExcBuffer;
private UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer;
private UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer;
#region Constructors
2024-08-24 12:29:58 +08:00
public unsafe EcsMaskIterator(EcsWorld source, EcsMask mask)
2024-08-23 22:31:43 +08:00
{
_source = source;
_mask = mask;
2024-08-24 12:29:58 +08:00
2024-08-24 21:02:53 +08:00
_sortIncBuffer = new UnsafeArray<int>(_mask._inc.Length);
_sortExcBuffer = new UnsafeArray<int>(_mask._exc.Length);
_sortIncChunckBuffer = new UnsafeArray<EcsMaskChunck>(_mask._incChunckMasks.Length);
_sortExcChunckBuffer = new UnsafeArray<EcsMaskChunck>(_mask._excChunckMasks.Length);
2024-08-24 12:29:58 +08:00
for (int i = 0; i < _sortIncBuffer.Length; i++)
{
_sortIncBuffer.ptr[i] = _mask._inc[i];
}
for (int i = 0; i < _sortExcBuffer.Length; i++)
{
_sortExcBuffer.ptr[i] = _mask._exc[i];
}
for (int i = 0; i < _sortIncChunckBuffer.Length; i++)
{
_sortIncChunckBuffer.ptr[i] = _mask._incChunckMasks[i];
}
for (int i = 0; i < _sortExcChunckBuffer.Length; i++)
{
_sortExcChunckBuffer.ptr[i] = _mask._excChunckMasks[i];
}
}
#endregion
#region Finalizator
unsafe ~EcsMaskIterator()
{
_sortIncBuffer.Dispose();
_sortExcBuffer.Dispose();
_sortIncChunckBuffer.Dispose();
_sortExcChunckBuffer.Dispose();
2024-08-23 22:31:43 +08:00
}
#endregion
#region Properties
public EcsWorld World
{
get { return _source; }
}
public EcsMask Mask
{
get { return _mask; }
}
#endregion
#region Enumerable
public Enumerable Iterate(EcsSpan span)
{
2024-08-24 13:10:50 +08:00
return new Enumerable(this, span);
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
public void CopyTo(EcsGroup group)
{
group.Clear();
var enumerator = GetEnumerator();
while (enumerator.MoveNext())
{
group.AddUnchecked(enumerator.Current);
}
}
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;
}
public EcsSpan CopyToSpan(ref int[] array)
{
int count = CopyTo(ref array);
return new EcsSpan(_iterator.World.id, array, count);
}
#endregion
#region Other
public override string ToString()
{
List<int> ints = new List<int>();
foreach (var e in this)
{
ints.Add(e);
}
return CollectionUtility.EntitiesToString(ints, "it");
}
#endregion
#region Enumerator
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Enumerator GetEnumerator() { return new Enumerator(_span, _iterator); }
public unsafe ref struct Enumerator
{
private ReadOnlySpan<int>.Enumerator _span;
private readonly int[] _entityComponentMasks;
private static EcsMaskChunck* _preSortedIncBuffer;
private static EcsMaskChunck* _preSortedExcBuffer;
private UnsafeArray<EcsMaskChunck> _sortIncChunckBuffer;
private UnsafeArray<EcsMaskChunck> _sortExcChunckBuffer;
private readonly int _entityComponentMaskLengthBitShift;
public unsafe Enumerator(EcsSpan span, EcsMaskIterator iterator)
{
_entityComponentMasks = iterator.World._entityComponentMasks;
_sortIncChunckBuffer = iterator._sortIncChunckBuffer;
_sortExcChunckBuffer = iterator._sortExcChunckBuffer;
_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;
if (_preSortedIncBuffer == null)
{
_preSortedIncBuffer = UnmanagedArrayUtility.New<EcsMaskChunck>(256);
_preSortedExcBuffer = UnmanagedArrayUtility.New<EcsMaskChunck>(256);
}
if (_sortIncChunckBuffer.Length > 1)
{
IncCountComparer incComparer = new IncCountComparer(counts);
UnsafeArraySortHalperX<int>.InsertionSort(_sortIncBuffer.ptr, _sortIncBuffer.Length, ref incComparer);
for (int i = 0; i < _sortIncBuffer.Length; i++)
{
_preSortedIncBuffer[i] = EcsMaskChunck.FromID(_sortIncBuffer.ptr[i]);
}
for (int i = 0, ii = 0; ii < _sortIncChunckBuffer.Length; ii++)
{
EcsMaskChunck chunkX = _preSortedIncBuffer[i];
int chankIndexX = chunkX.chankIndex;
int maskX = chunkX.mask;
for (int j = i + 1; j < _sortIncBuffer.Length; j++)
{
if (_preSortedIncBuffer[j].chankIndex == chankIndexX)
{
maskX |= _preSortedIncBuffer[j].mask;
}
}
_sortIncChunckBuffer.ptr[ii] = new EcsMaskChunck(chankIndexX, maskX);
while (++i < _sortIncBuffer.Length && _preSortedIncBuffer[i].chankIndex == chankIndexX)
{
// skip
}
}
}
if (_sortIncChunckBuffer.Length > 0 && counts[_sortIncBuffer.ptr[0]].count <= 0)
{
_span = span.Slice(0, 0).GetEnumerator();
return;
}
if (_sortExcChunckBuffer.Length > 1)
{
ExcCountComparer excComparer = new ExcCountComparer(counts);
UnsafeArraySortHalperX<int>.InsertionSort(_sortExcBuffer.ptr, _sortExcBuffer.Length, ref excComparer);
for (int i = 0; i < _sortExcBuffer.Length; i++)
{
_preSortedExcBuffer[i] = EcsMaskChunck.FromID(_sortExcBuffer.ptr[i]);
}
for (int i = 0, ii = 0; ii < _sortExcChunckBuffer.Length; ii++)
{
EcsMaskChunck bas = _preSortedExcBuffer[i];
int chankIndexX = bas.chankIndex;
int maskX = bas.mask;
for (int j = i + 1; j < _sortExcBuffer.Length; j++)
{
if (_preSortedExcBuffer[j].chankIndex == chankIndexX)
{
maskX |= _preSortedExcBuffer[j].mask;
}
}
_sortExcChunckBuffer.ptr[ii] = new EcsMaskChunck(chankIndexX, maskX);
while (++i < _sortExcBuffer.Length && _preSortedExcBuffer[i].chankIndex == chankIndexX)
{
// skip
}
}
}
#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.chankIndex] & bit.mask) != bit.mask)
{
goto skip;
}
}
for (int i = 0; i < _sortExcChunckBuffer.Length; i++)
{
var bit = _sortExcChunckBuffer.ptr[i];
if ((_entityComponentMasks[chunck + bit.chankIndex] & bit.mask) != 0)
{
goto skip;
}
}
return true;
skip: continue;
}
return false;
}
}
#endregion
}
#endregion
}
2024-08-24 12:29:58 +08:00
#endregion
2024-10-02 22:13:10 +08:00
}