fix(launcher): report game exits separately
Keep successful process creation separate from exit status. Report the exit code and game log directory when the process exits abnormally. Verified by type checking and launcher build. The guarded game exit remains part of acceptance.
This commit is contained in:
1 parent
b3a30c0fd9
commit
84158f719d
9 files changed
+55
-26
No files matched your search
@@ -3,6 +3,7 @@ import { join } from 'node:path';
|
||||
import { promisify } from 'node:util';
|
||||
import { GAME_FILES } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { GameExit } from '@shared/schemas/launch.ts';
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const OFFLINE_ARGUMENTS = ['-forcestart', '-nobeac', '-crashpad-disable-upload'];
|
||||
@@ -15,7 +16,7 @@ export async function gameProcessRunning(signal: AbortSignal): Promise<boolean>
|
||||
|
||||
export async function runGameProcess(
|
||||
copyPath: string, additionalArguments: readonly string[], signal: AbortSignal, onStart: () => void,
|
||||
): Promise<void> {
|
||||
): Promise<GameExit> {
|
||||
if (await gameProcessRunning(signal)) throw new AppError('launch_game_running');
|
||||
signal.throwIfAborted();
|
||||
const child = spawn(join(copyPath, GAME_FILES.executable), [...OFFLINE_ARGUMENTS, ...additionalArguments], {
|
||||
@@ -24,7 +25,7 @@ export async function runGameProcess(
|
||||
const stop = (): void => { child.kill(); };
|
||||
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
return await new Promise<GameExit>((resolve, reject) => {
|
||||
child.once('spawn', () => {
|
||||
if (signal.aborted) stop();
|
||||
else onStart();
|
||||
@@ -34,11 +35,11 @@ export async function runGameProcess(
|
||||
reject(new AppError('launch_game_failed'));
|
||||
});
|
||||
child.once('close', (code, reason) => {
|
||||
if (code === 0 || signal.aborted) resolve();
|
||||
else {
|
||||
console.error('[game] process exited:', code ?? reason);
|
||||
reject(new AppError('launch_game_failed'));
|
||||
const logFolder = join(copyPath, '.game_logs');
|
||||
if (code !== 0 && !signal.aborted) {
|
||||
console.warn('[game] process exited:', code ?? reason, logFolder);
|
||||
}
|
||||
resolve({ code, signal: reason, logFolder });
|
||||
});
|
||||
signal.addEventListener('abort', stop, { once: true });
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ import { app, BrowserWindow, shell } from 'electron';
|
||||
import { AccountStore, LOOPBACK } from '@wto/server';
|
||||
import { executableVersion, GAME_FILES, isInSteamLibrary } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { LaunchState } from '@shared/schemas/launch.ts';
|
||||
import type { GameExit, LaunchState } from '@shared/schemas/launch.ts';
|
||||
import { getSettings } from '../config/settings-store.ts';
|
||||
import { decalCachePath } from '../install/decals.ts';
|
||||
import { copyPathContains, readInstallRecord } from '../install/install-record.ts';
|
||||
@@ -160,16 +160,20 @@ async function launch(job: LaunchJob): Promise<void> {
|
||||
});
|
||||
});
|
||||
|
||||
const publish = (phase: LaunchState['phase']): void => {
|
||||
const publish = (phase: LaunchState['phase'], gameExit?: GameExit): void => {
|
||||
if (signal.aborted) return;
|
||||
update(job, { phase, port });
|
||||
update(job, { phase, port, gameExit });
|
||||
if (phase === 'running' && settings.afterGameStarts === 'minimize') {
|
||||
for (const window of BrowserWindow.getAllWindows()) window.minimize();
|
||||
}
|
||||
};
|
||||
if (lab) await runLab(lab, stockPath, dataPath, signal, publish);
|
||||
else await runOrdinaryLaunch(copyPath, dataPath, port, settings, signal, publish);
|
||||
update(job, { phase: 'ready' });
|
||||
if (lab) {
|
||||
await runLab(lab, stockPath, dataPath, signal, publish);
|
||||
update(job, { phase: 'ready' });
|
||||
return;
|
||||
}
|
||||
const gameExit = await runOrdinaryLaunch(copyPath, dataPath, port, settings, signal, publish);
|
||||
update(job, { phase: 'game-exited', gameExit });
|
||||
}
|
||||
|
||||
async function readVersion(folder: string, signal: AbortSignal): Promise<string | null> {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { GAME_FILES, isInSteamLibrary } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import { parseLaunchArguments } from '@shared/launch-arguments.ts';
|
||||
import type { InstallRecord } from '@shared/schemas/install.ts';
|
||||
import type { GameExit } from '@shared/schemas/launch.ts';
|
||||
import type { Settings } from '@shared/schemas/settings.ts';
|
||||
import { canonicalCopyPath, copyPathContains } from '../install/install-record.ts';
|
||||
import { STOCK_BACKUP_FOLDER } from '../install/stock-backup.ts';
|
||||
@@ -32,8 +33,8 @@ type OrdinaryPhase = 'starting-server' | 'starting-game' | 'running' | 'server-r
|
||||
|
||||
export async function runOrdinaryLaunch(
|
||||
copyPath: string, dataPath: string, port: number, settings: Settings, signal: AbortSignal,
|
||||
publish: (phase: OrdinaryPhase) => void,
|
||||
): Promise<void> {
|
||||
publish: (phase: OrdinaryPhase, gameExit?: GameExit) => void,
|
||||
): Promise<GameExit> {
|
||||
const args = parseLaunchArguments(settings.launchArguments);
|
||||
if (!args) throw new AppError('launch_game_failed');
|
||||
publish('starting-server');
|
||||
@@ -44,12 +45,12 @@ export async function runOrdinaryLaunch(
|
||||
publish('starting-game');
|
||||
const game = runGameProcess(copyPath, args, runSignal, () => publish('running'));
|
||||
try {
|
||||
await Promise.race([game, serverExited]);
|
||||
runSignal.throwIfAborted();
|
||||
if (!settings.closeServerOnGameExit) {
|
||||
publish('server-running');
|
||||
const gameExit = await Promise.race([game, serverExited]);
|
||||
if (!runSignal.aborted && !settings.closeServerOnGameExit) {
|
||||
publish('server-running', gameExit);
|
||||
if (!runSignal.aborted) await Promise.race([once(runSignal, 'abort'), serverExited]);
|
||||
}
|
||||
return gameExit;
|
||||
} finally {
|
||||
stop.abort();
|
||||
await game.catch(() => undefined);
|
||||
|
||||
Reference in new issue
Block a user