Files
WT-Offline/apps/launcher/src/shared/ipc/contract.ts
T

402 lines
13 KiB
TypeScript

import { z } from 'zod';
import { AppReleaseVersionSchema, AppUpdateCheckSchema, AppUpdateProgressSchema } from '../schemas/app-update.ts';
import { BackgroundPictureSchema } from '../schemas/background.ts';
import {
CopiesSchema,
CopyIdSchema,
CopyNameSchema,
DATA_FOLDER_PROBLEMS,
FolderSizeSchema,
GameFolderInspectionSchema,
InstallProgressSchema,
InstallRecordSchema,
InstallRequestSchema,
PatchCheckSchema,
PatchFeatureSchema,
TargetFolderInspectionSchema,
} from '../schemas/install.ts';
import { LabInfoSchema, LaunchStateSchema } from '../schemas/launch.ts';
import { MaintenanceInfoSchema, MaintenanceProgressSchema, MaintenanceRequestSchema } from '../schemas/maintenance.ts';
import { ModsStateViewSchema } from '../schemas/mods.ts';
import { ActivePatchSetSchema, PackApplyResultSchema, PackCheckSchema, PackReleaseSchema } from '../schemas/patch-pack.ts';
import { AccountEditsSchema, SaveAvatarChoicesSchema, SaveAvatarIdSchema, ResearchBranchSchema, ResearchOperationSchema, SaveResearchSchema, OfficialImportOptionsSchema, OfficialPreviewSchema, OnlineDataSourceSchema, SaveOfficialRequestSchema, CurrencyEditsSchema, SaveBackupNameSchema, SaveBackupSchema, SaveDetailsSchema, SaveKeysSchema, SaveListSchema, SaveRegistrationSchema, SaveTransferSchema } from '../schemas/saves.ts';
import { TuningPatchSchema, TuningViewSchema } from '../schemas/tuning.ts';
import { SettingsPatchSchema, SettingsSchema } from '../schemas/settings.ts';
/**
* The IPC contract: every renderer → main call and every main → renderer event, with the
* schemas of what goes in and what comes out. The main process validates both sides when it
* registers a handler; the window derives its types from here. No channel name is written as
* a bare string anywhere else.
*/
export const ipcContract = {
'app:getInfo': {
input: z.void(),
output: z.object({
version: z.string(),
electron: z.string(),
development: z.boolean(),
appUpdatesSupported: z.boolean(),
/** The operating system's preferred language, as Electron reports it (e.g. "zh-CN"). */
systemLocale: z.string(),
userDataPath: z.string(),
/** The game versions every patch was verified on. */
supportedGameVersions: z.array(z.string()),
}),
},
'app:listBackgrounds': {
/** The website's wallpapers (when allowed in the settings) and the installed copy's pictures. */
input: z.void(),
output: z.array(BackgroundPictureSchema),
},
'app:openFolder': {
/** Opens a folder in the file manager; refused unless it exists. */
input: z.object({ path: z.string().min(1) }),
output: z.void(),
},
'app:openLogsFolder': {
input: z.void(),
output: z.void(),
},
'app:exportDiagnostics': {
input: z.void(),
/** The saved archive, or null when the player cancelled. */
output: z.object({ path: z.string().nullable() }),
},
'dialog:pickFolder': {
/** The dialog title, already translated by the window. */
input: z.object({ title: z.string().optional() }),
/** The chosen folder, or null when the player cancelled. */
output: z.object({ path: z.string().nullable() }),
},
'install:prepareCopy': {
input: z.void(),
output: z.object({ id: CopyIdSchema, dataPath: z.string() }),
},
'install:inspectGameFolder': {
input: z.object({ path: z.string().min(1) }),
output: GameFolderInspectionSchema,
},
'install:inspectTargetFolder': {
input: z.object({
path: z.string().min(1),
stockPath: z.string().min(1),
version: z.string().nullable().optional(),
}),
output: TargetFolderInspectionSchema,
},
'install:inspectDataFolder': {
input: z.object({
path: z.string().min(1),
copyPath: z.string().min(1).optional(),
sourcePath: z.string().min(1).optional(),
stockSourcePath: z.string().min(1).optional(),
}),
output: z.object({
path: z.string(),
inSteam: z.boolean(),
problem: z.enum(DATA_FOLDER_PROBLEMS).nullable(),
}),
},
'install:measureFolder': {
input: z.object({ path: z.string().min(1), version: z.string().nullable().optional() }),
output: FolderSizeSchema,
},
'install:checkPatches': {
/** The game folder to install from, optional stock source folder, and the data folder whose store tuning the build uses. */
input: z.object({
gamePath: z.string().min(1),
dataPath: z.string().min(1),
stockSourcePath: z.string().min(1).optional(),
}),
output: PatchCheckSchema,
},
'install:start': {
/** Runs until the installation is done; progress arrives as event:install-progress. */
input: InstallRequestSchema,
output: InstallRecordSchema,
},
'install:cancel': {
input: z.void(),
/** Whether an installation was running. */
output: z.object({ cancelled: z.boolean() }),
},
'install:getRecord': {
input: z.void(),
output: z.object({ record: InstallRecordSchema.nullable() }),
},
'install:listFeatures': {
/** Every feature the patches cover; which of them a copy lacks is in its install record. */
input: z.void(),
output: z.array(PatchFeatureSchema),
},
'copies:list': {
input: z.void(),
output: CopiesSchema,
},
'copies:setCurrent': {
input: z.object({ id: CopyIdSchema }),
output: CopiesSchema,
},
'copies:rename': {
input: z.object({ id: CopyIdSchema, name: CopyNameSchema }),
output: CopiesSchema,
},
'copies:relocate': {
input: z.object({ id: CopyIdSchema, path: z.string().min(1) }),
output: CopiesSchema,
},
'copies:setDecalSource': {
input: z.object({ id: CopyIdSchema, path: z.string().min(1).nullable() }),
output: CopiesSchema,
},
'copies:maintenanceInfo': {
input: z.object({ id: CopyIdSchema }),
output: MaintenanceInfoSchema,
},
'copies:maintenanceState': {
input: z.void(),
output: MaintenanceProgressSchema.nullable(),
},
'copies:maintain': {
input: MaintenanceRequestSchema,
output: CopiesSchema,
},
'copies:cancelMaintenance': {
input: z.void(),
output: z.void(),
},
'saves:list': {
input: z.object({ copyId: CopyIdSchema }),
output: SaveListSchema,
},
'saves:details': {
input: SaveKeysSchema,
output: SaveDetailsSchema,
},
'saves:setCurrent': {
input: SaveKeysSchema,
output: SaveListSchema,
},
'saves:create': {
input: SaveRegistrationSchema,
output: SaveListSchema,
},
'saves:avatars': {
input: z.object({ copyId: CopyIdSchema }),
output: SaveAvatarChoicesSchema,
},
'saves:avatarPreview': {
input: z.object({ copyId: CopyIdSchema, avatar: SaveAvatarIdSchema }),
output: z.base64().nullable(),
},
'saves:editAccount': {
input: SaveKeysSchema.extend({ edits: AccountEditsSchema }),
output: SaveDetailsSchema,
},
'saves:editCurrencies': {
input: SaveKeysSchema.extend({ values: CurrencyEditsSchema }),
output: SaveDetailsSchema,
},
'saves:pickArchive': {
input: z.void(),
output: z.object({ path: z.string().nullable() }),
},
'saves:export': {
input: SaveKeysSchema,
output: z.object({ path: z.string().nullable() }),
},
'saves:transfer': {
input: SaveTransferSchema,
output: SaveListSchema,
},
'saves:delete': {
input: SaveKeysSchema,
output: SaveListSchema,
},
'saves:listBackups': {
input: SaveKeysSchema,
output: z.array(SaveBackupSchema),
},
'saves:takeBackup': {
input: SaveKeysSchema,
output: z.array(SaveBackupSchema),
},
'saves:restoreBackup': {
input: SaveKeysSchema.extend({ name: SaveBackupNameSchema }),
output: SaveDetailsSchema,
},
'saves:discoverOnlineData': {
input: z.void(),
output: z.array(OnlineDataSourceSchema),
},
'saves:copyOnlineData': {
input: SaveKeysSchema.extend({ accountId: z.string().regex(/^\d+$/) }),
output: z.array(z.string()),
},
'saves:pickOfficialBackups': {
input: z.void(),
output: z.array(z.string()),
},
'saves:previewOfficial': {
input: SaveOfficialRequestSchema,
output: OfficialPreviewSchema,
},
'saves:importOfficial': {
input: SaveOfficialRequestSchema.extend({ options: OfficialImportOptionsSchema }),
output: OfficialPreviewSchema,
},
'saves:research': {
input: SaveKeysSchema.extend({ branch: ResearchBranchSchema.optional() }),
output: SaveResearchSchema,
},
'saves:editResearch': {
input: SaveKeysSchema.extend({ branch: ResearchBranchSchema, action: ResearchOperationSchema }),
output: SaveResearchSchema,
},
'tuning:get': {
input: z.object({ copyId: CopyIdSchema }),
output: TuningViewSchema,
},
'tuning:update': {
input: z.object({ copyId: CopyIdSchema, patch: TuningPatchSchema }),
output: TuningViewSchema,
},
'mods:getState': {
input: z.object({ copyId: CopyIdSchema }),
output: ModsStateViewSchema,
},
'mods:setEnabled': {
input: z.object({ copyId: CopyIdSchema, modId: z.string().min(1), enabled: z.boolean() }),
output: ModsStateViewSchema,
},
'mods:setOrder': {
input: z.object({ copyId: CopyIdSchema, order: z.array(z.string()) }),
output: ModsStateViewSchema,
},
'mods:autoSort': {
input: z.object({ copyId: CopyIdSchema }),
output: ModsStateViewSchema,
},
'mods:savePreset': {
input: z.object({ copyId: CopyIdSchema, name: z.string().trim().min(1).max(80), overwriteId: z.string().min(1).optional() }),
output: ModsStateViewSchema,
},
'mods:applyPreset': {
input: z.object({ copyId: CopyIdSchema, presetId: z.string().min(1) }),
output: ModsStateViewSchema,
},
'mods:deletePreset': {
input: z.object({ copyId: CopyIdSchema, presetId: z.string().min(1) }),
output: ModsStateViewSchema,
},
'mods:exportPreset': {
input: z.object({ copyId: CopyIdSchema, presetId: z.string().min(1) }),
output: z.object({ path: z.string().nullable() }),
},
'mods:importPreset': {
input: z.object({ copyId: CopyIdSchema }),
output: z.object({ imported: z.boolean(), state: ModsStateViewSchema }),
},
'mods:openFolder': {
input: z.void(),
output: z.void(),
},
'mods:openDocs': {
input: z.object({ language: z.string().optional() }).optional(),
output: z.void(),
},
'mods:setFolder': {
input: z.object({ copyId: CopyIdSchema, path: z.string().min(1).nullable() }),
output: ModsStateViewSchema,
},
'launch:getState': {
input: z.void(),
output: LaunchStateSchema,
},
'launch:getLabInfo': {
input: z.void(),
output: LabInfoSchema.nullable(),
},
'launch:start': {
input: z.void(),
output: LaunchStateSchema,
},
'launch:stop': {
input: z.void(),
output: z.void(),
},
'launch:openServerStatus': {
input: z.void(),
output: z.void(),
},
'settings:get': {
input: z.void(),
output: SettingsSchema,
},
'settings:update': {
input: SettingsPatchSchema,
output: SettingsSchema,
},
'patches:active': {
/** The patch set builds use: the bundled one or a downloaded adaptation pack. */
input: z.void(),
output: ActivePatchSetSchema,
},
'patches:check': {
/** Checks for a newer adaptation pack. */
input: z.void(),
output: PackCheckSchema,
},
'patches:apply': {
/** Downloads, verifies and activates the pack version selected by the player. */
input: z.object({ version: PackReleaseSchema.shape.version }),
output: PackApplyResultSchema,
},
'patches:useBundled': {
/** Stops using downloaded packs. */
input: z.void(),
output: ActivePatchSetSchema,
},
'updates:check': {
input: z.void(),
output: AppUpdateCheckSchema,
},
'updates:download': {
input: z.object({ version: AppReleaseVersionSchema }),
output: AppUpdateCheckSchema,
},
'updates:install': {
input: z.object({ version: AppReleaseVersionSchema }),
output: z.void(),
},
'window:minimize': {
input: z.void(),
output: z.void(),
},
'window:close': {
input: z.void(),
output: z.void(),
},
} as const;
export const ipcEvents = {
'event:settings-changed': SettingsSchema,
'event:install-progress': InstallProgressSchema,
'event:app-update-progress': AppUpdateProgressSchema,
'event:launch-state': LaunchStateSchema,
'event:copies-changed': CopiesSchema,
'event:copy-maintenance': MaintenanceProgressSchema.nullable(),
/** Another patch set became active (a pack was installed, or the bundled set restored). */
'event:patch-set-changed': ActivePatchSetSchema,
} as const;
export type IpcChannel = keyof typeof ipcContract;
export type IpcInput<C extends IpcChannel> = z.input<(typeof ipcContract)[C]['input']>;
export type IpcOutput<C extends IpcChannel> = z.output<(typeof ipcContract)[C]['output']>;
export type IpcParsedInput<C extends IpcChannel> = z.output<(typeof ipcContract)[C]['input']>;
export type IpcEventChannel = keyof typeof ipcEvents;
export type IpcEventPayload<C extends IpcEventChannel> = z.output<(typeof ipcEvents)[C]>;