Various changes, block telemetry feature.
-Sp/--patch is now required to do block telemetry before patching. Still preparing for hdiffpatch (will be coming at 1.10) Ay yo hosty support coming soon xD
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import re
|
||||
import shutil
|
||||
|
||||
import platform
|
||||
import aiohttp
|
||||
import appdirs
|
||||
import zipfile
|
||||
@ -13,6 +13,86 @@ from worthless import constants
|
||||
from worthless.launcher import Launcher
|
||||
|
||||
|
||||
async def _download_file(file_url: str, file_name: str, file_path: Path | str, file_len: int = None, overwrite=False, chunks=8192):
|
||||
"""
|
||||
Download file name to temporary directory,
|
||||
:param file_url:
|
||||
:param file_name:
|
||||
:return:
|
||||
"""
|
||||
params = {}
|
||||
file_path = AsyncPath(file_path).joinpath(file_name)
|
||||
if overwrite:
|
||||
await file_path.unlink(missing_ok=True)
|
||||
if await file_path.exists():
|
||||
cur_len = len(await file_path.read_bytes())
|
||||
params |= {
|
||||
"Range": f"bytes={cur_len}-{file_len if file_len else ''}"
|
||||
}
|
||||
else:
|
||||
await file_path.touch()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
rsp = await session.get(file_url, params=params, timeout=None)
|
||||
rsp.raise_for_status()
|
||||
while True:
|
||||
chunk = await rsp.content.read(chunks)
|
||||
if not chunk:
|
||||
break
|
||||
async with file_path.open("ab") as f:
|
||||
await f.write(chunk)
|
||||
|
||||
|
||||
class HDiffPatch:
|
||||
def __init__(self, git_url=None, data_dir=None):
|
||||
if not git_url:
|
||||
repo_url = constants.HDIFFPATCH_GIT_URL
|
||||
self._git_url = git_url
|
||||
if not data_dir:
|
||||
self._appdirs = appdirs.AppDirs(constants.APP_NAME, constants.APP_AUTHOR)
|
||||
self.temp_path = Path(self._appdirs.user_cache_dir).joinpath("Tools/HDiffPatch")
|
||||
else:
|
||||
if not isinstance(data_dir, Path):
|
||||
data_dir = Path(data_dir)
|
||||
self.temp_path = data_dir.joinpath("Temp/Tools/HDiffPatch")
|
||||
self.temp_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
@staticmethod
|
||||
def _get_platform_arch():
|
||||
match platform.system():
|
||||
case "Windows":
|
||||
match platform.architecture()[0]:
|
||||
case "32bit":
|
||||
return "windows32"
|
||||
case "64bit":
|
||||
return "windows64"
|
||||
case "Linux":
|
||||
match platform.architecture()[0]:
|
||||
case "32bit":
|
||||
return "linux32"
|
||||
case "64bit":
|
||||
return "linux64"
|
||||
case "Darwin":
|
||||
return "macos"
|
||||
|
||||
raise RuntimeError("Unsupported platform")
|
||||
|
||||
def _get_hdiffpatch_exec(self, exec_name):
|
||||
if shutil.which(exec_name):
|
||||
return exec_name
|
||||
if not any(self.temp_path.iterdir()):
|
||||
return None
|
||||
platform_arch_path = self.temp_path.joinpath(self._get_platform_arch())
|
||||
if platform_arch_path.joinpath(exec_name).exists():
|
||||
return str(platform_arch_path.joinpath(exec_name))
|
||||
return None
|
||||
|
||||
def get_hpatchz_executable(self):
|
||||
return self._get_hdiffpatch_exec("hpatchz")
|
||||
|
||||
def get_hdiffz_executable(self):
|
||||
return self._get_hdiffpatch_exec("hdiffz")
|
||||
|
||||
|
||||
class Installer:
|
||||
def _read_version_from_config(self):
|
||||
warnings.warn("This function is not reliable as upgrading game version from worthless\
|
||||
@ -88,6 +168,7 @@ class Installer:
|
||||
self._version = None
|
||||
self._overseas = overseas
|
||||
self._launcher = Launcher(self._gamedir, overseas=self._overseas)
|
||||
self._hdiffpatch = HDiffPatch(data_dir=data_dir)
|
||||
self._version = self.get_game_version()
|
||||
|
||||
def set_download_chunk(self, chunk: int):
|
||||
@ -100,26 +181,7 @@ class Installer:
|
||||
:param file_name:
|
||||
:return:
|
||||
"""
|
||||
params = {}
|
||||
file_path = AsyncPath(self.temp_path).joinpath(file_name)
|
||||
if overwrite:
|
||||
await file_path.unlink(missing_ok=True)
|
||||
if await file_path.exists():
|
||||
cur_len = len(await file_path.read_bytes())
|
||||
params |= {
|
||||
"Range": f"bytes={cur_len}-{file_len if file_len else ''}"
|
||||
}
|
||||
else:
|
||||
await file_path.touch()
|
||||
async with aiohttp.ClientSession() as session:
|
||||
rsp = await session.get(file_url, params=params, timeout=None)
|
||||
rsp.raise_for_status()
|
||||
while True:
|
||||
chunk = await rsp.content.read(self._download_chunk)
|
||||
if not chunk:
|
||||
break
|
||||
async with file_path.open("ab") as f:
|
||||
await f.write(chunk)
|
||||
await _download_file(file_url, file_name, self.temp_path, file_len=file_len, overwrite=overwrite)
|
||||
|
||||
def get_game_archive_version(self, game_archive: str | Path):
|
||||
if not game_archive.exists():
|
||||
|
||||
Reference in New Issue
Block a user