-
Notifications
You must be signed in to change notification settings - Fork 3
/
widget_update.py
404 lines (310 loc) · 17.5 KB
/
widget_update.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
import gui_common
import widget_progress
import utils
import debug_actions
import constants
import arduboy.fxcart
# import main_cart
from arduboy.bloggingadeadhorse import *
import time
import logging
from PyQt6.QtWidgets import QPushButton, QLabel, QDialog, QVBoxLayout, QProgressBar, QMessageBox
from PyQt6.QtWidgets import QGroupBox, QListWidget, QHBoxLayout, QWidget, QCheckBox, QListWidgetItem
from PyQt6.QtCore import Qt, QThread, pyqtSignal
from widget_titleimage import TitleImageWidget
DEBUG_NETWORK_FILE = False
class UpdateWindow(QDialog):
def __init__(self, cartwindow): #: main_cart.CartWindow):
super().__init__(parent=cartwindow)
self.cartwindow = cartwindow
# The progress thing shows exception errors itself... I think
self.updateresult, self.original_slots = self.check_for_updates(cartwindow)
if not self.updateresult:
raise Exception("Failed to download update metadata!")
if len(self.updateresult[UPKEY_NEW]) + len(self.updateresult[UPKEY_UPDATES]) == 0:
# QMessageBox.information(self, "Update cancelled", "No updates found for your cart", QMessageBox.StandardButton.Ok)
raise Exception("No updates found for your cart")
self.setWindowTitle("Update Cart")
self.resize(800, 700)
layout = QVBoxLayout()
self.setLayout(layout)
updatebox = QGroupBox(f"Updates ({len(self.updateresult[UPKEY_UPDATES])})")
layout.addWidget(updatebox)
self.updatelist = self.make_basic_list(updatebox)
newbox = QGroupBox(f"New ({len(self.updateresult[UPKEY_NEW])})")
layout.addWidget(newbox)
self.newlist = self.make_basic_list(newbox)
updateinfo = QLabel(f"{len(self.updateresult[UPKEY_CURRENT])} up-to-date, {len(self.updateresult[UPKEY_UNMATCHED])} unmatched")
updateinfo.setStyleSheet(f"color: {gui_common.SUBDUEDCOLOR}")
updateinfo.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(updateinfo)
controls = QWidget()
controls_layout = QHBoxLayout()
controls.setLayout(controls_layout)
layout.addWidget(controls)
self.update_button = QPushButton("Update")
self.cancel_button = QPushButton("Cancel")
self.cancel_button.clicked.connect(self.close)
self.update_button.clicked.connect(self.do_update)
self.update_button.setStyleSheet("font-weight: bold")
controls_layout.addWidget(self.cancel_button)
controls_layout.addWidget(self.update_button)
for (original,update) in self.updateresult[UPKEY_UPDATES]:
self.add_selectable_listitem(self.updatelist, UpdateInfo(original, update))
for update in self.updateresult[UPKEY_NEW]:
self.add_selectable_listitem(self.newlist, NewInfo(update))
def check_for_updates(self, cartwindow):
# Connect to the semi-official cart builder website, download the json, and check which games need an update.
# Scan through all the non-category items and see how many don't have author + version + title information. If it's missing
# ANY of them, count it against the percentage
slots = cartwindow.get_slots()
check_update_slots = [s for s in slots if not s.is_category()]
cartmeta = None
updateresult = None
def do_work(repprog, repstatus):
nonlocal cartmeta
repstatus(f"Downloading metadata from\n{constants.OFFICIAL_CARTMETA_URL}")
cartmeta = gui_common.get_official_cartmeta(force = True)
if DEBUG_NETWORK_FILE:
with open("badh_last.json", "w") as f:
json.dump(cartmeta, f)
def do_work_update(repprog, repstatus):
nonlocal updateresult
updateresult = compute_update(check_update_slots, cartmeta, cartwindow.device_select.currentText())
if DEBUG_NETWORK_FILE:
with open("updateresult_last.json", "w") as f:
json.dump(updateresult, f, cls=CartMetaDecoder)
dialog = widget_progress.do_progress_work(do_work, f"Retrieving update data...", simple = True, unknown_progress=True)
if not dialog.error_state:
debug_actions.global_debug.add_action_str(f"Retrieved update master list from {constants.OFFICIAL_CARTMETA_URL}")
dialog = widget_progress.do_progress_work(do_work_update, f"Computing update data...", simple = True, unknown_progress=True)
if not dialog.error_state:
return updateresult,slots
return None,slots
def make_basic_list(self, box):
mlayout = QVBoxLayout()
listwidget = QListWidget(self)
mlayout.addWidget(listwidget)
box.setLayout(mlayout)
controls = QWidget()
controls_layout = QHBoxLayout()
controls_layout.setContentsMargins(0,0,0,0)
controls.setLayout(controls_layout)
mlayout.addWidget(controls)
select_none = QPushButton("Select None")
select_all = QPushButton("Select All")
select_none.clicked.connect(lambda: self.do_select(listwidget, False))
select_all.clicked.connect(lambda: self.do_select(listwidget, True))
controls_layout.addWidget(select_none)
controls_layout.addWidget(select_all)
return listwidget
def do_select(self, parent, selected):
for x in range(parent.count()):
widget = parent.itemWidget(parent.item(x)) #.get_slot_data() for x in range(self.list_widget.count())]
widget.checkbox.setChecked(selected)
def do_update(self):
# First, go collect the values
updates = self.get_selected(self.updatelist)
new = self.get_selected(self.newlist)
if len(updates) + len(new) == 0:
raise Exception("Nothing selected!")
cartbin_updates = None
cartbin_new = None
def do_work(repprog, repstatus):
nonlocal cartbin_updates, cartbin_new
repstatus("Creating CSV for request...")
csv_new = create_csv(new)
csv_updates = create_csv([u[1] for u in updates])
if DEBUG_NETWORK_FILE:
with open(utils.get_filesafe_datetime() + "_updates.csv", "w") as f:
f.write(csv_updates.replace(BADH_EOL, "\n"))
with open(utils.get_filesafe_datetime() + "_new.csv", "w") as f:
f.write(csv_new.replace(BADH_EOL, "\n"))
repstatus(f"Downloading updates from\n{constants.OFFICIAL_CARTCREATE_URL}")
cartbin_updates = gui_common.get_official_bin(csv_updates)
repstatus(f"Downloading new programs from\n{constants.OFFICIAL_CARTCREATE_URL}")
cartbin_new = gui_common.get_official_bin(csv_new)
if DEBUG_NETWORK_FILE:
with open(utils.get_filesafe_datetime() + "_updates.bin", "wb") as f:
f.write(cartbin_updates)
with open(utils.get_filesafe_datetime() + "_new.bin", "wb") as f:
f.write(cartbin_new)
# Perform the work to apply the update. Note that we expect the cart windowo to be empty by this time, so
# all actions should be "adding" the slots back in.
def do_work_apply(repprog, repstatus):
nonlocal cartbin_new, cartbin_updates
self.apply_update(cartbin_new, cartbin_updates)
dialog = widget_progress.do_progress_work(do_work, f"Downloading update...", simple = True, unknown_progress=True)
if not dialog.error_state:
debug_actions.global_debug.add_action_str(f"Retrieved update binary from {constants.OFFICIAL_CARTCREATE_URL}")
self.cartwindow.clear() # Get rid of what's in there now, we'll be re-adding everything back in, just updated
dialog = widget_progress.do_progress_work(do_work_apply, f"Applying update...", simple = True, unknown_progress=True)
if not dialog.error_state:
self.cartwindow.set_modified(True)
debug_actions.global_debug.add_action_str(f"Applied update to cart: {len(updates)} updated, {len(new)} added")
QMessageBox.information(self, "Update complete", f"Update complete, {len(updates)} updated, {len(new)} added. Returning to cart editor", QMessageBox.StandardButton.Ok)
self.close()
def get_selected(self, whichlist):
result = []
for x in range(whichlist.count()):
widget = whichlist.itemWidget(whichlist.item(x))
if not widget.checkbox.isChecked():
continue
if whichlist == self.updatelist:
result.append((widget.widget.info_original, widget.widget.info_update))
elif whichlist == self.newlist:
result.append(widget.widget.info_update)
return result
def apply_update(self, cartbin_new, cartbin_updates):
# Decompile the binaries
parsed_updates = arduboy.fxcart.parse(cartbin_updates)
parsed_new = arduboy.fxcart.parse(cartbin_new)
# Simple: if your cart doesn't start with a category, add the bootloader category given by the cartbin
if len(self.original_slots) == 0 or not self.original_slots[0].is_category():
self.cartwindow._add_slot_signal.emit(parsed_new[0], False)
# Now that we've done that, remove the bootloaders and fake category
parsed_updates = parsed_updates[2:]
parsed_new = parsed_new[2:]
# Now we do a very careful iteration over every item in the original slot list. When it's a category,
# we add iti, stop, and iterate over the 'new' binaries to see which ones are in this category by name,
# and add them. If it's a program, we check the updates list to see if we should use that one instead.
# We use it wholesale, except for the save, which we overwrite with the one from the original slot (if it exists).
# This should preserve all the user's unique games, categories, and game order, while still applying updates
# and adding new games
time.sleep(0.05)
categories = []
for (i, slot) in enumerate(self.original_slots):
scan_new = False # Which category to scan new games for right now, False is "don't scan"
if slot.is_category():
if not slot.meta.title: # Go find a replacement
# This is ridiculously slow.... sorry, I should do better
for us in [s for s in (parsed_updates + parsed_new) if s.is_category()]: # Go find all categories
if us.image_raw == slot.image_raw:
slot = us
logging.debug(f"Using new category info for {slot.meta.title}")
break
if len(categories):
scan_new = categories[-1] # We're entering a new category. Scan new games in the old category (to put them at the end)
categories.append(slot.meta.title)
else:
# Check the updates for it, update it if so. Note that we ONLY update the 'slot' variable, which is
# about to be added. Since this is the "original", we need to preemptively pull out the save file, so
# we can overwrite it without worry
save_file = slot.save_raw
# This is ridiculously slow.... sorry, I should do better
for (uslot, umeta) in self.updateresult[UPKEY_UPDATES]:
if slot == uslot: # This was in the update
for us in parsed_updates: # Go look for the selected update slot.
if meta_matches_slot(umeta, us):
slot = us
if save_file:
slot.save_raw = save_file
parsed_updates.remove(us)
logging.debug(f"Updated {us.meta.title}")
break
break
if i == len(self.original_slots) - 1 and len(categories):
scan_new = categories[-1] # We reached the end of the list, still need to fill whatever this "last" category is
if scan_new:
self.add_all_to_category(scan_new, parsed_new)
self.cartwindow._add_slot_signal.emit(slot, False)
# Then, we iterate over whatever is left in the 'new' binary. These are all things that go into a
# potentially "new" category, which we'll probably have to create
for category in [s for s in parsed_new if s.is_category()]:
if category.meta.title not in categories: # and category.meta.title != FAKE_CATEGORY:
self.cartwindow._add_slot_signal.emit(category, False)
self.add_all_to_category(category.meta.title, parsed_new)
leftover_updates = [s.meta.title for s in parsed_updates if not s.is_category()]
leftover_new = [s.meta.title for s in parsed_new if not s.is_category()]
logging.warning(f"Leftover updates: {len(leftover_updates)} - {','.join(leftover_updates)} new: {len(leftover_new)} - {','.join(leftover_new)}")
def add_all_to_category(self, category, parsed_new):
putnew = []
# This is ridiculously slow
for nmeta in self.updateresult[UPKEY_NEW]:
if nmeta[CMKEY_CATEGORY].lower() == category.lower():
for ns in parsed_new:
if meta_matches_slot(nmeta, ns):
self.cartwindow._add_slot_signal.emit(ns, False)
parsed_new.remove(ns)
putnew.append(ns.meta.title)
break
if len(putnew):
logging.debug(f"Put '{','.join(putnew)}' into category {category}")
else:
logging.debug(f"No new games for category {category}")
def add_selectable_listitem(self, parent, widget):
item = QListWidgetItem()
selectable_widget = SelectableListItem(widget)
# item.setFlags(item.flags() | 2) # Add the ItemIsEditable flag to enable reordering
item.setSizeHint(selectable_widget.sizeHint())
parent.addItem(item)
parent.setItemWidget(item, selectable_widget)
# Also a downloadable item, but that comes later
class SelectableListItem(QWidget):
def __init__(self, widget):
super().__init__()
self.widget = widget
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
leftlayout = QVBoxLayout()
leftlayout_widget = QWidget()
leftlayout_widget.setLayout(leftlayout)
self.checkbox = QCheckBox()
leftlayout.addWidget(self.checkbox)
layout.addWidget(leftlayout_widget)
layout.addWidget(widget)
layout.setStretchFactor(leftlayout_widget, 0)
layout.setStretchFactor(widget, 1)
self.setLayout(layout)
class BasicInfo(QWidget):
def __init__(self, title, author, version, image):
super().__init__()
title = title or "???"
author = author or "???"
version = version or "0.0"
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0)
self.setLayout(layout)
self.image = TitleImageWidget(modifiable=False, immediate=False, scale=0.5)
if image and len(image):
self.image.set_image_bytes(image)
layout.addWidget(self.image)
infolayout = QVBoxLayout()
infowidget = QWidget()
infowidget.setLayout(infolayout)
layout.addWidget(infowidget)
titlewidget = QLabel(title)
titlewidget.setStyleSheet("font-weight: bold")
infolayout.addWidget(titlewidget)
metawidget = QLabel(f"{version} | {author}")
metawidget.setStyleSheet(f"color: {gui_common.SUBDUEDCOLOR}")
infolayout.addWidget(metawidget)
class UpdateInfo(QWidget):
def __init__(self, original, update):
super().__init__()
self.info_original = original
self.info_update = update
layout = QHBoxLayout()
layout.setContentsMargins(0,0,0,0) # Because the 'NewInfo' widget is directly basicinfo, meaning no content margins
self.setLayout(layout)
originalwidget = BasicInfo(original.meta.title, original.meta.developer, original.meta.version, original.image_raw)
originalwidget.setFixedWidth(280)
layout.addWidget(originalwidget)
arrow = QLabel("➡")
gui_common.set_emoji_font(arrow, 20)
arrow.setStyleSheet(f"QLabel {{ color: {gui_common.SUCCESSCOLOR} }}")
arrow.setAlignment(Qt.AlignmentFlag.AlignCenter)
layout.addWidget(arrow)
newwidget = BasicInfo(update[CMKEY_TITLE], update[CMKEY_DEVELOPER], update[CMKEY_VERSION], update[CMKEY_IMAGE])
newwidget.setFixedWidth(280)
layout.addWidget(newwidget)
spacer = QWidget()
layout.addWidget(spacer)
layout.setStretchFactor(originalwidget, 0)
layout.setStretchFactor(arrow, 0)
layout.setStretchFactor(newwidget, 0)
layout.setStretchFactor(spacer, 1)
class NewInfo(BasicInfo):
def __init__(self, update):
super().__init__(update[CMKEY_TITLE], update[CMKEY_DEVELOPER], update[CMKEY_VERSION], update[CMKEY_IMAGE])
self.info_update = update