Add user-initiated read-only discovery of local online client settings, copy selected files into the offline copy's staging directory before preview/import, clear the offline client's local cache for the imported account, and refresh save summaries after import.
190 lines
11 KiB
TypeScript
190 lines
11 KiB
TypeScript
import { app, BrowserWindow, dialog, shell } from 'electron';
|
|
import { isInSteamLibrary, patchFeatures, recordedGameVersions } from '@wto/patcher';
|
|
import { AppError } from '@shared/errors.ts';
|
|
import { appUpdatesSupported, checkAppUpdate, downloadAppUpdate, installAppUpdate } from '../services/app-update/app-update.ts';
|
|
import { listBackgroundPictures } from '../services/background-art/art-catalog.ts';
|
|
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 { 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 { 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 { 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';
|
|
import { resourcePaths } from '../services/resources/resource-paths.ts';
|
|
import { changeSaveAccount, changeSaveResearch, copyOnlineData, createSave, discoverOnlineData, exportSave, getSaveAvatarChoices, getSaveAvatarPreview, getSaveDetails, getSaveList, getSaveResearch, 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/. */
|
|
export function registerIpcHandlers(): void {
|
|
handle('app:getInfo', () => {
|
|
const resources = resourcePaths();
|
|
return {
|
|
version: app.getVersion(),
|
|
electron: process.versions.electron,
|
|
development: !app.isPackaged,
|
|
appUpdatesSupported: appUpdatesSupported(),
|
|
systemLocale: app.getLocale(),
|
|
userDataPath: app.getPath('userData'),
|
|
supportedGameVersions: recordedGameVersions(resources.patches),
|
|
};
|
|
});
|
|
handle('app:listBackgrounds', async () => {
|
|
const [record, settings] = await Promise.all([readInstallRecord(), getSettings()]);
|
|
return listBackgroundPictures(record?.copyPath ?? null, settings.officialWallpapers);
|
|
});
|
|
handle('app:openFolder', async ({ path }) => {
|
|
await requireFolder(path);
|
|
const failure = await shell.openPath(path);
|
|
if (failure) console.warn('[app] opening the folder failed:', path, failure);
|
|
});
|
|
|
|
handle('app:openLogsFolder', () => openLogsFolder());
|
|
handle('app:exportDiagnostics', (_input, sender) => exportDiagnostics(BrowserWindow.fromWebContents(sender)));
|
|
|
|
handle('window:minimize', (_input, sender) => {
|
|
BrowserWindow.fromWebContents(sender)?.minimize();
|
|
});
|
|
handle('window:close', (_input, sender) => {
|
|
BrowserWindow.fromWebContents(sender)?.close();
|
|
});
|
|
|
|
handle('dialog:pickFolder', async ({ title }, sender) => {
|
|
const window = BrowserWindow.fromWebContents(sender);
|
|
const options = { properties: ['openDirectory', 'createDirectory'] as Array<'openDirectory' | 'createDirectory'>, ...(title ? { title } : {}) };
|
|
const result = window ? await dialog.showOpenDialog(window, options) : await dialog.showOpenDialog(options);
|
|
return { path: result.canceled ? null : (result.filePaths[0] ?? null) };
|
|
});
|
|
|
|
handle('install:prepareCopy', () => createCopyDraft());
|
|
handle('install:inspectGameFolder', ({ path }) => inspectGameFolder(path));
|
|
handle('install:inspectTargetFolder', ({ path, stockPath }) => inspectTargetFolder(path, stockPath));
|
|
handle('install:inspectDataFolder', async ({ path }) => {
|
|
const resolved = await canonicalCopyPath(path);
|
|
return { path: resolved, inSteam: isInSteamLibrary(path) || isInSteamLibrary(resolved) };
|
|
});
|
|
handle('install:measureFolder', async ({ path, version }) => {
|
|
await requireFolder(path);
|
|
try {
|
|
return await measureFolder(path, version);
|
|
} catch (error) {
|
|
console.warn('[install] measuring the folder failed:', error);
|
|
throw new AppError('folder_unreadable', { path });
|
|
}
|
|
});
|
|
|
|
handle('install:checkPatches', ({ gamePath, dataPath }) => checkGameFolder(gamePath, dataPath));
|
|
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());
|
|
return record;
|
|
});
|
|
handle('install:cancel', () => ({ cancelled: cancelInstall() }));
|
|
handle('install:getRecord', async () => ({ record: await readInstallRecord() }));
|
|
handle('install:listFeatures', () => {
|
|
const resources = resourcePaths();
|
|
return patchFeatures(resources.patches).map((feature) => ({ ...feature, patchIds: [...feature.patchIds] }));
|
|
});
|
|
|
|
handle('copies:list', () => listCopies());
|
|
handle('copies:setCurrent', async ({ id }) => {
|
|
const registry = await setCurrentCopy(id);
|
|
broadcast('event:copies-changed', registry);
|
|
return registry;
|
|
});
|
|
handle('copies:rename', async ({ id, name }) => {
|
|
const registry = await renameCopy(id, name);
|
|
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);
|
|
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));
|
|
broadcast('event:copies-changed', updated);
|
|
return updated;
|
|
});
|
|
handle('copies:cancelMaintenance', () => cancelMaintenance());
|
|
|
|
handle('saves:list', ({ copyId }) => getSaveList(copyId));
|
|
handle('saves:details', ({ copyId, userId }) => getSaveDetails(copyId, userId));
|
|
handle('saves:setCurrent', ({ copyId, userId }) => selectSave(copyId, userId));
|
|
handle('saves:create', (request) => createSave(request));
|
|
handle('saves:avatars', ({ copyId }) => getSaveAvatarChoices(copyId));
|
|
handle('saves:avatarPreview', ({ copyId, avatar }) => getSaveAvatarPreview(copyId, avatar));
|
|
handle('saves:editAccount', ({ copyId, userId, edits }) => changeSaveAccount(copyId, userId, edits));
|
|
handle('saves:editCurrencies', ({ copyId, userId, values }) => setSaveCurrencies(copyId, userId, values));
|
|
|
|
handle('saves:pickArchive', (_input, sender) => pickSaveArchive(BrowserWindow.fromWebContents(sender)));
|
|
handle('saves:export', ({ copyId, userId }, sender) => exportSave(copyId, userId, BrowserWindow.fromWebContents(sender)));
|
|
handle('saves:transfer', (request) => transferSave(request));
|
|
handle('saves:delete', ({ copyId, userId }) => removeSave(copyId, userId));
|
|
handle('saves:listBackups', ({ copyId, userId }) => listSaveBackups(copyId, userId));
|
|
handle('saves:takeBackup', ({ copyId, userId }) => takeSaveBackup(copyId, userId));
|
|
handle('saves:restoreBackup', ({ copyId, userId, name }) => restoreSaveBackup(copyId, userId, name));
|
|
|
|
handle('saves:discoverOnlineData', () => discoverOnlineData());
|
|
handle('saves:copyOnlineData', ({ copyId, userId, accountId }) => copyOnlineData(copyId, userId, accountId));
|
|
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('saves:research', ({ copyId, userId, branch }) => getSaveResearch(copyId, userId, branch));
|
|
handle('saves:editResearch', ({ copyId, userId, branch, action }) => changeSaveResearch(copyId, userId, branch, action));
|
|
|
|
handle('tuning:get', ({ copyId }) => getCopyTuning(copyId));
|
|
handle('tuning:update', ({ copyId, patch }) => updateCopyTuning(copyId, patch));
|
|
|
|
handle('launch:getState', () => getLaunchState());
|
|
handle('launch:getLabInfo', () => readInstalledLab().catch(() => null));
|
|
handle('launch:start', () => {
|
|
if (maintenanceInProgress() || saveWriteInProgress() || decalImportInProgress()) throw new AppError('maintenance_busy');
|
|
return startLaunch((state) => broadcast('event:launch-state', state));
|
|
});
|
|
handle('launch:stop', async () => { await stopLaunch(); });
|
|
handle('launch:openServerStatus', () => openServerStatus());
|
|
|
|
handle('patches:active', () => activePatchSet());
|
|
handle('patches:check', () => checkPackUpdate());
|
|
handle('patches:apply', async () => {
|
|
// A build reads the patch set while it runs; switch only between builds.
|
|
if (maintenanceInProgress()) throw new AppError('maintenance_busy');
|
|
const result = await applyPackUpdate();
|
|
if (result.ok) broadcast('event:patch-set-changed', result.active);
|
|
return result;
|
|
});
|
|
handle('patches:useBundled', async () => {
|
|
if (maintenanceInProgress()) throw new AppError('maintenance_busy');
|
|
const active = await revertToBundledPatchSet();
|
|
broadcast('event:patch-set-changed', active);
|
|
return active;
|
|
});
|
|
|
|
handle('updates:check', () => checkAppUpdate());
|
|
handle('updates:download', ({ version }) => downloadAppUpdate(version, (progress) => broadcast('event:app-update-progress', progress)));
|
|
handle('updates:install', ({ version }) => installAppUpdate(version, () =>
|
|
launchInProgress() || installationInProgress() || maintenanceInProgress() || saveWriteInProgress() || decalImportInProgress()));
|
|
|
|
handle('settings:get', () => getSettings());
|
|
handle('settings:update', async (patch) => {
|
|
const settings = await updateSettings(patch);
|
|
broadcast('event:settings-changed', settings);
|
|
return settings;
|
|
});
|
|
}
|