2021-02-10 23:48:25 +13:00
|
|
|
import os
|
2021-06-21 07:55:31 +12:00
|
|
|
import platform
|
2021-03-09 08:36:42 +13:00
|
|
|
from logging import getLogger
|
2021-02-10 23:48:25 +13:00
|
|
|
|
2022-07-24 10:06:35 +12:00
|
|
|
from PyQt5.QtCore import pyqtSignal, QObject, QRunnable, QStandardPaths
|
2021-09-07 07:10:18 +12:00
|
|
|
from legendary.core import LegendaryCore
|
2022-07-02 03:42:41 +12:00
|
|
|
|
2022-07-24 10:06:35 +12:00
|
|
|
from rare.lgndr.api_monkeys import LgndrIndirectStatus
|
2022-07-16 06:16:12 +12:00
|
|
|
from rare.lgndr.api_arguments import LgndrVerifyGameArgs, LgndrUninstallGameArgs
|
2022-07-11 04:32:42 +12:00
|
|
|
from rare.lgndr.api_exception import LgndrException
|
2022-08-01 08:00:50 +12:00
|
|
|
from rare.shared import LegendaryCLISingleton, LegendaryCoreSingleton, ArgumentsSingleton
|
2022-02-06 02:48:50 +13:00
|
|
|
from rare.utils import config_helper
|
2021-02-10 23:48:25 +13:00
|
|
|
|
2021-03-09 08:36:42 +13:00
|
|
|
logger = getLogger("Legendary Utils")
|
|
|
|
|
2021-02-10 23:48:25 +13:00
|
|
|
|
2022-07-24 10:06:35 +12:00
|
|
|
def uninstall_game(core: LegendaryCore, app_name: str, keep_files=False, keep_config=False):
|
2021-03-09 08:36:42 +13:00
|
|
|
igame = core.get_installed_game(app_name)
|
2021-05-01 21:17:18 +12:00
|
|
|
|
|
|
|
# remove shortcuts link
|
2022-04-01 08:29:31 +13:00
|
|
|
desktop = QStandardPaths.writableLocation(QStandardPaths.DesktopLocation)
|
|
|
|
applications = QStandardPaths.writableLocation(QStandardPaths.ApplicationsLocation)
|
2021-06-21 07:55:31 +12:00
|
|
|
if platform.system() == "Linux":
|
2022-04-01 08:29:31 +13:00
|
|
|
desktop_shortcut = os.path.join(desktop, f"{igame.title}.desktop")
|
|
|
|
if os.path.exists(desktop_shortcut):
|
|
|
|
os.remove(desktop_shortcut)
|
|
|
|
|
|
|
|
applications_shortcut = os.path.join(applications, f"{igame.title}.desktop")
|
|
|
|
if os.path.exists(applications_shortcut):
|
|
|
|
os.remove(applications_shortcut)
|
2021-05-01 21:17:18 +12:00
|
|
|
|
2021-06-21 07:55:31 +12:00
|
|
|
elif platform.system() == "Windows":
|
2022-04-01 08:29:31 +13:00
|
|
|
game_title = igame.title.split(":")[0]
|
|
|
|
desktop_shortcut = os.path.join(desktop, f"{game_title}.lnk")
|
|
|
|
if os.path.exists(desktop_shortcut):
|
|
|
|
os.remove(desktop_shortcut)
|
|
|
|
|
|
|
|
start_menu_shortcut = os.path.join(applications, "..", f"{game_title}.lnk")
|
|
|
|
if os.path.exists(start_menu_shortcut):
|
|
|
|
os.remove(start_menu_shortcut)
|
2021-05-12 21:45:10 +12:00
|
|
|
|
2022-07-24 10:06:35 +12:00
|
|
|
status = LgndrIndirectStatus()
|
|
|
|
LegendaryCLISingleton().uninstall_game(
|
2022-07-16 06:16:12 +12:00
|
|
|
LgndrUninstallGameArgs(
|
|
|
|
app_name=app_name,
|
|
|
|
keep_files=keep_files,
|
2022-07-24 10:06:35 +12:00
|
|
|
indirect_status=status,
|
2022-07-16 06:16:12 +12:00
|
|
|
yes=True,
|
2021-12-24 22:09:50 +13:00
|
|
|
)
|
2022-07-16 06:16:12 +12:00
|
|
|
)
|
2022-07-24 10:06:35 +12:00
|
|
|
if not keep_config:
|
2022-07-02 03:42:41 +12:00
|
|
|
logger.info("Removing sections in config file")
|
|
|
|
config_helper.remove_section(app_name)
|
|
|
|
config_helper.remove_section(f"{app_name}.env")
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2022-07-02 03:42:41 +12:00
|
|
|
config_helper.save_config()
|
2021-08-31 01:14:40 +12:00
|
|
|
|
2022-07-24 10:06:35 +12:00
|
|
|
return status.success, status.message
|
2022-07-16 06:16:12 +12:00
|
|
|
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2021-11-10 09:31:25 +13:00
|
|
|
def update_manifest(app_name: str, core: LegendaryCore):
|
|
|
|
game = core.get_game(app_name)
|
2022-02-02 10:29:34 +13:00
|
|
|
logger.info(f"Reloading game manifest of {game.app_title}")
|
2021-11-10 09:31:25 +13:00
|
|
|
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)
|
2021-12-24 22:09:50 +13:00
|
|
|
logger.debug(f"Base urls: {base_urls}")
|
2021-11-10 09:31:25 +13:00
|
|
|
# save manifest with version name as well for testing/downgrading/etc.
|
2021-12-24 22:09:50 +13:00
|
|
|
core.lgd.save_manifest(
|
|
|
|
game.app_name, new_manifest_data, version=new_manifest.meta.build_version
|
|
|
|
)
|
2021-11-10 09:31:25 +13:00
|
|
|
|
|
|
|
|
2021-11-11 10:07:28 +13:00
|
|
|
class VerifyWorker(QRunnable):
|
2022-07-08 05:19:52 +12:00
|
|
|
class Signals(QObject):
|
2022-07-11 04:32:42 +12:00
|
|
|
status = pyqtSignal(str, int, int, float, float)
|
|
|
|
result = pyqtSignal(str, bool, int, int)
|
|
|
|
error = pyqtSignal(str, str)
|
2022-07-08 05:19:52 +12:00
|
|
|
|
2021-11-23 08:09:05 +13:00
|
|
|
num: int = 0
|
|
|
|
total: int = 1 # set default to 1 to avoid DivisionByZero before it is initialized
|
|
|
|
|
2022-01-16 04:40:31 +13:00
|
|
|
def __init__(self, app_name):
|
2021-11-11 10:07:28 +13:00
|
|
|
super(VerifyWorker, self).__init__()
|
2022-07-08 05:19:52 +12:00
|
|
|
self.signals = VerifyWorker.Signals()
|
2021-11-11 10:07:28 +13:00
|
|
|
self.setAutoDelete(True)
|
2022-07-08 05:19:52 +12:00
|
|
|
self.cli = LegendaryCLISingleton()
|
2022-02-26 06:43:27 +13:00
|
|
|
self.core = LegendaryCoreSingleton()
|
2022-08-01 08:00:50 +12:00
|
|
|
self.args = ArgumentsSingleton()
|
2022-02-26 06:43:27 +13:00
|
|
|
self.app_name = app_name
|
2021-03-10 04:52:51 +13:00
|
|
|
|
2022-07-08 05:19:52 +12:00
|
|
|
def status_callback(self, num: int, total: int, percentage: float, speed: float):
|
2022-07-11 04:32:42 +12:00
|
|
|
self.signals.status.emit(self.app_name, num, total, percentage, speed)
|
2022-07-08 05:19:52 +12:00
|
|
|
|
2021-03-10 04:52:51 +13:00
|
|
|
def run(self):
|
2022-07-24 10:06:35 +12:00
|
|
|
status = LgndrIndirectStatus()
|
2022-07-11 04:32:42 +12:00
|
|
|
args = LgndrVerifyGameArgs(app_name=self.app_name,
|
2022-07-24 10:06:35 +12:00
|
|
|
indirect_status=status,
|
2022-07-11 04:32:42 +12:00
|
|
|
verify_stdout=self.status_callback)
|
2022-08-01 08:00:50 +12:00
|
|
|
|
|
|
|
# lk: first pass, verify with the current manifest
|
|
|
|
repair_mode = False
|
2022-07-24 10:06:35 +12:00
|
|
|
result = self.cli.verify_game(
|
2022-08-01 08:00:50 +12:00
|
|
|
args, print_command=False, repair_mode=repair_mode, repair_online=not self.args.offline
|
|
|
|
)
|
|
|
|
if result is None:
|
|
|
|
# lk: second pass with 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 = self.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')
|
|
|
|
self.cli.clean_post_install(game=game, igame=igame, repair=True, repair_file=repair_file)
|
|
|
|
|
|
|
|
self.signals.result.emit(self.app_name, success, *result)
|
|
|
|
|