mirror of
				https://github.com/hydralauncher/hydra.git
				synced 2025-03-09 15:40:26 +00:00 
			
		
		
		
	feature: linux game observe
This commit is contained in:
		
							parent
							
								
									efcf778c95
								
							
						
					
					
						commit
						311b011ec6
					
				
					 1 changed files with 48 additions and 10 deletions
				
			
		| 
						 | 
					@ -5,6 +5,7 @@ import type { GameRunning } from "@types";
 | 
				
			||||||
import { PythonInstance } from "./download";
 | 
					import { PythonInstance } from "./download";
 | 
				
			||||||
import { Game } from "@main/entity";
 | 
					import { Game } from "@main/entity";
 | 
				
			||||||
import axios from "axios";
 | 
					import axios from "axios";
 | 
				
			||||||
 | 
					import { exec } from "child_process";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const gamesPlaytime = new Map<
 | 
					export const gamesPlaytime = new Map<
 | 
				
			||||||
  number,
 | 
					  number,
 | 
				
			||||||
| 
						 | 
					@ -81,6 +82,18 @@ const getSystemProcessMap = async () => {
 | 
				
			||||||
  return map;
 | 
					  return map;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const observeGameProcess = (hasProcess: boolean, game: Game) => {
 | 
				
			||||||
 | 
					  if (hasProcess) {
 | 
				
			||||||
 | 
					    if (gamesPlaytime.has(game.id)) {
 | 
				
			||||||
 | 
					      onTickGame(game);
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      onOpenGame(game);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  } else if (gamesPlaytime.has(game.id)) {
 | 
				
			||||||
 | 
					    onCloseGame(game);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const watchProcesses = async () => {
 | 
					export const watchProcesses = async () => {
 | 
				
			||||||
  const games = await gameRepository.find({
 | 
					  const games = await gameRepository.find({
 | 
				
			||||||
    where: {
 | 
					    where: {
 | 
				
			||||||
| 
						 | 
					@ -102,20 +115,45 @@ export const watchProcesses = async () => {
 | 
				
			||||||
      continue;
 | 
					      continue;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const executable = executablePath.slice(
 | 
					    const executable = executablePath
 | 
				
			||||||
      executablePath.lastIndexOf(process.platform === "win32" ? "\\" : "/") + 1
 | 
					      .slice(
 | 
				
			||||||
    );
 | 
					        executablePath.lastIndexOf(process.platform === "win32" ? "\\" : "/") +
 | 
				
			||||||
 | 
					          1
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					      .toLowerCase();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const gameProcess = processMap.get(executable)?.has(executablePath);
 | 
					    const processSet = processMap.get(executable);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (gameProcess) {
 | 
					    if (!processSet) continue;
 | 
				
			||||||
      if (gamesPlaytime.has(game.id)) {
 | 
					
 | 
				
			||||||
        onTickGame(game);
 | 
					    if (process.platform === "win32") {
 | 
				
			||||||
 | 
					      const hasProcess = processSet.has(executablePath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      observeGameProcess(hasProcess, game);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (process.platform === "linux") {
 | 
				
			||||||
 | 
					      if (executable.endsWith(".exe")) {
 | 
				
			||||||
 | 
					        exec(
 | 
				
			||||||
 | 
					          `lsof -c wine 2>/dev/null | grep -i ${executable} | awk \'{for(i=9;i<=NF;i++) printf "%s ", $i; print ""}\'`,
 | 
				
			||||||
 | 
					          (err, out) => {
 | 
				
			||||||
 | 
					            if (err) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const pathSet = new Set(
 | 
				
			||||||
 | 
					              out
 | 
				
			||||||
 | 
					                .trim()
 | 
				
			||||||
 | 
					                .split("\n")
 | 
				
			||||||
 | 
					                .map((path) => path.trim())
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const hasProcess = pathSet.has(executablePath!);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            observeGameProcess(hasProcess, game);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        onOpenGame(game);
 | 
					        //TODO: linux case
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    } else if (gamesPlaytime.has(game.id)) {
 | 
					 | 
				
			||||||
      onCloseGame(game);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue