mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-03-09 15:40:26 +00:00
refactor: change game process set to map
This commit is contained in:
parent
ff917b7ecf
commit
b8b000d88c
1 changed files with 18 additions and 12 deletions
|
@ -14,18 +14,24 @@ export const gamesPlaytime = new Map<
|
||||||
const TICKS_TO_UPDATE_API = 120;
|
const TICKS_TO_UPDATE_API = 120;
|
||||||
let currentTick = 1;
|
let currentTick = 1;
|
||||||
|
|
||||||
const getSystemProcessSet = async () => {
|
const getSystemProcessMap = async () => {
|
||||||
const processes = await PythonInstance.getProcessList();
|
const processes = await PythonInstance.getProcessList();
|
||||||
|
|
||||||
if (process.platform === "linux")
|
const map = new Map<string, Set<string>>();
|
||||||
return new Set(processes.map((process) => process.name));
|
|
||||||
return new Set(processes.map((process) => process.exe));
|
processes.forEach((process) => {
|
||||||
|
const [key, value] = [process.name?.toLowerCase(), process.exe];
|
||||||
|
|
||||||
|
if (!key || !value) return;
|
||||||
|
|
||||||
|
map.set(key, (map.get(key) ?? new Set()).add(value));
|
||||||
|
});
|
||||||
|
|
||||||
|
return map;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getExecutable = (game: Game) => {
|
const getExecutable = (path: string) => {
|
||||||
if (process.platform === "linux")
|
return path.slice(path.lastIndexOf("/") + 1);
|
||||||
return game.executablePath?.split("/").at(-1);
|
|
||||||
return game.executablePath;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const watchProcesses = async () => {
|
export const watchProcesses = async () => {
|
||||||
|
@ -38,14 +44,14 @@ export const watchProcesses = async () => {
|
||||||
|
|
||||||
if (games.length === 0) return;
|
if (games.length === 0) return;
|
||||||
|
|
||||||
const processSet = await getSystemProcessSet();
|
const processMap = await getSystemProcessMap();
|
||||||
|
|
||||||
for (const game of games) {
|
for (const game of games) {
|
||||||
const executable = getExecutable(game);
|
if (!game.executablePath) continue;
|
||||||
|
|
||||||
if (!executable) continue;
|
const executable = getExecutable(game.executablePath);
|
||||||
|
|
||||||
const gameProcess = processSet.has(executable);
|
const gameProcess = processMap.get(executable)?.has(game.executablePath);
|
||||||
|
|
||||||
if (gameProcess) {
|
if (gameProcess) {
|
||||||
if (gamesPlaytime.has(game.id)) {
|
if (gamesPlaytime.has(game.id)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue