feat(launcher): add the launcher application skeleton

The installer and launcher is an Electron app in apps/launcher: main
process, a sandboxed preload that only forwards allowed channels, and a
React window translated into Chinese and English.

Every renderer -> main call is declared once in the IPC contract
(shared/ipc/contract.ts) with zod schemas for its input and output; the
main side validates both when it registers a handler. The main process
throws error codes only, and the window translates them. The launcher's
own settings (for now the window language) are kept in the user data
folder and written through a temporary file and a rename. Development
runs can point the user data folder elsewhere with
WTO_LAUNCHER_USER_DATA.

The workspace packages (@wto/patcher, @wto/server) stay external to the
bundle: they ship as TypeScript sources and will run in utility
processes, where Electron's Node strips the types and they find their
web pages, tuning and keys next to their sources as they do today.

The root typecheck leaves out the launcher, which has its own tsconfig
files for the main process and the window.

Checked with the launcher typecheck, electron-vite build, the root
typecheck, and a start on a scratch user data folder.
This commit is contained in:
N0VAN0M4LY committed 2026-11-29 03:22:38 +08:00
1 parent 930ea07630
commit 70d59d7bf7
30 files changed
+3484 -1

No files matched your search

@@ -0,0 +1,54 @@
import { rename } from 'node:fs/promises';
import { join } from 'node:path';
import { app } from 'electron';
import { AppError } from '@shared/errors.ts';
import { defaultSettings, SettingsSchema, type Settings, type SettingsPatch } from '@shared/schemas/settings.ts';
import { readJsonOr, writeJsonAtomic } from '../../util/atomic-json.ts';
/**
* The launcher's settings file, `<user data>/settings.json`. Read once and cached; every change is
* validated as a whole and written before it is reported back.
*/
let cached: Settings | null = null;
function settingsPath(): string {
return join(app.getPath('userData'), 'settings.json');
}
export async function getSettings(): Promise<Settings> {
if (cached) return cached;
const path = settingsPath();
let raw: unknown = {};
try {
raw = await readJsonOr(path, {});
} catch (error) {
console.warn('[settings] settings.json is unreadable:', error);
}
const parsed = SettingsSchema.safeParse(raw);
if (!parsed.success) {
console.warn('[settings] settings.json did not validate; starting from defaults:', parsed.error.issues);
// Keep the player's file beside the new one instead of overwriting it on the next save.
await rename(path, `${path}.unreadable-${Date.now()}`).catch(() => undefined);
}
cached = parsed.success ? parsed.data : defaultSettings();
return cached;
}
export async function updateSettings(patch: SettingsPatch): Promise<Settings> {
const current = await getSettings();
const merged = SettingsSchema.parse({ ...current, ...definedFields(patch) });
try {
await writeJsonAtomic(settingsPath(), merged);
} catch (error) {
console.error('[settings] write failed:', error);
throw new AppError('settings_write_failed');
}
cached = merged;
return merged;
}
/** The patch without the fields it leaves out, so they do not overwrite current values. */
function definedFields(patch: SettingsPatch): Partial<Settings> {
return Object.fromEntries(Object.entries(patch).filter(([, value]) => value !== undefined)) as Partial<Settings>;
}
@@ -0,0 +1,46 @@
import { join } from 'node:path';
import { BrowserWindow, shell } from 'electron';
/** The launcher's single window. */
let mainWindow: BrowserWindow | null = null;
export function createMainWindow(): BrowserWindow {
const window = new BrowserWindow({
width: 960,
height: 640,
minWidth: 720,
minHeight: 480,
show: false,
autoHideMenuBar: true,
webPreferences: {
preload: join(import.meta.dirname, '../preload/index.cjs'),
sandbox: true,
contextIsolation: true,
nodeIntegration: false,
},
});
window.once('ready-to-show', () => window.show());
// Links leave the launcher for the player's browser; the window itself never navigates away.
window.webContents.setWindowOpenHandler(({ url }) => {
if (/^https?:\/\//.test(url)) void shell.openExternal(url);
return { action: 'deny' };
});
window.webContents.on('will-navigate', (event) => event.preventDefault());
window.on('closed', () => {
if (mainWindow === window) mainWindow = null;
});
const devServer = process.env['ELECTRON_RENDERER_URL'];
if (devServer) void window.loadURL(devServer);
else void window.loadFile(join(import.meta.dirname, '../renderer/index.html'));
mainWindow = window;
return window;
}
/** Brings the window forward, building it again if it was closed. */
export function showMainWindow(): void {
const window = mainWindow ?? createMainWindow();
if (window.isMinimized()) window.restore();
window.focus();
}