arrpc: migrate from websocket + plugin to electron ipc

This commit is contained in:
Vendicated 2023-10-13 04:00:28 +02:00
parent c445c6194f
commit 0d93e08e99
No known key found for this signature in database
GPG key ID: D66986BAF75ECF18
4 changed files with 16 additions and 13 deletions

View file

@ -5,6 +5,7 @@
*/
import Server from "arrpc";
import { IpcEvents } from "shared/IpcEvents";
import { mainWin } from "./mainWindow";
import { Settings } from "./settings";
@ -17,11 +18,8 @@ export async function initArRPC() {
if (server || !Settings.store.arRPC) return;
try {
// This module starts a server as a side effect, so it needs to be lazy imported
const { send: sendToBridge } = await import("arrpc/src/bridge");
server = await new Server();
server.on("activity", sendToBridge);
server.on("activity", (data: any) => mainWin.webContents.send(IpcEvents.ARRPC_ACTIVITY, JSON.stringify(data)));
server.on("invite", (invite: string, callback: (valid: boolean) => void) => {
invite = String(invite);
if (!inviteCodeRegex.test(invite)) return callback(false);

View file

@ -58,5 +58,10 @@ export const VesktopNative = {
},
capturer: {
getLargeThumbnail: (id: string) => invoke<string>(IpcEvents.CAPTURER_GET_LARGE_THUMBNAIL, id)
},
arrpc: {
onActivity(cb: (data: string) => void) {
ipcRenderer.on(IpcEvents.ARRPC_ACTIVITY, (_, data: string) => cb(data));
}
}
};

View file

@ -47,14 +47,12 @@ customSettingsSections.push(() => ({
className: "vc-vesktop-settings"
}));
const arRPC = Vencord.Plugins.plugins["WebRichPresence (arRPC)"];
const arRPC = Vencord.Plugins.plugins["WebRichPresence (arRPC)"] as any as {
handleEvent(e: MessageEvent): void;
};
arRPC.required = !!Settings.store.arRPC;
VesktopNative.arrpc.onActivity(data => {
if (!Settings.store.arRPC) return;
Settings.addChangeListener("arRPC", v => {
arRPC.required = !!v;
if (v && !arRPC.started) Vencord.Plugins.startPlugin(arRPC);
else if (arRPC.started) {
Vencord.Plugins.stopPlugin(arRPC);
}
arRPC.handleEvent(new MessageEvent("message", { data }));
});

View file

@ -40,5 +40,7 @@ export const enum IpcEvents {
AUTOSTART_ENABLED = "VCD_AUTOSTART_ENABLED",
ENABLE_AUTOSTART = "VCD_ENABLE_AUTOSTART",
DISABLE_AUTOSTART = "VCD_DISABLE_AUTOSTART"
DISABLE_AUTOSTART = "VCD_DISABLE_AUTOSTART",
ARRPC_ACTIVITY = "VCD_ARRPC_ACTIVITY"
}