worthless-launcher/worthless/game/helper.py

111 lines
3.9 KiB
Python

from os import PathLike
from pathlib import Path
from worthless import helper
from worthless.game import Game, Variant
class Helper(Game):
"""
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, if not specified it'll initialize a new one
using super().__init__ and use the following arguments for that instance creation
game_dir: Game directory
variant: Game variant
cache_dir: Cache directory, if not specified it'll automatically detect your
system cache directory and use that.
"""
def __init__(
self,
game: Game = None,
game_dir: PathLike = None,
variant: Variant = None,
cache_dir: PathLike = None,
):
if not game:
super().__init__(game_dir, variant, cache_dir)
game = self
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: The file url to download
file_name: The 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,
)
def set_download_chunk(self, chunk: int):
"""
Sets the download chunk for the internal download function
Args:
chunk: The chunk to set into
"""
self._download_chunk = chunk
async def download_full_game(self, pre_download=False) -> Path:
game = await self._game._get_game(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=False) -> Path:
game = await self._game._get_game(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=False
) -> Path:
from_version = from_version if from_version else self.version
game = await self._game._get_game(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=False
) -> Path:
from_version = from_version if from_version else self.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
)