| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using Lattice.Core; |
| | | 9 | | using Lattice.ECS.Framework; |
| | | 10 | | using Lattice.Math; |
| | | 11 | | |
| | | 12 | | namespace Lattice.ECS.Core |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// ECS 帧 - 高性能非托管实现(FrameSync 风格) |
| | | 16 | | /// |
| | | 17 | | /// 设计原则: |
| | | 18 | | /// 1. 零 GC 压力 - 所有数据使用非托管内存 |
| | | 19 | | /// 2. O(1) 组件访问 - 稀疏集实现 |
| | | 20 | | /// 3. 缓存友好 - 密集数组存储 |
| | | 21 | | /// 4. 确定性 - 完全可预测的行为 |
| | | 22 | | /// </summary> |
| | | 23 | | public unsafe class Frame : IDisposable |
| | | 24 | | { |
| | | 25 | | #region 常量 |
| | | 26 | | |
| | | 27 | | /// <summary>最大组件类型数</summary> |
| | | 28 | | public const int MaxComponentTypes = 512; |
| | | 29 | | |
| | | 30 | | /// <summary>组件位图块数(512位 = 8个ulong)</summary> |
| | | 31 | | private const int ComponentMaskBlockCount = 8; |
| | | 32 | | |
| | | 33 | | #endregion |
| | | 34 | | |
| | | 35 | | #region 实体管理 |
| | | 36 | | |
| | | 37 | | /// <summary>实体数组(密集存储)</summary> |
| | | 38 | | private EntityRef* _entities; |
| | | 39 | | |
| | | 40 | | /// <summary>实体版本数组</summary> |
| | | 41 | | private ushort* _entityVersions; |
| | | 42 | | |
| | | 43 | | /// <summary>实体下一个空闲索引</summary> |
| | | 44 | | private int* _entityNextFree; |
| | | 45 | | |
| | | 46 | | /// <summary>实体组件位图(每个实体8个ulong)</summary> |
| | | 47 | | private ulong* _entityComponentMasks; |
| | | 48 | | |
| | | 49 | | /// <summary>实体数组容量</summary> |
| | | 50 | | private int _entityCapacity; |
| | | 51 | | |
| | | 52 | | /// <summary>实体数量</summary> |
| | | 53 | | private int _entityCount; |
| | | 54 | | |
| | | 55 | | /// <summary>空闲实体链表头</summary> |
| | | 56 | | private int _freeListHead; |
| | | 57 | | |
| | | 58 | | #endregion |
| | | 59 | | |
| | | 60 | | #region 组件存储 |
| | | 61 | | |
| | | 62 | | /// <summary>组件存储数组(类型ID -> 存储指针)</summary> |
| | | 63 | | private void** _componentStorages; |
| | | 64 | | |
| | | 65 | | /// <summary>组件存储是否已初始化</summary> |
| | | 66 | | private bool* _storageInitialized; |
| | | 67 | | |
| | | 68 | | /// <summary>延迟删除队列中的实体。</summary> |
| | | 69 | | private EntityRef* _pendingRemovalEntities; |
| | | 70 | | |
| | | 71 | | /// <summary>延迟删除队列中的组件类型 ID。</summary> |
| | | 72 | | private ushort* _pendingRemovalTypeIds; |
| | | 73 | | |
| | | 74 | | /// <summary>延迟删除队列数量。</summary> |
| | | 75 | | private int _pendingRemovalCount; |
| | | 76 | | |
| | | 77 | | /// <summary>延迟删除队列容量。</summary> |
| | | 78 | | private int _pendingRemovalCapacity; |
| | | 79 | | |
| | | 80 | | /// <summary>内存分配器</summary> |
| | | 81 | | private Allocator* _allocator; |
| | | 82 | | |
| | | 83 | | /// <summary>显式注册的 Owning Group 索引。</summary> |
| | | 84 | | private Dictionary<ulong, object>? _owningGroups; |
| | | 85 | | |
| | | 86 | | /// <summary>Owning Group 生命周期维护列表。</summary> |
| | | 87 | | private List<IFrameOwningGroup>? _owningGroupBindings; |
| | | 88 | | |
| | | 89 | | /// <summary>结构性修改命令缓冲。</summary> |
| | | 90 | | private CommandBuffer _structuralCommandBuffer; |
| | | 91 | | |
| | | 92 | | /// <summary>结构性修改命令缓冲是否已初始化。</summary> |
| | | 93 | | private bool _structuralCommandBufferInitialized; |
| | | 94 | | |
| | | 95 | | /// <summary>当前是否正在延迟结构性修改。</summary> |
| | | 96 | | private bool _deferStructuralChanges; |
| | | 97 | | |
| | | 98 | | /// <summary>当前是否正在回放结构性修改。</summary> |
| | | 99 | | private bool _isReplayingStructuralChanges; |
| | | 100 | | |
| | | 101 | | /// <summary>当前是否处于系统作者契约守卫作用域。</summary> |
| | | 102 | | private bool _hasActiveSystemAuthoringContract; |
| | | 103 | | |
| | | 104 | | /// <summary>当前执行系统的作者契约。</summary> |
| | | 105 | | private SystemAuthoringContract _activeSystemAuthoringContract; |
| | | 106 | | |
| | | 107 | | /// <summary>当前执行系统名称,仅用于守卫报错。</summary> |
| | | 108 | | private string? _activeSystemName; |
| | | 109 | | |
| | | 110 | | #endregion |
| | | 111 | | |
| | | 112 | | #region 属性 |
| | | 113 | | |
| | | 114 | | /// <summary>当前帧号</summary> |
| | 10941 | 115 | | public int Tick { get; set; } |
| | | 116 | | |
| | | 117 | | /// <summary>固定时间步长</summary> |
| | | 118 | | public FP DeltaTime |
| | | 119 | | { |
| | 942 | 120 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 942 | 121 | | get; |
| | 2429 | 122 | | set; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary>实体数量</summary> |
| | 5 | 126 | | public int EntityCount => _entityCount; |
| | | 127 | | |
| | | 128 | | /// <summary>实体容量。</summary> |
| | 1006 | 129 | | internal int EntityCapacity => _entityCapacity; |
| | | 130 | | |
| | | 131 | | /// <summary>待提交的延迟删除数量。</summary> |
| | 3 | 132 | | public int PendingDeferredRemovalCount => _pendingRemovalCount; |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// 当前是否存在尚未提交的结构性修改。 |
| | | 136 | | /// 主要用于运行时 Tick 管线与测试验证,不属于快照状态的一部分。 |
| | | 137 | | /// </summary> |
| | 2 | 138 | | public bool HasPendingStructuralChanges => _structuralCommandBufferInitialized && !_structuralCommandBuffer.IsEm |
| | | 139 | | |
| | | 140 | | #endregion |
| | | 141 | | |
| | | 142 | | #region 构造函数 |
| | | 143 | | |
| | 469 | 144 | | public Frame(int maxEntities = 65536) |
| | | 145 | | { |
| | 469 | 146 | | InitializeState(maxEntities); |
| | 469 | 147 | | } |
| | | 148 | | |
| | | 149 | | #endregion |
| | | 150 | | |
| | | 151 | | #region 快照与复制 |
| | | 152 | | |
| | | 153 | | /// <summary> |
| | | 154 | | /// 创建帧状态对象图快照。 |
| | | 155 | | /// 该接口主要保留给兼容、调试和显式恢复测试,不用于 Session 热路径。 |
| | | 156 | | /// </summary> |
| | | 157 | | [Obsolete("请优先改用 CapturePackedSnapshot()。CreateSnapshot() 仅保留给兼容、调试和显式恢复测试。", false)] |
| | | 158 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 159 | | public FrameSnapshot CreateSnapshot(ComponentSerializationMode mode = ComponentSerializationMode.Default) |
| | | 160 | | { |
| | 4 | 161 | | int metadataCount = _entityCount; |
| | 4 | 162 | | var entityVersions = new ushort[metadataCount]; |
| | 4 | 163 | | var entityNextFree = new int[metadataCount]; |
| | 4 | 164 | | var entityComponentMasks = new ulong[metadataCount * ComponentMaskBlockCount]; |
| | | 165 | | |
| | 4 | 166 | | if (metadataCount > 0) |
| | | 167 | | { |
| | 4 | 168 | | new ReadOnlySpan<ushort>(_entityVersions, metadataCount).CopyTo(entityVersions); |
| | 4 | 169 | | new ReadOnlySpan<int>(_entityNextFree, metadataCount).CopyTo(entityNextFree); |
| | 4 | 170 | | new ReadOnlySpan<ulong>(_entityComponentMasks, entityComponentMasks.Length).CopyTo(entityComponentMasks) |
| | | 171 | | } |
| | | 172 | | |
| | 4 | 173 | | var pendingEntities = new EntityRef[_pendingRemovalCount]; |
| | 4 | 174 | | var pendingTypeIds = new ushort[_pendingRemovalCount]; |
| | 8 | 175 | | for (int i = 0; i < _pendingRemovalCount; i++) |
| | | 176 | | { |
| | 0 | 177 | | pendingEntities[i] = _pendingRemovalEntities[i]; |
| | 0 | 178 | | pendingTypeIds[i] = _pendingRemovalTypeIds[i]; |
| | | 179 | | } |
| | | 180 | | |
| | 4 | 181 | | var snapshots = new ComponentStorageSnapshot[ComponentRegistry.Count]; |
| | 4 | 182 | | int snapshotCount = 0; |
| | 342 | 183 | | for (int typeId = 1; typeId <= ComponentRegistry.Count; typeId++) |
| | | 184 | | { |
| | 167 | 185 | | if (!_storageInitialized[typeId] || _componentStorages[typeId] == null) |
| | | 186 | | { |
| | | 187 | | continue; |
| | | 188 | | } |
| | | 189 | | |
| | 7 | 190 | | ComponentStorageSnapshot? storageSnapshot = ComponentRegistry.GetSnapshotCapturer(typeId)(_componentStor |
| | 7 | 191 | | if (storageSnapshot == null) |
| | | 192 | | { |
| | | 193 | | continue; |
| | | 194 | | } |
| | | 195 | | |
| | 6 | 196 | | snapshots[snapshotCount++] = storageSnapshot; |
| | | 197 | | } |
| | | 198 | | |
| | 4 | 199 | | Array.Resize(ref snapshots, snapshotCount); |
| | | 200 | | |
| | 4 | 201 | | return new FrameSnapshot( |
| | 4 | 202 | | Tick, |
| | 4 | 203 | | DeltaTime, |
| | 4 | 204 | | _entityCapacity, |
| | 4 | 205 | | _entityCount, |
| | 4 | 206 | | _freeListHead, |
| | 4 | 207 | | entityVersions, |
| | 4 | 208 | | entityNextFree, |
| | 4 | 209 | | entityComponentMasks, |
| | 4 | 210 | | pendingEntities, |
| | 4 | 211 | | pendingTypeIds, |
| | 4 | 212 | | snapshots); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// 创建紧凑字节帧快照。 |
| | | 217 | | /// 主要用于 Session 的 checkpoint 与采样历史。 |
| | | 218 | | /// 当前会同时记录 packed snapshot 格式版本和本次实际写出的组件 schema 摘要。 |
| | | 219 | | /// </summary> |
| | | 220 | | public PackedFrameSnapshot CapturePackedSnapshot(ComponentSerializationMode mode = ComponentSerializationMode.De |
| | | 221 | | { |
| | 163 | 222 | | var writer = new FrameStateBufferWriter(); |
| | 163 | 223 | | ComponentSchemaManifest schemaManifest = WritePackedState(writer, mode); |
| | 163 | 224 | | return writer.ToSnapshot(Tick, _entityCapacity, schemaManifest); |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | #endregion |
| | | 228 | | |
| | | 229 | | /// <summary> |
| | | 230 | | /// 从对象图快照恢复帧状态。 |
| | | 231 | | /// 该接口主要保留给兼容 API 与测试使用。 |
| | | 232 | | /// </summary> |
| | | 233 | | [Obsolete("请优先改用 RestoreFromPackedSnapshot()。RestoreFromSnapshot() 仅保留给兼容 API 与测试使用。", false)] |
| | | 234 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | | 235 | | public void RestoreFromSnapshot(FrameSnapshot snapshot, ComponentSerializationMode mode = ComponentSerialization |
| | | 236 | | { |
| | 3 | 237 | | if (snapshot == null) |
| | | 238 | | { |
| | 0 | 239 | | throw new ArgumentNullException(nameof(snapshot)); |
| | | 240 | | } |
| | | 241 | | |
| | 3 | 242 | | PrepareForSnapshotRestore(snapshot.EntityCapacity, reuseExistingState: false); |
| | | 243 | | |
| | 3 | 244 | | Tick = snapshot.Tick; |
| | 3 | 245 | | DeltaTime = snapshot.DeltaTime; |
| | 3 | 246 | | _entityCount = snapshot.EntityCount; |
| | 3 | 247 | | _freeListHead = snapshot.FreeListHead; |
| | 3 | 248 | | int metadataCount = snapshot.EntityVersions.Length; |
| | | 249 | | |
| | 3 | 250 | | if (metadataCount < snapshot.EntityCount || |
| | 3 | 251 | | snapshot.EntityNextFree.Length != metadataCount || |
| | 3 | 252 | | snapshot.EntityComponentMasks.Length != metadataCount * ComponentMaskBlockCount) |
| | | 253 | | { |
| | 0 | 254 | | throw new InvalidOperationException("Snapshot entity metadata size does not match frame capacity."); |
| | | 255 | | } |
| | | 256 | | |
| | 14 | 257 | | for (int i = 0; i < metadataCount; i++) |
| | | 258 | | { |
| | 4 | 259 | | _entityVersions[i] = snapshot.EntityVersions[i]; |
| | 4 | 260 | | _entityNextFree[i] = snapshot.EntityNextFree[i]; |
| | 4 | 261 | | _entities[i] = i < _entityCount ? new EntityRef(i, _entityVersions[i]) : EntityRef.None; |
| | | 262 | | } |
| | | 263 | | |
| | 46 | 264 | | for (int i = metadataCount; i < _entityCapacity; i++) |
| | | 265 | | { |
| | 20 | 266 | | _entityVersions[i] = 1; |
| | 20 | 267 | | _entityNextFree[i] = -1; |
| | 20 | 268 | | _entities[i] = EntityRef.None; |
| | | 269 | | } |
| | | 270 | | |
| | 70 | 271 | | for (int i = 0; i < snapshot.EntityComponentMasks.Length; i++) |
| | | 272 | | { |
| | 32 | 273 | | _entityComponentMasks[i] = snapshot.EntityComponentMasks[i]; |
| | | 274 | | } |
| | | 275 | | |
| | 326 | 276 | | for (int i = snapshot.EntityComponentMasks.Length; i < _entityCapacity * ComponentMaskBlockCount; i++) |
| | | 277 | | { |
| | 160 | 278 | | _entityComponentMasks[i] = 0; |
| | | 279 | | } |
| | | 280 | | |
| | 14 | 281 | | for (int i = 0; i < snapshot.ComponentStorages.Length; i++) |
| | | 282 | | { |
| | 4 | 283 | | ComponentStorageSnapshot storageSnapshot = snapshot.ComponentStorages[i]; |
| | 4 | 284 | | ComponentRegistry.GetSnapshotRestorer(storageSnapshot.TypeId)(this, storageSnapshot, mode); |
| | | 285 | | } |
| | | 286 | | |
| | 3 | 287 | | EnsurePendingRemovalCapacity(snapshot.PendingRemovalEntities.Length); |
| | 3 | 288 | | _pendingRemovalCount = snapshot.PendingRemovalEntities.Length; |
| | 6 | 289 | | for (int i = 0; i < _pendingRemovalCount; i++) |
| | | 290 | | { |
| | 0 | 291 | | _pendingRemovalEntities[i] = snapshot.PendingRemovalEntities[i]; |
| | 0 | 292 | | _pendingRemovalTypeIds[i] = snapshot.PendingRemovalTypeIds[i]; |
| | | 293 | | } |
| | | 294 | | |
| | 3 | 295 | | FinalizeSnapshotRestore(); |
| | 3 | 296 | | } |
| | | 297 | | |
| | | 298 | | /// <summary> |
| | | 299 | | /// 从紧凑字节帧快照恢复状态。 |
| | | 300 | | /// </summary> |
| | | 301 | | public void RestoreFromPackedSnapshot(PackedFrameSnapshot snapshot, ComponentSerializationMode mode = ComponentS |
| | | 302 | | { |
| | 76 | 303 | | if (snapshot == null) |
| | | 304 | | { |
| | 0 | 305 | | throw new ArgumentNullException(nameof(snapshot)); |
| | | 306 | | } |
| | | 307 | | |
| | 76 | 308 | | if (snapshot.FormatVersion != PackedFrameSnapshot.CurrentFormatVersion) |
| | | 309 | | { |
| | 0 | 310 | | throw new InvalidOperationException( |
| | 0 | 311 | | $"Packed snapshot format version {snapshot.FormatVersion} is not compatible with current runtime for |
| | | 312 | | } |
| | | 313 | | |
| | 76 | 314 | | if (snapshot.SchemaManifest.IsSpecified && snapshot.SchemaManifest.SerializationMode != mode) |
| | | 315 | | { |
| | 0 | 316 | | throw new InvalidOperationException( |
| | 0 | 317 | | $"Packed snapshot was captured with serialization mode {snapshot.SchemaManifest.SerializationMode}, |
| | | 318 | | } |
| | | 319 | | |
| | 76 | 320 | | var reader = new FrameStateReader(snapshot.Data, snapshot.Length); |
| | | 321 | | |
| | 76 | 322 | | int tick = reader.ReadInt32(); |
| | 76 | 323 | | long deltaTimeRaw = reader.ReadInt64(); |
| | 76 | 324 | | int entityCapacity = reader.ReadInt32(); |
| | 76 | 325 | | int entityCount = reader.ReadInt32(); |
| | 76 | 326 | | int freeListHead = reader.ReadInt32(); |
| | | 327 | | |
| | 76 | 328 | | PrepareForSnapshotRestore(entityCapacity, reuseExistingState: true); |
| | | 329 | | |
| | 76 | 330 | | Tick = tick; |
| | 76 | 331 | | DeltaTime = FP.FromRaw(deltaTimeRaw); |
| | 76 | 332 | | _entityCount = entityCount; |
| | 76 | 333 | | _freeListHead = freeListHead; |
| | | 334 | | |
| | 76 | 335 | | int metadataCount = reader.ReadInt32(); |
| | 76 | 336 | | if (metadataCount < entityCount) |
| | | 337 | | { |
| | 0 | 338 | | throw new InvalidOperationException("Packed snapshot entity metadata size does not match frame capacity. |
| | | 339 | | } |
| | | 340 | | |
| | 596 | 341 | | for (int i = 0; i < metadataCount; i++) |
| | | 342 | | { |
| | 222 | 343 | | _entityVersions[i] = reader.ReadUInt16(); |
| | | 344 | | } |
| | | 345 | | |
| | 5767876 | 346 | | for (int i = metadataCount; i < _entityCapacity; i++) |
| | | 347 | | { |
| | 2883862 | 348 | | _entityVersions[i] = 1; |
| | | 349 | | } |
| | | 350 | | |
| | 76 | 351 | | int nextFreeCount = reader.ReadInt32(); |
| | 76 | 352 | | if (nextFreeCount != metadataCount) |
| | | 353 | | { |
| | 0 | 354 | | throw new InvalidOperationException("Packed snapshot next-free metadata size does not match entity metad |
| | | 355 | | } |
| | | 356 | | |
| | 596 | 357 | | for (int i = 0; i < metadataCount; i++) |
| | | 358 | | { |
| | 222 | 359 | | _entityNextFree[i] = reader.ReadInt32(); |
| | 222 | 360 | | _entities[i] = i < _entityCount ? new EntityRef(i, _entityVersions[i]) : EntityRef.None; |
| | | 361 | | } |
| | | 362 | | |
| | 5767876 | 363 | | for (int i = metadataCount; i < _entityCapacity; i++) |
| | | 364 | | { |
| | 2883862 | 365 | | _entityNextFree[i] = -1; |
| | 2883862 | 366 | | _entities[i] = EntityRef.None; |
| | | 367 | | } |
| | | 368 | | |
| | 76 | 369 | | int maskCount = reader.ReadInt32(); |
| | 76 | 370 | | if (maskCount != metadataCount * ComponentMaskBlockCount) |
| | | 371 | | { |
| | 0 | 372 | | throw new InvalidOperationException("Packed snapshot component mask size does not match entity metadata. |
| | | 373 | | } |
| | | 374 | | |
| | 76 | 375 | | ReadOnlySpan<byte> maskBytes = reader.ReadBytes(maskCount * sizeof(ulong)); |
| | 76 | 376 | | fixed (byte* maskPtr = maskBytes) |
| | | 377 | | { |
| | 76 | 378 | | Buffer.MemoryCopy(maskPtr, _entityComponentMasks, sizeof(ulong) * _entityCapacity * ComponentMaskBlockCo |
| | | 379 | | } |
| | | 380 | | |
| | 46141944 | 381 | | for (int i = maskCount; i < _entityCapacity * ComponentMaskBlockCount; i++) |
| | | 382 | | { |
| | 23070896 | 383 | | _entityComponentMasks[i] = 0; |
| | | 384 | | } |
| | | 385 | | |
| | 76 | 386 | | int pendingEntityCount = reader.ReadInt32(); |
| | 76 | 387 | | EnsurePendingRemovalCapacity(pendingEntityCount); |
| | 76 | 388 | | _pendingRemovalCount = pendingEntityCount; |
| | 152 | 389 | | for (int i = 0; i < pendingEntityCount; i++) |
| | | 390 | | { |
| | 0 | 391 | | _pendingRemovalEntities[i] = EntityRef.FromRaw(reader.ReadUInt64()); |
| | | 392 | | } |
| | | 393 | | |
| | 76 | 394 | | int pendingTypeCount = reader.ReadInt32(); |
| | 76 | 395 | | if (pendingTypeCount != pendingEntityCount) |
| | | 396 | | { |
| | 0 | 397 | | throw new InvalidOperationException("Packed snapshot pending removal metadata size does not match."); |
| | | 398 | | } |
| | | 399 | | |
| | 152 | 400 | | for (int i = 0; i < pendingTypeCount; i++) |
| | | 401 | | { |
| | 0 | 402 | | _pendingRemovalTypeIds[i] = reader.ReadUInt16(); |
| | | 403 | | } |
| | | 404 | | |
| | 76 | 405 | | int storageCount = reader.ReadInt32(); |
| | 76 | 406 | | ResetInitializedStorages(); |
| | 76 | 407 | | Span<int> serializedTypeIds = stackalloc int[MaxComponentTypes]; |
| | | 408 | | |
| | 630 | 409 | | for (int i = 0; i < storageCount; i++) |
| | | 410 | | { |
| | 239 | 411 | | int typeId = reader.ReadInt32(); |
| | 239 | 412 | | serializedTypeIds[i] = typeId; |
| | 239 | 413 | | ComponentRegistry.GetPackedStateRestorer(typeId)(this, reader, mode); |
| | 239 | 414 | | _storageInitialized[typeId] = true; |
| | | 415 | | } |
| | | 416 | | |
| | 76 | 417 | | if (snapshot.SchemaManifest.IsSpecified) |
| | | 418 | | { |
| | 76 | 419 | | ComponentSchemaManifest currentSchema = ComponentRegistry.CreateSchemaManifest(serializedTypeIds[..stora |
| | 76 | 420 | | if (!currentSchema.Equals(snapshot.SchemaManifest)) |
| | | 421 | | { |
| | 1 | 422 | | throw new InvalidOperationException( |
| | 1 | 423 | | $"Packed snapshot component schema fingerprint does not match the current registry for mode {mod |
| | 1 | 424 | | $"Snapshot={snapshot.SchemaManifest.Fingerprint}, Current={currentSchema.Fingerprint}."); |
| | | 425 | | } |
| | | 426 | | } |
| | | 427 | | |
| | 75 | 428 | | FinalizeSnapshotRestore(); |
| | 75 | 429 | | } |
| | | 430 | | |
| | | 431 | | /// <summary> |
| | | 432 | | /// 克隆当前帧。 |
| | | 433 | | /// </summary> |
| | | 434 | | public Frame Clone(ComponentSerializationMode mode = ComponentSerializationMode.Default) |
| | | 435 | | { |
| | 30 | 436 | | var clone = new Frame(_entityCapacity); |
| | 30 | 437 | | clone.RestoreFromPackedSnapshot(CapturePackedSnapshot(mode), mode); |
| | 30 | 438 | | return clone; |
| | | 439 | | } |
| | | 440 | | |
| | | 441 | | /// <summary> |
| | | 442 | | /// 克隆当前帧的完整运行时状态。 |
| | | 443 | | /// 该路径保留所有组件状态,供预测推进、回滚和历史补帧复用。 |
| | | 444 | | /// </summary> |
| | | 445 | | public Frame CloneState() |
| | | 446 | | { |
| | 97 | 447 | | var clone = new Frame(_entityCapacity); |
| | 97 | 448 | | clone.CopyStateFrom(this); |
| | 97 | 449 | | return clone; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | /// <summary> |
| | | 453 | | /// 从另一个帧复制状态到当前帧。 |
| | | 454 | | /// </summary> |
| | | 455 | | public void CopyFrom(Frame source, ComponentSerializationMode mode = ComponentSerializationMode.Default) |
| | | 456 | | { |
| | 0 | 457 | | ArgumentNullException.ThrowIfNull(source); |
| | 0 | 458 | | RestoreFromPackedSnapshot(source.CapturePackedSnapshot(mode), mode); |
| | 0 | 459 | | } |
| | | 460 | | |
| | | 461 | | /// <summary> |
| | | 462 | | /// 从另一个帧直接复制完整运行时状态,避免热路径走托管快照分配。 |
| | | 463 | | /// </summary> |
| | | 464 | | public void CopyStateFrom(Frame source) |
| | | 465 | | { |
| | 691 | 466 | | ArgumentNullException.ThrowIfNull(source); |
| | | 467 | | |
| | 691 | 468 | | if (ReferenceEquals(this, source)) |
| | | 469 | | { |
| | 0 | 470 | | return; |
| | | 471 | | } |
| | | 472 | | |
| | 691 | 473 | | bool preserveOwningGroups = _owningGroupBindings != null && _owningGroupBindings.Count > 0; |
| | 691 | 474 | | EnsureCompatibleStateForCopy(source._entityCapacity, preserveOwningGroups); |
| | 691 | 475 | | ResetStructuralChangeTracking(); |
| | | 476 | | |
| | 691 | 477 | | Tick = source.Tick; |
| | 691 | 478 | | DeltaTime = source.DeltaTime; |
| | 691 | 479 | | _entityCount = source._entityCount; |
| | 691 | 480 | | _freeListHead = source._freeListHead; |
| | | 481 | | |
| | 691 | 482 | | int entityBytes = sizeof(EntityRef) * source._entityCapacity; |
| | 691 | 483 | | int versionBytes = sizeof(ushort) * source._entityCapacity; |
| | 691 | 484 | | int nextFreeBytes = sizeof(int) * source._entityCapacity; |
| | 691 | 485 | | int maskBytes = sizeof(ulong) * source._entityCapacity * ComponentMaskBlockCount; |
| | | 486 | | |
| | 691 | 487 | | Buffer.MemoryCopy(source._entities, _entities, entityBytes, entityBytes); |
| | 691 | 488 | | Buffer.MemoryCopy(source._entityVersions, _entityVersions, versionBytes, versionBytes); |
| | 691 | 489 | | Buffer.MemoryCopy(source._entityNextFree, _entityNextFree, nextFreeBytes, nextFreeBytes); |
| | 691 | 490 | | Buffer.MemoryCopy(source._entityComponentMasks, _entityComponentMasks, maskBytes, maskBytes); |
| | | 491 | | |
| | 691 | 492 | | EnsurePendingRemovalCapacity(source._pendingRemovalCount); |
| | 691 | 493 | | _pendingRemovalCount = source._pendingRemovalCount; |
| | 691 | 494 | | if (_pendingRemovalCount > 0) |
| | | 495 | | { |
| | 0 | 496 | | int pendingEntityBytes = sizeof(EntityRef) * _pendingRemovalCount; |
| | 0 | 497 | | int pendingTypeBytes = sizeof(ushort) * _pendingRemovalCount; |
| | 0 | 498 | | Buffer.MemoryCopy(source._pendingRemovalEntities, _pendingRemovalEntities, pendingEntityBytes, pendingEn |
| | 0 | 499 | | Buffer.MemoryCopy(source._pendingRemovalTypeIds, _pendingRemovalTypeIds, pendingTypeBytes, pendingTypeBy |
| | | 500 | | } |
| | | 501 | | |
| | 67368 | 502 | | for (int typeId = 1; typeId <= ComponentRegistry.Count; typeId++) |
| | | 503 | | { |
| | 32993 | 504 | | if (source._storageInitialized[typeId] && source._componentStorages[typeId] != null) |
| | | 505 | | { |
| | 2113 | 506 | | ComponentRegistry.GetStorageCopier(typeId)(this, source._componentStorages[typeId]); |
| | 2113 | 507 | | _storageInitialized[typeId] = true; |
| | 2113 | 508 | | continue; |
| | | 509 | | } |
| | | 510 | | |
| | 30880 | 511 | | if (_storageInitialized[typeId] && _componentStorages[typeId] != null) |
| | | 512 | | { |
| | 0 | 513 | | ComponentRegistry.GetStorageResetter(typeId)(_componentStorages[typeId]); |
| | | 514 | | } |
| | | 515 | | } |
| | | 516 | | |
| | 691 | 517 | | RebuildOwningGroups(); |
| | 691 | 518 | | } |
| | | 519 | | |
| | | 520 | | /// <summary> |
| | | 521 | | /// 计算帧校验和。 |
| | | 522 | | /// </summary> |
| | | 523 | | public ulong CalculateChecksum(ComponentSerializationMode mode = ComponentSerializationMode.Checkpoint) |
| | | 524 | | { |
| | 82 | 525 | | var writer = new FrameStateChecksumWriter(); |
| | 82 | 526 | | WritePackedState(writer, mode); |
| | 82 | 527 | | return writer.Checksum; |
| | | 528 | | } |
| | | 529 | | |
| | | 530 | | #region 实体操作 |
| | | 531 | | |
| | | 532 | | /// <summary> |
| | | 533 | | /// 创建实体 - O(1)。 |
| | | 534 | | /// 若当前帧正处于运行时的结构性修改延迟阶段,则返回临时实体引用,并在后续提交阶段真正物化。 |
| | | 535 | | /// </summary> |
| | | 536 | | public EntityRef CreateEntity() |
| | | 537 | | { |
| | 590 | 538 | | EnsureStructuralChangesAllowed(nameof(CreateEntity)); |
| | | 539 | | |
| | 589 | 540 | | if (ShouldDeferStructuralChanges()) |
| | | 541 | | { |
| | 202 | 542 | | return QueueCreateEntity(); |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | int index; |
| | | 546 | | ushort version; |
| | | 547 | | |
| | 387 | 548 | | if (_freeListHead >= 0) |
| | | 549 | | { |
| | | 550 | | // 复用空闲槽位 |
| | 150 | 551 | | index = _freeListHead; |
| | 150 | 552 | | _freeListHead = _entityNextFree[index]; |
| | 150 | 553 | | version = _entityVersions[index]; |
| | | 554 | | } |
| | | 555 | | else |
| | | 556 | | { |
| | | 557 | | // 分配新槽位 |
| | 237 | 558 | | if (_entityCount >= _entityCapacity) |
| | 0 | 559 | | throw new InvalidOperationException("Entity limit reached"); |
| | | 560 | | |
| | 237 | 561 | | index = _entityCount++; |
| | 237 | 562 | | version = 1; |
| | 237 | 563 | | _entityVersions[index] = version; |
| | | 564 | | } |
| | | 565 | | |
| | | 566 | | // 清空组件位图 |
| | 387 | 567 | | ClearComponentMask(index); |
| | | 568 | | |
| | 387 | 569 | | var entity = new EntityRef(index, version); |
| | 387 | 570 | | _entities[index] = entity; |
| | 387 | 571 | | return entity; |
| | | 572 | | } |
| | | 573 | | |
| | | 574 | | /// <summary> |
| | | 575 | | /// 销毁实体 - O(C),C为组件数 |
| | | 576 | | /// </summary> |
| | | 577 | | public void DestroyEntity(EntityRef entity) |
| | | 578 | | { |
| | 339 | 579 | | EnsureStructuralChangesAllowed(nameof(DestroyEntity)); |
| | | 580 | | |
| | 339 | 581 | | if (ShouldDeferStructuralChanges()) |
| | | 582 | | { |
| | 166 | 583 | | QueueDestroyEntity(entity); |
| | 166 | 584 | | return; |
| | | 585 | | } |
| | | 586 | | |
| | 173 | 587 | | if (!IsValid(entity)) return; |
| | | 588 | | |
| | | 589 | | // 删除所有组件 |
| | 173 | 590 | | ulong* mask = GetComponentMaskPointer(entity.Index); |
| | 177498 | 591 | | for (int typeId = 0; typeId < MaxComponentTypes; typeId++) |
| | | 592 | | { |
| | 88576 | 593 | | int block = typeId >> 6; |
| | 88576 | 594 | | int bit = typeId & 0x3F; |
| | 88576 | 595 | | if ((mask[block] & (1UL << bit)) != 0) |
| | | 596 | | { |
| | 797 | 597 | | RemoveComponentInternal(entity, typeId); |
| | | 598 | | } |
| | | 599 | | } |
| | | 600 | | |
| | | 601 | | // 版本递增(使旧引用失效) |
| | 173 | 602 | | _entityVersions[entity.Index]++; |
| | | 603 | | |
| | | 604 | | // 加入空闲链表 |
| | 173 | 605 | | _entityNextFree[entity.Index] = _freeListHead; |
| | 173 | 606 | | _freeListHead = entity.Index; |
| | 173 | 607 | | } |
| | | 608 | | |
| | | 609 | | /// <summary> |
| | | 610 | | /// 检查实体是否有效 - O(1) |
| | | 611 | | /// </summary> |
| | | 612 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 613 | | public bool IsValid(EntityRef entity) |
| | | 614 | | { |
| | 2840 | 615 | | uint index = (uint)entity.Index; |
| | 2840 | 616 | | return index < (uint)_entityCount && |
| | 2840 | 617 | | _entityVersions[index] == entity.Version; |
| | | 618 | | } |
| | | 619 | | |
| | | 620 | | #endregion |
| | | 621 | | |
| | | 622 | | #region 组件操作 |
| | | 623 | | |
| | | 624 | | /// <summary> |
| | | 625 | | /// 添加组件 - O(1) 均摊。 |
| | | 626 | | /// 若当前帧正处于运行时的结构性修改延迟阶段,则该操作会先排入结构提交缓冲,而不是立即影响查询拓扑。 |
| | | 627 | | /// </summary> |
| | | 628 | | public void Add<T>(EntityRef entity, in T component) where T : unmanaged, IComponent |
| | | 629 | | { |
| | 2234 | 630 | | EnsureStructuralChangesAllowed(nameof(Add)); |
| | | 631 | | |
| | 2234 | 632 | | if (ShouldDeferStructuralChanges()) |
| | | 633 | | { |
| | 950 | 634 | | QueueAddComponent(entity, component); |
| | 950 | 635 | | return; |
| | | 636 | | } |
| | | 637 | | |
| | 1284 | 638 | | if (!IsValid(entity)) |
| | 0 | 639 | | throw new ArgumentException($"Invalid entity: {entity}"); |
| | | 640 | | |
| | 1284 | 641 | | int typeId = ComponentTypeId<T>.Id; |
| | 1284 | 642 | | var storage = GetOrCreateStorage<T>(typeId); |
| | 1284 | 643 | | bool hasComponentMask = HasComponentMask(entity.Index, typeId); |
| | | 644 | | |
| | 1284 | 645 | | if (storage->Has(entity)) |
| | | 646 | | { |
| | 0 | 647 | | if (storage->IsDeferredRemoval && !hasComponentMask) |
| | | 648 | | { |
| | 0 | 649 | | int pendingQueueIndex = storage->GetPendingQueueIndex(entity); |
| | 0 | 650 | | storage->CancelPendingRemoval(entity); |
| | 0 | 651 | | RemovePendingRemovalAt(pendingQueueIndex); |
| | 0 | 652 | | storage->Get(entity) = component; |
| | 0 | 653 | | SetComponentMask(entity.Index, typeId); |
| | 0 | 654 | | InvokeOnAdded(entity, storage); |
| | 0 | 655 | | return; |
| | | 656 | | } |
| | | 657 | | |
| | 0 | 658 | | throw new InvalidOperationException($"Component {typeof(T).Name} already exists on entity {entity}"); |
| | | 659 | | } |
| | | 660 | | |
| | 1284 | 661 | | storage->Add(entity, component); |
| | 1284 | 662 | | SetComponentMask(entity.Index, typeId); |
| | 1284 | 663 | | InvokeOnAdded(entity, storage); |
| | 1284 | 664 | | NotifyOwningGroups(entity, typeId); |
| | 1284 | 665 | | } |
| | | 666 | | |
| | | 667 | | /// <summary> |
| | | 668 | | /// 设置组件值;若组件不存在则添加。 |
| | | 669 | | /// 当运行时开启结构性修改延迟时,对已存在组件的字段写入仍会立即生效; |
| | | 670 | | /// 但对缺失组件或临时实体的写入会延迟到结构提交阶段统一生效。 |
| | | 671 | | /// </summary> |
| | | 672 | | public void Set<T>(EntityRef entity, in T component) where T : unmanaged, IComponent |
| | | 673 | | { |
| | 105 | 674 | | if (ShouldDeferSet<T>(entity)) |
| | | 675 | | { |
| | 0 | 676 | | EnsureStructuralChangesAllowed(nameof(Set)); |
| | 0 | 677 | | QueueSetComponent(entity, component); |
| | 0 | 678 | | return; |
| | | 679 | | } |
| | | 680 | | |
| | 105 | 681 | | if (!IsValid(entity)) |
| | 0 | 682 | | throw new ArgumentException($"Invalid entity: {entity}"); |
| | | 683 | | |
| | 105 | 684 | | int typeId = ComponentTypeId<T>.Id; |
| | 105 | 685 | | var storage = GetOrCreateStorage<T>(typeId); |
| | | 686 | | |
| | 105 | 687 | | if (HasComponentMask(entity.Index, typeId) && storage->Has(entity)) |
| | | 688 | | { |
| | 104 | 689 | | storage->Get(entity) = component; |
| | 104 | 690 | | NotifyOwningGroups(entity, typeId); |
| | 104 | 691 | | return; |
| | | 692 | | } |
| | | 693 | | |
| | 1 | 694 | | if (storage->IsDeferredRemoval && storage->Has(entity)) |
| | | 695 | | { |
| | 1 | 696 | | int pendingQueueIndex = storage->GetPendingQueueIndex(entity); |
| | 1 | 697 | | storage->CancelPendingRemoval(entity); |
| | 1 | 698 | | RemovePendingRemovalAt(pendingQueueIndex); |
| | 1 | 699 | | storage->Get(entity) = component; |
| | 1 | 700 | | SetComponentMask(entity.Index, typeId); |
| | 1 | 701 | | InvokeOnAdded(entity, storage); |
| | 1 | 702 | | NotifyOwningGroups(entity, typeId); |
| | 1 | 703 | | return; |
| | | 704 | | } |
| | | 705 | | |
| | 0 | 706 | | Add(entity, component); |
| | 0 | 707 | | } |
| | | 708 | | |
| | | 709 | | /// <summary> |
| | | 710 | | /// 移除组件 - O(1)。 |
| | | 711 | | /// 若当前帧正处于运行时的结构性修改延迟阶段,则移除会延迟到结构提交阶段统一生效。 |
| | | 712 | | /// </summary> |
| | | 713 | | public void Remove<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 714 | | { |
| | 809 | 715 | | EnsureStructuralChangesAllowed(nameof(Remove)); |
| | | 716 | | |
| | 809 | 717 | | if (ShouldDeferStructuralChanges()) |
| | | 718 | | { |
| | 1 | 719 | | QueueRemoveComponent<T>(entity); |
| | 1 | 720 | | return; |
| | | 721 | | } |
| | | 722 | | |
| | 808 | 723 | | if (!IsValid(entity)) return; |
| | | 724 | | |
| | 808 | 725 | | int typeId = ComponentTypeId<T>.Id; |
| | 808 | 726 | | if (!HasComponentMask(entity.Index, typeId)) return; |
| | | 727 | | |
| | 808 | 728 | | var storage = GetStorage<T>(typeId); |
| | 808 | 729 | | if (storage == null || !storage->Has(entity)) return; |
| | | 730 | | |
| | 808 | 731 | | ComponentChangedDelegate? onRemoved = ComponentTypeId<T>.Callbacks.OnRemoved; |
| | 808 | 732 | | if (onRemoved != null) |
| | | 733 | | { |
| | 1 | 734 | | T* removedComponent = storage->GetPointer(entity); |
| | 1 | 735 | | if (removedComponent != null) |
| | | 736 | | { |
| | 1 | 737 | | onRemoved(entity, removedComponent, this); |
| | | 738 | | } |
| | | 739 | | } |
| | | 740 | | |
| | 808 | 741 | | if (storage->IsDeferredRemoval) |
| | | 742 | | { |
| | 8 | 743 | | storage->MarkForRemoval(entity); |
| | 8 | 744 | | ClearComponentMaskBit(entity.Index, typeId); |
| | 8 | 745 | | int pendingQueueIndex = EnqueuePendingRemoval(entity, typeId); |
| | 8 | 746 | | storage->SetPendingQueueIndex(entity, pendingQueueIndex); |
| | 8 | 747 | | NotifyOwningGroups(entity, typeId); |
| | 8 | 748 | | return; |
| | | 749 | | } |
| | | 750 | | |
| | 800 | 751 | | storage->Remove(entity); |
| | 800 | 752 | | ClearComponentMaskBit(entity.Index, typeId); |
| | 800 | 753 | | NotifyOwningGroups(entity, typeId); |
| | 800 | 754 | | } |
| | | 755 | | |
| | | 756 | | /// <summary> |
| | | 757 | | /// 获取组件引用 - O(1),2次内存访问 |
| | | 758 | | /// </summary> |
| | | 759 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 760 | | public ref T Get<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 761 | | { |
| | 139 | 762 | | if (!IsValid(entity)) |
| | 1 | 763 | | throw new ArgumentException($"Invalid entity: {entity}"); |
| | | 764 | | |
| | 138 | 765 | | int typeId = ComponentTypeId<T>.Id; |
| | 138 | 766 | | if (!HasComponentMask(entity.Index, typeId)) |
| | 0 | 767 | | throw new InvalidOperationException($"Component {typeof(T).Name} not found"); |
| | | 768 | | |
| | 138 | 769 | | var storage = GetStorage<T>(typeId); |
| | 138 | 770 | | if (storage == null) |
| | 0 | 771 | | throw new InvalidOperationException($"Component {typeof(T).Name} not found"); |
| | 138 | 772 | | return ref storage->Get(entity); |
| | | 773 | | } |
| | | 774 | | |
| | | 775 | | /// <summary> |
| | | 776 | | /// 获取组件指针 - O(1),2次内存访问 |
| | | 777 | | /// </summary> |
| | | 778 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 779 | | public T* GetPointer<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 780 | | { |
| | 15 | 781 | | if (!IsValid(entity)) |
| | 1 | 782 | | return null; |
| | | 783 | | |
| | 14 | 784 | | int typeId = ComponentTypeId<T>.Id; |
| | 14 | 785 | | if (!HasComponentMask(entity.Index, typeId)) |
| | 0 | 786 | | return null; |
| | | 787 | | |
| | 14 | 788 | | var storage = GetStorage<T>(typeId); |
| | 14 | 789 | | return storage != null ? storage->GetPointer(entity) : null; |
| | | 790 | | } |
| | | 791 | | |
| | | 792 | | /// <summary> |
| | | 793 | | /// 尝试获取组件 - O(1) |
| | | 794 | | /// </summary> |
| | | 795 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 796 | | public bool TryGet<T>(EntityRef entity, out T component) where T : unmanaged, IComponent |
| | | 797 | | { |
| | 1 | 798 | | if (!IsValid(entity)) |
| | | 799 | | { |
| | 1 | 800 | | component = default; |
| | 1 | 801 | | return false; |
| | | 802 | | } |
| | | 803 | | |
| | 0 | 804 | | int typeId = ComponentTypeId<T>.Id; |
| | 0 | 805 | | if (!HasComponentMask(entity.Index, typeId)) |
| | | 806 | | { |
| | 0 | 807 | | component = default; |
| | 0 | 808 | | return false; |
| | | 809 | | } |
| | | 810 | | |
| | 0 | 811 | | var storage = GetStorage<T>(typeId); |
| | 0 | 812 | | if (storage != null) |
| | 0 | 813 | | return storage->TryGet(entity, out component); |
| | 0 | 814 | | component = default; |
| | 0 | 815 | | return false; |
| | | 816 | | } |
| | | 817 | | |
| | | 818 | | /// <summary> |
| | | 819 | | /// 检查是否有组件 - O(1) |
| | | 820 | | /// </summary> |
| | | 821 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 822 | | public bool Has<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 823 | | { |
| | 27 | 824 | | if (!IsValid(entity)) return false; |
| | 25 | 825 | | int typeId = ComponentTypeId<T>.Id; |
| | 25 | 826 | | if (!HasComponentMask(entity.Index, typeId)) |
| | | 827 | | { |
| | 4 | 828 | | return false; |
| | | 829 | | } |
| | | 830 | | |
| | 21 | 831 | | var storage = GetStorage<T>(typeId); |
| | 21 | 832 | | return storage != null && storage->Has(entity); |
| | | 833 | | } |
| | | 834 | | |
| | | 835 | | /// <summary> |
| | | 836 | | /// 检查当前帧是否存在指定的全局 singleton 组件。 |
| | | 837 | | /// 该 API 是玩法层正式推荐的全局状态入口,不再要求外部自己约定“特殊实体”。 |
| | | 838 | | /// </summary> |
| | | 839 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 840 | | public bool HasGlobal<T>() where T : unmanaged, IComponent |
| | | 841 | | { |
| | 5 | 842 | | EnsureGlobalReadAllowed(nameof(HasGlobal)); |
| | | 843 | | |
| | 5 | 844 | | int typeId = ComponentTypeId<T>.Id; |
| | 5 | 845 | | EnsureSingletonComponentType<T>(typeId); |
| | 4 | 846 | | var storage = GetStorage<T>(typeId); |
| | 4 | 847 | | return storage != null && storage->SingletonEntity != EntityRef.None; |
| | | 848 | | } |
| | | 849 | | |
| | | 850 | | /// <summary> |
| | | 851 | | /// 获取全局 singleton 组件引用。 |
| | | 852 | | /// </summary> |
| | | 853 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 854 | | public ref T GetGlobal<T>() where T : unmanaged, IComponent |
| | | 855 | | { |
| | 123 | 856 | | EnsureGlobalWriteAllowed(nameof(GetGlobal)); |
| | | 857 | | |
| | 123 | 858 | | int typeId = ComponentTypeId<T>.Id; |
| | 123 | 859 | | EnsureSingletonComponentType<T>(typeId); |
| | | 860 | | |
| | 123 | 861 | | var storage = GetStorage<T>(typeId); |
| | 123 | 862 | | if (storage == null || storage->SingletonEntity == EntityRef.None) |
| | | 863 | | { |
| | 0 | 864 | | throw new InvalidOperationException($"Global component {typeof(T).Name} not found."); |
| | | 865 | | } |
| | | 866 | | |
| | 123 | 867 | | return ref storage->GetSingleton(); |
| | | 868 | | } |
| | | 869 | | |
| | | 870 | | /// <summary> |
| | | 871 | | /// 尝试获取全局 singleton 组件值。 |
| | | 872 | | /// </summary> |
| | | 873 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 874 | | public bool TryGetGlobal<T>(out T component) where T : unmanaged, IComponent |
| | | 875 | | { |
| | 107 | 876 | | EnsureGlobalReadAllowed(nameof(TryGetGlobal)); |
| | | 877 | | |
| | 106 | 878 | | int typeId = ComponentTypeId<T>.Id; |
| | 106 | 879 | | EnsureSingletonComponentType<T>(typeId); |
| | | 880 | | |
| | 105 | 881 | | var storage = GetStorage<T>(typeId); |
| | 105 | 882 | | if (storage != null && storage->SingletonEntity != EntityRef.None) |
| | | 883 | | { |
| | 104 | 884 | | component = storage->GetSingleton(); |
| | 104 | 885 | | return true; |
| | | 886 | | } |
| | | 887 | | |
| | 1 | 888 | | component = default; |
| | 1 | 889 | | return false; |
| | | 890 | | } |
| | | 891 | | |
| | | 892 | | /// <summary> |
| | | 893 | | /// 获取全局 singleton 组件所在实体。 |
| | | 894 | | /// </summary> |
| | | 895 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 896 | | public EntityRef GetGlobalEntity<T>() where T : unmanaged, IComponent |
| | | 897 | | { |
| | 2 | 898 | | EnsureGlobalWriteAllowed(nameof(GetGlobalEntity)); |
| | | 899 | | |
| | 2 | 900 | | int typeId = ComponentTypeId<T>.Id; |
| | 2 | 901 | | EnsureSingletonComponentType<T>(typeId); |
| | | 902 | | |
| | 2 | 903 | | var storage = GetStorage<T>(typeId); |
| | 2 | 904 | | if (storage == null || storage->SingletonEntity == EntityRef.None) |
| | | 905 | | { |
| | 0 | 906 | | throw new InvalidOperationException($"Global component {typeof(T).Name} not found."); |
| | | 907 | | } |
| | | 908 | | |
| | 2 | 909 | | return storage->SingletonEntity; |
| | | 910 | | } |
| | | 911 | | |
| | | 912 | | /// <summary> |
| | | 913 | | /// 尝试获取全局 singleton 组件所在实体。 |
| | | 914 | | /// </summary> |
| | | 915 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 916 | | public bool TryGetGlobalEntity<T>(out EntityRef entity) where T : unmanaged, IComponent |
| | | 917 | | { |
| | 3 | 918 | | EnsureGlobalWriteAllowed(nameof(TryGetGlobalEntity)); |
| | | 919 | | |
| | 3 | 920 | | int typeId = ComponentTypeId<T>.Id; |
| | 3 | 921 | | EnsureSingletonComponentType<T>(typeId); |
| | | 922 | | |
| | 3 | 923 | | var storage = GetStorage<T>(typeId); |
| | 3 | 924 | | if (storage != null && storage->SingletonEntity != EntityRef.None) |
| | | 925 | | { |
| | 2 | 926 | | entity = storage->SingletonEntity; |
| | 2 | 927 | | return true; |
| | | 928 | | } |
| | | 929 | | |
| | 1 | 930 | | entity = EntityRef.None; |
| | 1 | 931 | | return false; |
| | | 932 | | } |
| | | 933 | | |
| | | 934 | | /// <summary> |
| | | 935 | | /// 设置全局 singleton 组件。 |
| | | 936 | | /// 若当前尚未存在对应全局状态,则会自动创建承载它的实体。 |
| | | 937 | | /// </summary> |
| | | 938 | | public EntityRef SetGlobal<T>(in T component) where T : unmanaged, IComponent |
| | | 939 | | { |
| | 130 | 940 | | EnsureGlobalWriteAllowed(nameof(SetGlobal)); |
| | | 941 | | |
| | 129 | 942 | | int typeId = ComponentTypeId<T>.Id; |
| | 129 | 943 | | EnsureSingletonComponentType<T>(typeId); |
| | | 944 | | |
| | 128 | 945 | | var storage = GetStorage<T>(typeId); |
| | 128 | 946 | | if (storage != null && storage->SingletonEntity != EntityRef.None) |
| | | 947 | | { |
| | 102 | 948 | | EntityRef entity = storage->SingletonEntity; |
| | 102 | 949 | | Set(entity, component); |
| | 102 | 950 | | return entity; |
| | | 951 | | } |
| | | 952 | | |
| | 26 | 953 | | EntityRef created = CreateEntity(); |
| | 26 | 954 | | Add(created, component); |
| | 26 | 955 | | return created; |
| | | 956 | | } |
| | | 957 | | |
| | | 958 | | /// <summary> |
| | | 959 | | /// 移除全局 singleton 组件。 |
| | | 960 | | /// 若该组件是实体上唯一的组件,则会一并销毁其承载实体,避免留下空壳全局实体。 |
| | | 961 | | /// </summary> |
| | | 962 | | public bool RemoveGlobal<T>() where T : unmanaged, IComponent |
| | | 963 | | { |
| | 3 | 964 | | EnsureGlobalWriteAllowed(nameof(RemoveGlobal)); |
| | | 965 | | |
| | 3 | 966 | | int typeId = ComponentTypeId<T>.Id; |
| | 3 | 967 | | EnsureSingletonComponentType<T>(typeId); |
| | | 968 | | |
| | 2 | 969 | | var storage = GetStorage<T>(typeId); |
| | 2 | 970 | | if (storage == null || storage->SingletonEntity == EntityRef.None) |
| | | 971 | | { |
| | 0 | 972 | | return false; |
| | | 973 | | } |
| | | 974 | | |
| | 2 | 975 | | EntityRef entity = storage->SingletonEntity; |
| | 2 | 976 | | if (EntityHasOnlyComponent(entity, typeId)) |
| | | 977 | | { |
| | 1 | 978 | | DestroyEntity(entity); |
| | 1 | 979 | | return true; |
| | | 980 | | } |
| | | 981 | | |
| | 1 | 982 | | Remove<T>(entity); |
| | 1 | 983 | | return true; |
| | | 984 | | } |
| | | 985 | | |
| | | 986 | | #endregion |
| | | 987 | | |
| | | 988 | | #region 查询支持 |
| | | 989 | | |
| | | 990 | | /// <summary> |
| | | 991 | | /// 获取组件存储。 |
| | | 992 | | /// 供 Query、兼容层 Filter 以及底层高性能遍历入口使用。 |
| | | 993 | | /// </summary> |
| | | 994 | | internal Storage<T>* GetStorage<T>(int typeId) where T : unmanaged, IComponent |
| | | 995 | | { |
| | 4388 | 996 | | if (!_storageInitialized[typeId]) return null; |
| | 4252 | 997 | | return (Storage<T>*)_componentStorages[typeId]; |
| | | 998 | | } |
| | | 999 | | |
| | | 1000 | | internal Storage<T>* GetOrCreateStorageForSnapshotRestore<T>(int typeId) where T : unmanaged, IComponent |
| | | 1001 | | { |
| | 2356 | 1002 | | return GetOrCreateStorage<T>(typeId); |
| | | 1003 | | } |
| | | 1004 | | |
| | | 1005 | | /// <summary> |
| | | 1006 | | /// 获取实体组件位图指针 |
| | | 1007 | | /// </summary> |
| | | 1008 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1009 | | internal ulong* GetComponentMaskPointer(int entityIndex) |
| | | 1010 | | { |
| | 562 | 1011 | | return _entityComponentMasks + (entityIndex * ComponentMaskBlockCount); |
| | | 1012 | | } |
| | | 1013 | | |
| | | 1014 | | /// <summary> |
| | | 1015 | | /// 检查实体位图上是否仍然持有指定组件。 |
| | | 1016 | | /// </summary> |
| | | 1017 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1018 | | internal bool HasComponentBit(int entityIndex, int typeId) |
| | | 1019 | | { |
| | 2454 | 1020 | | int block = typeId >> 6; |
| | 2454 | 1021 | | int bit = typeId & 0x3F; |
| | 2454 | 1022 | | return (_entityComponentMasks[entityIndex * ComponentMaskBlockCount + block] & (1UL << bit)) != 0; |
| | | 1023 | | } |
| | | 1024 | | |
| | | 1025 | | /// <summary> |
| | | 1026 | | /// 检查实体是否匹配位图组件过滤器。 |
| | | 1027 | | /// 该 API 主要用于动态条件匹配,不替代强类型 Query。 |
| | | 1028 | | /// </summary> |
| | | 1029 | | public bool MatchesFilter(EntityRef entity, in ComponentFilter filter) |
| | | 1030 | | { |
| | 0 | 1031 | | if (!IsValid(entity)) return false; |
| | | 1032 | | |
| | 0 | 1033 | | ulong* mask = GetComponentMaskPointer(entity.Index); |
| | | 1034 | | |
| | | 1035 | | // 检查必须包含的组件 |
| | 0 | 1036 | | if (!filter.Required.IsEmpty) |
| | | 1037 | | { |
| | 0 | 1038 | | for (int i = 0; i < ComponentMaskBlockCount; i++) |
| | | 1039 | | { |
| | 0 | 1040 | | ulong required = filter.Required.GetBlock(i); |
| | 0 | 1041 | | if ((mask[i] & required) != required) |
| | 0 | 1042 | | return false; |
| | | 1043 | | } |
| | | 1044 | | } |
| | | 1045 | | |
| | | 1046 | | // 检查必须排除的组件 |
| | 0 | 1047 | | if (!filter.Excluded.IsEmpty) |
| | | 1048 | | { |
| | 0 | 1049 | | for (int i = 0; i < ComponentMaskBlockCount; i++) |
| | | 1050 | | { |
| | 0 | 1051 | | ulong excluded = filter.Excluded.GetBlock(i); |
| | 0 | 1052 | | if ((mask[i] & excluded) != 0) |
| | 0 | 1053 | | return false; |
| | | 1054 | | } |
| | | 1055 | | } |
| | | 1056 | | |
| | 0 | 1057 | | return true; |
| | | 1058 | | } |
| | | 1059 | | |
| | | 1060 | | /// <summary> |
| | | 1061 | | /// 提交所有延迟删除。 |
| | | 1062 | | /// </summary> |
| | | 1063 | | public void CommitDeferredRemovals() |
| | | 1064 | | { |
| | 633 | 1065 | | if (ShouldDeferStructuralChanges()) |
| | | 1066 | | { |
| | 1 | 1067 | | throw new InvalidOperationException( |
| | 1 | 1068 | | "Cannot commit deferred removals while structural changes are being deferred. " + |
| | 1 | 1069 | | "Wait for the runtime structural commit stage to complete."); |
| | | 1070 | | } |
| | | 1071 | | |
| | 632 | 1072 | | if (_pendingRemovalCount == 0) |
| | | 1073 | | { |
| | 631 | 1074 | | return; |
| | | 1075 | | } |
| | | 1076 | | |
| | 4 | 1077 | | for (int i = 0; i < _pendingRemovalCount; i++) |
| | | 1078 | | { |
| | 1 | 1079 | | int typeId = _pendingRemovalTypeIds[i]; |
| | 1 | 1080 | | if (!_storageInitialized[typeId] || _componentStorages[typeId] == null) |
| | | 1081 | | { |
| | | 1082 | | continue; |
| | | 1083 | | } |
| | | 1084 | | |
| | 1 | 1085 | | ComponentRegistry.GetDeferredCommitter(typeId)(_componentStorages[typeId], _pendingRemovalEntities[i]); |
| | | 1086 | | } |
| | | 1087 | | |
| | 1 | 1088 | | _pendingRemovalCount = 0; |
| | 1 | 1089 | | } |
| | | 1090 | | |
| | | 1091 | | /// <summary> |
| | | 1092 | | /// 进入“结构性修改延迟提交”模式。 |
| | | 1093 | | /// 在该模式下,实体创建/销毁与组件增删会先进入命令缓冲,直到显式提交时才生效。 |
| | | 1094 | | /// </summary> |
| | | 1095 | | internal void BeginDeferredStructuralChanges() |
| | | 1096 | | { |
| | 626 | 1097 | | if (_deferStructuralChanges) |
| | | 1098 | | { |
| | 0 | 1099 | | throw new InvalidOperationException("Deferred structural changes have already been started for this fram |
| | | 1100 | | } |
| | | 1101 | | |
| | 626 | 1102 | | EnsureStructuralCommandBuffer(); |
| | 626 | 1103 | | _structuralCommandBuffer.Clear(); |
| | 626 | 1104 | | _deferStructuralChanges = true; |
| | 626 | 1105 | | _isReplayingStructuralChanges = false; |
| | 626 | 1106 | | } |
| | | 1107 | | |
| | | 1108 | | /// <summary> |
| | | 1109 | | /// 提交当前 Tick 累积的结构性修改。 |
| | | 1110 | | /// </summary> |
| | | 1111 | | internal void CommitStructuralChanges() |
| | | 1112 | | { |
| | 626 | 1113 | | if (!_deferStructuralChanges && !_isReplayingStructuralChanges) |
| | | 1114 | | { |
| | 0 | 1115 | | CommitDeferredRemovals(); |
| | 0 | 1116 | | return; |
| | | 1117 | | } |
| | | 1118 | | |
| | 626 | 1119 | | _deferStructuralChanges = false; |
| | | 1120 | | |
| | 626 | 1121 | | if (!_structuralCommandBufferInitialized || _structuralCommandBuffer.IsEmpty) |
| | | 1122 | | { |
| | 510 | 1123 | | CommitDeferredRemovals(); |
| | 510 | 1124 | | return; |
| | | 1125 | | } |
| | | 1126 | | |
| | 116 | 1127 | | _isReplayingStructuralChanges = true; |
| | | 1128 | | try |
| | | 1129 | | { |
| | 116 | 1130 | | _structuralCommandBuffer.Playback(this); |
| | 116 | 1131 | | } |
| | | 1132 | | finally |
| | | 1133 | | { |
| | 116 | 1134 | | _isReplayingStructuralChanges = false; |
| | 116 | 1135 | | } |
| | 116 | 1136 | | } |
| | | 1137 | | |
| | | 1138 | | /// <summary> |
| | | 1139 | | /// 中止当前 Tick 尚未提交的结构性修改。 |
| | | 1140 | | /// 仅供运行时在 Tick 失败或异常路径下回收临时状态。 |
| | | 1141 | | /// </summary> |
| | | 1142 | | internal void AbortStructuralChanges() |
| | | 1143 | | { |
| | 0 | 1144 | | if (_structuralCommandBufferInitialized) |
| | | 1145 | | { |
| | 0 | 1146 | | _structuralCommandBuffer.Clear(); |
| | | 1147 | | } |
| | | 1148 | | |
| | 0 | 1149 | | _deferStructuralChanges = false; |
| | 0 | 1150 | | _isReplayingStructuralChanges = false; |
| | 0 | 1151 | | } |
| | | 1152 | | |
| | | 1153 | | /// <summary> |
| | | 1154 | | /// 进入系统作者契约守卫作用域。 |
| | | 1155 | | /// 仅供 `SystemScheduler` 在系统 `OnUpdate(...)` 执行前后使用。 |
| | | 1156 | | /// </summary> |
| | | 1157 | | internal void BeginSystemAuthoringScope(ISystem system) |
| | | 1158 | | { |
| | 1004 | 1159 | | ArgumentNullException.ThrowIfNull(system); |
| | | 1160 | | |
| | 1004 | 1161 | | if (_hasActiveSystemAuthoringContract) |
| | | 1162 | | { |
| | 0 | 1163 | | throw new InvalidOperationException("A system authoring scope is already active for this frame."); |
| | | 1164 | | } |
| | | 1165 | | |
| | 1004 | 1166 | | _activeSystemAuthoringContract = system.Contract; |
| | 1004 | 1167 | | _activeSystemName = system.GetType().Name; |
| | 1004 | 1168 | | _hasActiveSystemAuthoringContract = true; |
| | 1004 | 1169 | | } |
| | | 1170 | | |
| | | 1171 | | /// <summary> |
| | | 1172 | | /// 退出系统作者契约守卫作用域。 |
| | | 1173 | | /// </summary> |
| | | 1174 | | internal void EndSystemAuthoringScope() |
| | | 1175 | | { |
| | 1004 | 1176 | | ResetSystemAuthoringGuard(); |
| | 1004 | 1177 | | } |
| | | 1178 | | |
| | | 1179 | | #endregion |
| | | 1180 | | |
| | | 1181 | | #region Unsafe API (高性能直接访问) |
| | | 1182 | | |
| | | 1183 | | /// <summary> |
| | | 1184 | | /// 获取组件块迭代器 - 批量遍历,最大化缓存命中率 |
| | | 1185 | | /// </summary> |
| | | 1186 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1187 | | public ComponentBlockIterator<T> GetComponentBlockIterator<T>() where T : unmanaged, IComponent |
| | | 1188 | | { |
| | 2 | 1189 | | int typeId = ComponentTypeId<T>.Id; |
| | 2 | 1190 | | var storage = GetStorage<T>(typeId); |
| | 2 | 1191 | | if (storage == null) |
| | 0 | 1192 | | return default; |
| | 2 | 1193 | | return new ComponentBlockIterator<T>(storage); |
| | | 1194 | | } |
| | | 1195 | | |
| | | 1196 | | /// <summary> |
| | | 1197 | | /// 获取组件块迭代器(范围版) |
| | | 1198 | | /// </summary> |
| | | 1199 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1200 | | public ComponentBlockIterator<T> GetComponentBlockIterator<T>(int offset, int count) where T : unmanaged, ICompo |
| | | 1201 | | { |
| | 0 | 1202 | | int typeId = ComponentTypeId<T>.Id; |
| | 0 | 1203 | | var storage = GetStorage<T>(typeId); |
| | 0 | 1204 | | if (storage == null) |
| | 0 | 1205 | | return default; |
| | 0 | 1206 | | return new ComponentBlockIterator<T>(storage, offset, count); |
| | | 1207 | | } |
| | | 1208 | | |
| | | 1209 | | /// <summary> |
| | | 1210 | | /// 获取单组件查询。 |
| | | 1211 | | /// </summary> |
| | | 1212 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1213 | | public Query<T> Query<T>() where T : unmanaged, IComponent |
| | | 1214 | | { |
| | 1148 | 1215 | | return new Query<T>(this); |
| | | 1216 | | } |
| | | 1217 | | |
| | | 1218 | | /// <summary> |
| | | 1219 | | /// 获取双组件查询。 |
| | | 1220 | | /// </summary> |
| | | 1221 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1222 | | public Query<T1, T2> Query<T1, T2>() |
| | | 1223 | | where T1 : unmanaged, IComponent |
| | | 1224 | | where T2 : unmanaged, IComponent |
| | | 1225 | | { |
| | 902 | 1226 | | return new Query<T1, T2>(this); |
| | | 1227 | | } |
| | | 1228 | | |
| | | 1229 | | /// <summary> |
| | | 1230 | | /// 获取三组件查询。 |
| | | 1231 | | /// </summary> |
| | | 1232 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1233 | | public Query<T1, T2, T3> Query<T1, T2, T3>() |
| | | 1234 | | where T1 : unmanaged, IComponent |
| | | 1235 | | where T2 : unmanaged, IComponent |
| | | 1236 | | where T3 : unmanaged, IComponent |
| | | 1237 | | { |
| | 2 | 1238 | | return new Query<T1, T2, T3>(this); |
| | | 1239 | | } |
| | | 1240 | | |
| | | 1241 | | /// <summary> |
| | | 1242 | | /// 获取四组件查询。 |
| | | 1243 | | /// 这是当前正式公开的强类型 Query 上限; |
| | | 1244 | | /// 更高维组合应优先考虑拆分系统职责,或结合 `MatchesFilter(...)` 做显式条件匹配。 |
| | | 1245 | | /// </summary> |
| | | 1246 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1247 | | public Query<T1, T2, T3, T4> Query<T1, T2, T3, T4>() |
| | | 1248 | | where T1 : unmanaged, IComponent |
| | | 1249 | | where T2 : unmanaged, IComponent |
| | | 1250 | | where T3 : unmanaged, IComponent |
| | | 1251 | | where T4 : unmanaged, IComponent |
| | | 1252 | | { |
| | 3 | 1253 | | return new Query<T1, T2, T3, T4>(this); |
| | | 1254 | | } |
| | | 1255 | | |
| | | 1256 | | /// <summary> |
| | | 1257 | | /// 注册并维护双组件 Owning Group。 |
| | | 1258 | | /// 仅建议用于极少数稳定热点系统。 |
| | | 1259 | | /// </summary> |
| | | 1260 | | public OwningGroup<T1, T2> RegisterOwningGroup<T1, T2>() |
| | | 1261 | | where T1 : unmanaged, IComponent |
| | | 1262 | | where T2 : unmanaged, IComponent |
| | | 1263 | | { |
| | 3 | 1264 | | int typeId1 = ComponentTypeId<T1>.Id; |
| | 3 | 1265 | | int typeId2 = ComponentTypeId<T2>.Id; |
| | 3 | 1266 | | ulong key = ComposeOwningGroupKey(2, typeId1, typeId2, 0); |
| | | 1267 | | |
| | 3 | 1268 | | if (_owningGroups != null && _owningGroups.TryGetValue(key, out object? existing)) |
| | | 1269 | | { |
| | 0 | 1270 | | return (OwningGroup<T1, T2>)existing; |
| | | 1271 | | } |
| | | 1272 | | |
| | 3 | 1273 | | var group = new OwningGroup<T1, T2>(_allocator, _entityCapacity, typeId1, typeId2); |
| | 3 | 1274 | | group.Rebuild(this); |
| | 3 | 1275 | | _owningGroups ??= new Dictionary<ulong, object>(); |
| | 3 | 1276 | | _owningGroupBindings ??= new List<IFrameOwningGroup>(); |
| | 3 | 1277 | | _owningGroups[key] = group; |
| | 3 | 1278 | | _owningGroupBindings.Add(group); |
| | 3 | 1279 | | return group; |
| | | 1280 | | } |
| | | 1281 | | |
| | | 1282 | | /// <summary> |
| | | 1283 | | /// 注册并维护三组件 Owning Group。 |
| | | 1284 | | /// </summary> |
| | | 1285 | | public OwningGroup<T1, T2, T3> RegisterOwningGroup<T1, T2, T3>() |
| | | 1286 | | where T1 : unmanaged, IComponent |
| | | 1287 | | where T2 : unmanaged, IComponent |
| | | 1288 | | where T3 : unmanaged, IComponent |
| | | 1289 | | { |
| | 1 | 1290 | | int typeId1 = ComponentTypeId<T1>.Id; |
| | 1 | 1291 | | int typeId2 = ComponentTypeId<T2>.Id; |
| | 1 | 1292 | | int typeId3 = ComponentTypeId<T3>.Id; |
| | 1 | 1293 | | ulong key = ComposeOwningGroupKey(3, typeId1, typeId2, typeId3); |
| | | 1294 | | |
| | 1 | 1295 | | if (_owningGroups != null && _owningGroups.TryGetValue(key, out object? existing)) |
| | | 1296 | | { |
| | 0 | 1297 | | return (OwningGroup<T1, T2, T3>)existing; |
| | | 1298 | | } |
| | | 1299 | | |
| | 1 | 1300 | | var group = new OwningGroup<T1, T2, T3>(_allocator, _entityCapacity, typeId1, typeId2, typeId3); |
| | 1 | 1301 | | group.Rebuild(this); |
| | 1 | 1302 | | _owningGroups ??= new Dictionary<ulong, object>(); |
| | 1 | 1303 | | _owningGroupBindings ??= new List<IFrameOwningGroup>(); |
| | 1 | 1304 | | _owningGroups[key] = group; |
| | 1 | 1305 | | _owningGroupBindings.Add(group); |
| | 1 | 1306 | | return group; |
| | | 1307 | | } |
| | | 1308 | | |
| | | 1309 | | /// <summary> |
| | | 1310 | | /// 获取已注册的双组件 Owning Group。 |
| | | 1311 | | /// </summary> |
| | | 1312 | | public OwningGroup<T1, T2> GetOwningGroup<T1, T2>() |
| | | 1313 | | where T1 : unmanaged, IComponent |
| | | 1314 | | where T2 : unmanaged, IComponent |
| | | 1315 | | { |
| | 2 | 1316 | | if (TryGetOwningGroup<T1, T2>(out OwningGroup<T1, T2> group)) |
| | | 1317 | | { |
| | 2 | 1318 | | return group; |
| | | 1319 | | } |
| | | 1320 | | |
| | 0 | 1321 | | throw new InvalidOperationException( |
| | 0 | 1322 | | $"OwningGroup<{typeof(T1).Name}, {typeof(T2).Name}> 尚未注册,请先调用 RegisterOwningGroup。"); |
| | | 1323 | | } |
| | | 1324 | | |
| | | 1325 | | /// <summary> |
| | | 1326 | | /// 获取已注册的三组件 Owning Group。 |
| | | 1327 | | /// </summary> |
| | | 1328 | | public OwningGroup<T1, T2, T3> GetOwningGroup<T1, T2, T3>() |
| | | 1329 | | where T1 : unmanaged, IComponent |
| | | 1330 | | where T2 : unmanaged, IComponent |
| | | 1331 | | where T3 : unmanaged, IComponent |
| | | 1332 | | { |
| | 0 | 1333 | | if (TryGetOwningGroup<T1, T2, T3>(out OwningGroup<T1, T2, T3> group)) |
| | | 1334 | | { |
| | 0 | 1335 | | return group; |
| | | 1336 | | } |
| | | 1337 | | |
| | 0 | 1338 | | throw new InvalidOperationException( |
| | 0 | 1339 | | $"OwningGroup<{typeof(T1).Name}, {typeof(T2).Name}, {typeof(T3).Name}> 尚未注册,请先调用 RegisterOwningGroup。"); |
| | | 1340 | | } |
| | | 1341 | | |
| | | 1342 | | /// <summary> |
| | | 1343 | | /// 尝试获取已注册的双组件 Owning Group。 |
| | | 1344 | | /// </summary> |
| | | 1345 | | public bool TryGetOwningGroup<T1, T2>(out OwningGroup<T1, T2> group) |
| | | 1346 | | where T1 : unmanaged, IComponent |
| | | 1347 | | where T2 : unmanaged, IComponent |
| | | 1348 | | { |
| | 2 | 1349 | | int typeId1 = ComponentTypeId<T1>.Id; |
| | 2 | 1350 | | int typeId2 = ComponentTypeId<T2>.Id; |
| | 2 | 1351 | | ulong key = ComposeOwningGroupKey(2, typeId1, typeId2, 0); |
| | | 1352 | | |
| | 2 | 1353 | | if (_owningGroups != null && _owningGroups.TryGetValue(key, out object? existing)) |
| | | 1354 | | { |
| | 2 | 1355 | | group = (OwningGroup<T1, T2>)existing; |
| | 2 | 1356 | | return true; |
| | | 1357 | | } |
| | | 1358 | | |
| | 0 | 1359 | | group = null!; |
| | 0 | 1360 | | return false; |
| | | 1361 | | } |
| | | 1362 | | |
| | | 1363 | | /// <summary> |
| | | 1364 | | /// 尝试获取已注册的三组件 Owning Group。 |
| | | 1365 | | /// </summary> |
| | | 1366 | | public bool TryGetOwningGroup<T1, T2, T3>(out OwningGroup<T1, T2, T3> group) |
| | | 1367 | | where T1 : unmanaged, IComponent |
| | | 1368 | | where T2 : unmanaged, IComponent |
| | | 1369 | | where T3 : unmanaged, IComponent |
| | | 1370 | | { |
| | 1 | 1371 | | int typeId1 = ComponentTypeId<T1>.Id; |
| | 1 | 1372 | | int typeId2 = ComponentTypeId<T2>.Id; |
| | 1 | 1373 | | int typeId3 = ComponentTypeId<T3>.Id; |
| | 1 | 1374 | | ulong key = ComposeOwningGroupKey(3, typeId1, typeId2, typeId3); |
| | | 1375 | | |
| | 1 | 1376 | | if (_owningGroups != null && _owningGroups.TryGetValue(key, out object? existing)) |
| | | 1377 | | { |
| | 1 | 1378 | | group = (OwningGroup<T1, T2, T3>)existing; |
| | 1 | 1379 | | return true; |
| | | 1380 | | } |
| | | 1381 | | |
| | 0 | 1382 | | group = null!; |
| | 0 | 1383 | | return false; |
| | | 1384 | | } |
| | | 1385 | | |
| | | 1386 | | /// <summary> |
| | | 1387 | | /// 获取组件存储的原始指针(高级用法) |
| | | 1388 | | /// </summary> |
| | | 1389 | | internal Storage<T>* GetStoragePointer<T>() where T : unmanaged, IComponent |
| | | 1390 | | { |
| | 2970 | 1391 | | int typeId = ComponentTypeId<T>.Id; |
| | 2970 | 1392 | | return GetStorage<T>(typeId); |
| | | 1393 | | } |
| | | 1394 | | |
| | | 1395 | | /// <summary> |
| | | 1396 | | /// 获取帧内部使用的分配器。 |
| | | 1397 | | /// </summary> |
| | | 1398 | | internal Allocator* GetAllocatorPointer() |
| | | 1399 | | { |
| | 23 | 1400 | | return _allocator; |
| | | 1401 | | } |
| | | 1402 | | |
| | | 1403 | | #endregion |
| | | 1404 | | |
| | | 1405 | | #region IDisposable |
| | | 1406 | | |
| | | 1407 | | public void Dispose() |
| | | 1408 | | { |
| | 468 | 1409 | | ReleaseState(); |
| | 468 | 1410 | | } |
| | | 1411 | | |
| | | 1412 | | #endregion |
| | | 1413 | | |
| | | 1414 | | #region 私有辅助 |
| | | 1415 | | |
| | | 1416 | | private Storage<T>* GetOrCreateStorage<T>(int typeId) where T : unmanaged, IComponent |
| | | 1417 | | { |
| | 3745 | 1418 | | if (!_storageInitialized[typeId]) |
| | | 1419 | | { |
| | 1360 | 1420 | | var storage = (Storage<T>*)_allocator->Alloc(sizeof(Storage<T>)); |
| | 1360 | 1421 | | storage->Initialize(_entityCapacity, _allocator, ComponentRegistry.GetStorageFlags(typeId), componentTyp |
| | 1360 | 1422 | | _componentStorages[typeId] = storage; |
| | 1360 | 1423 | | _storageInitialized[typeId] = true; |
| | 1360 | 1424 | | return storage; |
| | | 1425 | | } |
| | 2385 | 1426 | | return (Storage<T>*)_componentStorages[typeId]; |
| | | 1427 | | } |
| | | 1428 | | |
| | | 1429 | | private void RemoveComponentInternal(EntityRef entity, int typeId) |
| | | 1430 | | { |
| | 797 | 1431 | | if (!_storageInitialized[typeId] || _componentStorages[typeId] == null) |
| | | 1432 | | { |
| | 0 | 1433 | | ClearComponentMaskBit(entity.Index, typeId); |
| | 0 | 1434 | | return; |
| | | 1435 | | } |
| | | 1436 | | |
| | 797 | 1437 | | ComponentRegistry.GetRemoveInvoker(typeId)(this, entity); |
| | 797 | 1438 | | } |
| | | 1439 | | |
| | | 1440 | | private int EnqueuePendingRemoval(EntityRef entity, int typeId) |
| | | 1441 | | { |
| | 8 | 1442 | | EnsurePendingRemovalCapacity(_pendingRemovalCount + 1); |
| | 8 | 1443 | | _pendingRemovalEntities[_pendingRemovalCount] = entity; |
| | 8 | 1444 | | _pendingRemovalTypeIds[_pendingRemovalCount] = (ushort)typeId; |
| | 8 | 1445 | | return _pendingRemovalCount++; |
| | | 1446 | | } |
| | | 1447 | | |
| | | 1448 | | private void RemovePendingRemovalAt(int queueIndex) |
| | | 1449 | | { |
| | 1 | 1450 | | if ((uint)queueIndex >= (uint)_pendingRemovalCount) |
| | | 1451 | | { |
| | 0 | 1452 | | return; |
| | | 1453 | | } |
| | | 1454 | | |
| | 1 | 1455 | | int lastIndex = _pendingRemovalCount - 1; |
| | 1 | 1456 | | if (queueIndex != lastIndex) |
| | | 1457 | | { |
| | 0 | 1458 | | EntityRef swappedEntity = _pendingRemovalEntities[lastIndex]; |
| | 0 | 1459 | | int swappedTypeId = _pendingRemovalTypeIds[lastIndex]; |
| | | 1460 | | |
| | 0 | 1461 | | _pendingRemovalEntities[queueIndex] = swappedEntity; |
| | 0 | 1462 | | _pendingRemovalTypeIds[queueIndex] = (ushort)swappedTypeId; |
| | | 1463 | | |
| | 0 | 1464 | | if (_storageInitialized[swappedTypeId] && _componentStorages[swappedTypeId] != null) |
| | | 1465 | | { |
| | 0 | 1466 | | ComponentRegistry.GetPendingQueueIndexSetter(swappedTypeId)( |
| | 0 | 1467 | | _componentStorages[swappedTypeId], |
| | 0 | 1468 | | swappedEntity, |
| | 0 | 1469 | | queueIndex); |
| | | 1470 | | } |
| | | 1471 | | } |
| | | 1472 | | |
| | 1 | 1473 | | _pendingRemovalEntities[lastIndex] = EntityRef.None; |
| | 1 | 1474 | | _pendingRemovalTypeIds[lastIndex] = 0; |
| | 1 | 1475 | | _pendingRemovalCount--; |
| | 1 | 1476 | | } |
| | | 1477 | | |
| | | 1478 | | private void EnsurePendingRemovalCapacity(int required) |
| | | 1479 | | { |
| | 778 | 1480 | | if (required <= _pendingRemovalCapacity) |
| | | 1481 | | { |
| | 778 | 1482 | | return; |
| | | 1483 | | } |
| | | 1484 | | |
| | 0 | 1485 | | int newCapacity = _pendingRemovalCapacity * 2; |
| | 0 | 1486 | | while (newCapacity < required) |
| | | 1487 | | { |
| | 0 | 1488 | | newCapacity *= 2; |
| | | 1489 | | } |
| | | 1490 | | |
| | 0 | 1491 | | var newEntities = (EntityRef*)_allocator->Alloc(sizeof(EntityRef) * newCapacity); |
| | 0 | 1492 | | var newTypeIds = (ushort*)_allocator->Alloc(sizeof(ushort) * newCapacity); |
| | | 1493 | | |
| | 0 | 1494 | | Buffer.MemoryCopy(_pendingRemovalEntities, newEntities, sizeof(EntityRef) * newCapacity, sizeof(EntityRef) * |
| | 0 | 1495 | | Buffer.MemoryCopy(_pendingRemovalTypeIds, newTypeIds, sizeof(ushort) * newCapacity, sizeof(ushort) * _pendin |
| | | 1496 | | |
| | 0 | 1497 | | _pendingRemovalEntities = newEntities; |
| | 0 | 1498 | | _pendingRemovalTypeIds = newTypeIds; |
| | 0 | 1499 | | _pendingRemovalCapacity = newCapacity; |
| | 0 | 1500 | | } |
| | | 1501 | | |
| | | 1502 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1503 | | private void SetComponentMask(int entityIndex, int typeId) |
| | | 1504 | | { |
| | 1285 | 1505 | | int block = typeId >> 6; |
| | 1285 | 1506 | | int bit = typeId & 0x3F; |
| | 1285 | 1507 | | _entityComponentMasks[entityIndex * ComponentMaskBlockCount + block] |= (1UL << bit); |
| | 1285 | 1508 | | } |
| | | 1509 | | |
| | | 1510 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1511 | | private void ClearComponentMaskBit(int entityIndex, int typeId) |
| | | 1512 | | { |
| | 808 | 1513 | | int block = typeId >> 6; |
| | 808 | 1514 | | int bit = typeId & 0x3F; |
| | 808 | 1515 | | _entityComponentMasks[entityIndex * ComponentMaskBlockCount + block] &= ~(1UL << bit); |
| | 808 | 1516 | | } |
| | | 1517 | | |
| | | 1518 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1519 | | private void ClearComponentMask(int entityIndex) |
| | | 1520 | | { |
| | 387 | 1521 | | ulong* mask = GetComponentMaskPointer(entityIndex); |
| | 6966 | 1522 | | for (int i = 0; i < ComponentMaskBlockCount; i++) |
| | 3096 | 1523 | | mask[i] = 0; |
| | 387 | 1524 | | } |
| | | 1525 | | |
| | | 1526 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1527 | | private bool HasComponentMask(int entityIndex, int typeId) |
| | | 1528 | | { |
| | 2374 | 1529 | | int block = typeId >> 6; |
| | 2374 | 1530 | | int bit = typeId & 0x3F; |
| | 2374 | 1531 | | return (_entityComponentMasks[entityIndex * ComponentMaskBlockCount + block] & (1UL << bit)) != 0; |
| | | 1532 | | } |
| | | 1533 | | |
| | | 1534 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1535 | | private void InvokeOnAdded<T>(EntityRef entity, Storage<T>* storage) where T : unmanaged, IComponent |
| | | 1536 | | { |
| | 1285 | 1537 | | ComponentChangedDelegate? onAdded = ComponentTypeId<T>.Callbacks.OnAdded; |
| | 1285 | 1538 | | if (onAdded == null) |
| | | 1539 | | { |
| | 1284 | 1540 | | return; |
| | | 1541 | | } |
| | | 1542 | | |
| | 1 | 1543 | | T* addedComponent = storage->GetPointer(entity); |
| | 1 | 1544 | | if (addedComponent != null) |
| | | 1545 | | { |
| | 1 | 1546 | | onAdded(entity, addedComponent, this); |
| | | 1547 | | } |
| | 1 | 1548 | | } |
| | | 1549 | | |
| | | 1550 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1551 | | private void NotifyOwningGroups(EntityRef entity, int typeId) |
| | | 1552 | | { |
| | 2197 | 1553 | | if (_owningGroupBindings == null || _owningGroupBindings.Count == 0) |
| | | 1554 | | { |
| | 2185 | 1555 | | return; |
| | | 1556 | | } |
| | | 1557 | | |
| | 48 | 1558 | | for (int i = 0; i < _owningGroupBindings.Count; i++) |
| | | 1559 | | { |
| | 12 | 1560 | | IFrameOwningGroup binding = _owningGroupBindings[i]; |
| | 12 | 1561 | | if (binding.DependsOn(typeId)) |
| | | 1562 | | { |
| | 12 | 1563 | | binding.SyncEntity(this, entity, typeId); |
| | | 1564 | | } |
| | | 1565 | | } |
| | 12 | 1566 | | } |
| | | 1567 | | |
| | | 1568 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1569 | | private static void EnsureSingletonComponentType<T>(int typeId) where T : unmanaged, IComponent |
| | | 1570 | | { |
| | 371 | 1571 | | if ((ComponentRegistry.GetStorageFlags(typeId) & StorageFlags.Singleton) == 0) |
| | | 1572 | | { |
| | 4 | 1573 | | throw new InvalidOperationException( |
| | 4 | 1574 | | $"Component {typeof(T).Name} is not registered as a singleton/global component."); |
| | | 1575 | | } |
| | 367 | 1576 | | } |
| | | 1577 | | |
| | | 1578 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1579 | | private bool EntityHasOnlyComponent(EntityRef entity, int typeId) |
| | | 1580 | | { |
| | 2 | 1581 | | if (!IsValid(entity)) |
| | | 1582 | | { |
| | 0 | 1583 | | return false; |
| | | 1584 | | } |
| | | 1585 | | |
| | 2 | 1586 | | ulong* mask = GetComponentMaskPointer(entity.Index); |
| | 2 | 1587 | | int targetBlock = typeId >> 6; |
| | 2 | 1588 | | ulong targetBit = 1UL << (typeId & 0x3F); |
| | | 1589 | | |
| | 20 | 1590 | | for (int block = 0; block < ComponentMaskBlockCount; block++) |
| | | 1591 | | { |
| | 9 | 1592 | | ulong value = mask[block]; |
| | 9 | 1593 | | if (block == targetBlock) |
| | | 1594 | | { |
| | 2 | 1595 | | if (value != targetBit) |
| | | 1596 | | { |
| | 1 | 1597 | | return false; |
| | | 1598 | | } |
| | | 1599 | | |
| | | 1600 | | continue; |
| | | 1601 | | } |
| | | 1602 | | |
| | 7 | 1603 | | if (value != 0) |
| | | 1604 | | { |
| | 0 | 1605 | | return false; |
| | | 1606 | | } |
| | | 1607 | | } |
| | | 1608 | | |
| | 1 | 1609 | | return true; |
| | | 1610 | | } |
| | | 1611 | | |
| | | 1612 | | private void RebuildOwningGroups() |
| | | 1613 | | { |
| | 769 | 1614 | | if (_owningGroupBindings == null || _owningGroupBindings.Count == 0) |
| | | 1615 | | { |
| | 768 | 1616 | | return; |
| | | 1617 | | } |
| | | 1618 | | |
| | 4 | 1619 | | for (int i = 0; i < _owningGroupBindings.Count; i++) |
| | | 1620 | | { |
| | 1 | 1621 | | _owningGroupBindings[i].Rebuild(this); |
| | | 1622 | | } |
| | 1 | 1623 | | } |
| | | 1624 | | |
| | | 1625 | | private ComponentSchemaManifest WritePackedState(FrameStateWriter writer, ComponentSerializationMode mode) |
| | | 1626 | | { |
| | 245 | 1627 | | writer.WriteInt32(Tick); |
| | 245 | 1628 | | writer.WriteInt64(DeltaTime.RawValue); |
| | 245 | 1629 | | writer.WriteInt32(_entityCapacity); |
| | 245 | 1630 | | writer.WriteInt32(_entityCount); |
| | 245 | 1631 | | writer.WriteInt32(_freeListHead); |
| | | 1632 | | |
| | 245 | 1633 | | int metadataCount = _entityCount; |
| | 245 | 1634 | | writer.WriteInt32(metadataCount); |
| | 3144 | 1635 | | for (int i = 0; i < metadataCount; i++) |
| | | 1636 | | { |
| | 1327 | 1637 | | writer.WriteUInt16(_entityVersions[i]); |
| | | 1638 | | } |
| | | 1639 | | |
| | 245 | 1640 | | writer.WriteInt32(metadataCount); |
| | 3144 | 1641 | | for (int i = 0; i < metadataCount; i++) |
| | | 1642 | | { |
| | 1327 | 1643 | | writer.WriteInt32(_entityNextFree[i]); |
| | | 1644 | | } |
| | | 1645 | | |
| | 245 | 1646 | | int maskCount = metadataCount * ComponentMaskBlockCount; |
| | 245 | 1647 | | writer.WriteInt32(maskCount); |
| | 245 | 1648 | | if (maskCount > 0) |
| | | 1649 | | { |
| | 245 | 1650 | | writer.WriteBytes(_entityComponentMasks, maskCount * sizeof(ulong)); |
| | | 1651 | | } |
| | | 1652 | | |
| | 245 | 1653 | | writer.WriteInt32(_pendingRemovalCount); |
| | 490 | 1654 | | for (int i = 0; i < _pendingRemovalCount; i++) |
| | | 1655 | | { |
| | 0 | 1656 | | writer.WriteUInt64(_pendingRemovalEntities[i].Raw); |
| | | 1657 | | } |
| | | 1658 | | |
| | 245 | 1659 | | writer.WriteInt32(_pendingRemovalCount); |
| | 490 | 1660 | | for (int i = 0; i < _pendingRemovalCount; i++) |
| | | 1661 | | { |
| | 0 | 1662 | | writer.WriteUInt16(_pendingRemovalTypeIds[i]); |
| | | 1663 | | } |
| | | 1664 | | |
| | 245 | 1665 | | Span<int> serializableTypeIds = stackalloc int[MaxComponentTypes]; |
| | 245 | 1666 | | int storageCount = 0; |
| | 23690 | 1667 | | for (int typeId = 1; typeId <= ComponentRegistry.Count; typeId++) |
| | | 1668 | | { |
| | 11600 | 1669 | | if (!_storageInitialized[typeId] || _componentStorages[typeId] == null) |
| | | 1670 | | { |
| | | 1671 | | continue; |
| | | 1672 | | } |
| | | 1673 | | |
| | 1085 | 1674 | | if (!ComponentRegistry.CanSerializeComponent(typeId, mode)) |
| | | 1675 | | { |
| | | 1676 | | continue; |
| | | 1677 | | } |
| | | 1678 | | |
| | 1085 | 1679 | | if (!ComponentRegistry.GetPackedStatePresenceChecker(typeId)(_componentStorages[typeId], mode)) |
| | | 1680 | | { |
| | | 1681 | | continue; |
| | | 1682 | | } |
| | | 1683 | | |
| | 1085 | 1684 | | serializableTypeIds[storageCount++] = typeId; |
| | | 1685 | | } |
| | | 1686 | | |
| | 245 | 1687 | | ComponentSchemaManifest schemaManifest = ComponentRegistry.CreateSchemaManifest(serializableTypeIds[..storag |
| | | 1688 | | |
| | 245 | 1689 | | writer.WriteInt32(storageCount); |
| | 2660 | 1690 | | for (int i = 0; i < storageCount; i++) |
| | | 1691 | | { |
| | 1085 | 1692 | | int typeId = serializableTypeIds[i]; |
| | 1085 | 1693 | | ComponentRegistry.GetPackedStateWriter(typeId)(_componentStorages[typeId], writer, mode); |
| | | 1694 | | } |
| | | 1695 | | |
| | 245 | 1696 | | return schemaManifest; |
| | | 1697 | | } |
| | | 1698 | | |
| | | 1699 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1700 | | private static ulong ComposeOwningGroupKey(int arity, int typeId1, int typeId2, int typeId3) |
| | | 1701 | | { |
| | 7 | 1702 | | return ((ulong)(uint)arity << 48) |
| | 7 | 1703 | | | ((ulong)(uint)typeId1 << 32) |
| | 7 | 1704 | | | ((ulong)(uint)typeId2 << 16) |
| | 7 | 1705 | | | (uint)typeId3; |
| | | 1706 | | } |
| | | 1707 | | |
| | | 1708 | | private void InitializeState(int maxEntities, bool preserveOwningGroups = false) |
| | | 1709 | | { |
| | 472 | 1710 | | _entityCapacity = maxEntities; |
| | 472 | 1711 | | _entityCount = 0; |
| | 472 | 1712 | | _freeListHead = -1; |
| | 472 | 1713 | | Tick = 0; |
| | 472 | 1714 | | DeltaTime = FP.Zero; |
| | | 1715 | | |
| | 472 | 1716 | | _allocator = Allocator.Create(); |
| | | 1717 | | |
| | 472 | 1718 | | _entities = (EntityRef*)_allocator->Alloc(sizeof(EntityRef) * maxEntities); |
| | 472 | 1719 | | _entityVersions = (ushort*)_allocator->Alloc(sizeof(ushort) * maxEntities); |
| | 472 | 1720 | | _entityNextFree = (int*)_allocator->Alloc(sizeof(int) * maxEntities); |
| | 472 | 1721 | | _entityComponentMasks = (ulong*)_allocator->Alloc(sizeof(ulong) * maxEntities * ComponentMaskBlockCount); |
| | | 1722 | | |
| | 50989912 | 1723 | | for (int i = 0; i < maxEntities; i++) |
| | | 1724 | | { |
| | 25494484 | 1725 | | _entityVersions[i] = 1; |
| | 25494484 | 1726 | | _entityNextFree[i] = -1; |
| | | 1727 | | } |
| | | 1728 | | |
| | 472 | 1729 | | _componentStorages = (void**)_allocator->Alloc(sizeof(void*) * MaxComponentTypes); |
| | 472 | 1730 | | _storageInitialized = (bool*)_allocator->Alloc(sizeof(bool) * MaxComponentTypes); |
| | | 1731 | | |
| | 484272 | 1732 | | for (int i = 0; i < MaxComponentTypes; i++) |
| | | 1733 | | { |
| | 241664 | 1734 | | _componentStorages[i] = null; |
| | 241664 | 1735 | | _storageInitialized[i] = false; |
| | | 1736 | | } |
| | | 1737 | | |
| | 472 | 1738 | | _pendingRemovalCapacity = 64; |
| | 472 | 1739 | | _pendingRemovalCount = 0; |
| | 472 | 1740 | | _pendingRemovalEntities = (EntityRef*)_allocator->Alloc(sizeof(EntityRef) * _pendingRemovalCapacity); |
| | 472 | 1741 | | _pendingRemovalTypeIds = (ushort*)_allocator->Alloc(sizeof(ushort) * _pendingRemovalCapacity); |
| | | 1742 | | |
| | 472 | 1743 | | if (!preserveOwningGroups || _owningGroups == null || _owningGroupBindings == null) |
| | | 1744 | | { |
| | 471 | 1745 | | _owningGroups = new Dictionary<ulong, object>(); |
| | 471 | 1746 | | _owningGroupBindings = new List<IFrameOwningGroup>(); |
| | | 1747 | | } |
| | | 1748 | | |
| | 472 | 1749 | | _structuralCommandBuffer = default; |
| | 472 | 1750 | | _structuralCommandBufferInitialized = false; |
| | 472 | 1751 | | _deferStructuralChanges = false; |
| | 472 | 1752 | | _isReplayingStructuralChanges = false; |
| | 472 | 1753 | | ResetSystemAuthoringGuard(); |
| | 472 | 1754 | | } |
| | | 1755 | | |
| | | 1756 | | private void PrepareForSnapshotRestore(int entityCapacity, bool reuseExistingState) |
| | | 1757 | | { |
| | 79 | 1758 | | bool preserveOwningGroups = _owningGroupBindings != null && _owningGroupBindings.Count > 0; |
| | 79 | 1759 | | if (reuseExistingState) |
| | | 1760 | | { |
| | 76 | 1761 | | EnsureCompatibleStateForCopy(entityCapacity, preserveOwningGroups); |
| | 76 | 1762 | | return; |
| | | 1763 | | } |
| | | 1764 | | |
| | 3 | 1765 | | ReleaseState(preserveOwningGroups); |
| | 3 | 1766 | | InitializeState(entityCapacity, preserveOwningGroups); |
| | | 1767 | | |
| | 3 | 1768 | | if (preserveOwningGroups) |
| | | 1769 | | { |
| | 1 | 1770 | | ReinitializeOwningGroups(); |
| | | 1771 | | } |
| | 3 | 1772 | | } |
| | | 1773 | | |
| | | 1774 | | private void ResetInitializedStorages() |
| | | 1775 | | { |
| | 7248 | 1776 | | for (int typeId = 1; typeId <= ComponentRegistry.Count; typeId++) |
| | | 1777 | | { |
| | 3548 | 1778 | | if (_storageInitialized[typeId] && _componentStorages[typeId] != null) |
| | | 1779 | | { |
| | 62 | 1780 | | ComponentRegistry.GetStorageResetter(typeId)(_componentStorages[typeId]); |
| | | 1781 | | } |
| | | 1782 | | } |
| | 76 | 1783 | | } |
| | | 1784 | | |
| | | 1785 | | private void FinalizeSnapshotRestore() |
| | | 1786 | | { |
| | 78 | 1787 | | ResetStructuralChangeTracking(); |
| | | 1788 | | |
| | 156 | 1789 | | for (int i = 0; i < _pendingRemovalCount; i++) |
| | | 1790 | | { |
| | 0 | 1791 | | int typeId = _pendingRemovalTypeIds[i]; |
| | 0 | 1792 | | if (_storageInitialized[typeId] && _componentStorages[typeId] != null) |
| | | 1793 | | { |
| | 0 | 1794 | | ComponentRegistry.GetPendingRemovalMarker(typeId)(_componentStorages[typeId], _pendingRemovalEntitie |
| | | 1795 | | } |
| | | 1796 | | } |
| | | 1797 | | |
| | 78 | 1798 | | RebuildOwningGroups(); |
| | 78 | 1799 | | } |
| | | 1800 | | |
| | | 1801 | | private void EnsureCompatibleStateForCopy(int sourceEntityCapacity, bool preserveOwningGroups) |
| | | 1802 | | { |
| | 767 | 1803 | | if (_allocator != null && _entityCapacity == sourceEntityCapacity) |
| | | 1804 | | { |
| | 767 | 1805 | | return; |
| | | 1806 | | } |
| | | 1807 | | |
| | 0 | 1808 | | ReleaseState(preserveOwningGroups); |
| | 0 | 1809 | | InitializeState(sourceEntityCapacity, preserveOwningGroups); |
| | | 1810 | | |
| | 0 | 1811 | | if (preserveOwningGroups) |
| | | 1812 | | { |
| | 0 | 1813 | | ReinitializeOwningGroups(); |
| | | 1814 | | } |
| | 0 | 1815 | | } |
| | | 1816 | | |
| | | 1817 | | private void ReleaseState(bool preserveOwningGroups = false) |
| | | 1818 | | { |
| | 471 | 1819 | | ReleaseStructuralCommandBuffer(); |
| | | 1820 | | |
| | 471 | 1821 | | if (!preserveOwningGroups) |
| | | 1822 | | { |
| | 470 | 1823 | | _owningGroups?.Clear(); |
| | 470 | 1824 | | _owningGroupBindings?.Clear(); |
| | 470 | 1825 | | _owningGroups = null; |
| | 470 | 1826 | | _owningGroupBindings = null; |
| | | 1827 | | } |
| | | 1828 | | |
| | 471 | 1829 | | if (_storageInitialized != null && _componentStorages != null) |
| | | 1830 | | { |
| | 483246 | 1831 | | for (int i = 0; i < MaxComponentTypes; i++) |
| | | 1832 | | { |
| | 241152 | 1833 | | _componentStorages[i] = null; |
| | 241152 | 1834 | | _storageInitialized[i] = false; |
| | | 1835 | | } |
| | | 1836 | | } |
| | | 1837 | | |
| | 471 | 1838 | | if (_allocator != null) |
| | | 1839 | | { |
| | 471 | 1840 | | Allocator.Destroy(_allocator); |
| | 471 | 1841 | | _allocator = null; |
| | | 1842 | | } |
| | | 1843 | | |
| | 471 | 1844 | | _entities = null; |
| | 471 | 1845 | | _entityVersions = null; |
| | 471 | 1846 | | _entityNextFree = null; |
| | 471 | 1847 | | _entityComponentMasks = null; |
| | 471 | 1848 | | _componentStorages = null; |
| | 471 | 1849 | | _storageInitialized = null; |
| | 471 | 1850 | | _pendingRemovalEntities = null; |
| | 471 | 1851 | | _pendingRemovalTypeIds = null; |
| | 471 | 1852 | | _pendingRemovalCount = 0; |
| | 471 | 1853 | | _pendingRemovalCapacity = 0; |
| | 471 | 1854 | | _entityCapacity = 0; |
| | 471 | 1855 | | _entityCount = 0; |
| | 471 | 1856 | | _freeListHead = -1; |
| | 471 | 1857 | | Tick = 0; |
| | 471 | 1858 | | DeltaTime = FP.Zero; |
| | 471 | 1859 | | } |
| | | 1860 | | |
| | | 1861 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1862 | | private bool ShouldDeferStructuralChanges() |
| | | 1863 | | { |
| | 4709 | 1864 | | return _deferStructuralChanges && !_isReplayingStructuralChanges; |
| | | 1865 | | } |
| | | 1866 | | |
| | | 1867 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1868 | | private bool ShouldDeferSet<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 1869 | | { |
| | 105 | 1870 | | if (!ShouldDeferStructuralChanges()) |
| | | 1871 | | { |
| | 105 | 1872 | | return false; |
| | | 1873 | | } |
| | | 1874 | | |
| | 0 | 1875 | | if (entity.Index < 0) |
| | | 1876 | | { |
| | 0 | 1877 | | return true; |
| | | 1878 | | } |
| | | 1879 | | |
| | 0 | 1880 | | if (!IsValid(entity)) |
| | | 1881 | | { |
| | 0 | 1882 | | return false; |
| | | 1883 | | } |
| | | 1884 | | |
| | 0 | 1885 | | return !Has<T>(entity); |
| | | 1886 | | } |
| | | 1887 | | |
| | | 1888 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1889 | | private EntityRef QueueCreateEntity() |
| | | 1890 | | { |
| | 202 | 1891 | | EnsureStructuralCommandBuffer(); |
| | 202 | 1892 | | return _structuralCommandBuffer.CreateEntity(); |
| | | 1893 | | } |
| | | 1894 | | |
| | | 1895 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1896 | | private void QueueDestroyEntity(EntityRef entity) |
| | | 1897 | | { |
| | 166 | 1898 | | if (entity.Index >= 0 && !IsValid(entity)) |
| | | 1899 | | { |
| | 0 | 1900 | | return; |
| | | 1901 | | } |
| | | 1902 | | |
| | 166 | 1903 | | EnsureStructuralCommandBuffer(); |
| | 166 | 1904 | | _structuralCommandBuffer.DestroyEntity(entity); |
| | 166 | 1905 | | } |
| | | 1906 | | |
| | | 1907 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1908 | | private void QueueAddComponent<T>(EntityRef entity, in T component) where T : unmanaged, IComponent |
| | | 1909 | | { |
| | 950 | 1910 | | if (entity.Index >= 0 && !IsValid(entity)) |
| | | 1911 | | { |
| | 0 | 1912 | | throw new ArgumentException($"Invalid entity: {entity}"); |
| | | 1913 | | } |
| | | 1914 | | |
| | 950 | 1915 | | EnsureStructuralCommandBuffer(); |
| | 950 | 1916 | | _structuralCommandBuffer.AddComponent(entity, component); |
| | 950 | 1917 | | } |
| | | 1918 | | |
| | | 1919 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1920 | | private void QueueSetComponent<T>(EntityRef entity, in T component) where T : unmanaged, IComponent |
| | | 1921 | | { |
| | 0 | 1922 | | if (entity.Index >= 0 && !IsValid(entity)) |
| | | 1923 | | { |
| | 0 | 1924 | | throw new ArgumentException($"Invalid entity: {entity}"); |
| | | 1925 | | } |
| | | 1926 | | |
| | 0 | 1927 | | EnsureStructuralCommandBuffer(); |
| | 0 | 1928 | | _structuralCommandBuffer.SetComponent(entity, component); |
| | 0 | 1929 | | } |
| | | 1930 | | |
| | | 1931 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 1932 | | private void QueueRemoveComponent<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 1933 | | { |
| | 1 | 1934 | | if (entity.Index >= 0 && !IsValid(entity)) |
| | | 1935 | | { |
| | 0 | 1936 | | return; |
| | | 1937 | | } |
| | | 1938 | | |
| | 1 | 1939 | | EnsureStructuralCommandBuffer(); |
| | 1 | 1940 | | _structuralCommandBuffer.RemoveComponent<T>(entity); |
| | 1 | 1941 | | } |
| | | 1942 | | |
| | | 1943 | | private void EnsureStructuralCommandBuffer() |
| | | 1944 | | { |
| | 1945 | 1945 | | if (_structuralCommandBufferInitialized) |
| | | 1946 | | { |
| | 1695 | 1947 | | return; |
| | | 1948 | | } |
| | | 1949 | | |
| | 250 | 1950 | | _structuralCommandBuffer.Initialize(capacity: 1024); |
| | 250 | 1951 | | _structuralCommandBufferInitialized = true; |
| | 250 | 1952 | | } |
| | | 1953 | | |
| | | 1954 | | private void ResetStructuralChangeTracking() |
| | | 1955 | | { |
| | 769 | 1956 | | if (_structuralCommandBufferInitialized) |
| | | 1957 | | { |
| | 360 | 1958 | | _structuralCommandBuffer.Clear(); |
| | | 1959 | | } |
| | | 1960 | | |
| | 769 | 1961 | | _deferStructuralChanges = false; |
| | 769 | 1962 | | _isReplayingStructuralChanges = false; |
| | 769 | 1963 | | ResetSystemAuthoringGuard(); |
| | 769 | 1964 | | } |
| | | 1965 | | |
| | | 1966 | | private void ReleaseStructuralCommandBuffer() |
| | | 1967 | | { |
| | 471 | 1968 | | if (_structuralCommandBufferInitialized) |
| | | 1969 | | { |
| | 250 | 1970 | | _structuralCommandBuffer.Dispose(); |
| | 250 | 1971 | | _structuralCommandBufferInitialized = false; |
| | | 1972 | | } |
| | | 1973 | | |
| | 471 | 1974 | | _structuralCommandBuffer = default; |
| | 471 | 1975 | | _deferStructuralChanges = false; |
| | 471 | 1976 | | _isReplayingStructuralChanges = false; |
| | 471 | 1977 | | ResetSystemAuthoringGuard(); |
| | 471 | 1978 | | } |
| | | 1979 | | |
| | | 1980 | | private void EnsureGlobalReadAllowed(string apiName) |
| | | 1981 | | { |
| | 112 | 1982 | | if (!_hasActiveSystemAuthoringContract || _activeSystemAuthoringContract.AllowsGlobalReads) |
| | | 1983 | | { |
| | 111 | 1984 | | return; |
| | | 1985 | | } |
| | | 1986 | | |
| | 1 | 1987 | | throw new InvalidOperationException( |
| | 1 | 1988 | | $"System {_activeSystemName} cannot call {apiName} because its contract does not allow global state acce |
| | | 1989 | | } |
| | | 1990 | | |
| | | 1991 | | private void EnsureGlobalWriteAllowed(string apiName) |
| | | 1992 | | { |
| | 261 | 1993 | | if (!_hasActiveSystemAuthoringContract || _activeSystemAuthoringContract.AllowsGlobalWrites) |
| | | 1994 | | { |
| | 260 | 1995 | | return; |
| | | 1996 | | } |
| | | 1997 | | |
| | 1 | 1998 | | throw new InvalidOperationException( |
| | 1 | 1999 | | $"System {_activeSystemName} cannot call {apiName} because its contract does not allow writable global s |
| | | 2000 | | } |
| | | 2001 | | |
| | | 2002 | | private void EnsureStructuralChangesAllowed(string apiName) |
| | | 2003 | | { |
| | 3972 | 2004 | | if (!_hasActiveSystemAuthoringContract || _activeSystemAuthoringContract.AllowsStructuralChanges) |
| | | 2005 | | { |
| | 3971 | 2006 | | return; |
| | | 2007 | | } |
| | | 2008 | | |
| | 1 | 2009 | | throw new InvalidOperationException( |
| | 1 | 2010 | | $"System {_activeSystemName} cannot call {apiName} because its contract does not allow structural change |
| | | 2011 | | } |
| | | 2012 | | |
| | | 2013 | | private void ResetSystemAuthoringGuard() |
| | | 2014 | | { |
| | 2716 | 2015 | | _hasActiveSystemAuthoringContract = false; |
| | 2716 | 2016 | | _activeSystemAuthoringContract = SystemAuthoringContract.Default; |
| | 2716 | 2017 | | _activeSystemName = null; |
| | 2716 | 2018 | | } |
| | | 2019 | | |
| | | 2020 | | private void ReinitializeOwningGroups() |
| | | 2021 | | { |
| | 1 | 2022 | | if (_owningGroupBindings == null || _owningGroupBindings.Count == 0) |
| | | 2023 | | { |
| | 0 | 2024 | | return; |
| | | 2025 | | } |
| | | 2026 | | |
| | 4 | 2027 | | for (int i = 0; i < _owningGroupBindings.Count; i++) |
| | | 2028 | | { |
| | 1 | 2029 | | _owningGroupBindings[i].Reinitialize(_allocator, _entityCapacity); |
| | | 2030 | | } |
| | 1 | 2031 | | } |
| | | 2032 | | |
| | | 2033 | | #endregion |
| | | 2034 | | } |
| | | 2035 | | } |