/* * 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 { CspRequestResult } from "@main/csp/manager"; import { PluginIpcMappings } from "@main/ipcPlugins"; import type { UserThemeHeader } from "@main/themes"; import { IpcEvents } from "@shared/IpcEvents"; import { IpcRes } from "@utils/types"; import { ipcRenderer } from "electron"; 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: (fileName: string, fileData: string) => invoke(IpcEvents.UPLOAD_THEME, fileName, fileData), deleteTheme: (fileName: string) => invoke(IpcEvents.DELETE_THEME, fileName), 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), 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), }, native: { getVersions: () => process.versions as Partial, openExternal: (url: string) => invoke(IpcEvents.OPEN_EXTERNAL, url) }, 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), }, pluginHelpers: PluginHelpers };