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 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) => {

View File

@ -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;

View File

@ -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:") {

View File

@ -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);