import { z } from 'zod'; import { FolderSizeSchema, GameFolderInspectionSchema, InstallProgressSchema, InstallRecordSchema, InstallRequestSchema, PatchCheckSchema, PatchFeatureSchema, TargetFolderInspectionSchema, } from '../schemas/install.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(), /** 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:openFolder': { /** Opens a folder in the file manager; refused unless it exists. */ input: z.object({ path: z.string().min(1) }), output: z.void(), }, '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: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) }), output: TargetFolderInspectionSchema, }, 'install:measureFolder': { input: z.object({ path: z.string().min(1) }), output: FolderSizeSchema, }, 'install:checkPatches': { /** The game folder to install from, and the data folder whose store tuning the build uses. */ input: z.object({ gamePath: z.string().min(1), dataPath: z.string().min(1) }), 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(), defaultDataPath: z.string() }), }, '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), }, 'settings:get': { input: z.void(), output: SettingsSchema, }, 'settings:update': { input: SettingsPatchSchema, output: SettingsSchema, }, '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, } as const; export type IpcChannel = keyof typeof ipcContract; export type IpcInput = z.input<(typeof ipcContract)[C]['input']>; export type IpcOutput = z.output<(typeof ipcContract)[C]['output']>; export type IpcParsedInput = z.output<(typeof ipcContract)[C]['input']>; export type IpcEventChannel = keyof typeof ipcEvents; export type IpcEventPayload = z.output<(typeof ipcEvents)[C]>;