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

+12
View File
@@ -0,0 +1,12 @@
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:" />
<title>WTOffline</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="./src/main.tsx"></script>
</body>
</html>
+32
View File
@@ -0,0 +1,32 @@
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { Settings } from '@shared/schemas/settings.ts';
import { LanguageMenu } from './components/LanguageMenu.tsx';
import { applyLanguage, resolveLanguage } from './i18n/index.ts';
import { ipcOn } from './ipc.ts';
import { WelcomePage } from './pages/WelcomePage.tsx';
interface AppProps {
readonly initialSettings: Settings;
readonly systemLocale: string;
}
export function App({ initialSettings, systemLocale }: AppProps) {
const { t } = useTranslation();
const [settings, setSettings] = useState(initialSettings);
useEffect(() => ipcOn('event:settings-changed', setSettings), []);
useEffect(() => applyLanguage(resolveLanguage(settings.language, systemLocale)), [settings.language, systemLocale]);
return (
<div className="app">
<header className="app-header">
<h1 className="app-title">{t('app.title')}</h1>
<LanguageMenu value={settings.language} />
</header>
<main className="app-main">
<WelcomePage />
</main>
</div>
);
}
@@ -0,0 +1,29 @@
import { useTranslation } from 'react-i18next';
import { LANGUAGE_CHOICES, type LanguageChoice } from '@shared/languages.ts';
import { ipcInvoke } from '../ipc.ts';
import { useErrorMessage } from '../useErrorMessage.ts';
export function LanguageMenu({ value }: { readonly value: LanguageChoice }) {
const { t } = useTranslation();
const { message, report } = useErrorMessage();
return (
<label className="language-menu">
<span>{t('language.label')}</span>
<select
value={value}
onChange={(event) => {
const language = event.target.value as LanguageChoice;
ipcInvoke('settings:update', { language }).catch(report);
}}
>
{LANGUAGE_CHOICES.map((choice) => (
<option key={choice} value={choice}>
{t(`language.${choice}`)}
</option>
))}
</select>
{message && <span className="error-text">{message}</span>}
</label>
);
}
@@ -0,0 +1,33 @@
import i18next from 'i18next';
import { initReactI18next } from 'react-i18next';
import { LANGUAGES, type Language, type LanguageChoice } from '@shared/languages.ts';
import en from './locales/en.json';
import zhCN from './locales/zh-CN.json';
/** The translations. Both files carry the same keys; English is the fallback. */
const resources = {
en: { translation: en },
'zh-CN': { translation: zhCN },
} satisfies Record<Language, unknown>;
export async function initI18n(language: Language): Promise<void> {
await i18next.use(initReactI18next).init({
resources,
lng: language,
fallbackLng: 'en',
interpolation: { escapeValue: false },
});
}
/** The translation a settings choice stands for; `system` maps the system locale onto one of ours. */
export function resolveLanguage(choice: LanguageChoice, systemLocale: string): Language {
if (choice !== 'system') return choice;
const exact = LANGUAGES.find((language) => language.toLowerCase() === systemLocale.toLowerCase());
if (exact) return exact;
return systemLocale.toLowerCase().startsWith('zh') ? 'zh-CN' : 'en';
}
export function applyLanguage(language: Language): void {
void i18next.changeLanguage(language);
document.documentElement.lang = language;
}
@@ -0,0 +1,20 @@
{
"app": {
"title": "War Thunder Offline"
},
"language": {
"label": "Language",
"system": "System",
"zh-CN": "简体中文",
"en": "English"
},
"welcome": {
"title": "Not installed yet",
"body": "Install the offline version from your own copy of War Thunder. Your original game is only read, never changed."
},
"errors": {
"internal_error": "Something went wrong inside the launcher.",
"settings_write_failed": "Could not save the settings.",
"unknown": "Something went wrong."
}
}
@@ -0,0 +1,20 @@
{
"app": {
"title": "战争雷霆离线版"
},
"language": {
"label": "语言",
"system": "跟随系统",
"zh-CN": "简体中文",
"en": "English"
},
"welcome": {
"title": "尚未安装",
"body": "从你自己的战争雷霆游戏安装离线版。原版游戏只读取,不会被修改。"
},
"errors": {
"internal_error": "启动器内部出错了。",
"settings_write_failed": "设置保存失败。",
"unknown": "出错了。"
}
}
+39
View File
@@ -0,0 +1,39 @@
import { parseAppError, type AppErrorCode, type AppErrorParams } from '@shared/errors.ts';
import type { IpcChannel, IpcEventChannel, IpcEventPayload, IpcInput, IpcOutput } from '@shared/ipc/contract.ts';
/**
* Typed IPC on the window side.
* const settings = await ipcInvoke('settings:get');
* const off = ipcOn('event:settings-changed', (settings) => ...);
* An AppError thrown in the main process arrives here as an AppIpcError with its code, which the
* window translates through `errors.<code>`.
*/
export class AppIpcError extends Error {
readonly code: AppErrorCode;
readonly params: AppErrorParams;
constructor(code: AppErrorCode, params: AppErrorParams) {
super(code);
this.name = 'AppIpcError';
this.code = code;
this.params = params;
}
}
export async function ipcInvoke<C extends IpcChannel>(
channel: C,
...args: IpcInput<C> extends void ? [] : [input: IpcInput<C>]
): Promise<IpcOutput<C>> {
try {
return (await window.wto.invoke(channel, args[0])) as IpcOutput<C>;
} catch (error) {
const appError = error instanceof Error ? parseAppError(error.message) : null;
if (appError) throw new AppIpcError(appError.code, appError.params);
throw error;
}
}
export function ipcOn<C extends IpcEventChannel>(channel: C, listener: (payload: IpcEventPayload<C>) => void): () => void {
return window.wto.on(channel, listener as (payload: unknown) => void);
}
+22
View File
@@ -0,0 +1,22 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { App } from './App.tsx';
import { initI18n, resolveLanguage } from './i18n/index.ts';
import { ipcInvoke } from './ipc.ts';
import './styles/app.css';
async function start(): Promise<void> {
const [settings, info] = await Promise.all([ipcInvoke('settings:get'), ipcInvoke('app:getInfo')]);
const language = resolveLanguage(settings.language, info.systemLocale);
await initI18n(language);
document.documentElement.lang = language;
const root = document.getElementById('root');
if (!root) throw new Error('#root is missing from index.html');
createRoot(root).render(
<StrictMode>
<App initialSettings={settings} systemLocale={info.systemLocale} />
</StrictMode>,
);
}
void start();
@@ -0,0 +1,12 @@
import { useTranslation } from 'react-i18next';
/** What the launcher shows before anything is installed. The install wizard starts from here. */
export function WelcomePage() {
const { t } = useTranslation();
return (
<section className="welcome">
<h2>{t('welcome.title')}</h2>
<p>{t('welcome.body')}</p>
</section>
);
}
@@ -0,0 +1,96 @@
:root {
color-scheme: light dark;
--bg: #f6f6f4;
--surface: #ffffff;
--text: #1d1f21;
--text-muted: #5c6166;
--border: #d9dbdd;
--accent: #b5541c;
--danger: #b3261e;
--radius: 8px;
font-family: 'Segoe UI', 'Microsoft YaHei UI', system-ui, sans-serif;
font-size: 14px;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #17191b;
--surface: #212427;
--text: #e8e9ea;
--text-muted: #a2a7ac;
--border: #363a3e;
--accent: #e0864c;
--danger: #f2a49e;
}
}
* {
box-sizing: border-box;
}
body {
margin: 0;
background: var(--bg);
color: var(--text);
}
.app {
display: flex;
flex-direction: column;
min-height: 100vh;
}
.app-header {
display: flex;
align-items: center;
justify-content: space-between;
padding: 12px 20px;
border-bottom: 1px solid var(--border);
background: var(--surface);
}
.app-title {
margin: 0;
font-size: 16px;
font-weight: 600;
}
.app-main {
flex: 1;
padding: 24px 20px;
}
.language-menu {
display: flex;
align-items: center;
gap: 8px;
color: var(--text-muted);
}
.language-menu select {
font: inherit;
color: var(--text);
background: var(--surface);
border: 1px solid var(--border);
border-radius: var(--radius);
padding: 4px 8px;
}
.welcome {
max-width: 560px;
}
.welcome h2 {
margin: 0 0 8px;
font-size: 20px;
}
.welcome p {
margin: 0;
color: var(--text-muted);
line-height: 1.6;
}
.error-text {
color: var(--danger);
}
@@ -0,0 +1,20 @@
import { useCallback, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { AppIpcError } from './ipc.ts';
/** Turns a failed call into the translated message of its error code. */
export function useErrorMessage(): { message: string | null; report: (error: unknown) => void; clear: () => void } {
const { t } = useTranslation();
const [message, setMessage] = useState<string | null>(null);
const report = useCallback(
(error: unknown) => {
console.error(error);
setMessage(error instanceof AppIpcError ? t(`errors.${error.code}`, error.params) : t('errors.unknown'));
},
[t],
);
const clear = useCallback(() => setMessage(null), []);
return { message, report, clear };
}