DragonECS/src/entlong.cs

395 lines
13 KiB
C#
Raw Normal View History

2025-03-14 16:53:25 +08:00
#if DISABLE_DEBUG
#undef DEBUG
#endif
#pragma warning disable IDE1006
2024-01-25 20:11:28 +08:00
#pragma warning disable CS8981
using DCFApixels.DragonECS.Core.Unchecked;
2023-06-26 02:53:55 +08:00
using DCFApixels.DragonECS.Internal;
2023-05-26 04:25:09 +08:00
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
2024-04-16 12:45:55 +08:00
//using System.Runtime.Serialization;
namespace DCFApixels.DragonECS
{
2023-05-26 04:25:09 +08:00
// [ id 32 | gen 16 | world 16 ]
/// <summary>Strong identifier/Permanent entity identifier</summary>
[StructLayout(LayoutKind.Explicit, Pack = 2, Size = 8)]
[DebuggerTypeProxy(typeof(DebuggerProxy))]
2024-04-16 12:45:55 +08:00
//[DataContract]
2023-07-17 15:54:28 +08:00
[Serializable]
#if UNITY_EDITOR
public
#else
public readonly
#endif
2025-03-10 20:10:08 +08:00
struct entlong : IEquatable<long>, IEquatable<entlong>, IComparable<entlong>
{
public static readonly entlong NULL = default;
2024-04-16 12:45:55 +08:00
//[DataMember]
[FieldOffset(0)]
#if UNITY_EDITOR
[UnityEngine.SerializeField]
internal
#else
internal readonly
#endif
long _full; //Union
2023-07-17 15:54:28 +08:00
[FieldOffset(0), NonSerialized]
2024-04-16 12:45:55 +08:00
internal readonly int _id;
2023-07-17 15:54:28 +08:00
[FieldOffset(4), NonSerialized]
2024-04-16 12:45:55 +08:00
internal readonly short _gen;
2023-07-17 15:54:28 +08:00
[FieldOffset(6), NonSerialized]
2024-04-16 12:45:55 +08:00
internal readonly short _world;
#region Properties
public bool IsAlive
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
get { return EcsWorld.GetWorld(_world).IsAlive(_id, _gen); }
}
public bool IsNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
get { return _full == 0L; }
}
public bool IsDeadOrNull
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return IsAlive == false; }
}
public int ID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false) { return EcsConsts.NULL_ENTITY_ID; }
#endif
2024-04-16 12:45:55 +08:00
return _id;
}
}
public short Gen
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false) { return default; }
#endif
2024-04-16 12:45:55 +08:00
return _gen;
}
}
public EcsWorld World
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#endif
2024-04-16 12:45:55 +08:00
return GetWorld_Internal();
}
}
public short WorldID
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false) { return EcsConsts.NULL_WORLD_ID; }
#endif
2024-04-16 12:45:55 +08:00
return _world;
}
}
#endregion
#region Constructors
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public entlong(int id, short gen, short world) : this()
{
2024-04-16 12:45:55 +08:00
_id = id;
_gen = gen;
_world = world;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal entlong(long full) : this()
{
2024-04-16 12:45:55 +08:00
_full = full;
}
2023-11-21 10:43:02 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe entlong NewUnsafe(long id, long gen, long world)
{
long x = id << 48 | gen << 32 | id;
return *(entlong*)&x;
}
#endregion
2024-04-16 12:45:55 +08:00
#region Unpacking
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetID(out int id)
{
2024-04-16 12:45:55 +08:00
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryGetWorld(out EcsWorld world)
{
2024-04-16 12:45:55 +08:00
world = EcsWorld.GetWorld(_world);
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-10 19:21:52 +08:00
public bool TryGetWorldID(out short worldID)
2023-07-03 02:40:13 +08:00
{
2024-04-16 12:45:55 +08:00
worldID = _world;
2023-07-03 02:40:13 +08:00
return IsAlive;
}
2025-04-05 00:18:50 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Unpack(out int id)
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false)
{
id = EcsConsts.NULL_ENTITY_ID;
return;
}
#endif
id = _id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 00:28:02 +08:00
public void Unpack(out int id, out EcsWorld world)
2023-07-03 02:40:13 +08:00
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false)
{
world = EcsWorld.GetWorld(EcsConsts.NULL_WORLD_ID);
id = EcsConsts.NULL_ENTITY_ID;
return;
}
#endif
2024-04-16 12:45:55 +08:00
world = EcsWorld.GetWorld(_world);
id = _id;
2023-07-03 02:40:13 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:09 +08:00
public void Unpack(out int id, out short gen, out EcsWorld world)
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false)
{
world = EcsWorld.GetWorld(EcsConsts.NULL_WORLD_ID);
gen = default;
id = EcsConsts.NULL_ENTITY_ID;
return;
}
#endif
2024-04-16 12:45:55 +08:00
world = EcsWorld.GetWorld(_world);
gen = _gen;
id = _id;
2024-03-03 22:46:09 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-10 19:21:52 +08:00
public void Unpack(out int id, out short worldID)
2023-07-03 02:40:13 +08:00
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false)
{
worldID = EcsConsts.NULL_WORLD_ID;
id = EcsConsts.NULL_ENTITY_ID;
return;
}
#endif
2024-04-16 12:45:55 +08:00
worldID = _world;
id = _id;
2023-07-03 02:40:13 +08:00
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 00:28:02 +08:00
public void Unpack(out int id, out short gen, out short worldID)
{
#if DEBUG
if (IsAlive == false) { Throw.Ent_ThrowIsNotAlive(this); }
#elif DRAGONECS_STABILITY_MODE
if (IsAlive == false)
{
worldID = EcsConsts.NULL_WORLD_ID;
gen = default;
id = EcsConsts.NULL_ENTITY_ID;
return;
}
#endif
2024-04-16 12:45:55 +08:00
worldID = _world;
gen = _gen;
id = _id;
2024-03-03 00:28:02 +08:00
}
2025-04-05 00:18:50 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryUnpack(out int id)
{
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 00:28:02 +08:00
public bool TryUnpack(out int id, out EcsWorld world)
{
2024-04-16 12:45:55 +08:00
world = GetWorld_Internal();
id = _id;
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 22:46:09 +08:00
public bool TryUnpack(out int id, out short gen, out EcsWorld world)
{
2024-04-16 12:45:55 +08:00
world = GetWorld_Internal();
gen = _gen;
id = _id;
2024-03-03 22:46:09 +08:00
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-10 19:21:52 +08:00
public bool TryUnpack(out int id, out short worldID)
2024-03-03 00:28:02 +08:00
{
2024-04-16 12:45:55 +08:00
worldID = _world;
id = _id;
2024-03-03 00:28:02 +08:00
return IsAlive;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-03-03 00:28:02 +08:00
public bool TryUnpack(out int id, out short gen, out short worldID)
2023-07-03 02:40:13 +08:00
{
2024-04-16 12:45:55 +08:00
worldID = _world;
gen = _gen;
id = _id;
2023-07-03 02:40:13 +08:00
return IsAlive;
}
2024-04-16 12:45:55 +08:00
#endregion
2024-04-16 12:45:55 +08:00
#region Unpacking
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int GetIDUnchecked()
{
2024-04-16 12:45:55 +08:00
return _id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsWorld GetWorldUnchecked()
{
2024-04-16 12:45:55 +08:00
return EcsWorld.GetWorld(_world);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public short GetWorldIDUnchecked()
{
2024-04-16 12:45:55 +08:00
return _world;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnpackUnchecked(out int id, out EcsWorld world)
{
2024-04-16 12:45:55 +08:00
world = EcsWorld.GetWorld(_world);
id = _id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnpackUnchecked(out int id, out short gen, out EcsWorld world)
{
2024-04-16 12:45:55 +08:00
world = EcsWorld.GetWorld(_world);
gen = _gen;
id = _id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnpackUnchecked(out int id, out short worldID)
{
2024-04-16 12:45:55 +08:00
worldID = _world;
id = _id;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void UnpackUnchecked(out int id, out short gen, out short worldID)
{
2024-04-16 12:45:55 +08:00
worldID = _world;
gen = _gen;
id = _id;
}
#endregion
2024-02-28 01:20:35 +08:00
#region Operators
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public static bool operator ==(entlong a, entlong b) { return a._full == b._full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public static bool operator !=(entlong a, entlong b) { return a._full != b._full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public static implicit operator entlong((int entityID, EcsWorld world) a) { return Combine_Internal(a.entityID, a.world); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public static implicit operator entlong((EcsWorld world, int entityID) a) { return Combine_Internal(a.entityID, a.world); }
2023-07-17 15:54:28 +08:00
2025-03-10 20:12:29 +08:00
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public static implicit operator entlong((entlong entity, EcsWorld world) a) { return Combine_Internal(a.entity._id, a.world); }
//[MethodImpl(MethodImplOptions.AggressiveInlining)]
//public static implicit operator entlong((EcsWorld world, entlong entity) a) { return Combine_Internal(a.entity._id, a.world); }
2024-04-16 12:45:55 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static entlong Combine_Internal(int entityID, EcsWorld world)
{
return world == null ? new entlong(entityID, 0, 0) : world.GetEntityLong(entityID);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator long(entlong a) { return a._full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static explicit operator entlong(long a) { return new entlong(a); }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-02-28 01:20:35 +08:00
public static explicit operator int(entlong a) { return a.ID; }
#endregion
2024-04-16 12:45:55 +08:00
#region Deconstruct
2024-02-28 01:20:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public void Deconstruct(out int id, out short gen, out short worldID) { Unpack(out id, out gen, out worldID); }
2024-02-28 01:20:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public void Deconstruct(out int id, out EcsWorld world) { Unpack(out id, out world); }
#endregion
#region Other
2024-02-28 01:20:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private EcsWorld GetWorld_Internal()
{
#if DRAGONECS_STABILITY_MODE
if (IsAlive == false) { EcsWorld.GetWorld(EcsConsts.NULL_WORLD_ID); }
#endif
return EcsWorld.GetWorld(_world);
}
2024-02-28 01:20:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public override int GetHashCode() { return unchecked((int)_full) ^ (int)(_full >> 32); }
2024-02-28 01:20:35 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public override string ToString() { return $"entity(id:{_id} g:{_gen} w:{_world} {(IsNull ? "null" : IsAlive ? "alive" : "not alive")})"; }
2024-01-26 18:58:54 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public override bool Equals(object obj) { return obj is entlong other && _full == other._full; }
2024-01-26 18:58:54 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-04-16 12:45:55 +08:00
public bool Equals(entlong other) { return _full == other._full; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(long other) { return _full == other; }
2025-03-10 20:10:08 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int CompareTo(entlong other) { return Compare(_id, other._id); }
2025-03-10 20:10:08 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Compare(entlong left, entlong right) { return left.CompareTo(right); }
2024-02-29 03:28:13 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Compare(int left, int right)
2024-02-29 03:28:13 +08:00
{
// NOTE: Because _id cannot be less than 0,
// the case “_id - other._id > MaxValue” is impossible.
return left - right;
2024-02-29 03:28:13 +08:00
}
internal class DebuggerProxy : EntityDebuggerProxy
2024-02-29 03:28:13 +08:00
{
public DebuggerProxy(entlong entity) : base(entity._id, entity._gen, entity._world) { }
2024-03-01 22:02:36 +08:00
}
2024-04-16 12:45:55 +08:00
#endregion
2024-02-29 03:28:13 +08:00
}
}