fix: use f.tell to get file size

yeah, it fixes continuous downloading
This commit is contained in:
tretrauit 2024-06-07 23:59:47 +07:00
parent 19ce16f095
commit e3c256200a

View File

@ -28,10 +28,11 @@ async def _download_file(file_url: str, file_name: str, file_path: Path | str, f
if overwrite: if overwrite:
file_path.unlink(missing_ok=True) file_path.unlink(missing_ok=True)
if file_path.exists(): if file_path.exists():
cur_len = (file_path.stat()).st_size with file_path.open("rb") as f:
headers |= { cur_len = f.tell()
headers |= {
"Range": f"bytes={cur_len}-{file_len if file_len else ''}" "Range": f"bytes={cur_len}-{file_len if file_len else ''}"
} }
else: else:
file_path.touch() file_path.touch()
print(f"Downloading {file_url} to {file_path}...") print(f"Downloading {file_url} to {file_path}...")
@ -458,10 +459,16 @@ class Installer:
for _, segment in enumerate(game.latest.segments): for _, segment in enumerate(game.latest.segments):
archive_name = segment["path"].split("/")[-1] archive_name = segment["path"].split("/")[-1]
if self.temp_path.joinpath(archive_name + ".downloaded").exists() and self.temp_path.joinpath(archive_name).exists(): 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 continue
await self._download_file(segment["path"], archive_name) await self._download_file(segment["path"], archive_name)
if calculate_md5(self.temp_path.joinpath(archive_name)) != segment["md5"]: print(f"calculating md5 for downloaded game archive: {archive_name}")
raise RuntimeError(f"mismatch 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: # if i != 0:
# with open(self.temp_path.joinpath(base_archive), 'ab') as f: # with open(self.temp_path.joinpath(base_archive), 'ab') as f:
# with open(self.temp_path.joinpath(archive_name), 'rb') as f2: # 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 `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: if not force_reinstall:
raise ValueError(f"Game is already installed in {self._gamedir}") raise ValueError(f"Game is already installed in {self._gamedir}")
await self.uninstall_game() await self.uninstall_game()