worthless-launcher/worthless/installer.py

53 lines
2.2 KiB
Python

import re
import appdirs
from pathlib import Path
from configparser import ConfigParser
from worthless import constants
from worthless.launcher import Launcher
class Installer:
def _read_version_from_config(self):
if self._config_file.exists():
raise FileNotFoundError(f"Config file {self._config_file} not found")
cfg = ConfigParser()
cfg.read(str(self._config_file))
return cfg.get("miHoYo", "game_version")
# https://gitlab.com/KRypt0n_/an-anime-game-launcher/-/blob/main/src/ts/Game.ts#L26
def _read_version_from_game_file(self):
if self._overseas:
globalgamemanagers = self._gamedir.joinpath("./GenshinImpact_Data/globalgamemanagers")
else:
globalgamemanagers = self._gamedir.joinpath("./YuanShen_Data/globalgamemanagers")
if globalgamemanagers.exists():
with globalgamemanagers.open("rb") as f:
data = f.read().decode("ascii")
result = re.search(r"([1-9]+\.[0-9]+\.[0-9]+)_[\d]+_[\d]+", data)
if not result:
raise ValueError("Could not find version in game file")
return result.group(1)
def __init__(self, gamedir: str | Path = Path.cwd(), overseas: bool = True, data_dir: str | Path = None):
if isinstance(gamedir, str):
gamedir = Path(gamedir)
self._gamedir = gamedir
if not data_dir:
self._appdirs = appdirs.AppDirs(constants.APP_NAME, constants.APP_AUTHOR)
self._temp_path = Path(self._appdirs.user_cache_dir).joinpath("Installer")
else:
if not isinstance(data_dir, Path):
data_dir = Path(data_dir)
self._temp_path = data_dir.joinpath("Temp/Installer/")
config_file = self._gamedir.joinpath("config.ini")
self._config_file = config_file.resolve()
self._version = None
self._overseas = overseas
self._launcher = Launcher(self._gamedir, self._overseas)
if config_file.exists():
self._version = self._read_version_from_config()
elif gamedir.joinpath("./GenshinImpact_Data/globalgamemanagers").exists():
self._version = self._read_version_from_game_file()