|
| 1 | +import sys |
| 2 | +if sys.platform == 'win32': |
| 3 | + import winreg |
| 4 | + |
| 5 | +import os |
| 6 | +import logging as log |
| 7 | +import asyncio |
| 8 | +from consts import REGISTRY_LAUNCHER_PATH, PARADOX_LAUNCHER_EXE |
| 9 | +from dataclasses import dataclass |
| 10 | +from galaxy.proc_tools import process_iter, ProcessInfo |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class RunningGame: |
| 14 | + name: str |
| 15 | + process: ProcessInfo |
| 16 | + |
| 17 | + |
| 18 | +class LocalClient(object): |
| 19 | + def __init__(self): |
| 20 | + self._local_client_path = None |
| 21 | + self._local_client_exe = None |
| 22 | + self._local_games_path = None |
| 23 | + |
| 24 | + |
| 25 | + @property |
| 26 | + def installed(self): |
| 27 | + if self._local_client_exe and os.access(self._local_client_exe, os.F_OK): |
| 28 | + return True |
| 29 | + else: |
| 30 | + self.refresh_local_client_state() |
| 31 | + return self._local_client_exe and os.access(self._local_client_exe, os.F_OK) |
| 32 | + |
| 33 | + @property |
| 34 | + def local_client_exe(self): |
| 35 | + if self.installed: |
| 36 | + return self._local_client_exe |
| 37 | + |
| 38 | + @property |
| 39 | + def local_client_path(self): |
| 40 | + if self.installed: |
| 41 | + return self._local_client_path |
| 42 | + |
| 43 | + @property |
| 44 | + def bootstraper_exe(self): |
| 45 | + if self.installed: |
| 46 | + paradox_root = self._local_client_path[:-len('\\launcher')] |
| 47 | + bootstrapper = os.path.join(paradox_root, 'bootstrapper') |
| 48 | + return os.path.join(bootstrapper,'Bootstrapper.exe') |
| 49 | + |
| 50 | + @property |
| 51 | + def games_path(self): |
| 52 | + if self.installed: |
| 53 | + if self._local_games_path: |
| 54 | + return self._local_games_path |
| 55 | + else: |
| 56 | + paradox_root = self._local_client_path[:-len('\\launcher')] |
| 57 | + paradox_games = os.path.join(paradox_root, 'games') |
| 58 | + if not os.access(paradox_games, os.F_OK): |
| 59 | + return None |
| 60 | + self._local_games_path = paradox_games |
| 61 | + return paradox_games |
| 62 | + |
| 63 | + def refresh_local_client_state(self): |
| 64 | + if sys.platform == 'win32': |
| 65 | + try: |
| 66 | + with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE,REGISTRY_LAUNCHER_PATH, 0, winreg.KEY_READ) as key: |
| 67 | + local_client_path = winreg.QueryValueEx(key, "Path")[0] |
| 68 | + local_client_exe = os.path.join(local_client_path, PARADOX_LAUNCHER_EXE) |
| 69 | + self._local_client_path = local_client_path |
| 70 | + self._local_client_exe = local_client_exe |
| 71 | + except OSError: |
| 72 | + self._local_client_exe = self._local_client_path = self._local_games_path = None |
| 73 | + |
| 74 | + async def get_running_game(self, games, proc_iter_interval=0.05): |
| 75 | + if not games: |
| 76 | + return |
| 77 | + for process_info in process_iter(): |
| 78 | + try: |
| 79 | + await asyncio.sleep(proc_iter_interval) |
| 80 | + for game in games: |
| 81 | + if process_info.binary_path.lower() == games[game].lower(): |
| 82 | + log.info(f"Found a running game! {game}") |
| 83 | + return RunningGame(name=game, process=process_info) |
| 84 | + except: |
| 85 | + continue |
0 commit comments