# Liminalia Studio

## Reloading and saving

When project data (.json in `paths.data`) or plugin code changes and only the player reads it, the dev server doesn't reload the whole Studio;
it tells only the player (the preview) to reload (`liminalia:player-reload`), and the preview shows the same line again after reconnecting. When code loaded from Studio's entry point
(such as a plugin's `./studio`) changes, Studio reloads too, as Vite does by default.

**Refresh** reloads a project that was changed outside Studio (another editor, an AI, git and so on). It saves the changes waiting to be saved first, then with `POST /reload`
has the dev server reload the scripts, characters and assets, and fetches the table of contents, the open file (keeping its undo history), the current position data, the duration bars, the preview and the syntax check again.
Changes to scripts, `cast.json` and the asset folders are watched all the time and reloaded automatically, so use Refresh for changes the watcher doesn't detect (such as after a mass rewrite) or
when you want to be sure everything is up to date. If the project settings (`liminalia.json`) or the code of command plugins (.ts and .js in the `./commands` folder) changed,
plugins already loaded by Node can't be swapped, so `POST /reload` returns `{ ok: false, restart }`. The desktop app's client (opened with `?desktop=1`)
tries to go to `STUDIO_RESTART` (`?file=&line=`), and the app catches it, restarts the dev server and reopens the same line of the same file
(the test play window closes). In a browser (`lim studio`), it tells you to restart.

**Outside changes and screens**: the Characters, Assets, Flowchart, Variables, Game settings, Game screens, Project (including Translation), Plugins, Voices and Comments screens
redraw with the new content when a file changes outside while they are open (another app, AI, another window; if you are typing in a field, after you leave it).
Saving from a screen that still shows the old content doesn't overwrite the outside change: Characters and Voices write with the version they read and, if it changed, don't save but reload and tell you.
Game settings (including command defaults), the parts panel in Game screens, Project and Plugins send the hash they read and, if it changed, merge item by item before saving.
Variables, Assets and Comments write only what you changed. Which screen shows which files and how it handles them is the list in `client/view-sources.ts`; add new screens there
(`test/view-sources.test.ts` fails for screens that aren't listed, and for screens whose outside changes and saves with old content aren't checked).
The dev server also starts watching folders that didn't exist when it opened (`reviews/`, `lang/`, the data and asset folders) once they are created.

**When the same project is open in two windows**: each window sends its id to the dev server in a header (`x-liminalia-window`), and when a script is saved, created,
renamed or moved to the trash, the dev server tells every window with `file-changed` and the like, adding the id of the window that saved it (`by`). The window that saved skips its own notice,
and the other windows load it the same way as a change made in another app (merging if you're in the middle of typing). Project data (`data-changed`) works the same way.

**When saving fails**: if Studio's server can't be reached, a bar appears at the top with how to fix it (restart `lim studio`; in the desktop app, reopen the project) and
"Save again" (it also retries automatically). When saving is refused (4xx, 5xx), the reason is shown once in a notice. Each time saving fails, the content waiting to be saved is
backed up in localStorage (`unsaved-backup.ts`; per project id and file), and the backup is removed once saving succeeds. When you reopen the same project and open that file,
if the disk is still as it was then, the content is restored from the backup with "Undo," and if it changed elsewhere, the notice shows a "Restore from backup" button
(once restored, the backup is removed and the restored content waits to be saved as usual). When "Continue Without Saving" or "Close Without Saving" (the browser's "Leave") is chosen
in the confirmation before leaving, the changes were deliberately thrown away, so the backup is removed too (when `pagehide` comes after `beforeunload` stopped). Reopening in another process
can let the new page read first, so a backup kept after the confirmation was shown gets a mark (`left`); on reopening it isn't restored automatically,
and "Restore from backup" in the notice can restore it once (the mark is removed if you cancel the confirmation and keep writing).

**Plugins that can't be loaded**: `lim studio` and Studio open without plugins that can't be loaded (the package isn't found, the folder or `package.json` is missing,
loading `./commands` throws), and show a bar at the top ("Opened without plugins that can't be loaded: X (reason)" and "View in Check").
The "Plugins" category of the check shows a `plugin-load` diagnostic ("Can't load plugin X (reason)"), and findings about undefined commands that may belong to that plugin
are folded into one ("Adding a plugin" in [plugins.md](../plugins.md)). The reason isn't Node's raw message but one of: the package isn't found, the folder is missing,
`package.json` is missing, the entry file is missing, a module it loads isn't found (npm install), or a syntax mistake (`pluginErrorReason` in `@liminalia/project`),
and it takes the same form when `lim check` and others load plugins without leaving them out, and when the dev server side's entry (`./studio-server`) can't be loaded.

**Confirm before leaving**: only when the open script has changes that are unsaved, being saved, failed to save or in conflict (`FileSync.unsaved`), closing, reloading
and switching projects (the phone app's "Open another project" button) are stopped first (`beforeunload`; the browser's confirmation appears, and saving starts right away when it stops). If everything is saved, nothing stops.
Other files aren't checked, because Studio doesn't switch files unless it can save before switching. For "Open another project" in the phone app, the confirmation may not appear in an app added to the home screen,
so it saves the changes waiting to be saved first, and if it can't save, it doesn't move and shows the reason. In the desktop app, closing a window goes through the existing save before closing (if it can't save,
it asks about "Close Without Saving") and then closes; leaving the page by reloading or similar makes the app show a confirmation dialog ("Cancel," "Continue Without Saving") through `will-prevent-unload`,
so Electron doesn't silently stop (`packages/studio-desktop/src/main/unload-guard.ts`; once you decide to close, it doesn't ask twice).
Test play, export, switching the language and asking the app to restart (`STUDIO_PLAY` and so on; the page doesn't leave) aren't stopped.
How to check it: `npx tsx packages/studio-desktop/scripts/nav-check.ts [--shots folder]` (it also checks the test play window of "Play from Here").
