From 2ace675c0002b8b2ddffdf1093ba619d84a2d0a8 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Fri, 19 Jul 2024 00:52:32 +0200 Subject: [PATCH] migrate legacy installs --- src/main/patcher.ts | 21 ++++++++++++++------- src/main/updater/http.ts | 30 +++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main/patcher.ts b/src/main/patcher.ts index e5b87290d..7b484ff5f 100644 --- a/src/main/patcher.ts +++ b/src/main/patcher.ts @@ -22,18 +22,23 @@ import { dirname, join } from "path"; import { initIpc } from "./ipcMain"; import { RendererSettings } from "./settings"; +import { migrateLegacyToAsar } from "./updater/http"; import { IS_VANILLA } from "./utils/constants"; console.log("[Vencord] 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..."); + 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 +46,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") { @@ -157,5 +162,7 @@ if (!IS_VANILLA) { console.log("[Vencord] Running in vanilla mode. Not loading Vencord"); } -console.log("[Vencord] Loading original Discord app.asar"); -require(require.main!.filename); +if (!isLegacyNonAsarVencord) { + console.log("[Vencord] Loading original Discord app.asar"); + require(require.main!.filename); +} diff --git a/src/main/updater/http.ts b/src/main/updater/http.ts index 887d8e3ef..13a366510 100644 --- a/src/main/updater/http.ts +++ b/src/main/updater/http.ts @@ -18,7 +18,7 @@ import { IpcEvents } from "@shared/IpcEvents"; import { VENCORD_USER_AGENT } from "@shared/vencordUserAgent"; -import { ipcMain } from "electron"; +import { app, dialog, ipcMain } from "electron"; import { writeFile } from "fs/promises"; import { join } from "path"; @@ -76,14 +76,7 @@ async function applyUpdates() { if (!PendingUpdate) return true; const data = await get(PendingUpdate); - - if (__dirname.endsWith(".asar")) { - await writeFile(__dirname, data); - } else { // legacy plain folder install - await writeFile(join(__dirname, "../vencord.asar"), data); - - await writeFile(__filename, '// legacy install workaround\n\nrequire("../vencord.asar");'); - } + await writeFile(__dirname, data); PendingUpdate = null; @@ -94,3 +87,22 @@ 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 data = await get(`https://github.com/${gitRemote}/releases/latest/desktop.asar`); + await writeFile(join(__dirname, "../vencord.asar"), data); + await writeFile(__filename, "// Legacy shim for new asar\n\nrequire(\"../vencord.asar\");"); + app.relaunch(); + } 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.quit(); + }); + } +}