Wrap game resource info from the server

Also add function to get the diff archive (for faster updating)

Download function soon, although I can't make sure that the download function will work properly (like pause, resume download etc.)
This commit is contained in:
2022-02-16 22:18:56 +07:00
parent 81fbdec553
commit da3ee30ab1
14 changed files with 227 additions and 84 deletions

View File

@ -7,27 +7,35 @@ from worthless import constants
from worthless.launcher import Launcher
def _read_version_from_game_file(globalgamemanagers: Path):
with globalgamemanagers.open("rb") as f:
data = f.read().decode("ascii", errors="ignore")
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)
class Installer:
def _read_version_from_config(self):
if self._config_file.exists():
if not 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")
return cfg.get("General", "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")
def get_game_version(self):
if self._config_file.exists():
return self._read_version_from_config()
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)
if self._overseas:
globalgamemanagers = self._gamedir.joinpath("./GenshinImpact_Data/globalgamemanagers")
else:
globalgamemanagers = self._gamedir.joinpath("./YuanShen_Data/globalgamemanagers")
if not globalgamemanagers.exists():
return
return _read_version_from_game_file(globalgamemanagers)
def __init__(self, gamedir: str | Path = Path.cwd(), overseas: bool = True, data_dir: str | Path = None):
if isinstance(gamedir, str):
@ -44,9 +52,24 @@ class Installer:
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()
self._launcher = Launcher(self._gamedir, overseas=self._overseas)
self._version = self.get_game_version()
async def get_game_diff_archive(self, from_version: str = None):
"""Gets a diff archive from `from_version` to the latest one
If from_version is not specified, it will be taken from the game version.
"""
if not from_version:
if self._version:
from_version = self._version
else:
from_version = self._version = self.get_game_version()
if not from_version:
raise ValueError("No game version found")
game_resource = await self._launcher.get_resource_info()
if not game_resource:
raise ValueError("Could not fetch game resource")
for v in game_resource.game.diffs:
if v.version == from_version:
return v