diff options
-rw-r--r-- | browser/VencordNativeStub.ts | 83 | ||||
-rw-r--r-- | src/Vencord.ts | 3 | ||||
-rw-r--r-- | src/VencordNative.ts | 64 | ||||
-rw-r--r-- | src/api/settings.ts | 10 | ||||
-rw-r--r-- | src/components/DonateButton.tsx | 5 | ||||
-rw-r--r-- | src/components/Monaco.ts | 51 | ||||
-rw-r--r-- | src/components/VencordSettings/Updater.tsx | 3 | ||||
-rw-r--r-- | src/components/VencordSettings/VencordTab.tsx | 44 | ||||
-rw-r--r-- | src/components/VencordSettings/settingsStyles.css | 2 | ||||
-rw-r--r-- | src/main/ipcMain.ts | 2 | ||||
-rw-r--r-- | src/main/updater/git.ts | 2 | ||||
-rw-r--r-- | src/main/updater/http.ts | 2 | ||||
-rw-r--r-- | src/plugins/apiBadges.tsx | 3 | ||||
-rw-r--r-- | src/plugins/settings.tsx | 4 | ||||
-rw-r--r-- | src/plugins/spotifyControls/SpotifyStore.ts | 3 | ||||
-rw-r--r-- | src/preload.ts | 5 | ||||
-rw-r--r-- | src/utils/IpcEvents.ts | 43 | ||||
-rw-r--r-- | src/utils/index.ts | 1 | ||||
-rw-r--r-- | src/utils/quickCss.ts | 5 | ||||
-rw-r--r-- | src/utils/settingsSync.ts | 13 | ||||
-rw-r--r-- | src/utils/updater.ts | 20 |
21 files changed, 154 insertions, 214 deletions
diff --git a/browser/VencordNativeStub.ts b/browser/VencordNativeStub.ts index ef3923b..515ccc3 100644 --- a/browser/VencordNativeStub.ts +++ b/browser/VencordNativeStub.ts @@ -16,51 +16,70 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +/// <reference path="../src/modules.d.ts" /> +/// <reference path="../src/globals.d.ts" /> + +import monacoHtml from "~fileContent/../src/components/monacoWin.html"; import * as DataStore from "../src/api/DataStore"; -import IpcEvents from "../src/utils/IpcEvents"; +import { debounce } from "../src/utils"; +import { getTheme, Theme } from "../src/utils/discord"; // Discord deletes this so need to store in variable const { localStorage } = window; // listeners for ipc.on -const listeners = {} as Record<string, Set<Function>>; +const cssListeners = new Set<(css: string) => void>(); +const NOOP = () => { }; +const NOOP_ASYNC = async () => { }; -const handlers = { - [IpcEvents.GET_REPO]: () => "https://github.com/Vendicated/Vencord", // shrug - [IpcEvents.GET_SETTINGS_DIR]: () => "LocalStorage", +const setCssDebounced = debounce((css: string) => VencordNative.quickCss.set(css)); - [IpcEvents.GET_QUICK_CSS]: () => DataStore.get("VencordQuickCss").then(s => s ?? ""), - [IpcEvents.SET_QUICK_CSS]: (css: string) => { - DataStore.set("VencordQuickCss", css); - listeners[IpcEvents.QUICK_CSS_UPDATE]?.forEach(l => l(null, css)); +// probably should make this less cursed at some point +window.VencordNative = { + native: { + getVersions: () => ({}), + openExternal: async (url) => void open(url, "_blank") }, - [IpcEvents.GET_SETTINGS]: () => localStorage.getItem("VencordSettings") || "{}", - [IpcEvents.SET_SETTINGS]: (s: string) => localStorage.setItem("VencordSettings", s), - - [IpcEvents.GET_UPDATES]: () => ({ ok: true, value: [] }), + updater: { + getRepo: async () => ({ ok: true, value: "https://github.com/Vendicated/Vencord" }), + getUpdates: async () => ({ ok: true, value: [] }), + update: async () => ({ ok: true, value: false }), + rebuild: async () => ({ ok: true, value: true }), + }, - [IpcEvents.OPEN_EXTERNAL]: (url: string) => open(url, "_blank"), -}; + quickCss: { + get: () => DataStore.get("VencordQuickCss").then(s => s ?? ""), + set: async (css: string) => { + await DataStore.set("VencordQuickCss", css); + cssListeners.forEach(l => l(css)); + }, + addChangeListener(cb) { + cssListeners.add(cb); + }, + openFile: NOOP_ASYNC, + async openEditor() { + const features = `popup,width=${Math.min(window.innerWidth, 1000)},height=${Math.min(window.innerHeight, 1000)}`; + const win = open("about:blank", "VencordQuickCss", features); + if (!win) { + alert("Failed to open QuickCSS popup. Make sure to allow popups!"); + return; + } -function onEvent(event: string, ...args: any[]) { - const handler = handlers[event]; - if (!handler) throw new Error(`Event ${event} not implemented.`); - return handler(...args); -} + win.setCss = setCssDebounced; + win.getCurrentCss = () => VencordNative.quickCss.get(); + win.getTheme = () => + getTheme() === Theme.Light + ? "vs-light" + : "vs-dark"; -// probably should make this less cursed at some point -window.VencordNative = { - getVersions: () => ({}), - ipc: { - send: (event: string, ...args: any[]) => void onEvent(event, ...args), - sendSync: onEvent, - on(event: string, listener: () => {}) { - (listeners[event] ??= new Set()).add(listener); - }, - off(event: string, listener: () => {}) { - return listeners[event]?.delete(listener); + win.document.write(monacoHtml); }, - invoke: (event: string, ...args: any[]) => Promise.resolve(onEvent(event, ...args)) }, + + settings: { + get: () => localStorage.getItem("VencordSettings") || "{}", + set: async (s: string) => localStorage.setItem("VencordSettings", s), + getSettingsDir: async () => "LocalStorage" + } }; diff --git a/src/Vencord.ts b/src/Vencord.ts index ad79345..4c0d2a8 100644 --- a/src/Vencord.ts +++ b/src/Vencord.ts @@ -33,7 +33,7 @@ import { patches, PMLogger, startAllPlugins } from "./plugins"; import { localStorage } from "./utils/localStorage"; import { relaunch } from "./utils/native"; import { getCloudSettings, putCloudSettings } from "./utils/settingsSync"; -import { checkForUpdates, rebuild, update, UpdateLogger } from "./utils/updater"; +import { checkForUpdates, update,UpdateLogger } from "./utils/updater"; import { onceReady } from "./webpack"; import { SettingsRouter } from "./webpack/common"; @@ -76,7 +76,6 @@ async function init() { if (Settings.autoUpdate) { await update(); - await rebuild(); if (Settings.autoUpdateNotification) setTimeout(() => showNotification({ title: "Vencord has been updated!", diff --git a/src/VencordNative.ts b/src/VencordNative.ts index 3cd53e1..02de74f 100644 --- a/src/VencordNative.ts +++ b/src/VencordNative.ts @@ -16,34 +16,46 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import IPC_EVENTS from "@utils/IpcEvents"; -import { IpcRenderer, ipcRenderer } from "electron"; +import { IpcEvents } from "@utils/IpcEvents"; +import { IpcRes } from "@utils/types"; +import { ipcRenderer } from "electron"; -function assertEventAllowed(event: string) { - if (!(event in IPC_EVENTS)) throw new Error(`Event ${event} not allowed.`); +function invoke<T = any>(event: IpcEvents, ...args: any[]) { + return ipcRenderer.invoke(event, ...args) as Promise<T>; } + +export function sendSync<T = any>(event: IpcEvents, ...args: any[]) { + return ipcRenderer.sendSync(event, ...args) as T; +} + export default { - getVersions: () => process.versions, - ipc: { - send(event: string, ...args: any[]) { - assertEventAllowed(event); - ipcRenderer.send(event, ...args); - }, - sendSync<T = any>(event: string, ...args: any[]): T { - assertEventAllowed(event); - return ipcRenderer.sendSync(event, ...args); - }, - on(event: string, listener: Parameters<IpcRenderer["on"]>[1]) { - assertEventAllowed(event); - ipcRenderer.on(event, listener); - }, - off(event: string, listener: Parameters<IpcRenderer["off"]>[1]) { - assertEventAllowed(event); - ipcRenderer.off(event, listener); + updater: { + getUpdates: () => invoke<IpcRes<Record<"hash" | "author" | "message", string>[]>>(IpcEvents.GET_UPDATES), + update: () => invoke<IpcRes<boolean>>(IpcEvents.UPDATE), + rebuild: () => invoke<IpcRes<boolean>>(IpcEvents.BUILD), + getRepo: () => invoke<IpcRes<string>>(IpcEvents.GET_REPO), + }, + + settings: { + get: () => sendSync<string>(IpcEvents.GET_SETTINGS), + set: (settings: string) => invoke<void>(IpcEvents.SET_SETTINGS, settings), + getSettingsDir: () => invoke<string>(IpcEvents.GET_SETTINGS_DIR), + }, + + quickCss: { + get: () => invoke<string>(IpcEvents.GET_QUICK_CSS), + set: (css: string) => invoke<void>(IpcEvents.SET_QUICK_CSS, css), + + addChangeListener(cb: (newCss: string) => void) { + ipcRenderer.on(IpcEvents.QUICK_CSS_UPDATE, (_, css) => cb(css)); }, - invoke<T = any>(event: string, ...args: any[]): Promise<T> { - assertEventAllowed(event); - return ipcRenderer.invoke(event, ...args); - } - } + + openFile: () => invoke<void>(IpcEvents.OPEN_QUICKCSS), + openEditor: () => invoke<void>(IpcEvents.OPEN_MONACO_EDITOR), + }, + + native: { + getVersions: () => process.versions as Partial<NodeJS.ProcessVersions>, + openExternal: (url: string) => invoke<void>(IpcEvents.OPEN_EXTERNAL, url) + }, }; diff --git a/src/api/settings.ts b/src/api/settings.ts index 35381d8..2329f94 100644 --- a/src/api/settings.ts +++ b/src/api/settings.ts @@ -17,7 +17,6 @@ */ import { debounce } from "@utils/debounce"; -import IpcEvents from "@utils/IpcEvents"; import { localStorage } from "@utils/localStorage"; import Logger from "@utils/Logger"; import { mergeDefaults } from "@utils/misc"; @@ -94,7 +93,7 @@ const DefaultSettings: Settings = { }; try { - var settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS)) as Settings; + var settings = JSON.parse(VencordNative.settings.get()) as Settings; mergeDefaults(settings, DefaultSettings); } catch (err) { var settings = mergeDefaults({} as Settings, DefaultSettings); @@ -173,7 +172,7 @@ function makeProxy(settings: any, root = settings, path = ""): Settings { PlainSettings.cloud.settingsSyncVersion = Date.now(); localStorage.Vencord_settingsDirty = true; saveSettingsOnFrequentAction(); - VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(root, null, 4)); + VencordNative.settings.set(JSON.stringify(root, null, 4)); return true; } }); @@ -249,10 +248,7 @@ export function migratePluginSettings(name: string, ...oldNames: string[]) { logger.info(`Migrating settings from old name ${oldName} to ${name}`); plugins[name] = plugins[oldName]; delete plugins[oldName]; - VencordNative.ipc.invoke( - IpcEvents.SET_SETTINGS, - JSON.stringify(settings, null, 4) - ); + VencordNative.settings.set(JSON.stringify(settings, null, 4)); break; } } diff --git a/src/components/DonateButton.tsx b/src/components/DonateButton.tsx index 49f079b..c027fcf 100644 --- a/src/components/DonateButton.tsx +++ b/src/components/DonateButton.tsx @@ -16,7 +16,6 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import IpcEvents from "@utils/IpcEvents"; import { Button } from "@webpack/common"; import { Heart } from "./Heart"; @@ -27,9 +26,7 @@ export default function DonateButton(props: any) { {...props} look={Button.Looks.LINK} color={Button.Colors.TRANSPARENT} - onClick={() => - VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/sponsors/Vendicated") - } + onClick={() => VencordNative.native.openExternal("https://github.com/sponsors/Vendicated")} > <Heart /> Donate diff --git a/src/components/Monaco.ts b/src/components/Monaco.ts deleted file mode 100644 index 59ed7bb..0000000 --- a/src/components/Monaco.ts +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Vencord, a modification for Discord's desktop app - * Copyright (c) 2022 Vendicated and contributors - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <https://www.gnu.org/licenses/>. -*/ - -import { debounce } from "@utils/debounce"; -import IpcEvents from "@utils/IpcEvents"; -import { Queue } from "@utils/Queue"; -import { find } from "@webpack"; - -import monacoHtml from "~fileContent/monacoWin.html"; - -const queue = new Queue(); -const setCss = debounce((css: string) => { - queue.push(() => VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, css)); -}); - -export async function launchMonacoEditor() { - const features = `popup,width=${Math.min(window.innerWidth, 1000)},height=${Math.min(window.innerHeight, 1000)}`; - const win = open("about:blank", "VencordQuickCss", features); - if (!win) { - alert("Failed to open QuickCSS popup. Make sure to allow popups!"); - return; - } - - win.setCss = setCss; - win.getCurrentCss = () => VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS); - win.getTheme = () => - find(m => - m.ProtoClass?.typeName.endsWith("PreloadedUserSettings") - )?.getCurrentValue()?.appearance?.theme === 2 - ? "vs-light" - : "vs-dark"; - - win.document.write(monacoHtml); - - window.__VENCORD_MONACO_WIN__ = new WeakRef(win); -} diff --git a/src/components/VencordSettings/Updater.tsx b/src/components/VencordSettings/Updater.tsx index 6dffbc8..fb78394 100644 --- a/src/components/VencordSettings/Updater.tsx +++ b/src/components/VencordSettings/Updater.tsx @@ -25,7 +25,7 @@ import { Link } from "@components/Link"; import { Margins } from "@utils/margins"; import { classes, useAwaiter } from "@utils/misc"; import { relaunch } from "@utils/native"; -import { changes, checkForUpdates, getRepo, isNewer, rebuild, update, updateError, UpdateLogger } from "@utils/updater"; +import { changes, checkForUpdates, getRepo, isNewer, update, updateError, UpdateLogger } from "@utils/updater"; import { Alerts, Button, Card, Forms, Parser, React, Switch, Toasts } from "@webpack/common"; import gitHash from "~git-hash"; @@ -125,7 +125,6 @@ function Updatable(props: CommonProps) { onClick={withDispatcher(setIsUpdating, async () => { if (await update()) { setUpdates([]); - await rebuild(); await new Promise<void>(r => { Alerts.show({ title: "Update Success!", diff --git a/src/components/VencordSettings/VencordTab.tsx b/src/components/VencordSettings/VencordTab.tsx index 7512208..672e04e 100644 --- a/src/components/VencordSettings/VencordTab.tsx +++ b/src/components/VencordSettings/VencordTab.tsx @@ -23,7 +23,6 @@ import { classNameFactory } from "@api/Styles"; import DonateButton from "@components/DonateButton"; import ErrorBoundary from "@components/ErrorBoundary"; import { ErrorCard } from "@components/ErrorCard"; -import IpcEvents from "@utils/IpcEvents"; import { Margins } from "@utils/margins"; import { identity, useAwaiter } from "@utils/misc"; import { relaunch, showItemInFolder } from "@utils/native"; @@ -39,7 +38,7 @@ type KeysOfType<Object, Type> = { }[keyof Object]; function VencordSettings() { - const [settingsDir, , settingsDirPending] = useAwaiter(() => VencordNative.ipc.invoke<string>(IpcEvents.GET_SETTINGS_DIR), { + const [settingsDir, , settingsDirPending] = useAwaiter(VencordNative.settings.getSettingsDir, { fallbackValue: "Loading..." }); const settings = useSettings(); @@ -101,40 +100,35 @@ function VencordSettings() { <DonateCard image={donateImage} /> <Forms.FormSection title="Quick Actions"> <Card className={cl("quick-actions-card")}> - {IS_WEB ? ( - <Button - onClick={() => require("../Monaco").launchMonacoEditor()} - size={Button.Sizes.SMALL} - disabled={settingsDir === "Loading..."}> - Open QuickCSS File - </Button> - ) : ( - <React.Fragment> + <React.Fragment> + {!IS_WEB && ( <Button onClick={relaunch} size={Button.Sizes.SMALL}> Restart Client </Button> - <Button - onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_MONACO_EDITOR)} - size={Button.Sizes.SMALL} - disabled={settingsDir === "Loading..."}> - Open QuickCSS File - </Button> + )} + <Button + onClick={() => VencordNative.quickCss.openEditor()} + size={Button.Sizes.SMALL} + disabled={settingsDir === "Loading..."}> + Open QuickCSS File + </Button> + {!IS_WEB && ( <Button onClick={() => showItemInFolder(settingsDir)} size={Button.Sizes.SMALL} disabled={settingsDirPending}> Open Settings Folder </Button> - <Button - onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/Vendicated/Vencord")} - size={Button.Sizes.SMALL} - disabled={settingsDirPending}> - Open in GitHub - </Button> - </React.Fragment> - )} + )} + <Button + onClick={() => VencordNative.native.openExternal("https://github.com/Vendicated/Vencord")} + size={Button.Sizes.SMALL} + disabled={settingsDirPending}> + Open in GitHub + </Button> + </React.Fragment> </Card> </Forms.FormSection> diff --git a/src/components/VencordSettings/settingsStyles.css b/src/components/VencordSettings/settingsStyles.css index c25022a..3652756 100644 --- a/src/components/VencordSettings/settingsStyles.css +++ b/src/components/VencordSettings/settingsStyles.css @@ -15,7 +15,7 @@ display: flex; gap: 1em; align-items: center; - justify-content: space-between; + justify-content: space-evenly; flex-grow: 1; flex-flow: row wrap; margin-bottom: 1em; diff --git a/src/main/ipcMain.ts b/src/main/ipcMain.ts index d60200f..b734f36 100644 --- a/src/main/ipcMain.ts +++ b/src/main/ipcMain.ts @@ -19,7 +19,7 @@ import "./updater"; import { debounce } from "@utils/debounce"; -import IpcEvents from "@utils/IpcEvents"; +import { IpcEvents } from "@utils/IpcEvents"; import { Queue } from "@utils/Queue"; import { BrowserWindow, ipcMain, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; diff --git a/src/main/updater/git.ts b/src/main/updater/git.ts index d6a29a8..c6e5cc9 100644 --- a/src/main/updater/git.ts +++ b/src/main/updater/git.ts @@ -16,7 +16,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import IpcEvents from "@utils/IpcEvents"; +import { IpcEvents } from "@utils/IpcEvents"; import { execFile as cpExecFile } from "child_process"; import { ipcMain } from "electron"; import { join } from "path"; diff --git a/src/main/updater/http.ts b/src/main/updater/http.ts index e2a3520..5653d01 100644 --- a/src/main/updater/http.ts +++ b/src/main/updater/http.ts @@ -17,7 +17,7 @@ */ import { VENCORD_USER_AGENT } from "@utils/constants"; -import IpcEvents from "@utils/IpcEvents"; +import { IpcEvents } from "@utils/IpcEvents"; import { ipcMain } from "electron"; import { writeFile } from "fs/promises"; import { join } from "path"; diff --git a/src/plugins/apiBadges.tsx b/src/plugins/apiBadges.tsx index a156b63..bf1906f 100644 --- a/src/plugins/apiBadges.tsx +++ b/src/plugins/apiBadges.tsx @@ -22,7 +22,6 @@ import ErrorBoundary from "@components/ErrorBoundary"; import { Flex } from "@components/Flex"; import { Heart } from "@components/Heart"; import { Devs } from "@utils/constants"; -import IpcEvents from "@utils/IpcEvents"; import Logger from "@utils/Logger"; import { Margins } from "@utils/margins"; import { closeModal, Modals, openModal } from "@utils/modal"; @@ -115,7 +114,7 @@ export default definePlugin({ const modalKey = openModal(props => ( <ErrorBoundary noop onError={() => { closeModal(modalKey); - VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/sponsors/Vendicated"); + VencordNative.native.openExternal("https://github.com/sponsors/Vendicated"); }}> <Modals.ModalRoot {...props}> <Modals.ModalHeader> diff --git a/src/plugins/settings.tsx b/src/plugins/settings.tsx index 5dec83c..6fd424c 100644 --- a/src/plugins/settings.tsx +++ b/src/plugins/settings.tsx @@ -155,12 +155,12 @@ export default definePlugin({ }, get electronVersion() { - return VencordNative.getVersions().electron || window.armcord?.electron || null; + return VencordNative.native.getVersions().electron || window.armcord?.electron || null; }, get chromiumVersion() { try { - return VencordNative.getVersions().chrome + return VencordNative.native.getVersions().chrome // @ts-ignore Typescript will add userAgentData IMMEDIATELY || navigator.userAgentData?.brands?.find(b => b.brand === "Chromium" || b.brand === "Google Chrome")?.version || null; diff --git a/src/plugins/spotifyControls/SpotifyStore.ts b/src/plugins/spotifyControls/SpotifyStore.ts index 723bc4c..2ceb30c 100644 --- a/src/plugins/spotifyControls/SpotifyStore.ts +++ b/src/plugins/spotifyControls/SpotifyStore.ts @@ -17,7 +17,6 @@ */ import { Settings } from "@api/settings"; -import IpcEvents from "@utils/IpcEvents"; import { proxyLazy } from "@utils/proxyLazy"; import { findByPropsLazy } from "@webpack"; import { Flux, FluxDispatcher } from "@webpack/common"; @@ -94,7 +93,7 @@ export const SpotifyStore = proxyLazy(() => { ? "spotify:" + path.replaceAll("/", (_, idx) => idx === 0 ? "" : ":") : "https://open.spotify.com" + path; - VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, url); + VencordNative.native.openExternal(url); } // Need to keep track of this manually diff --git a/src/preload.ts b/src/preload.ts index 276cceb..5f6c445 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -17,7 +17,6 @@ */ import { debounce } from "@utils/debounce"; -import IpcEvents from "@utils/IpcEvents"; import { contextBridge, webFrame } from "electron"; import { readFileSync, watch } from "fs"; import { join } from "path"; @@ -58,8 +57,8 @@ if (location.protocol !== "data:") { } } // Monaco popout else { - contextBridge.exposeInMainWorld("setCss", debounce(s => VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, s))); - contextBridge.exposeInMainWorld("getCurrentCss", () => VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS)); + contextBridge.exposeInMainWorld("setCss", debounce(VencordNative.quickCss.set)); + contextBridge.exposeInMainWorld("getCurrentCss", VencordNative.quickCss.get); // shrug contextBridge.exposeInMainWorld("getTheme", () => "vs-dark"); } diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts index 57e4bb2..30a68e8 100644 --- a/src/utils/IpcEvents.ts +++ b/src/utils/IpcEvents.ts @@ -1,6 +1,6 @@ /* * Vencord, a modification for Discord's desktop app - * Copyright (c) 2022 Vendicated and contributors + * Copyright (c) 2023 Vendicated and contributors * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -16,31 +16,18 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -type Enum<T extends Record<string, string>> = { - [k in keyof T]: T[k]; -} & { [v in keyof T as T[v]]: v; }; - -function strEnum<T extends Record<string, string>>(obj: T): T { - const o = {} as T; - for (const key in obj) { - o[key] = obj[key] as any; - o[obj[key]] = key as any; - } - return Object.freeze(o); +export const enum IpcEvents { + QUICK_CSS_UPDATE = "VencordQuickCssUpdate", + GET_QUICK_CSS = "VencordGetQuickCss", + SET_QUICK_CSS = "VencordSetQuickCss", + GET_SETTINGS_DIR = "VencordGetSettingsDir", + GET_SETTINGS = "VencordGetSettings", + SET_SETTINGS = "VencordSetSettings", + OPEN_EXTERNAL = "VencordOpenExternal", + OPEN_QUICKCSS = "VencordOpenQuickCss", + GET_UPDATES = "VencordGetUpdates", + GET_REPO = "VencordGetRepo", + UPDATE = "VencordUpdate", + BUILD = "VencordBuild", + OPEN_MONACO_EDITOR = "VencordOpenMonacoEditor", } - -export default strEnum({ - QUICK_CSS_UPDATE: "VencordQuickCssUpdate", - GET_QUICK_CSS: "VencordGetQuickCss", - SET_QUICK_CSS: "VencordSetQuickCss", - GET_SETTINGS_DIR: "VencordGetSettingsDir", - GET_SETTINGS: "VencordGetSettings", - SET_SETTINGS: "VencordSetSettings", - OPEN_EXTERNAL: "VencordOpenExternal", - OPEN_QUICKCSS: "VencordOpenQuickCss", - GET_UPDATES: "VencordGetUpdates", - GET_REPO: "VencordGetRepo", - UPDATE: "VencordUpdate", - BUILD: "VencordBuild", - OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor", -} as const); diff --git a/src/utils/index.ts b/src/utils/index.ts index cfded6b..98e923f 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -20,7 +20,6 @@ export * from "./ChangeList"; export * as Constants from "./constants"; export * from "./debounce"; export * as Discord from "./discord"; -export { default as IpcEvents } from "./IpcEvents"; export { default as Logger } from "./Logger"; export * from "./margins"; export * from "./misc"; diff --git a/src/utils/quickCss.ts b/src/utils/quickCss.ts index 4ae1023..1b3f78d 100644 --- a/src/utils/quickCss.ts +++ b/src/utils/quickCss.ts @@ -18,7 +18,6 @@ import { addSettingsListener, Settings } from "@api/settings"; -import IpcEvents from "./IpcEvents"; let style: HTMLStyleElement; let themesStyle: HTMLStyleElement; @@ -29,8 +28,8 @@ export async function toggle(isEnabled: boolean) { style = document.createElement("style"); style.id = "vencord-custom-css"; document.head.appendChild(style); - VencordNative.ipc.on(IpcEvents.QUICK_CSS_UPDATE, (_, css: string) => style.textContent = css); - style.textContent = await VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS); + VencordNative.quickCss.addChangeListener(css => style.textContent = css); + style.textContent = await VencordNative.quickCss.get(); } } else style.disabled = !isEnabled; diff --git a/src/utils/settingsSync.ts b/src/utils/settingsSync.ts index ff49529..3ec2d43 100644 --- a/src/utils/settingsSync.ts +++ b/src/utils/settingsSync.ts @@ -22,7 +22,6 @@ import { Toasts } from "@webpack/common"; import { deflateSync, inflateSync } from "fflate"; import { getCloudAuth, getCloudUrl } from "./cloud"; -import IpcEvents from "./IpcEvents"; import Logger from "./Logger"; import { saveFile } from "./web"; @@ -36,15 +35,15 @@ export async function importSettings(data: string) { if ("settings" in parsed && "quickCss" in parsed) { Object.assign(PlainSettings, parsed.settings); - await VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(parsed.settings, null, 4)); - await VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, parsed.quickCss); + await VencordNative.settings.set(JSON.stringify(parsed.settings, null, 4)); + await VencordNative.quickCss.set(parsed.quickCss); } else throw new Error("Invalid Settings. Is this even a Vencord Settings file?"); } export async function exportSettings() { - const settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS)); - const quickCss = await VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS); + const settings = JSON.parse(VencordNative.settings.get()); + const quickCss = await VencordNative.quickCss.get(); return JSON.stringify({ settings, quickCss }, null, 4); } @@ -147,7 +146,7 @@ export async function putCloudSettings() { const { written } = await res.json(); PlainSettings.cloud.settingsSyncVersion = written; - VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(PlainSettings, null, 4)); + VencordNative.settings.set(JSON.stringify(PlainSettings, null, 4)); cloudSettingsLogger.info("Settings uploaded to cloud successfully"); showNotification({ @@ -230,7 +229,7 @@ export async function getCloudSettings(shouldNotify = true, force = false) { // sync with server timestamp instead of local one PlainSettings.cloud.settingsSyncVersion = written; - VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(PlainSettings, null, 4)); + VencordNative.settings.set(JSON.stringify(PlainSettings, null, 4)); cloudSettingsLogger.info("Settings loaded from cloud successfully"); if (shouldNotify) diff --git a/src/utils/updater.ts b/src/utils/updater.ts index 2fd1561..ce99aa4 100644 --- a/src/utils/updater.ts +++ b/src/utils/updater.ts @@ -18,7 +18,6 @@ import gitHash from "~git-hash"; -import IpcEvents from "./IpcEvents"; import Logger from "./Logger"; import { relaunch } from "./native"; import { IpcRes } from "./types"; @@ -39,7 +38,7 @@ async function Unwrap<T>(p: Promise<IpcRes<T>>) { } export async function checkForUpdates() { - changes = await Unwrap(VencordNative.ipc.invoke<IpcRes<typeof changes>>(IpcEvents.GET_UPDATES)); + changes = await Unwrap(VencordNative.updater.getUpdates()); if (changes.some(c => c.hash === gitHash)) { isNewer = true; return (isOutdated = false); @@ -50,22 +49,18 @@ export async function checkForUpdates() { export async function update() { if (!isOutdated) return true; - const res = await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.UPDATE)); + const res = await Unwrap(VencordNative.updater.update()); - if (res) + if (res) { isOutdated = false; + if (!await Unwrap(VencordNative.updater.rebuild())) + throw new Error("The Build failed. Please try manually building the new update"); + } return res; } -export function getRepo() { - return Unwrap(VencordNative.ipc.invoke<IpcRes<string>>(IpcEvents.GET_REPO)); -} - -export async function rebuild() { - if (!await Unwrap(VencordNative.ipc.invoke<IpcRes<boolean>>(IpcEvents.BUILD))) - throw new Error("The Build failed. Please try manually building the new update"); -} +export const getRepo = () => Unwrap(VencordNative.updater.getRepo()); export async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) { if (IS_WEB) return; @@ -78,7 +73,6 @@ export async function maybePromptToUpdate(confirmMessage: string, checkForDev = if (wantsUpdate && isNewer) return alert("Your local copy has more recent commits. Please stash or reset them."); if (wantsUpdate) { await update(); - await rebuild(); relaunch(); } } |