From e70abc57b6885e97dff54b89e752ab6df949bd67 Mon Sep 17 00:00:00 2001 From: Ven Date: Thu, 12 Jan 2023 23:15:38 +0100 Subject: [PATCH] Update Windows Update patcher (#404) --- src/ipcMain/index.ts | 5 +-- src/patchWin32Updater.ts | 74 ++++++++++++++++++---------------------- src/preload.ts | 17 +-------- src/utils/IpcEvents.ts | 1 - 4 files changed, 35 insertions(+), 62 deletions(-) diff --git a/src/ipcMain/index.ts b/src/ipcMain/index.ts index ae8a96db0..3f22b7253 100644 --- a/src/ipcMain/index.ts +++ b/src/ipcMain/index.ts @@ -22,7 +22,7 @@ import "./updater"; import { debounce } from "@utils/debounce"; import IpcEvents from "@utils/IpcEvents"; import { Queue } from "@utils/Queue"; -import { BrowserWindow, desktopCapturer, ipcMain, shell } from "electron"; +import { BrowserWindow, ipcMain, shell } from "electron"; import { mkdirSync, readFileSync, watch } from "fs"; import { open, readFile, writeFile } from "fs/promises"; import { join } from "path"; @@ -45,9 +45,6 @@ export function readSettings() { } } -// Fix for screensharing in Electron >= 17 -ipcMain.handle(IpcEvents.GET_DESKTOP_CAPTURE_SOURCES, (_, opts) => desktopCapturer.getSources(opts)); - ipcMain.handle(IpcEvents.OPEN_QUICKCSS, () => shell.openPath(QUICKCSS_PATH)); ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => { diff --git a/src/patchWin32Updater.ts b/src/patchWin32Updater.ts index e853ebf4d..e08e37dc0 100644 --- a/src/patchWin32Updater.ts +++ b/src/patchWin32Updater.ts @@ -17,7 +17,7 @@ */ import { app, autoUpdater } from "electron"; -import { existsSync, mkdirSync, readdirSync, writeFileSync } from "fs"; +import { existsSync, mkdirSync, readdirSync, renameSync, statSync, writeFileSync } from "fs"; import { basename, dirname, join } from "path"; const { setAppUserModelId } = app; @@ -44,58 +44,50 @@ function isNewer($new: string, old: string) { } function patchLatest() { - const currentAppPath = dirname(process.execPath); - const currentVersion = basename(currentAppPath); - const discordPath = join(currentAppPath, ".."); + try { + const currentAppPath = dirname(process.execPath); + const currentVersion = basename(currentAppPath); + const discordPath = join(currentAppPath, ".."); - const latestVersion = readdirSync(discordPath).reduce((prev, curr) => { - return (curr.startsWith("app-") && isNewer(curr, prev)) - ? curr - : prev; - }, currentVersion as string); + const latestVersion = readdirSync(discordPath).reduce((prev, curr) => { + return (curr.startsWith("app-") && isNewer(curr, prev)) + ? curr + : prev; + }, currentVersion as string); - if (latestVersion === currentVersion) return; + if (latestVersion === currentVersion) return; - const app = join(discordPath, latestVersion, "resources", "app"); - if (existsSync(app)) return; + const resources = join(discordPath, latestVersion, "resources"); + const app = join(resources, "app.asar"); + const _app = join(resources, "_app.asar"); - console.info("[Vencord] Detected Host Update. Repatching..."); + if (!existsSync(app) || statSync(app).isDirectory()) return; - const patcherPath = join(__dirname, "patcher.js"); - mkdirSync(app); - writeFileSync(join(app, "package.json"), JSON.stringify({ - name: "discord", - main: "index.js" - })); - writeFileSync(join(app, "index.js"), `require(${JSON.stringify(patcherPath)});`); + console.info("[Vencord] Detected Host Update. Repatching..."); + + renameSync(app, _app); + mkdirSync(app); + writeFileSync(join(app, "package.json"), JSON.stringify({ + name: "discord", + main: "index.js" + })); + writeFileSync(join(app, "index.js"), `require(${JSON.stringify(join(__dirname, "patcher.js"))});`); + } catch (err) { + console.error("[Vencord] Failed to repatch latest host update", err); + } } // Windows Host Updates install to a new folder app-{HOST_VERSION}, so we // need to reinject function patchUpdater() { - const main = require.main!; - const buildInfo = require(join(process.resourcesPath, "build_info.json")); - try { - if (buildInfo?.newUpdater) { - const autoStartScript = join(main.filename, "..", "autoStart", "win32.js"); - const { update } = require(autoStartScript); + const autoStartScript = join(require.main!.filename, "..", "autoStart", "win32.js"); + const { update } = require(autoStartScript); - // New Updater Injection - require.cache[autoStartScript]!.exports.update = function () { - patchLatest(); - update.apply(this, arguments); - }; - } else { - const hostUpdaterScript = join(main.filename, "..", "hostUpdater.js"); - const { quitAndInstall } = require(hostUpdaterScript); - - // Old Updater Injection - require.cache[hostUpdaterScript]!.exports.quitAndInstall = function () { - patchLatest(); - quitAndInstall.apply(this, arguments); - }; - } + require.cache[autoStartScript]!.exports.update = function () { + update.apply(this, arguments); + patchLatest(); + }; } catch { // OpenAsar uses electrons autoUpdater on Windows const { quitAndInstall } = autoUpdater; diff --git a/src/preload.ts b/src/preload.ts index 746008142..ee2fb80d7 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -18,27 +18,12 @@ import { debounce } from "@utils/debounce"; import IpcEvents from "@utils/IpcEvents"; -import electron, { contextBridge, ipcRenderer, webFrame } from "electron"; +import { contextBridge, ipcRenderer, webFrame } from "electron"; import { readFileSync } from "fs"; import { join } from "path"; import VencordNative from "./VencordNative"; -if (electron.desktopCapturer === void 0) { - // Fix for desktopCapturer being main only in Electron 17+ - // Discord accesses this in discord_desktop_core (DiscordNative.desktopCapture.getDesktopCaptureSources) - // and errors with cannot "read property getSources() of undefined" - // see discord_desktop_core/app/discord_native/renderer/desktopCapture.js - const electronPath = require.resolve("electron"); - delete require.cache[electronPath]!.exports; - require.cache[electronPath]!.exports = { - ...electron, - desktopCapturer: { - getSources: opts => ipcRenderer.invoke(IpcEvents.GET_DESKTOP_CAPTURE_SOURCES, opts) - } - }; -} - contextBridge.exposeInMainWorld("VencordNative", VencordNative); if (location.protocol !== "data:") { diff --git a/src/utils/IpcEvents.ts b/src/utils/IpcEvents.ts index 345146b22..e97e41b0f 100644 --- a/src/utils/IpcEvents.ts +++ b/src/utils/IpcEvents.ts @@ -43,7 +43,6 @@ export default strEnum({ GET_HASHES: "VencordGetHashes", UPDATE: "VencordUpdate", BUILD: "VencordBuild", - GET_DESKTOP_CAPTURE_SOURCES: "VencordGetDesktopCaptureSources", OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor", DOWNLOAD_VENCORD_CSS: "VencordDownloadVencordCss" } as const);