< Summary

Information
Class: Lattice.Core.EntityRef
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/Core/EntityRef.cs
Line coverage
43%
Covered lines: 13
Uncovered lines: 17
Coverable lines: 30
Total lines: 177
Line coverage: 43.3%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsValid()100%210%
.ctor(...)100%11100%
.ctor(...)100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
Equals(...)100%11100%
Equals(...)0%620%
GetHashCode()100%210%
ToRaw()100%210%
FromRaw(...)100%11100%
ToString()100%11100%
TryParse(...)0%156120%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/Core/EntityRef.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4using System;
 5using System.Runtime.CompilerServices;
 6using System.Runtime.InteropServices;
 7using Lattice.ECS.Core;
 8using static Lattice.ECS.Core.DeterministicHash;
 9
 10namespace Lattice.Core
 11{
 12    /// <summary>
 13    /// 实体引用 - FrameSync 风格(对齐 EntityRef)
 14    ///
 15    /// 用于标识 ECS 中的唯一实体。
 16    /// 实现了稀疏集合的 ECS 模型(类似 enTT)。
 17    ///
 18    /// 内存布局:8 字节(Index + Version)
 19    /// - Index: int (4 bytes) - 实体在存储中的索引
 20    /// - Version: int (4 bytes) - 版本号,用于检测过期引用
 21    /// - Raw: ulong (8 bytes) - 与 Index/Version 重叠,用于快速比较
 22    ///
 23    /// 设计参考:Bevy ECS, FrameSync (Quantum), Entitas
 24    /// </summary>
 25    [StructLayout(LayoutKind.Explicit, Size = 8)]
 26    public readonly struct EntityRef : IEquatable<EntityRef>
 27    {
 28        /// <summary>
 29        /// 实体结构体的字节大小(8字节)
 30        /// </summary>
 31        public const int Size = 8;
 32
 33        /// <summary>
 34        /// 实体在内部数组中的索引位置
 35        /// </summary>
 36        [FieldOffset(0)]
 37        public readonly int Index;
 38
 39        /// <summary>
 40        /// 版本号:槽位复用时递增,用于检测失效引用
 41        /// 最高位同时用作活跃标志位(参见 EntityRegistry.ActiveBit)
 42        /// </summary>
 43        [FieldOffset(4)]
 44        public readonly int Version;
 45
 46        /// <summary>
 47        /// 原始64位值,用于快速比较和哈希计算
 48        /// </summary>
 49        [FieldOffset(0)]
 50        public readonly ulong Raw;
 51
 52        /// <summary>
 53        /// 表示无效实体的静态实例(Index=0, Version=0)
 54        /// </summary>
 55        public static readonly EntityRef None = default;
 56
 57        /// <summary>
 58        /// 无效实体引用(None 的别名,与 FrameSync 命名一致)
 59        /// </summary>
 60        public static readonly EntityRef Invalid = default;
 61
 62        /// <summary>
 63        /// 判断此实体标识符是否有效(非零)
 64        /// </summary>
 065        public bool IsValid => Raw != 0;
 66
 67        /// <summary>
 68        /// 创建一个新的实体引用
 69        /// </summary>
 70        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 71        public EntityRef(int index, int version)
 72        {
 3362473            Index = index;
 3362474            Version = version;
 3362475            Raw = (ulong)(uint)index | ((ulong)(uint)version << 32);
 3362476        }
 77
 78        /// <summary>
 79        /// 从原始值创建实体引用(用于序列化)
 80        /// </summary>
 81        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 82        public EntityRef(ulong raw)
 83        {
 384            Raw = raw;
 385            Index = (int)(raw & 0xFFFFFFFF);
 386            Version = (int)(raw >> 32);
 387        }
 88
 89        #region 相等性比较
 90
 91        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 92        public static bool operator ==(EntityRef a, EntityRef b)
 93        {
 12794            return a.Raw == b.Raw;
 95        }
 96
 97        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 98        public static bool operator !=(EntityRef a, EntityRef b)
 99        {
 226100            return a.Raw != b.Raw;
 101        }
 102
 103        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 104        public bool Equals(EntityRef other)
 105        {
 35106            return Raw == other.Raw;
 107        }
 108
 109        public override bool Equals(object? obj)
 110        {
 0111            return obj is EntityRef other && Equals(other);
 112        }
 113
 114        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 115        public override int GetHashCode()
 116        {
 117            // 使用确定性哈希(跨平台一致)
 0118            return DeterministicHash.GetHashCode(this);
 119        }
 120
 121        #endregion
 122
 123        #region 转换
 124
 125        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 126        public ulong ToRaw()
 127        {
 0128            return Raw;
 129        }
 130
 131        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 132        public static EntityRef FromRaw(ulong raw)
 133        {
 3134            return new EntityRef(raw);
 135        }
 136
 137        #endregion
 138
 139        #region 字符串表示
 140
 141        public override string ToString()
 142        {
 1143            return $"E.{Index:D5}.{Version:D3}";
 144        }
 145
 146        public static bool TryParse(string str, out EntityRef result)
 147        {
 0148            result = None;
 0149            if (string.IsNullOrEmpty(str) || str.Length < 3)
 0150                return false;
 151
 0152            if (!str.StartsWith("E.", StringComparison.Ordinal))
 0153                return false;
 154
 0155            var parts = str[2..].Split('.');
 0156            if (parts.Length != 2)
 0157                return false;
 158
 0159            if (int.TryParse(parts[0], out int index) &&
 0160                int.TryParse(parts[1], out int version))
 161            {
 0162                result = new EntityRef(index, version);
 0163                return true;
 164            }
 165
 0166            return false;
 167        }
 168
 169        #endregion
 170
 171        #region 序列化支持
 172
 173        // 序列化方法在 FrameSerializer 中实现
 174
 175        #endregion
 176    }
 177}