refactor: rpc executable objects

This commit is contained in:
JackEnx 2024-12-24 13:48:50 -03:00
parent 89bfb517fb
commit 5564644378
3 changed files with 48 additions and 33 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ out
ludusavi/ ludusavi/
hydra-python-rpc/ hydra-python-rpc/
aria2/ aria2/
./python-version

View File

@ -1 +0,0 @@
3.9.20

View File

@ -21,6 +21,7 @@ export const gamesPlaytime = new Map<
interface ExecutableInfo { interface ExecutableInfo {
name: string; name: string;
os: string; os: string;
exe: string;
} }
interface GameExecutables { interface GameExecutables {
@ -30,6 +31,10 @@ interface GameExecutables {
const TICKS_TO_UPDATE_API = 120; const TICKS_TO_UPDATE_API = 120;
let currentTick = 1; let currentTick = 1;
const isWindowsPlatform = process.platform === "win32";
const isLinuxPlatform = process.platform === "linux";
const getGameExecutables = async () => {
const gameExecutables = ( const gameExecutables = (
await axios await axios
.get( .get(
@ -41,36 +46,50 @@ const gameExecutables = (
}) })
).data as GameExecutables; ).data as GameExecutables;
Object.keys(gameExecutables).forEach((key) => {
gameExecutables[key] = gameExecutables[key]
.filter((executable) => {
if (isWindowsPlatform) {
return executable.os === "win32";
} else if (isLinuxPlatform) {
return executable.os === "linux" || executable.os === "win32";
}
return false;
})
.map((executable) => {
return {
name: isWindowsPlatform
? executable.name.replace(/\//g, "\\")
: executable.name,
os: executable.os,
exe: executable.name.slice(executable.name.lastIndexOf("/") + 1),
};
});
});
return gameExecutables;
};
const gameExecutables = await getGameExecutables();
const findGamePathByProcess = ( const findGamePathByProcess = (
processMap: Map<string, Set<string>>, processMap: Map<string, Set<string>>,
gameId: string gameId: string
) => { ) => {
const executables = gameExecutables[gameId].filter((info) => { const executables = gameExecutables[gameId];
if (process.platform === "linux" && info.os === "linux") return true;
return info.os === "win32";
});
for (const executable of executables) { for (const executable of executables) {
const exe = executable.name.slice(executable.name.lastIndexOf("/") + 1); const pathSet = processMap.get(executable.exe);
if (!exe) continue;
const pathSet = processMap.get(exe);
if (pathSet) { if (pathSet) {
const executableName =
process.platform === "win32"
? executable.name.replace(/\//g, "\\")
: executable.name;
pathSet.forEach((path) => { pathSet.forEach((path) => {
if (path.toLowerCase().endsWith(executableName)) { if (path.toLowerCase().endsWith(executable.name)) {
gameRepository.update( gameRepository.update(
{ objectID: gameId, shop: "steam" }, { objectID: gameId, shop: "steam" },
{ executablePath: path } { executablePath: path }
); );
if (process.platform === "linux") { if (isLinuxPlatform) {
exec(commands.findWineDir, (err, out) => { exec(commands.findWineDir, (err, out) => {
if (err) return; if (err) return;
@ -105,7 +124,7 @@ const getSystemProcessMap = async () => {
map.set(key, currentSet.add(value)); map.set(key, currentSet.add(value));
}); });
if (process.platform === "linux") { if (isLinuxPlatform) {
await new Promise((res) => { await new Promise((res) => {
exec(commands.findWineExecutables, (err, out) => { exec(commands.findWineExecutables, (err, out) => {
if (err) { if (err) {
@ -152,7 +171,6 @@ export const watchProcesses = async () => {
for (const game of games) { for (const game of games) {
const executablePath = game.executablePath; const executablePath = game.executablePath;
if (!executablePath) { if (!executablePath) {
if (gameExecutables[game.objectID]) { if (gameExecutables[game.objectID]) {
findGamePathByProcess(processMap, game.objectID); findGamePathByProcess(processMap, game.objectID);
@ -161,10 +179,7 @@ export const watchProcesses = async () => {
} }
const executable = executablePath const executable = executablePath
.slice( .slice(executablePath.lastIndexOf(isWindowsPlatform ? "\\" : "/") + 1)
executablePath.lastIndexOf(process.platform === "win32" ? "\\" : "/") +
1
)
.toLowerCase(); .toLowerCase();
const hasProcess = processMap.get(executable)?.has(executablePath); const hasProcess = processMap.get(executable)?.has(executablePath);