From 6a6e3de64b5e9991eebbd669893f910bedd9c380 Mon Sep 17 00:00:00 2001 From: Sqaaakoi Date: Thu, 4 Apr 2024 12:51:36 +1300 Subject: [PATCH] process: fix linux game detection --- src/process/index.js | 50 ++++++++++++++++++++++--------------- src/process/native/linux.js | 2 +- 2 files changed, 31 insertions(+), 21 deletions(-) diff --git a/src/process/index.js b/src/process/index.js index e4f1ba7..3083c7f 100644 --- a/src/process/index.js +++ b/src/process/index.js @@ -29,9 +29,13 @@ export default class ProcessServer { // log(`got processed in ${(performance.now() - startTime).toFixed(2)}ms`); - for (const [ pid, _path ] of processes) { + for (const [ pid, _path, args ] of processes) { const path = _path.toLowerCase().replaceAll('\\', '/'); - const toCompare = [ path.split('/').pop(), path.split('/').slice(-2).join('/') ]; + const toCompare = []; + const splitPath = path.split('/'); + for (let i = 1; i < splitPath.length; i++) { + toCompare.push(splitPath.slice(-i).join('/')); + } for (const p of toCompare.slice()) { // add more possible tweaked paths for less false negatives toCompare.push(p.replace('64', '')); // remove 64bit identifiers-ish @@ -40,7 +44,12 @@ export default class ProcessServer { } for (const { executables, id, name } of DetectableDB) { - if (executables?.some(x => !x.isLauncher && toCompare.some(y => x.name === y))) { + if (executables?.some(x => { + if (x.is_launcher) return false; + if (x.name[0] === '>' ? x.name.substring(1) !== toCompare[0] : !toCompare.some(y => x.name === y)) return false; + if (x.arguments) return args.join(" ").indexOf(x.arguments) > -1; + return true; + })) { names[id] = name; pids[id] = pid; @@ -48,23 +57,24 @@ export default class ProcessServer { if (!timestamps[id]) { log('detected game!', name); timestamps[id] = Date.now(); - - this.handlers.message({ - socketId: id - }, { - cmd: 'SET_ACTIVITY', - args: { - activity: { - application_id: id, - name, - timestamps: { - start: timestamps[id] - } - }, - pid - } - }); } + + // Resending this on evry scan is intentional, so that in the case that arRPC scans processes before Discord, existing activities will be sent + this.handlers.message({ + socketId: id + }, { + cmd: 'SET_ACTIVITY', + args: { + activity: { + application_id: id, + name, + timestamps: { + start: timestamps[id] + } + }, + pid + } + }); } } } @@ -73,7 +83,7 @@ export default class ProcessServer { if (!ids.includes(id)) { log('lost game!', names[id]); delete timestamps[id]; - + this.handlers.message({ socketId: id }, { diff --git a/src/process/native/linux.js b/src/process/native/linux.js index bb26580..ed45681 100644 --- a/src/process/native/linux.js +++ b/src/process/native/linux.js @@ -3,6 +3,6 @@ import { readdir, readFile } from "fs/promises"; export const getProcesses = async () => (await Promise.all( (await readdir("/proc")).map(pid => (+pid > 0) && readFile(`/proc/${pid}/cmdline`, 'utf8') - .then(path => [+pid, path.replaceAll('0', '')], () => 0) + .then(path => [+pid, path.split("\0")[0], path.split("\0").slice(1)], () => 0) ) )).filter(x => x); \ No newline at end of file