From f4aa6c749ef8d7b37b848c95c6090ec892f7f47f Mon Sep 17 00:00:00 2001 From: aclist <92275929+aclist@users.noreply.github.com> Date: Tue, 7 Jul 2026 19:36:44 +0900 Subject: [PATCH] chore: move get_app_path() --- dzgui/api/mods.py | 4 ++-- dzgui/api/pefile.py | 23 ++------------------ dzgui/api/steam.py | 50 +++++++++++++++++++++++++++++++++++++++++--- dzgui/init/dayz.py | 4 ++-- tests/test_pefile.py | 6 +++--- 5 files changed, 56 insertions(+), 31 deletions(-) diff --git a/dzgui/api/mods.py b/dzgui/api/mods.py index 4089859..b1b5a71 100644 --- a/dzgui/api/mods.py +++ b/dzgui/api/mods.py @@ -7,7 +7,7 @@ from concurrent.futures import ThreadPoolExecutor from dataclasses import dataclass from pathlib import Path -import dzgui.api.pefile as PeFile +from dzgui.api.steam import get_app_path from dzgui.api.servers import get_rules, fqip_to_record from dzgui.const.constants import ( APP_NAME, @@ -40,7 +40,7 @@ def get_local_mod_ids(steam_path: Path) -> list[int]: def get_local_mod_path(steam_path: Path) -> Path: - p = PeFile.get_app_path(steam_path / Path(LIBRARYFOLDERS_PATH), APPID_DAYZ) + p = get_app_path(steam_path / Path(LIBRARYFOLDERS_PATH), APPID_DAYZ) workshop_path = p / WORKSHOP_PATH return workshop_path diff --git a/dzgui/api/pefile.py b/dzgui/api/pefile.py index d98bf3a..d651689 100644 --- a/dzgui/api/pefile.py +++ b/dzgui/api/pefile.py @@ -14,7 +14,7 @@ from dzgui.const.constants import ( DAYZ_BINARY, LIBRARYFOLDERS_PATH, ) -from dzgui.api.steam import vdf2json +from dzgui.api.steam import get_app_path # https://learn.microsoft.com/en-us/windows/win32/debug/pe-format endian = "<" @@ -257,25 +257,6 @@ class PeFileError(Exception): pass - -class AppNotInstalledError(Exception): - """App not present in user's libraryfolders""" - - pass - - -class AppMovedError(Exception): - """VDF points to a nonexistent location on disk""" - - pass - - -class VDFLoadError(Exception): - """Malformed VDF or JSON conversion""" - - pass - - def parse_version_number(data: BinaryIO) -> FileVersion: # https://learn.microsoft.com/en-us/windows/win32/api/verrsrc/ns-verrsrc-vs_fixedfileinfo minor = struct.unpack("> 16 & 0xFFFF @@ -425,7 +406,7 @@ def get_pefile_path(steam_path: Path, appid: int) -> Path: # TODO: move to dzgui.api.steam -def get_app_path(folders_path: Path, appid: int) -> Path: +def _get_app_path(folders_path: Path, appid: int) -> Path: app_path = None try: diff --git a/dzgui/api/steam.py b/dzgui/api/steam.py index 4527398..a6ef4a3 100644 --- a/dzgui/api/steam.py +++ b/dzgui/api/steam.py @@ -31,6 +31,24 @@ from dzgui.util.bash import concat_bash_args logger = logging.getLogger(APP_NAME) +class AppNotInstalledError(Exception): + """App not present in user's libraryfolders""" + + pass + + +class AppMovedError(Exception): + """VDF points to a nonexistent location on disk""" + + pass + + +class VDFLoadError(Exception): + """Malformed VDF or JSON conversion""" + + pass + + def get_steam_paths() -> list[tuple[Path, str]]: paths = [] if has_steam_client(): @@ -347,9 +365,8 @@ def get_running_app() -> int | None: # TODO: write tests def get_app_allows_downloads(path: Path, appid: int) -> bool: - # TODO: move PeFile.get_app_path() - # TODO: root_path = PeFile.get_app_path(Preferences.DEFAULT, appid) - # acf = "{root_path}/appmanifest_{aid}.acf" + root_path = get_app_path(Preferences.DEFAULT, appid) + acf = f"{root_path}/appmanifest_{aid}.acf" flag = ACF(acf).get_allows_downloads() match flag: # NOTE: adheres to global client setting @@ -387,3 +404,30 @@ def get_client_allows_downloads(path: Path) -> bool: def is_dayz_running() -> bool: appid = get_running_app() return appid in (APPID_DAYZ, APPID_DAYZ_EXP) + + +def get_app_path(folders_path: Path, appid: int) -> Path: + app_path = None + + try: + with open(folders_path) as f: + folders = vdf.load(f) + except Exception: + raise VDFLoadError("Failed to parse libraryfolders") + + for obj in folders["libraryfolders"]: + if str(appid) in folders["libraryfolders"][obj]["apps"]: + app_path = folders["libraryfolders"][obj]["path"] + if Path(app_path).exists(): + break + + if app_path is None: + raise AppNotInstalledError( + f"Failed to find a libraryfolder for the appid {appid}" + ) + if Path(app_path).exists() is False: + raise AppMovedError( + f"The location '{app_path}' pointed to by '{appid}' no longer exists and may have been changed on the disk." + ) + + return Path(app_path) diff --git a/dzgui/init/dayz.py b/dzgui/init/dayz.py index 66f60d9..b83fb45 100644 --- a/dzgui/init/dayz.py +++ b/dzgui/init/dayz.py @@ -2,11 +2,11 @@ import logging from pathlib import Path +from dzgui.api.steam import get_app_path from dzgui.config.query import lookup from dzgui.const.constants import APPID_DAYZ, APP_NAME, LIBRARYFOLDERS_PATH from dzgui.const.enum import Preferences -import dzgui.api.pefile as PeFile logger = logging.getLogger(APP_NAME) @@ -14,7 +14,7 @@ logger = logging.getLogger(APP_NAME) def is_dayz_installed(config: Path) -> None: try: path = lookup(config, Preferences.DEFAULT) - PeFile.get_app_path(Path(path) / LIBRARYFOLDERS_PATH, APPID_DAYZ) + get_app_path(Path(path) / LIBRARYFOLDERS_PATH, APPID_DAYZ) except Exception as e: logger.critical(e) raise e diff --git a/tests/test_pefile.py b/tests/test_pefile.py index 47edc2e..6f02557 100644 --- a/tests/test_pefile.py +++ b/tests/test_pefile.py @@ -2,7 +2,7 @@ import pytest from pathlib import Path import dzgui.api.pefile as PeFile -from dzgui.api.pefile import VDFLoadError, AppNotInstalledError, AppMovedError +from dzgui.api.steam import VDFLoadError, AppNotInstalledError, AppMovedError, get_app_path from dzgui.config.query import get_config from dzgui.config.xdg import get_xdg_paths, parse_filepaths @@ -56,11 +56,11 @@ def test_not_in_library(fixture, exception): fixture = fixture_path(fixture) with pytest.raises(exception): try: - PeFile.get_app_path(fixture, APPID_DAYZ) + get_app_path(fixture, APPID_DAYZ) except Exception as e: raise e def test_on_second_drive(second_drive): - path = PeFile.get_app_path(second_drive, APPID_DAYZ) + path = get_app_path(second_drive, APPID_DAYZ) assert path == Path("/tmp")