Update Windows Update patcher (#404)

This commit is contained in:
Ven 2023-01-12 23:15:38 +01:00 committed by GitHub
parent a8678db78c
commit e70abc57b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 62 deletions

View file

@ -22,7 +22,7 @@ import "./updater";
import { debounce } from "@utils/debounce"; import { debounce } from "@utils/debounce";
import IpcEvents from "@utils/IpcEvents"; import IpcEvents from "@utils/IpcEvents";
import { Queue } from "@utils/Queue"; 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 { mkdirSync, readFileSync, watch } from "fs";
import { open, readFile, writeFile } from "fs/promises"; import { open, readFile, writeFile } from "fs/promises";
import { join } from "path"; 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_QUICKCSS, () => shell.openPath(QUICKCSS_PATH));
ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => { ipcMain.handle(IpcEvents.OPEN_EXTERNAL, (_, url) => {

View file

@ -17,7 +17,7 @@
*/ */
import { app, autoUpdater } from "electron"; 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"; import { basename, dirname, join } from "path";
const { setAppUserModelId } = app; const { setAppUserModelId } = app;
@ -44,6 +44,7 @@ function isNewer($new: string, old: string) {
} }
function patchLatest() { function patchLatest() {
try {
const currentAppPath = dirname(process.execPath); const currentAppPath = dirname(process.execPath);
const currentVersion = basename(currentAppPath); const currentVersion = basename(currentAppPath);
const discordPath = join(currentAppPath, ".."); const discordPath = join(currentAppPath, "..");
@ -56,46 +57,37 @@ function patchLatest() {
if (latestVersion === currentVersion) return; if (latestVersion === currentVersion) return;
const app = join(discordPath, latestVersion, "resources", "app"); const resources = join(discordPath, latestVersion, "resources");
if (existsSync(app)) return; const app = join(resources, "app.asar");
const _app = join(resources, "_app.asar");
if (!existsSync(app) || statSync(app).isDirectory()) return;
console.info("[Vencord] Detected Host Update. Repatching..."); console.info("[Vencord] Detected Host Update. Repatching...");
const patcherPath = join(__dirname, "patcher.js"); renameSync(app, _app);
mkdirSync(app); mkdirSync(app);
writeFileSync(join(app, "package.json"), JSON.stringify({ writeFileSync(join(app, "package.json"), JSON.stringify({
name: "discord", name: "discord",
main: "index.js" main: "index.js"
})); }));
writeFileSync(join(app, "index.js"), `require(${JSON.stringify(patcherPath)});`); 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 // Windows Host Updates install to a new folder app-{HOST_VERSION}, so we
// need to reinject // need to reinject
function patchUpdater() { function patchUpdater() {
const main = require.main!;
const buildInfo = require(join(process.resourcesPath, "build_info.json"));
try { try {
if (buildInfo?.newUpdater) { const autoStartScript = join(require.main!.filename, "..", "autoStart", "win32.js");
const autoStartScript = join(main.filename, "..", "autoStart", "win32.js");
const { update } = require(autoStartScript); const { update } = require(autoStartScript);
// New Updater Injection
require.cache[autoStartScript]!.exports.update = function () { require.cache[autoStartScript]!.exports.update = function () {
patchLatest();
update.apply(this, arguments); 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(); patchLatest();
quitAndInstall.apply(this, arguments);
}; };
}
} catch { } catch {
// OpenAsar uses electrons autoUpdater on Windows // OpenAsar uses electrons autoUpdater on Windows
const { quitAndInstall } = autoUpdater; const { quitAndInstall } = autoUpdater;

View file

@ -18,27 +18,12 @@
import { debounce } from "@utils/debounce"; import { debounce } from "@utils/debounce";
import IpcEvents from "@utils/IpcEvents"; import IpcEvents from "@utils/IpcEvents";
import electron, { contextBridge, ipcRenderer, webFrame } from "electron"; import { contextBridge, ipcRenderer, webFrame } from "electron";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { join } from "path"; import { join } from "path";
import VencordNative from "./VencordNative"; 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); contextBridge.exposeInMainWorld("VencordNative", VencordNative);
if (location.protocol !== "data:") { if (location.protocol !== "data:") {

View file

@ -43,7 +43,6 @@ export default strEnum({
GET_HASHES: "VencordGetHashes", GET_HASHES: "VencordGetHashes",
UPDATE: "VencordUpdate", UPDATE: "VencordUpdate",
BUILD: "VencordBuild", BUILD: "VencordBuild",
GET_DESKTOP_CAPTURE_SOURCES: "VencordGetDesktopCaptureSources",
OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor", OPEN_MONACO_EDITOR: "VencordOpenMonacoEditor",
DOWNLOAD_VENCORD_CSS: "VencordDownloadVencordCss" DOWNLOAD_VENCORD_CSS: "VencordDownloadVencordCss"
} as const); } as const);