Files
VS-map-previewer/src/pb.sex
alex-eg 10ec6236fd order pb's methods by topic, and read rock-mode in the literal
A method can call one written below it now (sex#44), so pb's read as
an API: making one, asking about it, reading it, then the private
helpers. A path inside a compound literal follows the pointer
(sex#45). bytes keeps its local: the window starts where self stood
before it moves.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 18:17:55 +03:00

87 lines
2.8 KiB
Scheme

;;; Protobuf wire reader for the blobs in a .vcdbs save. The engine's
;;; message classes are closed, so callers walk fields by number and
;;; keep only what they know; everything else is skipped.
;;;
;;; A reader is a window over borrowed bytes. A malformed blob sets
;;; `err' and empties the window, so a loop on `more' always ends.
(pub include stddef.h)
(include string.h)
(pub enum wire ((WIRE-VARINT 0) (WIRE-FIXED64 1) (WIRE-BYTES 2) (WIRE-FIXED32 5)))
(pub record pb ((p (* const u8)) (end (* const u8)) (err bool)))
(ops pb
(pub fn of ((data (* const void)) (len size-t)) pb
(var p (* const u8) data)
(return #(pb : p (+ p len) false)))
(pub fn more ((self (* const pb))) bool
(return (< (. self p) (. self end))))
(pub fn len ((self (* const pb))) size-t
(return (cast (- (. self end) (. self p)) size-t)))
;; True when the rest of the window spells exactly S, e.g. a map
;; entry's key against "riverDistance".
(pub fn equals ((self (* const pb)) (s (* const char))) bool
(var n size-t (.len self))
(return (&& (== (strlen s) n) (== (memcmp (. self p) s n) 0))))
;; The next field's number, or 0 at the end. WIRE gets its wire type.
(pub fn field ((self (* pb)) (wire (* int))) u32
(if (! (.more self))
(return 0))
(var key u64 (.varint self))
(= (* wire) (cast (& key 7) int))
(return (cast (>> key 3) u32)))
(pub fn varint ((self (* pb))) u64
(var v u64 0)
(for (var shift int 0) (< shift 64) (+= shift 7)
(if (>= (. self p) (. self end))
(break))
(var b u8 (* (. self p)))
(++ (. self p))
(c-bit-or= v (<< (cast (& b 0x7F) u64) shift))
(if (< b 0x80)
(return v)))
(.fail self)
(return 0))
;; A packed or repeated `int' field: negative values arrive as ten
;; byte varints and keep their meaning when truncated.
(pub fn int32 ((self (* pb))) i32
(return (cast (.varint self) i32)))
;; A length-delimited field as a reader of its own: a submessage, a
;; string, or a packed array.
(pub fn bytes ((self (* pb))) pb
(var len u64 (.varint self))
;; where the window starts, before self moves past it
(var p (* const u8) (. self p))
(if (> len (.len self))
(do
(.fail self)
(return #(pb : p p true))))
(+= (. self p) len)
(return #(pb : p (+ p len) false)))
(pub fn skip ((self (* pb)) (wire int)) void
(switch wire
(case WIRE-VARINT (.varint self) (break))
(case WIRE-BYTES (.bytes self) (break))
(case WIRE-FIXED64 (.advance self 8) (break))
(case WIRE-FIXED32 (.advance self 4) (break))
(default (.fail self))))
(fn advance ((self (* pb)) (n size-t)) void
(if (> n (.len self))
(.fail self)
(+= (. self p) n)))
(fn fail ((self (* pb))) void
(= (. self err) true)
(= (. self p) (. self end))))