Add the launcher app 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:
1 parent
27fc535301
commit
ff65f6f3c9
30 files changed
+3484
-1
No files matched your search
@@ -0,0 +1,48 @@
|
||||
/**
|
||||
* Error codes shared by the main process and the window.
|
||||
*
|
||||
* The main process never produces text meant for the player. It throws an AppError carrying a
|
||||
* code and optional parameters; the code crosses IPC inside Error.message (Electron passes only
|
||||
* the message of a rejected invoke), and the window shows the translation of `errors.<code>`.
|
||||
* The underlying exception goes to the main process log.
|
||||
*
|
||||
* Dependency-free so the sandboxed preload could import it.
|
||||
*/
|
||||
|
||||
export const APP_ERROR_CODES = [
|
||||
/** A bug of ours: a handler returned something its contract does not allow. */
|
||||
'internal_error',
|
||||
/** The launcher's own settings file could not be written. */
|
||||
'settings_write_failed',
|
||||
] as const;
|
||||
|
||||
export type AppErrorCode = (typeof APP_ERROR_CODES)[number];
|
||||
export type AppErrorParams = Readonly<Record<string, string | number>>;
|
||||
|
||||
const MARKER = 'wto-app-error:';
|
||||
|
||||
export class AppError extends Error {
|
||||
readonly code: AppErrorCode;
|
||||
readonly params: AppErrorParams;
|
||||
|
||||
constructor(code: AppErrorCode, params: AppErrorParams = {}) {
|
||||
super(MARKER + JSON.stringify({ code, params }));
|
||||
this.name = 'AppError';
|
||||
this.code = code;
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
|
||||
/** The code and parameters an AppError put into a message, or null for any other error. */
|
||||
export function parseAppError(message: string): { code: AppErrorCode; params: AppErrorParams } | null {
|
||||
const start = message.indexOf(MARKER);
|
||||
if (start === -1) return null;
|
||||
try {
|
||||
const value = JSON.parse(message.slice(start + MARKER.length)) as { code?: unknown; params?: unknown };
|
||||
if (!(APP_ERROR_CODES as readonly unknown[]).includes(value.code)) return null;
|
||||
const params = value.params && typeof value.params === 'object' ? (value.params as AppErrorParams) : {};
|
||||
return { code: value.code as AppErrorCode, params };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Prefixes of the IPC channels the window may use. The preload forwards only these.
|
||||
*
|
||||
* Dependency-free: the preload runs sandboxed and cannot load packages.
|
||||
*/
|
||||
|
||||
/** Prefixes of renderer → main invoke channels. */
|
||||
export const IPC_CHANNEL_PREFIXES = [
|
||||
/** The launcher itself: version, folders. */
|
||||
'app:',
|
||||
/** The launcher's own settings. */
|
||||
'settings:',
|
||||
] as const;
|
||||
|
||||
/** Prefix of main → renderer event channels. */
|
||||
export const IPC_EVENT_PREFIX = 'event:';
|
||||
@@ -0,0 +1,42 @@
|
||||
import { z } from 'zod';
|
||||
import { SettingsPatchSchema, SettingsSchema } from '../schemas/settings.ts';
|
||||
|
||||
/**
|
||||
* The IPC contract: every renderer → main call and every main → renderer event, with the
|
||||
* schemas of what goes in and what comes out. The main process validates both sides when it
|
||||
* registers a handler; the window derives its types from here. No channel name is written as
|
||||
* a bare string anywhere else.
|
||||
*/
|
||||
|
||||
export const ipcContract = {
|
||||
'app:getInfo': {
|
||||
input: z.void(),
|
||||
output: z.object({
|
||||
version: z.string(),
|
||||
electron: z.string(),
|
||||
/** The operating system's preferred language, as Electron reports it (e.g. "zh-CN"). */
|
||||
systemLocale: z.string(),
|
||||
userDataPath: z.string(),
|
||||
}),
|
||||
},
|
||||
'settings:get': {
|
||||
input: z.void(),
|
||||
output: SettingsSchema,
|
||||
},
|
||||
'settings:update': {
|
||||
input: SettingsPatchSchema,
|
||||
output: SettingsSchema,
|
||||
},
|
||||
} as const;
|
||||
|
||||
export const ipcEvents = {
|
||||
'event:settings-changed': SettingsSchema,
|
||||
} as const;
|
||||
|
||||
export type IpcChannel = keyof typeof ipcContract;
|
||||
export type IpcInput<C extends IpcChannel> = z.input<(typeof ipcContract)[C]['input']>;
|
||||
export type IpcOutput<C extends IpcChannel> = z.output<(typeof ipcContract)[C]['output']>;
|
||||
export type IpcParsedInput<C extends IpcChannel> = z.output<(typeof ipcContract)[C]['input']>;
|
||||
|
||||
export type IpcEventChannel = keyof typeof ipcEvents;
|
||||
export type IpcEventPayload<C extends IpcEventChannel> = z.output<(typeof ipcEvents)[C]>;
|
||||
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* The languages the window is translated into. Dependency-free so the window can use it without
|
||||
* pulling in the schema library.
|
||||
*/
|
||||
export const LANGUAGES = ['zh-CN', 'en'] as const;
|
||||
export type Language = (typeof LANGUAGES)[number];
|
||||
|
||||
/** A language choice: one of the translations, or `system` to follow the operating system. */
|
||||
export const LANGUAGE_CHOICES = ['system', ...LANGUAGES] as const;
|
||||
export type LanguageChoice = (typeof LANGUAGE_CHOICES)[number];
|
||||
@@ -0,0 +1,22 @@
|
||||
import { z } from 'zod';
|
||||
import { LANGUAGE_CHOICES } from '../languages.ts';
|
||||
|
||||
const LanguageChoiceSchema = z.enum(LANGUAGE_CHOICES);
|
||||
|
||||
/** The launcher's own settings (not the game's, not the local service's tuning). */
|
||||
export const SettingsSchema = z.object({
|
||||
language: LanguageChoiceSchema.default('system'),
|
||||
});
|
||||
|
||||
export type Settings = z.output<typeof SettingsSchema>;
|
||||
|
||||
/** A change to some settings; the merged result is validated again before it is saved. */
|
||||
export const SettingsPatchSchema = z.object({
|
||||
language: LanguageChoiceSchema.optional(),
|
||||
});
|
||||
|
||||
export type SettingsPatch = z.output<typeof SettingsPatchSchema>;
|
||||
|
||||
export function defaultSettings(): Settings {
|
||||
return SettingsSchema.parse({});
|
||||
}
|
||||
Reference in new issue
Block a user