From 9e6e13895e8f8d8803b855705cc959dffa0d76b8 Mon Sep 17 00:00:00 2001 From: N0VAN0M4LY <224130499+N0VAN0M4LY@users.noreply.github.com> Date: Fri, 4 Dec 2026 19:55:05 +0800 Subject: [PATCH] fix(launcher): separate client removal from deleting disk files Split deleteFiles from deleteData in copy maintenance requests with safe false defaults. Block disk deletion for non-copy (in-place/existing) and Steam-library clients on both main and renderer sides, and allow pure list removal to bypass stockBackup/root checks so missing or damaged client entries can always be removed. --- .../main/services/copies/copy-maintenance.ts | 88 +++++++++++++++---- .../src/renderer/src/i18n/locales/en.json | 25 +++--- .../src/renderer/src/i18n/locales/zh-CN.json | 25 +++--- .../src/pages/copies/CopyMaintenance.tsx | 6 +- .../pages/copies/CopyMaintenanceControls.tsx | 52 ++++++++--- .../src/pages/copies/useCopyMaintenance.ts | 2 +- .../src/shared/schemas/maintenance.ts | 1 + 7 files changed, 146 insertions(+), 53 deletions(-) diff --git a/apps/launcher/src/main/services/copies/copy-maintenance.ts b/apps/launcher/src/main/services/copies/copy-maintenance.ts index db6521d..ec8e00d 100644 --- a/apps/launcher/src/main/services/copies/copy-maintenance.ts +++ b/apps/launcher/src/main/services/copies/copy-maintenance.ts @@ -1,5 +1,5 @@ import { readdir, rm } from 'node:fs/promises'; -import { isAbsolute, join, parse, relative } from 'node:path'; +import { isAbsolute, join, parse, relative, resolve } from 'node:path'; import { app } from 'electron'; import { isInSteamLibrary } from '@wto/patcher'; import { AppError } from '@shared/errors.ts'; @@ -90,10 +90,18 @@ export async function maintainCopy( } } +/** Only launcher-created copies outside any Steam library may have their game folder deleted on disk. */ +export function canDeleteCopyFiles(record: InstallRecord, root: string): boolean { + return record.method === 'copy' && !isInSteamLibrary(record.copyPath) && !isInSteamLibrary(root); +} + async function performMaintenance(request: MaintenanceRequest, signal: AbortSignal, report: Report): Promise { const { id, operation, steamLocations } = request; const record = await requireCopy(id); if (operation === 'set-source') return setCopySource(record, request.sourcePath); + if (operation === 'delete') { + return remove(record, request.deleteFiles === true, request.deleteData === true, signal, report); + } const root = await checkedRoot(record); const dataPath = await canonicalCopyPath(record.dataPath); @@ -101,7 +109,7 @@ async function performMaintenance(request: MaintenanceRequest, signal: AbortSign if (await gameProcessRunning(signal) || await labServerUses(root)) throw new AppError('maintenance_busy'); signal.throwIfAborted(); - if (operation !== 'confirm-steam' && operation !== 'delete' && needsConfirmation) { + if (operation !== 'confirm-steam' && needsConfirmation) { throw new AppError('launch_steam_confirmation_required'); } @@ -123,9 +131,6 @@ async function performMaintenance(request: MaintenanceRequest, signal: AbortSign return updateFromSource(record, root, signal, report); case 'compact': return compactCopy(record, root, signal, report); - case 'delete': - report('applying'); - return remove(record, root, request.deleteData); } } @@ -184,24 +189,73 @@ async function recordCheck(record: InstallRecord, folder: string): Promise { - if (isInSteamLibrary(record.copyPath) || isInSteamLibrary(root)) { - if (deleteData) failLocation('delete_data_blocked_in_steam', { copyPath: record.copyPath, root }); +async function remove( + record: InstallRecord, + deleteFiles: boolean, + deleteData: boolean, + signal: AbortSignal, + report: Report, +): Promise { + const canonicalRoot = await canonicalCopyPath(record.copyPath); + if (await gameProcessRunning(signal) || await labServerUses(canonicalRoot)) { + throw new AppError('maintenance_busy'); + } + signal.throwIfAborted(); + + // Pure list removal touches no files on disk and skips copyPath/stockBackup checks + // so missing, moved, or damaged client registrations can always be removed safely. + if (!deleteFiles && !deleteData) { + report('applying'); return removeCopyRecord(record.id); } - const data = deleteData ? await safeFolder(record.dataPath) : await canonicalCopyPath(record.dataPath); + const copyInSteam = isInSteamLibrary(record.copyPath) || isInSteamLibrary(canonicalRoot); + if (copyInSteam) { + failLocation(deleteFiles ? 'delete_files_blocked_in_steam' : 'delete_data_blocked_in_steam', { + method: record.method, + copyPath: record.copyPath, + root: canonicalRoot, + }); + } + + if (deleteFiles && !canDeleteCopyFiles(record, canonicalRoot)) { + failLocation('delete_files_blocked_for_method', { + method: record.method, + copyPath: record.copyPath, + root: canonicalRoot, + }); + } + + const root = deleteFiles ? await checkedRoot(record) : canonicalRoot; + if (deleteFiles && !canDeleteCopyFiles(record, root)) { + failLocation('delete_files_blocked', { method: record.method, copyPath: record.copyPath, root }); + } + + const data = deleteData ? await safeFolder(record.dataPath, false) : await canonicalCopyPath(record.dataPath); const source = record.sourcePath ? await canonicalCopyPath(record.sourcePath) : null; - if (deleteData && ((source && (copyPathContains(data, source) || copyPathContains(source, data))) || - (relative(data, root) !== '' && copyPathContains(data, root)))) { - failLocation('delete_data_overlaps_source_or_copy', { root, data, source }); + const stockBackup = await canonicalCopyPath(record.stockBackupPath); + if (deleteData) { + const rawRoot = resolve(record.copyPath); + const overlapsSource = source !== null && (copyPathContains(data, source) || copyPathContains(source, data)); + const containsCopy = deleteFiles + ? relative(data, root) !== '' && copyPathContains(data, root) + : copyPathContains(data, root) || copyPathContains(data, rawRoot) || copyPathContains(data, stockBackup); + if (overlapsSource || containsCopy) { + failLocation('delete_data_overlaps_source_or_copy', { root, data, source, stockBackup, deleteFiles }); + } } - if (!deleteData && copyPathContains(root, data)) { - await removeExcept(root, data); - } else { - await rm(root, { recursive: true, force: true }); + + report('applying'); + if (deleteFiles) { + if (!deleteData && copyPathContains(root, data)) { + await removeExcept(root, data); + } else { + await rm(root, { recursive: true, force: true }); + } + } + if (deleteData && (!deleteFiles || !copyPathContains(root, data))) { + await rm(data, { recursive: true, force: true }); } - if (deleteData && !copyPathContains(root, data)) await rm(data, { recursive: true, force: true }); return removeCopyRecord(record.id); } diff --git a/apps/launcher/src/renderer/src/i18n/locales/en.json b/apps/launcher/src/renderer/src/i18n/locales/en.json index 13e0c5e..fd5e504 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/en.json +++ b/apps/launcher/src/renderer/src/i18n/locales/en.json @@ -258,16 +258,19 @@ "repair": "Repair client", "repairAction": "Repair", "repairHint": "Restore the original files from the backup. Game files will be generated again at launch.", - "delete": "Delete client", - "deleteAction": "Delete…", - "deleteHint": "The data folder is kept by default.", - "unlink": "Remove registration", - "unlinkAction": "Remove…", - "unlinkHint": "The launcher keeps the Steam game files and data folder in place.", - "unlinkLocation": "Remove the launcher's registration for {{path}}? Steam game files and data will stay in place.", - "deleteLocation": "Delete the offline client at {{path}}?", - "deleteData": "Also delete the data folder at {{path}}", - "confirm": "Confirm deletion", + "remove": "Remove client", + "removeAction": "Remove…", + "removeCopyHint": "Removes from the launcher list by default without deleting files on disk.", + "removeUnlinkHint": "Removes only from the launcher list without deleting game files on disk.", + "removeSteamHint": "Removes only from the launcher list. Steam game files and the data folder stay in place.", + "removeCopyNotice": "Removes {{path}} from the launcher list. All game files and data on disk are kept by default.", + "removeListOnlyNotice": "Removes {{path}} from the launcher list only. Game files on disk will not be deleted.", + "removeSteamNotice": "Removes {{path}} from the launcher list only. Steam game files and data on disk will not be deleted.", + "deleteFiles": "Also delete the client folder at {{path}}", + "deleteFilesWarning": "This will permanently delete all files in the copied client folder at {{path}}. This action cannot be undone.", + "deleteData": "Also delete the data folder at {{path}} (contains saves, settings, and logs)", + "confirmRemove": "Remove from list", + "confirmDelete": "Confirm deletion", "phase": { "checking": "Checking the client", "copying": "Copying changed files", @@ -280,7 +283,7 @@ "repair": "Original files restored.", "update": "Client updated.", "compact": "Space-saving conversion complete.", - "delete": "Client deleted.", + "delete": "Client removed.", "cancelled": "Maintenance cancelled." } }, 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 8f515b8..34cb6c3 100644 --- a/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json +++ b/apps/launcher/src/renderer/src/i18n/locales/zh-CN.json @@ -258,16 +258,19 @@ "repair": "修复客户端", "repairAction": "修复", "repairHint": "从原版文件备份还原文件。下次启动时重新生成游戏文件。", - "delete": "删除客户端", - "deleteAction": "删除…", - "deleteHint": "默认保留数据目录。", - "unlink": "解除登记", - "unlinkAction": "解除登记…", - "unlinkHint": "Steam 游戏文件和数据目录将留在原处。", - "unlinkLocation": "从启动器中解除 {{path}} 的登记?Steam 游戏文件和数据将留在原处。", - "deleteLocation": "确认删除 {{path}} 中的离线客户端?", - "deleteData": "同时删除 {{path}} 中的数据", - "confirm": "确认删除", + "remove": "移除客户端", + "removeAction": "移除…", + "removeCopyHint": "默认仅从启动器列表移除,不会删除磁盘文件。", + "removeUnlinkHint": "仅从启动器列表移除,不会删除磁盘上的游戏文件。", + "removeSteamHint": "仅从启动器列表移除,Steam 游戏文件和数据目录将留在原处。", + "removeCopyNotice": "将从启动器列表移除 {{path}}。默认保留磁盘上的所有游戏文件和数据。", + "removeListOnlyNotice": "将仅从启动器列表移除 {{path}},不会删除磁盘上的游戏文件。", + "removeSteamNotice": "将仅从启动器列表移除 {{path}},不会删除磁盘上的 Steam 游戏文件和数据目录。", + "deleteFiles": "同时删除客户端文件夹 {{path}}", + "deleteFilesWarning": "将永久删除客户端副本文件夹 {{path}} 中的全部文件,此操作无法撤销。", + "deleteData": "同时删除数据文件夹 {{path}}(包含存档、配置与日志)", + "confirmRemove": "从列表移除", + "confirmDelete": "确认删除", "phase": { "checking": "正在检查客户端", "copying": "正在复制有变化的文件", @@ -280,7 +283,7 @@ "repair": "原版文件已还原。", "update": "客户端已更新。", "compact": "省空间转换已完成。", - "delete": "客户端已删除。", + "delete": "客户端已移除。", "cancelled": "维护已取消。" } }, diff --git a/apps/launcher/src/renderer/src/pages/copies/CopyMaintenance.tsx b/apps/launcher/src/renderer/src/pages/copies/CopyMaintenance.tsx index 5bb637a..5e352dc 100644 --- a/apps/launcher/src/renderer/src/pages/copies/CopyMaintenance.tsx +++ b/apps/launcher/src/renderer/src/pages/copies/CopyMaintenance.tsx @@ -20,6 +20,10 @@ export function CopyMaintenance({ record, maintenance, onError }: CopyMaintenanc const [info, setInfo] = useState(null); const [removal, setRemoval] = useState(null); + useEffect(() => { + setRemoval(null); + }, [record.id]); + useEffect(() => { let mounted = true; setInfo(null); @@ -60,7 +64,7 @@ export function CopyMaintenance({ record, maintenance, onError }: CopyMaintenanc busy={busy} options={removal} onOptionsChange={setRemoval} - onRemove={(deleteData) => run('delete', { deleteData })} + onRemove={(options) => run('delete', options)} /> {active && } {result &&

{t(`copies.maintenance.result.${result}`)}

} diff --git a/apps/launcher/src/renderer/src/pages/copies/CopyMaintenanceControls.tsx b/apps/launcher/src/renderer/src/pages/copies/CopyMaintenanceControls.tsx index f66cd7d..65cb9be 100644 --- a/apps/launcher/src/renderer/src/pages/copies/CopyMaintenanceControls.tsx +++ b/apps/launcher/src/renderer/src/pages/copies/CopyMaintenanceControls.tsx @@ -69,44 +69,72 @@ export function CopyMaintenanceActions({ record, info, busy, onRun, onChooseSour } export interface CopyRemovalOptions { + readonly deleteFiles: boolean; readonly deleteData: boolean; } interface CopyRemovalProps extends CopyMaintenanceControlsProps { readonly options: CopyRemovalOptions | null; readonly onOptionsChange: (options: CopyRemovalOptions | null) => void; - readonly onRemove: (deleteData: boolean) => void; + readonly onRemove: (options: CopyRemovalOptions) => void; } export function CopyRemoval({ record, info, busy, options, onOptionsChange, onRemove }: CopyRemovalProps) { const { t } = useTranslation(); const steam = info?.steamLocations; - const unlinkOnly = steam?.copyInSteam === true; - const canDeleteData = !unlinkOnly && !steam?.dataInSteam; - const action = t(unlinkOnly ? 'copies.maintenance.unlink' : 'copies.maintenance.confirm'); + const copyInSteam = steam?.copyInSteam === true; + const dataInSteam = steam?.dataInSteam === true; + const canDeleteFiles = info !== null && record.method === 'copy' && !copyInSteam; + const canDeleteData = info !== null && !copyInSteam && !dataInSteam; + const willDeleteFiles = canDeleteFiles && options?.deleteFiles === true; + const willDeleteData = canDeleteData && options?.deleteData === true; + const willDeleteAny = willDeleteFiles || willDeleteData; + const action = t(willDeleteAny ? 'copies.maintenance.confirmDelete' : 'copies.maintenance.confirmRemove'); + const rowHint = copyInSteam + ? t('copies.maintenance.removeSteamHint') + : record.method === 'copy' + ? t('copies.maintenance.removeCopyHint') + : t('copies.maintenance.removeUnlinkHint'); + const confirmNotice = copyInSteam + ? t('copies.maintenance.removeSteamNotice', { path: steam?.copyPath ?? record.copyPath }) + : canDeleteFiles + ? t('copies.maintenance.removeCopyNotice', { path: record.copyPath }) + : t('copies.maintenance.removeListOnlyNotice', { path: record.copyPath }); return ( <> - - {options && (
-

{t(unlinkOnly ? 'copies.maintenance.unlinkLocation' : 'copies.maintenance.deleteLocation', { path: steam?.copyPath ?? record.copyPath })}

+

{confirmNotice}

+ {canDeleteFiles && ( + + )} + {willDeleteFiles && ( + +

{t('copies.maintenance.deleteFilesWarning', { path: record.copyPath })}

+
+ )} {canDeleteData && ( )}
-
diff --git a/apps/launcher/src/renderer/src/pages/copies/useCopyMaintenance.ts b/apps/launcher/src/renderer/src/pages/copies/useCopyMaintenance.ts index 4ed7b99..b6b9910 100644 --- a/apps/launcher/src/renderer/src/pages/copies/useCopyMaintenance.ts +++ b/apps/launcher/src/renderer/src/pages/copies/useCopyMaintenance.ts @@ -4,7 +4,7 @@ import type { MaintenanceProgress, MaintenanceRequest } from '@shared/schemas/ma import { AppIpcError, ipcInvoke, ipcOn } from '../../ipc.ts'; export type MaintenanceOperation = MaintenanceRequest['operation']; -export type MaintenanceOptions = Pick, 'deleteData' | 'steamLocations' | 'sourcePath'>; +export type MaintenanceOptions = Pick, 'deleteFiles' | 'deleteData' | 'steamLocations' | 'sourcePath'>; export interface MaintenanceOutcome { readonly copyId: string; diff --git a/apps/launcher/src/shared/schemas/maintenance.ts b/apps/launcher/src/shared/schemas/maintenance.ts index 7e06f85..437daf4 100644 --- a/apps/launcher/src/shared/schemas/maintenance.ts +++ b/apps/launcher/src/shared/schemas/maintenance.ts @@ -5,6 +5,7 @@ export const MaintenanceOperationSchema = z.enum(['check', 'repair', 'update', ' export const MaintenanceRequestSchema = z.object({ id: CopyIdSchema, operation: MaintenanceOperationSchema, + deleteFiles: z.boolean().default(false), deleteData: z.boolean().default(false), sourcePath: z.string().min(1).optional(), steamLocations: z.object({