2021-03-27 04:09:42 +13:00
|
|
|
import datetime
|
2021-02-18 05:46:03 +13:00
|
|
|
from logging import getLogger
|
2021-03-15 10:11:03 +13:00
|
|
|
from multiprocessing import Queue as MPQueue
|
2021-02-18 05:46:03 +13:00
|
|
|
|
2021-04-13 20:31:26 +12:00
|
|
|
from PyQt5.QtCore import QThread, pyqtSignal, Qt, QSettings
|
2021-03-17 03:12:37 +13:00
|
|
|
from PyQt5.QtWidgets import QWidget, QMessageBox, QVBoxLayout, QLabel, QGridLayout, QProgressBar, QPushButton, QDialog, \
|
2021-04-08 04:18:50 +12:00
|
|
|
QListWidget, QHBoxLayout, QGroupBox
|
2021-03-16 00:03:03 +13:00
|
|
|
|
2021-04-08 08:39:23 +12:00
|
|
|
from rare.components.dialogs.install_dialog import InstallInfoDialog, InstallDialog
|
2021-04-08 08:42:30 +12:00
|
|
|
from rare.components.tabs.downloads.dl_queue_widget import DlQueueWidget
|
|
|
|
from rare.components.tabs.downloads.download_thread import DownloadThread
|
2021-04-08 08:39:23 +12:00
|
|
|
from rare.utils.models import InstallOptions
|
2021-03-17 03:12:37 +13:00
|
|
|
from custom_legendary.core import LegendaryCore
|
|
|
|
from custom_legendary.models.downloading import UIUpdate
|
2021-03-25 23:49:05 +13:00
|
|
|
from custom_legendary.models.game import Game, InstalledGame
|
2021-03-17 03:12:37 +13:00
|
|
|
from custom_legendary.utils.selective_dl import games
|
2021-04-12 07:02:56 +12:00
|
|
|
from rare.utils.utils import get_size
|
2021-02-18 05:46:03 +13:00
|
|
|
|
|
|
|
logger = getLogger("Download")
|
|
|
|
|
|
|
|
|
2021-02-10 23:48:25 +13:00
|
|
|
class DownloadTab(QWidget):
|
2021-04-13 00:24:10 +12:00
|
|
|
finished = pyqtSignal(bool)
|
2021-02-18 05:46:03 +13:00
|
|
|
thread: QThread
|
2021-04-08 00:46:51 +12:00
|
|
|
dl_queue = []
|
2021-02-18 05:46:03 +13:00
|
|
|
|
2021-03-01 00:06:33 +13:00
|
|
|
def __init__(self, core: LegendaryCore, updates: list):
|
2021-02-10 23:48:25 +13:00
|
|
|
super(DownloadTab, self).__init__()
|
2021-02-18 05:46:03 +13:00
|
|
|
self.core = core
|
|
|
|
self.layout = QVBoxLayout()
|
2021-02-20 00:57:55 +13:00
|
|
|
self.active_game: Game = None
|
2021-02-18 05:46:03 +13:00
|
|
|
|
2021-03-12 00:56:38 +13:00
|
|
|
self.installing_game = QLabel(self.tr("No active Download"))
|
2021-03-15 10:11:03 +13:00
|
|
|
self.dl_speed = QLabel()
|
|
|
|
self.cache_used = QLabel()
|
|
|
|
self.downloaded = QLabel()
|
2021-03-27 04:09:42 +13:00
|
|
|
self.time_left = QLabel()
|
2021-02-18 05:46:03 +13:00
|
|
|
|
|
|
|
self.info_layout = QGridLayout()
|
|
|
|
|
|
|
|
self.info_layout.addWidget(self.installing_game, 0, 0)
|
|
|
|
self.info_layout.addWidget(self.dl_speed, 0, 1)
|
2021-02-27 07:28:54 +13:00
|
|
|
self.info_layout.addWidget(self.cache_used, 1, 0)
|
|
|
|
self.info_layout.addWidget(self.downloaded, 1, 1)
|
2021-03-27 04:09:42 +13:00
|
|
|
self.info_layout.addWidget(self.time_left, 2, 0)
|
2021-02-18 05:46:03 +13:00
|
|
|
self.layout.addLayout(self.info_layout)
|
2021-03-15 10:11:03 +13:00
|
|
|
|
2021-03-17 03:12:37 +13:00
|
|
|
self.mini_layout = QHBoxLayout()
|
2021-02-18 05:46:03 +13:00
|
|
|
self.prog_bar = QProgressBar()
|
2021-03-15 10:11:03 +13:00
|
|
|
self.prog_bar.setMaximum(100)
|
2021-03-17 03:12:37 +13:00
|
|
|
self.mini_layout.addWidget(self.prog_bar)
|
|
|
|
|
|
|
|
self.kill_button = QPushButton(self.tr("Stop Download"))
|
|
|
|
self.kill_button.setDisabled(True)
|
|
|
|
self.kill_button.clicked.connect(self.stop_download)
|
2021-04-12 03:03:55 +12:00
|
|
|
self.mini_layout.addWidget(self.kill_button)
|
2021-03-17 03:12:37 +13:00
|
|
|
|
|
|
|
self.layout.addLayout(self.mini_layout)
|
2021-02-18 05:46:03 +13:00
|
|
|
|
2021-04-08 02:50:36 +12:00
|
|
|
self.queue_widget = DlQueueWidget()
|
|
|
|
self.layout.addWidget(self.queue_widget)
|
|
|
|
self.queue_widget.update_list.connect(self.update_dl_queue)
|
2021-04-08 00:46:51 +12:00
|
|
|
|
2021-04-08 04:18:50 +12:00
|
|
|
self.updates = QGroupBox(self.tr("Updates"))
|
|
|
|
self.updates.setObjectName("group")
|
|
|
|
self.update_layout = QVBoxLayout()
|
2021-03-17 09:25:07 +13:00
|
|
|
self.update_widgets = {}
|
2021-03-01 00:06:33 +13:00
|
|
|
|
2021-04-08 04:18:50 +12:00
|
|
|
self.update_text = QLabel(self.tr("No updates available"))
|
|
|
|
self.update_text.setVisible(len(updates) == 0)
|
|
|
|
self.update_layout.addWidget(self.update_text)
|
|
|
|
|
|
|
|
for igame in updates:
|
|
|
|
widget = UpdateWidget(core, igame)
|
|
|
|
self.update_widgets[igame.app_name] = widget
|
|
|
|
self.update_layout.addWidget(widget)
|
|
|
|
widget.update.connect(self.update_game)
|
2021-04-13 20:56:42 +12:00
|
|
|
if QSettings().value("auto_update", False, bool):
|
2021-04-14 05:08:49 +12:00
|
|
|
self.update_game(igame.app_name, True)
|
2021-04-08 04:18:50 +12:00
|
|
|
|
|
|
|
self.updates.setLayout(self.update_layout)
|
|
|
|
self.layout.addWidget(self.updates)
|
2021-02-18 05:46:03 +13:00
|
|
|
self.layout.addStretch(1)
|
|
|
|
|
|
|
|
self.setLayout(self.layout)
|
|
|
|
|
2021-04-08 02:50:36 +12:00
|
|
|
def update_dl_queue(self, dl_queue):
|
|
|
|
self.dl_queue = dl_queue
|
|
|
|
|
2021-03-17 03:12:37 +13:00
|
|
|
def stop_download(self):
|
2021-04-12 03:03:55 +12:00
|
|
|
self.thread.kill()
|
2021-03-17 03:12:37 +13:00
|
|
|
|
2021-04-13 20:56:42 +12:00
|
|
|
def install_game(self, options: InstallOptions, from_update=False):
|
2021-04-08 01:34:52 +12:00
|
|
|
|
2021-03-15 10:11:03 +13:00
|
|
|
status_queue = MPQueue()
|
|
|
|
try:
|
|
|
|
dlm, analysis, game, igame, repair, repair_file = self.core.prepare_download(
|
|
|
|
app_name=options.app_name,
|
|
|
|
base_path=options.path,
|
2021-04-05 21:53:33 +12:00
|
|
|
force=options.force,
|
2021-03-15 10:11:03 +13:00
|
|
|
no_install=options.download_only,
|
|
|
|
status_q=status_queue,
|
2021-03-17 03:12:37 +13:00
|
|
|
# max_shm=,
|
2021-03-15 10:11:03 +13:00
|
|
|
max_workers=options.max_workers,
|
2021-03-17 03:12:37 +13:00
|
|
|
# game_folder=,
|
|
|
|
# disable_patching=,
|
|
|
|
# override_manifest=,
|
|
|
|
# override_old_manifest=,
|
|
|
|
# override_base_url=,
|
|
|
|
# platform_override=,
|
|
|
|
# file_prefix_filter=,
|
|
|
|
# file_exclude_filter=,
|
|
|
|
# file_install_tag=,
|
|
|
|
# dl_optimizations=,
|
|
|
|
# dl_timeout=,
|
2021-03-15 10:11:03 +13:00
|
|
|
repair=options.repair,
|
2021-03-17 03:12:37 +13:00
|
|
|
# repair_use_latest=,
|
2021-04-05 21:53:33 +12:00
|
|
|
ignore_space_req=options.ignore_free_space,
|
2021-03-17 03:12:37 +13:00
|
|
|
# disable_delta=,
|
|
|
|
# override_delta_manifest=,
|
|
|
|
# reset_sdl=,
|
2021-03-15 10:11:03 +13:00
|
|
|
sdl_prompt=self.sdl_prompt)
|
|
|
|
except Exception as e:
|
|
|
|
QMessageBox.warning(self, self.tr("Error preparing download"),
|
|
|
|
str(e))
|
2021-03-09 05:20:28 +13:00
|
|
|
return
|
|
|
|
|
2021-02-18 05:46:03 +13:00
|
|
|
if not analysis.dl_size:
|
2021-03-01 08:01:15 +13:00
|
|
|
QMessageBox.information(self, "Warning", self.tr("Download size is 0. Game already exists"))
|
2021-02-18 05:46:03 +13:00
|
|
|
return
|
2021-04-08 01:34:52 +12:00
|
|
|
|
2021-02-18 05:46:03 +13:00
|
|
|
# Information
|
2021-04-13 20:56:42 +12:00
|
|
|
if not from_update:
|
|
|
|
if not InstallInfoDialog(dl_size=analysis.dl_size, install_size=analysis.install_size).get_accept():
|
|
|
|
return
|
2021-04-08 01:34:52 +12:00
|
|
|
|
|
|
|
if self.active_game is None:
|
2021-04-08 02:50:36 +12:00
|
|
|
self.start_installation(dlm, game, status_queue, igame, repair_file, options, analysis)
|
2021-04-08 01:34:52 +12:00
|
|
|
else:
|
2021-04-08 02:50:36 +12:00
|
|
|
self.dl_queue.append((dlm, game, status_queue, igame, repair_file, options, analysis))
|
|
|
|
self.queue_widget.update_queue(self.dl_queue)
|
2021-02-18 05:46:03 +13:00
|
|
|
|
2021-04-08 02:50:36 +12:00
|
|
|
def start_installation(self, dlm, game, status_queue, igame, repair_file, options: InstallOptions, analysis):
|
|
|
|
print("start installation", game.app_title)
|
2021-04-13 00:24:10 +12:00
|
|
|
if self.dl_queue:
|
|
|
|
self.dl_queue.pop(0)
|
|
|
|
self.queue_widget.update_queue(self.dl_queue)
|
2021-02-20 00:57:55 +13:00
|
|
|
self.active_game = game
|
2021-03-15 10:11:03 +13:00
|
|
|
self.thread = DownloadThread(dlm, self.core, status_queue, igame, options.repair, repair_file)
|
2021-02-18 05:46:03 +13:00
|
|
|
self.thread.status.connect(self.status)
|
2021-03-15 10:11:03 +13:00
|
|
|
self.thread.statistics.connect(self.statistics)
|
2021-02-18 05:46:03 +13:00
|
|
|
self.thread.start()
|
2021-03-17 03:12:37 +13:00
|
|
|
self.kill_button.setDisabled(False)
|
2021-04-12 07:02:56 +12:00
|
|
|
self.analysis = analysis
|
2021-04-08 00:46:51 +12:00
|
|
|
self.installing_game.setText(self.tr("Installing Game: ") + self.active_game.app_title)
|
2021-03-17 09:25:07 +13:00
|
|
|
|
2021-03-15 10:11:03 +13:00
|
|
|
def sdl_prompt(self, app_name: str = '', title: str = '') -> list:
|
|
|
|
sdl = QDialog()
|
|
|
|
sdl.setWindowTitle('Select Additional Downloads')
|
|
|
|
|
|
|
|
layout = QVBoxLayout(sdl)
|
|
|
|
sdl.setLayout(layout)
|
|
|
|
|
|
|
|
pack_list = QListWidget()
|
|
|
|
layout.addWidget(pack_list)
|
|
|
|
|
|
|
|
done = QPushButton(text='Done')
|
|
|
|
done.clicked.connect(sdl.accept)
|
|
|
|
layout.addWidget(done)
|
|
|
|
|
|
|
|
tags = ['']
|
|
|
|
if '__required' in games[app_name]:
|
|
|
|
tags.extend(games[app_name]['__required']['tags'])
|
|
|
|
|
|
|
|
# add available additional downloads to list
|
2021-03-17 03:12:37 +13:00
|
|
|
pack_list.addItems([tag + ': ' + info['name'] for tag, info in games[app_name].items() if tag != '__required'])
|
2021-03-15 10:11:03 +13:00
|
|
|
|
|
|
|
# enable checkboxes
|
|
|
|
for i in range(len(pack_list)):
|
|
|
|
item = pack_list.item(i)
|
|
|
|
item.setFlags(Qt.ItemIsUserCheckable | Qt.ItemIsEnabled)
|
|
|
|
item.setCheckState(Qt.Unchecked)
|
|
|
|
|
|
|
|
sdl.exec_()
|
|
|
|
|
|
|
|
# read checkboxes states
|
|
|
|
for i in range(len(pack_list)):
|
|
|
|
item = pack_list.item(i)
|
|
|
|
if item.checkState() == Qt.Checked:
|
|
|
|
tag = item.text().split(':')[0]
|
|
|
|
tags.extend(games[app_name][tag]['tags'])
|
|
|
|
|
|
|
|
return tags
|
|
|
|
|
2021-02-18 05:46:03 +13:00
|
|
|
def status(self, text):
|
|
|
|
if text == "dl_finished":
|
|
|
|
pass
|
|
|
|
elif text == "finish":
|
2021-04-06 21:00:37 +12:00
|
|
|
self.installing_game.setText(self.tr("Download finished. Reload library"))
|
2021-04-13 20:31:26 +12:00
|
|
|
if QSettings().value("notification", True, bool):
|
|
|
|
try:
|
|
|
|
from notifypy import Notify
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
logger.warning("No Notification Module found")
|
|
|
|
else:
|
|
|
|
notification = Notify()
|
|
|
|
notification.title = self.tr("Installation finished")
|
|
|
|
notification.message = self.tr("Finished Download of game {}").format(self.active_game.app_title)
|
|
|
|
notification.send()
|
2021-02-27 07:28:54 +13:00
|
|
|
# QMessageBox.information(self, "Info", "Download finished")
|
2021-03-25 23:49:05 +13:00
|
|
|
logger.info("Download finished: " + self.active_game.app_title)
|
2021-04-08 00:46:51 +12:00
|
|
|
|
2021-04-08 02:50:36 +12:00
|
|
|
if self.dl_queue:
|
|
|
|
if self.dl_queue[0][1] == self.active_game.app_name:
|
|
|
|
self.dl_queue.pop(0)
|
|
|
|
self.queue_widget.update_queue(self.dl_queue)
|
2021-04-08 00:46:51 +12:00
|
|
|
|
2021-03-19 04:00:43 +13:00
|
|
|
if self.active_game.app_name in self.update_widgets.keys():
|
|
|
|
self.update_widgets[self.active_game.app_name].setVisible(False)
|
|
|
|
self.update_widgets.pop(self.active_game.app_name)
|
2021-04-09 21:36:27 +12:00
|
|
|
if len(self.update_widgets) == 0:
|
|
|
|
self.update_text.setVisible(True)
|
2021-03-17 09:25:07 +13:00
|
|
|
|
2021-04-08 00:46:51 +12:00
|
|
|
self.active_game = None
|
|
|
|
|
2021-03-17 09:25:07 +13:00
|
|
|
for i in self.update_widgets.values():
|
|
|
|
i.update_button.setDisabled(False)
|
|
|
|
|
2021-04-13 00:24:10 +12:00
|
|
|
self.finished.emit(True)
|
2021-04-08 05:53:07 +12:00
|
|
|
self.reset_infos()
|
2021-04-08 00:46:51 +12:00
|
|
|
|
|
|
|
if len(self.dl_queue) != 0:
|
2021-04-08 01:34:52 +12:00
|
|
|
self.start_installation(*self.dl_queue[0])
|
2021-04-08 00:46:51 +12:00
|
|
|
else:
|
2021-04-08 02:50:36 +12:00
|
|
|
self.queue_widget.update_queue(self.dl_queue)
|
2021-04-08 00:46:51 +12:00
|
|
|
|
2021-04-12 03:03:55 +12:00
|
|
|
elif text[:5] == "error":
|
|
|
|
QMessageBox.warning(self, "warn", "Download error: "+text[6:])
|
2021-02-18 05:46:03 +13:00
|
|
|
|
2021-03-17 03:12:37 +13:00
|
|
|
elif text == "stop":
|
2021-04-08 05:53:07 +12:00
|
|
|
self.reset_infos()
|
2021-04-12 03:03:55 +12:00
|
|
|
self.active_game = None
|
2021-04-13 00:24:10 +12:00
|
|
|
self.finished.emit(False)
|
|
|
|
if self.dl_queue:
|
|
|
|
self.start_installation(*self.dl_queue[0])
|
2021-04-08 05:53:07 +12:00
|
|
|
|
|
|
|
def reset_infos(self):
|
|
|
|
self.kill_button.setDisabled(True)
|
|
|
|
self.installing_game.setText(self.tr("Installing Game: No active download"))
|
|
|
|
self.prog_bar.setValue(0)
|
|
|
|
self.dl_speed.setText("")
|
|
|
|
self.time_left.setText("")
|
|
|
|
self.cache_used.setText("")
|
|
|
|
self.downloaded.setText("")
|
2021-04-12 07:02:56 +12:00
|
|
|
self.analysis = None
|
2021-03-17 03:12:37 +13:00
|
|
|
|
2021-03-15 10:11:03 +13:00
|
|
|
def statistics(self, ui_update: UIUpdate):
|
|
|
|
self.prog_bar.setValue(ui_update.progress)
|
2021-03-17 03:12:37 +13:00
|
|
|
self.dl_speed.setText(self.tr("Download speed") + f": {ui_update.download_speed / 1024 / 1024:.02f}MB/s")
|
|
|
|
self.cache_used.setText(self.tr("Cache used") + f": {ui_update.cache_usage / 1024 / 1024:.02f}MB")
|
2021-04-12 07:02:56 +12:00
|
|
|
self.downloaded.setText(self.tr("Downloaded") + f": {get_size(ui_update.total_downloaded)} / {get_size(self.analysis.dl_size)}")
|
2021-03-27 04:09:42 +13:00
|
|
|
self.time_left.setText(self.tr("Time left: ") + self.get_time(ui_update.estimated_time_left))
|
|
|
|
|
|
|
|
def get_time(self, seconds: int) -> str:
|
|
|
|
return str(datetime.timedelta(seconds=seconds))
|
2021-03-15 10:11:03 +13:00
|
|
|
|
2021-04-14 05:08:49 +12:00
|
|
|
def update_game(self, app_name: str, auto=False):
|
2021-04-08 00:46:51 +12:00
|
|
|
logger.info("Update " + app_name)
|
2021-04-14 05:08:49 +12:00
|
|
|
if not auto:
|
|
|
|
infos = InstallDialog(app_name, self.core, True).get_information()
|
|
|
|
else:
|
|
|
|
self.install_game(InstallOptions(app_name=app_name), True)
|
|
|
|
return
|
2021-04-05 21:53:33 +12:00
|
|
|
if infos != 0:
|
|
|
|
path, max_workers, force, ignore_free_space = infos
|
|
|
|
self.install_game(InstallOptions(app_name=app_name, max_workers=max_workers, path=path,
|
2021-04-13 20:56:42 +12:00
|
|
|
force=force, ignore_free_space=ignore_free_space), True)
|
2021-03-09 05:20:28 +13:00
|
|
|
|
2021-03-01 00:06:33 +13:00
|
|
|
|
|
|
|
class UpdateWidget(QWidget):
|
|
|
|
update = pyqtSignal(str)
|
|
|
|
|
2021-03-25 23:49:05 +13:00
|
|
|
def __init__(self, core: LegendaryCore, game: InstalledGame):
|
2021-03-01 00:06:33 +13:00
|
|
|
super(UpdateWidget, self).__init__()
|
|
|
|
self.core = core
|
2021-03-25 23:49:05 +13:00
|
|
|
self.game = game
|
2021-03-01 00:06:33 +13:00
|
|
|
|
|
|
|
self.layout = QVBoxLayout()
|
|
|
|
self.title = QLabel(self.game.title)
|
|
|
|
self.layout.addWidget(self.title)
|
|
|
|
|
2021-03-01 08:01:15 +13:00
|
|
|
self.update_button = QPushButton(self.tr("Update Game"))
|
2021-04-10 08:40:27 +12:00
|
|
|
self.update_button.clicked.connect(self.update_game)
|
2021-03-01 00:06:33 +13:00
|
|
|
self.layout.addWidget(self.update_button)
|
|
|
|
|
|
|
|
self.setLayout(self.layout)
|
2021-04-10 08:40:27 +12:00
|
|
|
|
|
|
|
def update_game(self):
|
|
|
|
self.update_button.setDisabled(True)
|
|
|
|
self.update.emit(self.game.app_name)
|