2022-02-15 18:58:45 +00:00
|
|
|
import re
|
2022-02-15 19:55:19 +00:00
|
|
|
import appdirs
|
2022-02-15 17:49:33 +00:00
|
|
|
from pathlib import Path
|
|
|
|
from configparser import ConfigParser
|
2022-02-15 19:55:19 +00:00
|
|
|
|
|
|
|
from worthless import constants
|
2022-02-15 18:58:45 +00:00
|
|
|
from worthless.launcher import Launcher
|
2022-02-15 17:49:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Installer:
|
2022-02-15 18:58:45 +00:00
|
|
|
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):
|
2022-02-15 19:55:19 +00:00
|
|
|
if self._overseas:
|
|
|
|
globalgamemanagers = self._gamedir.joinpath("./GenshinImpact_Data/globalgamemanagers")
|
|
|
|
else:
|
|
|
|
globalgamemanagers = self._gamedir.joinpath("./YuanShen_Data/globalgamemanagers")
|
2022-02-15 18:58:45 +00:00
|
|
|
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)
|
|
|
|
|
2022-02-15 19:55:19 +00:00
|
|
|
def __init__(self, gamedir: str | Path = Path.cwd(), overseas: bool = True, data_dir: str | Path = None):
|
2022-02-15 17:49:33 +00:00
|
|
|
if isinstance(gamedir, str):
|
|
|
|
gamedir = Path(gamedir)
|
|
|
|
self._gamedir = gamedir
|
2022-02-15 19:55:19 +00:00
|
|
|
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/")
|
2022-02-15 17:49:33 +00:00
|
|
|
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()
|
2022-02-15 18:58:45 +00:00
|
|
|
elif gamedir.joinpath("./GenshinImpact_Data/globalgamemanagers").exists():
|
|
|
|
self._version = self._read_version_from_game_file()
|
2022-02-15 17:49:33 +00:00
|
|
|
|