Vencord/src/patcher.ts

149 lines
5.6 KiB
TypeScript
Raw Normal View History

2022-10-21 23:17:06 +00:00
/*
* Vencord, a modification for Discord's desktop app
* Copyright (c) 2022 Vendicated and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2022-08-29 00:25:27 +00:00
import electron, { app, BrowserWindowConstructorOptions } from "electron";
2022-10-22 16:18:41 +00:00
import { readFileSync } from "fs";
import { dirname, join } from "path";
2022-10-22 16:18:41 +00:00
import { initIpc } from "./ipcMain";
import { installExt } from "./ipcMain/extensions";
2022-10-11 19:48:28 +00:00
import { readSettings } from "./ipcMain/index";
2022-08-29 00:25:27 +00:00
2022-08-31 20:09:36 +00:00
console.log("[Vencord] Starting up...");
2022-08-29 00:25:27 +00:00
// Our injector file at app/index.js
const injectorPath = require.main!.filename;
// special discord_arch_electron injection method
const asarName = injectorPath.endsWith("app.asar/index.js") ? "_app.asar" : "app.asar";
// The original app.asar
const asarPath = join(dirname(injectorPath), "..", asarName);
2022-10-22 16:34:29 +00:00
const discordPkg = require(join(asarPath, "package.json"));
require.main!.filename = join(asarPath, discordPkg.main);
// @ts-ignore Untyped method? Dies from cringe
2022-10-22 16:34:29 +00:00
app.setAppPath(asarPath);
// Repatch after host updates on Windows
if (process.platform === "win32")
require("./patchWin32Updater");
2022-08-29 00:25:27 +00:00
class BrowserWindow extends electron.BrowserWindow {
constructor(options: BrowserWindowConstructorOptions) {
if (options?.webPreferences?.preload && options.title) {
const original = options.webPreferences.preload;
options.webPreferences.preload = join(__dirname, "preload.js");
options.webPreferences.sandbox = false;
2022-08-29 00:25:27 +00:00
process.env.DISCORD_PRELOAD = original;
2022-08-29 16:11:44 +00:00
super(options);
initIpc(this);
} else super(options);
2022-08-29 00:25:27 +00:00
}
}
Object.assign(BrowserWindow, electron.BrowserWindow);
2022-08-29 16:45:58 +00:00
// esbuild may rename our BrowserWindow, which leads to it being excluded
// from getFocusedWindow(), so this is necessary
2022-09-16 20:59:34 +00:00
// https://github.com/discord/electron/blob/13-x-y/lib/browser/api/browser-window.ts#L60-L62
2022-08-29 16:45:58 +00:00
Object.defineProperty(BrowserWindow, "name", { value: "BrowserWindow", configurable: true });
2022-08-29 00:25:27 +00:00
// Replace electrons exports with our custom BrowserWindow
const electronPath = require.resolve("electron");
delete require.cache[electronPath]!.exports;
require.cache[electronPath]!.exports = {
...electron,
BrowserWindow
};
2022-08-29 16:11:44 +00:00
// Patch appSettings to force enable devtools
2022-08-29 00:25:27 +00:00
Object.defineProperty(global, "appSettings", {
2022-08-29 16:11:44 +00:00
set: (v: typeof global.appSettings) => {
2022-08-29 00:25:27 +00:00
v.set("DANGEROUS_ENABLE_DEVTOOLS_ONLY_ENABLE_IF_YOU_KNOW_WHAT_YOURE_DOING", true);
2022-08-29 16:11:44 +00:00
// @ts-ignore
2022-08-29 00:25:27 +00:00
delete global.appSettings;
global.appSettings = v;
},
configurable: true
});
process.env.DATA_DIR = join(app.getPath("userData"), "..", "Vencord");
electron.app.whenReady().then(() => {
// Source Maps! Maybe there's a better way but since the renderer is executed
// from a string I don't think any other form of sourcemaps would work
electron.protocol.registerFileProtocol("vencord", ({ url: unsafeUrl }, cb) => {
let url = unsafeUrl.slice("vencord://".length);
if (url.endsWith("/")) url = url.slice(0, -1);
switch (url) {
case "renderer.js.map":
case "preload.js.map":
case "patcher.js.map": // doubt
cb(join(__dirname, url));
break;
default:
cb({ statusCode: 403 });
}
});
2022-10-11 19:48:28 +00:00
try {
const settings = JSON.parse(readSettings());
if (settings.enableReactDevtools)
installExt("fmkadmapgofadopljbjfkapdkoienihi")
2022-10-11 19:48:28 +00:00
.then(() => console.info("[Vencord] Installed React Developer Tools"))
.catch(err => console.error("[Vencord] Failed to install React Developer Tools", err));
} catch { }
2022-08-29 00:25:27 +00:00
// Remove CSP
electron.session.defaultSession.webRequest.onHeadersReceived(({ responseHeaders, url }, cb) => {
2022-08-29 16:11:44 +00:00
if (responseHeaders) {
2022-09-27 14:59:02 +00:00
delete responseHeaders["content-security-policy-report-only"];
2022-08-29 00:25:27 +00:00
delete responseHeaders["content-security-policy"];
// Fix hosts that don't properly set the content type, such as
// raw.githubusercontent.com
if (url.endsWith(".css"))
responseHeaders["content-type"] = ["text/css"];
2022-08-29 00:25:27 +00:00
}
2022-08-29 16:11:44 +00:00
cb({ cancel: false, responseHeaders });
2022-08-29 00:25:27 +00:00
});
});
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 {
2022-10-22 16:34:29 +00:00
console.log(require.main!.filename);
require(require.main!.filename);
}