@series-inc/rundot-syncplay 6.0.0-rc.91 → 6.0.0-rc.93

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -702,7 +702,10 @@ gives the APIs, ownership, and limits. The render adapter supplies dirty-chunk
702
702
  projection and packed attribute data. The game connects those outputs to its
703
703
  renderer.
704
704
 
705
- Session snapshots use binary transfer, LZ4-style compression, seed-baseline
706
- page deltas, and optional pinned asynchronous donation. Read
705
+ Session snapshots use binary transfer, LZ4-style compression, and optional
706
+ pinned asynchronous donation. Installed voxel runtimes can use native deltas
707
+ against retained tick-0 checkpoints. Unchanged chunks use no record. A
708
+ Teardown chunk with only solid-to-air changes can use one bit per changed
709
+ brick cell. Read
707
710
  [binary snapshots](../sdk-docs/rundot-developer-platform/api/SYNCPLAY.md#binary-snapshots-and-baseline-deltas)
708
711
  before you change the transport or the size limits.
@@ -264,18 +264,18 @@ std::int32_t rundot_kinetix_cellular_checkpoint_merkle_root(
264
264
 
265
265
  std::int32_t rundot_kinetix_cellular_serialize_checkpoint_bound(
266
266
  std::uint32_t handle,
267
- std::int32_t checkpointToken) noexcept;
267
+ std::int32_t checkpointToken, std::int32_t baselineToken = 0) noexcept;
268
268
 
269
269
  std::int32_t rundot_kinetix_cellular_serialize_checkpoint(
270
270
  std::uint32_t handle,
271
271
  std::int32_t checkpointToken,
272
272
  std::uint8_t* outBytes,
273
- std::int32_t maxBytes) noexcept;
273
+ std::int32_t maxBytes, std::int32_t baselineToken = 0) noexcept;
274
274
 
275
275
  std::int32_t rundot_kinetix_cellular_import_checkpoint(
276
276
  std::uint32_t handle,
277
277
  const std::uint8_t* bytes,
278
- std::int32_t byteLength) noexcept;
278
+ std::int32_t byteLength, std::int32_t baselineToken = 0) noexcept;
279
279
 
280
280
  std::int32_t rundot_kinetix_cellular_clear_dirty_collider_chunk(
281
281
  std::uint32_t handle,
@@ -2,11 +2,35 @@ namespace rundot::kinetix::cellular::detail {
2
2
 
3
3
  namespace {
4
4
 
5
+ std::uint64_t loadCellBytes(const std::uint8_t* bytes) noexcept {
6
+ std::uint64_t word;
7
+ std::memcpy(&word, bytes, sizeof(word));
8
+ return word;
9
+ }
10
+
11
+ std::int32_t endOfCellRun(const ChunkPage& page, std::int32_t start,
12
+ std::uint8_t material, std::uint8_t meta) noexcept {
13
+ constexpr std::uint64_t kRepeatByte = 0x0101010101010101ULL;
14
+ const std::uint64_t materials = kRepeatByte * material;
15
+ const std::uint64_t metas = kRepeatByte * meta;
16
+ std::int32_t end = start + 1;
17
+ while (end + 8 <= kCellularChunkCells &&
18
+ loadCellBytes(page.materials + end) == materials &&
19
+ loadCellBytes(page.meta + end) == metas) {
20
+ end += 8;
21
+ }
22
+ while (end < kCellularChunkCells && page.materials[end] == material && page.meta[end] == meta) {
23
+ ++end;
24
+ }
25
+ return end;
26
+ }
27
+
5
28
  bool isPageAllZero(const ChunkPage* page) noexcept {
6
29
  if (page == nullptr) return true;
7
30
  if (page->brickNonEmpty != 0ULL) return false;
8
- for (std::int32_t i = 0; i < kCellularChunkCells; ++i) {
9
- if (page->meta[i] != 0u) return false;
31
+ static_assert(kCellularChunkCells % 8 == 0);
32
+ for (std::int32_t i = 0; i < kCellularChunkCells; i += 8) {
33
+ if (loadCellBytes(page->meta + i) != 0u) return false;
10
34
  }
11
35
  return true;
12
36
  }
@@ -26,10 +50,7 @@ std::int32_t measureChunkRleBytes(const ChunkPage* page) noexcept {
26
50
  while (i < kCellularChunkCells) {
27
51
  const std::uint8_t mat = page->materials[i];
28
52
  const std::uint8_t met = page->meta[i];
29
- std::int32_t j = i + 1;
30
- while (j < kCellularChunkCells && page->materials[j] == mat && page->meta[j] == met) {
31
- ++j;
32
- }
53
+ const std::int32_t j = endOfCellRun(*page, i, mat, met);
33
54
  runs += 1;
34
55
  i = j;
35
56
  }
@@ -47,10 +68,7 @@ std::int32_t writeChunkRleBytes(const ChunkPage* page, std::uint8_t* dst) noexce
47
68
  while (i < kCellularChunkCells) {
48
69
  const std::uint8_t mat = page->materials[i];
49
70
  const std::uint8_t met = page->meta[i];
50
- std::int32_t j = i + 1;
51
- while (j < kCellularChunkCells && page->materials[j] == mat && page->meta[j] == met) {
52
- ++j;
53
- }
71
+ const std::int32_t j = endOfCellRun(*page, i, mat, met);
54
72
  storeLeU16(dst + pos, static_cast<std::uint16_t>(j - i));
55
73
  dst[pos + 2] = mat;
56
74
  dst[pos + 3] = met;
@@ -2,15 +2,23 @@ namespace rundot::kinetix::cellular::detail {
2
2
 
3
3
  namespace {
4
4
 
5
+ bool storesChunk(ChunkPage* const* pages, const std::uint64_t* activeBricks,
6
+ const CheckpointSlot* baseline, std::int32_t chunk) noexcept {
7
+ if (baseline == nullptr) return !isPageAllZero(pages[chunk]) || activeBricks[chunk] != 0ULL;
8
+ return activeBricks[chunk] != baseline->activeBricks[chunk] ||
9
+ !pagesEqualContent(pages[chunk], baseline->pages[chunk]);
10
+ }
11
+
5
12
  std::int32_t serializeStateBoundInternal(
6
13
  std::int32_t numChunks,
7
14
  std::int32_t ruleCount,
8
15
  ChunkPage* const* pages,
9
- const std::uint64_t* activeBricks) noexcept {
16
+ const std::uint64_t* activeBricks,
17
+ const CheckpointSlot* baseline = nullptr) noexcept {
10
18
  std::int64_t total = 56 + static_cast<std::int64_t>(kCellularMaxMaterials) * 16 +
11
19
  static_cast<std::int64_t>(ruleCount) * 12;
12
20
  for (std::int32_t c = 0; c < numChunks; ++c) {
13
- if (!isPageAllZero(pages[c]) || activeBricks[c] != 0ULL) {
21
+ if (storesChunk(pages, activeBricks, baseline, c)) {
14
22
  total += 12 + measureChunkRleBytes(pages[c]);
15
23
  }
16
24
  }
@@ -30,14 +38,15 @@ std::int32_t serializeStateBytesInternal(
30
38
  ChunkPage* const* pages,
31
39
  const std::uint64_t* activeBricks,
32
40
  std::uint8_t* outBytes,
33
- std::int32_t maxBytes) noexcept {
34
- const std::int32_t needed = serializeStateBoundInternal(f.numChunks, ruleCount, pages, activeBricks);
41
+ std::int32_t maxBytes,
42
+ const CheckpointSlot* baseline = nullptr) noexcept {
43
+ const std::int32_t needed = serializeStateBoundInternal(f.numChunks, ruleCount, pages, activeBricks, baseline);
35
44
  if (needed < 0) return needed;
36
45
  if (outBytes == nullptr || maxBytes < needed) return kStatusBufferTooSmall;
37
46
 
38
47
  storeLeU32(outBytes + 0, kSnapshotMagic);
39
- outBytes[4] = static_cast<std::uint8_t>(kCodecVersion);
40
- outBytes[5] = kCodecKindFull;
48
+ outBytes[4] = baseline == nullptr ? static_cast<std::uint8_t>(kCodecVersion) : 2u;
49
+ outBytes[5] = baseline == nullptr ? kCodecKindFull : kCodecKindDelta;
41
50
  storeLeU16(outBytes + 6, static_cast<std::uint16_t>(ruleCount));
42
51
  storeLeU32(outBytes + 8, static_cast<std::uint32_t>(f.chunksX));
43
52
  storeLeU32(outBytes + 12, static_cast<std::uint32_t>(f.chunksY));
@@ -76,7 +85,7 @@ std::int32_t serializeStateBytesInternal(
76
85
 
77
86
  std::uint32_t storedChunks = 0u;
78
87
  for (std::int32_t c = 0; c < f.numChunks; ++c) {
79
- if (!isPageAllZero(pages[c]) || activeBricks[c] != 0ULL) {
88
+ if (storesChunk(pages, activeBricks, baseline, c)) {
80
89
  storeLeU32(outBytes + pos, static_cast<std::uint32_t>(c));
81
90
  storeLeU64(outBytes + pos + 4, activeBricks[c]);
82
91
  pos += 12;
@@ -118,33 +127,39 @@ std::int32_t rundot_kinetix_cellular_serialize(
118
127
 
119
128
  std::int32_t rundot_kinetix_cellular_serialize_checkpoint_bound(
120
129
  std::uint32_t handle,
121
- std::int32_t checkpointToken) noexcept {
130
+ std::int32_t checkpointToken,
131
+ std::int32_t baselineToken) noexcept {
122
132
  using namespace rundot::kinetix::cellular;
123
133
  using namespace rundot::kinetix::cellular::detail;
124
134
  CellularField* f = resolveField(handle);
125
135
  if (f == nullptr) return kStatusInvalidHandle;
126
136
  CheckpointSlot* found = findCheckpointSlot(*f, checkpointToken);
127
- if (found == nullptr || found->config == nullptr) return kStatusInvalidCheckpoint;
137
+ const CheckpointSlot* baseline = findCheckpointSlot(*f, baselineToken);
138
+ if (found == nullptr || found->config == nullptr ||
139
+ (baselineToken != 0 && baseline == nullptr)) return kStatusInvalidCheckpoint;
128
140
  return serializeStateBoundInternal(
129
- f->numChunks, found->config->ruleCount, found->pages, found->activeBricks);
141
+ f->numChunks, found->config->ruleCount, found->pages, found->activeBricks, baseline);
130
142
  }
131
143
 
132
144
  std::int32_t rundot_kinetix_cellular_serialize_checkpoint(
133
145
  std::uint32_t handle,
134
146
  std::int32_t checkpointToken,
135
147
  std::uint8_t* outBytes,
136
- std::int32_t maxBytes) noexcept {
148
+ std::int32_t maxBytes,
149
+ std::int32_t baselineToken) noexcept {
137
150
  using namespace rundot::kinetix::cellular;
138
151
  using namespace rundot::kinetix::cellular::detail;
139
152
  CellularField* f = resolveField(handle);
140
153
  if (f == nullptr) return kStatusInvalidHandle;
141
154
  CheckpointSlot* found = findCheckpointSlot(*f, checkpointToken);
142
- if (found == nullptr || found->config == nullptr) return kStatusInvalidCheckpoint;
155
+ const CheckpointSlot* baseline = findCheckpointSlot(*f, baselineToken);
156
+ if (found == nullptr || found->config == nullptr ||
157
+ (baselineToken != 0 && baseline == nullptr)) return kStatusInvalidCheckpoint;
143
158
  const ConfigSnapshot& cfg = *found->config;
144
159
  return serializeStateBytesInternal(
145
160
  *f, cfg.cellSize, cfg.originX, cfg.originY, cfg.originZ,
146
161
  cfg.specs, cfg.visuals, cfg.rules, cfg.ruleCount,
147
- found->pages, found->activeBricks, outBytes, maxBytes);
162
+ found->pages, found->activeBricks, outBytes, maxBytes, baseline);
148
163
  }
149
164
 
150
165
  } // extern "C"
@@ -159,7 +174,8 @@ std::int32_t decodeFullSnapshotInternal(
159
174
  std::int32_t byteLength,
160
175
  ConfigSnapshot*& outCfg,
161
176
  ChunkPage**& outPages,
162
- std::uint64_t*& outActiveBricks) noexcept {
177
+ std::uint64_t*& outActiveBricks,
178
+ const CheckpointSlot* baseline = nullptr) noexcept {
163
179
  outCfg = nullptr;
164
180
  outPages = nullptr;
165
181
  outActiveBricks = nullptr;
@@ -167,9 +183,9 @@ std::int32_t decodeFullSnapshotInternal(
167
183
  const std::int32_t minHeader = 56 + kCellularMaxMaterials * 16;
168
184
  if (bytes == nullptr || byteLength < minHeader) return kStatusCorruptData;
169
185
 
170
- if (loadLeU32(bytes + 0) != kSnapshotMagic ||
171
- bytes[4] != static_cast<std::uint8_t>(kCodecVersion) ||
172
- bytes[5] != kCodecKindFull) {
186
+ const bool isDelta = bytes[4] == 2u && bytes[5] == kCodecKindDelta && baseline != nullptr;
187
+ const bool isFull = bytes[4] == kCodecVersion && bytes[5] == kCodecKindFull;
188
+ if (loadLeU32(bytes + 0) != kSnapshotMagic || (!isFull && !isDelta)) {
173
189
  return kStatusCorruptData;
174
190
  }
175
191
  const std::uint16_t ruleCount = loadLeU16(bytes + 6);
@@ -261,6 +277,30 @@ std::int32_t decodeFullSnapshotInternal(
261
277
  staged[cIdx] = decoded;
262
278
  }
263
279
  if (err == kStatusOk && pos != byteLength) err = kStatusCorruptData;
280
+ if (err == kStatusOk && isDelta) {
281
+ constexpr std::uint16_t maskFlags = kFlagBlocksMovement | kFlagBlocksProjectiles | kFlagStaticCollider;
282
+ bool refreshMasks = false;
283
+ for (std::int32_t m = 0; m < kCellularMaxMaterials; ++m) {
284
+ refreshMasks |= ((cfg->specs[m].flags ^ baseline->config->specs[m].flags) & maskFlags) != 0u;
285
+ }
286
+ for (std::int32_t c = 0; c < f.numChunks; ++c) {
287
+ if (seen[c] != 0u) continue;
288
+ stagedAct[c] = baseline->activeBricks[c];
289
+ ChunkPage* page = baseline->pages[c];
290
+ if (refreshMasks && page != nullptr) {
291
+ ChunkPage* refreshed = allocZeroed<ChunkPage>(1);
292
+ if (refreshed == nullptr) { err = kStatusOutOfMemory; break; }
293
+ refreshed->refCount = 1u;
294
+ std::memcpy(refreshed->materials, page->materials, kCellularChunkCells);
295
+ std::memcpy(refreshed->meta, page->meta, kCellularChunkCells);
296
+ rebuildPageMasksSpecs(cfg->specs, *refreshed);
297
+ staged[c] = refreshed;
298
+ } else {
299
+ retainPage(page);
300
+ staged[c] = page;
301
+ }
302
+ }
303
+ }
264
304
  freeMem(seen);
265
305
 
266
306
  if (err != kStatusOk) {
@@ -334,7 +374,8 @@ std::int32_t rundot_kinetix_cellular_hydrate(
334
374
  std::int32_t rundot_kinetix_cellular_import_checkpoint(
335
375
  std::uint32_t handle,
336
376
  const std::uint8_t* bytes,
337
- std::int32_t byteLength) noexcept {
377
+ std::int32_t byteLength,
378
+ std::int32_t baselineToken) noexcept {
338
379
  using namespace rundot::kinetix::cellular;
339
380
  using namespace rundot::kinetix::cellular::detail;
340
381
  CellularField* f = resolveField(handle);
@@ -350,10 +391,12 @@ std::int32_t rundot_kinetix_cellular_import_checkpoint(
350
391
  }
351
392
  if (!hasFreeSlot) return kStatusCapacityExceeded;
352
393
 
394
+ const CheckpointSlot* baseline = findCheckpointSlot(*f, baselineToken);
395
+ if (baselineToken != 0 && baseline == nullptr) return kStatusInvalidCheckpoint;
353
396
  ConfigSnapshot* cfg = nullptr;
354
397
  ChunkPage** staged = nullptr;
355
398
  std::uint64_t* stagedAct = nullptr;
356
- std::int32_t err = decodeFullSnapshotInternal(*f, bytes, byteLength, cfg, staged, stagedAct);
399
+ std::int32_t err = decodeFullSnapshotInternal(*f, bytes, byteLength, cfg, staged, stagedAct, baseline);
357
400
  if (err != kStatusOk) return err;
358
401
 
359
402
  std::uint8_t root32[32]{};
@@ -19,6 +19,12 @@ inline void freeArenaStorage(TeardownArena* a) noexcept {
19
19
  (void)releasePinInternal(a, i + 1);
20
20
  }
21
21
  }
22
+ for (std::int32_t i = 0; i < a->checkpointPinCapacity; ++i) {
23
+ if (a->checkpointPins[i].active) {
24
+ (void)releasePinInternal(a, kMaxPinsPerArena + i + 1);
25
+ }
26
+ }
27
+ std::free(a->checkpointPins);
22
28
  if (a->chunks) {
23
29
  for (std::int32_t c = 0; c < a->totalChunks; ++c) {
24
30
  if (a->chunks[c]) {
@@ -164,6 +170,9 @@ std::int32_t rundot_kinetix_teardown_get_stats(
164
170
  for (std::int32_t i = 0; i < kMaxPinsPerArena; ++i) {
165
171
  if (a->pins[i].active) activePins += 1u;
166
172
  }
173
+ for (std::int32_t i = 0; i < a->checkpointPinCapacity; ++i) {
174
+ if (a->checkpointPins[i].active) activePins += 1u;
175
+ }
167
176
  a->liveAllocatedPages = livePages;
168
177
 
169
178
  outStats16[0] = static_cast<std::uint32_t>(a->dimX);
@@ -504,10 +513,11 @@ std::int32_t rundot_kinetix_teardown_extract_islands(
504
513
  outOccupancies, maxOccupancyBytes, outMaterials, maxMaterialWords, outCounts3));
505
514
  }
506
515
 
507
- std::int32_t rundot_kinetix_teardown_pin_snapshot(std::int32_t arenaId) noexcept {
516
+ std::int32_t rundot_kinetix_teardown_pin_snapshot(
517
+ std::int32_t arenaId, std::int32_t checkpoint) noexcept {
508
518
  TeardownArena* a = getArena(arenaId);
509
519
  if (!a) return static_cast<std::int32_t>(TeardownStatus::InvalidArena);
510
- return pinSnapshotInternal(a);
520
+ return pinSnapshotInternal(a, checkpoint != 0);
511
521
  }
512
522
 
513
523
  std::int32_t rundot_kinetix_teardown_release_pin(
@@ -533,11 +543,12 @@ std::int32_t rundot_kinetix_teardown_export_pinned_raw(
533
543
  std::int32_t baselinePinId,
534
544
  std::uint8_t* outBuffer,
535
545
  std::uint32_t maxBytes,
536
- std::uint32_t* outBytesWritten) noexcept {
546
+ std::uint32_t* outBytesWritten,
547
+ std::int32_t useBrickRemovals) noexcept {
537
548
  TeardownArena* a = getArena(arenaId);
538
549
  if (!a) return static_cast<std::int32_t>(TeardownStatus::InvalidArena);
539
550
  return static_cast<std::int32_t>(exportPinnedRawInternal(
540
- a, pinId, baselinePinId, outBuffer, maxBytes, outBytesWritten));
551
+ a, pinId, baselinePinId, outBuffer, maxBytes, outBytesWritten, useBrickRemovals != 0));
541
552
  }
542
553
 
543
554
  std::int32_t rundot_kinetix_teardown_encode_raw_to_compressed(
@@ -549,8 +560,9 @@ std::int32_t rundot_kinetix_teardown_encode_raw_to_compressed(
549
560
  if (!rawPacket || !outCompressedBytes || rawBytes < 32u) {
550
561
  return static_cast<std::int32_t>(TeardownStatus::InvalidArgument);
551
562
  }
552
- const std::uint8_t expectedMagic[8] = {'S', 'Y', 'N', 'T', 'R', 'A', 'W', '1'};
553
- if (std::memcmp(rawPacket, expectedMagic, 8u) != 0) {
563
+ const std::uint8_t expectedMagic[7] = {'S', 'Y', 'N', 'T', 'R', 'A', 'W'};
564
+ const std::uint8_t version = rawPacket[7];
565
+ if (std::memcmp(rawPacket, expectedMagic, 7u) != 0 || (version != '1' && version != '2')) {
554
566
  return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
555
567
  }
556
568
  const std::uint32_t payloadBytes = rawBytes - 32u;
@@ -569,7 +581,7 @@ std::int32_t rundot_kinetix_teardown_encode_raw_to_compressed(
569
581
  rawPacket + 32u, payloadBytes, outCompressed + 36u, maxCompressedBytes - 36u, hashTable);
570
582
  if (compLen == 0u) return static_cast<std::int32_t>(TeardownStatus::BufferOverflow);
571
583
  }
572
- const std::uint8_t snapMagic[8] = {'S', 'Y', 'N', 'T', 'S', 'N', 'P', '1'};
584
+ const std::uint8_t snapMagic[8] = {'S', 'Y', 'N', 'T', 'S', 'N', 'P', version};
573
585
  std::memcpy(outCompressed, snapMagic, 8u);
574
586
  std::memcpy(outCompressed + 8u, rawPacket + 8u, 24u);
575
587
  writeU32LE(outCompressed + 32u, payloadBytes);
@@ -581,14 +593,16 @@ std::int32_t rundot_kinetix_teardown_restore_snapshot(
581
593
  std::int32_t arenaId,
582
594
  const std::uint8_t* compressedPacket,
583
595
  std::uint32_t compressedBytes,
584
- std::int32_t baselinePinId) noexcept {
596
+ std::int32_t baselinePinId,
597
+ std::int32_t* outPinId) noexcept {
585
598
  TeardownArena* a = getArena(arenaId);
586
599
  if (!a) return static_cast<std::int32_t>(TeardownStatus::InvalidArena);
587
600
  if (!compressedPacket || compressedBytes < 36u) {
588
601
  return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
589
602
  }
590
- const std::uint8_t snapMagic[8] = {'S', 'Y', 'N', 'T', 'S', 'N', 'P', '1'};
591
- if (std::memcmp(compressedPacket, snapMagic, 8u) != 0) {
603
+ const std::uint8_t snapMagic[7] = {'S', 'Y', 'N', 'T', 'S', 'N', 'P'};
604
+ const std::uint8_t version = compressedPacket[7];
605
+ if (std::memcmp(compressedPacket, snapMagic, 7u) != 0 || (version != '1' && version != '2')) {
592
606
  return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
593
607
  }
594
608
  const std::uint32_t flags = readU32LE(compressedPacket + 8u);
@@ -604,7 +618,7 @@ std::int32_t rundot_kinetix_teardown_restore_snapshot(
604
618
  }
605
619
  const std::uint64_t minimumPayload = static_cast<std::uint64_t>(entryCount) * 5u;
606
620
  const std::uint64_t maximumPayload = static_cast<std::uint64_t>(entryCount) * (5u + kChunkPageBytes);
607
- if ((flags & ~1u) != 0u || entryCount > static_cast<std::uint32_t>(a->totalChunks) ||
621
+ if ((flags & ~1u) != 0u || (version == '2' && flags != 1u) || entryCount > static_cast<std::uint32_t>(a->totalChunks) ||
608
622
  payloadBytes < minimumPayload || payloadBytes > maximumPayload) {
609
623
  return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
610
624
  }
@@ -627,56 +641,78 @@ std::int32_t rundot_kinetix_teardown_restore_snapshot(
627
641
  return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
628
642
  }
629
643
 
630
- // Validate entry framing before mutating live arena state.
631
- std::uint32_t cursor = 0u;
632
- std::uint64_t seenChunks[(kMaxArenaChunks + 63) / 64]{};
633
- for (std::uint32_t e = 0u; e < entryCount; ++e) {
634
- if (cursor + 5u > payloadBytes) {
635
- std::free(raw);
636
- return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
637
- }
638
- const std::uint32_t cIdx = readU32LE(raw + cursor);
639
- const std::uint8_t tag = raw[cursor + 4u];
640
- if (cIdx >= static_cast<std::uint32_t>(a->totalChunks) || tag > 1u) {
644
+ SnapshotRestorePreparation preparation(a, isDelta ? base : nullptr);
645
+ if (version == '2') {
646
+ const TeardownStatus status = stageArenaDelta(a, base, raw, payloadBytes, entryCount, preparation);
647
+ if (status != TeardownStatus::Success) {
641
648
  std::free(raw);
642
- return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
649
+ return static_cast<std::int32_t>(status);
643
650
  }
644
- const std::uint64_t bit = 1ULL << (cIdx & 63u);
645
- if ((seenChunks[cIdx >> 6] & bit) != 0u) {
646
- std::free(raw);
647
- return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
651
+ } else {
652
+ // Validate entry framing before mutating live arena state.
653
+ std::uint32_t cursor = 0u;
654
+ std::uint64_t seenChunks[(kMaxArenaChunks + 63) / 64]{};
655
+ for (std::uint32_t e = 0u; e < entryCount; ++e) {
656
+ if (cursor + 5u > payloadBytes) {
657
+ std::free(raw);
658
+ return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
659
+ }
660
+ const std::uint32_t cIdx = readU32LE(raw + cursor);
661
+ const std::uint8_t tag = raw[cursor + 4u];
662
+ if (cIdx >= static_cast<std::uint32_t>(a->totalChunks) || tag > 1u) {
663
+ std::free(raw);
664
+ return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
665
+ }
666
+ const std::uint64_t bit = 1ULL << (cIdx & 63u);
667
+ if ((seenChunks[cIdx >> 6] & bit) != 0u) {
668
+ std::free(raw);
669
+ return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
670
+ }
671
+ seenChunks[cIdx >> 6] |= bit;
672
+ cursor += (tag == 1u) ? (5u + kChunkPageBytes) : 5u;
673
+ if (cursor > payloadBytes) {
674
+ std::free(raw);
675
+ return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
676
+ }
648
677
  }
649
- seenChunks[cIdx >> 6] |= bit;
650
- cursor += (tag == 1u) ? (5u + kChunkPageBytes) : 5u;
651
- if (cursor > payloadBytes) {
678
+ if (cursor != payloadBytes) {
652
679
  std::free(raw);
653
680
  return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
654
681
  }
655
- }
656
- if (cursor != payloadBytes) {
657
- std::free(raw);
658
- return static_cast<std::int32_t>(TeardownStatus::CorruptSnapshot);
659
- }
660
682
 
661
- SnapshotRestorePreparation preparation(a, isDelta ? base : nullptr);
662
- cursor = 0u;
663
- for (std::uint32_t e = 0u; e < entryCount; ++e) {
664
- const std::int32_t cIdx = static_cast<std::int32_t>(readU32LE(raw + cursor));
665
- const std::uint8_t tag = raw[cursor + 4u];
666
- cursor += 5u;
667
- if (tag == 0u) {
668
- preparation.clearChunk(cIdx);
669
- } else {
670
- if (!preparation.writeChunk(cIdx, raw + cursor)) {
671
- std::free(raw);
672
- return static_cast<std::int32_t>(TeardownStatus::OutOfMemory);
683
+ cursor = 0u;
684
+ for (std::uint32_t e = 0u; e < entryCount; ++e) {
685
+ const std::int32_t cIdx = static_cast<std::int32_t>(readU32LE(raw + cursor));
686
+ const std::uint8_t tag = raw[cursor + 4u];
687
+ cursor += 5u;
688
+ if (tag == 0u) {
689
+ preparation.clearChunk(cIdx);
690
+ } else {
691
+ ChunkPage* previous = base ? base->pages[cIdx] : nullptr;
692
+ if (previous && std::memcmp(previous->data, raw + cursor, kChunkPageBytes) == 0) {
693
+ preparation.borrowChunk(cIdx, previous);
694
+ } else if (!preparation.writeChunk(cIdx, raw + cursor)) {
695
+ std::free(raw);
696
+ return static_cast<std::int32_t>(TeardownStatus::OutOfMemory);
697
+ }
698
+ cursor += kChunkPageBytes;
673
699
  }
674
- cursor += kChunkPageBytes;
675
700
  }
701
+
676
702
  }
677
703
 
678
- preparation.commit();
679
704
  std::free(raw);
705
+ if (outPinId) {
706
+ const std::int32_t pinId = nextPinId(a, true);
707
+ if (pinId < 0) return pinId;
708
+ std::uint32_t nonEmpty = 0u;
709
+ ChunkPage** pages = preparation.takePinnedPages(nonEmpty);
710
+ if (!pages) return static_cast<std::int32_t>(TeardownStatus::OutOfMemory);
711
+ *pinSlot(a, pinId) = {true, generation, nonEmpty, pages};
712
+ *outPinId = pinId;
713
+ return static_cast<std::int32_t>(TeardownStatus::Success);
714
+ }
715
+ preparation.commit();
680
716
  if (generation > a->generationCounter) a->generationCounter = generation;
681
717
  return static_cast<std::int32_t>(TeardownStatus::Success);
682
718
  }
@@ -161,7 +161,8 @@ std::int32_t rundot_kinetix_teardown_extract_islands(
161
161
  std::uint32_t maxMaterialWords,
162
162
  std::uint32_t* outCounts3) noexcept;
163
163
 
164
- std::int32_t rundot_kinetix_teardown_pin_snapshot(std::int32_t arenaId) noexcept;
164
+ std::int32_t rundot_kinetix_teardown_pin_snapshot(
165
+ std::int32_t arenaId, std::int32_t checkpoint = 0) noexcept;
165
166
  std::int32_t rundot_kinetix_teardown_release_pin(
166
167
  std::int32_t arenaId,
167
168
  std::int32_t pinId) noexcept;
@@ -175,7 +176,7 @@ std::int32_t rundot_kinetix_teardown_export_pinned_raw(
175
176
  std::int32_t baselinePinId,
176
177
  std::uint8_t* outBuffer,
177
178
  std::uint32_t maxBytes,
178
- std::uint32_t* outBytesWritten) noexcept;
179
+ std::uint32_t* outBytesWritten, std::int32_t useBrickRemovals = 0) noexcept;
179
180
 
180
181
  std::int32_t rundot_kinetix_teardown_encode_raw_to_compressed(
181
182
  const std::uint8_t* rawPacket,
@@ -188,7 +189,7 @@ std::int32_t rundot_kinetix_teardown_restore_snapshot(
188
189
  std::int32_t arenaId,
189
190
  const std::uint8_t* compressedPacket,
190
191
  std::uint32_t compressedBytes,
191
- std::int32_t baselinePinId) noexcept;
192
+ std::int32_t baselinePinId, std::int32_t* outPinId = nullptr) noexcept;
192
193
 
193
194
  } // extern "C"
194
195
 
@@ -0,0 +1,99 @@
1
+ // Version 2 uses the version 1 header and full-chunk records. Tag 2 holds
2
+ // an 8-bit brick mask and one 64-byte removal mask per selected brick.
3
+ namespace {
4
+
5
+ constexpr std::uint32_t brickPlaneOffset(std::uint32_t brick, std::uint32_t row) noexcept {
6
+ return (brick & 1u) * 8u + (brick & 2u) * 1024u + (brick & 4u) * 32u +
7
+ (row & 7u) * 16u + (row >> 3u) * 256u;
8
+ }
9
+
10
+ std::uint32_t encodeRemovals(const std::uint8_t* current, const std::uint8_t* base,
11
+ std::uint8_t* out) noexcept {
12
+ std::uint32_t pos = 1u;
13
+ std::uint8_t mask = 0u;
14
+ for (std::uint32_t brick = 0; brick < 8u; ++brick) {
15
+ std::uint8_t removals[64]{};
16
+ bool changed = false;
17
+ for (std::uint32_t row = 0; row < 64u; ++row) {
18
+ const std::uint32_t offset = brickPlaneOffset(brick, row);
19
+ if (std::memcmp(current + offset, base + offset, 8u) == 0 &&
20
+ std::memcmp(current + 4096u + offset, base + 4096u + offset, 8u) == 0) continue;
21
+ for (std::uint32_t x = 0; x < 8u; ++x) {
22
+ const std::uint32_t cell = offset + x;
23
+ if (current[cell] == base[cell] && current[4096u + cell] == base[4096u + cell]) continue;
24
+ if (base[cell] == 0u || current[cell] != 0u || current[4096u + cell] != 0u) return 0u;
25
+ removals[row] |= static_cast<std::uint8_t>(1u << x);
26
+ changed = true;
27
+ }
28
+ }
29
+ if (changed) {
30
+ mask |= static_cast<std::uint8_t>(1u << brick);
31
+ std::memcpy(out + pos, removals, 64u);
32
+ pos += 64u;
33
+ }
34
+ }
35
+ out[0] = mask;
36
+ return pos;
37
+ }
38
+
39
+ bool readRemovalBricks(const std::uint8_t* raw, std::uint32_t end, std::uint32_t& cursor,
40
+ std::uint8_t* page) noexcept {
41
+ if (cursor == end) return false;
42
+ const std::uint8_t mask = raw[cursor++];
43
+ if (mask == 0u) return false;
44
+ for (std::uint32_t brick = 0; brick < 8u; ++brick) {
45
+ if ((mask & (1u << brick)) == 0u) continue;
46
+ if (end - cursor < 64u) return false;
47
+ bool changed = false;
48
+ for (std::uint32_t row = 0; row < 64u; ++row) {
49
+ const std::uint8_t bits = raw[cursor++];
50
+ const std::uint32_t offset = brickPlaneOffset(brick, row);
51
+ for (std::uint32_t x = 0; x < 8u; ++x) {
52
+ if ((bits & (1u << x)) == 0u) continue;
53
+ if (page[offset + x] == 0u) return false;
54
+ page[offset + x] = 0u;
55
+ page[4096u + offset + x] = 0u;
56
+ changed = true;
57
+ }
58
+ }
59
+ if (!changed) return false;
60
+ }
61
+ return true;
62
+ }
63
+
64
+ } // namespace
65
+
66
+
67
+ TeardownStatus stageArenaDelta(TeardownArena* arena, const PinnedGeneration* baseline,
68
+ const std::uint8_t* raw, std::uint32_t byteLength,
69
+ std::uint32_t entryCount, SnapshotRestorePreparation& staged) noexcept {
70
+ std::int32_t lastChunk = -1;
71
+ std::uint32_t cursor = 0u;
72
+ for (std::uint32_t entry = 0; entry < entryCount; ++entry) {
73
+ if (byteLength - cursor < 5u) return TeardownStatus::CorruptSnapshot;
74
+ const std::uint32_t index = readU32LE(raw + cursor);
75
+ const std::uint8_t tag = raw[cursor + 4u];
76
+ cursor += 5u;
77
+ if (index >= static_cast<std::uint32_t>(arena->totalChunks) ||
78
+ static_cast<std::int32_t>(index) <= lastChunk || tag > 2u) return TeardownStatus::CorruptSnapshot;
79
+ lastChunk = static_cast<std::int32_t>(index);
80
+ if (tag == 0u) {
81
+ staged.clearChunk(lastChunk);
82
+ continue;
83
+ }
84
+ if (tag == 1u) {
85
+ if (byteLength - cursor < kChunkPageBytes) return TeardownStatus::CorruptSnapshot;
86
+ if (!staged.writeChunk(lastChunk, raw + cursor)) return TeardownStatus::OutOfMemory;
87
+ cursor += kChunkPageBytes;
88
+ continue;
89
+ }
90
+
91
+ std::uint8_t page[kChunkPageBytes]{};
92
+ const ChunkPage* previous = baseline->pages[index];
93
+ if (previous != nullptr) std::memcpy(page, previous->data, kChunkPageBytes);
94
+ if (!readRemovalBricks(raw, byteLength, cursor, page)) return TeardownStatus::CorruptSnapshot;
95
+ if (!staged.writeChunk(lastChunk, page)) return TeardownStatus::OutOfMemory;
96
+ }
97
+ if (cursor != byteLength) return TeardownStatus::CorruptSnapshot;
98
+ return TeardownStatus::Success;
99
+ }