From 7781dface9b0ea448d191a718beaabf14c6dfbc5 Mon Sep 17 00:00:00 2001 From: Vendicated Date: Sun, 21 Jul 2024 02:52:28 +0200 Subject: [PATCH] fix: apply update only on quit --- src/main/patcher.ts | 6 +----- src/main/updater/http.ts | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/main/patcher.ts b/src/main/patcher.ts index 6ab4162d7..2c2740289 100644 --- a/src/main/patcher.ts +++ b/src/main/patcher.ts @@ -27,11 +27,7 @@ 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..."); - require("./updater/http").migrateLegacyToAsar(); -} +export const isLegacyNonAsarVencord = IS_STANDALONE && !__dirname.endsWith(".asar"); // Our injector file at app/index.js const injectorPath = require.main!.filename; diff --git a/src/main/updater/http.ts b/src/main/updater/http.ts index bd70b51d2..70670a036 100644 --- a/src/main/updater/http.ts +++ b/src/main/updater/http.ts @@ -16,10 +16,15 @@ * along with this program. If not, see . */ +import { isLegacyNonAsarVencord } from "@main/patcher"; import { IpcEvents } from "@shared/IpcEvents"; import { VENCORD_USER_AGENT } from "@shared/vencordUserAgent"; import { app, dialog, ipcMain } from "electron"; -import { writeFileSync as originalWriteFileSync } from "original-fs"; +import { + existsSync as originalExistsSync, + renameSync as originalRenameSync, + writeFileSync as originalWriteFileSync, +} from "original-fs"; import { join } from "path"; import gitHash from "~git-hash"; @@ -31,6 +36,8 @@ import { ASAR_FILE, serializeErrors } from "./common"; const API_BASE = `https://api.github.com/repos/${gitRemote}`; let PendingUpdate: string | null = null; +let hasUpdateToApplyOnQuit = false; + async function githubGet(endpoint: string) { return get(API_BASE + endpoint, { headers: { @@ -76,7 +83,8 @@ async function applyUpdates() { if (!PendingUpdate) return true; const data = await get(PendingUpdate); - originalWriteFileSync(__dirname, data); + originalWriteFileSync(__dirname + ".new", data); + hasUpdateToApplyOnQuit = true; PendingUpdate = null; @@ -88,7 +96,7 @@ ipcMain.handle(IpcEvents.GET_UPDATES, serializeErrors(calculateGitChanges)); ipcMain.handle(IpcEvents.UPDATE, serializeErrors(fetchUpdates)); ipcMain.handle(IpcEvents.BUILD, serializeErrors(applyUpdates)); -export async function migrateLegacyToAsar() { +async function migrateLegacyToAsar() { try { const isFlatpak = process.platform === "linux" && !!process.env.FLATPAK_ID; if (isFlatpak) throw "Flatpak Discord can't automatically be migrated."; @@ -112,3 +120,26 @@ export async function migrateLegacyToAsar() { }); } } + +function applyPreviousUpdate() { + originalRenameSync(__dirname + ".new", __dirname); + + app.relaunch(); + app.exit(); +} + + +app.on("will-quit", () => { + if (hasUpdateToApplyOnQuit) + originalRenameSync(__dirname + ".new", __dirname); +}); + +if (isLegacyNonAsarVencord) { + console.warn("This is a legacy non asar install! Migrating to asar and restarting..."); + migrateLegacyToAsar(); +} + +if (originalExistsSync(__dirname + ".new")) { + console.warn("Found previous not applied update, applying now and restarting..."); + applyPreviousUpdate(); +}