Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linux Improvements #653

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"signingkey",
"Skema",
"skus",
"uncache",
"uninject",
"uninjector",
"uninjectors",
Expand Down
63 changes: 37 additions & 26 deletions scripts/inject/injector.mts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { chown, copyFile, mkdir, rename, rm, stat, writeFile } from "fs/promises";
import path, { join, sep } from "path";
import { fileURLToPath } from "url";
import { createPackage, extractAll, statFile, uncache } from "@electron/asar";
import { entryPoint as argEntryPoint, exitCode } from "./index.mjs";

import { AnsiEscapes, getCommand } from "./util.mjs";

import { execSync } from "child_process";
import { DiscordPlatform, PlatformModule } from "./types.mjs";
import { CONFIG_PATH } from "../../src/util.mjs";
Expand All @@ -26,29 +29,30 @@ export const isDiscordInstalled = async (appDir: string, silent?: boolean): Prom

// If app.orig.asar but no app.asar, move app.orig.asar to app.asar
// Fixes a case where app.asar was deleted (unplugged) but app.orig.asar couldn't be moved back
// Fixes incase using old version of replugged
export const correctMissingMainAsar = async (appDir: string): Promise<boolean> => {
try {
await stat(join(appDir, "..", "app.orig.asar"));
console.warn(
`${AnsiEscapes.YELLOW}Your Discord installation was not properly unplugged, attempting to fix...${AnsiEscapes.RESET}`,
);
try {
await stat(join(appDir, "..", "app.asar"));
await rm(join(appDir, "..", "app.asar"), { recursive: true, force: true });
} catch {}
try {
await rename(join(appDir, "..", "app.orig.asar"), join(appDir, "..", "app.asar"));
console.log(
`${AnsiEscapes.GREEN}Fixed your Discord installation successfully! Continuing with Replugged installation...${AnsiEscapes.RESET}`,
"\n",
);
} catch {
console.warn(
`${AnsiEscapes.YELLOW}Your Discord installation was not properly unplugged, attempting to fix...${AnsiEscapes.RESET}`,
console.error(
`${AnsiEscapes.RED}Failed to fix your Discord installation, please try unplugging and plugging again.${AnsiEscapes.RESET}`,
"\n",
);
try {
await rename(join(appDir, "..", "app.orig.asar"), join(appDir, "..", "app.asar"));
console.log(
`${AnsiEscapes.GREEN}Fixed your Discord installation successfully! Continuing with Replugged installation...${AnsiEscapes.RESET}`,
"\n",
);
} catch {
console.error(
`${AnsiEscapes.RED}Failed to fix your Discord installation, please try unplugging and plugging again.${AnsiEscapes.RESET}`,
"\n",
);
console.error("If the error persists, please reinstall Discord and try again.");
return false;
}
console.error("If the error persists, please reinstall Discord and try again.");
return false;
}
} catch {}

Expand All @@ -57,7 +61,8 @@ export const correctMissingMainAsar = async (appDir: string): Promise<boolean> =

export const isPlugged = async (appDir: string): Promise<boolean> => {
try {
await stat(join(appDir, "..", "app.orig.asar"));
uncache(appDir);
await statFile(appDir, "app.orig");
return true;
} catch {
return false;
Expand Down Expand Up @@ -112,19 +117,19 @@ export const inject = async (
const entryPoint =
argEntryPoint ??
(prod ? join(CONFIG_PATH, "replugged.asar") : join(dirname, "..", "..", "dist/main.js"));

const entryPointDir = path.dirname(entryPoint);

if (appDir.includes("flatpak")) {
const discordName = platform === "canary" ? "DiscordCanary" : "Discord";
const overrideCommand = `${
appDir.startsWith("/var") ? "sudo flatpak override" : "flatpak override --user"
} com.discordapp.${discordName} --filesystem=${entryPointDir}`;
} com.discordapp.${discordName} --filesystem=${prod ? entryPointDir : join(dirname, "..", "..")}`;

console.log(
`${AnsiEscapes.YELLOW}Flatpak detected, allowing Discord access to Replugged files (${entryPointDir})${AnsiEscapes.RESET}`,
`${AnsiEscapes.YELLOW}Flatpak detected, allowing Discord access to Replugged files (${prod ? entryPointDir : join(dirname, "..", "..")})${AnsiEscapes.RESET}`,
);
execSync(overrideCommand);
console.log("Done!");
}

try {
Expand Down Expand Up @@ -154,22 +159,26 @@ export const inject = async (
} catch {}
}
}

await mkdir(appDir);
const tempDir = join(appDir, "..", "temp");
await mkdir(tempDir);
await Promise.all([
writeFile(
join(appDir, "index.js"),
join(tempDir, "index.js"),
`require("${entryPoint.replace(RegExp(sep.repeat(2), "g"), "/")}")`,
),
writeFile(
join(appDir, "package.json"),
join(appDir, "..", "temp", "package.json"),
JSON.stringify({
main: "index.js",
name: "discord",
}),
),
extractAll(join(appDir, "..", "app.orig.asar"), join(tempDir, "app.orig")),
]);

await createPackage(tempDir, appDir);
await rm(join(appDir, "..", "app.orig.asar"), { recursive: true, force: true });
await rm(tempDir, { recursive: true, force: true });
return true;
};

Expand All @@ -190,9 +199,11 @@ export const uninject = async (
);
return false;
}

const tempDir = join(appDir, "..", "temp");
await extractAll(appDir, tempDir);
await rm(appDir, { recursive: true, force: true });
await rename(join(appDir, "..", "app.orig.asar"), appDir);
await createPackage(join(tempDir, "app.orig"), appDir);
await rm(tempDir, { recursive: true, force: true });
// For discord_arch_electron
if (existsSync(join(appDir, "..", "app.orig.asar.unpacked"))) {
await rename(
Expand Down
19 changes: 15 additions & 4 deletions src/main/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
import { dirname, join } from "path";

import { statSync } from "fs";
import electron from "electron";
import { CONFIG_PATHS } from "src/util.mjs";
import type { RepluggedWebContents } from "../types";
import { getSetting } from "./ipc/settings";

const electronPath = require.resolve("electron");
const discordPath = join(dirname(require.main!.filename), "..", "app.orig.asar");
const discordPackage = require(join(discordPath, "package.json"));
require.main!.filename = join(discordPath, discordPackage.main);

// This is for backwards compatibility, to be removed later.
let discordPath = join(dirname(require.main!.filename), "..", "app.orig.asar");
try {
// If using older replugged file system
statSync(discordPath);
const discordPackage = require(join(discordPath, "package.json"));
require.main!.filename = join(discordPath, discordPackage.main);
} catch {
// If using newer replugged file system
discordPath = join(dirname(require.main!.filename), "app.orig");
const discordPackage = require(join(discordPath, "package.json"));
require.main!.filename = join(discordPath, "..", discordPackage.main);
}

Object.defineProperty(global, "appSettings", {
set: (v /* : typeof global.appSettings*/) => {
Expand Down
Loading