Skip to content

nwn-bake serve: the protocol

nwn-bake serve is a baker that stays running: the game (and, for characters and pictures, a GPU device) is opened once, and requests are answered one after another. A program that asks for many pictures (a website's backend, a batch script) starts it once and keeps it.

nwn-bake serve [--root <game install>] [--user <user folder>] [--tlk <name or path>] [--module <module>] [--cache-mb <MiB>]
  • --root is the NWN:EE install (the folder with data/ and lang/); without it the install is found as the game finds it.
  • --user is the user folder: its hak/, tlk/ and override/ are laid over the install. Without it, NWN_BAKE_USER, then NWN_HOME, then the game's own default, the same rule as every other command. Characters, items and placeables are read with every .hak in hak/, by name. With --module (anything area-iso takes), that module's hak list is used instead, in its order (the first listed wins, as in the game).
  • Without any user folder, serve still starts: the area requests work (each finds its module's haks in the install's data/hk/), and the character requests answer with an error that says why the game could not be opened for them. A user folder that is named but not there is an error at the start.
  • --tlk (or NWN_BAKE_TLK) names the custom talk table: a file name in the user folder's tlk/ or hak/ (.tlk may be left out) or a path. Not finding it, or a file that is not a talk table, is an error. By default the module's (none when it is not there), else the one .tlk in the user folder's tlk/ or hak/.
  • --cache-mb (or NWN_BAKE_CACHE_MB) bounds, in MiB, what the model and texture caches keep between requests: a quarter for models, the rest for textures, the entries used least recently dropped first. The default is 4096; 0 keeps as little as can be. It is one budget for the whole process: the game the character requests use and every game the area requests open (below) share it, so it bounds them together, however many hak lists are asked for. It never changes an answer, a picture or a key. What a request is drawing is not counted. See the README's "Memory".

Lines

  • When it is ready, serve writes {"ready": true}.
  • Then each line on stdin is one request, a JSON object, and gets exactly one line on stdout, its answer, in the same order. Empty lines are skipped.
  • A request that fails answers {"error": "..."}, and serve goes on with the next one.
  • It ends when stdin ends.

Paths in requests are the server's own (absolute paths are safest). Files are written whole: to a temporary name, then renamed.

Character requests (need a GPU or Mesa lavapipe)

Without a GPU or a software adapter, these answer with an error; the placeable and area requests below draw nothing.

A look: what a character is made of, and every file a viewer needs to show it that the folder does not have yet.

{"in": "/path/to/character.bic", "dir": "/path/to/parts", "anim": "pause1", "t": 0}
  • in: a .bic or .utc. dir: the parts folder, shared by every character: chunk/<id> (a part's meshes, see nwn_bake::look), tex/<name>.png (plain textures), plt/<name>.png (PLT textures), palette/<name>.png. A file there is never changed: it is named for what is in it.
  • anim (default pause1) and t (seconds, default 0): the pose.
  • items (a folder) and strict_items (default false): where the items a .utc names by resref are looked up first, as the --items and --strict-items options do (see below).
  • The answer is the look (nwn_bake::look::Look): anim, t, base and parts, each part with its id, model, attachment, scale, textures, colours and whether it is animated, and missing_items.

Equipped items. A .bic carries the items it wears; a .utc names them by resref. Each is looked up in the request's items folder first (<resref>.uti, or nwn_gff's <resref>.uti.json, in it or its subfolders), then in the user folder's override and haks, then in the game. The folder is read again for every request, so a new item file is seen at once. An item found nowhere is left off the creature and listed in the answer's missing_items, one object for each, in the order of the blueprint's Equip_ItemList: {"slot": "lefthand", "resref": "nw_ashsw001"}, with "error" too when a file was found that does not read. The slot is named as the game's INVENTORY_SLOT_* constants name it, lower case (head, chest, righthand...). Every slot is checked, those a picture does not show (rings, boots) too. With "strict_items": true, a missing item fails the request instead ({"error": "equipped items not found: nw_ashsw001 (lefthand)"}). in may be nwn_gff's JSON (.utc.json).

A picture of a creature:

{"in": "/path/to/creature.utc", "thumb": "/path/to/out.png", "size": 256, "anim": "pause1", "t": 0, "yaw": 60}

Answers {"ok": true, "missing": [models not found], "missing_items": [...]}; a model the game has that does not read is listed as name: why, and missing_items is as for a look. items and strict_items are as for a look. size is the side in pixels (default 256), yaw the turn in degrees (default 60).

A picture of an item on its own: as a creature's, with "item": true and in a .uti. size is the width, height the height (default size), yaw default 0.

A picture of a placeable:

{"placeable": 42, "thumb": "/path/to/out.png", "size": 256, "height": 256, "yaw": 0, "textures": [["old", "new"]]}

placeable is a row of placeables.2da; textures replaces textures by name, as ReplaceObjectTexture does. Answers {"ok": true, "missing": [...], "particles": true|false}.

Several objects in one picture:

{"scene": {"objects": [{"placeable": 42}, {"creature": "/path/to/a.utc", "position": [2, 0, 0]}]}, "out": "/path/to/out.png"}

scene is a scene's spec (scene.md), out the PNG to write (a turntable's animated WebP, its frames also as PNGs in frames_dir when given); items and strict_items are as for a look. Answers what the scene command prints (the camera, the box round what is drawn, the points' pixel positions, each object's box, missing models and missing_items, and a turntable's frames), with "ok": true.

Placeable requests (no GPU needed)

These read the game's data and draw nothing, so they work on a machine with no GPU and no software adapter.

A placeable's textures: {"textures_of": 42} answers {"model": "...", "textures": [...]} (nwn_bake::model_textures: each texture's name, the image it means, its kind and how many triangles use it).

Every placeable: {"placeables_index": "/path/to/placeables.json"} writes every placeables.2da row with a model, described (nwn_bake::placeables_index), to that file and answers {"ok": true, "rows": n}.

Area requests (no GPU needed)

{"area_iso": {"module": "/data/mymodule", "area": "village", "views": ["sw", "se", "ne", "nw", "top"], "out": "/data/area-iso",
              "cache": "/data/area-iso-cache"}}
{"area_iso_key": {"module": "/data/mymodule", "area": "village", "views": ["sw"], "out": "/data/area-iso", "cache": "/data/area-iso-cache"}}

Instead of module (anything area-iso takes) and area, a request may give the area's files: are and git (paths, binary or JSON; git defaults to the one beside the .are), with haks and tlk (else those of the nearest module.ifo above the area). The options are area-iso's, by the same names (but quality, the supersampling, which is --supersample there) and with the same defaults: views (a list, or "all,top"), scale, quality, light, shadows (true or false), roof ("auto", "keep", metres, or "ground:2"), exposure, objects, background, depth, format, look (the constants that differ, as an object), region ([x0, y0, x1, y1]), marks (a list of strings as --mark takes them, "41.2,17.5,#ff8000"), layout (tree, the default, or flat), force and strict. Unknown fields are an error.

  • area_iso checks, and draws when it must, as a batch does, the views each on its own thread; it answers the area's manifest (area-iso.md), with action (current, cached or rendered), manifest_path and seconds (check, scene, render, write: what each part took, 0 for a part that did not run) added. It does not touch manifest.jsonl: a line holding the manifest (as answered) can be appended to it, and a reader of manifest.jsonl takes such a line as one line per view.
  • area_iso_key answers {"area", "quick_key", "key", "resources", "cached", "current"}: whether the cache, and the output folder when out is given, have pictures of that exact key. It builds the scene but draws nothing. A program can use it after a module or hak change to find, cheaply, which areas need drawing again.

Areas in 3D: area_3d bakes one or more areas as area-3d does (area-3d.md), to the same files and keys.

{"area_3d": {"module": "/data/mymodule", "areas": ["village", "mine"], "out": "/data/area-3d", "cache": "/data/area-3d-cache"}}

module is anything area-3d takes; areas the resrefs (left out for a lone area file), or "all": true for every area of the module. The options are area-3d's, by the same names with _ for -, and with the same defaults: tex_dir (<out>/tex), tex_uri (tex_dir relative to out), max_tex, quality, objects, cache, force, strict, haks and tlk. Unknown fields are an error. It answers {"areas": [...], "textures_written": n}, each area as area-3d's line on stdout (area, action, error, key, manifest...). An area that fails is in the list with action failed; the request does not fail for it. The areas are baked several at a time, as many as area-3d's default --jobs.

A module, as the app shows it: module_info answers what NWN Bake's first screen shows (the desktop app reads the same, through nwn_bake::module_info).

{"module_info": {"module": "/data/mymodule.mod"}}

module is anything area-iso takes; haks and tlk replace the module's own. The answer is path, haks (each name and whether it was found, in the module's order), tlk (the talk table the module names) and tlk_path (the file read), areas (each resref, name, tileset, tiles (width and height), kind (surface, interior or underground, from the .are's flags), links (its doors and area transitions with a LinkedTo: kind, tag, name, at in metres, linked_to, and to_area, the area with an object of that tag) and error when it did not read), and warnings.

A site: site writes the static site of a module (NWN Bake's Publish screen) into out, over the pictures area_iso requests have written there (laid out as a tree): manifest.jsonl and index.json as area-iso --all writes them, index.html (every area) and areas/<area>.html (each area's views and transitions), site.css and site.js.

{"site": {"module": "/data/mymodule.mod", "out": "/data/site", "views": ["sw", "se", "ne", "nw", "top"], "failed": {"mine": "why"}}}

title (default: the module's file name), views (those drawn) and failed (the areas that failed, and why) are optional; an area with no manifest in out is listed as failed. It answers {"ok": true, "pages": n}.

For areas, the game is opened once for each hak list and talk table (at most four kept), all of them within the one cache budget (--cache-mb), and the haks' digests are kept; the module is read again for each request, so a change to its files is seen at once (a nasher project is listed, well under a second for one of 1,300 areas, and only the area's two files are read).

One request a run: wasi-request

nwn-bake wasi-request [REQUEST] [ANSWER] [--root <game install>] [--user <user folder>] [--tlk <name or path>] [--module <module>]

The browser build's entry point (docs/DESIGN-app.md, decision 6), not listed in --help: the page runs nwn-bake once a job. It reads one request, as serve takes them, whole from the file REQUEST (else stdin), and writes its answer, one line, to the file ANSWER (else stdout; a file keeps it apart from anything else the program prints). The options are serve's. Only what the request needs is opened: an area request opens no character game and looks for no GPU. A request that fails is answered {"error": "..."} too, and the exit code is then 1.

REQUEST may also be a JSON array of requests: they are answered in turn by one baker, as serve would answer them (each game opened once for them all, the haks' digests kept), and the answer is one line, the array of their answers. A request in it that fails is answered {"error": "..."} in its place; the exit code is 0. The browser's page sends an atlas's areas this way, a few at a time: opening the game for each area would take longer than checking that its pictures are current.

Built for WASI (scripts/build-wasm.sh: wasm32-wasip1, without the creature feature), the requests that need no GPU work: area_iso, area_iso_key, area_3d, module_info, site, textures_of and placeables_index; the others answer that this build draws no characters, items, placeables or scenes. Under WASI the game is only where --root (and the user folder only where --user) says, in a folder the host has opened for it: no install is looked for. There are no threads (views and areas are done in turn), no memory maps and no process id. The pictures, .glb files, floors, textures, manifests and keys are byte for byte the native build's, but for one thing: the quick key holds the game files' times, and browser_wasi_shim 0.4.2 gives every file the time 0, so a quick key made in the browser differs (the exact key does not). tests/wasi-browser/ is a test page that runs it in a Worker, and run.mjs there the proof in headless Chromium. NWN Bake's page runs it the same way in a browser (app/ui/js/worker.js; scripts/pack-browser.sh packs the page with it, and tests/wasi-browser/app.mjs drives that page as a visitor would).

Versions

The answers are the documented formats (the look, the manifest), and fields may be added to them; readers ignore fields they do not know. A request field that is not known is an error, and so is a request with none of in, textures_of, placeables_index, placeable (with thumb), scene, area_iso, area_iso_key, area_3d, module_info or site.