This commit is contained in:
thororen1234 2024-07-18 23:54:25 -04:00
commit a64eae919a
20 changed files with 251 additions and 441 deletions

View file

@ -43,11 +43,9 @@ if (IS_VESKTOP || !IS_VANILLA) {
}
switch (url) {
case "renderer.js.map":
case "vencordDesktopRenderer.js.map":
case "preload.js.map":
case "vencordDesktopPreload.js.map":
case "patcher.js.map":
case "vencordDesktopMain.js.map":
case "main.js.map":
cb(join(__dirname, url));
break;
default:

View file

@ -119,7 +119,7 @@ ipcMain.handle(IpcEvents.OPEN_MONACO_EDITOR, async () => {
autoHideMenuBar: true,
darkTheme: true,
webPreferences: {
preload: join(__dirname, IS_DISCORD_DESKTOP ? "preload.js" : "vencordDesktopPreload.js"),
preload: join(__dirname, "preload.js"),
contextIsolation: true,
nodeIntegration: false,
sandbox: false

View file

@ -26,14 +26,18 @@ import { IS_VANILLA } from "./utils/constants";
console.log("[Equicord] Starting up...");
// FIXME: remove at some point
const isLegacyNonAsarVencord = IS_STANDALONE && !__dirname.endsWith(".asar");
if (isLegacyNonAsarVencord) {
console.warn("This is a legacy non asar install! Migrating to asar and restarting...");
require("./updater/http").migrateLegacyToAsar();
}
// Our injector file at app/index.js
const injectorPath = require.main!.filename;
// special discord_arch_electron injection method
const asarName = require.main!.path.endsWith("app.asar") ? "_app.asar" : "app.asar";
// The original app.asar
const asarPath = join(dirname(injectorPath), "..", asarName);
const asarPath = join(dirname(injectorPath), "..", "_app.asar");
const discordPkg = require(join(asarPath, "package.json"));
require.main!.filename = join(asarPath, discordPkg.main);
@ -41,7 +45,7 @@ require.main!.filename = join(asarPath, discordPkg.main);
// @ts-ignore Untyped method? Dies from cringe
app.setAppPath(asarPath);
if (!IS_VANILLA) {
if (!IS_VANILLA && !isLegacyNonAsarVencord) {
const settings = RendererSettings.store;
// Repatch after host updates on Windows
if (process.platform === "win32") {
@ -71,7 +75,7 @@ if (!IS_VANILLA) {
constructor(options: BrowserWindowConstructorOptions) {
if (options?.webPreferences?.preload && options.title) {
const original = options.webPreferences.preload;
options.webPreferences.preload = join(__dirname, IS_DISCORD_DESKTOP ? "preload.js" : "vencordDesktopPreload.js");
options.webPreferences.preload = join(__dirname, "preload.js");
options.webPreferences.sandbox = false;
// work around discord unloading when in background
options.webPreferences.backgroundThrottling = false;
@ -157,5 +161,7 @@ if (!IS_VANILLA) {
console.log("[Equicord] Running in vanilla mode. Not loading Equicord");
}
console.log("[Equicord] Loading original Discord app.asar");
require(require.main!.filename);
if (!isLegacyNonAsarVencord) {
console.log("[Equicord] Loading original Discord app.asar");
require(require.main!.filename);
}

View file

@ -16,12 +16,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export const VENCORD_FILES = [
IS_DISCORD_DESKTOP ? "patcher.js" : "vencordDesktopMain.js",
IS_DISCORD_DESKTOP ? "preload.js" : "vencordDesktopPreload.js",
IS_DISCORD_DESKTOP ? "renderer.js" : "vencordDesktopRenderer.js",
IS_DISCORD_DESKTOP ? "renderer.css" : "vencordDesktopRenderer.css",
];
export const ASAR_FILE = IS_VESKTOP
? "vesktop.asar"
: "desktop.asar";
export function serializeErrors(func: (...args: any[]) => any) {
return async function () {

View file

@ -18,25 +18,21 @@
import { IpcEvents } from "@shared/IpcEvents";
import { VENCORD_USER_AGENT } from "@shared/vencordUserAgent";
import axios from "axios";
import { ipcMain } from "electron";
import { writeFile } from "fs/promises";
import { app, dialog, ipcMain } from "electron";
import { writeFileSync as originalWriteFileSync } from "original-fs";
import { join } from "path";
import gitHash from "~git-hash";
import gitRemote from "~git-remote";
import { serializeErrors, VENCORD_FILES } from "./common";
import { get } from "../utils/simpleGet";
import { ASAR_FILE, serializeErrors } from "./common";
const API_BASE = `https://api.github.com/repos/${gitRemote}`;
let PendingUpdates = [] as [string, string][];
let PendingUpdate: string | null = null;
async function githubGet(endpoint: string) {
return axios({
method: "get",
responseType: "json",
url: API_BASE + endpoint,
return get(API_BASE + endpoint, {
headers: {
Accept: "application/vnd.github+json",
// "All API requests MUST include a valid User-Agent header.
@ -52,7 +48,8 @@ async function calculateGitChanges() {
const res = await githubGet(`/compare/${gitHash}...HEAD`);
return res.data.commits.map((c: any) => ({
const data = JSON.parse(res.toString("utf-8"));
return data.commits.map((c: any) => ({
// github api only sends the long sha
hash: c.sha.slice(0, 7),
author: c.author.login,
@ -63,32 +60,26 @@ async function calculateGitChanges() {
async function fetchUpdates() {
const release = await githubGet("/releases/latest");
const hash = release.data.name.slice(release.data.name.lastIndexOf(" ") + 1);
const data = JSON.parse(release.toString());
const hash = data.name.slice(data.name.lastIndexOf(" ") + 1);
if (hash === gitHash)
return false;
release.data.assets.forEach(({ name, browser_download_url }) => {
if (VENCORD_FILES.some(s => name.startsWith(s))) {
PendingUpdates.push([name, browser_download_url]);
}
});
const asset = data.assets.find(a => a.name === ASAR_FILE);
PendingUpdate = asset.browser_download_url;
return true;
}
async function applyUpdates() {
await Promise.all(PendingUpdates.map(
async ([name, data]) => {
writeFile(
join(__dirname, name),
(await axios({
method: "get",
responseType: "arraybuffer",
url: data,
})).data
);
}
));
PendingUpdates = [];
if (!PendingUpdate) return true;
const data = await get(PendingUpdate);
originalWriteFileSync(__dirname, data);
PendingUpdate = null;
return true;
}
@ -96,3 +87,28 @@ ipcMain.handle(IpcEvents.GET_REPO, serializeErrors(() => `https://github.com/${g
ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges));
ipcMain.handle(IpcEvents.UPDATE, serializeErrors(fetchUpdates));
ipcMain.handle(IpcEvents.BUILD, serializeErrors(applyUpdates));
export async function migrateLegacyToAsar() {
try {
const isFlatpak = process.platform === "linux" && !!process.env.FLATPAK_ID;
if (isFlatpak) throw "Flatpak Discord can't automatically be migrated.";
const data = await get(`https://github.com/${gitRemote}/releases/latest/download/desktop.asar`);
originalWriteFileSync(join(__dirname, "../vencord.asar"), data);
originalWriteFileSync(__filename, '// Legacy shim for new asar\n\nrequire("../vencord.asar");');
app.relaunch();
app.exit();
} catch (e) {
console.error("Failed to migrate to asar", e);
app.whenReady().then(() => {
dialog.showErrorBox(
"Legacy Install",
"The way Vencord loaded was changed and the updater failed to migrate. Please reinstall using the Vencord Installer!"
);
app.exit(1);
});
}
}

View file

@ -16,7 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import axios from "axios";
import { session } from "electron";
import { unzip } from "fflate";
import { constants as fsConstants } from "fs";
@ -25,6 +24,7 @@ import { join } from "path";
import { DATA_DIR } from "./constants";
import { crxToZip } from "./crxToZip";
import { get } from "./simpleGet";
const extensionCacheDir = join(DATA_DIR, "ExtensionCache");
@ -73,15 +73,12 @@ export async function installExt(id: string) {
// Unfortunately, Google does not serve old versions, so this is the only way
? "https://raw.githubusercontent.com/Vendicated/random-files/f6f550e4c58ac5f2012095a130406c2ab25b984d/fmkadmapgofadopljbjfkapdkoienihi.zip"
: `https://clients2.google.com/service/update2/crx?response=redirect&acceptformat=crx2,crx3&x=id%3D${id}%26uc&prodversion=32`;
const response = await axios({
method: "get",
responseType: "blob",
url: url,
const buf = await get(url, {
headers: {
"User-Agent": "Vencord (https://github.com/Vendicated/Vencord)"
"User-Agent": "Equicord (https://github.com/Equicord/Equicord)"
}
});
await extract(crxToZip(Buffer.from(response.data, "binary")), extDir).catch(console.error);
await extract(crxToZip(buf), extDir).catch(console.error);
}
session.defaultSession.loadExtension(extDir);