1
0
Fork 0
mirror of synced 2024-07-04 06:00:30 +12:00
Rare/rare/components/tabs/games/game_widgets/base_installed_widget.py

146 lines
6.2 KiB
Python
Raw Normal View History

2021-04-23 00:34:06 +12:00
import logging
2021-04-14 02:56:44 +12:00
import os
2021-03-25 23:49:27 +13:00
from logging import getLogger
2021-04-23 00:11:12 +12:00
from PyQt5.QtCore import pyqtSignal, QProcess, QSettings, Qt, QByteArray
2021-04-14 02:56:44 +12:00
from PyQt5.QtWidgets import QGroupBox, QMessageBox, QAction
2021-03-25 23:49:27 +13:00
2021-03-27 06:23:22 +13:00
from custom_legendary.core import LegendaryCore
from custom_legendary.models.game import InstalledGame
from rare.components.dialogs.uninstall_dialog import UninstallDialog
2021-04-10 21:27:40 +12:00
from rare.utils import legendary_utils
2021-04-14 02:56:44 +12:00
from rare.utils.utils import create_desktop_link
2021-03-25 23:49:27 +13:00
logger = getLogger("Game")
2021-03-27 01:29:26 +13:00
class BaseInstalledWidget(QGroupBox):
2021-03-25 23:49:27 +13:00
launch_signal = pyqtSignal(str)
show_info = pyqtSignal(str)
2021-03-27 00:50:57 +13:00
finish_signal = pyqtSignal(str)
update_list = pyqtSignal()
2021-03-25 23:49:27 +13:00
proc: QProcess()
2021-04-20 01:44:28 +12:00
def __init__(self, igame: InstalledGame, core: LegendaryCore, pixmap, offline):
2021-03-25 23:49:27 +13:00
super(BaseInstalledWidget, self).__init__()
2021-03-26 00:14:34 +13:00
self.igame = igame
2021-03-25 23:49:27 +13:00
self.core = core
2021-03-26 00:14:34 +13:00
self.game = self.core.get_game(self.igame.app_name)
2021-03-25 23:49:27 +13:00
self.pixmap = pixmap
self.game_running = False
2021-04-20 01:44:28 +12:00
self.offline = offline
2021-03-27 06:23:22 +13:00
self.update_available = self.core.get_asset(self.game.app_name, True).build_version != igame.version
2021-03-25 23:49:27 +13:00
2021-03-27 06:23:22 +13:00
self.setContentsMargins(0, 0, 0, 0)
2021-03-27 01:29:26 +13:00
2021-04-14 02:56:44 +12:00
self.setContextMenuPolicy(Qt.ActionsContextMenu)
launch = QAction(self.tr("Launch"), self)
launch.triggered.connect(self.launch)
self.addAction(launch)
2021-04-15 00:30:36 +12:00
if os.path.exists(os.path.expanduser(f"~/Desktop/{self.igame.title}.desktop")) \
or os.path.exists(os.path.expanduser(f"~/Desktop/{self.igame.title}.lnk")):
self.create_desktop = QAction(self.tr("Remove Desktop link"))
else:
self.create_desktop = QAction(self.tr("Create Desktop link"))
2021-04-14 04:01:25 +12:00
self.create_desktop.triggered.connect(lambda: self.create_desktop_link("desktop"))
self.addAction(self.create_desktop)
if os.name == "posix":
2021-04-14 04:01:25 +12:00
if os.path.exists(os.path.expanduser(f"~/.local/share/applications/{self.igame.title}.desktop")):
self.create_start_menu = QAction(self.tr("Remove start menu link"))
else:
self.create_start_menu = QAction(self.tr("Create start menu link"))
2021-04-14 02:56:44 +12:00
2021-04-14 04:01:25 +12:00
self.create_start_menu.triggered.connect(lambda: self.create_desktop_link("start_menu"))
self.addAction(self.create_start_menu)
2021-04-14 02:56:44 +12:00
uninstall = QAction(self.tr("Uninstall"), self)
uninstall.triggered.connect(self.uninstall)
self.addAction(uninstall)
2021-04-14 04:01:25 +12:00
def create_desktop_link(self, type_of_link):
if type_of_link == "desktop":
path = os.path.expanduser(f"~/Desktop/")
elif type_of_link == "start_menu":
path = os.path.expanduser("~/.local/share/applications/")
2021-04-14 02:56:44 +12:00
else:
2021-04-14 04:01:25 +12:00
return
2021-04-23 00:34:06 +12:00
if not (os.path.exists(os.path.expanduser(f"{path}{self.igame.title}.desktop"))
2021-04-14 04:01:25 +12:00
or os.path.exists(os.path.expanduser(f"{path}{self.igame.title}.lnk"))):
create_desktop_link(self.igame.app_name, self.core, type_of_link)
if type_of_link == "desktop":
self.create_desktop.setText(self.tr("Remove Desktop link"))
elif type_of_link == "start_menu":
self.create_start_menu.setText(self.tr("Remove Start menu link"))
else:
if os.path.exists(os.path.expanduser(f"{path}{self.igame.title}.desktop")):
os.remove(os.path.expanduser(f"{path}{self.igame.title}.desktop"))
elif os.path.exists(os.path.expanduser(f"{path}{self.igame.title}.lnk")):
os.remove(os.path.expanduser(f"{path}{self.igame.title}.lnk"))
if type_of_link == "desktop":
self.create_desktop.setText(self.tr("Create Desktop link"))
elif type_of_link == "start_menu":
self.create_start_menu.setText(self.tr("Create Start menu link"))
2021-04-14 02:56:44 +12:00
2021-03-25 23:49:27 +13:00
def launch(self, offline=False, skip_version_check=False):
if QSettings().value("confirm_start", False, bool):
if not QMessageBox.question(self, "Launch", self.tr("Do you want to launch {}").format(self.game.app_title),
QMessageBox.Yes | QMessageBox.No) == QMessageBox.Yes:
logger.info("Cancel Startup")
return 1
2021-03-26 00:14:34 +13:00
logger.info("Launching " + self.igame.title)
2021-04-20 01:44:28 +12:00
if offline or self.offline:
if not self.igame.can_run_offline:
2021-04-23 00:34:06 +12:00
QMessageBox.warning(self, "Offline",
self.tr("Game cannot run offline. Please start game in Online mode"))
2021-04-20 01:44:28 +12:00
return
2021-04-15 00:30:36 +12:00
try:
self.proc, params = legendary_utils.launch_game(self.core, self.igame.app_name, offline,
skip_version_check=skip_version_check)
except Exception as e:
logger.error(e)
2021-04-15 00:45:09 +12:00
QMessageBox.warning(self, "Error",
self.tr("An error occurred while starting game. Maybe game files are missing"))
2021-04-15 00:30:36 +12:00
return
2021-03-25 23:49:27 +13:00
if not self.proc:
logger.error("Could not start process")
return 1
2021-04-23 00:11:12 +12:00
self.game_logger = getLogger(self.game.app_name)
2021-03-25 23:49:27 +13:00
self.proc.finished.connect(self.finished)
2021-04-23 00:11:12 +12:00
self.proc.readyReadStandardOutput.connect(self.stdout)
self.proc.readyReadStandardError.connect(self.stderr)
2021-03-27 06:23:22 +13:00
self.proc.start(params[0], params[1:])
2021-03-26 00:14:34 +13:00
self.launch_signal.emit(self.igame.app_name)
2021-03-25 23:49:27 +13:00
self.game_running = True
2021-04-23 00:11:12 +12:00
self.data = QByteArray()
2021-03-25 23:49:27 +13:00
return 0
2021-03-27 00:50:57 +13:00
2021-04-23 00:11:12 +12:00
def stdout(self):
data = self.proc.readAllStandardOutput()
stdout = bytes(data).decode("utf-8")
self.game_logger.info(stdout)
def stderr(self):
stderr = bytes(self.proc.readAllStandardError()).decode("utf-8")
self.game_logger.error(stderr)
2021-04-23 00:34:06 +12:00
QMessageBox.warning(self, "Warning", stderr + "\nSee ~/.cache/rare/logs/")
2021-04-23 00:11:12 +12:00
def finished(self, exit_code):
logger.info("Game exited with exit code: " + str(exit_code))
2021-03-27 00:50:57 +13:00
self.finish_signal.emit(self.game.app_name)
2021-03-25 23:49:27 +13:00
self.game_running = False
def uninstall(self):
infos = UninstallDialog(self.game).get_information()
if infos == 0:
print("Cancel Uninstall")
return
legendary_utils.uninstall(self.game.app_name, self.core, infos)
self.update_list.emit()