feat(saves): add save archives and copy transfers

This commit is contained in:
N0VAN0M4LY committed 2026-11-29 15:55:23 +08:00
1 parent 073a907617
commit d0a767e692
19 files changed
+660 -25

No files matched your search

+1 -1
View File
@@ -134,7 +134,7 @@ export function App({ info, initialSettings, initialCopies }: AppProps) {
return {
side: <SubNav sections={SAVE_SECTIONS} active={route.saves} label={(section) => t(`saves.sections.${section}`)} onSelect={(section) => go({ page: 'saves', section })} />,
main: record
? <SavesPage key={record.id} copy={record} section={route.saves} readOnly={['checking', 'building', 'starting-server', 'starting-game', 'running', 'stopping'].includes(launch.phase)} onError={report} />
? <SavesPage key={record.id} copy={record} copies={registry.copies} section={route.saves} readOnly={['checking', 'building', 'starting-server', 'starting-game', 'running', 'stopping'].includes(launch.phase)} onError={report} />
: <Sheet title={t(`saves.sections.${route.saves}`)}><p className="lead">{t('saves.noCopy')}</p></Sheet>,
bar: home,
};
@@ -257,7 +257,29 @@
"list": "All saves",
"overview": "Overview",
"battles": "Battle history",
"vehicles": "Vehicles and research"
"vehicles": "Vehicles and research",
"backup": "Backup and restore"
},
"archive": {
"fromCopy": "Copy from another copy",
"import": "Import save file…",
"sourceCopy": "Source copy",
"transfer": "Create from save",
"copy": "Copy",
"export": "Export…",
"delete": "Delete…",
"confirmDelete": "Delete the local account and save for {{name}}?",
"automatic": "Back up before launch",
"retention": "The latest 10 automatic backups are retained for each save.",
"backup": "Back up now",
"restore": "Restore",
"noBackups": "There are no backups for this save.",
"confirmRestore": "Restore this backup? The current save will be backed up first.",
"type": {
"manual": "Manual backup",
"launch": "Before launch",
"restore": "Before restore"
}
},
"create": "Create save",
"login": "Login",
@@ -398,6 +420,7 @@
"internal_error": "An internal launcher error occurred.",
"logs_folder_open_failed": "The launcher log folder could not be opened.",
"diagnostics_export_failed": "The diagnostics archive could not be saved.",
"save_archive_invalid": "The selected file is not a supported WTOffline save archive.",
"save_not_found": "The selected save could not be found.",
"save_read_only": "Saves are read-only while the game or local server is running.",
"save_operation_failed": "The save operation failed. See the launcher log.",
@@ -257,7 +257,29 @@
"list": "全部存档",
"overview": "概览",
"battles": "战绩",
"vehicles": "载具与研发"
"vehicles": "载具与研发",
"backup": "备份与还原"
},
"archive": {
"fromCopy": "从其他副本复制",
"import": "导入存档文件…",
"sourceCopy": "来源副本",
"transfer": "创建存档",
"copy": "复制",
"export": "导出…",
"delete": "删除…",
"confirmDelete": "确认删除 {{name}} 的本地账号和存档?",
"automatic": "启动前自动备份",
"retention": "每个存档保留最近 10 份自动备份。",
"backup": "立即备份",
"restore": "还原",
"noBackups": "此存档没有备份。",
"confirmRestore": "还原此备份?将先备份当前存档。",
"type": {
"manual": "手动备份",
"launch": "启动前自动备份",
"restore": "还原前自动备份"
}
},
"create": "新建存档",
"login": "登录账号",
@@ -398,6 +420,7 @@
"internal_error": "启动器内部错误。",
"logs_folder_open_failed": "无法打开启动器日志文件夹。",
"diagnostics_export_failed": "无法保存诊断信息。",
"save_archive_invalid": "所选文件不是受支持的 WTOffline 存档归档。",
"save_not_found": "找不到所选存档。",
"save_read_only": "游戏或本地服务器运行期间存档为只读。",
"save_operation_failed": "存档操作失败。请查看启动器日志。",
@@ -0,0 +1,76 @@
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import type { SaveBackup, SaveDetails } from '@shared/schemas/saves.ts';
import { Row } from '../../components/Row.tsx';
import { formatBytes } from '../../format.ts';
import { ipcInvoke } from '../../ipc.ts';
interface Props {
readonly copyId: string;
readonly userId: string;
readonly readOnly: boolean;
readonly onError: (error: unknown) => void;
readonly onRestored: (details: SaveDetails) => void;
}
export function SaveBackups({ copyId, userId, readOnly, onError, onRestored }: Props) {
const { t, i18n } = useTranslation();
const [backups, setBackups] = useState<SaveBackup[]>([]);
const [automatic, setAutomatic] = useState(true);
const [busy, setBusy] = useState(false);
const [restore, setRestore] = useState<SaveBackup | null>(null);
const keys = { copyId, userId };
useEffect(() => {
let mounted = true;
Promise.all([ipcInvoke('saves:listBackups', { copyId, userId }), ipcInvoke('settings:get')]).then(([list, settings]) => {
if (!mounted) return;
setBackups(list);
setAutomatic(settings.autoSaveBackups);
}).catch(onError);
return () => { mounted = false; };
}, [copyId, userId, onError]);
function take(): void {
setBusy(true);
ipcInvoke('saves:takeBackup', keys).then(setBackups).catch(onError).finally(() => setBusy(false));
}
function apply(name: string): void {
setBusy(true);
ipcInvoke('saves:restoreBackup', { ...keys, name }).then(async (details) => {
onRestored(details);
setRestore(null);
setBackups(await ipcInvoke('saves:listBackups', keys));
}).catch(onError).finally(() => setBusy(false));
}
return (
<>
<Row label={t('saves.archive.automatic')} hint={t('saves.archive.retention')}>
<input type="checkbox" aria-label={t('saves.archive.automatic')} checked={automatic} onChange={(event) => {
ipcInvoke('settings:update', { autoSaveBackups: event.target.checked })
.then((settings) => setAutomatic(settings.autoSaveBackups)).catch(onError);
}} />
</Row>
{readOnly && <p className="small">{t('saves.readOnly')}</p>}
<button type="button" className="button" disabled={busy || readOnly} onClick={take}>{t('saves.archive.backup')}</button>
{backups.length === 0 && <p className="lead">{t('saves.archive.noBackups')}</p>}
{backups.map((backup) => (
<Row key={backup.name} label={new Date(backup.createdAt).toLocaleString(i18n.language)} hint={t(`saves.archive.type.${backup.type}`)}>
<span>{formatBytes(backup.size)}</span>
<button type="button" className="button" disabled={busy || readOnly} onClick={() => setRestore(backup)}>{t('saves.archive.restore')}</button>
</Row>
))}
{restore && (
<div className="maintenance-confirm">
<p>{t('saves.archive.confirmRestore')}</p>
<div className="copy-list-actions">
<button type="button" className="button" disabled={busy} onClick={() => setRestore(null)}>{t('wizard.cancel')}</button>
<button type="button" className="button button-primary" disabled={busy || readOnly} onClick={() => apply(restore.name)}>{t('saves.archive.restore')}</button>
</div>
</div>
)}
</>
);
}
@@ -1,14 +1,16 @@
import { useEffect, useState, type FormEvent } from 'react';
import { useTranslation } from 'react-i18next';
import type { InstallRecord } from '@shared/schemas/install.ts';
import type { CurrencyEdits, SaveDetails, SaveList } from '@shared/schemas/saves.ts';
import type { CurrencyEdits, SaveDetails, SaveList, SaveTransfer } from '@shared/schemas/saves.ts';
import { Row } from '../../components/Row.tsx';
import { Sheet } from '../../components/Sheet.tsx';
import { ipcInvoke } from '../../ipc.ts';
import type { SaveSection } from '../../shell/navigation.ts';
import { SaveBackups } from './SaveBackups.tsx';
interface SavesPageProps {
readonly copy: InstallRecord;
readonly copies: readonly InstallRecord[];
readonly section: SaveSection;
readonly readOnly: boolean;
readonly onError: (error: unknown) => void;
@@ -16,11 +18,15 @@ interface SavesPageProps {
const CURRENCIES = ['silverLions', 'goldenEagles', 'gjnCents', 'freeRp'] as const;
export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps) {
export function SavesPage({ copy, copies, section, readOnly, onError }: SavesPageProps) {
const { t, i18n } = useTranslation();
const [list, setList] = useState<SaveList | null>(null);
const [details, setDetails] = useState<SaveDetails | null>(null);
const [creating, setCreating] = useState(false);
const [source, setSource] = useState<SaveTransfer['source'] | null>(null);
const [selectingSource, setSelectingSource] = useState(false);
const [other, setOther] = useState<{ copyId: string; saves: SaveList['saves'] } | null>(null);
const [deleting, setDeleting] = useState<SaveList['saves'][number] | null>(null);
const [saving, setSaving] = useState(false);
const [message, setMessage] = useState('');
const userId = list?.current ?? null;
@@ -45,6 +51,18 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
const date = (time: number | null): string => time ? new Date(time * 1000).toLocaleString(i18n.language) : '—';
const format = new Intl.NumberFormat(i18n.language);
const begin = (from: SaveTransfer['source'] | null): void => {
setSelectingSource(false);
setSource(from);
setMessage('');
setCreating(true);
};
const updateDetails = (updated: SaveDetails): void => {
setDetails(updated);
setList((current) => current && { ...current, saves: current.saves.map((save) => save.userId === updated.save.userId ? updated.save : save) });
};
const submitNew = (event: FormEvent<HTMLFormElement>): void => {
event.preventDefault();
const data = new FormData(event.currentTarget);
@@ -55,7 +73,9 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
}
setSaving(true);
setMessage('');
ipcInvoke('saves:create', { copyId: copy.id, login: String(data.get('login') ?? ''), password, nick: String(data.get('nick') ?? '') })
const request = { copyId: copy.id, login: String(data.get('login') ?? ''), password, nick: String(data.get('nick') ?? '') };
const operation = source ? ipcInvoke('saves:transfer', { ...request, source }) : ipcInvoke('saves:create', request);
operation
.then((next) => { setList(next); setCreating(false); })
.catch(onError)
.finally(() => setSaving(false));
@@ -74,8 +94,7 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
setMessage('');
ipcInvoke('saves:editCurrencies', { copyId: copy.id, userId, values })
.then((updated) => {
setDetails(updated);
setList((current) => current && { ...current, saves: current.saves.map((save) => save.userId === userId ? updated.save : save) });
updateDetails(updated);
setMessage(t('saves.saved'));
})
.catch(onError)
@@ -87,10 +106,37 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
return (
<Sheet title={title} aside={copy.name}>
{readOnly && <p className="small">{t('saves.readOnly')}</p>}
<button type="button" className="button button-primary" disabled={readOnly || creating} onClick={() => {
setMessage('');
setCreating(true);
}}>{t('saves.create')}</button>
<div className="copy-list-actions">
<button type="button" className="button button-primary" disabled={readOnly || creating} onClick={() => begin(null)}>{t('saves.create')}</button>
<button type="button" className="button" disabled={readOnly || creating || copies.length <= 1} onClick={() => setSelectingSource(!selectingSource)}>
{t('saves.archive.fromCopy')}
</button>
<button type="button" className="button" disabled={readOnly || creating} onClick={() => {
ipcInvoke('saves:pickArchive').then(({ path }) => { if (path) begin({ kind: 'file', path }); }).catch(onError);
}}>{t('saves.archive.import')}</button>
</div>
{selectingSource && (
<div className="maintenance-confirm">
<Row label={t('saves.archive.sourceCopy')}>
<select className="select" aria-label={t('saves.archive.sourceCopy')} value={other?.copyId ?? ''} onChange={(event) => {
const copyId = event.target.value;
if (!copyId) { setOther(null); return; }
setOther({ copyId, saves: [] });
ipcInvoke('saves:list', { copyId }).then(({ saves }) =>
setOther((previous) => previous?.copyId === copyId ? { copyId, saves } : previous)).catch(onError);
}}>
<option value="">{t('folder.none')}</option>
{copies.filter(({ id }) => id !== copy.id).map(({ id, name }) => <option key={id} value={id}>{name}</option>)}
</select>
</Row>
{other?.saves.map((save) => (
<button key={save.userId} type="button" className="button" onClick={() => begin({ kind: 'save', copyId: other.copyId, userId: save.userId })}>
{save.nick} · {save.login}
</button>
))}
{other && other.saves.length === 0 && <p className="small">{t('saves.empty')}</p>}
</div>
)}
{creating && (
<form className="maintenance-confirm" onSubmit={submitNew}>
<Row label={t('saves.login')}><input className="input" aria-label={t('saves.login')} autoComplete="username" name="login" required /></Row>
@@ -100,7 +146,7 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
{message && <p className="error-text" role="alert">{message}</p>}
<div className="copy-list-actions">
<button type="button" className="button" disabled={saving} onClick={() => setCreating(false)}>{t('wizard.cancel')}</button>
<button type="submit" className="button button-primary" disabled={saving || readOnly}>{t('saves.create')}</button>
<button type="submit" className="button button-primary" disabled={saving || readOnly}>{t(source ? 'saves.archive.transfer' : 'saves.create')}</button>
</div>
</form>
)}
@@ -119,14 +165,40 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
<dt>{t('saves.battles')}</dt><dd>{format.format(save.battleCount)}</dd>
<dt>{t('saves.lastBattle')}</dt><dd>{date(save.lastBattle)}</dd>
</dl>
{save.userId !== list.current && (
<button type="button" className="button" onClick={() => {
ipcInvoke('saves:setCurrent', { copyId: copy.id, userId: save.userId }).then(setList).catch(onError);
}}>{t('copies.list.select')}</button>
)}
<div className="copy-list-actions">
{save.userId !== list.current && (
<button type="button" className="button" onClick={() => {
ipcInvoke('saves:setCurrent', { copyId: copy.id, userId: save.userId }).then(setList).catch(onError);
}}>{t('copies.list.select')}</button>
)}
<button type="button" className="button" disabled={readOnly || creating} onClick={() => begin({ kind: 'save', copyId: copy.id, userId: save.userId })}>
{t('saves.archive.copy')}
</button>
<button type="button" className="button" onClick={() => ipcInvoke('saves:export', { copyId: copy.id, userId: save.userId }).catch(onError)}>
{t('saves.archive.export')}
</button>
<button type="button" className="button" disabled={readOnly || saving} onClick={() => setDeleting(save)}>
{t('saves.archive.delete')}
</button>
</div>
</article>
))}
</div>
{deleting && (
<div className="maintenance-confirm">
<p>{t('saves.archive.confirmDelete', { name: deleting.nick })}</p>
<div className="copy-list-actions">
<button type="button" className="button" disabled={saving} onClick={() => setDeleting(null)}>{t('wizard.cancel')}</button>
<button type="button" className="button button-primary" disabled={readOnly || saving} onClick={() => {
setSaving(true);
ipcInvoke('saves:delete', { copyId: copy.id, userId: deleting.userId }).then((next) => {
setList(next);
setDeleting(null);
}).catch(onError).finally(() => setSaving(false));
}}>{t('saves.archive.delete')}</button>
</div>
</div>
)}
</Sheet>
);
}
@@ -164,6 +236,7 @@ export function SavesPage({ copy, section, readOnly, onError }: SavesPageProps)
</Row>
))
)}
{section === 'backup' && <SaveBackups key={save.userId} copyId={copy.id} userId={save.userId} readOnly={readOnly} onError={onError} onRestored={updateDetails} />}
{section === 'vehicles' && (
<>
<Row label={t('saves.owned')}>{details.owned.length ? details.owned.join(', ') : '—'}</Row>
@@ -6,7 +6,7 @@
export const PAGES = ['home', 'saves', 'copies', 'settings', 'about'] as const;
export type PageId = (typeof PAGES)[number];
export const SAVE_SECTIONS = ['list', 'overview', 'battles', 'vehicles'] as const;
export const SAVE_SECTIONS = ['list', 'overview', 'battles', 'vehicles', 'backup'] as const;
export type SaveSection = (typeof SAVE_SECTIONS)[number];
export const COPY_SECTIONS = ['list', 'summary', 'features', 'maintain'] as const;