-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
679 lines (615 loc) · 33 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
import os
import subprocess
import sys
import requests
import json
from PyQt6.QtWidgets import QMainWindow, QApplication, QLabel, QPushButton, QVBoxLayout, QWidget, QCheckBox, QStackedLayout, QHBoxLayout, QGroupBox, QComboBox, QProgressBar, QLineEdit, QMessageBox, QFileDialog, QInputDialog, QTreeWidget, QTreeWidgetItem
from PyQt6.QtGui import QPixmap, QIcon, QImage
from PyQt6.QtCore import Qt, QThread, pyqtSignal, QByteArray, QFile, pyqtSlot
from zipfile import ZipFile
import shutil
import tempfile
from icons import styledark_rc
import win32com.client
import winreg
from stylesheet import Style
from pathlib import Path
def get_latest_git_tag():
tag = "1.0"
github_token = os.getenv("GITHUB_TOKEN", "")
try:
command = f"GH_TOKEN={github_token} gh release list --limit 1 --json tagName --jq '.[0].tagName'"
process = subprocess.Popen(['bash', '-c', command], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if process.returncode == 0:
tag = out.decode('utf-8').strip()
if tag.startswith("v"):
tag = tag[1:]
else:
print(f"Failed to get latest GitHub release tag: {err.decode('utf-8')}")
except Exception as e:
print(f"Failed to get latest GitHub release tag: {e}")
return tag
class QtUi(QMainWindow, Style):
def __init__(self):
super().__init__()
self.logic = Logic()
self.logic.fetch_google_sheet_data()
# Show a message box indicating that Troppical is starting
self.loading_message_box = QMessageBox(self)
self.loading_message_box.setIcon(QMessageBox.Icon.Information)
self.loading_message_box.setText("Troppical is starting.... If you get a notifcation that Troppical is not responding, ignore it.")
self.loading_message_box.setWindowModality(Qt.WindowModality.ApplicationModal)
close_button = self.loading_message_box.addButton(QMessageBox.StandardButton.Close)
close_button.hide()
self.loading_message_box.show()
QApplication.processEvents()
self.ui() # Init UI
self.load_stylesheet()
# Logo Header
def Header(self):
# Header Layout
self.headerLayout = QVBoxLayout()
self.headerLayout.setContentsMargins(0, 20, 0, 0)
# Icon Widget
iconLabel = QLabel()
icon_path = os.path.join(sys._MEIPASS, 'icon.ico')
icon = QIcon(icon_path)
pixmap = icon.pixmap(180, 180) # Specify the size directly
iconLabel.setPixmap(pixmap)
# Text Widget
label = QLabel("<b><font size='10'>Troppical</font></b>")
# Set Widgets
self.headerLayout.addWidget(iconLabel, alignment=Qt.AlignmentFlag.AlignCenter)
self.headerLayout.addWidget(label, alignment=Qt.AlignmentFlag.AlignCenter)
return self.headerLayout
# Widgets
def ui(self):
# Init Window
self.setWindowTitle(f'Troppical - {version}') # Window name with version
self.setCentralWidget(QWidget(self)) # Set a central widget
self.layout = QStackedLayout(self.centralWidget()) # Set the layout on the central widget
self.setMaximumSize(1000, 720) # Set the maximum window size to 1280x720
self.setMinimumSize(1000, 720) # Set the minimum window size to 800x600
# Set the window icon
icon_path = os.path.join(sys._MEIPASS, 'icon.ico')
self.setWindowIcon(QIcon(icon_path))
self.selection_page()
# Emulator Select Page
def selection_page(self):
self.emulatorSelectPage = QWidget()
emulatorSelectLayout = QVBoxLayout()
emulatorSelectGroup = QGroupBox("Select your emulator from the list")
emulatorSelectGroupLayout = QVBoxLayout()
# Create a QTreeWidget
self.emulatorTreeWidget = QTreeWidget()
self.emulatorTreeWidget.setHeaderHidden(True)
self.emulatorTreeWidget.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOn)
emulatorSelectGroupLayout.addWidget(self.emulatorTreeWidget)
# Keep track of emulator systems
system_items = {}
for troppical_api_data in self.logic.troppical_api:
emulator_system = troppical_api_data['emulator_system']
emulator_name = troppical_api_data['emulator_name']
emulator_desc = troppical_api_data.get('emulator_desc', '')
# Fetch and decode the logo
logo_url = troppical_api_data['emulator_logo']
response = requests.get(logo_url)
if response.status_code == 200:
image_bytes = response.content
qimage = QImage.fromData(QByteArray(image_bytes))
pixmap = QPixmap.fromImage(qimage).scaled(32, 32, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
icon = QIcon(pixmap)
print(logo_url)
else:
QMessageBox.critical(self, "Failed to fetch logo", f"Failed to fetch logo for {emulator_name}. Status code: {response.status_code}")
icon = QIcon()
# Check if the emulator system already has a tree item, if not create one
if emulator_system not in system_items:
system_item = QTreeWidgetItem(self.emulatorTreeWidget)
system_item.setText(0, emulator_system)
system_item.setExpanded(True) # Uncollapse the category by default
system_items[emulator_system] = system_item
else:
system_item = system_items[emulator_system]
# Add the emulator to the appropriate tree item
emulator_item = QTreeWidgetItem(system_item)
emulator_item.setText(0, emulator_name)
emulator_item.setIcon(0, icon)
emulator_item.setToolTip(0, emulator_desc)
# Sort the systems and emulators alphabetically
self.emulatorTreeWidget.sortItems(0, Qt.SortOrder.AscendingOrder)
for i in range(self.emulatorTreeWidget.topLevelItemCount()):
system_item = self.emulatorTreeWidget.topLevelItem(i)
system_item.sortChildren(0, Qt.SortOrder.AscendingOrder)
# Set layout for the group and add to the main layout
emulatorSelectGroup.setLayout(emulatorSelectGroupLayout)
emulatorSelectLayout.addWidget(emulatorSelectGroup)
self.emulatorSelectPage.setLayout(emulatorSelectLayout) # Set the layout for the emulator selection page
self.layout.addWidget(self.emulatorSelectPage)
# Next button to confirm selection
self.nextButton = QPushButton("Next")
self.nextButton.clicked.connect(self.logic.set_emulator)
emulatorSelectLayout.addWidget(self.nextButton)
# Welcome page
self.welcomePage = QWidget()
## Layout and gorup
welcomerLayout = QVBoxLayout()
welcomerGroup = QGroupBox("")
welcomerGroupLayout = QVBoxLayout()
buttonsLayout = QHBoxLayout()
welcomerGroup.setLayout(welcomerGroupLayout)
self.welcomePage.setLayout(welcomerLayout) # Set the welcomepage layout
## Widgets
self.welcomerLabel = QLabel()
self.welcomerLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.installButton = QPushButton('Install')
self.installButton.clicked.connect(self.logic.qt_button_click)
self.updateButton = QPushButton('Check for Updates')
self.updateButton.clicked.connect(self.logic.qt_button_click)
self.uninstallButton = QPushButton('Uninstall')
self.uninstallButton.clicked.connect(self.logic.qt_button_click)
self.backButton = QPushButton("Back")
self.backButton.setFixedSize(80, 30)
self.backButton.clicked.connect(self.logic.qt_button_click)
## Add widgets / layouts
welcomerLayout.addLayout(self.Header())
welcomerLayout.addWidget(welcomerGroup)
welcomerGroupLayout.addWidget(self.welcomerLabel)
buttonsLayout.addWidget(self.installButton)
buttonsLayout.addWidget(self.updateButton)
buttonsLayout.addWidget(self.uninstallButton)
welcomerLayout.addLayout(buttonsLayout)
welcomerLayout.insertWidget(0, self.backButton)
## Add the welcome page to the layout
self.layout.addWidget(self.welcomePage)
# Install page
self.installPage = QWidget()
## Layout and gorup
installLayout = QVBoxLayout()
checkboxLayout = QVBoxLayout()
checkboxGroup = QGroupBox("Do you want to create shortcuts?") # Checkboxes
checkboxGroup.setLayout(checkboxLayout) # Set the layout of Checkboxes
pathSelectorLayout = QHBoxLayout() # Browse widget layout
self.installPage.setLayout(installLayout) # Set the installpage layout
## Widgets
InstalOpt = QLabel('<b>Installation Options')
InstalOpt.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.installationSourceComboBox = QComboBox() # Dropdown menu
self.desktopShortcutCheckbox = QCheckBox("Create a desktop shortcut") # Checkboxes
self.startMenuShortcutCheckbox = QCheckBox("Create a start menu shortcut")
self.installationPathLineEdit = QLineEdit() # Browse for installation path widget
self.browseButton = QPushButton("Browse")
self.browseButton.clicked.connect(self.logic.InstallPath)
self.install_emu_button = QPushButton('Install') # Install button
self.install_emu_button.clicked.connect(self.logic.qt_button_click)
## Add widgets / layouts
installLayout.addLayout(self.Header()) ### Icon self.header
installLayout.addWidget(InstalOpt) ### Instalation Option Label
installLayout.addWidget(self.installationSourceComboBox) ## Install Sorce Widget
pathSelectorLayout.addWidget(self.installationPathLineEdit) ## Browse Widget
pathSelectorLayout.addWidget(self.browseButton)
installLayout.addLayout(pathSelectorLayout)
checkboxLayout.addWidget(self.desktopShortcutCheckbox) # Checkboxes
checkboxLayout.addWidget(self.startMenuShortcutCheckbox)
installLayout.addWidget(checkboxGroup)
installLayout.addWidget(self.install_emu_button)
## Add the install page to the layout
self.layout.addWidget(self.installPage)
# Progress bar page
self.progressBarPage = QWidget()
## Layout and groups
progressBarLayout = QVBoxLayout()
self.progressBarPage.setLayout(progressBarLayout)
## Widgets
self.downloadProgressBar = QProgressBar()
self.downloadProgressBar.setRange(0, 100) # Progress bar Widgets
self.extractionProgressBar = QProgressBar()
self.extractionProgressBar.setRange(0, 100)
self.labeldown = QLabel("Downloading: ")
self.labelext = QLabel("Extracting: ")
## Add widgets / layouts
progressBarLayout.addLayout(self.Header()) # Add the icon self.header
progressBarLayout.addWidget(self.labeldown)
progressBarLayout.addWidget(self.downloadProgressBar)
progressBarLayout.addWidget(self.labelext)
progressBarLayout.addWidget(self.extractionProgressBar)
# Add the progress bar page to the layout
self.layout.addWidget(self.progressBarPage)
# Finish page
self.finishPage = QWidget()
## Layout and groups
finishLayout = QVBoxLayout()
self.finishPage.setLayout(finishLayout)
## Widgets
finishLabel = QLabel("<b>Installation Complete!")
finishLabel.setAlignment(Qt.AlignmentFlag.AlignCenter) # Center the text horizontally
finishButton = QPushButton("Finish")
finishButton.clicked.connect(self.close)
installAnotherButton = QPushButton("Install another emulator")
installAnotherButton.clicked.connect(lambda: (
self.layout.setCurrentIndex(0),
self.downloadProgressBar.setValue(0),
self.extractionProgressBar.setValue(0),
setattr(self.logic, 'install_mode', None),
setattr(self.logic, 'selection', None)
))
## Add widgets / layouts
finishLayout.addLayout(self.Header()) # Add the icon self.header
finishLayout.addWidget(finishLabel)
# Create a horizontal layout for the buttons
buttonLayout = QHBoxLayout()
buttonLayout.addWidget(finishButton)
buttonLayout.addWidget(installAnotherButton)
finishLayout.addLayout(buttonLayout)
# Add the progress bar page to the layout
self.layout.addWidget(self.finishPage)
self.loading_message_box.close()
def load_stylesheet(app):
app.setStyleSheet(Style.dark_stylesheet)
class Logic:
def __init__(self):
self.regvalue = None
self.install_mode = None
self.emulator = None
def fetch_google_sheet_data(self):
url = "https://script.googleusercontent.com/macros/echo?user_content_key=Hw-G9S_OHELhOUAsT-oQr8ux2HPMIpva3U1w0Su7P1ZYrr1ngXyqlN6LBhfev1taFoRtJ07w_KDhWVbMaBaeJ3c86H4e0k8Xm5_BxDlH2jW0nuo2oDemN9CCS2h10ox_1xSncGQajx_ryfhECjZEnL69XsVZDOhipZMwrhs3JioNozSVnp4Chm6SveAF_nlUSMgTaOh-zk0bQ5F9LtyaiRZKic-heYuYVV866SySaVfv-0TkTPKcCtz9Jw9Md8uu&lib=MmjrdpKGbUxdyxLDAqWkoFhoZjK-0W8qS"
response = requests.get(url)
if response.status_code == 200:
all_data = response.json()
self.troppical_api = [item for item in all_data if item.get('emulator_platform') != 'android']
return self.troppical_api
else:
print("Failed to fetch data:", response.status_code)
# Set which emulator to use for the installer depeanding on the selected emulator
def set_emulator(self):
selected_item = qtui.emulatorTreeWidget.currentItem()
if not selected_item or not selected_item.parent():
QMessageBox.warning(qtui, "Selection Error", "Please select an emulator.")
return
emulator_name = selected_item.text(0)
if self.emulator != emulator_name:
# Clear previous emulator settings
qtui.labeldown.setText("Downloading: ")
qtui.labelext.setText("Extracting: ")
qtui.welcomerLabel.setText("")
# Set new emulator
self.emulator = emulator_name
for selected_emulator in self.troppical_api:
if selected_emulator['emulator_name'] == self.emulator:
self.releases_url = f"https://api.github.com/repos/{selected_emulator['emulator_owner']}/{selected_emulator['emulator_repo']}/releases"
# Update UI components with new emulator settings
reg_result = self.checkreg()
installed_emulator = "Not Installed" if reg_result is None else reg_result[1]
qtui.installationPathLineEdit.setText(os.path.join(os.environ['LOCALAPPDATA'], self.emulator))
qtui.labeldown.setText("Downloading: " + self.emulator)
qtui.labelext.setText("Extracting: " + self.emulator)
qtui.welcomerLabel.setText(f'<big>Your currently selected emulator is <b>{self.emulator}</b> and current version is <b>{installed_emulator}</b>.</big>')
print (self.emulator)
self.checkreg()
self.disable_qt_buttons_if_installed()
qtui.layout.setCurrentIndex(1)
# Disable buttons depanding on if it the program is already installed
def disable_qt_buttons_if_installed(self):
regvalue = self.checkreg()
if regvalue is None:
qtui.installButton.setEnabled(True)
qtui.updateButton.setEnabled(False)
qtui.uninstallButton.setEnabled(False)
else:
qtui.installButton.setEnabled(False)
qtui.updateButton.setEnabled(True)
qtui.uninstallButton.setEnabled(True)
# Button clinking function
def qt_button_click(self):
button = qtui.sender()
if button is qtui.installButton:
self.install_mode = "Install"
qtui.layout.setCurrentIndex(2)
self.Add_releases_to_combobox()
elif button is qtui.updateButton:
self.emulator_updates()
elif button is qtui.uninstallButton:
self.install_mode = "Uninstall" # Unused for now
self.uninstall()
if button is qtui.install_emu_button:
qtui.layout.setCurrentIndex(3)
self.Prepare_Download()
if button is qtui.backButton:
current_index = qtui.layout.currentIndex()
if current_index > 0:
qtui.layout.setCurrentIndex(current_index - 1)
return self.install_mode
# Select installation path function
def InstallPath(self):
self.Install_Dir = qtui.installationPathLineEdit.text()
selectedDirectory = QFileDialog.getExistingDirectory(qtui, "Select Installation Directory", qtui.installationPathLineEdit.text())
# Check if a directory was selected
if selectedDirectory:
# Set Emulator name to the selected directory path
self.Install_Dir = os.path.join(selectedDirectory, self.emulator)
qtui.installationPathLineEdit.setText(self.Install_Dir)
return self.Install_Dir
# Add the various version to the selection combobox
def Add_releases_to_combobox(self):
print (self.releases_url)
response = requests.get(self.releases_url)
self.releases = response.json()[:5]
# Clear the combo box before adding new items
qtui.installationSourceComboBox.clear()
# Remove or add items based on the emulator
for release in self.releases:
qtui.installationSourceComboBox.addItem(release['tag_name'])
# Update button function
def emulator_updates(self):
self.checkreg() # Initialise the reg value function
installed_emulator = self.updatevalue
response = requests.get(self.releases_url + "/latest")
latest_release = response.json()
latest_tag = latest_release['tag_name']
# Check for specific emulators that use a rolling-release
if self.emulator in ['Vita3K', 'NooDS']:
reply = QMessageBox.question(qtui, "Rolling-Release Emulator Detected", f"{self.emulator} uses rolling-releases instead of numbered releases. This means that the latest version may not be the one you have installed (We cannot detect the version). Would you like to proceed with the download anyway?", QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
if reply == QMessageBox.StandardButton.Yes:
self.install_mode = "Update"
qtui.layout.setCurrentIndex(3)
self.Prepare_Download()
return
else:
pass
if latest_tag > installed_emulator:
reply = QMessageBox.question(qtui, "Update Found", "Would you like to update " + self.emulator + " to " + latest_tag + "?", QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
if reply == QMessageBox.StandardButton.Yes:
self.install_mode = "Update"
qtui.layout.setCurrentIndex(3)
self.Prepare_Download()
else:
pass
else:
QMessageBox.information(qtui, "No Update Found", f"You are up to date with the latest version of {self.emulator}", QMessageBox.StandardButton.Ok)
# Preparing which file to downlaod
def Prepare_Download(self):
print (self.install_mode)
reg_key = self.checkreg()
if reg_key is not None:
UpdateChannelValue = reg_key[1]
else:
UpdateChannelValue = None
self.selection = qtui.installationSourceComboBox.currentText()
if self.install_mode == "Install":
response = requests.get(self.releases_url)
releases = response.json()
for release in releases:
if release['tag_name'] == self.selection:
windows_assets = [asset for asset in release['assets'] if ('_win' in asset['name'].lower() or 'win' in asset['name'].lower() or 'xenia' in asset['name'].lower()) and asset['name'].endswith('.zip') and not asset['name'].endswith('.7z')]
if len(windows_assets) > 1:
options = "\n".join([f"{idx + 1}: {asset['name']}" for idx, asset in enumerate(windows_assets)])
choice, ok = QInputDialog.getItem(qtui, "Select Version", "Multiple Windows versions found. Please select one:\n" + options, [asset['name'] for asset in windows_assets], 0, False)
if ok:
self.selected_asset = next(asset for asset in windows_assets if asset['name'] == choice)
self.selected_asset_name = self.selected_asset['name']
print(self.selected_asset_name)
self.target_download = self.selected_asset['browser_download_url']
self.url = self.target_download # url for the download thread
self.Download_Emulator()
self.createreg()
else:
sys.exit("No release selected. Exiting.")
elif len(windows_assets) == 1:
self.selected_asset_name = windows_assets[0]['name']
self.target_download = windows_assets[0]['browser_download_url']
print(self.target_download)
self.url = self.target_download # url for the download thread
self.Download_Emulator()
self.createreg()
else:
QMessageBox.critical(qtui, "Error", f"No suitable Windows download found for {self.emulator} {self.selection}. Please try another release.")
qtui.layout.setCurrentIndex(2)
elif self.install_mode == "Update":
response = requests.get(self.releases_url + "/latest")
latest_release = response.json()
windows_assets = [asset for asset in latest_release['assets'] if ('_win' in asset['name'].lower() or 'win' in asset['name'].lower()) and asset['name'].endswith('.zip') and not asset['name'].endswith('.7z')]
if windows_assets:
if len(windows_assets) > 1:
options = "\n".join([f"{idx + 1}: {asset['name']}" for idx, asset in enumerate(windows_assets)])
choice, ok = QInputDialog.getItem(qtui, "Select Version", "Multiple Windows versions found. Please select one:\n" + options, [asset['name'] for asset in windows_assets], 0, False)
if ok:
latest_asset = next(asset for asset in windows_assets if asset['name'] == choice)
else:
sys.exit("No release selected. Exiting.")
else:
latest_asset = windows_assets[0]
self.target_download = latest_asset['browser_download_url']
self.url = self.target_download # url for the download thread
print(self.url)
self.Download_Emulator()
self.selection = latest_release['tag_name']
self.createreg()
else:
QMessageBox.critical(qtui, "Error", f"No suitable Windows download found for {self.emulator} in the latest release. Please try another release or check for updates.")
# Download function
def Download_Emulator(self):
temp_file = tempfile.NamedTemporaryFile(delete=False).name
if self.install_mode == "Install":
self.installationPath = qtui.installationPathLineEdit.text()
elif self.install_mode == "Update":
self.installationPath = self.regvalue
os.makedirs(self.installationPath, exist_ok=True)
# Threads
self.download_thread = QThread()
self.download_worker = DownloadWorker(self.url, temp_file)
self.download_worker.moveToThread(self.download_thread)
self.download_thread.started.connect(self.download_worker.do_download)
self.download_worker.progress.connect(qtui.downloadProgressBar.setValue)
self.download_thread.start()
self.download_worker.finished.connect(self.on_download_finished)
self.download_worker.finished.connect(self.download_thread.quit)
self.download_worker.finished.connect(self.download_worker.deleteLater)
self.download_thread.finished.connect(self.download_thread.deleteLater)
def on_download_finished(self):
self.extract_and_install(self.download_worker.dest, self.installationPath)
# Extract and install function
def extract_and_install(self, temp_file, extract_to):
extract_to = self.installationPath
# Rename the temporary file to have a .zip extension and create a temporary extraction folder
zip_file_path = f"{temp_file}.zip"
os.rename(temp_file, zip_file_path)
self.temp_extract_folder = tempfile.mkdtemp()
with ZipFile(zip_file_path, 'r') as emu_zip:
emu_zip.extractall(self.temp_extract_folder)
# Check for nested zip files and extract them
nested_zips = [f for f in emu_zip.namelist() if f.endswith('.zip')]
for nested_zip in nested_zips:
nested_zip_path = os.path.join(self.temp_extract_folder, nested_zip)
with ZipFile(nested_zip_path, 'r') as nested_zip_file:
nested_zip_file.extractall(self.temp_extract_folder)
os.remove(nested_zip_path)
os.remove(zip_file_path)
self.move_files(extract_to)
def move_files(self, extract_to):
# Find the first directory containing an executable
for root, dirs, files in os.walk(self.temp_extract_folder):
if any(file.endswith('.exe') for file in files):
src_path = root
break
else:
src_path = self.temp_extract_folder # Fallback if no .exe is found
dest_path = extract_to
os.makedirs(dest_path, exist_ok=True)
for item in os.listdir(src_path):
src_item_path = os.path.join(src_path, item)
dest_item_path = os.path.join(dest_path, item)
if os.path.isdir(src_item_path):
shutil.copytree(src_item_path, dest_item_path, dirs_exist_ok=True)
else:
shutil.copy2(src_item_path, dest_item_path)
# Mark the installation as complete
self.installation_complete()
# Install is complete
def installation_complete(self):
qtui.extractionProgressBar.setValue(100)
# Fetch the executable path from the troppical_api data
for troppical_api_data in self.troppical_api:
if troppical_api_data['emulator_name'] == self.emulator:
exe_name = troppical_api_data.get('exe_path', '')
executable_path = os.path.normpath(os.path.join(qtui.installationPathLineEdit.text(), exe_name))
print (executable_path)
if executable_path is None:
QMessageBox.critical(qtui, "Error", f"Executable path not found for emulator: {self.emulator}")
return
if qtui.desktopShortcutCheckbox.isChecked():
self.define_shortcut(executable_path, 'desktop')
if qtui.startMenuShortcutCheckbox.isChecked():
self.define_shortcut(executable_path, 'start_menu')
self.checkreg()
qtui.layout.setCurrentIndex(4)
# Function to check the reg values
def checkreg(self):
try:
self.registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, f"Software\\{self.emulator}", 0, winreg.KEY_READ)
self.regvalue, regtype = winreg.QueryValueEx(self.registry_key, 'Install_Dir')
self.updatevalue, regtype = winreg.QueryValueEx(self.registry_key, 'Version')
self.asset_version, regtype = winreg.QueryValueEx(self.registry_key, 'Asset_version')
winreg.CloseKey(self.registry_key)
return self.regvalue, self.updatevalue, self.asset_version
except FileNotFoundError:
pass
# Function to create the reg values
def createreg (self):
winreg.CreateKey(winreg.HKEY_CURRENT_USER, f"Software\\{self.emulator}")
self.registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, f"Software\\{self.emulator}", 0,
winreg.KEY_WRITE)
if self.install_mode == "Install":
winreg.SetValueEx(self.registry_key, 'Install_Dir', 0, winreg.REG_SZ, qtui.installationPathLineEdit.text())
winreg.SetValueEx(self.registry_key, 'Asset_version', 0, winreg.REG_SZ, self.selected_asset_name)
winreg.SetValueEx(self.registry_key, 'Version', 0, winreg.REG_SZ, self.selection)
winreg.CloseKey(self.registry_key)
# Function to create the shortcuts
def define_shortcut(self, target, location_type):
if location_type == 'desktop':
path = os.path.join(os.environ['USERPROFILE'], 'Desktop')
elif location_type == 'start_menu':
path = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu', 'Programs')
filename = f'{self.emulator}.lnk'
shortcut_path = os.path.join(path, filename)
# Verify the target exists
if not os.path.exists(target):
QMessageBox.critical(qtui, "Error", f"Shortcut target does not exist: {target}")
return
try:
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(shortcut_path)
shortcut.TargetPath = target
shortcut.WorkingDirectory = os.path.dirname(target)
shortcut.Description = ""
shortcut.Arguments = ""
shortcut.IconLocation = target # You can customize this if needed
shortcut.save()
except Exception as e:
QMessageBox.critical(qtui, "Error", f"Failed to create shortcut: {e}")
# Uninstall function
def uninstall(self):
self.checkreg()
print (self.regvalue)
if self.regvalue is not None:
# Paths to the shortcuts
desktopshortcut = os.path.join(os.environ['USERPROFILE'], 'Desktop', f'{self.emulator}.lnk')
startmenushortcut = os.path.join(os.environ['APPDATA'], 'Microsoft', 'Windows', 'Start Menu', 'Programs', f'{self.emulator}.lnk')
reply = QMessageBox.question(qtui, "Uninstall", f"Are you sure you want to uninstall {self.emulator} located on {self.regvalue}?", QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
if reply == QMessageBox.StandardButton.No:
return
else:
# Remove the shortcut and the, DIR and REG keys
if os.path.exists(desktopshortcut):
os.remove(desktopshortcut)
if os.path.exists(startmenushortcut):
os.remove(startmenushortcut)
dirpath = Path(self.regvalue)
if dirpath.exists() and dirpath.is_dir():
shutil.rmtree(dirpath)
winreg.DeleteKey(winreg.HKEY_CURRENT_USER, f"Software\\{self.emulator}")
QMessageBox.information(qtui, "Uninstall", f"{self.emulator} has been successfully uninstalled.")
self.emulator = None
qtui.layout.setCurrentIndex(0)
else:
QMessageBox.critical(qtui, "Error", "The directory might have been moved or deleted. Please reinstall the program.")
winreg.DeleteKey(winreg.HKEY_CURRENT_USER, f"Software\\{self.emulator}")
qtui.updateButton.setEnabled(False)
qtui.uninstallButton.setEnabled(False)
qtui.installButton.setEnabled(True)
else:
QMessageBox.critical(qtui, "Error",("Failed to read the registry key. Try and reinstall again!"))
qtui.layout.setCurrentIndex(1)
# Download Worker class to download the files
class DownloadWorker(QThread):
progress = pyqtSignal(int)
def __init__(self, url, dest):
super().__init__()
self.url = url
self.dest = dest
@pyqtSlot()
def do_download(self):
try:
response = requests.get(self.url, stream=True)
total_size = int(response.headers.get('content-length', 0))
if total_size == 0:
print("The content-length of the response is zero.")
return
downloaded_size = 0
with open(self.dest, 'wb') as file:
for data in response.iter_content(1024):
downloaded_size += len(data)
file.write(data)
progress_percentage = (downloaded_size / total_size) * 100
self.progress.emit(int(progress_percentage))
self.finished.emit()
except Exception as e:
QMessageBox.critical(QtUi, "Error",("Error doing download."))
self.finished.emit()
if __name__ == "__main__":
version = get_latest_git_tag()
app = QApplication(sys.argv)
qtui = QtUi()
qtui.show()
self = Logic()
sys.exit(app.exec())