mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
feat: remove pslist and use sudo-prompt to close game if needed
This commit is contained in:
parent
0f5db4f34e
commit
1397e3932d
14 changed files with 51 additions and 114 deletions
|
@ -1,39 +1,45 @@
|
|||
import path from "node:path";
|
||||
|
||||
import { gameRepository } from "@main/repository";
|
||||
import { getProcesses } from "@main/helpers";
|
||||
|
||||
import { registerEvent } from "../register-event";
|
||||
import { RPCManager, logger } from "@main/services";
|
||||
import sudo from "sudo-prompt";
|
||||
import { app } from "electron";
|
||||
|
||||
const getKillCommand = (pid: number) => {
|
||||
if (process.platform == "win32") {
|
||||
return `taskkill /PID ${pid}`;
|
||||
}
|
||||
|
||||
return `kill -9 ${pid}`;
|
||||
};
|
||||
|
||||
const closeGame = async (
|
||||
_event: Electron.IpcMainInvokeEvent,
|
||||
gameId: number
|
||||
) => {
|
||||
const processes = await getProcesses();
|
||||
const processes = await RPCManager.getProcessList();
|
||||
const game = await gameRepository.findOne({
|
||||
where: { id: gameId, isDeleted: false },
|
||||
});
|
||||
|
||||
if (!game) return false;
|
||||
|
||||
const executablePath = game.executablePath!;
|
||||
|
||||
const basename = path.win32.basename(executablePath);
|
||||
const basenameWithoutExtension = path.win32.basename(
|
||||
executablePath,
|
||||
path.extname(executablePath)
|
||||
);
|
||||
if (!game) return;
|
||||
|
||||
const gameProcess = processes.find((runningProcess) => {
|
||||
if (process.platform === "win32") {
|
||||
return runningProcess.name === basename;
|
||||
}
|
||||
|
||||
return [basename, basenameWithoutExtension].includes(runningProcess.name);
|
||||
return runningProcess.exe === game.executablePath;
|
||||
});
|
||||
|
||||
if (gameProcess) return process.kill(gameProcess.pid);
|
||||
return false;
|
||||
if (gameProcess) {
|
||||
try {
|
||||
process.kill(gameProcess.pid);
|
||||
} catch (err) {
|
||||
sudo.exec(
|
||||
getKillCommand(gameProcess.pid),
|
||||
{ name: app.getName() },
|
||||
(error, _stdout, _stderr) => {
|
||||
logger.error(error);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
registerEvent("closeGame", closeGame);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue