# Distribution and updates

## Cleaning up exports

The Mac, Windows, Linux and iOS versions are exported to `<out>/<target>/<name>_<datetime>/`, and the Android version to `<out>/android/<name>_<datetime>.apk`, so they pile up with every export.

```bash
lim clean [project] --keep 2 --dry-run   # only shows what would be deleted and how big it is
lim clean [project] --keep 2             # keeps the newest 2 for each name and deletes the rest (narrow the targets with --target mac,windows)
lim clean [project] --target sourcemaps   # cleans up only the source map copies (when build.sourcemap is "separate")
```

```json
"build": { "keep": 5, "keepSourcemaps": 20 }
```

- Every `lim build` deletes old exports in the target's folder, keeping only the newest `build.keep` of them (the environment variable `LIMINALIA_KEEP` takes precedence).
  If it's left out, 3 are kept. `0` deletes nothing (the default before 2026-11; to keep a distributed version as the base for patches, copy it outside `dist` or set `0`).
  This number is also used when you leave `--keep` out of `lim clean` (1 if it isn't set or is `0`)
- Exports are counted per name before the date and time (exports of an edition with `--variant` have a different name, and Android `.apk` and `.aab` files are also counted separately).
  Items whose names aren't in the date-and-time form, the browser version (overwritten every time), `patches/` and `update/` aren't touched
- Exports from Studio's export screen work the same as `lim build`, so `build.keep` applies to them too
- Source map copies (`<out>/sourcemaps/<name>-<datetime>/`) are also deleted on every export, keeping the newest `build.keepSourcemaps` for each name
  (the same number as `build.keep` if it's left out; `0` deletes nothing). When you leave `--keep` out, `lim clean` uses this number (or `build.keep`'s if it isn't set or is `0`)

## Export records (the game version and the engine version)

Each export records the game version and the version of the engine it used, both in what's exported and in the export folder (the record stays after old exports are cleaned up).

| Where | Contents |
|---|---|
| `project/build.json` in the exported game | `id`, `version` (the game version: `version` in `liminalia.json` or the environment variable `LIMINALIA_VERSION`; empty if neither is set), `engine` (the engine version), `variant`, `feedback` (`true` for a test build) and `date`. It's in the browser, desktop and smartphone versions alike |
| `build-history.jsonl` in the export folder | One line of JSON is added for each export: `date`, `id`, `version` (the game version; `1.0.0`, the version the export used, if it isn't set), `engine`, `targets`, `variant`, `feedback` (`true` for a test build) and `outputs` (what was exported, as paths from the export folder) |

- The engine version is the same number as `lim --version` ([versioning.md](../versioning.md)). The end of the export shows "Game version 1.2.0, engine version 0.2.0"
- The engine version isn't written into the program (`engine/*.js` and `index.html`). Raising the version alone doesn't change the game itself, so `lim patch` can still make a patch
- The export that `lim patch` makes for comparison isn't recorded

## Encrypting assets and scenarios

```json
"build": { "encrypt": true, "browser": { "encrypt": true } }
```

| Setting | What's encrypted | Where it's decrypted | Where the key is stored |
|---|---|---|---|
| `build.encrypt` | The desktop version's assets (inside assets.asar), scenarios, data, translations, characters, flowchart and included packs | The app (when main.cjs serves them over `liminalia://`) | `.liminalia/encrypt-key` |
| `build.browser.encrypt` | The browser version's assets, the game's content and included packs | The Service Worker (`lm-sw.js`; index.html registers it before loading the engine) | `.liminalia/encrypt-key-browser` |
| `lim pack` (`--encrypt desktop`, `browser`, `off`) | The pack's scenarios, data, translations, characters and assets (`pack.json` stays as it is) | The game the pack is installed in | One of the above |

- The format is the magic number `LMEN`, a 16-byte IV and AES-256-CTR content. Files without the magic number are read as they are, so unencrypted packs can be mixed in
- The IV is derived from the key, the file's relative path and a hash of its content (HMAC). Files whose content doesn't change come out the same in every export,
  so delta updates on Steam and itch.io (butler) only send the files you changed. The readers take the IV from the file, so encrypted files from earlier exports still read as before
- By default, `lim pack` uses the desktop key in a project with `build.encrypt`, the browser key if there's only `build.browser.encrypt`, and doesn't encrypt if there's neither.
  When `lim update-manifest --pack packs/<id>` exports a project's pack on the spot, it also encrypts it with the desktop key if `build.encrypt` is set
- There's one key per project, and exports for every OS use the same key. `.liminalia/` is kept out of version control (by the template's `.gitignore`).
  If you lose the key, you can no longer use `lim pack` to make packs that fit the game you've already distributed (`lim patch` still can, because it uses the key inside the previous export). Back up the key like your other secrets
- Anyone can read the browser key from the published page. It's a separate key so that the desktop key can't be read
- The browser version needs a Service Worker for this, so it can only be played on https pages (itch.io and so on) or at `http://localhost` (it can't be opened from file://).
  When Service Workers can't be used, the page shows why. Range requests for audio and video are answered with slices of the decrypted content
- The mobile versions (Android, iOS) aren't encrypted

Limits: the key is inside the distributed game (game.json in the desktop version, lm-sw.js in the browser version). This keeps assets from being seen just by opening the folder,
or from being read directly by other tools, but anyone who takes the trouble to investigate the game can break it. It's no substitute for DRM or copyright protection.

### Playing the browser version offline

So that it can be played offline after being opened once, the browser version caches the engine, scenarios and data with a Service Worker (`lm-sw.js`, the same file as for encryption)
(`build.browser.offline`, default true). Assets are cached when they're used. With `"all"`, all the assets are also cached when the game is opened (mind the download size for large games).
Web fonts loaded by plugins and themes (the `.woff2` `.woff` `.ttf` `.otf` files exported with the engine; they're split into files by the characters they cover, over a thousand in a large game) aren't fetched when the game is opened; they're cached when they're used (fonts in the asset folders are handled like other assets).
With `false`, no Service Worker is registered.
- Unencrypted exports register the Service Worker after the page has loaded (so they can still be played as before from `file://` and non-https locations, where it can't be registered. On localhost, it is registered).
  Encrypted exports register the Service Worker before loading the engine, as before, keep encrypted files encrypted in the cache, and decrypt them when serving them
- The page is fetched from the network first, and the cache is used if there's no response within 3 seconds. Files whose names include a hash of their content (`engine/` and assets with `?v=`) and `project/` (scenarios, data)
  are served from the cache first (the cache is per export, so the engine and the scenarios always come from the same export)
- When you put up a new export while someone is playing, the new Service Worker is installed and waits, and the page being played keeps going with the previous export's engine and scenarios.
  The player sees "A new version is available. Reload to update" and "Reload" at the top of the screen; pressing it autosaves, switches to the new export and reloads
  (closing and opening the game again also switches. The old cache for the same game is deleted when the switch happens). Whether there's a new export is decided by the export time in `project/build.json`,
  checked 30 seconds after starting, when the player comes back to the tab, and every 10 minutes. Exports that don't use a Service Worker (`offline: false` and not encrypted) and exports that are only encrypted
  just show the notice and reload, and until they reload, the scenarios they read come from the new export
- Range requests for audio and video (streaming long BGM) are answered with slices of the cached file. Range requests for assets that aren't cached are fetched from the network as they are, and the whole file is cached in the background
- It isn't included in the export that `lim patch` makes for the comparison
