From 7785d420214c6ba48e57babd0b70e86ead13ceb4 Mon Sep 17 00:00:00 2001 From: JackEnx Date: Tue, 12 Nov 2024 15:38:30 -0300 Subject: [PATCH 1/2] fix: linux game tracking and closed button --- .python-version | 1 + src/main/events/library/close-game.ts | 6 +++- src/main/services/download/types.ts | 1 + src/main/services/process-watcher.ts | 44 ++++++++++++++++++++------- torrent-client/main.py | 2 +- 5 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 .python-version diff --git a/.python-version b/.python-version new file mode 100644 index 00000000..94329086 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.9.20 diff --git a/src/main/events/library/close-game.ts b/src/main/events/library/close-game.ts index 9c431d06..48cb681b 100644 --- a/src/main/events/library/close-game.ts +++ b/src/main/events/library/close-game.ts @@ -24,7 +24,11 @@ const closeGame = async ( if (!game) return; const gameProcess = processes.find((runningProcess) => { - return runningProcess.exe === game.executablePath; + if (process.platform === "linux") { + return runningProcess.name === game.executablePath?.split("/").at(-1); + } else { + return runningProcess.exe === game.executablePath; + } }); if (gameProcess) { diff --git a/src/main/services/download/types.ts b/src/main/services/download/types.ts index fd8009a2..3ddaff39 100644 --- a/src/main/services/download/types.ts +++ b/src/main/services/download/types.ts @@ -35,4 +35,5 @@ export interface LibtorrentPayload { export interface ProcessPayload { exe: string; pid: number; + name: string; } diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 51b39bbe..23915355 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -25,21 +25,43 @@ export const watchProcesses = async () => { if (games.length === 0) return; const processes = await PythonInstance.getProcessList(); - const processSet = new Set(processes.map((process) => process.exe)); + if (process.platform === "linux") { + const processSet = new Set(processes.map((process) => process.name)); - for (const game of games) { - const executablePath = game.executablePath!; + for (const game of games) { + const executable = game.executablePath?.split("/").at(-1); - const gameProcess = processSet.has(executablePath); + if (!executable) continue; - if (gameProcess) { - if (gamesPlaytime.has(game.id)) { - onTickGame(game); - } else { - onOpenGame(game); + const gameProcess = processSet.has(executable); + + if (gameProcess) { + if (gamesPlaytime.has(game.id)) { + onTickGame(game); + } else { + onOpenGame(game); + } + } else if (gamesPlaytime.has(game.id)) { + onCloseGame(game); + } + } + } else { + const processSet = new Set(processes.map((process) => process.exe)); + + for (const game of games) { + const executablePath = game.executablePath!; + + const gameProcess = processSet.has(executablePath); + + if (gameProcess) { + if (gamesPlaytime.has(game.id)) { + onTickGame(game); + } else { + onOpenGame(game); + } + } else if (gamesPlaytime.has(game.id)) { + onCloseGame(game); } - } else if (gamesPlaytime.has(game.id)) { - onCloseGame(game); } } diff --git a/torrent-client/main.py b/torrent-client/main.py index d62150b8..0e65c564 100644 --- a/torrent-client/main.py +++ b/torrent-client/main.py @@ -60,7 +60,7 @@ class Handler(BaseHTTPRequestHandler): self.end_headers() return - process_list = [proc.info for proc in psutil.process_iter(['exe', 'pid', 'username'])] + process_list = [proc.info for proc in psutil.process_iter(['exe', 'pid', 'name'])] self.send_response(200) self.send_header("Content-type", "application/json") From 46c3b335481c3526306a684a3f5bfb6d033d17b5 Mon Sep 17 00:00:00 2001 From: JackEnx Date: Wed, 13 Nov 2024 11:54:01 -0300 Subject: [PATCH 2/2] refactor: remove process watcher duplicated code --- src/main/services/process-watcher.ts | 59 ++++++++++++---------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/services/process-watcher.ts b/src/main/services/process-watcher.ts index 23915355..fd9bb148 100644 --- a/src/main/services/process-watcher.ts +++ b/src/main/services/process-watcher.ts @@ -14,6 +14,20 @@ export const gamesPlaytime = new Map< const TICKS_TO_UPDATE_API = 120; let currentTick = 1; +const getSystemProcessSet = async () => { + const processes = await PythonInstance.getProcessList(); + + if (process.platform === "linux") + return new Set(processes.map((process) => process.name)); + return new Set(processes.map((process) => process.exe)); +}; + +const getExecutable = (game: Game) => { + if (process.platform === "linux") + return game.executablePath?.split("/").at(-1); + return game.executablePath; +}; + export const watchProcesses = async () => { const games = await gameRepository.find({ where: { @@ -23,45 +37,24 @@ export const watchProcesses = async () => { }); if (games.length === 0) return; - const processes = await PythonInstance.getProcessList(); - if (process.platform === "linux") { - const processSet = new Set(processes.map((process) => process.name)); + const processSet = await getSystemProcessSet(); - for (const game of games) { - const executable = game.executablePath?.split("/").at(-1); + for (const game of games) { + const executable = getExecutable(game); - if (!executable) continue; + if (!executable) continue; - const gameProcess = processSet.has(executable); + const gameProcess = processSet.has(executable); - if (gameProcess) { - if (gamesPlaytime.has(game.id)) { - onTickGame(game); - } else { - onOpenGame(game); - } - } else if (gamesPlaytime.has(game.id)) { - onCloseGame(game); - } - } - } else { - const processSet = new Set(processes.map((process) => process.exe)); - - for (const game of games) { - const executablePath = game.executablePath!; - - const gameProcess = processSet.has(executablePath); - - if (gameProcess) { - if (gamesPlaytime.has(game.id)) { - onTickGame(game); - } else { - onOpenGame(game); - } - } else if (gamesPlaytime.has(game.id)) { - onCloseGame(game); + if (gameProcess) { + if (gamesPlaytime.has(game.id)) { + onTickGame(game); + } else { + onOpenGame(game); } + } else if (gamesPlaytime.has(game.id)) { + onCloseGame(game); } }