Skip to content

Commit 0c41845

Browse files
authored
vkd3d-proton: Refactor to use download_file (#526)
1 parent 69fa871 commit 0c41845

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

pupgui2/resources/ctmods/ctmod_vkd3dproton.py

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66
import requests
77

88
from PySide6.QtCore import QObject, QCoreApplication, Signal, Property
9+
from PySide6.QtWidgets import QMessageBox
10+
911

1012
from pupgui2.datastructures import Launcher
13+
from pupgui2.networkutil import download_file
1114
from pupgui2.util import extract_tar, extract_tar_zst, get_launcher_from_installdir
1215
from pupgui2.util import build_headers_with_authorization, fetch_project_release_data, fetch_project_releases
1316

@@ -24,6 +27,7 @@ class CtInstaller(QObject):
2427

2528
p_download_progress_percent = 0
2629
download_progress_percent = Signal(int)
30+
message_box_message = Signal((str, str, QMessageBox.Icon))
2731

2832
def __init__(self, main_window = None):
2933
super(CtInstaller, self).__init__()
@@ -48,35 +52,29 @@ def __set_download_progress_percent(self, value : int):
4852
self.p_download_progress_percent = value
4953
self.download_progress_percent.emit(value)
5054

51-
def __download(self, url, destination):
55+
def __download(self, url: str, destination: str) -> bool:
5256
"""
5357
Download files from url to destination
5458
Return Type: bool
5559
"""
60+
5661
try:
57-
file = self.rs.get(url, stream=True)
58-
except OSError:
59-
return False
62+
return download_file(
63+
url=url,
64+
destination=os.path.expanduser(destination),
65+
progress_callback=self.__set_download_progress_percent,
66+
download_cancelled=self.download_canceled,
67+
)
68+
except Exception as e:
69+
print(f"Failed to download tool {CT_NAME} - Reason: {e}")
70+
71+
self.message_box_message.emit(
72+
self.tr("Download Error!"),
73+
self.tr("Failed to download tool '{CT_NAME}'!\n\nReason: {EXCEPTION}".format(CT_NAME=CT_NAME, EXCEPTION=e)),
74+
QMessageBox.Icon.Warning
75+
)
6076

61-
self.__set_download_progress_percent(1) # 1 download started
62-
f_size = int(file.headers.get('content-length'))
63-
c_count = int(f_size / self.BUFFER_SIZE)
64-
c_current = 1
65-
destination = os.path.expanduser(destination)
66-
os.makedirs(os.path.dirname(destination), exist_ok=True)
67-
with open(destination, 'wb') as dest:
68-
for chunk in file.iter_content(chunk_size=self.BUFFER_SIZE):
69-
if self.download_canceled:
70-
self.download_canceled = False
71-
self.__set_download_progress_percent(-2) # -2 download canceled
72-
return False
73-
if chunk:
74-
dest.write(chunk)
75-
dest.flush()
76-
self.__set_download_progress_percent(int(min(c_current / c_count * 98.0, 98.0))) # 1-98, 100 after extract
77-
c_current += 1
78-
self.__set_download_progress_percent(99) # 99 download complete
79-
return True
77+
return False
8078

8179
def __fetch_github_data(self, tag):
8280
"""

0 commit comments

Comments
 (0)