/* * Vencord, a Discord client mod * Copyright (c) 2023 Vendicated and contributors * SPDX-License-Identifier: GPL-3.0-or-later */ import type { Settings } from "@api/Settings"; import type { CspRequestResult } from "@main/csp/manager"; import type { PluginIpcMappings } from "@main/ipcPlugins"; import { IpcEvents } from "@shared/IpcEvents"; import type { IpcRes } from "@utils/types"; import { ipcRenderer } from "electron/renderer"; export function invoke(event: IpcEvents, ...args: any[]) { return ipcRenderer.invoke(event, ...args) as Promise; } export function sendSync(event: IpcEvents, ...args: any[]) { return ipcRenderer.sendSync(event, ...args) as T; } const PluginHelpers = {} as Record Promise>>; const pluginIpcMap = sendSync(IpcEvents.GET_PLUGIN_IPC_METHOD_MAP); for (const [plugin, methods] of Object.entries(pluginIpcMap)) { const map = PluginHelpers[plugin] = {}; for (const [methodName, method] of Object.entries(methods)) { map[methodName] = (...args: any[]) => invoke(method as IpcEvents, ...args); } } export default { themes: { uploadTheme: async (fileName: string, fileData: string): Promise => { throw new Error("uploadTheme is WEB only"); }, deleteTheme: (fileName: string) => invoke(IpcEvents.DELETE_THEME, fileName), getThemesDir: () => invoke(IpcEvents.GET_THEMES_DIR), getThemesList: () => invoke>(IpcEvents.GET_THEMES_LIST), getThemeData: (fileName: string) => invoke(IpcEvents.GET_THEME_DATA, fileName), getSystemValues: () => invoke>(IpcEvents.GET_THEME_SYSTEM_VALUES), openFolder: () => invoke(IpcEvents.OPEN_THEMES_FOLDER), }, updater: { getUpdates: () => invoke[]>>(IpcEvents.GET_UPDATES), update: () => invoke>(IpcEvents.UPDATE), rebuild: () => invoke>(IpcEvents.BUILD), getRepo: () => invoke>(IpcEvents.GET_REPO), }, settings: { get: () => sendSync(IpcEvents.GET_SETTINGS), set: (settings: Settings, pathToNotify?: string) => invoke(IpcEvents.SET_SETTINGS, settings, pathToNotify), getSettingsDir: () => invoke(IpcEvents.GET_SETTINGS_DIR), openFolder: () => invoke(IpcEvents.OPEN_SETTINGS_FOLDER), }, quickCss: { get: () => invoke(IpcEvents.GET_QUICK_CSS), set: (css: string) => invoke(IpcEvents.SET_QUICK_CSS, css), addChangeListener(cb: (newCss: string) => void) { ipcRenderer.on(IpcEvents.QUICK_CSS_UPDATE, (_, css) => cb(css)); }, addThemeChangeListener(cb: () => void) { ipcRenderer.on(IpcEvents.THEME_UPDATE, () => cb()); }, openFile: () => invoke(IpcEvents.OPEN_QUICKCSS), openEditor: () => invoke(IpcEvents.OPEN_MONACO_EDITOR), getEditorTheme: () => sendSync(IpcEvents.GET_MONACO_THEME), }, native: { getVersions: () => process.versions as Partial, openExternal: (url: string) => invoke(IpcEvents.OPEN_EXTERNAL, url), getRendererCss: () => invoke(IpcEvents.GET_RENDERER_CSS), onRendererCssUpdate: (cb: (newCss: string) => void) => { if (!IS_DEV) return; ipcRenderer.on(IpcEvents.RENDERER_CSS_UPDATE, (_e, newCss: string) => cb(newCss)); } }, csp: { /** * Note: Only supports full explicit matches, not wildcards. * * If `*.example.com` is allowed, `isDomainAllowed("https://sub.example.com")` will return false. */ isDomainAllowed: (url: string, directives: string[]) => invoke(IpcEvents.CSP_IS_DOMAIN_ALLOWED, url, directives), removeOverride: (url: string) => invoke(IpcEvents.CSP_REMOVE_OVERRIDE, url), requestAddOverride: (url: string, directives: string[], callerName: string) => invoke(IpcEvents.CSP_REQUEST_ADD_OVERRIDE, url, directives, callerName), }, tray: { setUpdateState: (available: boolean) => ipcRenderer.send(IpcEvents.SET_TRAY_UPDATE_STATE, available), onCheckUpdates: (cb: () => void) => { ipcRenderer.on(IpcEvents.TRAY_CHECK_UPDATES, cb); }, onRepair: (cb: () => void) => { ipcRenderer.on(IpcEvents.TRAY_REPAIR, cb); }, onAbout: (cb: () => void) => { ipcRenderer.on(IpcEvents.TRAY_ABOUT, cb); }, }, pluginHelpers: PluginHelpers };