| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using Lattice.Core; |
| | | 8 | | using Lattice.ECS.Core; |
| | | 9 | | |
| | | 10 | | namespace Lattice.ECS.Framework |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// 命令类型 |
| | | 14 | | /// </summary> |
| | | 15 | | public enum CommandType : byte |
| | | 16 | | { |
| | | 17 | | CreateEntity, |
| | | 18 | | DestroyEntity, |
| | | 19 | | AddComponent, |
| | | 20 | | RemoveComponent, |
| | | 21 | | SetComponent, |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// 命令头(变长命令的前4字节) |
| | | 26 | | /// </summary> |
| | | 27 | | [StructLayout(LayoutKind.Sequential, Pack = 1)] |
| | | 28 | | public unsafe struct CommandHeader |
| | | 29 | | { |
| | | 30 | | public CommandType Type; |
| | | 31 | | public ushort ComponentTypeId; // ushort.MaxValue = 无组件 |
| | | 32 | | public ushort PayloadSize; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// 命令缓冲 - 预测回滚模型的核心组件 |
| | | 37 | | /// |
| | | 38 | | /// 设计原理: |
| | | 39 | | /// 1. 所有修改 ECS 状态的操作都通过命令缓冲 |
| | | 40 | | /// 2. 命令在帧结束时批量执行(Playback) |
| | | 41 | | /// 3. 支持命令的序列化(用于网络同步) |
| | | 42 | | /// 4. 支持命令的撤销(用于回滚) |
| | | 43 | | /// </summary> |
| | | 44 | | public unsafe struct CommandBuffer |
| | | 45 | | { |
| | | 46 | | // 命令数据缓冲区 |
| | | 47 | | private byte* _buffer; |
| | | 48 | | private int _capacity; |
| | | 49 | | private int _writePosition; |
| | | 50 | | |
| | | 51 | | // 实体创建追踪(用于映射临时索引) |
| | | 52 | | private EntityRef* _createdEntities; |
| | | 53 | | private int _createdCount; |
| | | 54 | | private int _createdCapacity; |
| | | 55 | | |
| | | 56 | | // 分配器 |
| | | 57 | | private Allocator* _allocator; |
| | | 58 | | private bool _ownsAllocator; |
| | | 59 | | |
| | | 60 | | /// <summary>是否为空</summary> |
| | 770 | 61 | | public bool IsEmpty => _writePosition == 0; |
| | | 62 | | |
| | | 63 | | /// <summary>命令数量</summary> |
| | 0 | 64 | | public int CreatedEntityCount => _createdCount; |
| | | 65 | | |
| | | 66 | | /// <summary> |
| | | 67 | | /// 初始化命令缓冲 |
| | | 68 | | /// </summary> |
| | | 69 | | public void Initialize(int capacity = 4096) |
| | | 70 | | { |
| | 251 | 71 | | _allocator = Allocator.Create(); |
| | 251 | 72 | | Initialize(_allocator, capacity); |
| | 251 | 73 | | _ownsAllocator = true; |
| | 251 | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// 初始化命令缓冲 |
| | | 78 | | /// </summary> |
| | | 79 | | public void Initialize(Frame frame, int capacity = 4096) |
| | | 80 | | { |
| | 23 | 81 | | ArgumentNullException.ThrowIfNull(frame); |
| | 23 | 82 | | Initialize(frame.GetAllocatorPointer(), capacity); |
| | 23 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// 初始化命令缓冲 |
| | | 87 | | /// </summary> |
| | | 88 | | public void Initialize(Allocator* allocator, int capacity = 4096) |
| | | 89 | | { |
| | 274 | 90 | | if (allocator == null) |
| | | 91 | | { |
| | 0 | 92 | | throw new ArgumentNullException(nameof(allocator)); |
| | | 93 | | } |
| | | 94 | | |
| | 274 | 95 | | _allocator = allocator; |
| | 274 | 96 | | _capacity = capacity; |
| | 274 | 97 | | _writePosition = 0; |
| | 274 | 98 | | _createdCount = 0; |
| | 274 | 99 | | _createdCapacity = 64; |
| | 274 | 100 | | _ownsAllocator = false; |
| | | 101 | | |
| | 274 | 102 | | _buffer = (byte*)allocator->Alloc(capacity); |
| | 274 | 103 | | _createdEntities = (EntityRef*)allocator->Alloc(sizeof(EntityRef) * _createdCapacity); |
| | 274 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// 释放命令缓冲 |
| | | 108 | | /// </summary> |
| | | 109 | | public void Dispose() |
| | | 110 | | { |
| | 298 | 111 | | _buffer = null; |
| | 298 | 112 | | _createdEntities = null; |
| | 298 | 113 | | _writePosition = 0; |
| | 298 | 114 | | _createdCount = 0; |
| | | 115 | | |
| | 298 | 116 | | if (_ownsAllocator && _allocator != null) |
| | | 117 | | { |
| | 251 | 118 | | Allocator.Destroy(_allocator); |
| | | 119 | | } |
| | | 120 | | |
| | 298 | 121 | | _allocator = null; |
| | 298 | 122 | | _ownsAllocator = false; |
| | 298 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <summary> |
| | | 126 | | /// 清空缓冲 |
| | | 127 | | /// </summary> |
| | | 128 | | public void Clear() |
| | | 129 | | { |
| | 1107 | 130 | | _writePosition = 0; |
| | 1107 | 131 | | _createdCount = 0; |
| | 1107 | 132 | | } |
| | | 133 | | |
| | | 134 | | #region 命令写入 |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// 创建实体命令 |
| | | 138 | | /// </summary> |
| | | 139 | | public EntityRef CreateEntity() |
| | | 140 | | { |
| | 204 | 141 | | EnsureSpace(sizeof(CommandHeader)); |
| | | 142 | | |
| | 204 | 143 | | var header = new CommandHeader |
| | 204 | 144 | | { |
| | 204 | 145 | | Type = CommandType.CreateEntity, |
| | 204 | 146 | | ComponentTypeId = ushort.MaxValue, |
| | 204 | 147 | | PayloadSize = 0 |
| | 204 | 148 | | }; |
| | | 149 | | |
| | 204 | 150 | | WriteHeader(&header); |
| | | 151 | | |
| | | 152 | | // 记录临时实体引用(实际索引在 Playback 时确定) |
| | 204 | 153 | | var tempRef = new EntityRef(-(_createdCount + 1), 0); // 负索引表示临时 |
| | | 154 | | |
| | | 155 | | // 扩展创建列表 |
| | 204 | 156 | | if (_createdCount >= _createdCapacity) |
| | | 157 | | { |
| | 0 | 158 | | _createdCapacity *= 2; |
| | 0 | 159 | | var newArray = (EntityRef*)_allocator->Alloc(sizeof(EntityRef) * _createdCapacity); |
| | 0 | 160 | | Buffer.MemoryCopy(_createdEntities, newArray, |
| | 0 | 161 | | sizeof(EntityRef) * _createdCapacity, sizeof(EntityRef) * _createdCount); |
| | 0 | 162 | | _createdEntities = newArray; |
| | | 163 | | } |
| | | 164 | | |
| | 204 | 165 | | _createdEntities[_createdCount++] = tempRef; |
| | 204 | 166 | | return tempRef; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// 销毁实体命令 |
| | | 171 | | /// </summary> |
| | | 172 | | public void DestroyEntity(EntityRef entity) |
| | | 173 | | { |
| | 167 | 174 | | int payloadSize = sizeof(EntityRef); |
| | 167 | 175 | | EnsureSpace(sizeof(CommandHeader) + payloadSize); |
| | | 176 | | |
| | 167 | 177 | | var header = new CommandHeader |
| | 167 | 178 | | { |
| | 167 | 179 | | Type = CommandType.DestroyEntity, |
| | 167 | 180 | | ComponentTypeId = ushort.MaxValue, |
| | 167 | 181 | | PayloadSize = checked((ushort)payloadSize) |
| | 167 | 182 | | }; |
| | | 183 | | |
| | 167 | 184 | | WriteHeader(&header); |
| | 167 | 185 | | WriteData(&entity, payloadSize); |
| | 167 | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// 添加组件命令(泛型版本,需要编译时知道类型) |
| | | 190 | | /// </summary> |
| | | 191 | | public void AddComponent<T>(EntityRef entity, T component) where T : unmanaged, IComponent |
| | | 192 | | { |
| | 955 | 193 | | int componentSize = sizeof(T); |
| | 955 | 194 | | int payloadSize = sizeof(EntityRef) + componentSize; |
| | 955 | 195 | | EnsureSpace(sizeof(CommandHeader) + payloadSize); |
| | | 196 | | |
| | 955 | 197 | | var header = new CommandHeader |
| | 955 | 198 | | { |
| | 955 | 199 | | Type = CommandType.AddComponent, |
| | 955 | 200 | | ComponentTypeId = checked((ushort)ComponentTypeId<T>.Id), |
| | 955 | 201 | | PayloadSize = checked((ushort)payloadSize) |
| | 955 | 202 | | }; |
| | | 203 | | |
| | 955 | 204 | | WriteHeader(&header); |
| | 955 | 205 | | WriteData(&entity, sizeof(EntityRef)); |
| | 955 | 206 | | WriteData(&component, componentSize); |
| | 955 | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// 移除组件命令 |
| | | 211 | | /// </summary> |
| | | 212 | | public void RemoveComponent<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 213 | | { |
| | 3 | 214 | | int payloadSize = sizeof(EntityRef); |
| | 3 | 215 | | EnsureSpace(sizeof(CommandHeader) + payloadSize); |
| | | 216 | | |
| | 3 | 217 | | var header = new CommandHeader |
| | 3 | 218 | | { |
| | 3 | 219 | | Type = CommandType.RemoveComponent, |
| | 3 | 220 | | ComponentTypeId = checked((ushort)ComponentTypeId<T>.Id), |
| | 3 | 221 | | PayloadSize = checked((ushort)payloadSize) |
| | 3 | 222 | | }; |
| | | 223 | | |
| | 3 | 224 | | WriteHeader(&header); |
| | 3 | 225 | | WriteData(&entity, payloadSize); |
| | 3 | 226 | | } |
| | | 227 | | |
| | | 228 | | /// <summary> |
| | | 229 | | /// 设置组件命令(替换现有组件) |
| | | 230 | | /// </summary> |
| | | 231 | | public void SetComponent<T>(EntityRef entity, T component) where T : unmanaged, IComponent |
| | | 232 | | { |
| | 3 | 233 | | int componentSize = sizeof(T); |
| | 3 | 234 | | int payloadSize = sizeof(EntityRef) + componentSize; |
| | 3 | 235 | | EnsureSpace(sizeof(CommandHeader) + payloadSize); |
| | | 236 | | |
| | 3 | 237 | | var header = new CommandHeader |
| | 3 | 238 | | { |
| | 3 | 239 | | Type = CommandType.SetComponent, |
| | 3 | 240 | | ComponentTypeId = checked((ushort)ComponentTypeId<T>.Id), |
| | 3 | 241 | | PayloadSize = checked((ushort)payloadSize) |
| | 3 | 242 | | }; |
| | | 243 | | |
| | 3 | 244 | | WriteHeader(&header); |
| | 3 | 245 | | WriteData(&entity, sizeof(EntityRef)); |
| | 3 | 246 | | WriteData(&component, componentSize); |
| | 3 | 247 | | } |
| | | 248 | | |
| | | 249 | | #endregion |
| | | 250 | | |
| | | 251 | | #region 命令执行 |
| | | 252 | | |
| | | 253 | | /// <summary> |
| | | 254 | | /// 执行所有命令(在帧开始时调用) |
| | | 255 | | /// </summary> |
| | | 256 | | public void Playback(Frame frame) |
| | | 257 | | { |
| | 161 | 258 | | if (IsEmpty) return; |
| | | 259 | | |
| | 121 | 260 | | byte* ptr = _buffer; |
| | 121 | 261 | | byte* end = _buffer + _writePosition; |
| | 121 | 262 | | int createdIndex = 0; |
| | | 263 | | |
| | 1453 | 264 | | while (ptr < end) |
| | | 265 | | { |
| | 1332 | 266 | | if (end - ptr < sizeof(CommandHeader)) |
| | | 267 | | { |
| | 0 | 268 | | throw new InvalidOperationException("Command buffer ended before a full header could be read."); |
| | | 269 | | } |
| | | 270 | | |
| | 1332 | 271 | | var header = *(CommandHeader*)ptr; |
| | 1332 | 272 | | ptr += sizeof(CommandHeader); |
| | 1332 | 273 | | byte* payload = ptr; |
| | 1332 | 274 | | if (payload + header.PayloadSize > end) |
| | | 275 | | { |
| | 0 | 276 | | throw new InvalidOperationException($"Command payload exceeds buffer bounds. Type={header.Type}, Pay |
| | | 277 | | } |
| | | 278 | | |
| | 1332 | 279 | | ValidatePayloadSize(header); |
| | 1332 | 280 | | ptr += header.PayloadSize; |
| | | 281 | | |
| | 1332 | 282 | | switch (header.Type) |
| | | 283 | | { |
| | | 284 | | case CommandType.CreateEntity: |
| | | 285 | | { |
| | 204 | 286 | | var entity = frame.CreateEntity(); |
| | 204 | 287 | | if (createdIndex < _createdCount) |
| | | 288 | | { |
| | 204 | 289 | | _createdEntities[createdIndex++] = entity; |
| | | 290 | | } |
| | 204 | 291 | | break; |
| | | 292 | | } |
| | | 293 | | |
| | | 294 | | case CommandType.DestroyEntity: |
| | | 295 | | { |
| | 167 | 296 | | EntityRef entity = ResolveEntity(payload); |
| | 167 | 297 | | frame.DestroyEntity(entity); |
| | 167 | 298 | | break; |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | case CommandType.AddComponent: |
| | | 302 | | { |
| | 955 | 303 | | EntityRef entity = ResolveEntity(payload); |
| | 955 | 304 | | PlaybackAddComponent(frame, entity, header.ComponentTypeId, payload + sizeof(EntityRef)); |
| | 955 | 305 | | break; |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | case CommandType.RemoveComponent: |
| | | 309 | | { |
| | 3 | 310 | | EntityRef entity = ResolveEntity(payload); |
| | 3 | 311 | | PlaybackRemoveComponent(frame, entity, header.ComponentTypeId); |
| | 3 | 312 | | break; |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | case CommandType.SetComponent: |
| | | 316 | | { |
| | 3 | 317 | | EntityRef entity = ResolveEntity(payload); |
| | 3 | 318 | | PlaybackSetComponent(frame, entity, header.ComponentTypeId, payload + sizeof(EntityRef)); |
| | | 319 | | break; |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | } |
| | | 323 | | |
| | 121 | 324 | | frame.CommitDeferredRemovals(); |
| | 121 | 325 | | Clear(); |
| | 121 | 326 | | } |
| | | 327 | | |
| | | 328 | | #endregion |
| | | 329 | | |
| | | 330 | | #region 序列化(网络同步) |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// 序列化命令缓冲到字节数组(用于网络同步) |
| | | 334 | | /// </summary> |
| | | 335 | | public byte[] Serialize() |
| | | 336 | | { |
| | 0 | 337 | | var result = new byte[_writePosition + sizeof(int)]; |
| | 0 | 338 | | fixed (byte* ptr = result) |
| | | 339 | | { |
| | 0 | 340 | | *(int*)ptr = _writePosition; |
| | 0 | 341 | | Buffer.MemoryCopy(_buffer, ptr + sizeof(int), _writePosition, _writePosition); |
| | | 342 | | } |
| | 0 | 343 | | return result; |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | /// <summary> |
| | | 347 | | /// 从字节数组反序列化 |
| | | 348 | | /// </summary> |
| | | 349 | | public void Deserialize(byte[] data) |
| | | 350 | | { |
| | 0 | 351 | | if (data == null) |
| | | 352 | | { |
| | 0 | 353 | | throw new ArgumentNullException(nameof(data)); |
| | | 354 | | } |
| | | 355 | | |
| | 0 | 356 | | if (data.Length < sizeof(int)) |
| | | 357 | | { |
| | 0 | 358 | | throw new InvalidOperationException("Serialized command buffer is too short."); |
| | | 359 | | } |
| | | 360 | | |
| | 0 | 361 | | fixed (byte* ptr = data) |
| | | 362 | | { |
| | 0 | 363 | | int size = *(int*)ptr; |
| | 0 | 364 | | if (size < 0 || size > data.Length - sizeof(int)) |
| | | 365 | | { |
| | 0 | 366 | | throw new InvalidOperationException($"Serialized command buffer size is invalid: {size}"); |
| | | 367 | | } |
| | | 368 | | |
| | 0 | 369 | | EnsureSpace(size); |
| | 0 | 370 | | Buffer.MemoryCopy(ptr + sizeof(int), _buffer, size, size); |
| | 0 | 371 | | _writePosition = size; |
| | 0 | 372 | | RecountCreatedEntities(); |
| | | 373 | | } |
| | 0 | 374 | | } |
| | | 375 | | |
| | | 376 | | #endregion |
| | | 377 | | |
| | | 378 | | #region 内部辅助 |
| | | 379 | | |
| | | 380 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 381 | | private void EnsureSpace(int size) |
| | | 382 | | { |
| | 1332 | 383 | | if (_writePosition + size > _capacity) |
| | | 384 | | { |
| | | 385 | | // 扩展缓冲区 |
| | 0 | 386 | | int newCapacity = _capacity * 2; |
| | 0 | 387 | | while (newCapacity < _writePosition + size) |
| | 0 | 388 | | newCapacity *= 2; |
| | | 389 | | |
| | 0 | 390 | | var newBuffer = (byte*)_allocator->Alloc(newCapacity); |
| | 0 | 391 | | Buffer.MemoryCopy(_buffer, newBuffer, _capacity, _writePosition); |
| | 0 | 392 | | _buffer = newBuffer; |
| | 0 | 393 | | _capacity = newCapacity; |
| | | 394 | | } |
| | 1332 | 395 | | } |
| | | 396 | | |
| | | 397 | | private void EnsureCreatedCapacity(int requiredCapacity) |
| | | 398 | | { |
| | 0 | 399 | | if (requiredCapacity <= _createdCapacity) |
| | | 400 | | { |
| | 0 | 401 | | return; |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | int newCapacity = _createdCapacity; |
| | 0 | 405 | | while (newCapacity < requiredCapacity) |
| | | 406 | | { |
| | 0 | 407 | | newCapacity *= 2; |
| | | 408 | | } |
| | | 409 | | |
| | 0 | 410 | | var newArray = (EntityRef*)_allocator->Alloc(sizeof(EntityRef) * newCapacity); |
| | 0 | 411 | | if (_createdCount > 0) |
| | | 412 | | { |
| | 0 | 413 | | Buffer.MemoryCopy( |
| | 0 | 414 | | _createdEntities, |
| | 0 | 415 | | newArray, |
| | 0 | 416 | | sizeof(EntityRef) * newCapacity, |
| | 0 | 417 | | sizeof(EntityRef) * _createdCount); |
| | | 418 | | } |
| | | 419 | | |
| | 0 | 420 | | _createdEntities = newArray; |
| | 0 | 421 | | _createdCapacity = newCapacity; |
| | 0 | 422 | | } |
| | | 423 | | |
| | | 424 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 425 | | private void WriteHeader(CommandHeader* header) |
| | | 426 | | { |
| | 1332 | 427 | | *(CommandHeader*)(_buffer + _writePosition) = *header; |
| | 1332 | 428 | | _writePosition += sizeof(CommandHeader); |
| | 1332 | 429 | | } |
| | | 430 | | |
| | | 431 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 432 | | private void WriteData(void* data, int size) |
| | | 433 | | { |
| | 2086 | 434 | | Buffer.MemoryCopy(data, _buffer + _writePosition, size, size); |
| | 2086 | 435 | | _writePosition += size; |
| | 2086 | 436 | | } |
| | | 437 | | |
| | | 438 | | private void PlaybackAddComponent(Frame frame, EntityRef entity, ushort typeId, byte* ptr) |
| | | 439 | | { |
| | 955 | 440 | | ComponentCommandRegistry.PlaybackAdd(frame, entity, typeId, ptr); |
| | 955 | 441 | | } |
| | | 442 | | |
| | | 443 | | private void PlaybackRemoveComponent(Frame frame, EntityRef entity, ushort typeId) |
| | | 444 | | { |
| | 3 | 445 | | ComponentCommandRegistry.PlaybackRemove(frame, entity, typeId); |
| | 3 | 446 | | } |
| | | 447 | | |
| | | 448 | | private void PlaybackSetComponent(Frame frame, EntityRef entity, ushort typeId, byte* ptr) |
| | | 449 | | { |
| | 3 | 450 | | ComponentCommandRegistry.PlaybackSet(frame, entity, typeId, ptr); |
| | 3 | 451 | | } |
| | | 452 | | |
| | | 453 | | private EntityRef ResolveEntity(byte* payload) |
| | | 454 | | { |
| | 1128 | 455 | | EntityRef entity = *(EntityRef*)payload; |
| | 1128 | 456 | | if (entity.Index >= 0) |
| | | 457 | | { |
| | 172 | 458 | | return entity; |
| | | 459 | | } |
| | | 460 | | |
| | 956 | 461 | | int createdOrdinal = -entity.Index - 1; |
| | 956 | 462 | | if ((uint)createdOrdinal >= (uint)_createdCount) |
| | | 463 | | { |
| | 0 | 464 | | throw new InvalidOperationException($"未找到临时实体映射:{entity}。"); |
| | | 465 | | } |
| | | 466 | | |
| | 956 | 467 | | return _createdEntities[createdOrdinal]; |
| | | 468 | | } |
| | | 469 | | |
| | | 470 | | private static void ValidatePayloadSize(in CommandHeader header) |
| | | 471 | | { |
| | 1332 | 472 | | int expectedSize = header.Type switch |
| | 1332 | 473 | | { |
| | 204 | 474 | | CommandType.CreateEntity => 0, |
| | 167 | 475 | | CommandType.DestroyEntity => sizeof(EntityRef), |
| | 3 | 476 | | CommandType.RemoveComponent => sizeof(EntityRef), |
| | 955 | 477 | | CommandType.AddComponent => sizeof(EntityRef) + ComponentCommandRegistry.GetComponentSize(header.Compone |
| | 3 | 478 | | CommandType.SetComponent => sizeof(EntityRef) + ComponentCommandRegistry.GetComponentSize(header.Compone |
| | 0 | 479 | | _ => throw new InvalidOperationException($"Unknown command type: {header.Type}") |
| | 1332 | 480 | | }; |
| | | 481 | | |
| | 1332 | 482 | | if (header.PayloadSize != expectedSize) |
| | | 483 | | { |
| | 0 | 484 | | throw new InvalidOperationException( |
| | 0 | 485 | | $"Command payload size mismatch. Type={header.Type}, ComponentTypeId={header.ComponentTypeId}, Expec |
| | | 486 | | } |
| | 1332 | 487 | | } |
| | | 488 | | |
| | | 489 | | private void RecountCreatedEntities() |
| | | 490 | | { |
| | 0 | 491 | | _createdCount = 0; |
| | | 492 | | |
| | 0 | 493 | | byte* ptr = _buffer; |
| | 0 | 494 | | byte* end = _buffer + _writePosition; |
| | 0 | 495 | | while (ptr < end) |
| | | 496 | | { |
| | 0 | 497 | | CommandHeader header = *(CommandHeader*)ptr; |
| | 0 | 498 | | ptr += sizeof(CommandHeader); |
| | | 499 | | |
| | 0 | 500 | | if (header.Type == CommandType.CreateEntity) |
| | | 501 | | { |
| | 0 | 502 | | EnsureCreatedCapacity(_createdCount + 1); |
| | 0 | 503 | | _createdEntities[_createdCount++] = new EntityRef(-_createdCount, 0); |
| | | 504 | | } |
| | | 505 | | |
| | 0 | 506 | | ptr += header.PayloadSize; |
| | 0 | 507 | | if (ptr > end) |
| | | 508 | | { |
| | 0 | 509 | | throw new InvalidOperationException("Serialized command buffer is malformed."); |
| | | 510 | | } |
| | | 511 | | } |
| | 0 | 512 | | } |
| | | 513 | | |
| | | 514 | | #endregion |
| | | 515 | | } |
| | | 516 | | } |