Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Hydra authored Apr 27, 2024
2 parents 9908280 + 592ccb5 commit 900e2b2
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 93 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
postinstall.js
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "eslint .",
"format": "prettier . --write"
"format": "prettier . --write",
"postinstall": "node ./postinstall.js"
},
"devDependencies": {
"@electron-forge/cli": "^7.3.0",
Expand Down Expand Up @@ -96,7 +97,6 @@
"react-redux": "^9.1.0",
"react-router-dom": "^6.22.3",
"sqlite3": "^5.1.7",
"tasklist": "^5.0.0",
"tough-cookie": "^4.1.3",
"typeorm": "^0.3.20",
"update-electron-app": "^3.0.0",
Expand Down
9 changes: 9 additions & 0 deletions postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const fs = require("fs")

if (process.platform === "win32"){
if (!fs.existsSync("resources/dist")) {
fs.mkdirSync("resources/dist")
}

fs.copyFileSync("node_modules/ps-list/vendor/fastlist-0.3.0-x64.exe", "resources/dist/fastlist.exe")
}
13 changes: 0 additions & 13 deletions src/declaration.d.ts

This file was deleted.

35 changes: 28 additions & 7 deletions src/main/helpers/ps.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
import psList from "ps-list";
import { tasklist } from "tasklist";
import path from "node:path";
import childProcess from "node:child_process";
import { promisify } from "node:util";
import { app } from "electron";

const TEN_MEGABYTES = 1000 * 1000 * 10;
const execFile = promisify(childProcess.execFile);

export const getProcesses = async () => {
if (process.platform === "win32") {
return tasklist().then((tasks) =>
tasks.map((task) => ({ ...task, name: task.imageName }))
);
}
if (process.platform == "win32") {
const binaryPath = app.isPackaged
? path.join(process.resourcesPath, "dist", "fastlist.exe")
: path.join(__dirname, "..", "..", "resources", "dist", "fastlist.exe");

return psList();
const { stdout } = await execFile(binaryPath, {
maxBuffer: TEN_MEGABYTES,
windowsHide: true,
});

return stdout
.trim()
.split("\r\n")
.map((line) => line.split("\t"))
.map(([pid, ppid, name]) => ({
pid: Number.parseInt(pid, 10),
ppid: Number.parseInt(ppid, 10),
name,
}));
} else {
return psList();
}
};
34 changes: 14 additions & 20 deletions src/main/services/process-watcher.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,40 @@
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 () => {
const sleepTime = 100;
const sleepTime = 300;
const gamesPlaytime = new Map<number, number>();

// eslint-disable-next-line no-constant-condition
while (true) {
await sleep(sleepTime);

const games = await gameRepository.find({
where: {
executablePath: Not(IsNull()),
},
});

if (games.length == 0) {
continue;
}

const processes = await getProcesses();

for (const game of games) {
const gameProcess = processes.find((runningProcess) => {
const basename = path.win32.basename(game.executablePath);
const basenameWithoutExtension = path.win32.basename(
game.executablePath,
path.extname(game.executablePath)
);
const basename = path.win32.basename(game.executablePath);
const basenameWithoutExtension = path.win32.basename(
game.executablePath,
path.extname(game.executablePath)
);

const gameProcess = processes.find((runningProcess) => {
if (process.platform === "win32") {
return runningProcess.name === basename;
}
Expand All @@ -55,26 +60,15 @@ export const startProcessWatcher = async () => {
gameRepository.update(game.id, {
lastTimePlayed: new Date().toUTCString(),
});

gamesPlaytime.set(game.id, performance.now());
await sleep(sleepTime);
continue;
}

gamesPlaytime.set(game.id, performance.now());

await sleep(sleepTime);
continue;
}

if (gamesPlaytime.has(game.id)) {
} else if (gamesPlaytime.has(game.id)) {
gamesPlaytime.delete(game.id);
if (WindowManager.mainWindow) {
WindowManager.mainWindow.webContents.send("on-game-close", game.id);
}
}

await sleep(sleepTime);
}
}
};
52 changes: 1 addition & 51 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4611,31 +4611,6 @@ csstype@^3.0.2, csstype@^3.0.7:
resolved "https://registry.npmjs.org/csstype/-/csstype-3.1.3.tgz"
integrity sha512-M1uQkMl8rQK/szD0LNhtqxIPLpimGm8sOBwU7lLnCpSbTyY3yeU1Vc7l4KT5zT4s/yOxHH5O7tIuuLOCnLADRw==

csv-generate@^3.4.3:
version "3.4.3"
resolved "https://registry.npmjs.org/csv-generate/-/csv-generate-3.4.3.tgz"
integrity sha512-w/T+rqR0vwvHqWs/1ZyMDWtHHSJaN06klRqJXBEpDJaM/+dZkso0OKh1VcuuYvK3XM53KysVNq8Ko/epCK8wOw==

csv-parse@^4.16.3:
version "4.16.3"
resolved "https://registry.npmjs.org/csv-parse/-/csv-parse-4.16.3.tgz"
integrity sha512-cO1I/zmz4w2dcKHVvpCr7JVRu8/FymG5OEpmvsZYlccYolPBLoVGKUHgNoc4ZGkFeFlWGEDmMyBM+TTqRdW/wg==

csv-stringify@^5.6.5:
version "5.6.5"
resolved "https://registry.npmjs.org/csv-stringify/-/csv-stringify-5.6.5.tgz"
integrity sha512-PjiQ659aQ+fUTQqSrd1XEDnOr52jh30RBurfzkscaE2tPaFsDH5wOAHJiw8XAHphRknCwMUE9KRayc4K/NbO8A==

csv@^5.5.0:
version "5.5.3"
resolved "https://registry.npmjs.org/csv/-/csv-5.5.3.tgz"
integrity sha512-QTaY0XjjhTQOdguARF0lGKm5/mEq9PD9/VhZZegHDIBq2tQwgNpHc3dneD4mGo2iJs+fTKv5Bp0fZ+BRuY3Z0g==
dependencies:
csv-generate "^3.4.3"
csv-parse "^4.16.3"
csv-stringify "^5.6.5"
stream-transform "^2.1.3"

[email protected]:
version "1.0.3"
resolved "https://registry.npmjs.org/cycle/-/cycle-1.0.3.tgz"
Expand Down Expand Up @@ -7936,11 +7911,6 @@ minizlib@^2.0.0, minizlib@^2.1.1, minizlib@^2.1.2:
minipass "^3.0.0"
yallist "^4.0.0"

mixme@^0.5.1:
version "0.5.10"
resolved "https://registry.npmjs.org/mixme/-/mixme-0.5.10.tgz"
integrity sha512-5H76ANWinB1H3twpJ6JY8uvAtpmFvHNArpilJAjXRKXSDDLPIMoZArw5SH0q9z+lLs8IrMw7Q2VWpWimFKFT1Q==

mkdirp-classic@^0.5.2, mkdirp-classic@^0.5.3:
version "0.5.3"
resolved "https://registry.npmjs.org/mkdirp-classic/-/mkdirp-classic-0.5.3.tgz"
Expand Down Expand Up @@ -8866,7 +8836,7 @@ proxy-from-env@^1.1.0:

ps-list@^8.1.1:
version "8.1.1"
resolved "https://registry.npmjs.org/ps-list/-/ps-list-8.1.1.tgz"
resolved "https://registry.yarnpkg.com/ps-list/-/ps-list-8.1.1.tgz#9ff1952b26a9a07fcc05270407e60544237ae581"
integrity sha512-OPS9kEJYVmiO48u/B9qneqhkMvgCxT+Tm28VCEJpheTpl8cJ0ffZRRNgS5mrQRTrX5yRTpaJ+hRDeefXYmmorQ==

psl@^1.1.33:
Expand Down Expand Up @@ -9463,11 +9433,6 @@ schema-utils@^4.0.0:
ajv-formats "^2.1.1"
ajv-keywords "^5.1.0"

sec@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/sec/-/sec-2.0.0.tgz"
integrity sha512-uq35HWa7mG6YyojrduMXjF8UhOySEf3X0V1uMpSOBYUF09xAMnJaKNSmWXeE3mN7NfJTpNUkmGa6nIpEBMN8Xw==

select-hose@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/select-hose/-/select-hose-2.0.0.tgz"
Expand Down Expand Up @@ -9882,13 +9847,6 @@ [email protected]:
resolved "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz"
integrity sha512-OpZ3zP+jT1PI7I8nemJX4AKmAX070ZkYPVWV/AaKTJl+tXCTGyVdC1a4SL8RUQYEwk/f34ZX8UTykN68FwrqAA==

stream-transform@^2.1.3:
version "2.1.3"
resolved "https://registry.npmjs.org/stream-transform/-/stream-transform-2.1.3.tgz"
integrity sha512-9GHUiM5hMiCi6Y03jD2ARC1ettBXkQBoQAe7nJsPknnI0ow10aXjTnew8QtYQmLjzn974BnmWEAJgCY6ZP1DeQ==
dependencies:
mixme "^0.5.1"

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
Expand Down Expand Up @@ -10144,14 +10102,6 @@ tar@^6.0.2, tar@^6.0.5, tar@^6.1.11, tar@^6.1.2:
mkdirp "^1.0.3"
yallist "^4.0.0"

tasklist@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/tasklist/-/tasklist-5.0.0.tgz"
integrity sha512-qPB4J6pseXRqdxAFT1GhlvDPv4FHxWkXs8QVYQWIqusGwn7UXVKOoYu09DZuYWe1K7T5iusHfSoKrv8k9+RfxA==
dependencies:
csv "^5.5.0"
sec "^2.0.0"

temp-dir@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/temp-dir/-/temp-dir-2.0.0.tgz"
Expand Down

0 comments on commit 900e2b2

Please sign in to comment.