2021-05-21 23:09:26 +12:00
|
|
|
import os
|
2021-02-18 06:19:37 +13:00
|
|
|
from logging import getLogger
|
|
|
|
|
2021-10-04 08:29:33 +13:00
|
|
|
from PyQt5.QtCore import Qt, pyqtSignal, QRunnable, QObject, QThreadPool
|
2021-05-21 23:09:26 +12:00
|
|
|
from PyQt5.QtWidgets import QDialog
|
2021-04-23 00:34:06 +12:00
|
|
|
from requests.exceptions import ConnectionError
|
2021-02-18 06:19:37 +13:00
|
|
|
|
2021-09-07 07:10:18 +12:00
|
|
|
from legendary.core import LegendaryCore
|
2021-10-04 08:29:33 +13:00
|
|
|
from legendary.models.game import GameAsset
|
2021-08-17 09:08:15 +12:00
|
|
|
from rare import image_dir
|
2021-04-08 08:39:23 +12:00
|
|
|
from rare.components.dialogs.login import LoginDialog
|
2021-05-21 23:09:26 +12:00
|
|
|
from rare.ui.components.dialogs.launch_dialog import Ui_LaunchDialog
|
2021-10-04 08:29:33 +13:00
|
|
|
from rare.utils.models import ApiResults
|
2021-05-21 23:09:26 +12:00
|
|
|
from rare.utils.utils import download_images
|
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
|
|
|
|
2021-10-04 08:29:33 +13:00
|
|
|
class ApiSignals(QObject):
|
|
|
|
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):
|
|
|
|
download_progress = pyqtSignal(int)
|
|
|
|
|
|
|
|
def __init__(self, core: LegendaryCore):
|
|
|
|
super(ImageWorker, self).__init__()
|
2021-02-18 06:19:37 +13:00
|
|
|
self.core = core
|
2021-10-04 08:29:33 +13:00
|
|
|
self.signal = ApiSignals()
|
|
|
|
self.setAutoDelete(True)
|
2021-02-18 06:19:37 +13:00
|
|
|
|
|
|
|
def run(self):
|
2021-10-04 08:29:33 +13:00
|
|
|
download_images(self.signal.image_progress, self.core)
|
|
|
|
self.signal.image_progress.emit(100)
|
|
|
|
|
|
|
|
|
|
|
|
class ApiRequestWorker(QRunnable):
|
|
|
|
def __init__(self, text: str, function: callable, args: tuple):
|
|
|
|
super(ApiRequestWorker, self).__init__()
|
|
|
|
self.function, self.args = function, args
|
|
|
|
self.signals = ApiSignals()
|
|
|
|
self.text = text
|
|
|
|
self.setAutoDelete(True)
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
result = self.function(*self.args)
|
|
|
|
self.signals.result.emit(result, self.text)
|
2021-05-21 23:09:26 +12:00
|
|
|
|
|
|
|
|
|
|
|
class LaunchDialog(QDialog, Ui_LaunchDialog):
|
2021-06-08 08:12:26 +12:00
|
|
|
quit_app = pyqtSignal(int)
|
2021-10-04 08:29:33 +13:00
|
|
|
start_app = pyqtSignal(bool, ApiResults)
|
|
|
|
finished = False
|
2021-03-25 23:49:05 +13:00
|
|
|
|
2021-06-08 08:12:26 +12:00
|
|
|
def __init__(self, core: LegendaryCore, offline=False, parent=None):
|
|
|
|
super(LaunchDialog, self).__init__(parent=parent)
|
2021-05-21 23:09:26 +12:00
|
|
|
self.setupUi(self)
|
2021-06-04 09:33:36 +12:00
|
|
|
self.setAttribute(Qt.WA_DeleteOnClose, True)
|
2021-02-18 06:19:37 +13:00
|
|
|
self.core = core
|
2021-06-08 08:12:26 +12:00
|
|
|
self.offline = offline
|
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
|
|
|
|
2021-02-18 21:46:30 +13:00
|
|
|
def login(self):
|
2021-06-08 08:12:26 +12:00
|
|
|
do_launch = True
|
|
|
|
try:
|
|
|
|
if self.offline:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
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_launch = LoginDialog(core=self.core, parent=self).login()
|
|
|
|
except ConnectionError as e:
|
|
|
|
logger.warning(e)
|
|
|
|
self.offline = True
|
|
|
|
finally:
|
|
|
|
if do_launch:
|
|
|
|
self.show()
|
|
|
|
self.launch()
|
|
|
|
else:
|
|
|
|
self.quit_app.emit(0)
|
2021-02-18 21:46:30 +13:00
|
|
|
|
2021-06-08 08:12:26 +12:00
|
|
|
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)
|
2021-05-21 23:09:26 +12:00
|
|
|
|
2021-06-08 08:12:26 +12:00
|
|
|
if not self.offline:
|
2021-05-21 23:09:26 +12:00
|
|
|
self.image_info.setText(self.tr("Downloading Images"))
|
2021-10-04 08:29:33 +13:00
|
|
|
image_worker = ImageWorker(self.core)
|
|
|
|
image_worker.signal.image_progress.connect(self.update_image_progbar)
|
|
|
|
self.thread_pool.start(image_worker)
|
|
|
|
|
|
|
|
api_requests = [
|
|
|
|
["gamelist", self.core.get_game_and_dlc_list, (True,)],
|
|
|
|
["32bit", self.core.get_game_and_dlc_list, (True, "Win32")],
|
|
|
|
["mac", self.core.get_game_and_dlc_list, (True, "Mac")],
|
|
|
|
["assets", self.core.egs.get_game_assets, ()],
|
|
|
|
["no_assets", self.core.get_non_asset_library_items, ()]
|
|
|
|
]
|
|
|
|
for r in api_requests:
|
|
|
|
worker = ApiRequestWorker(*r)
|
|
|
|
worker.signals.result.connect(self.handle_api_worker_result)
|
|
|
|
self.thread_pool.start(worker)
|
|
|
|
|
2021-06-12 10:29:55 +12:00
|
|
|
else:
|
2021-10-04 08:29:33 +13:00
|
|
|
self.finished = True
|
|
|
|
self.api_results.game_list, self.api_results.dlcs = self.core.get_game_and_dlc_list(False)
|
|
|
|
self.finish()
|
|
|
|
|
|
|
|
def handle_api_worker_result(self, result, text):
|
|
|
|
logger.debug("Api Request got from " + text)
|
|
|
|
if text == "gamelist":
|
|
|
|
self.api_results.game_list, self.api_results.dlcs = result
|
|
|
|
elif text == "32bit":
|
|
|
|
self.api_results.bit32_games = [i.app_name for i in result[0]]
|
|
|
|
elif text == "mac":
|
|
|
|
self.api_results.mac_games = [i.app_name for i in result[0]]
|
|
|
|
elif text == "assets":
|
|
|
|
assets = [GameAsset.from_egs_json(a) for a in result]
|
|
|
|
self.core.lgd.assets = assets
|
|
|
|
self.api_results.assets = assets
|
|
|
|
elif text == "no_assets":
|
|
|
|
self.api_results.no_asset_games = result[0]
|
|
|
|
|
|
|
|
if self.api_results:
|
2021-06-12 10:29:55 +12:00
|
|
|
self.finish()
|
|
|
|
|
2021-05-21 23:09:26 +12:00
|
|
|
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()
|
2021-05-21 23:09:26 +12:00
|
|
|
|
|
|
|
def finish(self):
|
2021-10-04 08:29:33 +13:00
|
|
|
if self.finished:
|
|
|
|
logger.info("App starting")
|
|
|
|
self.image_info.setText(self.tr("Starting..."))
|
|
|
|
self.start_app.emit(self.offline, self.api_results)
|
|
|
|
else:
|
|
|
|
self.finished = True
|