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 sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
|
|
|
|
export const startProcessWatcher = async () => {
|
2024-05-03 15:07:05 +03:00
|
|
|
const sleepTime = 500;
|
2024-04-21 08:26:29 +03:00
|
|
|
const gamesPlaytime = new Map<number, number>();
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-constant-condition
|
|
|
|
while (true) {
|
|
|
|
const games = await gameRepository.find({
|
|
|
|
where: {
|
|
|
|
executablePath: Not(IsNull()),
|
2024-05-05 21:18:48 +03:00
|
|
|
isDeleted: false,
|
2024-04-21 08:26:29 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-05-02 01:11:05 +03:00
|
|
|
if (games.length === 0) {
|
2024-05-04 01:00:53 +03:00
|
|
|
await sleep(sleepTime);
|
2024-04-24 03:10:15 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-04-21 08:26:29 +03:00
|
|
|
const processes = await getProcesses();
|
|
|
|
|
|
|
|
for (const game of games) {
|
2024-04-28 21:21:14 +03:00
|
|
|
const executablePath = game.executablePath!;
|
|
|
|
const basename = path.win32.basename(executablePath);
|
2024-04-24 03:44:09 +03:00
|
|
|
const basenameWithoutExtension = path.win32.basename(
|
2024-04-28 21:21:14 +03:00
|
|
|
executablePath,
|
|
|
|
path.extname(executablePath)
|
2024-04-24 03:44:09 +03:00
|
|
|
);
|
2024-04-21 08:26:29 +03:00
|
|
|
|
2024-04-24 03:42:35 +03:00
|
|
|
const gameProcess = processes.find((runningProcess) => {
|
2024-04-21 08:26:29 +03:00
|
|
|
if (process.platform === "win32") {
|
|
|
|
return runningProcess.name === basename;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [basename, basenameWithoutExtension].includes(
|
|
|
|
runningProcess.name
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (gameProcess) {
|
|
|
|
if (gamesPlaytime.has(game.id)) {
|
2024-04-28 21:21:14 +03:00
|
|
|
const zero = gamesPlaytime.get(game.id) ?? 0;
|
2024-04-21 08:26:29 +03:00
|
|
|
const delta = performance.now() - zero;
|
|
|
|
|
|
|
|
if (WindowManager.mainWindow) {
|
|
|
|
WindowManager.mainWindow.webContents.send("on-playtime", game.id);
|
|
|
|
}
|
|
|
|
|
|
|
|
await gameRepository.update(game.id, {
|
|
|
|
playTimeInMilliseconds: game.playTimeInMilliseconds + delta,
|
|
|
|
});
|
|
|
|
|
|
|
|
gameRepository.update(game.id, {
|
|
|
|
lastTimePlayed: new Date().toUTCString(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
gamesPlaytime.set(game.id, performance.now());
|
2024-04-24 04:21:07 +03:00
|
|
|
} else if (gamesPlaytime.has(game.id)) {
|
2024-04-21 08:26:29 +03:00
|
|
|
gamesPlaytime.delete(game.id);
|
|
|
|
if (WindowManager.mainWindow) {
|
|
|
|
WindowManager.mainWindow.webContents.send("on-game-close", game.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-02 01:11:05 +03:00
|
|
|
|
|
|
|
await sleep(sleepTime);
|
2024-04-21 08:26:29 +03:00
|
|
|
}
|
|
|
|
};
|