mirror of
https://github.com/aclist/dztui.git
synced 2026-08-26 09:47:36 +02:00
chore: move get_app_path()
This commit is contained in:
parent
3ed4747d08
commit
f4aa6c749e
@ -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
|
||||
|
||||
|
||||
@ -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("<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
|
||||
def get_app_path(folders_path: Path, appid: int) -> Path:
|
||||
def _get_app_path(folders_path: Path, appid: int) -> Path:
|
||||
app_path = None
|
||||
|
||||
try:
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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")
|
||||
|
||||
Loading…
Reference in New Issue
Block a user