Files
Blockline/scripts/snapshot_fragments.gd
T

25 lines
831 B
GDScript
Raw Normal View History

2026-09-12 11:01:37 +02:00
class_name SnapshotFragments
extends RefCounted
const PART_BYTES := 1000
const MAX_PARTS := 64
var sequence := -1
var expected := 0
var pieces: Dictionary[int, PackedByteArray] = {}
func accept(serial: int, index: int, count: int, bytes: PackedByteArray) -> PackedByteArray:
if serial < sequence or count < 1 or count > MAX_PARTS or index < 0 or index >= count or bytes.is_empty() or bytes.size() > PART_BYTES:
return PackedByteArray()
if serial > sequence:
sequence = serial
expected = count
pieces.clear()
if count != expected: return PackedByteArray()
pieces[index] = bytes
if pieces.size() != count: return PackedByteArray()
var result := PackedByteArray()
for i in count: result.append_array(pieces[i])
pieces.clear()
expected = 0 # Ignore duplicate fragments of an already applied snapshot.
return result