# Distribution and updates

## Updates (desktop versions sold outside Steam)

Steam delivers updates itself, but when you sell on your own site, on BOOTH or elsewhere, the game checks for new versions on its own.

1. Make the files to distribute and the manifest
   ```bash
   lim update-manifest --base-url https://example.com/game/updates/ --pack packs/patch1 --notes-file notes.json
   lim update-manifest --base-url https://example.com/game/updates/ --app windows=dist/windows/…/Game_1.1.0_Setup.exe,mac=dist/Game-mac.zip --page https://example.com/game
   ```
   This creates `update.json` and the files to distribute (packs as `<id>-<version>.zip`, and copies of the game files) in `dist/update/`. Put its contents at the `--base-url` location
2. Write `"build": { "update": { "url": "https://example.com/game/updates/update.json" } }` in `liminalia.json` and export
   (the environment variable `LIMINALIA_UPDATE_URL` takes precedence; `off` leaves it out)

For how the game behaves (how it asks the player and what it shows), see [Updates in player.md](../player.md).

- Packs (`--pack`): a project's `packs/<id>/` (exported the same way as `lim pack` first), a folder exported with `lim pack`, or its zip. Whether a pack is newer is decided by `version` in `pack.json`,
  so `version` is required. `requires` (which version of the game it goes into) is `requires` in `pack.json`, or the current `version` if there's none
- The game itself (`--app`): an installer or zip for each OS (`windows`, `mac`, `linux`) (the Mac zip and dmg can be made as described in "Packaging" above). `--page` is the download page (opened when there's no file for the OS).
  The version is `version` in `liminalia.json` (`--version` overrides it)
- Release notes go in `--notes "…"` or `--notes-file` (text, or JSON such as `{"ja": "…", "en": "…"}`; the one in the player's language is shown)
- If `dist/update/update.json` already exists, packs for other game versions that weren't made this time are kept (so you can still deliver fixes to players of older versions). `--fresh` doesn't keep them
- It isn't included in the Steam-connected version (it is with `build.update.steam: true`). The automatic check after export doesn't test it

The manifest looks like this:

```json
{
  "format": "liminalia-update", "formatVersion": 1,
  "app": { "version": "1.1.0", "page": "https://example.com/game", "notes": { "ja": "…", "en": "…" },
           "files": { "windows": { "url": "Game_1.1.0_Setup.exe", "sha256": "…", "size": 123 } } },
  "packs": [ { "id": "patch1", "version": "1.0.1", "requires": "1.0.0", "title": "Fix 1", "url": "patch1-1.0.1.zip", "sha256": "…", "size": 456 } ]
}
```

- URLs can be relative to the manifest. Only https is allowed (`http://localhost` is accepted for local testing). `sha256` is required, and a download that doesn't match isn't used
- Packs are installed in `packs/<id>/` in the save location (because the inside of a Mac app and Program Files can't be modified, due to signatures and permissions). A pack is extracted under a different name and then swapped in,
  so if the process stops partway, the previous one remains. Inside the zip, `pack.json` must be at the top level or inside a single folder, and its `id` must match the manifest
- To update the game itself on Windows, a `.exe` or `.msi` is downloaded and launched, and the game quits (the installer removes the previous version and installs the new one)
- On Mac with a `.zip` (made with `build.mac.zip` or `lim build mac --zip`) and on Linux with a `.tar.gz` (made by `lim build linux`), the game replaces itself
  (`packages/desktop/app-swap.cjs`; the same flow as Studio's updates)
  1. It downloads the file, checks `sha256`, and extracts it into `.<name>.update/` next to the app (on Linux, next to the game folder)
  2. It checks that it's the same game at the expected version (on Mac, `CFBundleIdentifier` and `CFBundleShortVersionString` and `codesign --verify --deep --strict`,
     and if the current app has a Developer ID signature, only a signature from the same Team is accepted. On Linux, the executable's name, and the name and version in `package.json` inside `resources/app.asar`)
  3. The game quits (after the autosave and saving it does before closing), and a small sh script swaps the app and launches it again. The script doesn't delete the previous version; it waits up to 120 seconds for a marker showing that the new version started.
     If the marker appears, it deletes the previous version. If not, it stops the new version, goes back to the previous version and launches it (the script's log is in `liminalia-update-<id>/` in the temporary folder).
     The new version places the marker once the title screen (or the game, for a project without one) appears, so a version that starts but never shows the game is rolled back too
  4. On Linux, the whole folder is replaced, so things that exist only in the old folder (the save data `savedata` next to the executable when `saves.location` is `"portable"`, `resources/packs` the player added, and so on)
     are moved to the new folder (and moved back when reverting to the previous version). The new version is launched with the same arguments as the current game (such as `--no-sandbox` on systems where the sandbox can't be used).
     On Mac, the save data next to the app (`<title> savedata`) is outside the app, so it isn't touched
- When the app can't be replaced (it's in a location that can't be written to, Gatekeeper is running it from a moved location, or the file isn't a `.zip` on a Mac (such as a `.dmg`) or a `.tar.gz` or `.tgz` on Linux), the downloaded file is opened as before.
  If there's no file and only a page, the download page is opened

### Signing the manifest (Ed25519)

So that a fake manifest can't make the game install other files even if the update server (or something along the way) is taken over, you can sign the manifest and put the public key in the game.

1. Make a key pair (only once)
   ```bash
   lim update-key            # creates the private key at ~/.liminalia/update-keys/<project id>.pem and prints the public key (--out changes the location)
   ```
2. Write the printed public key in `liminalia.json` and export. A game with the public key only uses manifests whose signatures verify
   ```json
   "build": { "update": { "url": "https://example.com/game/updates/update.json", "publicKey": "the public key printed by lim update-key (32 bytes in base64)" } }
   ```
3. Pass the private key when you make the manifest (`--sign-key` or the environment variable `LIMINALIA_UPDATE_SIGN_KEY`)
   ```bash
   lim update-manifest --base-url https://example.com/game/updates/ --pack packs/patch1 --sign-key ~/.liminalia/update-keys/my_game.pem
   ```

- The signature is `"signature": { "alg": "ed25519", "value": "…" }` in the manifest. What's signed is the manifest without `signature`, as JSON with its keys sorted by name
  (`canonicalJson` in `packages/desktop/update.cjs`). Changing whitespace or order still verifies, but changing even one value doesn't
- If `build.update.publicKey` is set but no private key is passed, or the private key doesn't pair with that public key, `lim update-manifest` stops
- Keep the private key out of the project folder and version control (`lim update-key` makes it a file that only you can read). To sign in CI, store the private key as a CI secret,
  write it to a file, and pass that to `LIMINALIA_UPDATE_SIGN_KEY`
- To change keys, distribute a version that lists both the old and new public keys in an array (`"publicKey": ["old", "new"]`) through a manifest signed with the old key, and once it has reached players, sign with the new key.
  If you lose the private key, games with its public key can't be made to accept new manifests (players will have to reinstall the game by hand)
- Packs and game files are still checked with `sha256` as before (there's only one signature, on the manifest)
