From e3c256200aca16114a6b68ed27363950996dfaa2 Mon Sep 17 00:00:00 2001 From: tretrauit Date: Fri, 7 Jun 2024 23:59:47 +0700 Subject: [PATCH] fix: use f.tell to get file size yeah, it fixes continuous downloading --- worthless/installer.py | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/worthless/installer.py b/worthless/installer.py index 7e9923d..3cf417a 100644 --- a/worthless/installer.py +++ b/worthless/installer.py @@ -28,10 +28,11 @@ async def _download_file(file_url: str, file_name: str, file_path: Path | str, f if overwrite: file_path.unlink(missing_ok=True) if file_path.exists(): - cur_len = (file_path.stat()).st_size - headers |= { + with file_path.open("rb") as f: + cur_len = f.tell() + headers |= { "Range": f"bytes={cur_len}-{file_len if file_len else ''}" - } + } else: file_path.touch() print(f"Downloading {file_url} to {file_path}...") @@ -458,10 +459,16 @@ class Installer: for _, segment in enumerate(game.latest.segments): archive_name = segment["path"].split("/")[-1] if self.temp_path.joinpath(archive_name + ".downloaded").exists() and self.temp_path.joinpath(archive_name).exists(): + print(f"calculating md5 for downloaded game archive: {archive_name}") + md5 = calculate_md5(self.temp_path.joinpath(archive_name)) + if md5 != segment["md5"]: + print(f"mismatch md5 for downloaded game archive: {archive_name} ({md5}; expected {segment['md5']}), continuing anyway...") continue await self._download_file(segment["path"], archive_name) - if calculate_md5(self.temp_path.joinpath(archive_name)) != segment["md5"]: - raise RuntimeError(f"mismatch md5 for downloaded game archive: {archive_name}") + print(f"calculating md5 for downloaded game archive: {archive_name}") + md5 = calculate_md5(self.temp_path.joinpath(archive_name)) + if md5 != segment["md5"]: + print(f"mismatch md5 for downloaded game archive: {archive_name} ({md5}; expected {segment['md5']}), continuing anyway...") # if i != 0: # with open(self.temp_path.joinpath(base_archive), 'ab') as f: # with open(self.temp_path.joinpath(archive_name), 'rb') as f2: @@ -512,7 +519,9 @@ class Installer: If `force_reinstall` is True, the game will be uninstalled then reinstalled. """ - if self.get_game_data_path().exists(): + if self.get_game_data_path().exists() \ + and all([self._gamedir.joinpath(x).exists() for x in ["UnityPlayer.dll", "pkg_version"]]) \ + and any([self._gamedir.joinpath(x).exists() for x in ["GenshinImpact.exe", "YuanShen.exe"]]): if not force_reinstall: raise ValueError(f"Game is already installed in {self._gamedir}") await self.uninstall_game()