GizStackGizStack
View .mdOpen in ChatGPTOpen in Claude

gizgame/files

Host-delivered files, lent font glyphs, ranged byte parts, and paced later preparation steps.

gizlang
import * as Files from "gizgame/files";

Types

FileStore

gizlang
class FileStore { files = new Map<string, bytes>(); lent = new Map<i64, Lent>(); lacking: Vec<i64> = []; sought = new Set<i64>(); wanted: Vec<string> = []; awaited = new Set<string>(); asked = new Set<string>(); needed: string = ""; wholeCount: i64 = 0; received: i64 = 0; expected: i64 = 0; steps: Vec<() => void throws string> = []; done: i64 = 0; paced: bool = false; made: Vec<Texture> = []; at: i64 = 0; stalled: bool = false; seen = new Set<string>(); early = new Set<string>(); ranged = new Set<string>(); gathered = new Set<string>(); taken: Vec<string> = []; receive(input: bytes): bytes throws string { const end = input.indexOf(b"\n", 0); if (end <1) { throw "A file is NAME, a newline and the bytes"; } const name = string(input.slice(0, end)); if (!this.files.has(name) && !name.includes("@")) { this.wholeCount + = 1; } this.files.set(name, input.slice(end + 1)); this.awaited.delete(name); this.asked.delete(name); this.received + = 1; const cut = name.lastIndexOf("@"); if (cut> 0) { this.ranged.add(name.slice(0, cut)); } return b""; } given(): f64 { if (this.expected <= 0) { return 1.0; } return Math.min(1.0, f64(this.wholeCount) / f64(this.expected)); } glyph(code: i64): Lent | undefined { const found = this.lent.get(code); if (found === undefined && !this.sought.has(code)) { this.sought.add(code); this.lacking.push(code); } return found; } addGlyph(code: i64, width: i64, height: i64, alpha: bytes, size: f64, advance: f64, left: f64, top: f64): void { let pixels = b""; for (const a of alpha) { pixels.push(255u); pixels.push(255u); pixels.push(255u); pixels.push(a); } if (pixels.length === width * height * 4) { this.lent.set(code, { picture: { width, height, pixels }, size: Math.max(1, size), advance, left, top }); } } hold(name: string, data: bytes): void { this.files.set(name, data); } knows(names: Vec<string>): void { for (const name of names) { this.seen.add(name); this.early.add(name); } } has(name: string, supplied: ((name: string) => bytes | undefined) | undefined, holds: ((name: string) => bool) | undefined): bool { if (this.files.has(name)) { return true; } const here = supplied !== undefined && (holds !== undefined ? holds!(name): supplied!(name) !== undefined); if (here) { this.seen.add(name); } return here; } file(name: string, supplied: ((name: string) => bytes | undefined) | undefined, drawn: bool, reading: bool): bytes throws string { if (this.paced && !this.early.has(name)) { this.taken.push(name); } const given = this.files.get(name); if (given !== undefined) { return given; } if (supplied !== undefined) { const found = supplied!(name); if (found === undefined) { throw `The host gave no file ${name }:  is it in the assets lock?`; } if (drawn && !reading && !this.seen.has(name)) { throw `${name }:  read outside a step of later,  where a page cannot wait for it`; } this.seen.add(name); return found!; } if (!this.awaited.has(name)) { this.awaited.add(name); this.wanted.push(name); } this.needed = name; throw WAITING; } letGo(file: string): void { this.release(file); this.ranged.delete(file); } keep(file: string): void { this.gathered.add(file); } release(file: string): void { this.gathered.delete(file); const prefix = `${file }@`; for (const name of this.files.keys().filter(n => n.startsWith(prefix))) { this.files.delete(name); } for (const name of [...this.asked].filter(n => n.startsWith(prefix))) { this.asked.delete(name); } } forget(names: Vec<string>): void { for (const name of names) { this.files.delete(name); this.asked.delete(name); this.seen.delete(name); } } held(): Vec<string> { const coming = [...this.awaited]; return [...this.ranged].filter(file => !this.gathered.has(file) && !coming.some(name => name.startsWith(`${file }@`))); } private spent(): void { this.forget(this.taken); this.taken = []; } ask(names: Vec<string>, hasSupplier: bool): void { if (hasSupplier) { return; } for (const name of names) { if (!this.files.has(name) && !this.awaited.has(name) && !this.asked.has(name)) { this.asked.add(name); this.wanted.push(`?${name }`); } } } waiting(): bool { return this.awaited.has(this.needed); } needs(): string { return this.needed; } awaiting(): bool { return this.awaited.size> 0; } later(step: () => void throws string): void { this.steps.push(step); } prepared(): f64 { return this.steps.length === 0 ? 1.0: f64(this.done) / f64(this.steps.length); } filesIn(folder: string): Vec<string> { return this.files.keys().filter(n => n.startsWith(folder)); } prepare(setReading: (on: bool) => void): bool throws string { if (this.done> = this.steps.length) { this.steps = []; this.done = 0; return false; } this.stalled = false; if (this.waiting()) { return true; } const step = this.steps[this.done]; this.at = 0; this.paced = true; setReading(true); this.taken = []; const made = this.made.length; try { step(); } catch (why) { this.paced = false; setReading(false); if (why === WAITING) { this.stalled = this.made.length === made; return true; } this.made = []; this.done + = 1; this.spent(); throw why; } this.paced = false; setReading(false); this.made = []; this.done + = 1; this.spent(); return true; } paceTextures(requests: Vec<Request>, budget: i64): Vec<Texture> throws string { let out: Vec<Texture> = []; for (const r of requests) { if (!this.paced) { out.push(textureBatch([r], {}, budget)[0]); continue; } if (this.at === this.made.length) { this.made.push(textureBatch([r], {}, budget)[0]); throw WAITING; } out.push(this.made[this.at]); this.at + = 1; } return out; } drainCues(out: mut Vec<HostCue>): void { if (this.lacking.length> 0) { out.push({ kind: "glyphs", text: fromCodePoints(this.lacking) }); this.lacking = []; } if (this.wanted.length> 0) { out.push({ kind: "files", text: this.wanted.join("\n") }); this.wanted = []; } } }

Manages files and glyphs streamed from a host alongside paced later asset-preparation steps.

  • files: _gizlang_sdk_collections.Map<string, bytes>
  • lent: _gizlang_sdk_collections.Map<i64, gizlang_draw.Lent>
  • lacking: Vec<i64>
  • sought: _gizlang_sdk_collections.Set<i64>
  • wanted: Vec<string>
  • awaited: _gizlang_sdk_collections.Set<string>
  • asked: _gizlang_sdk_collections.Set<string>
  • needed: string
  • wholeCount: i64
  • received: i64
  • expected: i64
  • steps: Vec<() => void throws string>
  • done: i64
  • paced: bool
  • made: Vec<gizlang_meshdraw.Texture>
  • at: i64
  • stalled: bool
  • seen: _gizlang_sdk_collections.Set<string>
  • early: _gizlang_sdk_collections.Set<string>
  • ranged: _gizlang_sdk_collections.Set<string>
  • gathered: _gizlang_sdk_collections.Set<string>
  • taken: Vec<string>

Methods

FileStore.receive

gizlang
receive(input: bytes): bytes throws string

Receives one host file message: NAME, a newline, and the bytes.

Effects: fail, read, write, alloc.

FileStore.given

gizlang
given(): f64

Share of expected initial host files received so far (1 when all are here).

Effects: fail, read.

FileStore.glyph

gizlang
glyph(code: i64): Lent | undefined

Returns the lent glyph picture for CODE, requesting it from the host once if absent.

Effects: fail, read, write, alloc.

FileStore.addGlyph

gizlang
addGlyph(code: i64, width: i64, height: i64, alpha: bytes, size: f64, advance: f64, left: f64, top: f64): void

Stores a host-rendered alpha bitmap glyph.

Effects: fail, read, write, alloc.

FileStore.hold

gizlang
hold(name: string, data: bytes): void

Stores DATA under NAME (e.g. bytes brought by a world edit).

Effects: fail, write, alloc.

FileStore.knows

gizlang
knows(names: Vec<string>): void

Marks NAMES as provided before start so reads may take them anywhere.

Effects: fail, write, alloc.

FileStore.has

gizlang
has(name: string, supplied: ((name: string) => bytes | undefined) | undefined, holds: ((name: string) => bool) | undefined): bool

Whether NAME is present in the store or in the host supplier.

Effects: fail, read, write, console, io, alloc.

FileStore.file

gizlang
file(name: string, supplied: ((name: string) => bytes | undefined) | undefined, drawn: bool, reading: bool): bytes throws string

Reads file NAME, or requests it from the host and throws WAITING.

Effects: fail, read, write, console, io, alloc.

FileStore.letGo

gizlang
letGo(file: string): void

Releases ranged parts of FILE and stops tracking it as held.

Effects: fail, read, write, alloc.

FileStore.keep

gizlang
keep(file: string): void

Keeps ranged parts of FILE until release/letGo.

Effects: fail, write, alloc.

FileStore.release

gizlang
release(file: string): void

Forgets ranged parts of FILE (FILE@OFFSET+LENGTH).

Effects: fail, read, write, alloc.

FileStore.forget

gizlang
forget(names: Vec<string>): void

Forgets whole files NAMES so a later read asks the host again.

Effects: fail, write, alloc.

FileStore.held

gizlang
held(): Vec<string>

Ranged files with no in-flight parts and no active keep().

Effects: fail, read, alloc.

FileStore.ask

gizlang
ask(names: Vec<string>, hasSupplier: bool): void

Prefetches NAMES ahead of later steps when no local supplier is attached.

Effects: fail, read, write, alloc.

FileStore.waiting

gizlang
waiting(): bool

Whether the file asked for last has yet to come.

Effects: fail, read.

FileStore.needs

gizlang
needs(): string

The name of the file asked for last.

Effects: fail, read.

FileStore.awaiting

gizlang
awaiting(): bool

Whether any file asked for has yet to come.

Effects: fail, read.

FileStore.later

gizlang
later(step: () => void throws string): void

Adds STEP to the paced preparation steps.

Effects: fail, write, alloc.

FileStore.prepared

gizlang
prepared(): f64

The share of the preparation steps done, 0 to 1.

Effects: fail, read.

FileStore.filesIn

gizlang
filesIn(folder: string): Vec<string>

The names of the held files under FOLDER.

Effects: fail, read, alloc.

FileStore.prepare

gizlang
prepare(setReading: (on: bool) => void): bool throws string

Runs the next later step, pausing on WAITING and dropping a step that throws another error.

Effects: fail, read, write, console, io, alloc.

FileStore.paceTextures

gizlang
paceTextures(requests: Vec<Request>, budget: i64): Vec<Texture> throws string

Converts one texture per later frame on single-threaded hosts so the loading bar keeps moving.

Effects: fail, read, write, console, io, alloc.

FileStore.drainCues

gizlang
drainCues(out: mut Vec<HostCue>): void

Appends pending glyph and file request cues to OUT.

Effects: fail, read, write, alloc.

Constants

WAITING

gizlang
const WAITING = "gizgame: the host has yet to give a file"

What Game.file throws when a page has yet to give the file it asked for.