| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Buffers.Binary; |
| | | 6 | | using Lattice.ECS.Serialization; |
| | | 7 | | |
| | | 8 | | namespace Lattice.ECS.Core |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// 帧状态写入器基类。 |
| | | 12 | | /// </summary> |
| | | 13 | | internal abstract unsafe class FrameStateWriter |
| | | 14 | | { |
| | | 15 | | public abstract void WriteByte(byte value); |
| | | 16 | | |
| | | 17 | | public abstract void WriteBytes(void* data, int length); |
| | | 18 | | |
| | | 19 | | public abstract void WriteBytes(ReadOnlySpan<byte> data); |
| | | 20 | | |
| | | 21 | | public abstract int BytesWritten { get; } |
| | | 22 | | |
| | | 23 | | public void WriteInt32(int value) |
| | | 24 | | { |
| | | 25 | | Span<byte> buffer = stackalloc byte[sizeof(int)]; |
| | | 26 | | BinaryPrimitives.WriteInt32LittleEndian(buffer, value); |
| | | 27 | | WriteBytes(buffer); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | public void WriteInt64(long value) |
| | | 31 | | { |
| | | 32 | | Span<byte> buffer = stackalloc byte[sizeof(long)]; |
| | | 33 | | BinaryPrimitives.WriteInt64LittleEndian(buffer, value); |
| | | 34 | | WriteBytes(buffer); |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | public void WriteUInt16(ushort value) |
| | | 38 | | { |
| | | 39 | | Span<byte> buffer = stackalloc byte[sizeof(ushort)]; |
| | | 40 | | BinaryPrimitives.WriteUInt16LittleEndian(buffer, value); |
| | | 41 | | WriteBytes(buffer); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | public void WriteUInt64(ulong value) |
| | | 45 | | { |
| | | 46 | | Span<byte> buffer = stackalloc byte[sizeof(ulong)]; |
| | | 47 | | BinaryPrimitives.WriteUInt64LittleEndian(buffer, value); |
| | | 48 | | WriteBytes(buffer); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// 直接写入连续字节缓冲区的状态写入器。 |
| | | 54 | | /// </summary> |
| | | 55 | | internal sealed unsafe class FrameStateBufferWriter : FrameStateWriter |
| | | 56 | | { |
| | | 57 | | private byte[] _buffer; |
| | | 58 | | private int _position; |
| | | 59 | | |
| | | 60 | | public FrameStateBufferWriter(int initialCapacity = 1024) |
| | | 61 | | { |
| | | 62 | | _buffer = new byte[initialCapacity]; |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public int Length => _position; |
| | | 66 | | |
| | | 67 | | public override int BytesWritten => _position; |
| | | 68 | | |
| | | 69 | | public override void WriteByte(byte value) |
| | | 70 | | { |
| | | 71 | | EnsureCapacity(sizeof(byte)); |
| | | 72 | | _buffer[_position++] = value; |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | public override void WriteBytes(void* data, int length) |
| | | 76 | | { |
| | | 77 | | if (length <= 0) |
| | | 78 | | { |
| | | 79 | | return; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | EnsureCapacity(length); |
| | | 83 | | fixed (byte* destination = &_buffer[_position]) |
| | | 84 | | { |
| | | 85 | | Buffer.MemoryCopy(data, destination, length, length); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | _position += length; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | public override void WriteBytes(ReadOnlySpan<byte> data) |
| | | 92 | | { |
| | | 93 | | if (data.Length == 0) |
| | | 94 | | { |
| | | 95 | | return; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | EnsureCapacity(data.Length); |
| | | 99 | | data.CopyTo(_buffer.AsSpan(_position)); |
| | | 100 | | _position += data.Length; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | public byte[] ToArray() |
| | | 104 | | { |
| | | 105 | | byte[] data = new byte[_position]; |
| | | 106 | | Buffer.BlockCopy(_buffer, 0, data, 0, _position); |
| | | 107 | | return data; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | public PackedFrameSnapshot ToSnapshot(int tick, int entityCapacity, ComponentSchemaManifest schemaManifest) |
| | | 111 | | { |
| | | 112 | | byte[] data = _buffer; |
| | | 113 | | _buffer = Array.Empty<byte>(); |
| | | 114 | | return new PackedFrameSnapshot(tick, entityCapacity, data, _position, PackedFrameSnapshot.CurrentFormatVersi |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private void EnsureCapacity(int appendLength) |
| | | 118 | | { |
| | | 119 | | int required = _position + appendLength; |
| | | 120 | | if (required <= _buffer.Length) |
| | | 121 | | { |
| | | 122 | | return; |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | int newCapacity = System.Math.Max(required, _buffer.Length * 2); |
| | | 126 | | byte[] newBuffer = new byte[newCapacity]; |
| | | 127 | | Buffer.BlockCopy(_buffer, 0, newBuffer, 0, _position); |
| | | 128 | | _buffer = newBuffer; |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// 仅统计写入字节数的状态写入器。 |
| | | 134 | | /// </summary> |
| | | 135 | | internal sealed unsafe class FrameStateSizingWriter : FrameStateWriter |
| | | 136 | | { |
| | | 137 | | private int _bytesWritten; |
| | | 138 | | |
| | | 139 | | public override int BytesWritten => _bytesWritten; |
| | | 140 | | |
| | | 141 | | public override void WriteByte(byte value) |
| | | 142 | | { |
| | | 143 | | _bytesWritten += sizeof(byte); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public override void WriteBytes(void* data, int length) |
| | | 147 | | { |
| | | 148 | | if (length > 0) |
| | | 149 | | { |
| | | 150 | | _bytesWritten += length; |
| | | 151 | | } |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public override void WriteBytes(ReadOnlySpan<byte> data) |
| | | 155 | | { |
| | | 156 | | _bytesWritten += data.Length; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// 仅计算校验和的状态写入器,不保留实际缓冲区。 |
| | | 162 | | /// </summary> |
| | | 163 | | internal sealed unsafe class FrameStateChecksumWriter : FrameStateWriter |
| | | 164 | | { |
| | | 165 | | private const ulong FnvPrime = 0x00000100000001B3; |
| | | 166 | | private const ulong FnvOffset = 0xCBF29CE484222325; |
| | | 167 | | |
| | 84 | 168 | | private ulong _hash = FnvOffset; |
| | | 169 | | private int _bytesWritten; |
| | | 170 | | |
| | 84 | 171 | | public ulong Checksum => _hash; |
| | | 172 | | |
| | 0 | 173 | | public override int BytesWritten => _bytesWritten; |
| | | 174 | | |
| | | 175 | | public override void WriteByte(byte value) |
| | | 176 | | { |
| | 626 | 177 | | _hash ^= value; |
| | 626 | 178 | | _hash *= FnvPrime; |
| | 626 | 179 | | _bytesWritten += sizeof(byte); |
| | 626 | 180 | | } |
| | | 181 | | |
| | | 182 | | public override void WriteBytes(void* data, int length) |
| | | 183 | | { |
| | 1324 | 184 | | if (length <= 0) |
| | | 185 | | { |
| | 0 | 186 | | return; |
| | | 187 | | } |
| | | 188 | | |
| | 1324 | 189 | | ulong hash = _hash; |
| | 1324 | 190 | | byte* cursor = (byte*)data; |
| | 214602 | 191 | | for (int i = 0; i < length; i++) |
| | | 192 | | { |
| | 105977 | 193 | | hash ^= cursor[i]; |
| | 105977 | 194 | | hash *= FnvPrime; |
| | | 195 | | } |
| | | 196 | | |
| | 1324 | 197 | | _hash = hash; |
| | 1324 | 198 | | _bytesWritten += length; |
| | 1324 | 199 | | } |
| | | 200 | | |
| | | 201 | | public override void WriteBytes(ReadOnlySpan<byte> data) |
| | | 202 | | { |
| | 4618 | 203 | | if (data.IsEmpty) |
| | | 204 | | { |
| | 0 | 205 | | return; |
| | | 206 | | } |
| | | 207 | | |
| | 4618 | 208 | | ulong hash = _hash; |
| | 43464 | 209 | | for (int i = 0; i < data.Length; i++) |
| | | 210 | | { |
| | 17114 | 211 | | hash ^= data[i]; |
| | 17114 | 212 | | hash *= FnvPrime; |
| | | 213 | | } |
| | | 214 | | |
| | 4618 | 215 | | _hash = hash; |
| | 4618 | 216 | | _bytesWritten += data.Length; |
| | 4618 | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// 帧状态读取器。 |
| | | 222 | | /// </summary> |
| | | 223 | | internal sealed class FrameStateReader |
| | | 224 | | { |
| | | 225 | | private readonly byte[] _buffer; |
| | | 226 | | private readonly int _end; |
| | | 227 | | private int _position; |
| | | 228 | | |
| | | 229 | | public FrameStateReader(byte[] buffer, int length) |
| | | 230 | | { |
| | | 231 | | _buffer = buffer ?? throw new ArgumentNullException(nameof(buffer)); |
| | | 232 | | _position = 0; |
| | | 233 | | _end = System.Math.Min(buffer.Length, length); |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public int Position => _position; |
| | | 237 | | |
| | | 238 | | public byte ReadByte() |
| | | 239 | | { |
| | | 240 | | EnsureCanRead(sizeof(byte)); |
| | | 241 | | return _buffer[_position++]; |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | public int ReadInt32() |
| | | 245 | | { |
| | | 246 | | EnsureCanRead(sizeof(int)); |
| | | 247 | | int value = BinaryPrimitives.ReadInt32LittleEndian(_buffer.AsSpan(_position, sizeof(int))); |
| | | 248 | | _position += sizeof(int); |
| | | 249 | | return value; |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | public long ReadInt64() |
| | | 253 | | { |
| | | 254 | | EnsureCanRead(sizeof(long)); |
| | | 255 | | long value = BinaryPrimitives.ReadInt64LittleEndian(_buffer.AsSpan(_position, sizeof(long))); |
| | | 256 | | _position += sizeof(long); |
| | | 257 | | return value; |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | public ushort ReadUInt16() |
| | | 261 | | { |
| | | 262 | | EnsureCanRead(sizeof(ushort)); |
| | | 263 | | ushort value = BinaryPrimitives.ReadUInt16LittleEndian(_buffer.AsSpan(_position, sizeof(ushort))); |
| | | 264 | | _position += sizeof(ushort); |
| | | 265 | | return value; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | public ulong ReadUInt64() |
| | | 269 | | { |
| | | 270 | | EnsureCanRead(sizeof(ulong)); |
| | | 271 | | ulong value = BinaryPrimitives.ReadUInt64LittleEndian(_buffer.AsSpan(_position, sizeof(ulong))); |
| | | 272 | | _position += sizeof(ulong); |
| | | 273 | | return value; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | public ReadOnlySpan<byte> ReadBytes(int length) |
| | | 277 | | { |
| | | 278 | | EnsureCanRead(length); |
| | | 279 | | ReadOnlySpan<byte> slice = new ReadOnlySpan<byte>(_buffer, _position, length); |
| | | 280 | | _position += length; |
| | | 281 | | return slice; |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | public unsafe void ReadBytes(void* destination, int length) |
| | | 285 | | { |
| | | 286 | | EnsureCanRead(length); |
| | | 287 | | fixed (byte* source = &_buffer[_position]) |
| | | 288 | | { |
| | | 289 | | Buffer.MemoryCopy(source, destination, length, length); |
| | | 290 | | } |
| | | 291 | | |
| | | 292 | | _position += length; |
| | | 293 | | } |
| | | 294 | | |
| | | 295 | | public BitStream CreateBitStreamSlice(int length) |
| | | 296 | | { |
| | | 297 | | EnsureCanRead(length); |
| | | 298 | | var stream = new BitStream(_buffer, _position, length); |
| | | 299 | | _position += length; |
| | | 300 | | return stream; |
| | | 301 | | } |
| | | 302 | | |
| | | 303 | | private void EnsureCanRead(int length) |
| | | 304 | | { |
| | | 305 | | if (_position + length > _end) |
| | | 306 | | { |
| | | 307 | | throw new InvalidOperationException("Packed frame snapshot payload is truncated."); |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | /// <summary> |
| | | 313 | | /// 连续字节表示的帧快照。 |
| | | 314 | | /// 主要供 Session 的 checkpoint、采样历史和其他运行时热路径使用。 |
| | | 315 | | /// </summary> |
| | | 316 | | public sealed class PackedFrameSnapshot |
| | | 317 | | { |
| | | 318 | | public const int CurrentFormatVersion = 1; |
| | | 319 | | |
| | | 320 | | public PackedFrameSnapshot( |
| | | 321 | | int tick, |
| | | 322 | | int entityCapacity, |
| | | 323 | | byte[] data, |
| | | 324 | | int length, |
| | | 325 | | int formatVersion, |
| | | 326 | | ComponentSchemaManifest schemaManifest) |
| | | 327 | | { |
| | | 328 | | Tick = tick; |
| | | 329 | | EntityCapacity = entityCapacity; |
| | | 330 | | Data = data ?? Array.Empty<byte>(); |
| | | 331 | | Length = length; |
| | | 332 | | FormatVersion = formatVersion; |
| | | 333 | | SchemaManifest = schemaManifest; |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | public int Tick { get; } |
| | | 337 | | |
| | | 338 | | public int EntityCapacity { get; } |
| | | 339 | | |
| | | 340 | | public byte[] Data { get; } |
| | | 341 | | |
| | | 342 | | public int Length { get; } |
| | | 343 | | |
| | | 344 | | public int FormatVersion { get; } |
| | | 345 | | |
| | | 346 | | public ComponentSchemaManifest SchemaManifest { get; } |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | /// <summary> |
| | | 350 | | /// 直接把位级序列化结果写入状态写入器,避免额外的 BitStream 中转缓冲区。 |
| | | 351 | | /// </summary> |
| | | 352 | | internal sealed unsafe class FrameStateBitStreamWriter : IBitStream |
| | | 353 | | { |
| | | 354 | | private readonly FrameStateWriter _writer; |
| | | 355 | | private byte _pendingByte; |
| | | 356 | | private int _bitPosition; |
| | | 357 | | private int _payloadBytesWritten; |
| | | 358 | | |
| | | 359 | | public FrameStateBitStreamWriter(FrameStateWriter writer) |
| | | 360 | | { |
| | | 361 | | _writer = writer ?? throw new ArgumentNullException(nameof(writer)); |
| | | 362 | | Writing = true; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | public bool Writing { get; set; } |
| | | 366 | | |
| | | 367 | | public bool Reading => !Writing; |
| | | 368 | | |
| | | 369 | | internal void BeginPayload() |
| | | 370 | | { |
| | | 371 | | if (!Writing) |
| | | 372 | | { |
| | | 373 | | throw new InvalidOperationException("Cannot begin payload while reading."); |
| | | 374 | | } |
| | | 375 | | |
| | | 376 | | if (_bitPosition != 0) |
| | | 377 | | { |
| | | 378 | | throw new InvalidOperationException("Previous payload was not finalized before starting the next payload |
| | | 379 | | } |
| | | 380 | | |
| | | 381 | | _pendingByte = 0; |
| | | 382 | | _payloadBytesWritten = 0; |
| | | 383 | | } |
| | | 384 | | |
| | | 385 | | internal int EndPayload() |
| | | 386 | | { |
| | | 387 | | FlushPendingBits(); |
| | | 388 | | int written = _payloadBytesWritten; |
| | | 389 | | _payloadBytesWritten = 0; |
| | | 390 | | return written; |
| | | 391 | | } |
| | | 392 | | |
| | | 393 | | public void Serialize(ref long value) |
| | | 394 | | { |
| | | 395 | | if (Writing) |
| | | 396 | | { |
| | | 397 | | if (TryWriteAlignedInt64(value)) |
| | | 398 | | { |
| | | 399 | | return; |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | WriteBits((uint)value, 32); |
| | | 403 | | WriteBits((uint)(value >> 32), 32); |
| | | 404 | | return; |
| | | 405 | | } |
| | | 406 | | |
| | | 407 | | value = 0; |
| | | 408 | | } |
| | | 409 | | |
| | | 410 | | public void Serialize(ref ulong value) |
| | | 411 | | { |
| | | 412 | | if (Writing) |
| | | 413 | | { |
| | | 414 | | if (TryWriteAlignedUInt64(value)) |
| | | 415 | | { |
| | | 416 | | return; |
| | | 417 | | } |
| | | 418 | | |
| | | 419 | | WriteBits((uint)value, 32); |
| | | 420 | | WriteBits((uint)(value >> 32), 32); |
| | | 421 | | return; |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | value = 0; |
| | | 425 | | } |
| | | 426 | | |
| | | 427 | | public void Serialize(ref uint value) |
| | | 428 | | { |
| | | 429 | | if (Writing) |
| | | 430 | | { |
| | | 431 | | if (TryWriteAlignedUInt32(value)) |
| | | 432 | | { |
| | | 433 | | return; |
| | | 434 | | } |
| | | 435 | | |
| | | 436 | | WriteBits(value, 32); |
| | | 437 | | return; |
| | | 438 | | } |
| | | 439 | | |
| | | 440 | | value = 0; |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | public void Serialize(ref int value) |
| | | 444 | | { |
| | | 445 | | if (Writing) |
| | | 446 | | { |
| | | 447 | | if (TryWriteAlignedInt32(value)) |
| | | 448 | | { |
| | | 449 | | return; |
| | | 450 | | } |
| | | 451 | | |
| | | 452 | | WriteBits((uint)value, 32); |
| | | 453 | | return; |
| | | 454 | | } |
| | | 455 | | |
| | | 456 | | value = 0; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | public void Serialize(ref short value) |
| | | 460 | | { |
| | | 461 | | if (Writing) |
| | | 462 | | { |
| | | 463 | | if (TryWriteAlignedInt16(value)) |
| | | 464 | | { |
| | | 465 | | return; |
| | | 466 | | } |
| | | 467 | | |
| | | 468 | | WriteBits((uint)(ushort)value, 16); |
| | | 469 | | return; |
| | | 470 | | } |
| | | 471 | | |
| | | 472 | | value = 0; |
| | | 473 | | } |
| | | 474 | | |
| | | 475 | | public void Serialize(ref ushort value) |
| | | 476 | | { |
| | | 477 | | if (Writing) |
| | | 478 | | { |
| | | 479 | | if (TryWriteAlignedUInt16(value)) |
| | | 480 | | { |
| | | 481 | | return; |
| | | 482 | | } |
| | | 483 | | |
| | | 484 | | WriteBits(value, 16); |
| | | 485 | | return; |
| | | 486 | | } |
| | | 487 | | |
| | | 488 | | value = 0; |
| | | 489 | | } |
| | | 490 | | |
| | | 491 | | public void Serialize(ref byte value) |
| | | 492 | | { |
| | | 493 | | if (Writing) |
| | | 494 | | { |
| | | 495 | | if (_bitPosition == 0) |
| | | 496 | | { |
| | | 497 | | _writer.WriteByte(value); |
| | | 498 | | _payloadBytesWritten += sizeof(byte); |
| | | 499 | | return; |
| | | 500 | | } |
| | | 501 | | |
| | | 502 | | WriteBits(value, 8); |
| | | 503 | | return; |
| | | 504 | | } |
| | | 505 | | |
| | | 506 | | value = 0; |
| | | 507 | | } |
| | | 508 | | |
| | | 509 | | public void Serialize(ref bool value) |
| | | 510 | | { |
| | | 511 | | if (Writing) |
| | | 512 | | { |
| | | 513 | | WriteBit(value); |
| | | 514 | | return; |
| | | 515 | | } |
| | | 516 | | |
| | | 517 | | value = false; |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | public void Serialize(void* data, int size) |
| | | 521 | | { |
| | | 522 | | if (size < 0) |
| | | 523 | | { |
| | | 524 | | throw new ArgumentOutOfRangeException(nameof(size)); |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | AlignToByteBoundary(); |
| | | 528 | | _writer.WriteBytes(data, size); |
| | | 529 | | _payloadBytesWritten += size; |
| | | 530 | | } |
| | | 531 | | |
| | | 532 | | public byte[] ToArray() |
| | | 533 | | { |
| | | 534 | | throw new NotSupportedException("Direct frame-state bit stream does not expose an intermediate byte array.") |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | public void Reset() |
| | | 538 | | { |
| | | 539 | | throw new NotSupportedException("Direct frame-state bit stream cannot reset previously written payload bytes |
| | | 540 | | } |
| | | 541 | | |
| | | 542 | | private void WriteBit(bool value) |
| | | 543 | | { |
| | | 544 | | if (!Writing) |
| | | 545 | | { |
| | | 546 | | throw new InvalidOperationException("Cannot write in reading mode."); |
| | | 547 | | } |
| | | 548 | | |
| | | 549 | | if (value) |
| | | 550 | | { |
| | | 551 | | _pendingByte |= (byte)(1 << _bitPosition); |
| | | 552 | | } |
| | | 553 | | |
| | | 554 | | _bitPosition++; |
| | | 555 | | if (_bitPosition >= 8) |
| | | 556 | | { |
| | | 557 | | FlushPendingBits(); |
| | | 558 | | } |
| | | 559 | | } |
| | | 560 | | |
| | | 561 | | private void WriteBits(uint value, int bits) |
| | | 562 | | { |
| | | 563 | | for (int i = 0; i < bits; i++) |
| | | 564 | | { |
| | | 565 | | WriteBit(((value >> i) & 1U) != 0); |
| | | 566 | | } |
| | | 567 | | } |
| | | 568 | | |
| | | 569 | | private bool TryWriteAlignedInt16(short value) |
| | | 570 | | { |
| | | 571 | | if (_bitPosition != 0) |
| | | 572 | | { |
| | | 573 | | return false; |
| | | 574 | | } |
| | | 575 | | |
| | | 576 | | Span<byte> buffer = stackalloc byte[sizeof(short)]; |
| | | 577 | | BinaryPrimitives.WriteInt16LittleEndian(buffer, value); |
| | | 578 | | _writer.WriteBytes(buffer); |
| | | 579 | | _payloadBytesWritten += buffer.Length; |
| | | 580 | | return true; |
| | | 581 | | } |
| | | 582 | | |
| | | 583 | | private bool TryWriteAlignedUInt16(ushort value) |
| | | 584 | | { |
| | | 585 | | if (_bitPosition != 0) |
| | | 586 | | { |
| | | 587 | | return false; |
| | | 588 | | } |
| | | 589 | | |
| | | 590 | | Span<byte> buffer = stackalloc byte[sizeof(ushort)]; |
| | | 591 | | BinaryPrimitives.WriteUInt16LittleEndian(buffer, value); |
| | | 592 | | _writer.WriteBytes(buffer); |
| | | 593 | | _payloadBytesWritten += buffer.Length; |
| | | 594 | | return true; |
| | | 595 | | } |
| | | 596 | | |
| | | 597 | | private bool TryWriteAlignedInt32(int value) |
| | | 598 | | { |
| | | 599 | | if (_bitPosition != 0) |
| | | 600 | | { |
| | | 601 | | return false; |
| | | 602 | | } |
| | | 603 | | |
| | | 604 | | Span<byte> buffer = stackalloc byte[sizeof(int)]; |
| | | 605 | | BinaryPrimitives.WriteInt32LittleEndian(buffer, value); |
| | | 606 | | _writer.WriteBytes(buffer); |
| | | 607 | | _payloadBytesWritten += buffer.Length; |
| | | 608 | | return true; |
| | | 609 | | } |
| | | 610 | | |
| | | 611 | | private bool TryWriteAlignedUInt32(uint value) |
| | | 612 | | { |
| | | 613 | | if (_bitPosition != 0) |
| | | 614 | | { |
| | | 615 | | return false; |
| | | 616 | | } |
| | | 617 | | |
| | | 618 | | Span<byte> buffer = stackalloc byte[sizeof(uint)]; |
| | | 619 | | BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); |
| | | 620 | | _writer.WriteBytes(buffer); |
| | | 621 | | _payloadBytesWritten += buffer.Length; |
| | | 622 | | return true; |
| | | 623 | | } |
| | | 624 | | |
| | | 625 | | private bool TryWriteAlignedInt64(long value) |
| | | 626 | | { |
| | | 627 | | if (_bitPosition != 0) |
| | | 628 | | { |
| | | 629 | | return false; |
| | | 630 | | } |
| | | 631 | | |
| | | 632 | | Span<byte> buffer = stackalloc byte[sizeof(long)]; |
| | | 633 | | BinaryPrimitives.WriteInt64LittleEndian(buffer, value); |
| | | 634 | | _writer.WriteBytes(buffer); |
| | | 635 | | _payloadBytesWritten += buffer.Length; |
| | | 636 | | return true; |
| | | 637 | | } |
| | | 638 | | |
| | | 639 | | private bool TryWriteAlignedUInt64(ulong value) |
| | | 640 | | { |
| | | 641 | | if (_bitPosition != 0) |
| | | 642 | | { |
| | | 643 | | return false; |
| | | 644 | | } |
| | | 645 | | |
| | | 646 | | Span<byte> buffer = stackalloc byte[sizeof(ulong)]; |
| | | 647 | | BinaryPrimitives.WriteUInt64LittleEndian(buffer, value); |
| | | 648 | | _writer.WriteBytes(buffer); |
| | | 649 | | _payloadBytesWritten += buffer.Length; |
| | | 650 | | return true; |
| | | 651 | | } |
| | | 652 | | |
| | | 653 | | private void FlushPendingBits() |
| | | 654 | | { |
| | | 655 | | if (_bitPosition == 0) |
| | | 656 | | { |
| | | 657 | | return; |
| | | 658 | | } |
| | | 659 | | |
| | | 660 | | _writer.WriteByte(_pendingByte); |
| | | 661 | | _pendingByte = 0; |
| | | 662 | | _bitPosition = 0; |
| | | 663 | | _payloadBytesWritten += sizeof(byte); |
| | | 664 | | } |
| | | 665 | | |
| | | 666 | | private void AlignToByteBoundary() |
| | | 667 | | { |
| | | 668 | | if (_bitPosition != 0) |
| | | 669 | | { |
| | | 670 | | FlushPendingBits(); |
| | | 671 | | } |
| | | 672 | | } |
| | | 673 | | } |
| | | 674 | | |
| | | 675 | | /// <summary> |
| | | 676 | | /// 直接从状态读取器消费固定位长 payload,避免每个条目构造一个 BitStream 实例。 |
| | | 677 | | /// </summary> |
| | | 678 | | internal sealed unsafe class FrameStateBitStreamReader : IBitStream |
| | | 679 | | { |
| | | 680 | | private readonly FrameStateReader _reader; |
| | | 681 | | private byte _currentByte; |
| | | 682 | | private int _bitPosition; |
| | | 683 | | private int _remainingBytes; |
| | | 684 | | |
| | | 685 | | public FrameStateBitStreamReader(FrameStateReader reader) |
| | | 686 | | { |
| | | 687 | | _reader = reader ?? throw new ArgumentNullException(nameof(reader)); |
| | | 688 | | Writing = false; |
| | | 689 | | } |
| | | 690 | | |
| | | 691 | | public bool Writing { get; set; } |
| | | 692 | | |
| | | 693 | | public bool Reading => !Writing; |
| | | 694 | | |
| | | 695 | | internal void BeginPayload(int length) |
| | | 696 | | { |
| | | 697 | | if (length < 0) |
| | | 698 | | { |
| | | 699 | | throw new ArgumentOutOfRangeException(nameof(length)); |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | if (Writing) |
| | | 703 | | { |
| | | 704 | | throw new InvalidOperationException("Cannot begin payload while writing."); |
| | | 705 | | } |
| | | 706 | | |
| | | 707 | | _currentByte = 0; |
| | | 708 | | _bitPosition = 0; |
| | | 709 | | _remainingBytes = length; |
| | | 710 | | } |
| | | 711 | | |
| | | 712 | | internal void EndPayload() |
| | | 713 | | { |
| | | 714 | | if (_remainingBytes != 0) |
| | | 715 | | { |
| | | 716 | | throw new InvalidOperationException("Serialized payload was not fully consumed during restore."); |
| | | 717 | | } |
| | | 718 | | |
| | | 719 | | _currentByte = 0; |
| | | 720 | | _bitPosition = 0; |
| | | 721 | | } |
| | | 722 | | |
| | | 723 | | public void Serialize(ref long value) |
| | | 724 | | { |
| | | 725 | | if (Writing) |
| | | 726 | | { |
| | | 727 | | return; |
| | | 728 | | } |
| | | 729 | | |
| | | 730 | | if (_bitPosition == 0) |
| | | 731 | | { |
| | | 732 | | EnsureRemaining(sizeof(long)); |
| | | 733 | | value = _reader.ReadInt64(); |
| | | 734 | | _remainingBytes -= sizeof(long); |
| | | 735 | | return; |
| | | 736 | | } |
| | | 737 | | |
| | | 738 | | long low = ReadBits(32); |
| | | 739 | | long high = ReadBits(32); |
| | | 740 | | value = low | (high << 32); |
| | | 741 | | } |
| | | 742 | | |
| | | 743 | | public void Serialize(ref ulong value) |
| | | 744 | | { |
| | | 745 | | if (Writing) |
| | | 746 | | { |
| | | 747 | | return; |
| | | 748 | | } |
| | | 749 | | |
| | | 750 | | if (_bitPosition == 0) |
| | | 751 | | { |
| | | 752 | | EnsureRemaining(sizeof(ulong)); |
| | | 753 | | value = _reader.ReadUInt64(); |
| | | 754 | | _remainingBytes -= sizeof(ulong); |
| | | 755 | | return; |
| | | 756 | | } |
| | | 757 | | |
| | | 758 | | ulong low = ReadBits(32); |
| | | 759 | | ulong high = ReadBits(32); |
| | | 760 | | value = low | (high << 32); |
| | | 761 | | } |
| | | 762 | | |
| | | 763 | | public void Serialize(ref uint value) |
| | | 764 | | { |
| | | 765 | | if (Writing) |
| | | 766 | | { |
| | | 767 | | return; |
| | | 768 | | } |
| | | 769 | | |
| | | 770 | | if (_bitPosition == 0) |
| | | 771 | | { |
| | | 772 | | EnsureRemaining(sizeof(uint)); |
| | | 773 | | value = unchecked((uint)_reader.ReadInt32()); |
| | | 774 | | _remainingBytes -= sizeof(uint); |
| | | 775 | | return; |
| | | 776 | | } |
| | | 777 | | |
| | | 778 | | value = ReadBits(32); |
| | | 779 | | } |
| | | 780 | | |
| | | 781 | | public void Serialize(ref int value) |
| | | 782 | | { |
| | | 783 | | if (Writing) |
| | | 784 | | { |
| | | 785 | | return; |
| | | 786 | | } |
| | | 787 | | |
| | | 788 | | if (_bitPosition == 0) |
| | | 789 | | { |
| | | 790 | | EnsureRemaining(sizeof(int)); |
| | | 791 | | value = _reader.ReadInt32(); |
| | | 792 | | _remainingBytes -= sizeof(int); |
| | | 793 | | return; |
| | | 794 | | } |
| | | 795 | | |
| | | 796 | | value = (int)ReadBits(32); |
| | | 797 | | } |
| | | 798 | | |
| | | 799 | | public void Serialize(ref short value) |
| | | 800 | | { |
| | | 801 | | if (Writing) |
| | | 802 | | { |
| | | 803 | | return; |
| | | 804 | | } |
| | | 805 | | |
| | | 806 | | if (_bitPosition == 0) |
| | | 807 | | { |
| | | 808 | | EnsureRemaining(sizeof(short)); |
| | | 809 | | value = unchecked((short)_reader.ReadUInt16()); |
| | | 810 | | _remainingBytes -= sizeof(short); |
| | | 811 | | return; |
| | | 812 | | } |
| | | 813 | | |
| | | 814 | | value = (short)ReadBits(16); |
| | | 815 | | } |
| | | 816 | | |
| | | 817 | | public void Serialize(ref ushort value) |
| | | 818 | | { |
| | | 819 | | if (Writing) |
| | | 820 | | { |
| | | 821 | | return; |
| | | 822 | | } |
| | | 823 | | |
| | | 824 | | if (_bitPosition == 0) |
| | | 825 | | { |
| | | 826 | | EnsureRemaining(sizeof(ushort)); |
| | | 827 | | value = _reader.ReadUInt16(); |
| | | 828 | | _remainingBytes -= sizeof(ushort); |
| | | 829 | | return; |
| | | 830 | | } |
| | | 831 | | |
| | | 832 | | value = (ushort)ReadBits(16); |
| | | 833 | | } |
| | | 834 | | |
| | | 835 | | public void Serialize(ref byte value) |
| | | 836 | | { |
| | | 837 | | if (Writing) |
| | | 838 | | { |
| | | 839 | | return; |
| | | 840 | | } |
| | | 841 | | |
| | | 842 | | if (_bitPosition == 0) |
| | | 843 | | { |
| | | 844 | | EnsureRemaining(sizeof(byte)); |
| | | 845 | | value = _reader.ReadByte(); |
| | | 846 | | _remainingBytes -= sizeof(byte); |
| | | 847 | | return; |
| | | 848 | | } |
| | | 849 | | |
| | | 850 | | value = (byte)ReadBits(8); |
| | | 851 | | } |
| | | 852 | | |
| | | 853 | | public void Serialize(ref bool value) |
| | | 854 | | { |
| | | 855 | | if (Writing) |
| | | 856 | | { |
| | | 857 | | return; |
| | | 858 | | } |
| | | 859 | | |
| | | 860 | | value = ReadBit(); |
| | | 861 | | } |
| | | 862 | | |
| | | 863 | | public void Serialize(void* data, int size) |
| | | 864 | | { |
| | | 865 | | if (size < 0) |
| | | 866 | | { |
| | | 867 | | throw new ArgumentOutOfRangeException(nameof(size)); |
| | | 868 | | } |
| | | 869 | | |
| | | 870 | | AlignToByteBoundary(); |
| | | 871 | | EnsureRemaining(size); |
| | | 872 | | _reader.ReadBytes(data, size); |
| | | 873 | | _remainingBytes -= size; |
| | | 874 | | } |
| | | 875 | | |
| | | 876 | | public byte[] ToArray() |
| | | 877 | | { |
| | | 878 | | throw new NotSupportedException("Direct frame-state bit stream reader does not materialize intermediate byte |
| | | 879 | | } |
| | | 880 | | |
| | | 881 | | public void Reset() |
| | | 882 | | { |
| | | 883 | | throw new NotSupportedException("Direct frame-state bit stream reader cannot rewind an already-consumed payl |
| | | 884 | | } |
| | | 885 | | |
| | | 886 | | private bool ReadBit() |
| | | 887 | | { |
| | | 888 | | if (Writing) |
| | | 889 | | { |
| | | 890 | | throw new InvalidOperationException("Cannot read in writing mode."); |
| | | 891 | | } |
| | | 892 | | |
| | | 893 | | if (_bitPosition == 0) |
| | | 894 | | { |
| | | 895 | | EnsureRemaining(sizeof(byte)); |
| | | 896 | | _currentByte = _reader.ReadByte(); |
| | | 897 | | _remainingBytes--; |
| | | 898 | | } |
| | | 899 | | |
| | | 900 | | bool value = (_currentByte & (1 << _bitPosition)) != 0; |
| | | 901 | | _bitPosition++; |
| | | 902 | | if (_bitPosition >= 8) |
| | | 903 | | { |
| | | 904 | | _bitPosition = 0; |
| | | 905 | | } |
| | | 906 | | |
| | | 907 | | return value; |
| | | 908 | | } |
| | | 909 | | |
| | | 910 | | private uint ReadBits(int bits) |
| | | 911 | | { |
| | | 912 | | uint value = 0; |
| | | 913 | | for (int i = 0; i < bits; i++) |
| | | 914 | | { |
| | | 915 | | if (ReadBit()) |
| | | 916 | | { |
| | | 917 | | value |= 1U << i; |
| | | 918 | | } |
| | | 919 | | } |
| | | 920 | | |
| | | 921 | | return value; |
| | | 922 | | } |
| | | 923 | | |
| | | 924 | | private void AlignToByteBoundary() |
| | | 925 | | { |
| | | 926 | | if (_bitPosition != 0) |
| | | 927 | | { |
| | | 928 | | _bitPosition = 0; |
| | | 929 | | } |
| | | 930 | | } |
| | | 931 | | |
| | | 932 | | private void EnsureRemaining(int length) |
| | | 933 | | { |
| | | 934 | | if (length > _remainingBytes) |
| | | 935 | | { |
| | | 936 | | throw new InvalidOperationException("Packed frame snapshot payload is truncated."); |
| | | 937 | | } |
| | | 938 | | } |
| | | 939 | | } |
| | | 940 | | } |