Refactor ipc to be strongly typed and hide impl details (#1018)
This commit is contained in:
parent
6a1cb133cd
commit
c62d05e1b3
|
@ -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",
|
||||
|
||||
[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);
|
||||
}
|
||||
const setCssDebounced = debounce((css: string) => VencordNative.quickCss.set(css));
|
||||
|
||||
// probably should make this less cursed at some point
|
||||
window.VencordNative = {
|
||||
native: {
|
||||
getVersions: () => ({}),
|
||||
ipc: {
|
||||
send: (event: string, ...args: any[]) => void onEvent(event, ...args),
|
||||
sendSync: onEvent,
|
||||
on(event: string, listener: () => {}) {
|
||||
(listeners[event] ??= new Set()).add(listener);
|
||||
openExternal: async (url) => void open(url, "_blank")
|
||||
},
|
||||
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 { 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!",
|
||||
|
|
|
@ -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);
|
||||
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),
|
||||
},
|
||||
sendSync<T = any>(event: string, ...args: any[]): T {
|
||||
assertEventAllowed(event);
|
||||
return ipcRenderer.sendSync(event, ...args);
|
||||
|
||||
settings: {
|
||||
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);
|
||||
ipcRenderer.on(event, listener);
|
||||
|
||||
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));
|
||||
},
|
||||
off(event: string, listener: Parameters<IpcRenderer["off"]>[1]) {
|
||||
assertEventAllowed(event);
|
||||
ipcRenderer.off(event, listener);
|
||||
|
||||
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)
|
||||
},
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 { 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!",
|
||||
|
|
|
@ -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>
|
||||
{!IS_WEB && (
|
||||
<Button
|
||||
onClick={relaunch}
|
||||
size={Button.Sizes.SMALL}>
|
||||
Restart Client
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
onClick={() => VencordNative.ipc.invoke(IpcEvents.OPEN_MONACO_EDITOR)}
|
||||
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")}
|
||||
onClick={() => VencordNative.native.openExternal("https://github.com/Vendicated/Vencord")}
|
||||
size={Button.Sizes.SMALL}
|
||||
disabled={settingsDirPending}>
|
||||
Open in GitHub
|
||||
</Button>
|
||||
</React.Fragment>
|
||||
)}
|
||||
</Card>
|
||||
</Forms.FormSection>
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
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",
|
||||
}
|
||||
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 * 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";
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue