From d7a54045174c1067c7431b166c4356fe491328df Mon Sep 17 00:00:00 2001 From: loathingKernel <142770+loathingKernel@users.noreply.github.com> Date: Sat, 24 Dec 2022 14:14:41 +0200 Subject: [PATCH] VerifyWorker: Move to `rare/shared/workers` Signed-off-by: loathingKernel <142770+loathingKernel@users.noreply.github.com> --- .../tabs/games/game_info/game_info.py | 4 +- rare/shared/workers/verify.py | 72 +++++++++++++++++ rare/utils/legendary_utils.py | 81 +------------------ 3 files changed, 76 insertions(+), 81 deletions(-) create mode 100644 rare/shared/workers/verify.py diff --git a/rare/components/tabs/games/game_info/game_info.py b/rare/components/tabs/games/game_info/game_info.py index 4c062c40..7fd4e3f7 100644 --- a/rare/components/tabs/games/game_info/game_info.py +++ b/rare/components/tabs/games/game_info/game_info.py @@ -26,11 +26,11 @@ from rare.shared import ( LegendaryCoreSingleton, GlobalSignalsSingleton, ArgumentsSingleton, + ImageManagerSingleton, ) -from rare.shared import ImageManagerSingleton from rare.shared.image_manager import ImageSize +from rare.shared.workers.verify import VerifyWorker from rare.ui.components.tabs.games.game_info.game_info import Ui_GameInfo -from rare.utils.legendary_utils import VerifyWorker from rare.utils.misc import get_size from rare.utils.steam_grades import SteamWorker from rare.widgets.image_widget import ImageWidget diff --git a/rare/shared/workers/verify.py b/rare/shared/workers/verify.py new file mode 100644 index 00000000..dca52349 --- /dev/null +++ b/rare/shared/workers/verify.py @@ -0,0 +1,72 @@ +import os +from logging import getLogger + +from PyQt5.QtCore import pyqtSignal, QObject, QRunnable + +from rare.lgndr.cli import LegendaryCLI +from rare.lgndr.glue.arguments import LgndrVerifyGameArgs +from rare.lgndr.glue.monkeys import LgndrIndirectStatus +from rare.shared import LegendaryCoreSingleton, ArgumentsSingleton + +logger = getLogger("VerificationWorker") + + +class VerifyWorker(QRunnable): + class Signals(QObject): + status = pyqtSignal(str, int, int, float, float) + result = pyqtSignal(str, bool, int, int) + error = pyqtSignal(str, str) + + num: int = 0 + total: int = 1 # set default to 1 to avoid DivisionByZero before it is initialized + + def __init__(self, app_name): + super(VerifyWorker, self).__init__() + self.signals = VerifyWorker.Signals() + self.setAutoDelete(True) + self.core = LegendaryCoreSingleton() + self.args = ArgumentsSingleton() + self.app_name = app_name + + def status_callback(self, num: int, total: int, percentage: float, speed: float): + self.signals.status.emit(self.app_name, num, total, percentage, speed) + + def run(self): + cli = LegendaryCLI(self.core) + status = LgndrIndirectStatus() + args = LgndrVerifyGameArgs( + app_name=self.app_name, indirect_status=status, verify_stdout=self.status_callback + ) + + # lk: first pass, verify with the current manifest + repair_mode = False + result = cli.verify_game( + args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline + ) + if result is None: + # lk: second pass with downloading the latest manifest + # lk: this happens if the manifest was not found and repair_mode was not requested + # lk: we already have checked if the directory exists before starting the worker + try: + # lk: this try-except block handles the exception caused by a missing manifest + # lk: and is raised only in the case we are offline + repair_mode = True + result = cli.verify_game( + args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline + ) + if result is None: + raise ValueError + except ValueError: + self.signals.error.emit(self.app_name, status.message) + return + + success = result is not None and not any(result) + if success: + # lk: if verification was successful we delete the repair file and run the clean procedure + # lk: this could probably be cut down to what is relevant for this use-case and skip the `cli` call + igame = self.core.get_installed_game(self.app_name) + game = self.core.get_game(self.app_name, platform=igame.platform) + repair_file = os.path.join(self.core.lgd.get_tmp_path(), f"{self.app_name}.repair") + cli.install_game_cleanup(game=game, igame=igame, repair_mode=True, repair_file=repair_file) + + self.signals.result.emit(self.app_name, success, *result) diff --git a/rare/utils/legendary_utils.py b/rare/utils/legendary_utils.py index f8d0523f..54714892 100644 --- a/rare/utils/legendary_utils.py +++ b/rare/utils/legendary_utils.py @@ -2,13 +2,12 @@ import os import platform from logging import getLogger -from PyQt5.QtCore import pyqtSignal, QObject, QRunnable, QStandardPaths +from PyQt5.QtCore import QStandardPaths from legendary.core import LegendaryCore from rare.lgndr.cli import LegendaryCLI -from rare.lgndr.glue.arguments import LgndrVerifyGameArgs, LgndrUninstallGameArgs +from rare.lgndr.glue.arguments import LgndrUninstallGameArgs from rare.lgndr.glue.monkeys import LgndrIndirectStatus -from rare.shared import LegendaryCoreSingleton, ArgumentsSingleton from rare.utils import config_helper logger = getLogger("Legendary Utils") @@ -56,79 +55,3 @@ def uninstall_game(core: LegendaryCore, app_name: str, keep_files=False, keep_co config_helper.save_config() return status.success, status.message - - -def update_manifest(app_name: str, core: LegendaryCore): - game = core.get_game(app_name) - logger.info(f"Reloading game manifest of {game.app_title}") - new_manifest_data, base_urls = core.get_cdn_manifest(game) - # overwrite base urls in metadata with current ones to avoid using old/dead CDNs - game.base_urls = base_urls - # save base urls to game metadata - core.lgd.set_game_meta(game.app_name, game) - - new_manifest = core.load_manifest(new_manifest_data) - logger.debug(f"Base urls: {base_urls}") - # save manifest with version name as well for testing/downgrading/etc. - core.lgd.save_manifest(game.app_name, new_manifest_data, version=new_manifest.meta.build_version) - - -class VerifyWorker(QRunnable): - class Signals(QObject): - status = pyqtSignal(str, int, int, float, float) - result = pyqtSignal(str, bool, int, int) - error = pyqtSignal(str, str) - - num: int = 0 - total: int = 1 # set default to 1 to avoid DivisionByZero before it is initialized - - def __init__(self, app_name): - super(VerifyWorker, self).__init__() - self.signals = VerifyWorker.Signals() - self.setAutoDelete(True) - self.core = LegendaryCoreSingleton() - self.args = ArgumentsSingleton() - self.app_name = app_name - - def status_callback(self, num: int, total: int, percentage: float, speed: float): - self.signals.status.emit(self.app_name, num, total, percentage, speed) - - def run(self): - cli = LegendaryCLI(self.core) - status = LgndrIndirectStatus() - args = LgndrVerifyGameArgs( - app_name=self.app_name, indirect_status=status, verify_stdout=self.status_callback - ) - - # lk: first pass, verify with the current manifest - repair_mode = False - result = cli.verify_game( - args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline - ) - if result is None: - # lk: second pass with downloading the latest manifest - # lk: this happens if the manifest was not found and repair_mode was not requested - # lk: we already have checked if the directory exists before starting the worker - try: - # lk: this try-except block handles the exception caused by a missing manifest - # lk: and is raised only in the case we are offline - repair_mode = True - result = cli.verify_game( - args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline - ) - if result is None: - raise ValueError - except ValueError: - self.signals.error.emit(self.app_name, status.message) - return - - success = result is not None and not any(result) - if success: - # lk: if verification was successful we delete the repair file and run the clean procedure - # lk: this could probably be cut down to what is relevant for this use-case and skip the `cli` call - igame = self.core.get_installed_game(self.app_name) - game = self.core.get_game(self.app_name, platform=igame.platform) - repair_file = os.path.join(self.core.lgd.get_tmp_path(), f"{self.app_name}.repair") - cli.install_game_cleanup(game=game, igame=igame, repair_mode=True, repair_file=repair_file) - - self.signals.result.emit(self.app_name, success, *result)