1
0
Fork 0
mirror of synced 2024-07-02 05:01:00 +12:00
Rare/rare/components/tabs/games/game_widgets/base_installed_widget.py

205 lines
7.9 KiB
Python
Raw Normal View History

2021-04-14 02:56:44 +12:00
import os
import platform
2021-03-25 23:49:27 +13:00
from logging import getLogger
from PyQt5.QtCore import pyqtSignal, QProcess, QSettings, Qt, QByteArray
2021-09-09 05:22:45 +12:00
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QGroupBox, QMessageBox, QAction, QLabel
2021-03-25 23:49:27 +13:00
from rare.shared import LegendaryCoreSingleton, GlobalSignalsSingleton, ArgumentsSingleton
from rare.components.tabs.games.game_utils import GameUtils
from rare.utils import 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-10-24 12:47:49 +13:00
launch_signal = pyqtSignal(str, QProcess, list)
show_info = pyqtSignal(str)
2021-10-24 12:47:49 +13:00
finish_signal = pyqtSignal(str, int)
2021-03-25 23:49:27 +13:00
proc: QProcess()
def __init__(self, app_name, pixmap: QPixmap, game_utils: GameUtils):
2021-03-25 23:49:27 +13:00
super(BaseInstalledWidget, self).__init__()
self.core = LegendaryCoreSingleton()
self.signals = GlobalSignalsSingleton()
self.args = ArgumentsSingleton()
self.game_utils = game_utils
self.syncing_cloud_saves = False
self.texts = {
"needs_verification": self.tr("Please verify game before playing"),
"hover": {
"update_available": self.tr("Start game without version check"),
"launch": self.tr("Launch Game"),
"launch_origin": self.tr("Launch/Link"),
2021-12-24 22:09:50 +13:00
"running": self.tr("Game running"),
},
"default": {
"running": self.tr("Game running"),
"syncing": self.tr("Syncing cloud saves"),
2021-12-24 22:09:50 +13:00
"update_available": self.tr("Update available"),
},
}
2021-10-24 12:47:49 +13:00
self.game = self.core.get_game(app_name)
self.igame = self.core.get_installed_game(app_name) # None if origin
2021-10-24 12:47:49 +13:00
if self.game.app_title == "Unreal Engine":
self.game.app_title = f"{self.game.app_title} {self.game.app_name.split('_')[-1]}"
2021-09-09 05:22:45 +12:00
self.image = QLabel()
2021-12-24 22:09:50 +13:00
self.image.setPixmap(
pixmap.scaled(200, int(200 * 4 / 3), transformMode=Qt.SmoothTransformation)
)
2021-03-25 23:49:27 +13:00
self.game_running = False
self.offline = self.args.offline
self.update_available = False
if self.igame and self.core.lgd.assets:
try:
2021-12-24 22:09:50 +13:00
remote_version = self.core.get_asset(
self.game.app_name, platform=self.igame.platform, update=False
).build_version
except ValueError:
logger.error(f"Asset error for {self.game.app_title}")
self.update_available = False
else:
if remote_version != self.igame.version:
self.update_available = True
2021-06-30 10:03:03 +12:00
self.data = QByteArray()
2021-03-27 06:23:22 +13:00
self.setContentsMargins(0, 0, 0, 0)
2021-08-14 08:51:36 +12:00
self.settings = QSettings()
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-10-24 12:47:49 +13:00
if self.game.supports_cloud_saves:
sync = QAction(self.tr("Sync with cloud"), self)
sync.triggered.connect(self.sync_game)
2021-10-24 12:47:49 +13:00
self.addAction(sync)
2021-12-24 22:09:50 +13:00
if os.path.exists(
os.path.expanduser(f"~/Desktop/{self.game.app_title}.desktop")
2021-12-24 22:09:50 +13:00
) or os.path.exists(os.path.expanduser(f"~/Desktop/{self.game.app_title}.lnk")):
self.create_desktop = QAction(self.tr("Remove Desktop link"))
else:
self.create_desktop = QAction(self.tr("Create Desktop link"))
if self.igame:
2021-12-24 22:09:50 +13:00
self.create_desktop.triggered.connect(
lambda: self.create_desktop_link("desktop")
)
2021-09-19 02:34:43 +12:00
self.addAction(self.create_desktop)
if platform.system() == "Linux":
2021-12-24 22:09:50 +13:00
start_menu_file = os.path.expanduser(
f"~/.local/share/applications/{self.game.app_title}.desktop"
)
elif platform.system() == "Windows":
2021-12-24 22:09:50 +13:00
start_menu_file = os.path.expandvars(
"%appdata%/Microsoft/Windows/Start Menu"
)
2021-05-12 03:29:35 +12:00
else:
start_menu_file = ""
if platform.system() in ["Windows", "Linux"]:
if os.path.exists(start_menu_file):
self.create_start_menu = QAction(self.tr("Remove start menu link"))
else:
self.create_start_menu = QAction(self.tr("Create start menu link"))
if self.igame:
2021-12-24 22:09:50 +13: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
2021-09-09 05:22:45 +12:00
reload_image = QAction(self.tr("Reload Image"), self)
reload_image.triggered.connect(self.reload_image)
self.addAction(reload_image)
2021-04-14 02:56:44 +12:00
uninstall = QAction(self.tr("Uninstall"), self)
uninstall.triggered.connect(
lambda: self.signals.update_gamelist.emit([self.game.app_name])
2021-12-24 22:09:50 +13:00
if self.game_utils.uninstall_game(self.game.app_name)
else None
)
2021-04-14 02:56:44 +12:00
self.addAction(uninstall)
2021-09-09 05:22:45 +12:00
def reload_image(self):
utils.download_image(self.game, True)
pm = utils.get_pixmap(self.game.app_name)
2021-12-24 22:09:50 +13:00
self.image.setPixmap(
pm.scaled(200, int(200 * 4 / 3), transformMode=Qt.SmoothTransformation)
)
2021-09-09 05:22:45 +12:00
2021-04-14 04:01:25 +12:00
def create_desktop_link(self, type_of_link):
if platform.system() not in ["Windows", "Linux"]:
2021-12-24 22:09:50 +13:00
QMessageBox.warning(
self,
"Warning",
f"Create a Desktop link is currently not supported on {platform.system()}",
)
return
2021-04-14 04:01:25 +12:00
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-12-24 22:09:50 +13:00
if not (
os.path.exists(os.path.expanduser(f"{path}{self.game.app_title}.desktop"))
or os.path.exists(os.path.expanduser(f"{path}{self.game.app_title}.lnk"))
2021-12-24 22:09:50 +13:00
):
2021-09-19 02:34:43 +12:00
try:
if not create_desktop_link(self.game.app_name, self.core, type_of_link):
return
except PermissionError:
2021-12-24 22:09:50 +13:00
QMessageBox.warning(
self, "Error", "Permission error. Cannot create Desktop Link"
)
2021-04-14 04:01:25 +12:00
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:
2021-12-24 22:09:50 +13:00
if os.path.exists(
os.path.expanduser(f"{path}{self.game.app_title}.desktop")
2021-12-24 22:09:50 +13:00
):
os.remove(os.path.expanduser(f"{path}{self.game.app_title}.desktop"))
elif os.path.exists(os.path.expanduser(f"{path}{self.game.app_title}.lnk")):
os.remove(os.path.expanduser(f"{path}{self.game.app_title}.lnk"))
2021-04-14 04:01:25 +12:00
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 not self.game_running:
if self.game.supports_cloud_saves:
self.syncing_cloud_saves = True
2021-12-24 22:09:50 +13:00
self.game_utils.prepare_launch(
self.game.app_name, offline, skip_version_check
)
2021-08-14 08:51:36 +12:00
def sync_finished(self, app_name):
self.syncing_cloud_saves = False
def sync_game(self):
try:
sync = self.game_utils.cloud_save_utils.sync_before_launch_game(
self.game.app_name, True
)
except Exception:
sync = False
if sync:
self.syncing_cloud_saves = True
def game_finished(self, app_name, error):
if error:
QMessageBox.warning(self, "Error", error)
self.game_running = False