Read version from game file (thanks to An Anime Game Launcher project)

Basically I copied their method of doing that and their regex pattern.
This commit is contained in:
tretrauit 2022-02-16 01:58:45 +07:00
parent 2b811d99b9
commit ad391a551c
Signed by: tretrauit
GPG Key ID: 862760FF1903319E
3 changed files with 25 additions and 18 deletions

View File

@ -1,3 +1,4 @@
aiohttp==3.8.1
appdirs~=1.4.4
aiofiles~=0.8.0
aiofiles~=0.8.0
aiopath~=0.6.10

View File

@ -1,16 +1,28 @@
import asyncio
import tarfile
import constants
import appdirs
import aiofiles
import re
from pathlib import Path
import shutil
import aiohttp
from worthless.launcher import Launcher
from configparser import ConfigParser
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):
globalgamemanagers = self._gamedir.joinpath("./GenshinImpact_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):
if isinstance(gamedir, str):
gamedir = Path(gamedir)
@ -22,12 +34,6 @@ class Installer:
self._launcher = Launcher(self._gamedir, self._overseas)
if config_file.exists():
self._version = self._read_version_from_config()
else: # TODO: Use An Anime Game Launcher method (which is more brutal, but it works)
self._version = "mangosus"
elif gamedir.joinpath("./GenshinImpact_Data/globalgamemanagers").exists():
self._version = self._read_version_from_game_file()
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")

View File

@ -18,7 +18,7 @@ class Patcher:
self._temp_path = Path(self._appdirs.user_cache_dir)
else:
if not isinstance(data_dir, Path):
override_data_dir = Path(data_dir)
data_dir = Path(data_dir)
self._patch_path = data_dir.joinpath("Patch")
self._temp_path = data_dir.joinpath("Temp")