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

Auto relaunch #517

Open
wants to merge 32 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2ac01b9
added types
yofukashino Jul 16, 2023
edba415
added util functions
yofukashino Jul 16, 2023
ae7daf7
main changes in script
yofukashino Jul 16, 2023
a93391e
cspell fix
yofukashino Jul 16, 2023
c510c93
timeout
yofukashino Jul 16, 2023
1835ddf
added the function to injector
yofukashino Jul 16, 2023
8d03383
use new function
yofukashino Jul 16, 2023
fb64269
linux support added???
yofukashino Jul 18, 2023
aa55735
prettier is ***
yofukashino Jul 18, 2023
bfdf699
Merge branch 'main' into auto-relaunch
yofukashino Jul 19, 2023
5ad3e4b
Merge branch 'main' into auto-relaunch
yofukashino Aug 22, 2023
5a9f609
Merge branch 'main' into auto-relaunch
yofukashino Sep 24, 2023
5c2ddf4
requested changes
yofukashino Sep 24, 2023
c9aeec3
Merge branch 'main' into auto-relaunch
yofukashino Sep 30, 2023
1167e1c
Merge branch 'main' into auto-relaunch
yofukashino Oct 19, 2023
e2b2613
warnings
yofukashino Oct 19, 2023
971cad8
Merge branch 'main' into auto-relaunch
yofukashino Apr 5, 2024
3c1d139
Merge branch 'main' into auto-relaunch
yofukashino Apr 13, 2024
4e356ae
Merge branch 'main' into auto-relaunch
yofukashino Jul 6, 2024
365c37f
prettier and dev mode fix
yofukashino Jul 11, 2024
890561f
prettier
yofukashino Jul 11, 2024
63bc230
await
yofukashino Jul 11, 2024
ea52882
FRCIK KEYBOARD SHORTCUT
yofukashino Jul 11, 2024
f7f85aa
right place
yofukashino Jul 11, 2024
4da86b0
fix: find parent pid on windows and kill all processes on linux
FedeIlLeone Jul 12, 2024
f4c66f0
Merge branch 'main' into auto-relaunch
FedeIlLeone Oct 18, 2024
d586b36
Merge branch 'main' into auto-relaunch
yofukashino Oct 18, 2024
d65b9c9
macos support
yofukashino Oct 18, 2024
dab08db
refractor
yofukashino Oct 18, 2024
6e166ff
Merge branch 'main' into auto-relaunch
yofukashino Nov 11, 2024
ed3c4f4
Merge branch 'main' into auto-relaunch
yofukashino Nov 13, 2024
b0a0bbf
Merge branch 'main' into auto-relaunch
yofukashino Nov 26, 2024
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 @@ -30,6 +30,7 @@
"globstar",
"groupstart",
"HighlightJS",
"IMAGENAME",
"installdir",
"Jsonifiable",
"konami",
Expand Down
42 changes: 39 additions & 3 deletions scripts/inject/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import "./checks/elevate.mjs";
import "./checks/env.mjs";

import { join } from "path";
import { AnsiEscapes, getCommand } from "./util.mjs";
import {
AnsiEscapes,
PlatformNames,
getCommand,
getProcessInfoByName,
killProcessByPID,
openProcess,
} from "./util.mjs";
import { inject, uninject } from "./injector.mjs";

import * as darwin from "./platforms/darwin.mjs";
Expand All @@ -20,6 +27,7 @@ const platformModules = {

const exitCode = process.argv.includes("--no-exit-codes") ? 0 : 1;
const prod = process.argv.includes("--production");
const noRelaunch = process.argv.includes("--no-relaunch");
const processArgs = process.argv.filter((v) => !v.startsWith("-"));

if (!(process.platform in platformModules)) {
Expand Down Expand Up @@ -94,7 +102,20 @@ const run = async (cmd = processArgs[2], replug = false): Promise<void> => {

if (cmd === "inject") {
try {
result = await inject(platformModule, platform, prod);
if (noRelaunch) {
result = await inject(platformModule, platform, prod);
} else {
const processName = PlatformNames[platform].replace(" ", "");
const processInfo = getProcessInfoByName(processName)!;
await killProcessByPID(processInfo?.pid);
result = await inject(platformModule, platform, prod);
const appDir = await platformModule.getAppDir(platform);
openProcess(
join(appDir, "..", "..", "..", "Update"),
process.platform === "win32" ? ["--processStart", `${processName}.exe`] : [],
{ detached: true, stdio: "ignore" },
);
}
} catch (e) {
console.error(
`${AnsiEscapes.RED}An error occurred while trying to inject into Discord!${AnsiEscapes.RESET}`,
Expand All @@ -121,7 +142,22 @@ To plug into a different platform, use the following syntax: ${AnsiEscapes.BOLD}
}
} else if (cmd === "uninject") {
try {
result = await uninject(platformModule, platform);
if (noRelaunch) {
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
result = await uninject(platformModule, platform);
} else {
const processName = PlatformNames[platform].replace(" ", "");
const processInfo = getProcessInfoByName(processName)!;
await killProcessByPID(processInfo?.pid);
result = await uninject(platformModule, platform);
if (!replug) {
const appDir = await platformModule.getAppDir(platform);
openProcess(
join(appDir, "..", "..", "..", "Update"),
process.platform === "win32" ? ["--processStart", "Discord.exe"] : [],
{ detached: true, stdio: "ignore" },
);
}
}
} catch (e) {
console.error(
`${AnsiEscapes.RED}An error occurred while trying to uninject from Discord!${AnsiEscapes.RESET}`,
Expand Down
5 changes: 5 additions & 0 deletions scripts/inject/types.mts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export type DiscordPlatform = "stable" | "ptb" | "canary" | "dev";
export interface PlatformModule {
getAppDir: (platform: DiscordPlatform) => Promisable<string>;
}

export interface ProcessInfo {
pid: number;
cmd: string[];
}
56 changes: 55 additions & 1 deletion scripts/inject/util.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DiscordPlatform } from "./types.mjs";
import { SpawnOptions, execSync, spawn } from "child_process";
import { DiscordPlatform, ProcessInfo } from "./types.mjs";

export const AnsiEscapes = {
RESET: "\x1b[0m",
Expand Down Expand Up @@ -29,3 +30,56 @@ export const getCommand = ({
cmd += ` ${platform || `[${Object.keys(PlatformNames).join("|")}]`}`;
return cmd;
};

export const getProcessInfoByName = (processName: string): ProcessInfo | null => {
if (process.platform === "win32") {
const command = `tasklist /FI "IMAGENAME eq ${processName}.exe" /FO CSV`;
const output = execSync(command).toString().trim();

const lines = output.split("\r\n");
if (lines.length <= 2) {
return null;
}

const [_header, data] = lines.slice(0, 2);
const [name, pid] = data.split('","');

return { pid: Number(pid), cmd: name.substring(1).split(/\s+/) };
}
const command = `ps -eo pid,command | grep -E "(^|/)${processName}(\\s|$)" | grep -v grep`;
const output = execSync(command).toString().trim();

if (output.length === 0) {
return null;
}

const [pid, ...cmd] = output.split(/\s+/);

return { pid: Number(pid), cmd };
};

export const killCheckProcessExists = (pid: number): boolean => {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
};

export const killProcessByPID = (pid: number): Promise<void> => {
return new Promise((resolve) => {
if (!pid) resolve();
process.kill(pid, "SIGTERM");
const checkInterval = setInterval(() => {
yofukashino marked this conversation as resolved.
Show resolved Hide resolved
if (!killCheckProcessExists(pid)) {
clearInterval(checkInterval);
resolve();
}
}, 1000);
});
};

export const openProcess = (command: string, args: string[], options: SpawnOptions): void => {
spawn(command, args, options).unref();
};