# The dialogue and the text

> How the Yarn Spinner port, its conformance suite, the translation tables and the tools that keep them are maintained: what came from where, what holds them to the original, how a new Yarn release is taken in, and how a language is added.

Source: https://omardev29.github.io/rmp-docs/framework/maintaining/dialogue-and-text.html

Two parts of the framework follow something outside it: `rmp::Dialogue` follows Yarn Spinner, whose language moves with its releases, and `rmp::Text` follows CLDR's plural rules and the way each platform names its language. This page is what to know to keep both true: where each piece came from, what holds it to the original, and what to do when the original moves.

## The Yarn Spinner port

`src/rmp/dialogue/` is a port to C++ of [Yarn Spinner](https://github.com/YarnSpinnerTool/YarnSpinner)'s compiler and runtime, which are C# and MIT. It is not a copy and not a binding: there is no ANTLR, no protobuf and no .NET, the whole of it is the standard library, and nothing in it throws. Its licence row in `THIRD_PARTY_LICENSES.md` points at `src/rmp/dialogue/LICENSE-YarnSpinner.md`, which ships in every game's `LICENSES.txt`, and `src/rmp/dialogue/PATCHES.md` says, file by file, which C# it follows and what differs:

| File | Follows |
|---|---|
| `src/rmp/dialogue/yarn_parse.cpp` | the lexer and parser grammars and the indentation lexer — hand-written, a line at a time |
| `src/rmp/dialogue/yarn_compile.cpp` | the compiler, the node-group compiler, the code generator and the type solver |
| `src/rmp/dialogue/yarn_vm.cpp`, `src/rmp/dialogue/yarn_value.cpp` | the virtual machine, the dialogue, the library of functions and the saliency strategies |
| `src/rmp/dialogue/yarn_markup.cpp` | the markup parser, with `select`, `plural` and `ordinal` by the framework's own CLDR table |
| `src/rmp/dialogue/yarn_bytes.cpp` | the compiled program's format: one of ours instead of protobuf, for the embedded build |
| `src/rmp/dialogue/dialogue.cpp` | `rmp::Dialogue` and `load_dialogue()`: the framework's side, translation included |

The one difference in behaviour that is a choice, not a gap: the machine has a budget — a hundred thousand instructions between two things it shows, and a detour stack of 256 nodes — so a script that loops without ever reaching a line stops with an error instead of hanging the game. The case that found it, a node that detours into itself before any line, is in `tests/dialogue_test.cpp`.

## Held to Yarn Spinner's own tests

Yarn Spinner's test cases are vendored, as they are, in `tests/dialogue/yarn/`, with their licence and the commit they were taken at in `tests/dialogue/yarn/COMMIT`. A test case is a `.yarn` script and a test plan that says, step by step, what a run of it must produce — each line, each set of options, which one is chosen, each command — and the scripts that must not compile say so. `tests/yarn_plan_test.cpp` compiles every one and plays every plan; it is part of `unit_test`, so `rmp test` runs it.

Every case passes. A case that does not, after a new Yarn release, is listed in `tests/dialogue/yarn_unsupported.txt` with the reason, and the runner holds that list: a listed case that passes fails the run until its line is taken out. The list only shrinks, and it is empty today.

### Taking in a new Yarn release

1. Replace `tests/dialogue/yarn/` with the new release's test cases, and write its commit in `tests/dialogue/yarn/COMMIT`.
2. Run `rmp test unit`. Each failing case names its script and the step where the run went another way.
3. Read what changed in the C# between the two commits — the grammars, the compiler, the machine — and port it to the file of `src/rmp/dialogue/` that follows it, a test of the framework's own for anything the cases do not show.
4. Write the difference into `src/rmp/dialogue/PATCHES.md` and the licence notice if it changed. What cannot be ported yet goes in `tests/dialogue/yarn_unsupported.txt`, with why.

## The other guards

- **Properties.** `tests/property/dialogue_prop.cpp` compiles and plays scripts made of random Yarn statements, and requires that nothing crashes and nothing runs forever; `tests/property/text_prop.cpp` does the same for any table, any key and any arguments, and holds the number formatting to `std::to_chars`' digits.
- **Fuzzing.** `tests/fuzz/yarn.cpp` feeds the compiler and the machine arbitrary bytes and `tests/fuzz/lang_tsv.cpp` the table reader; their corpora are replayed on every push.
- **Mutation.** The modules `dialogue`, `dialogue_ui` and `text` of `tests/mutation/floors/` are measured every week.
- **The presenters** are laid out headless in `tests/ui_layout_test.cpp` — typing, choices, timers, moods, the stage, the bubble — and rich text's pixels are read back on a GPU in `tests/ui_render_test.cpp`.

## The embedded build

`rmp_yarnc` (`tools/rmp_yarnc.cpp`) is the same compiler built for the machine that builds the game. CMake runs it over every `.yarn` of a game's `resources/` when [`[dialogue] build`](https://omardev29.github.io/rmp-docs/reference/configuration.html#dialogue-build) asks for it, and links the bytes it writes into the game. It is not built when cross-compiling, which is why Android, iOS and the web compile their scripts at runtime. A change to the compiler is a change to both: there is one compiler.

## The text and its tables

`src/rmp/text/text.cpp` is `rmp::Text` and `rmp::lang`: the table reader, the lookup and its fallback chain, placeholders, the CLDR plural rules and the system language of every platform. Three things in it follow the outside world:

- **The plural rules.** The cardinal rules of the shipped languages are a small table in `src/rmp/text/text.cpp`, and `tools/text_tool.py` holds how many forms each language has, to check the tables. Adding a language is a line in both, and a case in `tests/text_test.cpp`'s plural rules; a language in neither takes the last form.
- **The system language.** One branch per platform — the environment, CoreFoundation, `GetUserDefaultLocaleName`, the native activity's configuration, `navigator.language` — and only CI's matrix compiles them all. The tests set the language through a seam, so none depends on the machine's.
- **The codes a folder is searched for.** With no `[lang] languages`, the per-language files are looked for under a list of common codes in `src/rmp/text/text.cpp`.

`tools/text_tool.py` is `rmp text scan` and `rmp text check`: a scanner of C++ literals the framework translates and of Yarn line ids, and a checker of the tables against `[lang] languages`. It writes a Yarn line the way Yarn Spinner's own string table does, its expressions numbered `{0}`, `{1}`, because that is what the machine fills. Its tests, `tests/text_tool_test.py`, plant each mistake the checker is for; `tools/ink_to_yarn.py` has its own in `tests/ink_to_yarn_test.py`, and the story it converts is played by the C++ in `tests/dialogue_test.cpp`.

## Numbers

A number written into a text or a line goes through `shortest_number()`, in `src/rmp/save/number_internal.h`: the fewest significant digits that read back as the same number, laid out as C# lays it out, since Yarn's numbers are C#'s. It does not call a floating-point `std::to_chars` on purpose: that one brings libc++'s formatting tables into every static link, and cost the demo game 126 KB on Linux ([Performance](https://omardev29.github.io/rmp-docs/framework/performance.html#binary)). The property in `tests/property/text_prop.cpp` holds its digits to `to_chars`' on any double and any float.
