hydra/src/main/services/process-watcher.ts

63 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-21 08:26:29 +03:00
import path from "node:path";
import { IsNull, Not } from "typeorm";
import { gameRepository } from "@main/repository";
import { getProcesses } from "@main/helpers";
import { WindowManager } from "./window-manager";
const gamesPlaytime = new Map<number, number>();
export const watchProcesses = async () => {
const games = await gameRepository.find({
where: {
executablePath: Not(IsNull()),
isDeleted: false,
},
});
if (games.length === 0) return;
const processes = await getProcesses();
for (const game of games) {
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;
}
2024-04-21 08:26:29 +03:00
return [basename, basenameWithoutExtension].includes(runningProcess.name);
2024-04-21 08:26:29 +03:00
});
if (gameProcess) {
if (gamesPlaytime.has(game.id)) {
const zero = gamesPlaytime.get(game.id) ?? 0;
const delta = performance.now() - zero;
2024-04-21 08:26:29 +03:00
if (WindowManager.mainWindow) {
WindowManager.mainWindow.webContents.send("on-playtime", game.id);
2024-04-21 08:26:29 +03:00
}
await gameRepository.update(game.id, {
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
lastTimePlayed: new Date().toUTCString(),
});
}
2024-04-21 08:26:29 +03:00
gamesPlaytime.set(game.id, performance.now());
} else if (gamesPlaytime.has(game.id)) {
gamesPlaytime.delete(game.id);
2024-04-21 08:26:29 +03:00
if (WindowManager.mainWindow) {
WindowManager.mainWindow.webContents.send("on-game-close", game.id);
2024-04-21 08:26:29 +03:00
}
}
}
};