diff --git a/apps/launcher/src/main/services/launch/game-process.ts b/apps/launcher/src/main/services/launch/game-process.ts index 2182f85..406decb 100644 --- a/apps/launcher/src/main/services/launch/game-process.ts +++ b/apps/launcher/src/main/services/launch/game-process.ts @@ -1,5 +1,7 @@ import { execFile, spawn } from 'node:child_process'; -import { join } from 'node:path'; +import { constants } from 'node:fs'; +import { access, lstat, open, readdir } from 'node:fs/promises'; +import { dirname, join } from 'node:path'; import { promisify } from 'node:util'; import { GAME_FILES } from '@wto/patcher'; import { AppError } from '@shared/errors.ts'; @@ -7,6 +9,14 @@ import offlineGameArguments from '@shared/offline-game-arguments.json'; import { formatExitCode, type GameExit } from '@shared/schemas/launch.ts'; const execute = promisify(execFile); +const HOOK_DLL_FILENAME = 'wto_ws.dll'; +const HOOK_CFG_FILENAME = 'wto_ws.cfg'; +const HOOK_LOG_FILENAME = 'wto_ws.log'; +const HOOK_LOG_TAIL_BYTES = 8 * 1024; +const HOOK_LOG_TAIL_LINES = 8; +const HOOK_LOG_LINE_LIMIT = 200; +/** Allow small filesystem timestamp skew when matching newly created .game_logs entries. */ +const MTIME_SKEW_MS = 2000; export async function gameProcessRunning(signal: AbortSignal): Promise { if (process.platform !== 'win32') return false; @@ -18,6 +28,7 @@ export async function runGameProcess( copyPath: string, additionalArguments: readonly string[], signal: AbortSignal, onStart: () => void, ): Promise { if (await gameProcessRunning(signal)) throw new AppError('launch_game_running'); + await verifyLaunchFiles(copyPath); signal.throwIfAborted(); const executable = join(copyPath, GAME_FILES.executable); const args = [...offlineGameArguments, ...additionalArguments]; @@ -45,9 +56,12 @@ export async function runGameProcess( const summary = code !== null ? formatExitCode(code) : (reason ?? 'unknown'); if (code !== 0 && !signal.aborted) { console.warn('[game] process exited:', summary, `after ${elapsedMs} ms`, logFolder); - } else { - console.info('[game] process exited:', summary, `after ${elapsedMs} ms`); + void logAbnormalExitContext(copyPath, startedAt) + .catch((error: unknown) => console.warn('[game] inspecting post-exit files failed:', error)) + .finally(() => resolve({ code, signal: reason, logFolder })); + return; } + console.info('[game] process exited:', summary, `after ${elapsedMs} ms`); resolve({ code, signal: reason, logFolder }); }); signal.addEventListener('abort', stop, { once: true }); @@ -56,3 +70,103 @@ export async function runGameProcess( signal.removeEventListener('abort', stop); } } + +/** Verify the patched executable and hook files are still present and readable right before spawn. */ +async function verifyLaunchFiles(copyPath: string): Promise { + const exeFolder = dirname(GAME_FILES.executable); + const required = [ + GAME_FILES.executable, + join(exeFolder, HOOK_DLL_FILENAME), + join(exeFolder, HOOK_CFG_FILENAME), + ]; + const missing: string[] = []; + for (const relativePath of required) { + const fullPath = join(copyPath, relativePath); + try { + const stats = await lstat(fullPath); + if (!stats.isFile() || stats.size === 0) { + missing.push(`${relativePath} (not a non-empty file)`); + continue; + } + await access(fullPath, constants.R_OK); + } catch (error) { + const code = (error as NodeJS.ErrnoException).code ?? 'error'; + missing.push(`${relativePath} (${code})`); + } + } + if (missing.length > 0) { + console.error('[game] required launch files unavailable before start:', missing.join(', ')); + throw new AppError('launch_game_files_missing'); + } +} + +/** Record a compact read-only snapshot of hook and game log files after an unexpected non-zero exit. */ +async function logAbnormalExitContext(copyPath: string, startedAt: number): Promise { + const exeFolder = join(copyPath, dirname(GAME_FILES.executable)); + const [dllState, cfgState, hookLogStat, newGameLogs] = await Promise.all([ + describeFile(join(exeFolder, HOOK_DLL_FILENAME)), + describeFile(join(exeFolder, HOOK_CFG_FILENAME)), + safeRegularStat(join(exeFolder, HOOK_LOG_FILENAME)), + listNewGameLogs(join(copyPath, '.game_logs'), startedAt), + ]); + const hookLogDesc = hookLogStat + ? `${hookLogStat.size} bytes (${hookLogStat.mtimeMs >= startedAt - MTIME_SKEW_MS ? 'updated' : 'stale'})` + : 'missing'; + console.warn( + `[game] post-exit check: ${HOOK_DLL_FILENAME}=${dllState}, ${HOOK_CFG_FILENAME}=${cfgState}, ` + + `${HOOK_LOG_FILENAME}=${hookLogDesc}, newGameLogs=${newGameLogs}`, + ); + if (hookLogStat && hookLogStat.size > 0) { + const tail = await readTailLines(join(exeFolder, HOOK_LOG_FILENAME), hookLogStat.size); + if (tail.length > 0) { + console.warn(`[game] ${HOOK_LOG_FILENAME} tail:\n ${tail.join('\n ')}`); + } + } +} + +async function describeFile(path: string): Promise { + const stats = await safeRegularStat(path); + return stats ? `${stats.size} bytes` : 'missing'; +} + +async function safeRegularStat(path: string) { + try { + const stats = await lstat(path); + return stats.isFile() ? stats : null; + } catch { + return null; + } +} + +async function listNewGameLogs(folder: string, startedAt: number): Promise { + try { + const entries = await readdir(folder, { withFileTypes: true }); + const recent: string[] = []; + for (const entry of entries) { + if (!entry.isFile()) continue; + const stats = await safeRegularStat(join(folder, entry.name)); + if (!stats || stats.mtimeMs < startedAt - MTIME_SKEW_MS) continue; + recent.push(`${entry.name} (${stats.size} bytes)`); + } + return recent.length > 0 ? recent.slice(0, 6).join(', ') : 'none'; + } catch { + return 'unavailable'; + } +} + +async function readTailLines(path: string, fileSize: number): Promise { + const length = Math.min(fileSize, HOOK_LOG_TAIL_BYTES); + const start = fileSize - length; + const handle = await open(path, 'r'); + try { + const buffer = Buffer.alloc(length); + const { bytesRead } = await handle.read(buffer, 0, length, start); + const text = buffer.subarray(0, bytesRead).toString('utf8'); + const lines = text.split(/\r?\n/).filter((line) => line.length > 0); + return lines.slice(-HOOK_LOG_TAIL_LINES).map((line) => ( + line.length > HOOK_LOG_LINE_LIMIT ? `${line.slice(0, HOOK_LOG_LINE_LIMIT)}…` : line + )); + } finally { + await handle.close(); + } +} diff --git a/apps/launcher/src/renderer/src/i18n/locales/en.json b/apps/launcher/src/renderer/src/i18n/locales/en.json index 38acdab..f91cecc 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/en.json +++ b/apps/launcher/src/renderer/src/i18n/locales/en.json @@ -712,6 +712,7 @@ "launch_server_failed": "The local server could not be started. See the launcher log.", "launch_server_exited": "The local server is unavailable.", "launch_game_failed": "The game process could not start. See the launcher log.", + "launch_game_files_missing": "Required game launch files are missing or unreadable (they may have been quarantined by antivirus software). Check your security software and the launcher log.", "settings_write_failed": "The settings could not be saved.", "folder_unreadable": "The folder cannot be read: {{path}}", "not_a_game_folder": "The selected folder is not a War Thunder installation: {{path}}", diff --git a/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json b/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json index d9f984e..91d68a5 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json +++ b/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json @@ -712,6 +712,7 @@ "launch_server_failed": "无法启动本地服务器。请查看启动器日志。", "launch_server_exited": "本地服务器不可用。", "launch_game_failed": "游戏进程无法启动。请查看启动器日志。", + "launch_game_files_missing": "启动所需的核心文件缺失或无法访问(可能已被安全软件隔离)。请检查安全软件拦截记录或查看启动器日志。", "settings_write_failed": "无法保存设置。", "folder_unreadable": "无法读取文件夹:{{path}}", "not_a_game_folder": "所选文件夹不是战争雷霆的安装文件夹:{{path}}", diff --git a/apps/launcher/src/shared/errors.ts b/apps/launcher/src/shared/errors.ts index 917f5da..22484fc 100644 --- a/apps/launcher/src/shared/errors.ts +++ b/apps/launcher/src/shared/errors.ts @@ -65,6 +65,8 @@ export const APP_ERROR_CODES = [ 'launch_server_failed', 'launch_server_exited', 'launch_game_failed', + /** Core files built for the launch (aces.exe, wto_ws.dll, wto_ws.cfg) are missing or unreadable. */ + 'launch_game_files_missing', /** A folder the player chose does not exist or cannot be read (params: path). */ 'folder_unreadable', /** The chosen folder lacks the files of a War Thunder installation (params: path). */