The Starter Game and Desktop Hosts
In v2 the starter game, the folder people download and turn into their own game, is the repository’s dist/ folder. This page is about maintaining it: what’s in it, how to run it, and the two desktop hosts it carries. Everything here was run at the v2.8.1 tag on Linux, in copies of dist/ (never in dist/ itself, for the reason in Keep dist/ clean).
What is in it, and what you edit
| Path | Who writes it |
|---|---|
index.html, js/options.js, js/storage.js, js/script.js, js/main.js, style/main.css |
you: the sample game and its settings, which every game developer edits |
assets/ |
you: the media folders (each kept with an empty .gitignore) and the app icons in assets/icons/ |
service-worker.js, manifest.json, favicon.ico |
you: the offline cache and the web app manifest |
engine/core/, engine/debug/, types/ |
the build. Never edit them; rebuild (Building and Packaging) |
engine/electron/, engine/electrobun/, engine/desktop/, electrobun.config.ts, package.json, bun.lock |
you: the desktop hosts, the file storage they share, and their dependencies |
serve.ts, build-web.ts, jsconfig.json, jsconfig.node.json |
you: development tools for game developers who have Bun |
A change here reaches every new game, and game developers are told they need no tools (Writing and Publishing the Docs): index.html and js/ must keep working opened in a browser with nothing installed. The Cypress specs load dist/index.html and its js/ files, so a change to js/options.js changes what every spec runs with (turning on Screenshots there broke a spec, see Testing the Engine).
dist/index.html loads, in order: engine/core/monogatari.css, style/main.css, engine/debug/debug.js, engine/core/monogatari.js, then js/options.js, js/storage.js, js/script.js and js/main.js. Its markup is the <visual-novel> element with every screen inside <div id="monogatari">. It sets a Content Security Policy in a <meta> tag (script-src 'self' 'unsafe-eval' blob:, img-src 'self' data: https:…), which forbids inline scripts.
js/main.js sets monogatari.debug.level(5) and calls monogatari.init('#monogatari') when the page is ready. js/options.js sets every setting explicitly; it differs from the engine’s defaults in src/monogatari.ts in a few places: InstantText: true, Orientation: 'landscape', Storage.Adapter: 'IndexedDB', Screenshots: true.
Run the game
Any static server works, and serving is how players get it:
cd dist && python3 -m http.server 8080 # or: bunx serve dist -l 8080, as CI doesOpening dist/index.html from the disk works too, with limits (no service worker, and audio plays without its effects): see What works opened from a file.
The development server
serve.ts is a small Bun server for game developers: bun run serve in dist/ runs bun --hot serve.ts on port 5100 (or PORT). It serves the folder with Cache-Control: no-cache on every file, decodes the path (so my%20room.png finds my room.png) and refuses paths outside the folder.
It reloads the page when a file changes:
- It adds
<script src="/__live-reload.js"></script>before</body>of every HTML page it serves. A script file, not an inline script: the page’s Content Security Policy (script-src-elem blob: 'self') allows scripts from the page’s own address only. Until 2.9.0,serve.tsinjected an inline script, which the policy blocked. - That script opens an
EventSourceon/__live-reload, a stream of server events, whichdefault-src 'self'allows. (2.8.1 used a WebSocket, and itsfetch()returnedundefinedwithout callingserver.upgrade(), so the connection failed as well.) The server keeps the stream open (idleTimeout: 0; Bun closes idle connections after 10 seconds otherwise). - It watches the whole folder recursively, except
node_modules/,build/and.git/, and 100 ms after the last change sends the paths that changed. The page deletes those URLs from every Cache Storage cache, so the service worker can’t answer with its old copy, then reloads. When the stream drops and comes back (the server restarted), the page reloads too.
The service worker leaves requests that accept text/event-stream alone, so the stream isn’t held up by its cache.
The web release
bun run build:web (in dist/, with Bun) runs build-web.ts, which copies assets/, engine/core/, engine/LICENSE, js/, style/, favicon.ico, manifest.json and service-worker.js into build/web/ and writes index.html without the debug module’s <script> tag. Without the debug module no error dialog can appear; js/main.js still sets the debug level, which only changes what goes to the console. The copy includes engine/core/monogatari.js.map (5.4 MB), which browsers only fetch with the developer tools open.
The service worker
When the ServiceWorkers setting is on and the game is served (not from a file, not in Electron or Cordova), setup() registers ./service-worker.js. The worker belongs to the game: an engine update doesn’t replace a game’s copy. Media and Assets describes what it does with each request. When a new worker installs while an old one controls the page, the engine shows a “new content” notice.
The desktop hosts
The starter game carries two hosts: Electron (engine/electron/, the main process in index.js and a preload script) and Electrobun (engine/electrobun/index.ts for the Bun process, view.ts for the webview). Both expose the same small bridge to the page, window.electron or window.electrobun, with send(channel, data) and on(channel, callback), which src/lib/DesktopBridge.ts finds. The engine uses these channels:
| Channel | Sent by | For |
|---|---|---|
window-info-request, answered by window-info-reply |
the settings screen | the window title and, when the window isn’t resizable, the sizes for the resolution selector |
resize-request, answered by resize-reply |
the settings screen | changing the resolution or full screen |
quit-request |
the quit listener the End action registers, when no game is being played |
closing the app |
monogatari:storage-get, -set, -update, -remove, -get-all, -keys, -contains, -clear, answered by monogatari:storage-response |
src/lib/FileSystemStorage.ts, the FileSystem storage adapter |
the game’s data, as files (Storage Engines has the messages) |
Electron’s main process registers its handlers for these channels once, at the top of engine/electron/index.js, and each one acts on the window the message came from (BrowserWindow.fromWebContents(event.sender)). Before 2.9.0 the window channels were registered inside createWindow(), so every window opened again (on macOS, a click on the dock icon after closing the window) added another set, and the game got each answer once more.
Both hosts answer the storage channels with the same module, engine/desktop/storage.js (CommonJS, so Electron’s main process can require it and Electrobun’s bundler can import it): one JSON file per key in <app data folder>/Storage/<name>/<store>/, file names made safe with encodeURIComponent plus . and *, and Windows’ reserved names escaped, written through a temporary file and a rename, one operation after another.
Each host also opens links to websites in the player’s browser: Electron’s preload catches a click on a plain link (without target="_blank") and sends open-external to the main process, which calls shell.openExternal(); target="_blank" links and window.open() go through setWindowOpenHandler. Without that, a plain link would replace the game (or, after a click, ask “Do you want to quit?”, because the settings screen sets window.onbeforeunload in desktop apps). Electrobun’s view script does the same with an open-external message and Utils.openExternal().
How the Electrobun window reaches the game
Electrobun builds view.ts into views/mainview/view.js (an IIFE, format: "iife" in electrobun.config.ts). index.ts reads that file and passes its text, wrapped in a function, as the window’s preload, so it runs before the game’s scripts. Two details matter:
- A
views://address aspreloaddidn’t load the script in Electrobun 1.18 (no RPC request reached the Bun process); the script’s text does. - The view must create an
Electroview(new Electroview({ rpc })), or no RPC request reaches the Bun process, and must never overwritewindow.__electrobun, which Electrobun’s own preload uses.
Its requests are window-info-request, resize-request and storage-request (the storage channels, forwarded with their payload); its messages are quit-request and open-external. The view raises Electrobun’s one-second request limit to ten seconds. window-info-request answers with the primary display’s work area from Screen.getPrimaryDisplay().
What Electrobun 1.18 can’t do: let the app refuse a window close (its close event fires once the window is gone, so the “Do you want to quit?” question can’t appear), or lock a window’s size (there is no resizable option).
Install their dependencies
Work in a copy of dist/:
cp -r dist /tmp/game-desktop
cd /tmp/game-desktop
bun installThat installs Electron 42, electron-builder 26, @electron/notarize, dotenv and Electrobun 1.18, all as devDependencies, so none of them goes into the Electron app. If node_modules/electron/path.txt is missing afterwards, Electron’s binary wasn’t downloaded: run node node_modules/electron/install.js.
Commands
| Command (in the copy) | What it does | Run here |
|---|---|---|
bun run start (or npm run start) |
electron . --enable-logging |
yes, under xvfb-run -a on a machine without a display, with both runners and no Yarn on the PATH. The game boots and its console prints to the terminal |
bun run build:linux |
electron-builder, AppImage in build/electron/ |
yes: My Game-0.1.0.AppImage (128 MB; the game’s files in app.asar are about 10 MB), and the unpacked app boots |
bun run build:windows, build:mac, build |
NSIS installer, macOS zip, all three | not run: Windows needs Wine on Linux, macOS needs a Mac |
bun run start:electrobun |
electrobun build && electrobun dev |
yes, under Xvfb. The first run downloads Electrobun’s core binaries (54 MB) into node_modules/electrobun/; it builds build/electrobun/dev-linux-x64/ and opens the window. The webview’s console isn’t printed |
bun run build:electrobun |
electrobun build --env=stable, the release build |
yes: build/electrobun/artifacts/stable-linux-x64-MyGame-Setup.tar.gz (34 MB), the .tar.zst and update.json |
To look inside a running host, evaluate code in its page from the main process: in Electron, a small main script that requires engine/electron/index.js and calls webContents.executeJavaScript() once the page loads; in Electrobun, win.webview.rpc.request.evaluateJavascriptWithResponse({ script }) (the script is a function body, so it returns its result, within the one-second request limit). Both were used to check the hosts for 2.9.0. --user-data-dir=<folder> keeps an Electron run’s data out of your own ~/.config.
Before 2.9.0, the starter’s package.json ran its commands through Yarn, build:linux failed (electron-builder made the Linux icon from assets/icons/icon.icns, which holds only an old is32 frame; build.linux.icon now points at assets/icons/512x512.png), Electrobun was a dependency and filled most of the Electron app (77 MB of files), build:electrobun made a development build, and the FileSystem adapter hung both hosts.
macOS notarization (engine/electron/notarize.js, run after signing) reads APPLEID, APPLEIDPASS and APPLETEAM from the environment or a .env file, skips notarizing when none is set and stops naming the missing ones when only some are. @electron/notarize 3 requires the team id with an Apple ID. The options it passes were checked against @electron/notarize’s own argument check on Linux; notarizing itself wasn’t run, since it needs a Mac.
Keep dist/ clean
Installing or building in dist/ itself leaves dist/node_modules/ and dist/build/ behind. Git ignores them, but the npm package doesn’t: its files list includes all of dist/, so they’d be published (Building and Packaging). Work in a copy, or delete them before you pack.