From a7dbd73547104540218d4699fcf643fca727d2fc Mon Sep 17 00:00:00 2001 From: Ven Date: Sat, 22 Oct 2022 18:17:02 +0200 Subject: Windows: Patch host updater to reinject (#138) --- src/patcher.ts | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) (limited to 'src/patcher.ts') diff --git a/src/patcher.ts b/src/patcher.ts index eca35f0..14f0812 100644 --- a/src/patcher.ts +++ b/src/patcher.ts @@ -17,13 +17,27 @@ */ import electron, { app, BrowserWindowConstructorOptions } from "electron"; -import { join } from "path"; +import { dirname, join } from "path"; import { initIpc } from "./ipcMain"; import { installExt } from "./ipcMain/extensions"; import { readSettings } from "./ipcMain/index"; +import { readFileSync } from "fs"; console.log("[Vencord] Starting up..."); +// Our injector file at app/index.js +const injectorPath = require.main!.filename; +// The original app.asar +const discordPath = join(dirname(injectorPath), "..", "app.asar"); +// Full main path Discord uses +require.main!.filename = join(discordPath, "app_bootstrap/index.js"); +// @ts-ignore Untyped method? Dies from cringe +app.setAppPath(discordPath); + +// Repatch after host updates on Windows +if (process.platform === "win32") + require("./patchWin32Updater"); + class BrowserWindow extends electron.BrowserWindow { constructor(options: BrowserWindowConstructorOptions) { if (options?.webPreferences?.preload && options.title) { @@ -88,3 +102,23 @@ electron.app.whenReady().then(() => { cb({ cancel: false, responseHeaders }); }); }); + +console.log("[Vencord] Loading original Discord app.asar"); +// Legacy Vencord Injector requires "../app.asar". However, because we +// restore the require.main above this is messed up, so monkey patch Module._load to +// redirect such requires +// FIXME: remove this eventually +if (readFileSync(injectorPath, "utf-8").includes('require("../app.asar")')) { + console.warn("[Vencord] [--> WARNING <--] You have a legacy Vencord install. Please reinject"); + const Module = require("module"); + const loadModule = Module._load; + Module._load = function (path: string) { + if (path === "../app.asar") { + Module._load = loadModule; + arguments[0] = require.main!.filename; + } + return loadModule.apply(this, arguments); + }; +} else { + require(discordPath); +} -- cgit