Skip to content

Commit f335c82

Browse files
authored
Improve persistence after Discord updates (#4441)
improves update persistence & ports to Linux
1 parent d524de8 commit f335c82

2 files changed

Lines changed: 34 additions & 37 deletions

File tree

src/main/patcher.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ console.log("[Vencord] Starting up...");
2828
// Our injector file at app/index.js
2929
const injectorPath = require.main!.filename;
3030

31-
// special discord_arch_electron injection method
31+
// Original Discord app.asar name
3232
const asarName = require.main!.path.endsWith("app.asar") ? "_app.asar" : "app.asar";
3333

34-
// The original app.asar
34+
// Original Discord app.asar
3535
const asarPath = join(dirname(injectorPath), "..", asarName);
3636

3737
const discordPkg = require(join(asarPath, "package.json"));
@@ -42,28 +42,29 @@ app.setAppPath(asarPath);
4242

4343
if (!IS_VANILLA) {
4444
const settings = RendererSettings.store;
45-
// Repatch after host updates on Windows
46-
if (process.platform === "win32") {
47-
require("./patchWin32Updater");
48-
49-
if (settings.winCtrlQ) {
50-
const originalBuild = Menu.buildFromTemplate;
51-
Menu.buildFromTemplate = function (template) {
52-
if (template[0]?.label === "&File") {
53-
const { submenu } = template[0];
54-
if (Array.isArray(submenu)) {
55-
submenu.push({
56-
label: "Quit (Hidden)",
57-
visible: false,
58-
acceleratorWorksWhenHidden: true,
59-
accelerator: "Control+Q",
60-
click: () => app.quit()
61-
});
62-
}
45+
46+
// Repatch after host updates on Windows and Linux
47+
if (process.platform === "win32" || process.platform === "linux") {
48+
require("./persistAfterDiscordUpdates");
49+
}
50+
51+
if (process.platform === "win32" && settings.winCtrlQ) {
52+
const originalBuild = Menu.buildFromTemplate;
53+
Menu.buildFromTemplate = function (template) {
54+
if (template[0]?.label === "&File") {
55+
const { submenu } = template[0];
56+
if (Array.isArray(submenu)) {
57+
submenu.push({
58+
label: "Quit (Hidden)",
59+
visible: false,
60+
acceleratorWorksWhenHidden: true,
61+
accelerator: "Control+Q",
62+
click: () => app.quit()
63+
});
6364
}
64-
return originalBuild.call(this, template);
65-
};
66-
}
65+
}
66+
return originalBuild.call(this, template);
67+
};
6768
}
6869

6970
class BrowserWindow extends electron.BrowserWindow {
@@ -137,13 +138,11 @@ if (!IS_VANILLA) {
137138
process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord");
138139

139140
// Monkey patch commandLine to:
140-
// - disable WidgetLayering: Fix DevTools context menus https://github.com/electron/electron/issues/38790
141141
// - disable UseEcoQoSForBackgroundProcess: Work around Discord unloading when in background
142142
const originalAppend = app.commandLine.appendSwitch;
143143
app.commandLine.appendSwitch = function (...args) {
144144
if (args[0] === "disable-features") {
145145
const disabledFeatures = new Set((args[1] ?? "").split(","));
146-
disabledFeatures.add("WidgetLayering");
147146
disabledFeatures.add("UseEcoQoSForBackgroundProcess");
148147
args[1] += [...disabledFeatures].join(",");
149148
}
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import { app } from "electron";
20-
import { existsSync, mkdirSync, readdirSync, renameSync, statSync, writeFileSync } from "original-fs";
20+
import { copyFileSync, existsSync, readdirSync, renameSync } from "original-fs";
2121
import { basename, dirname, join } from "path";
2222

2323
function isNewer($new: string, old: string) {
@@ -47,21 +47,19 @@ function patchLatest() {
4747

4848
if (latestVersion === currentVersion) return;
4949

50+
const oldResources = join(discordPath, currentVersion, "resources");
51+
const oldVencordAsar = join(oldResources, "app.asar");
52+
5053
const resources = join(discordPath, latestVersion, "resources");
51-
const app = join(resources, "app.asar");
52-
const _app = join(resources, "_app.asar");
54+
const newAppAsar = join(resources, "app.asar");
55+
const newAppAsarBackup = join(resources, "_app.asar");
5356

54-
if (!existsSync(app) || statSync(app).isDirectory()) return;
57+
if (!existsSync(oldVencordAsar) || !existsSync(newAppAsar)) return;
5558

56-
console.info("[Vencord] Detected Host Update. Repatching...");
59+
console.info(`[Vencord] Detected Host Update (${currentVersion} -> ${latestVersion}). Repatching...`);
5760

58-
renameSync(app, _app);
59-
mkdirSync(app);
60-
writeFileSync(join(app, "package.json"), JSON.stringify({
61-
name: "discord",
62-
main: "index.js"
63-
}));
64-
writeFileSync(join(app, "index.js"), `require(${JSON.stringify(join(__dirname, "patcher.js"))});`);
61+
renameSync(newAppAsar, newAppAsarBackup);
62+
copyFileSync(oldVencordAsar, newAppAsar);
6563
} catch (err) {
6664
console.error("[Vencord] Failed to repatch latest host update", err);
6765
}

0 commit comments

Comments
 (0)