From 08822dd19064ea5b707117c5ed51f62dd0a8e316 Mon Sep 17 00:00:00 2001 From: V Date: Sun, 9 Apr 2023 04:04:02 +0200 Subject: [PATCH] Improvements for VencordDesktop (#847) --- src/Vencord.ts | 17 ++--- src/components/VencordSettings/Updater.tsx | 7 +- src/globals.d.ts | 1 + src/main/index.ts | 5 +- src/main/updater/common.ts | 27 ++------ src/main/updater/git.ts | 3 +- src/main/updater/http.ts | 20 +----- src/plugins/settings.tsx | 81 ++++++++++------------ src/plugins/supportHelper.tsx | 9 ++- src/preload.ts | 35 +++++----- src/utils/IpcEvents.ts | 1 - src/utils/native.ts | 4 +- src/utils/updater.ts | 22 +----- 13 files changed, 85 insertions(+), 147 deletions(-) diff --git a/src/Vencord.ts b/src/Vencord.ts index a23b1a8..f11ca16 100644 --- a/src/Vencord.ts +++ b/src/Vencord.ts @@ -27,12 +27,14 @@ export { PlainSettings, Settings }; import "./utils/quickCss"; import "./webpack/patchWebpack"; +import { relaunch } from "@utils/native"; + import { showNotification } from "./api/Notifications"; import { PlainSettings, Settings } from "./api/settings"; import { patches, PMLogger, startAllPlugins } from "./plugins"; import { localStorage } from "./utils/localStorage"; import { getCloudSettings, putCloudSettings } from "./utils/settingsSync"; -import { checkForUpdates, rebuild, update, UpdateLogger } from "./utils/updater"; +import { checkForUpdates, rebuild, update,UpdateLogger } from "./utils/updater"; import { onceReady } from "./webpack"; import { SettingsRouter } from "./webpack/common"; @@ -75,23 +77,14 @@ async function init() { if (Settings.autoUpdate) { await update(); - const needsFullRestart = await rebuild(); + await rebuild(); if (Settings.autoUpdateNotification) setTimeout(() => showNotification({ title: "Vencord has been updated!", body: "Click here to restart", permanent: true, noPersist: true, - onClick() { - if (needsFullRestart) { - if (IS_DISCORD_DESKTOP) - window.DiscordNative.app.relaunch(); - else - window.VencordDesktop.app.relaunch(); - } - else - location.reload(); - } + onClick: relaunch }), 10_000); return; } diff --git a/src/components/VencordSettings/Updater.tsx b/src/components/VencordSettings/Updater.tsx index 809a3e7..6dffbc8 100644 --- a/src/components/VencordSettings/Updater.tsx +++ b/src/components/VencordSettings/Updater.tsx @@ -125,7 +125,7 @@ function Updatable(props: CommonProps) { onClick={withDispatcher(setIsUpdating, async () => { if (await update()) { setUpdates([]); - const needFullRestart = await rebuild(); + await rebuild(); await new Promise(r => { Alerts.show({ title: "Update Success!", @@ -133,10 +133,7 @@ function Updatable(props: CommonProps) { confirmText: "Restart", cancelText: "Not now!", onConfirm() { - if (needFullRestart) - relaunch(); - else - location.reload(); + relaunch(); r(); }, onCancel: r diff --git a/src/globals.d.ts b/src/globals.d.ts index 98eccde..3d9e176 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -57,6 +57,7 @@ declare global { */ export var DiscordNative: any; export var VencordDesktop: any; + export var VencordDesktopNative: any; interface Window { webpackChunkdiscord_app: { diff --git a/src/main/index.ts b/src/main/index.ts index b04ef6c..cb723bb 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -32,9 +32,10 @@ if (IS_VENCORD_DESKTOP || !IS_VANILLA) { if (url.endsWith("/")) url = url.slice(0, -1); switch (url) { case "renderer.js.map": + case "vencordDesktopRenderer.js.map": case "preload.js.map": - case "patcher.js.map": // doubt - case "main.js.map": + case "patcher.js.map": + case "vencordDesktopMain.js.map": cb(join(__dirname, url)); break; default: diff --git a/src/main/updater/common.ts b/src/main/updater/common.ts index bc628f0..7cea82d 100644 --- a/src/main/updater/common.ts +++ b/src/main/updater/common.ts @@ -16,28 +16,13 @@ * along with this program. If not, see . */ -import { createHash } from "crypto"; -import { createReadStream } from "fs"; -import { join } from "path"; -export async function calculateHashes() { - const hashes = {} as Record; - - await Promise.all( - [IS_DISCORD_DESKTOP ? "patcher.js" : "main.js", "preload.js", "renderer.js", "renderer.css"].map(file => new Promise(r => { - const fis = createReadStream(join(__dirname, file)); - const hash = createHash("sha1", { encoding: "hex" }); - fis.once("end", () => { - hash.end(); - hashes[file] = hash.read(); - r(); - }); - fis.pipe(hash); - })) - ); - - return hashes; -} +export const VENCORD_FILES = [ + IS_DISCORD_DESKTOP ? "patcher.js" : "vencordDesktopMain.js", + "preload.js", + IS_DISCORD_DESKTOP ? "renderer.js" : "vencordDesktopRenderer.js", + "renderer.css" +]; export function serializeErrors(func: (...args: any[]) => any) { return async function () { diff --git a/src/main/updater/git.ts b/src/main/updater/git.ts index 89c2d3c..d6a29a8 100644 --- a/src/main/updater/git.ts +++ b/src/main/updater/git.ts @@ -22,7 +22,7 @@ import { ipcMain } from "electron"; import { join } from "path"; import { promisify } from "util"; -import { calculateHashes, serializeErrors } from "./common"; +import { serializeErrors } from "./common"; const VENCORD_SRC_DIR = join(__dirname, ".."); @@ -76,7 +76,6 @@ async function build() { return !res.stderr.includes("Build failed"); } -ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes)); ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(getRepo)); ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges)); ipcMain.handle(IpcEvents.UPDATE, serializeErrors(pull)); diff --git a/src/main/updater/http.ts b/src/main/updater/http.ts index a2e2aad..e2a3520 100644 --- a/src/main/updater/http.ts +++ b/src/main/updater/http.ts @@ -26,7 +26,7 @@ import gitHash from "~git-hash"; import gitRemote from "~git-remote"; import { get } from "../utils/simpleGet"; -import { calculateHashes, serializeErrors } from "./common"; +import { serializeErrors, VENCORD_FILES } from "./common"; const API_BASE = `https://api.github.com/repos/${gitRemote}`; let PendingUpdates = [] as [string, string][]; @@ -57,13 +57,6 @@ async function calculateGitChanges() { })); } -const FILES_TO_DOWNLOAD = [ - IS_DISCORD_DESKTOP ? "patcher.js" : "vencordDesktopMain.js", - "preload.js", - IS_DISCORD_DESKTOP ? "renderer.js" : "vencordDesktopRenderer.js", - "renderer.css" -]; - async function fetchUpdates() { const release = await githubGet("/releases/latest"); @@ -73,7 +66,7 @@ async function fetchUpdates() { return false; data.assets.forEach(({ name, browser_download_url }) => { - if (FILES_TO_DOWNLOAD.some(s => name.startsWith(s))) { + if (VENCORD_FILES.some(s => name.startsWith(s))) { PendingUpdates.push([name, browser_download_url]); } }); @@ -83,13 +76,7 @@ async function fetchUpdates() { async function applyUpdates() { await Promise.all(PendingUpdates.map( async ([name, data]) => writeFile( - join( - __dirname, - IS_VENCORD_DESKTOP - // vencordDesktopRenderer.js -> renderer.js - ? name.replace(/vencordDesktop(\w)/, (_, c) => c.toLowerCase()) - : name - ), + join(__dirname, name), await get(data) ) )); @@ -97,7 +84,6 @@ async function applyUpdates() { return true; } -ipcMain.handle(IpcEvents.GET_HASHES, serializeErrors(calculateHashes)); ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(() => `https://github.com/${gitRemote}`)); ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges)); ipcMain.handle(IpcEvents.UPDATE, serializeErrors(fetchUpdates)); diff --git a/src/plugins/settings.tsx b/src/plugins/settings.tsx index c8a6372..f9a7548 100644 --- a/src/plugins/settings.tsx +++ b/src/plugins/settings.tsx @@ -71,66 +71,63 @@ export default definePlugin({ makeSettingsCategories({ ID }: { ID: Record; }) { const makeOnClick = (tab: string) => () => SettingsRouter.open(tab); - const cats = [ + return [ { section: ID.HEADER, label: "Vencord" - }, { + }, + { section: "VencordSettings", label: "Vencord", element: () => , onClick: makeOnClick("VencordSettings") - }, { + }, + { section: "VencordPlugins", label: "Plugins", element: () => , onClick: makeOnClick("VencordPlugins") - }, { + }, + { section: "VencordThemes", label: "Themes", element: () => , onClick: makeOnClick("VencordThemes") - } - ] as Array<{ - section: unknown, - label?: string; - element?: React.ComponentType; - onClick?(): void; - }>; - - if (!IS_WEB) - cats.push({ + }, + !IS_WEB && { section: "VencordUpdater", label: "Updater", element: () => , onClick: makeOnClick("VencordUpdater") - }); - - cats.push({ - section: "VencordCloud", - label: "Cloud", - element: () => , - onClick: makeOnClick("VencordCloud") - }); - - cats.push({ - section: "VencordSettingsSync", - label: "Backup & Restore", - element: () => , - onClick: makeOnClick("VencordSettingsSync") - }); - - if (IS_DEV) - cats.push({ + }, + { + section: "VencordCloud", + label: "Cloud", + element: () => , + onClick: makeOnClick("VencordCloud") + }, + { + section: "VencordSettingsSync", + label: "Backup & Restore", + element: () => , + onClick: makeOnClick("VencordSettingsSync") + }, + IS_DEV && { section: "VencordPatchHelper", label: "Patch Helper", element: PatchHelper!, onClick: makeOnClick("VencordPatchHelper") - }); - - cats.push({ section: ID.DIVIDER }); - - return cats; + }, + IS_VENCORD_DESKTOP && { + section: "VencordDesktop", + label: "Desktop Settings", + element: VencordDesktop.Components.Settings, + onClick: makeOnClick("VencordDesktop") + }, + { + section: ID.DIVIDER + } + ].filter(Boolean); }, options: { @@ -149,14 +146,6 @@ export default definePlugin({ }, }, - tabs: { - vencord: () => , - plugins: () => , - themes: () => , - updater: () => , - sync: () => - }, - get electronVersion() { return VencordNative.getVersions().electron || window.armcord?.electron || null; }, @@ -175,7 +164,7 @@ export default definePlugin({ get additionalInfo() { if (IS_DEV) return " (Dev)"; if (IS_WEB) return " (Web)"; - if (IS_VENCORD_DESKTOP) return " (Vencord Desktop)"; + if (IS_VENCORD_DESKTOP) return ` (VencordDesktop v${VencordDesktopNative.app.getVersion()})`; if (IS_STANDALONE) return " (Standalone)"; return ""; }, diff --git a/src/plugins/supportHelper.tsx b/src/plugins/supportHelper.tsx index 92a5f84..21d9059 100644 --- a/src/plugins/supportHelper.tsx +++ b/src/plugins/supportHelper.tsx @@ -44,11 +44,18 @@ export default definePlugin({ execute() { const { RELEASE_CHANNEL } = window.GLOBAL_ENV; + const client = (() => { + if (IS_DISCORD_DESKTOP) return `Desktop v${DiscordNative.app.getVersion()}`; + if (IS_VENCORD_DESKTOP) return `Vencord Desktop v${VencordDesktopNative.app.getVersion()}`; + if ("armcord" in window) return `ArmCord v${window.armcord.version}`; + return `Web (${navigator.userAgent})`; + })(); + const debugInfo = ` **Vencord Debug Info** > Discord Branch: ${RELEASE_CHANNEL} -> Client: ${typeof DiscordNative === "undefined" ? window.armcord ? "Armcord" : `Web (${navigator.userAgent})` : `Desktop (Electron v${settings.electronVersion})`} +> Client: ${client} > Platform: ${window.navigator.platform} > Vencord Version: ${gitHash}${settings.additionalInfo} > Outdated: ${isOutdated} diff --git a/src/preload.ts b/src/preload.ts index b75574a..276cceb 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -26,27 +26,23 @@ import VencordNative from "./VencordNative"; contextBridge.exposeInMainWorld("VencordNative", VencordNative); +// Discord if (location.protocol !== "data:") { - // Discord - webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8")); + // #region cssInsert const rendererCss = join(__dirname, "renderer.css"); - function insertCss(css: string) { - const style = document.createElement("style"); - style.id = "vencord-css-core"; - style.textContent = css; + const style = document.createElement("style"); + style.id = "vencord-css-core"; + style.textContent = readFileSync(rendererCss, "utf-8"); - if (document.readyState === "complete") { - document.documentElement.appendChild(style); - } else { - document.addEventListener("DOMContentLoaded", () => document.documentElement.appendChild(style), { - once: true - }); - } + if (document.readyState === "complete") { + document.documentElement.appendChild(style); + } else { + document.addEventListener("DOMContentLoaded", () => document.documentElement.appendChild(style), { + once: true + }); } - const css = readFileSync(rendererCss, "utf-8"); - insertCss(css); if (IS_DEV) { // persistent means keep process running if watcher is the only thing still running // which we obviously don't want @@ -54,11 +50,14 @@ if (location.protocol !== "data:") { document.getElementById("vencord-css-core")!.textContent = readFileSync(rendererCss, "utf-8"); }); } + // #endregion - if (process.env.DISCORD_PRELOAD) + if (process.env.DISCORD_PRELOAD) { + webFrame.executeJavaScript(readFileSync(join(__dirname, "renderer.js"), "utf-8")); require(process.env.DISCORD_PRELOAD); -} else { - // Monaco Popout + } +} // 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)); // shrug diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts index dbf3540..57e4bb2 100644 --- a/src/utils/IpcEvents.ts +++ b/src/utils/IpcEvents.ts @@ -40,7 +40,6 @@ export default strEnum({ OPEN_QUICKCSS: "VencordOpenQuickCss", GET_UPDATES: "VencordGetUpdates", GET_REPO: "VencordGetRepo", - GET_HASHES: "VencordGetHashes", UPDATE: "VencordUpdate", BUILD: "VencordBuild", OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor", diff --git a/src/utils/native.ts b/src/utils/native.ts index 05fa0aa..1d29d40 100644 --- a/src/utils/native.ts +++ b/src/utils/native.ts @@ -20,12 +20,12 @@ export function relaunch() { if (IS_DISCORD_DESKTOP) window.DiscordNative.app.relaunch(); else - window.VencordDesktop.app.relaunch(); + window.VencordDesktopNative.app.relaunch(); } export function showItemInFolder(path: string) { if (IS_DISCORD_DESKTOP) window.DiscordNative.fileManager.showItemInFolder(path); else - window.VencordDesktop.fileManager.showItemInFolder(path); + window.VencordDesktopNative.fileManager.showItemInFolder(path); } diff --git a/src/utils/updater.ts b/src/utils/updater.ts index 3606e5f..2fd1561 100644 --- a/src/utils/updater.ts +++ b/src/utils/updater.ts @@ -62,24 +62,9 @@ export function getRepo() { return Unwrap(VencordNative.ipc.invoke>(IpcEvents.GET_REPO)); } -type Hashes = Record<"patcher.js" | "main.js" | "preload.js" | "renderer.js" | "renderer.css", string>; - -/** - * @returns true if hard restart is required - */ export async function rebuild() { - const oldHashes = await Unwrap(VencordNative.ipc.invoke>(IpcEvents.GET_HASHES)); - if (!await Unwrap(VencordNative.ipc.invoke>(IpcEvents.BUILD))) throw new Error("The Build failed. Please try manually building the new update"); - - const newHashes = await Unwrap(VencordNative.ipc.invoke>(IpcEvents.GET_HASHES)); - - if (oldHashes["preload.js"] !== newHashes["preload.js"]) return true; - if (IS_DISCORD_DESKTOP && oldHashes["patcher.js"] !== newHashes["patcher.js"]) return true; - if (IS_VENCORD_DESKTOP && oldHashes["main.js"] !== newHashes["main.js"]) return true; - - return false; } export async function maybePromptToUpdate(confirmMessage: string, checkForDev = false) { @@ -93,11 +78,8 @@ 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(); - const needFullRestart = await rebuild(); - if (needFullRestart) - relaunch(); - else - location.reload(); + await rebuild(); + relaunch(); } } } catch (err) {