worthless-launcher/worthless/game/gamehelper.py

104 lines
3.5 KiB
Python

from pathlib import Path
from worthless import helper
from worthless.game import Game
class Helper:
"""
Quick and dirty extra functions for Game
Since this is quick and dirty, you are recommended to write your own method instead.
Args:
game: A worthless.game.Game instance
"""
def __init__(self, game: Game):
self._download_chunk = 8192
self._game = game
async def _download_file(
self, file_url: str, file_name: str, file_len: int = None, overwrite=False
) -> Path:
"""
Download file name to temporary directory.
This function is a wrapper for helper.download_file
Args:
file_url: File url to download
file_name: File name to download into
Returns:
A Path object containing downloaded file
"""
return await helper.download_file(
file_url,
file_name,
self._game.cache_path,
file_len=file_len,
overwrite=overwrite,
chunks=self._download_chunk,
)
@property
def download_chunk(self):
return self._download_chunk
@download_chunk.setter
def download_chunk(self, chunk: int) -> None:
"""
Sets the download chunk for the internal download function
Args:
chunk: Chunk to set into
"""
self._download_chunk = chunk
async def download_full_game(self, pre_download: bool = False) -> Path:
game = await self._game.get_server_game_info(pre_download)
archive_name = game.latest.path.split("/")[-1]
return await self._download_file(
game.latest.path, archive_name, game.latest.size
)
async def download_full_voicepack(
self, language: str, pre_download: bool = False
) -> Path:
game = await self._game.get_server_game_info(pre_download)
translated_lang = self._game.voicepack_lang_translate(language)
for vo in game.latest.voice_packs:
if vo.language == translated_lang:
return await self._download_file(vo.path, vo.get_name(), vo.size)
async def download_game_update(
self, from_version: str = None, pre_download: bool = False
) -> Path:
from_version = from_version if from_version else self._game.version
game = await self._game.get_server_game_info(pre_download=pre_download)
if self._game.version == game.latest.version:
raise ValueError("Game is already up to date.")
diff_archive = await self._game.get_game_diff_archive(
from_version, pre_download
)
if diff_archive is None:
raise ValueError(
"Game diff archive is not available for this version, please reinstall."
)
return await self._download_file(
diff_archive.path, diff_archive.name, diff_archive.size
)
async def download_voicepack_update(
self, language: str, from_version: str = None, pre_download: bool = False
) -> Path:
from_version = from_version if from_version else self._game.version
diff_archive = await self._game.get_voicepack_diff_archive(
language, from_version, pre_download
)
if diff_archive is None:
raise ValueError("Voiceover diff archive is not available for this version")
return await self._download_file(
diff_archive.path, diff_archive.name, diff_archive.size
)