1
0
Fork 0
mirror of synced 2024-07-04 14:10:46 +12:00
Rare/rare/components/dialogs/launch_dialog.py

193 lines
6.7 KiB
Python
Raw Normal View History

import os
2021-12-19 11:24:20 +13:00
import platform
2021-02-18 06:19:37 +13:00
from logging import getLogger
from PyQt5.QtCore import Qt, pyqtSignal, QRunnable, QObject, QThreadPool, QSettings
from PyQt5.QtWidgets import QDialog, QApplication
from legendary.core import LegendaryCore
2021-10-05 08:01:45 +13:00
from requests.exceptions import ConnectionError, HTTPError
2021-02-18 06:19:37 +13:00
from rare.shared import LegendaryCoreSingleton, ArgumentsSingleton, ApiResultsSingleton
2021-04-08 08:39:23 +12:00
from rare.components.dialogs.login import LoginDialog
from rare.ui.components.dialogs.launch_dialog import Ui_LaunchDialog
2021-10-04 08:29:33 +13:00
from rare.utils.models import ApiResults
from rare.utils.paths import image_dir
from rare.utils.utils import download_images, CloudWorker
2021-02-23 07:02:49 +13:00
2021-02-18 06:19:37 +13:00
logger = getLogger("Login")
2021-02-23 07:02:49 +13:00
class LaunchDialogSignals(QObject):
2021-10-04 08:29:33 +13:00
image_progress = pyqtSignal(int)
result = pyqtSignal(object, str)
2021-02-18 06:19:37 +13:00
2021-10-04 08:29:33 +13:00
class ImageWorker(QRunnable):
def __init__(self, core: LegendaryCore):
super(ImageWorker, self).__init__()
self.signals = LaunchDialogSignals()
2021-10-04 08:29:33 +13:00
self.setAutoDelete(True)
self.core = core
2021-02-18 06:19:37 +13:00
def run(self):
download_images(self.signals.image_progress, self.signals.result, self.core)
self.signals.image_progress.emit(100)
2021-10-04 08:29:33 +13:00
class ApiRequestWorker(QRunnable):
def __init__(self):
2021-10-04 08:29:33 +13:00
super(ApiRequestWorker, self).__init__()
self.signals = LaunchDialogSignals()
2021-10-04 08:29:33 +13:00
self.setAutoDelete(True)
self.core = LegendaryCoreSingleton()
self.settings = QSettings()
2021-10-04 08:29:33 +13:00
def run(self) -> None:
if self.settings.value("mac_meta", platform.system() == "Darwin", bool):
try:
result = self.core.get_game_and_dlc_list(True, "Mac")
except HTTPError:
result = [], {}
self.signals.result.emit(result, "mac")
else:
self.signals.result.emit(([], {}), "mac")
if self.settings.value("win32_meta", False, bool):
try:
result = self.core.get_game_and_dlc_list(True, "Win32")
except HTTPError:
result = [], {}
else:
result = [], {}
self.signals.result.emit(result, "32bit")
class LaunchDialog(QDialog, Ui_LaunchDialog):
quit_app = pyqtSignal(int)
2021-10-08 07:19:24 +13:00
start_app = pyqtSignal()
finished = 0
2021-10-08 07:19:24 +13:00
def __init__(self, parent=None):
super(LaunchDialog, self).__init__(parent=parent)
self.setupUi(self)
2021-06-04 09:33:36 +12:00
self.setAttribute(Qt.WA_DeleteOnClose, True)
self.setWindowFlags(Qt.Dialog | Qt.CustomizeWindowHint | Qt.WindowTitleHint)
self.setWindowModality(Qt.WindowModal)
self.core = LegendaryCoreSingleton()
self.args = ArgumentsSingleton()
2021-10-04 08:29:33 +13:00
self.thread_pool = QThreadPool()
2021-10-04 09:20:54 +13:00
self.thread_pool.setMaxThreadCount(2)
2021-10-04 08:29:33 +13:00
self.api_results = ApiResults()
2021-04-20 01:44:28 +12:00
def login(self):
do_launch = True
try:
if self.args.offline:
pass
else:
QApplication.processEvents()
if self.core.login():
logger.info("You are logged in")
else:
raise ValueError("You are not logged in. Open Login Window")
except ValueError as e:
logger.info(str(e))
# Do not set parent, because it won't show a task bar icon
do_launch = LoginDialog(core=self.core).login()
except ConnectionError as e:
logger.warning(e)
self.args.offline = True
finally:
if do_launch:
if not self.args.silent:
self.show()
self.launch()
else:
self.quit_app.emit(0)
def launch(self):
2021-03-19 00:45:59 +13:00
# self.core = core
2021-08-17 09:08:15 +12:00
if not os.path.exists(image_dir):
os.makedirs(image_dir)
if not self.args.offline:
self.image_info.setText(self.tr("Downloading Images"))
2021-10-04 08:29:33 +13:00
image_worker = ImageWorker(self.core)
image_worker.signals.image_progress.connect(self.update_image_progbar)
image_worker.signals.result.connect(self.handle_api_worker_result)
2021-10-04 08:29:33 +13:00
self.thread_pool.start(image_worker)
# gamelist and no_asset games are from Image worker
worker = ApiRequestWorker()
worker.signals.result.connect(self.handle_api_worker_result)
self.thread_pool.start(worker)
2021-10-04 08:29:33 +13:00
# cloud save from another worker, because it is used in cloud_save_utils too
cloud_worker = CloudWorker()
2021-12-24 22:09:50 +13:00
cloud_worker.signals.result_ready.connect(
lambda x: self.handle_api_worker_result(x, "saves")
)
self.thread_pool.start(cloud_worker)
2021-06-12 10:29:55 +12:00
else:
self.finished = 2
if self.core.lgd.assets:
2021-12-24 22:09:50 +13:00
(
self.api_results.game_list,
self.api_results.dlcs,
) = self.core.get_game_and_dlc_list(False)
self.api_results.bit32_games = list(
map(lambda i: i.app_name, self.core.get_game_list(False, "Win32"))
)
self.api_results.mac_games = list(
map(lambda i: i.app_name, self.core.get_game_list(False, "Mac"))
)
else:
logger.warning("No assets found. Falling back to empty game lists")
self.api_results.game_list, self.api_results.dlcs = [], {}
self.api_results.mac_games = self.api_results.bit32_games = []
2021-10-04 08:29:33 +13:00
self.finish()
def handle_api_worker_result(self, result, text):
logger.debug(f"Api Request got from {text}")
2021-10-04 08:29:33 +13:00
if text == "gamelist":
2021-10-05 08:01:45 +13:00
if result:
self.api_results.game_list, self.api_results.dlcs = result
else:
2021-12-24 22:09:50 +13:00
(
self.api_results.game_list,
self.api_results.dlcs,
) = self.core.get_game_and_dlc_list(False)
2021-10-04 08:29:33 +13:00
elif text == "32bit":
2021-12-24 22:09:50 +13:00
self.api_results.bit32_games = (
[i.app_name for i in result[0]] if result else []
)
2021-10-04 08:29:33 +13:00
elif text == "mac":
2021-12-24 22:09:50 +13:00
self.api_results.mac_games = (
[i.app_name for i in result[0]] if result else []
)
2021-10-04 08:29:33 +13:00
elif text == "no_assets":
self.api_results.no_asset_games = result if result else []
2021-10-04 08:29:33 +13:00
2021-10-24 12:47:49 +13:00
elif text == "saves":
self.api_results.saves = result
2021-10-04 08:29:33 +13:00
if self.api_results:
2021-06-12 10:29:55 +12:00
self.finish()
def update_image_progbar(self, i: int):
self.image_prog_bar.setValue(i)
2021-10-04 08:29:33 +13:00
if i == 100:
self.finish()
def finish(self):
if self.finished == 1:
2021-10-04 08:29:33 +13:00
logger.info("App starting")
self.image_info.setText(self.tr("Starting..."))
ApiResultsSingleton(self.api_results)
2021-10-08 07:19:24 +13:00
self.start_app.emit()
2021-10-04 08:29:33 +13:00
else:
self.finished += 1