# Writing plugins

## Import (`./import`)

Settings for importing a TyranoScript project with `lim import`. `lim import` uses the built-in conversion table for the standard commands,
with each plugin's `./import` (and `./import` of the package that `import.tyrano.profile` points to) laid over it in the order of `plugins`.
Command specs (parameter types, unnamed parameters, defaults) are looked up from the command tables in `./commands`, so here you write only how they correspond to .ks.

```ts
const profile: TyranoImportProfile = {
  mappings: [
    // turn [stamp_show storage=… x=…] into @stamp; storage in .ks is image in @stamp
    { command: "stamp", tags: ["stamp_show"], params: { image: "storage" } },
    { command: "stamp-clear", tags: ["stamp_clear"] },
  ],
};
export default profile;
```

- `mappings` (an array of `TagMapping`): the conversion table from .ks tags to commands.
  - `command` is the name of the command to convert to, and `tags` are the names of the .ks tags to convert from.
  - `params` maps ".lim parameter name → .ks parameter name". Parameters you don't list use their name with `-` turned into `_`, and `null` means there's no counterpart in .ks.
  - If you don't write anything else, the conversion is mechanical (of the values written in the .ks, only those that differ from the command's defaults are written out, and parameters that weren't converted are left in the diagnostics).
  - When mechanical conversion isn't enough, use `convert(pm, tag, ctx)`. `ctx.spec` is the command's spec, `ctx.mechanical(pm)` is for converting mechanically after tidying the values,
    and `ctx.convertAs(command, pm, tag)` is for handing off to another conversion table. Returning null falls back to mechanical conversion.
  - For forms you want to leave to the built-in conversion, use `defer(pm)` (to turn only certain destinations of `[jump]` into your own command, for example);
    list .ks parameters not to convert in `ignore`; and use `swallowStop` to remove the `[s]` right after.
- `scriptRules` / `scriptProfile`: replace JS in `[iscript]` with commands (return the replacement .lim lines, or null if the rule doesn't apply)
- `unsupportedTags`: tags that aren't handled, and why (importing them gives an error with the reason, and they're listed in `lim reference` too)
- `tags`: conversions for tags that can't be written in the table (tag name → `(pm) => TagConversion | null`; takes precedence over the table)
- `plugins`: TyranoScript plugins whose work is taken over (they aren't reported as manual work even if they have JS)
- `skipFiles`: .ks files not to convert as story text (ones the system screens take care of, such as the title screen)
- `afterImport(ctx)`: writes out project-specific data (JSON under `data/`) and returns the character definitions
  (if the project keeps settings in the same form as Tyrano Tatie under another name, `castFromTatieSettings` can turn them into character definitions)
- `writeCast(ctx)`: writes character definitions saved in the sprite position editor back to the import source
- `backport`: for writing back (`lim backport`), the reverse conversion that turns command lines back into .ks tags (command name → `(line, originalTag) => tag text | null`).
  Writing back finds parameters by rewriting them with the conversion table and the original tag as a model, so you usually don't need this. Use it for commands that can't be turned back from a model alone, such as conversions written with `tags`
  ([tyrano-backport.md](../tyrano-backport.md))

The conversion table for the standard commands (`@image` and so on) is `STD_MAPPINGS` in the engine (`packages/ks-import/src/std-tags` in the public repository).
You can read it as an example of how to write conversion tables.
