From 2820d40d872c4037a6db904d73d91476ceabbf95 Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Mon, 30 Nov 2026 21:13:33 +0800 Subject: [PATCH] feat(launcher): create dedicated WTOffline subfolder and default versioned client name --- apps/launcher/src/main/ipc/handlers.ts | 5 +- .../game-folder/inspect-target-folder.ts | 64 ++++++++++++++++++- .../src/renderer/src/i18n/locales/en.json | 3 +- .../src/renderer/src/i18n/locales/zh-CN.json | 3 +- .../renderer/src/pages/install/MethodStep.tsx | 3 +- .../src/pages/install/useInstallWizard.ts | 15 ++++- apps/launcher/src/shared/ipc/contract.ts | 6 +- 7 files changed, 88 insertions(+), 11 deletions(-) diff --git a/apps/launcher/src/main/ipc/handlers.ts b/apps/launcher/src/main/ipc/handlers.ts index dae1802..2a6a9a7 100644 --- a/apps/launcher/src/main/ipc/handlers.ts +++ b/apps/launcher/src/main/ipc/handlers.ts @@ -9,7 +9,7 @@ import { cancelMaintenance, maintainCopy, maintenanceInfo, maintenanceInProgress import { exportDiagnostics, openLogsFolder } from '../services/diagnostics/diagnostics.ts'; import { measureFolder } from '../services/game-folder/folder-size.ts'; import { inspectGameFolder, requireFolder } from '../services/game-folder/inspect-game-folder.ts'; -import { inspectTargetFolder } from '../services/game-folder/inspect-target-folder.ts'; +import { inspectPickedTargetFolder } from '../services/game-folder/inspect-target-folder.ts'; import { changeDecalSource, decalImportInProgress } from '../services/install/decals.ts'; import { cancelInstall, checkGameFolder, installGame, installationInProgress } from '../services/install/install-game.ts'; import { canonicalCopyPath, createCopyDraft, listCopies, readInstallRecord, renameCopy, setCurrentCopy } from '../services/install/install-record.ts'; @@ -63,7 +63,8 @@ export function registerIpcHandlers(): void { handle('install:prepareCopy', () => createCopyDraft()); handle('install:inspectGameFolder', ({ path }) => inspectGameFolder(path)); - handle('install:inspectTargetFolder', ({ path, stockPath }) => inspectTargetFolder(path, stockPath)); + handle('install:inspectTargetFolder', ({ path, stockPath, version }) => + inspectPickedTargetFolder(path, stockPath, version ?? null)); handle('install:inspectDataFolder', async ({ path }) => { const resolved = await canonicalCopyPath(path); return { path: resolved, inSteam: isInSteamLibrary(path) || isInSteamLibrary(resolved) }; diff --git a/apps/launcher/src/main/services/game-folder/inspect-target-folder.ts b/apps/launcher/src/main/services/game-folder/inspect-target-folder.ts index e0ea008..dfa572f 100644 --- a/apps/launcher/src/main/services/game-folder/inspect-target-folder.ts +++ b/apps/launcher/src/main/services/game-folder/inspect-target-folder.ts @@ -1,11 +1,15 @@ import { readdir, stat, statfs } from 'node:fs/promises'; -import { dirname, relative, resolve, isAbsolute } from 'node:path'; +import { basename, dirname, isAbsolute, join, relative, resolve } from 'node:path'; import { isInSteamLibrary } from '@wto/patcher'; import type { TargetFolderInspection } from '@shared/schemas/install.ts'; -import { canonicalCopyPath } from '../install/install-record.ts'; +import { canonicalCopyPath, listCopies } from '../install/install-record.ts'; + +const DEFAULT_SUBFOLDER = 'WTOffline'; +const SUBFOLDER_PATTERN = /^wtoffline(?:[-_].+)?$/i; +const MAX_DUPLICATE_SUFFIX = 99; /** - * Look at the folder the player chose for the game copy (install by copying): it must be new or + * Look at the exact folder for the game copy (install by copying): it must be new or * empty, must not overlap the stock installation, and the drive must have room. Only reads. */ export async function inspectTargetFolder(path: string, stockPath: string): Promise { @@ -20,6 +24,60 @@ export async function inspectTargetFolder(path: string, stockPath: string): Prom }; } +/** + * Resolve a player-picked parent folder into a dedicated `WTOffline` subfolder (falling back to + * `WTOffline-` and `WTOffline-2`, `WTOffline-3`, ... when an earlier folder already has + * files or is registered), unless the picked folder is already an empty `WTOffline*` directory. + */ +export async function inspectPickedTargetFolder( + pickedPath: string, + stockPath: string, + version: string | null, +): Promise { + const [picked, stock] = await Promise.all([canonicalCopyPath(pickedPath), canonicalCopyPath(stockPath)]); + if (contains(stock, picked) || contains(picked, stock)) { + return inspectTargetFolder(pickedPath, stockPath); + } + try { + const info = await stat(picked); + if (!info.isDirectory()) return inspectTargetFolder(pickedPath, stockPath); + } catch { + // Picked folder does not exist yet; still check it directly if it already looks like WTOffline*. + } + + const registered = await registeredCopyPaths(); + if (SUBFOLDER_PATTERN.test(basename(pickedPath)) && !registered.has(picked.toLowerCase())) { + const directProblem = await targetProblem(picked, stock); + if (directProblem === null) return inspectTargetFolder(pickedPath, stockPath); + } + + const candidateNames = [DEFAULT_SUBFOLDER]; + const safeVersion = version?.replace(/[^0-9A-Za-z._-]/g, '').trim(); + if (safeVersion) candidateNames.push(`${DEFAULT_SUBFOLDER}-${safeVersion}`); + for (let i = 2; i <= MAX_DUPLICATE_SUFFIX; i += 1) { + candidateNames.push(`${DEFAULT_SUBFOLDER}-${i}`); + } + + for (const name of candidateNames) { + const candidate = join(pickedPath, name); + const resolvedCandidate = await canonicalCopyPath(candidate); + if (registered.has(resolvedCandidate.toLowerCase())) continue; + const problem = await targetProblem(resolvedCandidate, stock); + if (problem === null) return inspectTargetFolder(candidate, stockPath); + } + return inspectTargetFolder(join(pickedPath, DEFAULT_SUBFOLDER), stockPath); +} + +async function registeredCopyPaths(): Promise> { + try { + const { copies } = await listCopies(); + const resolved = await Promise.all(copies.map((entry) => canonicalCopyPath(entry.copyPath).catch(() => entry.copyPath))); + return new Set(resolved.map((folder) => folder.toLowerCase())); + } catch { + return new Set(); + } +} + /** Compare actual volumes, including directories reached through junctions. */ export async function sameVolume(left: string, right: string): Promise { const [first, second] = await Promise.all([volumeDevice(left), volumeDevice(right)]); diff --git a/apps/launcher/src/renderer/src/i18n/locales/en.json b/apps/launcher/src/renderer/src/i18n/locales/en.json index 6e6527a..2dc0991 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/en.json +++ b/apps/launcher/src/renderer/src/i18n/locales/en.json @@ -152,7 +152,8 @@ "spaceSavingPending": "Calculating how much space can be saved.", "spaceSavingUnavailable": "This installation has no large resources eligible for space-saving copying.", "sameVolumeRequired": "Select a destination on the same volume as the original installation.", - "targetTitle": "Select a folder for the client", + "targetTitle": "Select an install location (a WTOffline subfolder will be created)", + "targetSubfolderHint": "A dedicated WTOffline folder is automatically created under the selected location to keep game files organized.", "inPlace": "In-place installation", "inPlaceBody": "Uses the selected game folder directly as the client. Original files are backed up to wtoffline\\stock, and patches are applied on first launch.", "existing": "Add existing client", 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 bd54538..04a94c1 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json +++ b/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json @@ -152,7 +152,8 @@ "spaceSavingPending": "正在估算可节省的空间。", "spaceSavingUnavailable": "该安装目录没有可用于省空间复制的大型资源。", "sameVolumeRequired": "请选择与原版位于同一磁盘卷的目标文件夹。", - "targetTitle": "选择存放客户端的文件夹", + "targetTitle": "选择安装位置(将自动创建 WTOffline 子文件夹)", + "targetSubfolderHint": "将在所选位置下自动创建独立的 WTOffline 文件夹存放游戏文件,避免文件散落。", "inPlace": "原位安装", "inPlaceBody": "使用所选游戏文件夹作为客户端。原版文件备份至 wtoffline\\stock,首次启动时将应用补丁。", "existing": "添加已有客户端", diff --git a/apps/launcher/src/renderer/src/pages/install/MethodStep.tsx b/apps/launcher/src/renderer/src/pages/install/MethodStep.tsx index aec1699..f92ce7d 100644 --- a/apps/launcher/src/renderer/src/pages/install/MethodStep.tsx +++ b/apps/launcher/src/renderer/src/pages/install/MethodStep.tsx @@ -30,7 +30,7 @@ export function MethodStep({ source, sourceSize, choice, onChoice, onError }: Me const choose = (method: Method): void => onChoice({ method, target: choice.target, steamConfirmed: false }); const inspectTarget = (path: string): void => { onChoice((current) => ({ ...current, target: null, steamConfirmed: false })); - ipcInvoke('install:inspectTargetFolder', { path, stockPath: source.path }) + ipcInvoke('install:inspectTargetFolder', { path, stockPath: source.path, version: source.version }) .then((target) => onChoice((current) => ({ ...current, target, steamConfirmed: false }))) .catch(onError); }; @@ -43,6 +43,7 @@ export function MethodStep({ source, sourceSize, choice, onChoice, onError }: Me const targetPicker = ( <> +

{t('method.targetSubfolderHint')}

{choice.target && } {steamWarning} diff --git a/apps/launcher/src/renderer/src/pages/install/useInstallWizard.ts b/apps/launcher/src/renderer/src/pages/install/useInstallWizard.ts index 568cf8f..07df51f 100644 --- a/apps/launcher/src/renderer/src/pages/install/useInstallWizard.ts +++ b/apps/launcher/src/renderer/src/pages/install/useInstallWizard.ts @@ -119,9 +119,20 @@ export function useInstallWizard({ onInstalled }: WizardOptions) { const chooseSource = (found: GameFolderInspection | null): void => { setSource(found); setStockSource(null); - const defaultMethod = mode === 'existing' || found?.hasOfflineBackup || found?.needsStockSource ? 'existing' : 'copy'; + const isExisting = mode === 'existing' || found?.hasOfflineBackup || found?.needsStockSource; + const defaultMethod = isExisting ? 'existing' : 'copy'; setChoice({ method: defaultMethod, target: null, steamConfirmed: false }); - setName(found?.path.split(/[\\/]/).filter(Boolean).at(-1) ?? ''); + if (!found) { + setName(''); + return; + } + const folderName = found.path.split(/[\\/]/).filter(Boolean).at(-1) ?? ''; + const genericName = /^(war thunder|wtoffline)$/i.test(folderName); + if (!isExisting || genericName || !folderName) { + setName(found.version ? `WTOffline ${found.version}` : 'WTOffline'); + } else { + setName(folderName); + } }; const install = (): void => { diff --git a/apps/launcher/src/shared/ipc/contract.ts b/apps/launcher/src/shared/ipc/contract.ts index 7b272b9..8f4a485 100644 --- a/apps/launcher/src/shared/ipc/contract.ts +++ b/apps/launcher/src/shared/ipc/contract.ts @@ -77,7 +77,11 @@ export const ipcContract = { output: GameFolderInspectionSchema, }, 'install:inspectTargetFolder': { - input: z.object({ path: z.string().min(1), stockPath: z.string().min(1) }), + input: z.object({ + path: z.string().min(1), + stockPath: z.string().min(1), + version: z.string().nullable().optional(), + }), output: TargetFolderInspectionSchema, }, 'install:inspectDataFolder': {