From eef5dba950ef20989486789d7f7aac9b93371c70 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Wed, 26 Oct 2022 22:13:38 -0400 Subject: [PATCH 1/6] Switch to discord_desktop_core injection --- src/renderer/actions/install.js | 56 ++++++++++++++++++++++++--------- src/renderer/actions/paths.js | 35 ++++++--------------- 2 files changed, 50 insertions(+), 41 deletions(-) diff --git a/src/renderer/actions/install.js b/src/renderer/actions/install.js index e917da2b..d0a1b050 100644 --- a/src/renderer/actions/install.js +++ b/src/renderer/actions/install.js @@ -4,6 +4,7 @@ import {remote, shell} from "electron"; import {promises as fs} from "fs"; import path from "path"; import phin from "phin"; +import rimraf from "rimraf"; import {log, lognewline} from "./utils/log"; import succeed from "./utils/succeed"; @@ -18,7 +19,7 @@ const MAKE_DIR_PROGRESS = 30; const CHECK_OLD_INSTALL = 40; const TRANSFER_OLD_ADDONS = 50; const DOWNLOAD_PACKAGE_PROGRESS = 60; -const INJECT_SHIM_PROGRESS = 90; +const INJECT_SHIM_PROGRESS = 80; const RESTART_DISCORD_PROGRESS = 100; const oldBDFolder = path.join(remote.app.getPath("home"), "Library", "Preferences", "betterdiscord"); // Old MacOS @@ -79,18 +80,33 @@ const downloadFile = phin.defaults({method: "GET", followRedirects: true, header const asarPath = path.join(bdDataFolder, "betterdiscord.asar"); async function downloadAsar() { let downloadUrl = "https://api.github.com/repos/BetterDiscord/BetterDiscord/releases"; + let response; try { - const response = await getJSON(downloadUrl); + response = await getJSON(downloadUrl); const releases = response.body; - const asset = releases && releases.length ? releases[0].assets.find(a => a.name === "betterdiscord.asar") : "https://api.github.com/repos/BetterDiscord/BetterDiscord/releases/assets/39982244"; // temporary workaround - downloadUrl = asset.url; + const asset = releases && releases.length && releases[0].assets && releases[0].assets.find(a => a.name.toLowerCase() === "betterdiscord.asar"); + downloadUrl = asset && asset.url; + if (!downloadUrl) { + let errMessage = "Could not get the asset url"; + if (!asset) errMessage = "Could not get asset object"; + if (!releases) errMessage = "Could not get response body"; + if (!response) errMessage = "Could not get any response"; + throw new Error(errMessage); + } - const resp = await downloadFile(downloadUrl); - const originalFs = require("original-fs").promises; // because electron doesn't like when I write asar files - await originalFs.writeFile(asarPath, resp.body); + try { + const resp = await downloadFile(downloadUrl); + const originalFs = require("original-fs").promises; // because electron doesn't like when I write asar files + await originalFs.writeFile(asarPath, resp.body); + } + catch (error) { + log(`❌ Failed to download package ${downloadUrl}`); + log(`❌ ${error.message}`); + return error; + } } catch (err) { - log(`❌ Failed to download package ${downloadUrl}`); + log(`❌ Failed to get asset url ${downloadUrl}`); log(`❌ ${err.message}`); return err; } @@ -100,18 +116,28 @@ async function injectShims(paths) { const progressPerLoop = (INJECT_SHIM_PROGRESS - progress.value) / paths.length; for (const discordPath of paths) { log("Injecting into: " + discordPath); + const appAsar = path.join(discordPath, "app.asar"); + const discordAsar = path.join(discordPath, "discord.asar"); const appPath = path.join(discordPath, "app"); const pkgFile = path.join(appPath, "package.json"); const indexFile = path.join(appPath, "index.js"); try { - if (process.platform === "win32" || process.platform === "darwin") { - if (!(await exists(appPath))) await fs.mkdir(appPath); - await fs.writeFile(pkgFile, JSON.stringify({name: "betterdiscord", main: "index.js"})); - await fs.writeFile(indexFile, `require("${asarPath.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}");`); - } - else { + // if (process.platform === "win32" || process.platform === "darwin") { + // const originalFs = require("original-fs").promises; + // if (await exists(appAsar)) { + // await originalFs.rename(appAsar, discordAsar); + // // await originalFs.copyFile(appAsar, discordAsar); + // // await originalFs.unlink(appAsar); + // // const error = await new Promise(resolve => rimraf(appAsar, originalFs, resolve)); + // // if (error) throw error; + // } + // if (!(await exists(appPath))) await fs.mkdir(appPath); + // await fs.writeFile(pkgFile, JSON.stringify({name: "betterdiscord", main: "index.js"})); + // await fs.writeFile(indexFile, `require("${asarPath.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}");`); + // } + // else { await fs.writeFile(path.join(discordPath, "index.js"), `require("${asarPath.replace(/\\/g, "\\\\").replace(/"/g, "\\\"")}");\nmodule.exports = require("./core.asar");`); - } + // } log("✅ Injection successful"); progress.set(progress.value + progressPerLoop); } diff --git a/src/renderer/actions/paths.js b/src/renderer/actions/paths.js index 5e07e570..199a838f 100644 --- a/src/renderer/actions/paths.js +++ b/src/renderer/actions/paths.js @@ -22,10 +22,7 @@ const getDiscordPath = function(releaseChannel) { if (!fs.existsSync(basedir)) return ""; const version = fs.readdirSync(basedir).filter(f => safeIsDir(path.join(basedir, f)) && f.split(".").length > 1).sort().reverse()[0]; if (!version) return ""; - resourcePath = path.join(basedir, version, "resources"); - } - else if (process.platform === "darwin") { - resourcePath = path.join("/Applications", `${releaseChannel}.app`, "Contents", "Resources"); + resourcePath = path.join(basedir, version, "modules", "discord_desktop_core-1", "discord_desktop_core"); } else { const basedir = path.join(remote.app.getPath("userData"), "..", releaseChannel.toLowerCase().replace(" ", "")); @@ -45,14 +42,12 @@ for (const channel in platforms) { export const getBrowsePath = function(channel) { if (process.platform === "win32") return path.join(process.env.LOCALAPPDATA, platforms[channel].replace(" ", "")); - else if (process.platform === "darwin") return path.join("/Applications", `${platforms[channel]}.app`); return path.join(remote.app.getPath("userData"), "..", platforms[channel].toLowerCase().replace(" ", "")); }; export const validatePath = function(channel, proposedPath) { if (process.platform === "win32") return validateWindows(channel, proposedPath); - else if (process.platform === "darwin") return validateMac(channel, proposedPath); - return validateLinux(channel, proposedPath); + return validateLinuxMac(channel, proposedPath); }; const validateWindows = function(channel, proposedPath) { @@ -61,36 +56,24 @@ const validateWindows = function(channel, proposedPath) { const isParentDir = fs.existsSync(path.join(proposedPath, channelName)); if (isParentDir) proposedPath = path.join(proposedPath, channelName); - let resourcePath = ""; + let corePath = ""; const selected = path.basename(proposedPath); const isBaseDir = selected === channelName; if (isBaseDir) { const version = fs.readdirSync(proposedPath).filter(f => safeIsDir(path.join(proposedPath, f)) && f.split(".").length > 1).sort().reverse()[0]; if (!version) return ""; - resourcePath = path.join(proposedPath, version, "resources"); + corePath = path.join(proposedPath, version, "modules", "discord_desktop_core-1", "discord_desktop_core"); } - if (selected.startsWith("app-") && selected.split(".").length > 2) resourcePath = path.join(proposedPath, "resources"); - if (selected === "resources") resourcePath = proposedPath; - - const executablePath = path.join(resourcePath, "..", `${channelName}.exe`); - if (fs.existsSync(executablePath)) return resourcePath; - return ""; -}; - -const validateMac = function(channel, proposedPath) { - let resourcePath = ""; - const selected = path.basename(proposedPath); - if (selected === `${platforms[channel]}.app`) resourcePath = path.join(proposedPath, "Contents", "Resources"); - if (selected === "Contents") resourcePath = path.join(proposedPath, "Resources"); - if (selected === "Resources") resourcePath = proposedPath; + if (selected.split(".").length > 2) corePath = path.join(proposedPath, "modules", "discord_desktop_core-1", "discord_desktop_core"); + if (selected === "discord_desktop_core") corePath = proposedPath; - const executablePath = path.join(resourcePath, "..", "MacOS", platforms[channel]); - if (fs.existsSync(executablePath)) return resourcePath; + const coreAsar = path.join(corePath, `core.asar`); + if (fs.existsSync(coreAsar)) return corePath; return ""; }; -const validateLinux = function(channel, proposedPath) { +const validateLinuxMac = function(channel, proposedPath) { if (proposedPath.includes("/snap/")) { remote.dialog.showErrorBox("BetterDiscord Incompatible", "BetterDiscord is currently incompatible with Snap installs of Discord. Support for snap installs is coming soon!"); return ""; From 273c86ac7eb2027f547d30d891df77d22a32d1e6 Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Thu, 27 Oct 2022 20:35:14 -0400 Subject: [PATCH 2/6] Improve download logging --- src/renderer/actions/install.js | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/renderer/actions/install.js b/src/renderer/actions/install.js index d0a1b050..892cbcf9 100644 --- a/src/renderer/actions/install.js +++ b/src/renderer/actions/install.js @@ -79,34 +79,32 @@ const getJSON = phin.defaults({method: "GET", parse: "json", followRedirects: tr const downloadFile = phin.defaults({method: "GET", followRedirects: true, headers: {"User-Agent": "BetterDiscord Installer", "Accept": "application/octet-stream"}}); const asarPath = path.join(bdDataFolder, "betterdiscord.asar"); async function downloadAsar() { - let downloadUrl = "https://api.github.com/repos/BetterDiscord/BetterDiscord/releases"; - let response; + const releaseApiUrl = "https://api.github.com/repos/BetterDiscord/BetterDiscord/releases"; try { - response = await getJSON(downloadUrl); + const response = await getJSON(releaseApiUrl); const releases = response.body; const asset = releases && releases.length && releases[0].assets && releases[0].assets.find(a => a.name.toLowerCase() === "betterdiscord.asar"); - downloadUrl = asset && asset.url; - if (!downloadUrl) { + const assetUrl = asset && asset.url; + if (!assetUrl) { let errMessage = "Could not get the asset url"; if (!asset) errMessage = "Could not get asset object"; if (!releases) errMessage = "Could not get response body"; if (!response) errMessage = "Could not get any response"; throw new Error(errMessage); } - try { - const resp = await downloadFile(downloadUrl); + const resp = await downloadFile(assetUrl); const originalFs = require("original-fs").promises; // because electron doesn't like when I write asar files await originalFs.writeFile(asarPath, resp.body); } catch (error) { - log(`❌ Failed to download package ${downloadUrl}`); + log(`❌ Failed to download package from ${assetUrl}`); log(`❌ ${error.message}`); return error; } } catch (err) { - log(`❌ Failed to get asset url ${downloadUrl}`); + log(`❌ Failed to get asset url from ${releaseApiUrl}`); log(`❌ ${err.message}`); return err; } From 60536959e8dd3c819fd9874e7b979043c52aca1b Mon Sep 17 00:00:00 2001 From: Zack Rauen Date: Sat, 10 Dec 2022 20:15:09 -0500 Subject: [PATCH 3/6] Update deps and meta info --- .github/FUNDING.yml | 2 - README.md | 2 +- package.json | 4 +- src/renderer/actions/utils/fail.js | 2 +- src/renderer/common/SocialLinks.svelte | 2 +- yarn.lock | 496 ++++++++----------------- 6 files changed, 162 insertions(+), 346 deletions(-) diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml index 88390710..04acc8c3 100644 --- a/.github/FUNDING.yml +++ b/.github/FUNDING.yml @@ -1,3 +1 @@ github: rauenzi -patreon: Zerebos -custom: ["https://paypal.me/ZackRauen"] diff --git a/README.md b/README.md index 935a1174..e3c0e8ac 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A simple standalone program which automates the installation, removal and maintenance of BetterDiscord.

- + Chat diff --git a/package.json b/package.json index 8aa68e33..3943d6e8 100644 --- a/package.json +++ b/package.json @@ -18,8 +18,8 @@ "source-map-support": "^0.5.16" }, "devDependencies": { - "electron": "^9.4.0", - "electron-builder": "^22.4.1", + "electron": "^13.6.9", + "electron-builder": "^23.6.0", "electron-webpack": "^2.8.2", "eslint": "^7.21.0", "eslint-plugin-svelte3": "^3.1.2", diff --git a/src/renderer/actions/utils/fail.js b/src/renderer/actions/utils/fail.js index ec01a5e9..56881785 100644 --- a/src/renderer/actions/utils/fail.js +++ b/src/renderer/actions/utils/fail.js @@ -1,7 +1,7 @@ import {log} from "./log"; import {action, status} from "../../stores/installation"; -const discordURL = "https://discord.gg/0Tmfo5ZbORCRqbAd"; +const discordURL = "https://betterdiscord.app/invite"; export default function fail() { log(""); diff --git a/src/renderer/common/SocialLinks.svelte b/src/renderer/common/SocialLinks.svelte index e57ae1cc..32a5caa7 100644 --- a/src/renderer/common/SocialLinks.svelte +++ b/src/renderer/common/SocialLinks.svelte @@ -3,7 +3,7 @@ const webUrl = "https://betterdiscord.app"; const githubUrl = "http://github.com/BetterDiscord/BetterDiscord"; - const donateUrl = "https://www.patreon.com/Zerebos"; + const donateUrl = "https://github.com/sponsors/rauenzi/";