Vencord/src/main/ipcMain.ts

108 lines
3.3 KiB
TypeScript
Raw Normal View History

2022-10-21 23:17:06 +00:00
/*
* 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/>.
*/
2022-10-22 16:18:41 +00:00
import "./updater";
import { debounce } from "@utils/debounce";
import IpcEvents from "@utils/IpcEvents";
import { Queue } from "@utils/Queue";
2023-01-12 22:15:38 +00:00
import { BrowserWindow, ipcMain, shell } from "electron";
2022-10-02 01:11:30 +00:00
import { mkdirSync, readFileSync, watch } from "fs";
import { open, readFile, writeFile } from "fs/promises";
import { join } from "path";
2022-10-22 16:18:41 +00:00
import monacoHtml from "~fileContent/../components/monacoWin.html;base64";
import { ALLOWED_PROTOCOLS, QUICKCSS_PATH, SETTINGS_DIR, SETTINGS_FILE } from "./utils/constants";
2022-10-02 01:11:30 +00:00
mkdirSync(SETTINGS_DIR, { recursive: true });
function readCss() {
return readFile(QUICKCSS_PATH, "utf-8").catch(() => "");
}
2022-10-11 19:48:28 +00:00
export function readSettings() {
2022-10-02 01:11:30 +00:00
try {
return readFileSync(SETTINGS_FILE, "utf-8");
} catch {
return "{}";
}
}
export function getSettings(): typeof import("@api/settings").Settings {
try {
return JSON.parse(readSettings());
} catch {
return {} as any;
}
}
ipcMain.handle(IpcEvents.OPEN_QUICKCSS, () => shell.openPath(QUICKCSS_PATH));
ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => {
try {
var { protocol } = new URL(url);
} catch {
throw "Malformed URL";
}
if (!ALLOWED_PROTOCOLS.includes(protocol))
throw "Disallowed protocol.";
shell.openExternal(url);
});
2022-10-02 01:11:30 +00:00
2022-10-22 02:41:33 +00:00
const cssWriteQueue = new Queue();
const settingsWriteQueue = new Queue();
2022-10-02 01:11:30 +00:00
ipcMain.handle(IpcEvents.GET_QUICK_CSS, () => readCss());
2022-10-22 02:41:33 +00:00
ipcMain.handle(IpcEvents.SET_QUICK_CSS, (_, css) =>
cssWriteQueue.push(() => writeFile(QUICKCSS_PATH, css))
2022-10-22 02:41:33 +00:00
);
2022-10-02 01:11:30 +00:00
ipcMain.handle(IpcEvents.GET_SETTINGS_DIR, () => SETTINGS_DIR);
ipcMain.on(IpcEvents.GET_SETTINGS, e => e.returnValue = readSettings());
2022-10-02 01:11:30 +00:00
ipcMain.handle(IpcEvents.SET_SETTINGS, (_, s) => {
settingsWriteQueue.push(() => writeFile(SETTINGS_FILE, s));
2022-10-02 01:11:30 +00:00
});
export function initIpc(mainWindow: BrowserWindow) {
open(QUICKCSS_PATH, "a+").then(fd => {
fd.close();
2023-01-22 03:29:58 +00:00
watch(QUICKCSS_PATH, { persistent: false }, debounce(async () => {
2022-10-02 01:11:30 +00:00
mainWindow.webContents.postMessage(IpcEvents.QUICK_CSS_UPDATE, await readCss());
}, 50));
});
}
2022-10-22 02:41:33 +00:00
ipcMain.handle(IpcEvents.OPEN_MONACO_EDITOR, async () => {
const win = new BrowserWindow({
title: "QuickCss Editor",
autoHideMenuBar: true,
darkTheme: true,
2022-10-22 02:41:33 +00:00
webPreferences: {
preload: join(__dirname, "preload.js"),
contextIsolation: true,
2023-02-17 14:37:38 +00:00
nodeIntegration: false,
sandbox: false
2022-10-22 02:41:33 +00:00
}
});
await win.loadURL(`data:text/html;base64,${monacoHtml}`);
});