Skip to content

Commit dfea808

Browse files
committed
process: fix game detection for many games
it's still slow
1 parent e439b27 commit dfea808

File tree

1 file changed

+88
-18
lines changed

1 file changed

+88
-18
lines changed

src/process/index.js

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ const DetectableDB = JSON.parse(fs.readFileSync(join(__dirname, 'detectable.json
1111
import * as Natives from './native/index.js';
1212
const Native = Natives[process.platform];
1313

14-
function basename(str) {
15-
return str.substr(str.lastIndexOf('/') + 1);
16-
}
17-
1814
// https://stackoverflow.com/a/56641259
1915
/**
2016
* Replaces all occurrences of words in a sentence with new words.
@@ -38,6 +34,16 @@ const bitness_suffixes = {
3834
'64': '',
3935
}
4036

37+
String.prototype.replaceArray = function(find, replace) {
38+
var replaceString = this;
39+
var regex;
40+
for (var i = 0; i < find.length; i++) {
41+
regex = new RegExp(find[i], "g");
42+
replaceString = replaceString.replace(regex, replace[i]);
43+
}
44+
return replaceString;
45+
};
46+
4147
const timestamps = {}, names = {}, pids = {};
4248
export default class ProcessServer {
4349
constructor(handlers) {
@@ -60,27 +66,91 @@ export default class ProcessServer {
6066

6167
// log(`got processes list in ${(performance.now() - startTime).toFixed(2)}ms`);
6268

63-
for (const [ pid, _path, args, _cwdPath = '' ] of processes) {
64-
if (_path.length < 1) continue;
65-
const path = _path.toLowerCase().replaceAll('\\', '/');
69+
for (const [pid, _path, args, _cwdPath = ''] of processes) {
70+
if (pid === 1) continue // init system
71+
if (_path.length < 1) continue; // process has no name, i.e. kernel thread
72+
if (_path.startsWith('/proc')) continue; // internal *nix stuff
73+
if (_path.startsWith('/usr/lib/')) continue; // internal *nix stuff
74+
if (_path.includes('systemd')) continue;
6675
const cwdPath = _cwdPath.toLowerCase().replaceAll('\\', '/');
67-
const toCompare = [];
76+
const path = _path.toLowerCase().replaceAll('\\', '/');
77+
if (path.startsWith('c:/windows')) continue // system processes (wine)
78+
if (_path.includes('webhelper')) continue; // CEF Processes
6879

69-
let newPath = basename(path);
80+
const toCompare = [];
81+
let newPath
7082
if (path.includes(' --')) {
7183
newPath = path.split(' --')[0];
7284
}
85+
else
86+
{
87+
newPath = path;
88+
}
89+
newPath = newPath.substr(newPath.lastIndexOf('/') + 1);
90+
91+
// log(`performance checkpoint: ${(performance.now() - startTime).toFixed(2)}ms`);
92+
7393
toCompare.push(newPath);
74-
replaceAll(toCompare,bitness_suffixes);
94+
if (path.includes('.exe')) {
95+
const part2 = path.split('/').slice(-2).join('/');
96+
toCompare.push(part2);
97+
replaceAll(toCompare, bitness_suffixes);
98+
}
7599

100+
// TODO: Convert into an inline function similar to findInObjArray
101+
// TODO: Don't try to match the running executable more than once
76102
for (const { executables, id, name } of DetectableDB) {
77-
if (executables?.some(x => {
78-
if (x.is_launcher) return false;
79-
if (toCompare.some(y => y.includes('.exe') && (x.name.includes('game/'+y) || x.name.includes('games/'+y)))) return true;
80-
if (x.name[0] === '>' ? x.name.substring(1) !== toCompare[0] : !toCompare.some(y => x.name === y || `${cwdPath}/${y}`.includes(`/${x.name}`))) return false;
81-
if (args && x.arguments) return args.join(" ").indexOf(x.arguments) > -1;
82-
return true;
83-
})) {
103+
if (
104+
executables?.some((known_exe) => {
105+
if (known_exe.is_launcher) return false;
106+
if (known_exe.name[0] === '>') {
107+
if (known_exe.name.substring(1) === toCompare[0]) {
108+
return true;
109+
}
110+
} else {
111+
if (
112+
toCompare.some((running) => {
113+
// if (running === 'exe') {
114+
// throw new Error("Tried to match 'exe', this usually indicates a broken filter!")
115+
// return false;
116+
// }
117+
// explicit match first
118+
if (known_exe.name === running) {
119+
// console.log(`Got explicit match: ${known_exe.name} <==> ${running}`)
120+
return true;
121+
}
122+
// Try comparing against an exe.suffixed version (Linux native games and such)
123+
if (known_exe.name === running+'.exe') {
124+
return true
125+
}
126+
// Try comparing against an exe-less version (mistake in database)
127+
if (known_exe.name === running.replace('.exe','')) {
128+
return true
129+
}
130+
if (`${cwdPath}/${running}`.includes(`/${known_exe.name}`)
131+
) {
132+
console.log('hiiiiiiiiiiiiiiiii ');
133+
return false;
134+
}
135+
if (
136+
running.includes('zenlesszonezero') &&
137+
known_exe.name.includes('zenlesszonezero')
138+
) {
139+
// log(
140+
// `WARNING: Failed to match known problematic but running game '${known_exe.name}' via '${running}\n`,
141+
// 'The database needs to be fixed to make the following match succeed:\n',
142+
// `Running ==> [${running}] <==> [${known_exe.name}] <== Database`,
143+
// );
144+
return true
145+
}
146+
})
147+
) {
148+
return true;
149+
}
150+
}
151+
if (args && known_exe.arguments) return args.join(" ").indexOf(known_exe.arguments) > -1;
152+
})
153+
) {
84154
names[id] = name;
85155
pids[id] = pid;
86156

@@ -128,6 +198,6 @@ export default class ProcessServer {
128198
}
129199

130200
// log(`finished scan in ${(performance.now() - startTime).toFixed(2)}ms`);
131-
// process.stdout.write(`\r${' '.repeat(100)}\r[${rgb(88, 101, 242, 'arRPC')} > ${rgb(237, 66, 69, 'process')}] scanned (took ${(performance.now() - startTime).toFixed(2)}ms)\n`);
201+
132202
}
133203
}

0 commit comments

Comments
 (0)