272 lines
14 KiB
TypeScript
272 lines
14 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, maintenanceState } 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, dataFolderProblem, installGame, installationInProgress } from '../services/install/install-game.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';
|
|
import {
|
|
applyModPreset,
|
|
autoSortMods,
|
|
deleteModPreset,
|
|
exportModPreset,
|
|
getModsState,
|
|
importModPreset,
|
|
openModDocs,
|
|
openModsFolder,
|
|
saveModPreset,
|
|
setModEnabled,
|
|
setModsFolder,
|
|
setModsOrder,
|
|
} from '../services/mods/mods-service.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, version }) =>
|
|
inspectPickedTargetFolder(path, stockPath, version ?? null));
|
|
handle('install:inspectDataFolder', async ({ path, copyPath, sourcePath, stockSourcePath }) => {
|
|
const [resolved, resolvedCopy, resolvedSource, resolvedStock] = await Promise.all([
|
|
canonicalCopyPath(path),
|
|
copyPath ? canonicalCopyPath(copyPath) : Promise.resolve(null),
|
|
sourcePath ? canonicalCopyPath(sourcePath) : Promise.resolve(null),
|
|
stockSourcePath ? canonicalCopyPath(stockSourcePath) : Promise.resolve(null),
|
|
]);
|
|
return {
|
|
path: resolved,
|
|
inSteam: isInSteamLibrary(path) || isInSteamLibrary(resolved),
|
|
problem: dataFolderProblem(resolved, resolvedCopy, resolvedSource, resolvedStock),
|
|
};
|
|
});
|
|
handle('install:measureFolder', async ({ path }) => {
|
|
await requireFolder(path);
|
|
try {
|
|
return await measureFolder(path);
|
|
} catch (error) {
|
|
console.warn('[install] measuring the folder failed:', error);
|
|
throw new AppError('folder_unreadable', { path });
|
|
}
|
|
});
|
|
|
|
handle('install:checkPatches', ({ gamePath, dataPath, stockSourcePath }) =>
|
|
checkGameFolder(gamePath, dataPath, stockSourcePath));
|
|
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 hydrateCopiesStatus(await listCopies()));
|
|
return record;
|
|
});
|
|
handle('install:cancel', () => ({ cancelled: cancelInstall() }));
|
|
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', async () => hydrateCopiesStatus(await listCopies()));
|
|
handle('copies:setCurrent', async ({ id }) => {
|
|
const registry = await hydrateCopiesStatus(await setCurrentCopy(id));
|
|
broadcast('event:copies-changed', registry);
|
|
return registry;
|
|
});
|
|
handle('copies:rename', async ({ 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 hydrateCopiesStatus(await changeDecalSource(id, path));
|
|
broadcast('event:copies-changed', updated);
|
|
return updated;
|
|
});
|
|
handle('copies:maintenanceInfo', ({ id }) => maintenanceInfo(id));
|
|
handle('copies:maintenanceState', () => maintenanceState());
|
|
handle('copies:maintain', async (request) => {
|
|
if (saveWriteInProgress() || decalImportInProgress()) throw new AppError('maintenance_busy');
|
|
const updated = await hydrateCopiesStatus(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 }) =>
|
|
previewSelectedBackups(copyId, userId, paths));
|
|
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));
|
|
|
|
const requireModsWritable = (): void => {
|
|
if (launchInProgress() || maintenanceInProgress()) throw new AppError('mods_read_only');
|
|
};
|
|
handle('mods:getState', ({ copyId }) => getModsState(copyId));
|
|
handle('mods:setEnabled', ({ copyId, modId, enabled }) => {
|
|
requireModsWritable();
|
|
return setModEnabled(copyId, modId, enabled);
|
|
});
|
|
handle('mods:setOrder', ({ copyId, order }) => {
|
|
requireModsWritable();
|
|
return setModsOrder(copyId, order);
|
|
});
|
|
handle('mods:autoSort', ({ copyId }) => {
|
|
requireModsWritable();
|
|
return autoSortMods(copyId);
|
|
});
|
|
handle('mods:savePreset', ({ copyId, name, overwriteId }) => {
|
|
requireModsWritable();
|
|
return saveModPreset(copyId, name, overwriteId);
|
|
});
|
|
handle('mods:applyPreset', ({ copyId, presetId }) => {
|
|
requireModsWritable();
|
|
return applyModPreset(copyId, presetId);
|
|
});
|
|
handle('mods:deletePreset', ({ copyId, presetId }) => {
|
|
requireModsWritable();
|
|
return deleteModPreset(copyId, presetId);
|
|
});
|
|
handle('mods:exportPreset', ({ copyId, presetId }, sender) =>
|
|
exportModPreset(copyId, presetId, BrowserWindow.fromWebContents(sender)));
|
|
handle('mods:importPreset', ({ copyId }, sender) => {
|
|
requireModsWritable();
|
|
return importModPreset(copyId, BrowserWindow.fromWebContents(sender));
|
|
});
|
|
handle('mods:openFolder', () => openModsFolder());
|
|
handle('mods:openDocs', (input) => openModDocs(input?.language));
|
|
handle('mods:setFolder', async ({ copyId, path }) => {
|
|
requireModsWritable();
|
|
const state = await setModsFolder(copyId, path);
|
|
broadcast('event:settings-changed', await getSettings());
|
|
return state;
|
|
});
|
|
|
|
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 ({ version }) => {
|
|
// A build reads the patch set while it runs; switch only between builds.
|
|
if (maintenanceInProgress()) throw new AppError('maintenance_busy');
|
|
const result = await applyPackUpdate(version);
|
|
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;
|
|
});
|
|
}
|