chore: move get_app_path()

This commit is contained in:
aclist committed 2026-07-07 19:36:44 +09:00
1 parent 3ed4747d08
commit f4aa6c749e
5 files changed
+56 -31

No files matched your search

+2 -2
View File
@@ -7,7 +7,7 @@ from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path 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.api.servers import get_rules, fqip_to_record
from dzgui.const.constants import ( from dzgui.const.constants import (
APP_NAME, 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: 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 workshop_path = p / WORKSHOP_PATH
return workshop_path return workshop_path
+2 -21
View File
@@ -14,7 +14,7 @@ from dzgui.const.constants import (
DAYZ_BINARY, DAYZ_BINARY,
LIBRARYFOLDERS_PATH, 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 # https://learn.microsoft.com/en-us/windows/win32/debug/pe-format
endian = "<" endian = "<"
@@ -257,25 +257,6 @@ class PeFileError(Exception):
pass 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: def parse_version_number(data: BinaryIO) -> FileVersion:
# https://learn.microsoft.com/en-us/windows/win32/api/verrsrc/ns-verrsrc-vs_fixedfileinfo # https://learn.microsoft.com/en-us/windows/win32/api/verrsrc/ns-verrsrc-vs_fixedfileinfo
minor = struct.unpack("<L", data.read(4))[0] >> 16 & 0xFFFF minor = struct.unpack("<L", data.read(4))[0] >> 16 & 0xFFFF
@@ -425,7 +406,7 @@ def get_pefile_path(steam_path: Path, appid: int) -> Path:
# TODO: move to dzgui.api.steam # 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 app_path = None
try: try:
+47 -3
View File
@@ -31,6 +31,24 @@ from dzgui.util.bash import concat_bash_args
logger = logging.getLogger(APP_NAME) 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]]: def get_steam_paths() -> list[tuple[Path, str]]:
paths = [] paths = []
if has_steam_client(): if has_steam_client():
@@ -347,9 +365,8 @@ def get_running_app() -> int | None:
# TODO: write tests # TODO: write tests
def get_app_allows_downloads(path: Path, appid: int) -> bool: def get_app_allows_downloads(path: Path, appid: int) -> bool:
# TODO: move PeFile.get_app_path() root_path = get_app_path(Preferences.DEFAULT, appid)
# TODO: root_path = PeFile.get_app_path(Preferences.DEFAULT, appid) acf = f"{root_path}/appmanifest_{aid}.acf"
# acf = "{root_path}/appmanifest_{aid}.acf"
flag = ACF(acf).get_allows_downloads() flag = ACF(acf).get_allows_downloads()
match flag: match flag:
# NOTE: adheres to global client setting # NOTE: adheres to global client setting
@@ -387,3 +404,30 @@ def get_client_allows_downloads(path: Path) -> bool:
def is_dayz_running() -> bool: def is_dayz_running() -> bool:
appid = get_running_app() appid = get_running_app()
return appid in (APPID_DAYZ, APPID_DAYZ_EXP) 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)
+2 -2
View File
@@ -2,11 +2,11 @@ import logging
from pathlib import Path from pathlib import Path
from dzgui.api.steam import get_app_path
from dzgui.config.query import lookup from dzgui.config.query import lookup
from dzgui.const.constants import APPID_DAYZ, APP_NAME, LIBRARYFOLDERS_PATH from dzgui.const.constants import APPID_DAYZ, APP_NAME, LIBRARYFOLDERS_PATH
from dzgui.const.enum import Preferences from dzgui.const.enum import Preferences
import dzgui.api.pefile as PeFile
logger = logging.getLogger(APP_NAME) logger = logging.getLogger(APP_NAME)
@@ -14,7 +14,7 @@ logger = logging.getLogger(APP_NAME)
def is_dayz_installed(config: Path) -> None: def is_dayz_installed(config: Path) -> None:
try: try:
path = lookup(config, Preferences.DEFAULT) 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: except Exception as e:
logger.critical(e) logger.critical(e)
raise e raise e
+3 -3
View File
@@ -2,7 +2,7 @@ import pytest
from pathlib import Path from pathlib import Path
import dzgui.api.pefile as PeFile 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.query import get_config
from dzgui.config.xdg import get_xdg_paths, parse_filepaths 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) fixture = fixture_path(fixture)
with pytest.raises(exception): with pytest.raises(exception):
try: try:
PeFile.get_app_path(fixture, APPID_DAYZ) get_app_path(fixture, APPID_DAYZ)
except Exception as e: except Exception as e:
raise e raise e
def test_on_second_drive(second_drive): 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") assert path == Path("/tmp")