mirror of
https://github.com/hydralauncher/hydra.git
synced 2026-01-20 01:33:56 +00:00
40 lines
1012 B
TypeScript
40 lines
1012 B
TypeScript
import path from "node:path";
|
|
|
|
import { gameRepository } from "@main/repository";
|
|
import { getProcesses } from "@main/helpers";
|
|
|
|
import { registerEvent } from "../register-event";
|
|
|
|
const closeGame = async (
|
|
_event: Electron.IpcMainInvokeEvent,
|
|
gameId: number
|
|
) => {
|
|
const processes = await getProcesses();
|
|
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)
|
|
);
|
|
|
|
const gameProcess = processes.find((runningProcess) => {
|
|
if (process.platform === "win32") {
|
|
return runningProcess.name === basename;
|
|
}
|
|
|
|
return [basename, basenameWithoutExtension].includes(runningProcess.name);
|
|
});
|
|
|
|
if (gameProcess) return process.kill(gameProcess.pid);
|
|
return false;
|
|
};
|
|
|
|
registerEvent("closeGame", closeGame);
|