feat(saves): import official game backups
This commit is contained in:
1 parent
2e3cc0aaec
commit
597df6b4f5
15 files changed
+661
-16
No files matched your search
@@ -13,7 +13,7 @@ import { createCopyDraft, listCopies, readInstallRecord, renameCopy, setCurrentC
|
||||
import { readInstalledLab } from '../services/launch/lab-handoff.ts';
|
||||
import { getLaunchState, openServerStatus, startLaunch, stopLaunch } from '../services/launch/launch-controller.ts';
|
||||
import { resourcePaths } from '../services/resources/resource-paths.ts';
|
||||
import { createSave, exportSave, getSaveDetails, getSaveList, listSaveBackups, pickSaveArchive, removeSave, restoreSaveBackup, saveWriteInProgress, selectSave, setSaveCurrencies, takeSaveBackup, transferSave } from '../services/saves/manage-saves.ts';
|
||||
import { createSave, exportSave, getSaveDetails, getSaveList, importSelectedBackups, listSaveBackups, pickOfficialBackups, pickSaveArchive, previewSelectedBackups, removeSave, restoreSaveBackup, saveWriteInProgress, selectSave, setSaveCurrencies, takeSaveBackup, transferSave } from '../services/saves/manage-saves.ts';
|
||||
import { broadcast, handle } from './register.ts';
|
||||
|
||||
/** Registers the handler of every contract channel. One line per channel; the work is in services/. */
|
||||
@@ -118,6 +118,12 @@ export function registerIpcHandlers(): void {
|
||||
handle('saves:takeBackup', ({ copyId, userId }) => takeSaveBackup(copyId, userId));
|
||||
handle('saves:restoreBackup', ({ copyId, userId, name }) => restoreSaveBackup(copyId, userId, name));
|
||||
|
||||
handle('saves:pickOfficialBackups', (_input, sender) => pickOfficialBackups(BrowserWindow.fromWebContents(sender)));
|
||||
handle('saves:previewOfficial', ({ copyId, userId, paths, includeUsedVehicles }) =>
|
||||
previewSelectedBackups(copyId, userId, paths, includeUsedVehicles));
|
||||
handle('saves:importOfficial', ({ copyId, userId, paths, options }) =>
|
||||
importSelectedBackups(copyId, userId, paths, options));
|
||||
|
||||
handle('launch:getState', () => getLaunchState());
|
||||
handle('launch:getLabInfo', () => readInstalledLab().catch(() => null));
|
||||
handle('launch:start', () => {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { join } from 'node:path';
|
||||
import { dialog, utilityProcess, type BrowserWindow } from 'electron';
|
||||
import { compareVersions } from '@wto/patcher';
|
||||
import { isAbsolute, join } from 'node:path';
|
||||
import { app, dialog, utilityProcess, type BrowserWindow } from 'electron';
|
||||
import { compareVersions, isInSteamLibrary } from '@wto/patcher';
|
||||
import { AppError } from '@shared/errors.ts';
|
||||
import type { InstallRecord } from '@shared/schemas/install.ts';
|
||||
import type { CurrencyEdits, SaveBackup, SaveDetails, SaveList, SaveRegistration, SaveTransfer } from '@shared/schemas/saves.ts';
|
||||
import type { CurrencyEdits, OfficialImportOptions, OfficialPreview, SaveBackup, SaveDetails, SaveList, SaveRegistration, SaveTransfer } from '@shared/schemas/saves.ts';
|
||||
import type { SaveReply, SaveResult, SaveTask } from '../../workers/save-protocol.ts';
|
||||
import { maintenanceInProgress } from '../copies/copy-maintenance.ts';
|
||||
import { listCopies, updateCopyRecord } from '../install/install-record.ts';
|
||||
import { canonicalCopyPath, copyPathContains, listCopies, updateCopyRecord } from '../install/install-record.ts';
|
||||
import { gameProcessRunning, launchInProgress } from '../launch/launch-controller.ts';
|
||||
import { labStatusUrl, readInstalledLab } from '../launch/lab-handoff.ts';
|
||||
import { forwardProcessOutput } from '../launch/process-output.ts';
|
||||
@@ -152,6 +152,52 @@ export async function restoreSaveBackup(copyId: string, userId: string, name: st
|
||||
});
|
||||
}
|
||||
|
||||
export async function pickOfficialBackups(window: BrowserWindow | null): Promise<string[]> {
|
||||
const options = { properties: ['openFile' as const, 'multiSelections' as const], filters: [{ name: 'BLK', extensions: ['blk'] }] };
|
||||
const result = window ? await dialog.showOpenDialog(window, options) : await dialog.showOpenDialog(options);
|
||||
return result.canceled ? [] : result.filePaths;
|
||||
}
|
||||
|
||||
export async function previewSelectedBackups(
|
||||
copyId: string, userId: string, paths: readonly string[], includeUsedVehicles: boolean,
|
||||
): Promise<OfficialPreview> {
|
||||
const copy = await requireCopy(copyId);
|
||||
await getSaveDetails(copyId, userId);
|
||||
const selected = await verifyBackupLocations(copy, paths);
|
||||
return await run({ operation: 'previewOfficial', ...location(copy), paths: selected, includeUsedVehicles }) as OfficialPreview;
|
||||
}
|
||||
|
||||
export async function importSelectedBackups(
|
||||
copyId: string, userId: string, paths: readonly string[], options: OfficialImportOptions,
|
||||
): Promise<OfficialPreview> {
|
||||
return write(async () => {
|
||||
const copy = await requireCopy(copyId);
|
||||
await getSaveDetails(copyId, userId);
|
||||
const selected = await verifyBackupLocations(copy, paths);
|
||||
const before = await captureSave(copy, userId);
|
||||
await saveBackup(copy, userId, 'import');
|
||||
try {
|
||||
return await run({ operation: 'importOfficial', ...location(copy), userId, paths: selected, options }) as OfficialPreview;
|
||||
} catch (error) {
|
||||
await restoreDirectory(copy, userId, before);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function verifyBackupLocations(copy: InstallRecord, paths: readonly string[]): Promise<string[]> {
|
||||
const documents = await canonicalCopyPath(join(app.getPath('documents'), 'My Games', 'WarThunder'));
|
||||
const stock = copy.sourcePath && await canonicalCopyPath(copy.sourcePath);
|
||||
return Promise.all(paths.map(async (path) => {
|
||||
if (!isAbsolute(path)) throw new AppError('save_official_location');
|
||||
const selected = await canonicalCopyPath(path);
|
||||
if (isInSteamLibrary(selected) || copyPathContains(documents, selected) || (stock && copyPathContains(stock, selected))) {
|
||||
throw new AppError('save_official_location');
|
||||
}
|
||||
return selected;
|
||||
}));
|
||||
}
|
||||
|
||||
async function validateProfile(archive: SaveArchive, copy: InstallRecord): Promise<void> {
|
||||
const profile = archive.files['profile.blk'];
|
||||
if (!profile) return;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { AppErrorCode } from '@shared/errors.ts';
|
||||
import type { CurrencyEdits, SaveDetails, SaveList } from '@shared/schemas/saves.ts';
|
||||
import type { CurrencyEdits, OfficialImportOptions, OfficialPreview, SaveDetails, SaveList } from '@shared/schemas/saves.ts';
|
||||
|
||||
interface Location {
|
||||
readonly gameFolder: string;
|
||||
@@ -10,9 +10,11 @@ export type SaveTask = Location & (
|
||||
| { readonly operation: 'list' }
|
||||
| { readonly operation: 'details'; readonly userId: string }
|
||||
| { readonly operation: 'edit'; readonly userId: string; readonly values: CurrencyEdits }
|
||||
| { readonly operation: 'previewOfficial'; readonly paths: readonly string[]; readonly includeUsedVehicles: boolean }
|
||||
| { readonly operation: 'importOfficial'; readonly paths: readonly string[]; readonly userId: string; readonly options: OfficialImportOptions }
|
||||
| { readonly operation: 'remove'; readonly userId: string }
|
||||
| { readonly operation: 'filter'; readonly profile: Uint8Array; readonly filter: boolean }
|
||||
| { readonly operation: 'create'; readonly login: string; readonly password: string; readonly nick: string }
|
||||
);
|
||||
export type SaveResult = SaveList['saves'] | SaveDetails | { userId: string } | Uint8Array | null;
|
||||
export type SaveResult = SaveList['saves'] | SaveDetails | OfficialPreview | { userId: string } | Uint8Array | null;
|
||||
export type SaveReply = { ok: true; result: SaveResult } | { ok: false; error: AppErrorCode };
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { MessageEvent } from 'electron';
|
||||
import { AccountStore, editSaveCurrencies, listSaveSummaries, prepareTransferredProfile, readSaveDetails } from '@wto/server';
|
||||
import { AccountStore, editSaveCurrencies, importOfficialBackup, listSaveSummaries, prepareTransferredProfile, previewOfficialBackup, readSaveDetails } from '@wto/server';
|
||||
import type { SaveTask, SaveReply } from './save-protocol.ts';
|
||||
|
||||
function execute(task: SaveTask): SaveReply {
|
||||
@@ -13,6 +13,10 @@ function execute(task: SaveTask): SaveReply {
|
||||
case 'edit':
|
||||
return editSaveCurrencies(task, task.userId, task.values)
|
||||
? { ok: true, result: null } : { ok: false, error: 'save_not_found' };
|
||||
case 'previewOfficial':
|
||||
return { ok: true, result: previewOfficialBackup(task, task.paths, task.includeUsedVehicles) };
|
||||
case 'importOfficial':
|
||||
return { ok: true, result: importOfficialBackup(task, task.userId, task.paths, task.options) };
|
||||
case 'remove':
|
||||
return new AccountStore(task.dataFolder).remove(task.userId)
|
||||
? { ok: true, result: null } : { ok: false, error: 'save_not_found' };
|
||||
@@ -37,7 +41,8 @@ process.parentPort.once('message', (event: MessageEvent) => {
|
||||
reply = execute(event.data as SaveTask);
|
||||
} catch (error) {
|
||||
console.error('[saves] the save operation failed:', error);
|
||||
reply = { ok: false, error: 'save_operation_failed' };
|
||||
const { operation } = event.data as SaveTask;
|
||||
reply = { ok: false, error: operation === 'previewOfficial' || operation === 'importOfficial' ? 'save_official_invalid' : 'save_operation_failed' };
|
||||
}
|
||||
process.parentPort.postMessage(reply);
|
||||
});
|
||||
Reference in new issue
Block a user