Refactor ipc to be strongly typed and hide impl details (#1018)

This commit is contained in:
V 2023-05-02 02:50:51 +02:00 committed by GitHub
parent 6a1cb133cd
commit c62d05e1b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 158 additions and 218 deletions

@ -16,51 +16,70 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * 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 * 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 // Discord deletes this so need to store in variable
const { localStorage } = window; const { localStorage } = window;
// listeners for ipc.on // 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 = { const setCssDebounced = debounce((css: string) => VencordNative.quickCss.set(css));
[IpcEvents.GET_REPO]: () => "https://github.com/Vendicated/Vencord", // shrug
[IpcEvents.GET_SETTINGS_DIR]: () => "LocalStorage",
[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));
},
[IpcEvents.GET_SETTINGS]: () => localStorage.getItem("VencordSettings") || "{}",
[IpcEvents.SET_SETTINGS]: (s: string) => localStorage.setItem("VencordSettings", s),
[IpcEvents.GET_UPDATES]: () => ({ ok: true, value: [] }),
[IpcEvents.OPEN_EXTERNAL]: (url: string) => open(url, "_blank"),
};
function onEvent(event: string, ...args: any[]) {
const handler = handlers[event];
if (!handler) throw new Error(`Event ${event} not implemented.`);
return handler(...args);
}
// probably should make this less cursed at some point // probably should make this less cursed at some point
window.VencordNative = { window.VencordNative = {
native: {
getVersions: () => ({}), getVersions: () => ({}),
ipc: { openExternal: async (url) => void open(url, "_blank")
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); 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 }),
}, },
invoke: (event: string, ...args: any[]) => Promise.resolve(onEvent(event, ...args))
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;
}
win.setCss = setCssDebounced;
win.getCurrentCss = () => VencordNative.quickCss.get();
win.getTheme = () =>
getTheme() === Theme.Light
? "vs-light"
: "vs-dark";
win.document.write(monacoHtml);
},
},
settings: {
get: () => localStorage.getItem("VencordSettings") || "{}",
set: async (s: string) => localStorage.setItem("VencordSettings", s),
getSettingsDir: async () => "LocalStorage"
}
}; };

@ -33,7 +33,7 @@ import { patches, PMLogger, startAllPlugins } from "./plugins";
import { localStorage } from "./utils/localStorage"; import { localStorage } from "./utils/localStorage";
import { relaunch } from "./utils/native"; import { relaunch } from "./utils/native";
import { getCloudSettings, putCloudSettings } from "./utils/settingsSync"; 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 { onceReady } from "./webpack";
import { SettingsRouter } from "./webpack/common"; import { SettingsRouter } from "./webpack/common";
@ -76,7 +76,6 @@ async function init() {
if (Settings.autoUpdate) { if (Settings.autoUpdate) {
await update(); await update();
await rebuild();
if (Settings.autoUpdateNotification) if (Settings.autoUpdateNotification)
setTimeout(() => showNotification({ setTimeout(() => showNotification({
title: "Vencord has been updated!", title: "Vencord has been updated!",

@ -16,34 +16,46 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import IPC_EVENTS from "@utils/IpcEvents"; import { IpcEvents } from "@utils/IpcEvents";
import { IpcRenderer, ipcRenderer } from "electron"; import { IpcRes } from "@utils/types";
import { ipcRenderer } from "electron";
function assertEventAllowed(event: string) { function invoke<T = any>(event: IpcEvents, ...args: any[]) {
if (!(event in IPC_EVENTS)) throw new Error(`Event ${event} not allowed.`); 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 { export default {
getVersions: () => process.versions, updater: {
ipc: { getUpdates: () => invoke<IpcRes<Record<"hash" | "author" | "message", string>[]>>(IpcEvents.GET_UPDATES),
send(event: string, ...args: any[]) { update: () => invoke<IpcRes<boolean>>(IpcEvents.UPDATE),
assertEventAllowed(event); rebuild: () => invoke<IpcRes<boolean>>(IpcEvents.BUILD),
ipcRenderer.send(event, ...args); getRepo: () => invoke<IpcRes<string>>(IpcEvents.GET_REPO),
}, },
sendSync<T = any>(event: string, ...args: any[]): T {
assertEventAllowed(event); settings: {
return ipcRenderer.sendSync(event, ...args); get: () => sendSync<string>(IpcEvents.GET_SETTINGS),
set: (settings: string) => invoke<void>(IpcEvents.SET_SETTINGS, settings),
getSettingsDir: () => invoke<string>(IpcEvents.GET_SETTINGS_DIR),
}, },
on(event: string, listener: Parameters<IpcRenderer["on"]>[1]) {
assertEventAllowed(event); quickCss: {
ipcRenderer.on(event, listener); 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));
}, },
off(event: string, listener: Parameters<IpcRenderer["off"]>[1]) {
assertEventAllowed(event); openFile: () => invoke<void>(IpcEvents.OPEN_QUICKCSS),
ipcRenderer.off(event, listener); openEditor: () => invoke<void>(IpcEvents.OPEN_MONACO_EDITOR),
},
native: {
getVersions: () => process.versions as Partial<NodeJS.ProcessVersions>,
openExternal: (url: string) => invoke<void>(IpcEvents.OPEN_EXTERNAL, url)
}, },
invoke<T = any>(event: string, ...args: any[]): Promise<T> {
assertEventAllowed(event);
return ipcRenderer.invoke(event, ...args);
}
}
}; };

@ -17,7 +17,6 @@
*/ */
import { debounce } from "@utils/debounce"; import { debounce } from "@utils/debounce";
import IpcEvents from "@utils/IpcEvents";
import { localStorage } from "@utils/localStorage"; import { localStorage } from "@utils/localStorage";
import Logger from "@utils/Logger"; import Logger from "@utils/Logger";
import { mergeDefaults } from "@utils/misc"; import { mergeDefaults } from "@utils/misc";
@ -94,7 +93,7 @@ const DefaultSettings: Settings = {
}; };
try { 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); mergeDefaults(settings, DefaultSettings);
} catch (err) { } catch (err) {
var settings = mergeDefaults({} as Settings, DefaultSettings); var settings = mergeDefaults({} as Settings, DefaultSettings);
@ -173,7 +172,7 @@ function makeProxy(settings: any, root = settings, path = ""): Settings {
PlainSettings.cloud.settingsSyncVersion = Date.now(); PlainSettings.cloud.settingsSyncVersion = Date.now();
localStorage.Vencord_settingsDirty = true; localStorage.Vencord_settingsDirty = true;
saveSettingsOnFrequentAction(); saveSettingsOnFrequentAction();
VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(root, null, 4)); VencordNative.settings.set(JSON.stringify(root, null, 4));
return true; return true;
} }
}); });
@ -249,10 +248,7 @@ export function migratePluginSettings(name: string, ...oldNames: string[]) {
logger.info(`Migrating settings from old name ${oldName} to ${name}`); logger.info(`Migrating settings from old name ${oldName} to ${name}`);
plugins[name] = plugins[oldName]; plugins[name] = plugins[oldName];
delete plugins[oldName]; delete plugins[oldName];
VencordNative.ipc.invoke( VencordNative.settings.set(JSON.stringify(settings, null, 4));
IpcEvents.SET_SETTINGS,
JSON.stringify(settings, null, 4)
);
break; break;
} }
} }

@ -16,7 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import IpcEvents from "@utils/IpcEvents";
import { Button } from "@webpack/common"; import { Button } from "@webpack/common";
import { Heart } from "./Heart"; import { Heart } from "./Heart";
@ -27,9 +26,7 @@ export default function DonateButton(props: any) {
{...props} {...props}
look={Button.Looks.LINK} look={Button.Looks.LINK}
color={Button.Colors.TRANSPARENT} color={Button.Colors.TRANSPARENT}
onClick={() => onClick={() => VencordNative.native.openExternal("https://github.com/sponsors/Vendicated")}
VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/sponsors/Vendicated")
}
> >
<Heart /> <Heart />
Donate Donate

@ -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);
}

@ -25,7 +25,7 @@ import { Link } from "@components/Link";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
import { classes, useAwaiter } from "@utils/misc"; import { classes, useAwaiter } from "@utils/misc";
import { relaunch } from "@utils/native"; 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 { Alerts, Button, Card, Forms, Parser, React, Switch, Toasts } from "@webpack/common";
import gitHash from "~git-hash"; import gitHash from "~git-hash";
@ -125,7 +125,6 @@ function Updatable(props: CommonProps) {
onClick={withDispatcher(setIsUpdating, async () => { onClick={withDispatcher(setIsUpdating, async () => {
if (await update()) { if (await update()) {
setUpdates([]); setUpdates([]);
await rebuild();
await new Promise<void>(r => { await new Promise<void>(r => {
Alerts.show({ Alerts.show({
title: "Update Success!", title: "Update Success!",

@ -23,7 +23,6 @@ import { classNameFactory } from "@api/Styles";
import DonateButton from "@components/DonateButton"; import DonateButton from "@components/DonateButton";
import ErrorBoundary from "@components/ErrorBoundary"; import ErrorBoundary from "@components/ErrorBoundary";
import { ErrorCard } from "@components/ErrorCard"; import { ErrorCard } from "@components/ErrorCard";
import IpcEvents from "@utils/IpcEvents";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
import { identity, useAwaiter } from "@utils/misc"; import { identity, useAwaiter } from "@utils/misc";
import { relaunch, showItemInFolder } from "@utils/native"; import { relaunch, showItemInFolder } from "@utils/native";
@ -39,7 +38,7 @@ type KeysOfType<Object, Type> = {
}[keyof Object]; }[keyof Object];
function VencordSettings() { function VencordSettings() {
const [settingsDir, , settingsDirPending] = useAwaiter(() => VencordNative.ipc.invoke<string>(IpcEvents.GET_SETTINGS_DIR), { const [settingsDir, , settingsDirPending] = useAwaiter(VencordNative.settings.getSettingsDir, {
fallbackValue: "Loading..." fallbackValue: "Loading..."
}); });
const settings = useSettings(); const settings = useSettings();
@ -101,40 +100,35 @@ function VencordSettings() {
<DonateCard image={donateImage} /> <DonateCard image={donateImage} />
<Forms.FormSection title="Quick Actions"> <Forms.FormSection title="Quick Actions">
<Card className={cl("quick-actions-card")}> <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 <Button
onClick={relaunch} onClick={relaunch}
size={Button.Sizes.SMALL}> size={Button.Sizes.SMALL}>
Restart Client Restart Client
</Button> </Button>
)}
<Button <Button
onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_MONACO_EDITOR)} onClick={() => VencordNative.quickCss.openEditor()}
size={Button.Sizes.SMALL} size={Button.Sizes.SMALL}
disabled={settingsDir === "Loading..."}> disabled={settingsDir === "Loading..."}>
Open QuickCSS File Open QuickCSS File
</Button> </Button>
{!IS_WEB && (
<Button <Button
onClick={() => showItemInFolder(settingsDir)} onClick={() => showItemInFolder(settingsDir)}
size={Button.Sizes.SMALL} size={Button.Sizes.SMALL}
disabled={settingsDirPending}> disabled={settingsDirPending}>
Open Settings Folder Open Settings Folder
</Button> </Button>
)}
<Button <Button
onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, "https://github.com/Vendicated/Vencord")} onClick={() => VencordNative.native.openExternal("https://github.com/Vendicated/Vencord")}
size={Button.Sizes.SMALL} size={Button.Sizes.SMALL}
disabled={settingsDirPending}> disabled={settingsDirPending}>
Open in GitHub Open in GitHub
</Button> </Button>
</React.Fragment> </React.Fragment>
)}
</Card> </Card>
</Forms.FormSection> </Forms.FormSection>

@ -15,7 +15,7 @@
display: flex; display: flex;
gap: 1em; gap: 1em;
align-items: center; align-items: center;
justify-content: space-between; justify-content: space-evenly;
flex-grow: 1; flex-grow: 1;
flex-flow: row wrap; flex-flow: row wrap;
margin-bottom: 1em; margin-bottom: 1em;

@ -19,7 +19,7 @@
import "./updater"; import "./updater";
import { debounce } from "@utils/debounce"; import { debounce } from "@utils/debounce";
import IpcEvents from "@utils/IpcEvents"; import { IpcEvents } from "@utils/IpcEvents";
import { Queue } from "@utils/Queue"; import { Queue } from "@utils/Queue";
import { BrowserWindow, ipcMain, shell } from "electron"; import { BrowserWindow, ipcMain, shell } from "electron";
import { mkdirSync, readFileSync, watch } from "fs"; import { mkdirSync, readFileSync, watch } from "fs";

@ -16,7 +16,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * 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 { execFile as cpExecFile } from "child_process";
import { ipcMain } from "electron"; import { ipcMain } from "electron";
import { join } from "path"; import { join } from "path";

@ -17,7 +17,7 @@
*/ */
import { VENCORD_USER_AGENT } from "@utils/constants"; import { VENCORD_USER_AGENT } from "@utils/constants";
import IpcEvents from "@utils/IpcEvents"; import { IpcEvents } from "@utils/IpcEvents";
import { ipcMain } from "electron"; import { ipcMain } from "electron";
import { writeFile } from "fs/promises"; import { writeFile } from "fs/promises";
import { join } from "path"; import { join } from "path";

@ -22,7 +22,6 @@ import ErrorBoundary from "@components/ErrorBoundary";
import { Flex } from "@components/Flex"; import { Flex } from "@components/Flex";
import { Heart } from "@components/Heart"; import { Heart } from "@components/Heart";
import { Devs } from "@utils/constants"; import { Devs } from "@utils/constants";
import IpcEvents from "@utils/IpcEvents";
import Logger from "@utils/Logger"; import Logger from "@utils/Logger";
import { Margins } from "@utils/margins"; import { Margins } from "@utils/margins";
import { closeModal, Modals, openModal } from "@utils/modal"; import { closeModal, Modals, openModal } from "@utils/modal";
@ -115,7 +114,7 @@ export default definePlugin({
const modalKey = openModal(props => ( const modalKey = openModal(props => (
<ErrorBoundary noop onError={() => { <ErrorBoundary noop onError={() => {
closeModal(modalKey); 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.ModalRoot {...props}>
<Modals.ModalHeader> <Modals.ModalHeader>

@ -155,12 +155,12 @@ export default definePlugin({
}, },
get electronVersion() { get electronVersion() {
return VencordNative.getVersions().electron || window.armcord?.electron || null; return VencordNative.native.getVersions().electron || window.armcord?.electron || null;
}, },
get chromiumVersion() { get chromiumVersion() {
try { try {
return VencordNative.getVersions().chrome return VencordNative.native.getVersions().chrome
// @ts-ignore Typescript will add userAgentData IMMEDIATELY // @ts-ignore Typescript will add userAgentData IMMEDIATELY
|| navigator.userAgentData?.brands?.find(b => b.brand === "Chromium" || b.brand === "Google Chrome")?.version || navigator.userAgentData?.brands?.find(b => b.brand === "Chromium" || b.brand === "Google Chrome")?.version
|| null; || null;

@ -17,7 +17,6 @@
*/ */
import { Settings } from "@api/settings"; import { Settings } from "@api/settings";
import IpcEvents from "@utils/IpcEvents";
import { proxyLazy } from "@utils/proxyLazy"; import { proxyLazy } from "@utils/proxyLazy";
import { findByPropsLazy } from "@webpack"; import { findByPropsLazy } from "@webpack";
import { Flux, FluxDispatcher } from "@webpack/common"; import { Flux, FluxDispatcher } from "@webpack/common";
@ -94,7 +93,7 @@ export const SpotifyStore = proxyLazy(() => {
? "spotify:" + path.replaceAll("/", (_, idx) => idx === 0 ? "" : ":") ? "spotify:" + path.replaceAll("/", (_, idx) => idx === 0 ? "" : ":")
: "https://open.spotify.com" + path; : "https://open.spotify.com" + path;
VencordNative.ipc.invoke(IpcEvents.OPEN_EXTERNAL, url); VencordNative.native.openExternal(url);
} }
// Need to keep track of this manually // Need to keep track of this manually

@ -17,7 +17,6 @@
*/ */
import { debounce } from "@utils/debounce"; import { debounce } from "@utils/debounce";
import IpcEvents from "@utils/IpcEvents";
import { contextBridge, webFrame } from "electron"; import { contextBridge, webFrame } from "electron";
import { readFileSync, watch } from "fs"; import { readFileSync, watch } from "fs";
import { join } from "path"; import { join } from "path";
@ -58,8 +57,8 @@ if (location.protocol !== "data:") {
} }
} // Monaco popout } // Monaco popout
else { else {
contextBridge.exposeInMainWorld("setCss", debounce(s => VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, s))); contextBridge.exposeInMainWorld("setCss", debounce(VencordNative.quickCss.set));
contextBridge.exposeInMainWorld("getCurrentCss", () => VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS)); contextBridge.exposeInMainWorld("getCurrentCss", VencordNative.quickCss.get);
// shrug // shrug
contextBridge.exposeInMainWorld("getTheme", () => "vs-dark"); contextBridge.exposeInMainWorld("getTheme", () => "vs-dark");
} }

@ -1,6 +1,6 @@
/* /*
* Vencord, a modification for Discord's desktop app * 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 * 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 * 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/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
type Enum<T extends Record<string, string>> = { export const enum IpcEvents {
[k in keyof T]: T[k]; QUICK_CSS_UPDATE = "VencordQuickCssUpdate",
} & { [v in keyof T as T[v]]: v; }; GET_QUICK_CSS = "VencordGetQuickCss",
SET_QUICK_CSS = "VencordSetQuickCss",
function strEnum<T extends Record<string, string>>(obj: T): T { GET_SETTINGS_DIR = "VencordGetSettingsDir",
const o = {} as T; GET_SETTINGS = "VencordGetSettings",
for (const key in obj) { SET_SETTINGS = "VencordSetSettings",
o[key] = obj[key] as any; OPEN_EXTERNAL = "VencordOpenExternal",
o[obj[key]] = key as any; OPEN_QUICKCSS = "VencordOpenQuickCss",
GET_UPDATES = "VencordGetUpdates",
GET_REPO = "VencordGetRepo",
UPDATE = "VencordUpdate",
BUILD = "VencordBuild",
OPEN_MONACO_EDITOR = "VencordOpenMonacoEditor",
} }
return Object.freeze(o);
}
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);

@ -20,7 +20,6 @@ export * from "./ChangeList";
export * as Constants from "./constants"; export * as Constants from "./constants";
export * from "./debounce"; export * from "./debounce";
export * as Discord from "./discord"; export * as Discord from "./discord";
export { default as IpcEvents } from "./IpcEvents";
export { default as Logger } from "./Logger"; export { default as Logger } from "./Logger";
export * from "./margins"; export * from "./margins";
export * from "./misc"; export * from "./misc";

@ -18,7 +18,6 @@
import { addSettingsListener, Settings } from "@api/settings"; import { addSettingsListener, Settings } from "@api/settings";
import IpcEvents from "./IpcEvents";
let style: HTMLStyleElement; let style: HTMLStyleElement;
let themesStyle: HTMLStyleElement; let themesStyle: HTMLStyleElement;
@ -29,8 +28,8 @@ export async function toggle(isEnabled: boolean) {
style = document.createElement("style"); style = document.createElement("style");
style.id = "vencord-custom-css"; style.id = "vencord-custom-css";
document.head.appendChild(style); document.head.appendChild(style);
VencordNative.ipc.on(IpcEvents.QUICK_CSS_UPDATE, (_, css: string) => style.textContent = css); VencordNative.quickCss.addChangeListener(css => style.textContent = css);
style.textContent = await VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS); style.textContent = await VencordNative.quickCss.get();
} }
} else } else
style.disabled = !isEnabled; style.disabled = !isEnabled;

@ -22,7 +22,6 @@ import { Toasts } from "@webpack/common";
import { deflateSync, inflateSync } from "fflate"; import { deflateSync, inflateSync } from "fflate";
import { getCloudAuth, getCloudUrl } from "./cloud"; import { getCloudAuth, getCloudUrl } from "./cloud";
import IpcEvents from "./IpcEvents";
import Logger from "./Logger"; import Logger from "./Logger";
import { saveFile } from "./web"; import { saveFile } from "./web";
@ -36,15 +35,15 @@ export async function importSettings(data: string) {
if ("settings" in parsed && "quickCss" in parsed) { if ("settings" in parsed && "quickCss" in parsed) {
Object.assign(PlainSettings, parsed.settings); Object.assign(PlainSettings, parsed.settings);
await VencordNative.ipc.invoke(IpcEvents.SET_SETTINGS, JSON.stringify(parsed.settings, null, 4)); await VencordNative.settings.set(JSON.stringify(parsed.settings, null, 4));
await VencordNative.ipc.invoke(IpcEvents.SET_QUICK_CSS, parsed.quickCss); await VencordNative.quickCss.set(parsed.quickCss);
} else } else
throw new Error("Invalid Settings. Is this even a Vencord Settings file?"); throw new Error("Invalid Settings. Is this even a Vencord Settings file?");
} }
export async function exportSettings() { export async function exportSettings() {
const settings = JSON.parse(VencordNative.ipc.sendSync(IpcEvents.GET_SETTINGS)); const settings = JSON.parse(VencordNative.settings.get());
const quickCss = await VencordNative.ipc.invoke(IpcEvents.GET_QUICK_CSS); const quickCss = await VencordNative.quickCss.get();
return JSON.stringify({ settings, quickCss }, null, 4); return JSON.stringify({ settings, quickCss }, null, 4);
} }
@ -147,7 +146,7 @@ export async function putCloudSettings() {
const { written } = await res.json(); const { written } = await res.json();
PlainSettings.cloud.settingsSyncVersion = written; 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"); cloudSettingsLogger.info("Settings uploaded to cloud successfully");
showNotification({ showNotification({
@ -230,7 +229,7 @@ export async function getCloudSettings(shouldNotify = true, force = false) {
// sync with server timestamp instead of local one // sync with server timestamp instead of local one
PlainSettings.cloud.settingsSyncVersion = written; 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"); cloudSettingsLogger.info("Settings loaded from cloud successfully");
if (shouldNotify) if (shouldNotify)

@ -18,7 +18,6 @@
import gitHash from "~git-hash"; import gitHash from "~git-hash";
import IpcEvents from "./IpcEvents";
import Logger from "./Logger"; import Logger from "./Logger";
import { relaunch } from "./native"; import { relaunch } from "./native";
import { IpcRes } from "./types"; import { IpcRes } from "./types";
@ -39,7 +38,7 @@ async function Unwrap<T>(p: Promise<IpcRes<T>>) {
} }
export async function checkForUpdates() { 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)) { if (changes.some(c => c.hash === gitHash)) {
isNewer = true; isNewer = true;
return (isOutdated = false); return (isOutdated = false);
@ -50,22 +49,18 @@ export async function checkForUpdates() {
export async function update() { export async function update() {
if (!isOutdated) return true; 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; isOutdated = false;
if (!await Unwrap(VencordNative.updater.rebuild()))
throw new Error("The Build failed. Please try manually building the new update");
}
return res; return res;
} }
export function getRepo() { export const getRepo = () => Unwrap(VencordNative.updater.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 async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) { export async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) {
if (IS_WEB) return; 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 && isNewer) return alert("Your local copy has more recent commits. Please stash or reset them.");
if (wantsUpdate) { if (wantsUpdate) {
await update(); await update();
await rebuild();
relaunch(); relaunch();
} }
} }