# Writing plugins

## Adding a plugin

List package names or folders in `plugins` in the project's `liminalia.json`.

```json
{ "plugins": ["./plugins/stamp"] }
```

You can also list folders outside the project (`../my-plugin` or an absolute path). On a computer that doesn't have that folder (another Mac),
the plugin is skipped without being loaded (you're told once, and `lim check`, `lim build` and Studio keep going). Folders inside the project and package names stop with an error if they're missing.

A plugin that can't be loaded (the package isn't found, the folder or `package.json` is missing, loading `./commands` throws, or there's no default export)
stops `lim check`, `lim build` and so on with the one sentence "Can't load plugin X (reason)". `lim studio` and Studio open without that plugin and
show the same sentence in the bar at the top and in the "Plugins" category of the check. That plugin's commands become undefined, so findings about undefined commands are folded into this diagnostic
(the preview's player also skips the entry points it can't load and runs with the rest). Once it's fixed, reopen with "Refresh".

Put the following entry points in `exports` in the package's `package.json` (all optional).

| Entry point | default export | Loaded by |
|---|---|---|
| `./commands` | `CommandPlugin` (`defineCommandPlugin` from `@liminalia/sdk`) | The check, running without a screen, import, export and the player (loaded in both Node and the browser) |
| `./web` | `WebPlugin` (`defineWebPlugin` from `@liminalia/sdk/web`) | The player only (browser) |
| `./import` | `TyranoImportProfile` (`@liminalia/sdk/import`) | Only `lim import`, `lim backport`, `lim reference` and the dev server's sprite position editor (`writeCast`) (Node) |
| `./studio` | `StudioPlugin` (`defineStudioPlugin` from `@liminalia/sdk/studio`) | Studio (Liminalia Studio) only (browser). Adds project-specific screens to the "Plugins" tab |
| `./studio-server` | `StudioServerPlugin` (`defineStudioServerPlugin` from `@liminalia/sdk/studio-server`) | Studio's dev server only (Node). Adds APIs that the `./studio` screens call ([The dev server side](../plugins.md#開発サーバー側の口studio-server)) |

Anything related to importing from TyranoScript (.ks tag names, conversion, imports of `@liminalia/sdk/import`) goes only in `./import`.
`./commands` and `./web` are included in the exported game, so they don't import the import engine (`@liminalia/ks-import`)
(if they do, `lim build` stops and shows where it's imported from).

Integration with desktop stores (GOG Galaxy, Epic Games Store and so on) doesn't use these entry points. Instead, you build it in with a CommonJS file set in `build.store.desktop.module` in `liminalia.json`
(it runs in the main process of the desktop version; see “Other stores” in [distribution.md](../distribution.md)).

The standard commands (`@liminalia/std`) are always loaded first, without being listed. When two commands have the same name, the one from the later plugin wins.

**Declaring compatible engines**: the plugin interfaces (`CommandPlugin`, `WebPlugin` and what's passed to their `api`) have a version (`PLUGIN_API_VERSION`, readable from `@liminalia/sdk`; currently 8. Version 8 added the `@liminalia/sdk` entry points).
If you write the interface version the plugin needs in its `package.json` as `"liminalia": { "api": 2 }` or `"engines": { "liminalia": ">=2" }` (`>=`, `<`, `^`, `2 - 3` and `||` are understood),
`lim check` and Studio's check warn when this engine doesn't meet it (`plugin-engine-version`). You can also write it in `apiVersion` of `defineCommandPlugin` and `defineWebPlugin`
(on the command side, `lim check` reports it with `plugin-api-version`; on the rendering side, it's shown in the developer tools of the game screen).
`engines.liminalia`, written the same way, can also go in a theme's `theme.json` ("Themes" in [player.md](../player.md)) and in the `liminalia.json` of games and templates.
All of them are compared with the same interface version, and `lim check` warns when they don't match (`theme-engine-version`, `engine-version`). When you create a game from a template someone shares with `lim new <folder> --template <template>`,
`lim new` also tells you before creating it (the template's `engines` and `format` stay in the created game). The interface version also goes up when items are added to what themes can write in `ui` or to the game's settings.
Studio's "New plugin" and "Save the current look as a theme" write `engines.liminalia` from the current interface version (such as `">=2"`).

### The package plugins import (`@liminalia/sdk`)

Plugins import only `@liminalia/sdk` (MIT), the plugin contract, and its entry points.

| Entry point | Contents |
|---|---|
| `@liminalia/sdk` | Command plugins (`defineCommandPlugin`, `t`, `d`, `defineState`, `withEnglish`, `argOrigin`, `PLUGIN_API_VERSION`) and types such as `Cast`, `Value` and `StageState` |
| `@liminalia/sdk/std` | Parts of the standard commands (`leaveParam`, `presentLeaving`) |
| `@liminalia/sdk/web` | Rendering plugins (`defineWebPlugin`, `ui`, `systemText`, `dataText`, `loadScript`, `registerLayerItem`, `ChoiceHistory` and more) and their types |
| `@liminalia/sdk/studio` | Studio plugins (`defineStudioPlugin` and the screen parts) and their types |
| `@liminalia/sdk/studio-server` | Dev server plugins (`defineStudioServerPlugin`, `StudioServerError`, `findRunningStudio`) and their types |
| `@liminalia/sdk/import` | Types for import profiles (`TyranoImportProfile` and more) and `castFromTatieSettings` |

The sdk holds only the contract (types and small functions). Anything that needs an implementation, such as the rendering parts, calls the implementation of the running `lim` or Studio app.
Plugins run inside `lim` and the Studio app, so they work without the sdk installed in the game folder (even if it's installed, the implementation of `lim` and the Studio app is used when they run).
When you write a plugin in a separate repository, put `@liminalia/sdk` in devDependencies and you get the types with your own tsconfig.
To parse and check scenarios and run them without a screen in tests, also put `@liminalia/cli` in devDependencies and import from its test entry point `@liminalia/cli/testing` (`parse`, `check`, `createRegistry`,
`CORE_COMMANDS`, `Runtime`, `HeadlessHost`, `combinePlugins`, `runPluginChecks`, `checkPluginManifest`, `convert`, `resolveImportProfile`).

Names that plugins from earlier versions import, such as `@liminalia/web`, `@liminalia/lang` and `@liminalia/studio/plugin`, still work inside `lim` and the Studio app.
They are part of `lim`, though, and aren't published on npm, so to get the types, change them to the `@liminalia/sdk` entry points.

You can also make a game from a template URL (`lim new <folder> --from github:owner/repo#tag`, `--from https://…/template.zip`, or an https Git URL).
After fetching, it's made the same way as with `--template` (Git fetches shallowly, without submodules or the repository's hooks; unencrypted `http://` is accepted only for localhost).
If the fetched template has code that runs when the project is opened, such as plugins or a signing command, it shows what runs and tells you once to check the contents before opening it if you don't know who made it.

### Opening in restricted mode

Plugins run just by opening a project, and they can read and write files on that computer.
The Studio app and `lim` always run the project's plugins. Each tool handles it as follows (`packages/project/src/trust.ts`).

| Tool | How it's handled |
|---|---|
| `lim` | With `--restricted` (accepted by every command; the same as `LIMINALIA_RESTRICTED=1`) it's restricted |
| VS Code | Follows workspace trust. In an untrusted workspace, plugin code doesn't run, and Play from Here doesn't run the `lim` inside the project either (the `lim studio` it starts is restricted). The `liminalia.lim`, `liminalia.node` and `liminalia.studioApp` settings don't use workspace values until you trust it |
| Browser version | Asks whether to run plugins the first time you open each folder on a PC (see [studio-mobile.md](../studio-mobile.md)). Connecting from a phone to desktop Studio uses the desktop version's plugins |

What counts as code that runs: plugins in `plugins` and `import.tyrano.profile` (shown with their entry points), the Windows signing command (`build.windows.sign.command`) and
the command that wraps the executable (`build.windows.wrap`), and the desktop store module (`build.store.desktop.module`; it runs in the check after exporting).

When restricted, the project's plugins and import profile aren't loaded (checks use only the standard commands, and the preview doesn't show plugin rendering), and exporting stops.
Studio shows "The project is open in restricted mode, so it was opened without running its plugins: …" in the banner at the top.

## Official plugins

"Official plugins" in the "Plugins" tab of Studio lists the plugins bundled with the app. "Install" copies a plugin into `plugins/<id>` in the project and
adds `./plugins/<id>` to `plugins` in `liminalia.json` (npm isn't used; it takes effect after a restart). For now there are three: AI consultation (`ai`, from the separate liminalia-ai repository),
Live2D (`live2d`) and Spine (`spine`) (Live2D and Spine are in `plugins/` in this repository. How to use them, and the runtimes authors get themselves, are in [live2d-spine.md](../live2d-spine.md)).

The list is `catalog.json`, looked up in this order (`packages/studio/src/server/catalog.ts`).

1. The folder in the `LIMINALIA_PLUGIN_CATALOG` environment variable (the desktop version passes the app's `Resources/plugins`, or `resources/plugins` on Windows.
   `lim` installed from npm passes the `plugins/` inside the cli package when it isn't set)
2. `catalog/` in the repository (for `lim dev` and `lim studio` run from the repository)

```json
{
  "plugins": [
    {
      "id": "ai",
      "name": "AI 相談",
      "nameEn": "AI consultation",
      "description": "…",
      "descriptionEn": "…",
      "folder": "../../liminalia-ai",
      "folderEnv": "LIMINALIA_AI_DIR"
    }
  ]
}
```

| Field | Contents |
|---|---|
| `id` | The name of the `plugins/<id>` folder in the project (starts with a lowercase letter; lowercase letters, digits and `-`) |
| `name`, `nameEn`, `description`, `descriptionEn` | The name and description shown in the list (chosen by Studio's language; `name` and `description` are the Japanese ones) |
| `folder` | The plugin's folder. A path relative to the folder containing `catalog.json`, or an absolute path |
| `folderEnv` | If set, the folder in this environment variable is used before `folder` |
| `version`, `entries` | If left out, they're taken from `version` and `exports` in the folder's `package.json` |

The AI consultation source isn't kept in this repository, and the repository's `catalog/catalog.json` points to a separate repository placed next to it. Live2D and Spine point to `plugins/live2d` and `plugins/spine` in this repository.
and `npm run studio:package`, which builds the desktop version, copies them from there into the app's `Resources/plugins/<id>` (without `node_modules`, `.git` and `test`).
In the `catalog.json` included in the app, `folder` is rewritten to the name of the copied folder, and `version` and `entries` to the values worked out.
AI consultation is copied from `LIMINALIA_AI_DIR`, or from `../liminalia-ai` next to the repository. If it isn't found, a warning is shown and it's left out (the app still builds).
The npm package (`scripts/npm-pack.mjs`) puts them in the cli package's `plugins/` in the same shape, but only the ones in this repository (Live2D and Spine).
AI consultation, which points outside the repository, isn't included (users can pass their own list folder with `LIMINALIA_PLUGIN_CATALOG`).
Bundled plugins can't have dependencies (`dependencies`; if they do, the build stops). For `@liminalia/…`, import only types, or use the app's own.

Once installed in a project, it's the same as any other plugin in the project (you can edit its name and description, and open its folder). If the bundled version is newer than the installed version
(`version` in `plugins/<id>/package.json`), "Update to <version>" appears. Reinstalling keeps the previous contents in `.liminalia/backups/plugins/`.
If `plugins/<id>` holds a different package (one with a different `name`), it isn't installed.
