Log inGet started
Package

cartlib.module

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

cartlib — the session cartridge library

require("stations.cartlib") gives every console station one shared list of the cartridges in the room: each core's committed demo carts, the local player's own uploads, and what every other player in the session holds. Players upload ROMs from their computer and anyone in the room can play them; the bytes are copied peer to peer on demand.

Uploaded ROMs are never written to /zero/source: they live in the uploader's runtime-data library (chunked under the 256 KB value cap, the retro.host RomLibrary layout, so a console's own loader lists them too) and in memory. Carts copied from another player live in memory for the session only.

API

local Cartlib = require("stations.cartlib")

Cartlib.list(consoleId?)  -> { Entry }       -- sorted by console, BIOS first, then title
-- Entry = { id, console, title, size, holders = {peerId}, builtin, isBios }
-- id = console .. ":" .. filename .. ":" .. size (stable across peers)
Cartlib.has(id) -> boolean                   -- bytes available on this peer
Cartlib.bytes(id) -> string?                 -- committed file, runtime library, or session cache
Cartlib.upload(consoleId?) -> (Entry?, why?) -- yields; system file picker, validate, store, announce
Cartlib.fetch(id, function(ok, why) end)     -- copy from a holder peer (<= 8 MB)
Cartlib.onChanged(cb) -> unsubscribe
Cartlib.biosFor(consoleId) -> id?            -- a locally held BIOS (e.g. neogeo.zip)
Cartlib.openLoader({ title, consoles = {"gameboy", ...}, onPick = function(entry) end })
Cartlib.closeLoader()
Cartlib.handleCallback(id, data) -> boolean  -- forward a UI callback (see below)
Cartlib.tick()                               -- prune players who left (CartLoader calls it)

Extras: Cartlib.get(id), Cartlib.progress(id) (0..1 while copying), Cartlib.consoles(), Cartlib.formatSize(n), Cartlib.refresh() (rescan disk and the runtime library), Cartlib.isLoaderOpen(), Cartlib.isUploading().

Consoles come from every installed emulatorCore (retro.host.registry). Any .zip is a Neo Geo set (console neogeo); neogeo.zip is the Neo Geo BIOS (isBios = true, validation skipped). If no core claims neogeo, the library still files zips under it. builtin entries are the files in each core's romsDir, held by everyone.

Neo Geo system files

The Neo Geo core reads its BIOS from a zip (neogeo.package romset): a system ROM (sp-s2.sp1, sp-s3.sp1, sp-u2.sp1, sp-j2.sp1, uni-bios*.rom, … the romset's BIOS_NAMES), sfix.sfix, sm1.sm1, and optionally 000-lo.lo. Players don't have to build that zip themselves:

  • Loose files (.sp1 .sfix .sm1 .rom .bin .lo, renamed copies like sp-s2 (1).sp1 included; Cartlib._biosPart(name) names each one's role) are kept in the player's runtime-data library under neogeo_bios/. Once a system ROM, sfix and sm1 are all held, they are packed (stored, uncompressed, Cartlib._storeZip) into neogeo.zip, which becomes the BIOS entry.
  • A zip of system files under any name is the BIOS when it holds all three; otherwise its files join the loose parts.
  • Cartlib.neoSetup() answers what is here: { ready, system, sfix, sm1, lo, missing, games }.

The picker takes several files at once on the All and Neo Geo tabs.

Help on the shelf

Every tab opens with an Adding games strip: the file types it takes and that uploads stay in the player's library and are shared with the room while they are here. The Neo Geo tab adds a setup card: step 1 System files (each file ✓/○, an Upload system files button while any is missing) and step 2 Your game zip. PLAY on a Neo Geo game without the system files keeps the shelf open, switches to that tab, says what is missing and puts focus on the upload button.

The loader panel

openLoader shows a centred card over a dimmed backdrop: console tabs (All + the consoles you pass), one row per cart (console tag, BIOS tag, title, console, size, who has it, and an action: PLAY when the bytes are here, GET to copy them, COPYING n%, TOO BIG past 8 MB), a close button (Esc) and an Upload from your computer button that filters the picker to the current tab's extensions. Everything is a button, so arrows / d-pad / stick move focus, Enter / south press, and touch taps. Picking a cart that is elsewhere copies it first and then calls onPick; an upload calls onPick with the new entry.

UI callbacks run in play. openLoader registers its own callback env, so callbacks reach it when it is opened from a component's code. A component that wants to be sure can forward its own onCallback(id, data) to Cartlib.handleCallback(id, data) (repeats within the same frame are ignored), or carry the stations/CartLoader component, which does that and calls Cartlib.tick().

Session sync

  • cartlib.announce {v=1, carts={meta...}} — each peer broadcasts the metadata (never bytes) of what it holds beyond the built-in carts: on load, after any upload or copy, and in answer to a hello.
  • cartlib.hello — sent on load and when a loader opens; every peer answers with an announce, so a late joiner learns the room.
  • Players who leave are dropped from every list (multiplayer.getPeers() checked on each list() and each tick()).

Transfer protocol

All messages are broadcasts that name their target peer first; other peers ignore them.

  1. requester → cartlib.req (holder, reqId, cartId)
  2. holder → cartlib.head (requester, reqId, chunkCount, byteLen) or cartlib.deny (requester, reqId, why)
  3. holder → cartlib.chunk (requester, reqId, i, base64) — 12 000 characters each, paced by a 150 KB/s budget shared by all transfers it serves; the budget halves when multiplayer.getDiagnostics().refusals rises and recovers by 10 % every half second.
  4. holder → cartlib.done; requester → cartlib.nack (holder, reqId, {missing}) for any gaps (also after 4 s without a chunk), up to 6 times.
  5. requester reassembles, checks the decoded length, caches in memory, announces itself as a holder. A holder that does not answer in 6 s, stalls or denies is swapped for the next holder (lowest latency first).

Carts over 8 MB never move (requester and holder both refuse); players upload their own copy.

Testing hook

Cartlib._addLocal(name, bytes, consoleHint?, { persist = false, share = false }?) is the path the picker uses after it reads the file (persist = false: session only; share = false: kept off the room's list); Cartlib._addFiles(files, consoleHint?, opts?) is the same for a picker's whole selection. _split, _assembler, _encode, _decode, _classify and _fetchFrom(peer, id, meta, onDone) are exposed for tests.