diff --git a/apps/launcher/src/main/ipc/handlers.ts b/apps/launcher/src/main/ipc/handlers.ts index 2a6a9a7..93f29ee 100644 --- a/apps/launcher/src/main/ipc/handlers.ts +++ b/apps/launcher/src/main/ipc/handlers.ts @@ -6,13 +6,14 @@ import { listBackgroundPictures } from '../services/background-art/art-catalog.t import { getCopyTuning, updateCopyTuning } from '../services/config/copy-tuning.ts'; import { getSettings, updateSettings } from '../services/config/settings-store.ts'; import { cancelMaintenance, maintainCopy, maintenanceInfo, maintenanceInProgress } from '../services/copies/copy-maintenance.ts'; +import { relocateCopy } from '../services/copies/relocate-copy.ts'; 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 { 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'; +import { canonicalCopyPath, copyFolderMissing, createCopyDraft, hydrateCopiesStatus, listCopies, readInstallRecord, renameCopy, setCurrentCopy } from '../services/install/install-record.ts'; import { readInstalledLab } from '../services/launch/lab-handoff.ts'; import { getLaunchState, launchInProgress, openServerStatus, startLaunch, stopLaunch } from '../services/launch/launch-controller.ts'; import { activePatchSet, applyPackUpdate, checkPackUpdate, revertToBundledPatchSet } from '../services/patch-pack/pack-update.ts'; @@ -84,38 +85,50 @@ export function registerIpcHandlers(): void { handle('install:start', async (request) => { if (maintenanceInProgress()) throw new AppError('maintenance_busy'); const record = await installGame(request, (progress) => broadcast('event:install-progress', progress)); - broadcast('event:copies-changed', await listCopies()); + broadcast('event:copies-changed', await hydrateCopiesStatus(await listCopies())); return record; }); handle('install:cancel', () => ({ cancelled: cancelInstall() })); - handle('install:getRecord', async () => ({ record: await readInstallRecord() })); + handle('install:getRecord', async () => { + const record = await readInstallRecord(); + if (!record) return { record: null }; + return { record: (await copyFolderMissing(record.copyPath)) ? { ...record, folderMissing: true } : record }; + }); handle('install:listFeatures', () => { const resources = resourcePaths(); return patchFeatures(resources.patches).map((feature) => ({ ...feature, patchIds: [...feature.patchIds] })); }); - handle('copies:list', () => listCopies()); + handle('copies:list', async () => hydrateCopiesStatus(await listCopies())); handle('copies:setCurrent', async ({ id }) => { - const registry = await setCurrentCopy(id); + const registry = await hydrateCopiesStatus(await setCurrentCopy(id)); broadcast('event:copies-changed', registry); return registry; }); handle('copies:rename', async ({ id, name }) => { - const registry = await renameCopy(id, name); + const registry = await hydrateCopiesStatus(await renameCopy(id, name)); + broadcast('event:copies-changed', registry); + return registry; + }); + handle('copies:relocate', async ({ id, path }) => { + if (launchInProgress() || maintenanceInProgress() || installationInProgress() || saveWriteInProgress() || decalImportInProgress()) { + throw new AppError('maintenance_busy'); + } + const registry = await relocateCopy(id, path); broadcast('event:copies-changed', registry); return registry; }); handle('copies:setDecalSource', async ({ id, path }) => { if (launchInProgress() || maintenanceInProgress() || installationInProgress()) throw new AppError('maintenance_busy'); - const updated = await changeDecalSource(id, path); + const updated = await hydrateCopiesStatus(await changeDecalSource(id, path)); broadcast('event:copies-changed', updated); return updated; }); handle('copies:maintenanceInfo', ({ id }) => maintenanceInfo(id)); handle('copies:maintain', async (request) => { if (saveWriteInProgress() || decalImportInProgress()) throw new AppError('maintenance_busy'); - const updated = await maintainCopy(request, (progress) => broadcast('event:copy-maintenance', progress)); + const updated = await hydrateCopiesStatus(await maintainCopy(request, (progress) => broadcast('event:copy-maintenance', progress))); broadcast('event:copies-changed', updated); return updated; }); diff --git a/apps/launcher/src/main/services/copies/relocate-copy.ts b/apps/launcher/src/main/services/copies/relocate-copy.ts new file mode 100644 index 0000000..3a58b2c --- /dev/null +++ b/apps/launcher/src/main/services/copies/relocate-copy.ts @@ -0,0 +1,22 @@ +import { AppError } from '@shared/errors.ts'; +import type { Copies } from '@shared/schemas/install.ts'; +import { inspectGameFolder } from '../game-folder/inspect-game-folder.ts'; +import { hydrateCopiesStatus, relocateCopyRecord } from '../install/install-record.ts'; +import { backUpStockFiles } from '../install/stock-backup.ts'; + +/** + * Re-point an existing registered client to its moved folder while keeping its id, name, saves, + * and data folder intact. + */ +export async function relocateCopy(id: string, newPath: string): Promise { + const game = await inspectGameFolder(newPath); + if (game.missingFiles.length) throw new AppError('not_a_game_folder', { path: newPath }); + if (game.needsStockSource) throw new AppError('stock_source_invalid'); + const stockBackupPath = await backUpStockFiles(game.resolvedPath); + const updated = await relocateCopyRecord(id, { + copyPath: newPath, + stockBackupPath, + gameVersion: game.version, + }); + return hydrateCopiesStatus(updated); +} diff --git a/apps/launcher/src/main/services/install/install-record.ts b/apps/launcher/src/main/services/install/install-record.ts index 2b93127..588a4c4 100644 --- a/apps/launcher/src/main/services/install/install-record.ts +++ b/apps/launcher/src/main/services/install/install-record.ts @@ -1,8 +1,8 @@ import { randomUUID } from 'node:crypto'; -import { realpath } from 'node:fs/promises'; +import { access, realpath } from 'node:fs/promises'; import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'; import { app } from 'electron'; -import { isInSteamLibrary } from '@wto/patcher'; +import { GAME_FILES, isInSteamLibrary } from '@wto/patcher'; import { AppError } from '@shared/errors.ts'; import { CopiesSchema, type Copies, type InstallRecord } from '@shared/schemas/install.ts'; import { readJsonOr, writeJsonAtomic } from '../../util/atomic-json.ts'; @@ -75,11 +75,72 @@ export function renameCopy(id: string, name: string): Promise { return updateCopyRecord(id, (record) => ({ ...record, name })); } +export async function copyFolderMissing(copyPath: string): Promise { + for (const file of Object.values(GAME_FILES)) { + try { + await access(join(copyPath, file)); + } catch { + return true; + } + } + return false; +} + +/** Attach live `folderMissing` status to each registered copy without modifying `copies.json`. */ +export async function hydrateCopiesStatus(registry: Copies): Promise { + const copies = await Promise.all(registry.copies.map(async (copy) => { + const missing = await copyFolderMissing(copy.copyPath); + const { folderMissing: _old, ...base } = copy; + return missing ? { ...base, folderMissing: true } : base; + })); + return { ...registry, copies }; +} + +export function relocateCopyRecord( + id: string, + next: { readonly copyPath: string; readonly stockBackupPath: string; readonly gameVersion: string | null }, +): Promise { + return changeRegistry(async (registry) => { + const current = registry.copies.find((copy) => copy.id === id); + if (!current) throw new AppError('copy_not_found'); + const resolvedCopy = await canonicalCopyPath(next.copyPath); + const resolvedData = await canonicalCopyPath(current.dataPath); + if (overlaps(resolvedCopy, resolvedData) && copyPathContains(next.stockBackupPath, resolvedData)) { + throw new AppError('copy_location_in_use'); + } + for (const existing of registry.copies) { + if (existing.id === id) continue; + const used = await Promise.all([canonicalCopyPath(existing.copyPath), canonicalCopyPath(existing.dataPath)]); + if (used.some((other) => overlaps(resolvedCopy, other))) { + throw new AppError('copy_location_in_use'); + } + } + return { + ...registry, + copies: registry.copies.map((record) => { + if (record.id !== id) return record; + const { steamLibraryConfirmation: _staleSteam, folderMissing: _staleMissing, ...rest } = record; + return { + ...rest, + copyPath: next.copyPath, + stockBackupPath: next.stockBackupPath, + gameVersion: next.gameVersion, + }; + }), + }; + }); +} + /** Serialize writes so switching, renaming and completing an installation preserve each other. */ function changeRegistry(change: (registry: Copies) => Promise | Copies): Promise { const operation = pending.then(async () => { try { - const updated = CopiesSchema.parse(await change(await readRegistry())); + const next = await change(await readRegistry()); + const sanitized: Copies = { + ...next, + copies: next.copies.map(({ folderMissing: _ignored, ...rest }) => rest), + }; + const updated = CopiesSchema.parse(sanitized); await writeJsonAtomic(recordPath(), updated); return updated; } catch (error) { diff --git a/apps/launcher/src/renderer/src/App.tsx b/apps/launcher/src/renderer/src/App.tsx index 6882e69..63316c7 100644 --- a/apps/launcher/src/renderer/src/App.tsx +++ b/apps/launcher/src/renderer/src/App.tsx @@ -72,10 +72,17 @@ export function App({ info, initialSettings, initialCopies }: AppProps) { const wizard = useInstallWizard({ onInstalled: () => setRoute(START_ROUTE) }); - const backgrounds = useBackgrounds(record?.copyPath ?? null, settings, report); + const backgrounds = useBackgrounds(record && !record.folderMissing ? record.copyPath : null, settings, report); useEffect(() => ipcOn('event:settings-changed', setSettings), []); useEffect(() => ipcOn('event:copies-changed', setRegistry), []); + useEffect(() => { + const refreshCopies = (): void => { + ipcInvoke('copies:list').then(setRegistry).catch(() => undefined); + }; + window.addEventListener('focus', refreshCopies); + return () => window.removeEventListener('focus', refreshCopies); + }, []); useEffect(() => applyLanguage(resolveLanguage(settings.language, info.systemLocale)), [settings.language, info.systemLocale]); useEffect(() => { @@ -106,12 +113,22 @@ export function App({ info, initialSettings, initialCopies }: AppProps) { if (details) go({ page: 'copies', section: 'summary' }); }).catch(report); }; + const relocateCopy = (id: string): void => { + clearError(); + ipcInvoke('dialog:pickFolder', { title: t('copies.relocateDialogTitle') }) + .then(({ path }) => { + if (!path) return; + return ipcInvoke('copies:relocate', { id, path }).then(setRegistry); + }) + .catch(report); + }; const perCopyStatus = ['version-changed', 'failed', 'port-busy', 'server-exited', 'game-exited'].includes(launch.phase); const launchStatus: LaunchState = perCopyStatus && launch.copyId && launch.copyId !== record?.id ? { phase: 'ready' } : launch; const home = homeBar(record, launchStatus, t, () => wizard.start('new'), { start: () => { ipcInvoke('launch:start').catch(report); }, stop: () => { ipcInvoke('launch:stop').catch(report); }, checkVersion: () => go({ page: 'copies', section: 'features' }), + relocate: () => { if (record) relocateCopy(record.id); }, }); const view = ((): View => { @@ -168,7 +185,16 @@ export function App({ info, initialSettings, initialCopies }: AppProps) { if (route.copies === 'list') { return { side, - main: wizard.start('new')} onAddExisting={() => wizard.start('existing')} />, + main: ( + wizard.start('new')} + onAddExisting={() => wizard.start('existing')} + /> + ), bar: home, }; } @@ -192,7 +218,15 @@ export function App({ info, initialSettings, initialCopies }: AppProps) { } const main = route.copies === 'features' ? - : go({ page: 'copies', section: 'features' })} onError={report} />; + : ( + go({ page: 'copies', section: 'features' })} + onRelocate={() => relocateCopy(record.id)} + onError={report} + /> + ); return { side, main, bar: home }; } } diff --git a/apps/launcher/src/renderer/src/i18n/locales/en.json b/apps/launcher/src/renderer/src/i18n/locales/en.json index 2dc0991..662af78 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/en.json +++ b/apps/launcher/src/renderer/src/i18n/locales/en.json @@ -67,6 +67,7 @@ "ready": "Ready", "readyMissing": "Ready · {{count}} features unavailable", "readyDetail": "Game {{version}}", + "missingFolder": "Client folder not found", "launch": "Launch" }, "launch": { @@ -215,6 +216,9 @@ }, "add": "Add a client", "addExisting": "Add existing client", + "missingFolder": "Folder missing", + "relocate": "Relocate…", + "relocateDialogTitle": "Select the moved client folder", "list": { "current": "Current", "select": "Set as current" }, "none": "The offline edition is not installed yet.", "maintenance": { @@ -267,6 +271,7 @@ "adaptation": "Check status", "adaptationHint": "Whether every patch was checked on this game version and verified in game", "location": "Location", + "missingHint": "Game files were not found at this path. Relocate to the moved folder to keep existing saves and tuning.", "method": "Installation type", "methods": { "copy": "Copy installation", 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 04a94c1..8dd35ad 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json +++ b/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json @@ -67,6 +67,7 @@ "ready": "就绪", "readyMissing": "就绪 · {{count}} 项功能不可用", "readyDetail": "游戏 {{version}}", + "missingFolder": "客户端目录不存在", "launch": "启动" }, "launch": { @@ -215,6 +216,9 @@ }, "add": "添加客户端", "addExisting": "添加已有客户端", + "missingFolder": "目录不存在", + "relocate": "重新定位…", + "relocateDialogTitle": "选择挪动后的客户端文件夹", "list": { "current": "当前", "select": "设为当前" }, "none": "尚未安装离线版。", "maintenance": { @@ -267,6 +271,7 @@ "adaptation": "核对状态", "adaptationHint": "这个游戏版本是否检查过全部补丁并在游戏里验证过", "location": "位置", + "missingHint": "未在原位置找到游戏文件。可重新定位至挪动后的文件夹,保留现有存档和数值。", "method": "安装方式", "methods": { "copy": "复制安装", diff --git a/apps/launcher/src/renderer/src/pages/copies/CopyList.tsx b/apps/launcher/src/renderer/src/pages/copies/CopyList.tsx index 4931049..24411f3 100644 --- a/apps/launcher/src/renderer/src/pages/copies/CopyList.tsx +++ b/apps/launcher/src/renderer/src/pages/copies/CopyList.tsx @@ -7,11 +7,12 @@ interface CopyListProps { readonly registry: Copies; readonly supportedVersions: readonly string[]; readonly onSelect: (id: string, detail?: boolean) => void; + readonly onRelocate: (id: string) => void; readonly onAdd: () => void; readonly onAddExisting: () => void; } -export function CopyList({ registry, supportedVersions, onSelect, onAdd, onAddExisting }: CopyListProps) { +export function CopyList({ registry, supportedVersions, onSelect, onRelocate, onAdd, onAddExisting }: CopyListProps) { const { t } = useTranslation(); return ( @@ -25,6 +26,7 @@ export function CopyList({ registry, supportedVersions, onSelect, onAdd, onAddEx
{copy.name} {current && {t('copies.list.current')}} + {copy.folderMissing && {t('copies.missingFolder')}}
{t('copies.summary.version')}
@@ -39,7 +41,14 @@ export function CopyList({ registry, supportedVersions, onSelect, onAdd, onAddEx
{copy.copyPath}
- {!current && } + {copy.folderMissing && ( + + )} + {!current && ( + + )}
diff --git a/apps/launcher/src/renderer/src/pages/copies/CopySummary.tsx b/apps/launcher/src/renderer/src/pages/copies/CopySummary.tsx index 46af1d4..062aa85 100644 --- a/apps/launcher/src/renderer/src/pages/copies/CopySummary.tsx +++ b/apps/launcher/src/renderer/src/pages/copies/CopySummary.tsx @@ -11,11 +11,12 @@ interface CopySummaryProps { readonly record: InstallRecord; readonly supportedVersions: readonly string[]; readonly onShowFeatures: () => void; + readonly onRelocate: () => void; readonly onError: (error: unknown) => void; } /** What is installed where: version, adaptation, folders, installation type. */ -export function CopySummary({ record, supportedVersions, onShowFeatures, onError }: CopySummaryProps) { +export function CopySummary({ record, supportedVersions, onShowFeatures, onRelocate, onError }: CopySummaryProps) { const { t, i18n } = useTranslation(); const [name, setName] = useState(record.name); const [importing, setImporting] = useState(false); @@ -52,7 +53,18 @@ export function CopySummary({ record, supportedVersions, onShowFeatures, onError {verified ? t('adaptation.verified') : t('adaptation.unverified')} - {folder(record.copyPath)} + + {record.copyPath} + {record.folderMissing && {t('copies.missingFolder')}} + + {!record.folderMissing && ( + + )} + {t(`copies.summary.methods.${record.hardlinkedFiles?.length ? 'hardlink' : record.method}`)} {record.sourcePath && {folder(record.sourcePath)}} {folder(record.stockBackupPath)} diff --git a/apps/launcher/src/renderer/src/pages/home/HomeSide.tsx b/apps/launcher/src/renderer/src/pages/home/HomeSide.tsx index 87ff5f0..8da9e7d 100644 --- a/apps/launcher/src/renderer/src/pages/home/HomeSide.tsx +++ b/apps/launcher/src/renderer/src/pages/home/HomeSide.tsx @@ -39,7 +39,10 @@ export function HomeSide({ record, copies, supportedVersions, onNavigate, onSele <>

{t('side.currentCopy')}

))} diff --git a/apps/launcher/src/renderer/src/pages/home/home-bar.ts b/apps/launcher/src/renderer/src/pages/home/home-bar.ts index 00b3cd8..3f04478 100644 --- a/apps/launcher/src/renderer/src/pages/home/home-bar.ts +++ b/apps/launcher/src/renderer/src/pages/home/home-bar.ts @@ -8,6 +8,7 @@ interface LaunchActions { readonly start: () => void; readonly stop: () => void; readonly checkVersion: () => void; + readonly relocate: () => void; } /** The installation and launch actions shown outside the wizard. */ @@ -90,6 +91,14 @@ export function homeBar( primary: { label: t('bar.install'), onClick: onInstall }, }; } + if (record.folderMissing) { + return { + tone: 'bad', + title: t('bar.missingFolder'), + detail: record.copyPath, + primary: { label: t('copies.relocate'), onClick: actions.relocate }, + }; + } const missing = missingFeatures(record).length; return { tone: missing ? 'warn' : 'ok', diff --git a/apps/launcher/src/shared/ipc/contract.ts b/apps/launcher/src/shared/ipc/contract.ts index 8f4a485..1f78ebf 100644 --- a/apps/launcher/src/shared/ipc/contract.ts +++ b/apps/launcher/src/shared/ipc/contract.ts @@ -132,6 +132,10 @@ export const ipcContract = { input: z.object({ id: CopyIdSchema, name: CopyNameSchema }), output: CopiesSchema, }, + 'copies:relocate': { + input: z.object({ id: CopyIdSchema, path: z.string().min(1) }), + output: CopiesSchema, + }, 'copies:setDecalSource': { input: z.object({ id: CopyIdSchema, path: z.string().min(1).nullable() }), output: CopiesSchema, diff --git a/apps/launcher/src/shared/schemas/install.ts b/apps/launcher/src/shared/schemas/install.ts index b1ad893..83fb762 100644 --- a/apps/launcher/src/shared/schemas/install.ts +++ b/apps/launcher/src/shared/schemas/install.ts @@ -164,6 +164,8 @@ export const InstallRecordSchema = z.object({ sourceFiles: z.array(z.string().min(1)).optional(), /** Relative resource paths linked to the source and detached on a version update. */ hardlinkedFiles: z.array(z.string().min(1)).optional(), + /** Runtime check hydrated when listing copies (never persisted to copies.json): true if copyPath or core game files are missing on disk. */ + folderMissing: z.boolean().optional(), }); export type InstallRecord = z.output;