1
0
Fork 0
mirror of synced 2024-07-04 22:21:21 +12:00
Rare/rare/components/tabs/cloud_saves/sync_widget.py

231 lines
9.6 KiB
Python
Raw Normal View History

2021-02-28 05:20:56 +13:00
import os
from logging import getLogger
2021-05-12 19:19:50 +12:00
from PyQt5.QtCore import QThread, pyqtSignal, Qt, QSettings
2021-06-16 04:31:33 +12:00
from PyQt5.QtWidgets import QVBoxLayout, QPushButton, QHBoxLayout, QLabel, QGroupBox, QMessageBox
2021-02-28 05:20:56 +13:00
from legendary.core import LegendaryCore
from legendary.models.game import InstalledGame, SaveGameStatus
2021-04-17 09:54:50 +12:00
from rare.components.dialogs.path_input_dialog import PathInputDialog
2021-04-20 05:12:39 +12:00
logger = getLogger("Sync")
2021-02-28 05:20:56 +13:00
2021-04-17 09:54:50 +12:00
def get_raw_save_path(app_name, core):
game = core.lgd.get_game_meta(app_name)
save_path = game.metadata['customAttributes'].get('CloudSaveFolder', {}).get('value')
return save_path
2021-02-28 05:20:56 +13:00
2021-04-20 05:12:39 +12:00
2021-02-28 05:20:56 +13:00
class _UploadThread(QThread):
2021-05-31 21:55:54 +12:00
signal = pyqtSignal(bool)
2021-02-28 05:20:56 +13:00
def __init__(self, app_name, date_time, save_path, core: LegendaryCore):
super(_UploadThread, self).__init__()
self.core = core
self.app_name = app_name
self.date_time = date_time
self.save_path = save_path
def run(self) -> None:
2021-04-20 05:12:39 +12:00
try:
self.core.upload_save(self.app_name, self.save_path, self.date_time)
2021-05-31 21:55:54 +12:00
self.signal.emit(True)
2021-04-20 05:12:39 +12:00
except Exception as e:
logger.error(e)
2021-05-31 21:55:54 +12:00
self.signal.emit(False)
2021-02-28 05:20:56 +13:00
class _DownloadThread(QThread):
signal = pyqtSignal(bool)
2021-02-28 05:20:56 +13:00
def __init__(self, app_name, latest_save, save_path, core: LegendaryCore):
super(_DownloadThread, self).__init__()
self.core = core
self.app_name = app_name
self.latest_save = latest_save
self.save_path = save_path
def run(self) -> None:
2021-04-20 05:12:39 +12:00
try:
self.core.download_saves(self.app_name, self.latest_save.manifest_name, self.save_path, clean_dir=True)
2021-05-31 21:55:54 +12:00
self.signal.emit(True)
2021-04-20 05:12:39 +12:00
except Exception as e:
logger.error(e)
self.signal.emit(False)
2021-02-28 05:20:56 +13:00
class SyncWidget(QGroupBox):
2021-04-10 21:27:40 +12:00
reload = pyqtSignal(str)
2021-03-19 00:45:59 +13:00
2021-02-28 05:20:56 +13:00
def __init__(self, igame: InstalledGame, save, core: LegendaryCore):
super(SyncWidget, self).__init__(igame.title)
self.setObjectName("group")
2021-02-28 05:20:56 +13:00
self.layout = QVBoxLayout()
self.setContentsMargins(10, 20, 10, 20)
2021-03-18 23:35:07 +13:00
self.thr = None
2021-02-28 05:20:56 +13:00
self.core = core
self.save = save
self.logger = getLogger("Sync " + igame.app_name)
self.game = self.core.get_game(igame.app_name)
self.igame = igame
self.has_save_path = True
2021-06-15 08:33:53 +12:00
if not igame.save_path or igame.save_path is None:
try:
save_path = self.core.get_save_path(igame.app_name)
except Exception as e:
self.logger.error(e)
return
2021-02-28 05:20:56 +13:00
if '%' in save_path or '{' in save_path:
# status = self.tr("Path not found")
2021-02-28 05:20:56 +13:00
self.logger.info("Could not find save path")
igame.save_path = ""
2021-02-28 05:20:56 +13:00
else:
igame.save_path = save_path
2021-06-16 04:31:33 +12:00
if not igame.save_path:
igame.save_path = os.path.expanduser(f"~/{igame.app_name}/")
2021-08-29 06:03:25 +12:00
QMessageBox.warning(self, "Savepath error",
self.tr("Please edit save path of game {} manually in Cload saves tab").format(
igame.title))
if igame.save_path and not os.path.exists(igame.save_path):
2021-02-28 05:20:56 +13:00
os.makedirs(igame.save_path)
2021-06-16 04:31:33 +12:00
self.core.lgd.set_installed_game(self.igame.app_name, self.igame)
2021-02-28 05:20:56 +13:00
self.res, (self.dt_local, dt_remote) = self.core.check_savegame_state(igame.save_path, save)
if self.res == SaveGameStatus.NO_SAVE:
self.logger.debug('No cloud or local savegame found.')
2021-02-28 05:20:56 +13:00
return
2021-03-18 01:47:58 +13:00
# game_title = QLabel(f"<h2>{igame.title}</h2>")
2021-03-18 01:47:58 +13:00
2021-02-28 05:20:56 +13:00
if self.dt_local:
local_save_date = QLabel(
self.tr("Local Save date: ") + str(self.dt_local.strftime('%Y-%m-%d %H:%M:%S')))
else:
local_save_date = QLabel(self.tr("No Local Save files"))
if dt_remote:
cloud_save_date = QLabel(self.tr("Cloud save date: ") + str(dt_remote.strftime('%Y-%m-%d %H:%M:%S')))
else:
cloud_save_date = QLabel(self.tr("No Cloud saves"))
if self.res == SaveGameStatus.SAME_AGE:
self.logger.debug(f'Save game for "{igame.title}" is up to date')
2021-02-28 05:20:56 +13:00
status = self.tr("Game is up to date")
self.upload_button = QPushButton(self.tr("Upload anyway"))
self.download_button = QPushButton(self.tr("Download anyway"))
elif self.res == SaveGameStatus.REMOTE_NEWER:
status = self.tr("Cloud save is newer")
self.download_button = QPushButton(self.tr("Download Cloud saves"))
self.download_button.setObjectName("success")
2021-02-28 05:20:56 +13:00
self.upload_button = QPushButton(self.tr("Upload Saves"))
self.logger.debug(f'Cloud save for "{igame.title}" is newer:')
self.logger.debug(f'- Cloud save date: {dt_remote.strftime("%Y-%m-%d %H:%M:%S")}')
2021-02-28 05:20:56 +13:00
if self.dt_local:
self.logger.debug(f'- Local save date: {self.dt_local.strftime("%Y-%m-%d %H:%M:%S")}')
2021-02-28 05:20:56 +13:00
else:
self.logger.debug('- Local save date: N/A')
2021-02-28 05:20:56 +13:00
self.upload_button.setDisabled(True)
self.upload_button.setToolTip("No local save")
elif self.res == SaveGameStatus.LOCAL_NEWER:
status = self.tr("Local save is newer")
self.upload_button = QPushButton(self.tr("Upload saves"))
self.upload_button.setObjectName("success")
2021-02-28 05:20:56 +13:00
self.download_button = QPushButton(self.tr("Download saves"))
self.logger.info(f'Local save for "{igame.title}" is newer')
if dt_remote:
self.logger.debug(f'- Cloud save date: {dt_remote.strftime("%Y-%m-%d %H:%M:%S")}')
2021-02-28 05:20:56 +13:00
else:
self.logger.debug('- Cloud save date: N/A')
2021-02-28 05:20:56 +13:00
self.download_button.setDisabled(True)
self.logger.debug(f'- Local save date: {self.dt_local.strftime("%Y-%m-%d %H:%M:%S")}')
2021-02-28 05:20:56 +13:00
else:
2021-03-18 01:47:58 +13:00
self.logger.error(self.res)
2021-02-28 05:20:56 +13:00
return
self.upload_button.clicked.connect(self.upload)
self.download_button.clicked.connect(self.download)
self.info_text = QLabel(status)
# self.layout.addWidget(game_title)
2021-02-28 05:20:56 +13:00
self.layout.addWidget(local_save_date)
self.layout.addWidget(cloud_save_date)
save_path_layout = QHBoxLayout()
2021-04-17 09:54:50 +12:00
2021-04-20 05:12:39 +12:00
self.raw_path = QLabel("Raw path: " + get_raw_save_path(self.game.app_name, self.core))
2021-04-17 09:54:50 +12:00
self.raw_path.setTextInteractionFlags(Qt.TextSelectableByMouse)
self.layout.addWidget(self.raw_path)
2021-02-28 05:20:56 +13:00
self.save_path_text = QLabel(igame.save_path)
self.save_path_text.setTextInteractionFlags(Qt.TextSelectableByMouse)
2021-02-28 05:20:56 +13:00
self.save_path_text.setWordWrap(True)
self.change_save_path = QPushButton(self.tr("Change path"))
self.change_save_path.setFixedWidth(100)
2021-03-18 23:35:07 +13:00
self.change_save_path.clicked.connect(self.change_path)
2021-02-28 05:20:56 +13:00
save_path_layout.addWidget(self.save_path_text)
save_path_layout.addWidget(self.change_save_path)
self.layout.addLayout(save_path_layout)
self.layout.addWidget(self.info_text)
button_layout = QHBoxLayout()
button_layout.addWidget(self.upload_button)
button_layout.addWidget(self.download_button)
self.layout.addLayout(button_layout)
self.layout.addStretch(1)
2021-02-28 05:20:56 +13:00
self.setLayout(self.layout)
2021-05-12 19:19:50 +12:00
if self.res == SaveGameStatus.REMOTE_NEWER:
settings = QSettings()
if settings.value(f"{igame.app_name}/auto_sync_cloud", False, bool):
self.download()
2021-03-18 23:35:07 +13:00
def change_path(self):
path = PathInputDialog("Select directory", "Select savepath. Warning: Do not change if you are not sure",
self.igame.save_path).get_path()
if path != "":
self.igame.save_path = path
self.core.lgd.set_installed_game(self.igame.app_name, self.igame)
self.save_path_text.setText(self.igame.save_path)
2021-04-10 21:27:40 +12:00
self.reload.emit(self.game.app_name)
2021-03-19 00:45:59 +13:00
2021-02-28 05:20:56 +13:00
def upload(self):
self.logger.info("Uploading Saves for game " + self.igame.title)
self.info_text.setText(self.tr("Uploading..."))
self.upload_button.setDisabled(True)
self.download_button.setDisabled(True)
self.thr = _UploadThread(self.igame.app_name, self.dt_local, self.igame.save_path, self.core)
self.thr.finished.connect(self.uploaded)
self.thr.start()
2021-05-31 21:55:54 +12:00
def uploaded(self, success):
if success:
self.info_text.setText(self.tr("Upload finished"))
else:
self.info_text.setText(self.tr("Upload failed"))
2021-03-18 23:35:07 +13:00
self.upload_button.setDisabled(False)
2021-04-10 21:27:40 +12:00
self.reload.emit(self.game.app_name)
2021-02-28 05:20:56 +13:00
def download(self):
if not os.path.exists(self.igame.save_path):
os.makedirs(self.igame.save_path)
self.upload_button.setDisabled(True)
self.download_button.setDisabled(True)
self.logger.info("Downloading Saves for game " + self.igame.title)
self.info_text.setText(self.tr("Downloading..."))
2021-03-19 04:00:43 +13:00
self.thr = _DownloadThread(self.igame.app_name, self.save, self.igame.save_path, self.core)
self.thr.signal.connect(self.downloaded)
2021-03-19 04:00:43 +13:00
self.thr.start()
2021-02-28 05:20:56 +13:00
def downloaded(self, success):
if success:
self.info_text.setText(self.tr("Download finished"))
self.upload_button.setDisabled(True)
self.download_button.setDisabled(True)
self.download_button.setStyleSheet("QPushButton{background-color: black}")
self.reload.emit(self.game.app_name)
else:
self.info_text.setText("Error while downloading save")
self.download_button.setStyleSheet("QPushButton{background-color: black}")