import { z } from 'zod'; import { BackgroundPictureSchema } from '../schemas/background.ts'; import { CopiesSchema, CopyIdSchema, CopyNameSchema, 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 { ActivePatchSetSchema, PackApplyResultSchema, PackCheckSchema } from '../schemas/patch-pack.ts'; import { AccountEditsSchema, ResearchBranchSchema, ResearchOperationSchema, SaveResearchSchema, OfficialImportOptionsSchema, OfficialPreviewSchema, 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(), /** 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) }), 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() }), }, '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:maintenanceInfo': { input: z.object({ id: CopyIdSchema }), output: MaintenanceInfoSchema, }, '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: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:pickOfficialBackups': { input: z.void(), output: z.array(z.string()), }, 'saves:previewOfficial': { input: SaveOfficialRequestSchema.extend({ includeUsedVehicles: z.boolean() }), 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, }, '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': { /** Asks GitHub for the newest adaptation pack. */ input: z.void(), output: PackCheckSchema, }, 'patches:apply': { /** Downloads, verifies and activates the newest pack; the window asks the player first. */ input: z.void(), output: PackApplyResultSchema, }, 'patches:useBundled': { /** Stops using downloaded packs. */ input: z.void(), output: ActivePatchSetSchema, }, '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:launch-state': LaunchStateSchema, 'event:copies-changed': CopiesSchema, 'event:copy-maintenance': MaintenanceProgressSchema, /** 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 = 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]>;