Skip to content

Commit

Permalink
Merge pull request #1334 from hydralauncher/refactor/optimize-rpc
Browse files Browse the repository at this point in the history
Refactor/optimize rpc
  • Loading branch information
JackEnx authored Dec 24, 2024
2 parents 89bfb51 + 90841bf commit 58a8937
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ out
ludusavi/
hydra-python-rpc/
aria2/
.python-version
1 change: 0 additions & 1 deletion .python-version

This file was deleted.

79 changes: 47 additions & 32 deletions src/main/services/process-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const gamesPlaytime = new Map<
interface ExecutableInfo {
name: string;
os: string;
exe: string;
}

interface GameExecutables {
Expand All @@ -30,47 +31,65 @@ interface GameExecutables {
const TICKS_TO_UPDATE_API = 120;
let currentTick = 1;

const gameExecutables = (
await axios
.get(
import.meta.env.MAIN_VITE_EXTERNAL_RESOURCES_URL +
"/game-executables.json"
)
.catch(() => {
return { data: {} };
})
).data as GameExecutables;
const isWindowsPlatform = process.platform === "win32";
const isLinuxPlatform = process.platform === "linux";

const getGameExecutables = async () => {
const gameExecutables = (
await axios
.get(
import.meta.env.MAIN_VITE_EXTERNAL_RESOURCES_URL +
"/game-executables.json"
)
.catch(() => {
return { data: {} };
})
).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 = (
processMap: Map<string, Set<string>>,
gameId: string
) => {
const executables = gameExecutables[gameId].filter((info) => {
if (process.platform === "linux" && info.os === "linux") return true;
return info.os === "win32";
});
const executables = gameExecutables[gameId];

for (const executable of executables) {
const exe = executable.name.slice(executable.name.lastIndexOf("/") + 1);

if (!exe) continue;

const pathSet = processMap.get(exe);
const pathSet = processMap.get(executable.exe);

if (pathSet) {
const executableName =
process.platform === "win32"
? executable.name.replace(/\//g, "\\")
: executable.name;

pathSet.forEach((path) => {
if (path.toLowerCase().endsWith(executableName)) {
if (path.toLowerCase().endsWith(executable.name)) {
gameRepository.update(
{ objectID: gameId, shop: "steam" },
{ executablePath: path }
);

if (process.platform === "linux") {
if (isLinuxPlatform) {
exec(commands.findWineDir, (err, out) => {
if (err) return;

Expand Down Expand Up @@ -105,7 +124,7 @@ const getSystemProcessMap = async () => {
map.set(key, currentSet.add(value));
});

if (process.platform === "linux") {
if (isLinuxPlatform) {
await new Promise((res) => {
exec(commands.findWineExecutables, (err, out) => {
if (err) {
Expand Down Expand Up @@ -152,7 +171,6 @@ export const watchProcesses = async () => {

for (const game of games) {
const executablePath = game.executablePath;

if (!executablePath) {
if (gameExecutables[game.objectID]) {
findGamePathByProcess(processMap, game.objectID);
Expand All @@ -161,10 +179,7 @@ export const watchProcesses = async () => {
}

const executable = executablePath
.slice(
executablePath.lastIndexOf(process.platform === "win32" ? "\\" : "/") +
1
)
.slice(executablePath.lastIndexOf(isWindowsPlatform ? "\\" : "/") + 1)
.toLowerCase();

const hasProcess = processMap.get(executable)?.has(executablePath);
Expand Down

0 comments on commit 58a8937

Please sign in to comment.