-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGeckoLoader.py
More file actions
1078 lines (942 loc) · 41 KB
/
GeckoLoader.py
File metadata and controls
1078 lines (942 loc) · 41 KB
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Written by JoshuaMK 2020
import atexit
import logging
import os
import pickle as cPickle
import re
import shutil
import signal
import sys
import tempfile
from contextlib import redirect_stderr, redirect_stdout
from distutils.version import LooseVersion
from io import StringIO
from pathlib import Path
from PyQt5 import QtCore, QtGui, QtWidgets
from children_ui import PrefWindow, SettingsWindow
from dolreader import DolFile
from fileutils import get_program_folder, resource_path
from kernel import CodeHandler, KernelLoader
from main_ui import MainWindow
from tools import CommandLineParser, color_text
from versioncheck import Updater
try:
import colorama
from colorama import Fore, Style
colorama.init()
TRESET = Style.RESET_ALL
TGREEN = Fore.GREEN
TGREENLIT = Style.BRIGHT + Fore.GREEN
TYELLOW = Fore.YELLOW
TYELLOWLIT = Style.BRIGHT + Fore.YELLOW
TRED = Fore.RED
TREDLIT = Style.BRIGHT + Fore.RED
except ImportError:
TRESET = ""
TGREEN = ""
TGREENLIT = ""
TYELLOW = ""
TYELLOWLIT = ""
TRED = ""
TREDLIT = ""
__version__ = "v7.1.1"
TMPDIR = Path(tempfile.mkdtemp(prefix="GeckoLoader-"))
@atexit.register
def clean_tmp_resources():
tmpfolder = TMPDIR.parent
for entry in tmpfolder.iterdir():
if entry.name.startswith("GeckoLoader-"):
shutil.rmtree(entry, ignore_errors=True)
class GeckoLoaderCli(CommandLineParser):
def __init__(self, name, version=None, description=""):
super().__init__(
prog=(f"{name} {version}"), description=description, allow_abbrev=False
)
self.__version__ = version
self.__doc__ = description
self.add_argument("dolfile", help="DOL file")
self.add_argument("codelist", help="Folder or Gecko GCT|TXT file")
self.add_argument(
"-a",
"--alloc",
help="Define the size of the code allocation in hex, only applies when using the ARENA space",
metavar="SIZE",
)
self.add_argument(
"-i",
"--init",
help="Define where GeckoLoader is initialized in hex",
metavar="ADDRESS",
)
self.add_argument(
"-tc",
"--txtcodes",
help="""["ACTIVE", "ALL"] What codes get parsed when a txt file is used.
"ALL" makes all codes get parsed,
"ACTIVE" makes only activated codes get parsed.
"ACTIVE" is the default""",
default="ACTIVE",
metavar="TYPE",
)
self.add_argument(
"--handlerpath",
help="Define the path to the codehandler file, overrides the default",
metavar="PATH",
)
self.add_argument(
"--hooktype",
help="""["VI", "GX", "PAD"] The type of hook used for the RAM search. "VI" or "GX" are recommended,
although "PAD" can work just as well. "VI" is the default""",
default="VI",
choices=["VI", "GX", "PAD"],
metavar="HOOK",
)
self.add_argument(
"--hookaddress",
help="Choose where the codehandler hooks to in hex, overrides auto hooks",
metavar="ADDRESS",
)
self.add_argument(
"-o",
"--optimize",
help="""Optimizes the codelist by directly patching qualifying
ram writes into the dol file, and removing them from the codelist""",
action="store_true",
)
self.add_argument(
"-p",
"--protect",
help="""Targets and nullifies the standard codehandler provided by loaders and Dolphin Emulator,
only applies when the ARENA is used""",
action="store_true",
)
self.add_argument(
"--dest",
help="Target path to put the modified DOL, can be a folder or file",
metavar="PATH",
)
self.add_argument(
"--checkupdate",
help="""Checks to see if a new update exists on the GitHub Repository releases page,
this option overrides all other commands.""",
action="store_true",
)
self.add_argument(
"--splash",
help="""Print the splash screen, this option overrides
all other commands excluding --checkupdate""",
action="store_true",
)
self.add_argument(
"--encrypt",
help="Encrypts the codelist on compile time, helping to slow the snoopers",
action="store_true",
)
self.add_argument(
"-q", "--quiet", help="Print nothing to the console", action="store_true"
)
self.add_argument(
"-v",
"--verbose",
help="Print extra info to the console",
default=0,
action="count",
)
def __str__(self) -> str:
return self.__doc__
def print_splash(self):
helpMessage = "Try option -h for more info on this program".center(64, " ")
version = self.__version__.rjust(9, " ")
logo = [
" ",
" ╔═══════════════════════════════════════════════════════════╗ ",
" ║ ║ ",
" ║ ┌───┐┌───┐┌───┐┌┐┌─┐┌───┐┌┐ ┌───┐┌───┐┌───┐┌───┐┌───┐ ║ ",
" ║ │┌─┐││┌──┘│┌─┐││││┌┘│┌─┐│││ │┌─┐││┌─┐│└┐┌┐││┌──┘│┌─┐│ ║ ",
" ║ ││ └┘│└──┐││ └┘│└┘┘ ││ ││││ ││ ││││ ││ │││││└──┐│└─┘│ ║ ",
" ║ ││┌─┐│┌──┘││ ┌┐│┌┐│ ││ ││││ ┌┐││ │││└─┘│ │││││┌──┘│┌┐┌┘ ║ ",
" ║ │└┴─││└──┐│└─┘││││└┐│└─┘││└─┘││└─┘││┌─┐│┌┘└┘││└──┐│││└┐ ║ ",
" ║ └───┘└───┘└───┘└┘└─┘└───┘└───┘└───┘└┘ └┘└───┘└───┘└┘└─┘ ║ ",
" ║ ║ ",
" ║ ┌┐┌───┐┌───┐┌┐ ┌┐┌┐ ┌┐┌───┐┌─┐┌─┐┌┐┌─┐ ║ ",
" ║ │││┌─┐││┌─┐│││ ││││ │││┌─┐││ └┘ ││││┌┘ ║ ",
" ║ ││││ │││└──┐│└─┘│││ ││││ │││┌┐┌┐││└┘┘ ║ ",
" ║ ┌──┐┌┐││││ ││└──┐││┌─┐│││ │││└─┘││││││││┌┐│ ┌──┐ ║ ",
" ║ └──┘│└┘││└─┘││└─┘│││ │││└─┘││┌─┐││││││││││└┐└──┘ ║ ",
" ║ └──┘└───┘└───┘└┘ └┘└───┘└┘ └┘└┘└┘└┘└┘└─┘ ║ ",
f" ║ {version} ║ ",
" ╚═══════════════════════════════════════════════════════════╝ ",
" ",
" GeckoLoader is a cli tool for allowing extended ",
" gecko code space in all Wii and GC games. ",
" ",
f"{helpMessage}",
" ",
]
for line in logo:
print(color_text(line, [("║", TREDLIT), ("╔╚╝╗═", TRED)], TGREENLIT))
def check_updates(self):
repoChecker = Updater("JoshuaMKW", "GeckoLoader")
tag, status = repoChecker.get_newest_version()
if status is False:
self.error(color_text(tag + "\n", defaultColor=TREDLIT), print_usage=False)
print("")
if LooseVersion(tag) > LooseVersion(self.__version__):
print(
color_text(
f" :: A new update is live at {repoChecker.gitReleases.format(repoChecker.owner, repoChecker.repo)}",
defaultColor=TYELLOWLIT,
)
)
print(
color_text(
f' :: Current version is "{self.__version__}", Most recent version is "{tag}"',
defaultColor=TYELLOWLIT,
)
)
elif LooseVersion(tag) < LooseVersion(self.__version__):
print(color_text(" :: No update available", defaultColor=TGREENLIT))
print(
color_text(
f' :: Current version is "{self.__version__}(dev)", Most recent version is "{tag}(release)"',
defaultColor=TGREENLIT,
)
)
else:
print(color_text(" :: No update available", defaultColor=TGREENLIT))
print(
color_text(
f' :: Current version is "{self.__version__}(release)", Most recent version is "{tag}(release)"',
defaultColor=TGREENLIT,
)
)
print("")
def _validate_args(self, args) -> dict:
dolFile = Path(args.dolfile).resolve()
codeList = Path(args.codelist).resolve()
if args.dest:
dest = Path(args.dest).resolve()
if dest.suffix == "":
dest = dest / dolFile.name
else:
dest = Path.cwd() / "geckoloader-build" / dolFile.name
if args.alloc:
try:
_allocation = int(args.alloc, 16)
except ValueError:
self.error(
color_text("The allocation was invalid\n", defaultColor=TREDLIT)
)
else:
_allocation = None
if args.hookaddress:
if 0x80000000 > int(args.hookaddress, 16) >= 0x81800000:
self.error(
color_text(
"The codehandler hook address was beyond bounds\n",
defaultColor=TREDLIT,
)
)
else:
try:
_codehook = int(args.hookaddress, 16)
except ValueError:
self.error(
color_text(
"The codehandler hook address was invalid\n",
defaultColor=TREDLIT,
)
)
else:
_codehook = None
if args.handlerpath:
codeHandlerFile = Path(args.handlerpath).resolve()
else:
codeHandlerFile = Path("bin/codehandler.bin")
if not dolFile.is_file():
self.error(
color_text(f'File "{dolFile}" does not exist\n', defaultColor=TREDLIT)
)
if not codeList.exists():
self.error(
color_text(
f'File/folder "{codeList}" does not exist\n', defaultColor=TREDLIT
)
)
return {
"dol": dolFile,
"codepath": codeList,
"codehandler": codeHandlerFile,
"destination": dest,
"allocation": _allocation,
"hookaddress": _codehook,
"hooktype": args.hooktype,
"initaddress": None if args.init is None else int(args.init, 16),
"includeall": args.txtcodes.lower() == "all",
"optimize": args.optimize,
"protect": args.protect,
"encrypt": args.encrypt,
"verbosity": args.verbose,
"quiet": args.quiet,
}
def _exec(self, args, tmpdir):
context = self._validate_args(args)
try:
with context["dol"].open("rb") as dol:
dolFile = DolFile(dol)
with resource_path(context["codehandler"]).open("rb") as handler:
codeHandler = CodeHandler(handler)
codeHandler.allocation = context["allocation"]
codeHandler.hookAddress = context["hookaddress"]
codeHandler.hookType = context["hooktype"]
codeHandler.includeAll = context["includeall"]
codeHandler.optimizeList = context["optimize"]
with resource_path("bin/geckoloader.bin").open("rb") as kernelfile:
geckoKernel = KernelLoader(kernelfile, cli)
geckoKernel.initAddress = context["initaddress"]
geckoKernel.verbosity = context["verbosity"]
geckoKernel.quiet = context["quiet"]
geckoKernel.encrypt = context["encrypt"]
geckoKernel.protect = context["protect"]
if not context["destination"].parent.exists():
context["destination"].parent.mkdir(parents=True, exist_ok=True)
geckoKernel.build(
context["codepath"],
dolFile,
codeHandler,
TMPDIR,
context["destination"],
)
except FileNotFoundError as e:
self.error(color_text(e, defaultColor=TREDLIT))
class GUI(object):
class Dialogs:
LOAD_DEST = 0
LOAD_GCT = 1
LOAD_FOLDER = 2
LOAD_DOL = 3
LOAD_SESSION = 4
SAVE_SESSION = 5
SAVE_SESSION_AS = 6
def __init__(self, cli: GeckoLoaderCli):
self.cli = cli
self.app = None
self.ui = None
self.uiprefs = None
self.uiexSettings = None
self.dolPath = None
self.codePath = [None, None]
self.destPath = None
self.sessionPath = None
self.prefs = {"qtstyle": "Default", "darktheme": False}
self.style_log = []
self.compileCount = 0
self.log = logging.getLogger(f"GeckoLoader {self.version}")
if not get_program_folder("GeckoLoader").exists():
get_program_folder("GeckoLoader").mkdir()
hdlr = logging.FileHandler(get_program_folder("GeckoLoader") / "error.log")
formatter = logging.Formatter("\n%(levelname)s (%(asctime)s): %(message)s")
hdlr.setFormatter(formatter)
self.log.addHandler(hdlr)
def show_dialog(self, dialog_type=None):
if dialog_type == "aboutqt":
QtWidgets.QMessageBox.aboutQt(self.app.activeWindow())
elif dialog_type == "aboutGeckoLoader":
desc = "".join(
[
"GeckoLoader is a cross platform tool designed to give ",
"the user the most efficient codespace usage possible.\n\n ",
"This application supports various features, such as ",
"pre-patching codes, dynamic codehandler hooks, codespace ",
"extension through memory reallocation, multiple patching ",
"of a single DOL, and more.\n\n",
f"Current running version: {self.version}\n\n"
"Copyright (c) 2020\n\n",
"JoshuaMK <joshuamkw2002@gmail.com> \n\n",
"All rights reserved.",
]
)
QtWidgets.QMessageBox.about(
self.app.activeWindow(), "About GeckoLoader", desc
)
elif dialog_type == "Preferences":
self.uiprefs.show()
else:
self.uiexSettings.show()
@property
def version(self) -> str:
return self.cli.__version__
def _open_dol(self) -> tuple:
if self.dolPath is None: # Just start in the home directory
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open DOL",
str(Path.home()),
"Nintendo DOL Executable (*.dol);;All files (*)",
)[0]
)
else: # Start in the last directory used by the user
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open DOL",
str(self.dolPath.parent),
"Nintendo DOL Executable (*.dol);;All files (*)",
)[0]
)
if fname == "" or fname is None: # Make sure we have something to open
return False, None
else:
self.dolPath = Path(fname).resolve()
if self.dolPath.is_file():
self.ui.dolTextBox.setText(str(self.dolPath))
return True, None
else:
return False, "The file does not exist!"
def _load_codes(self, isFolder: bool = False) -> tuple:
if not isFolder:
if self.codePath[0] is None: # Just start in the home directory
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open Codelist",
str(Path.home()),
"Gecko Code Table (*.gct);;Gecko Codelist (*.txt);;All files (*)",
)[0]
)
else: # Start in the last directory used by the user
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open Codelist",
str(self.codePath[0].parent),
"Gecko Code Table (*.gct);;Gecko Codelist (*.txt);;All files (*)",
)[0]
)
else:
if self.codePath[0] is None: # Just start in the home directory
fname = str(
QtWidgets.QFileDialog.getExistingDirectory(
self.ui,
"Open Codelist",
str(Path.home()),
QtWidgets.QFileDialog.ShowDirsOnly,
)
)
else: # Start in the last directory used by the user
fname = str(
QtWidgets.QFileDialog.getExistingDirectory(
self.ui,
"Open Codelist",
str(self.codePath[0].parent),
QtWidgets.QFileDialog.ShowDirsOnly,
)
)
if fname == "" or fname is None: # Make sure we have something to open
return False, None
else:
self.codePath = [Path(fname).resolve(), isFolder]
if not isFolder:
self.ui.gctFileTextBox.setText(str(self.codePath[0]))
self.ui.gctFolderTextBox.setText("")
else:
self.ui.gctFileTextBox.setText("")
self.ui.gctFolderTextBox.setText(str(self.codePath[0]))
return True, None
def _open_dest(self) -> tuple:
if self.dolPath is None: # Just start in the home directory
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open DOL",
str(Path.home()),
"Nintendo DOL Executable (*.dol);;All files (*)",
)[0]
)
else: # Start in the last directory used by the user
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open DOL",
str(self.dolPath.parent),
"Nintendo DOL Executable (*.dol);;All files (*)",
)[0]
)
if fname == "" or fname is None: # Make sure we have something to open
return False, None
else:
self.destPath = Path(fname).resolve()
self.ui.destTextBox.setText(str(self.destPath))
return True, None
def _load_session(self) -> tuple:
if self.sessionPath is None:
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open Session",
str(Path.home()),
"GeckoLoader Session (*.gprf);;All files (*)",
)[0]
)
else: # Start in the last directory used by the user
fname = str(
QtWidgets.QFileDialog.getOpenFileName(
self.ui,
"Open Session",
str(self.sessionPath.parent),
"GeckoLoader Session (*.gprf);;All files (*)",
)[0]
)
if fname == "" or fname is None: # Make sure we have something to open
return False, None
else:
self.sessionPath = Path(fname).resolve()
with self.sessionPath.open("rb") as session:
p = cPickle.load(session)
self.ui.dolTextBox.setText(p["dolPath"])
if p["gctFilePath"] != "":
self.ui.gctFileTextBox.setText(p["gctFilePath"])
self.ui.gctFolderTextBox.setText("")
elif p["gctFolderPath"] != "":
self.ui.gctFileTextBox.setText("")
self.ui.gctFolderTextBox.setText(p["gctFolderPath"])
else:
self.ui.gctFileTextBox.setText("")
self.ui.gctFolderTextBox.setText("")
self.ui.destTextBox.setText(p["destPath"])
self.ui.allocLineEdit.setText(p["alloc"])
self.ui.handlerTypeSelect.setCurrentIndex(p["handlerIndex"])
self.ui.hookTypeSelect.setCurrentIndex(p["hookIndex"])
self.ui.txtCodesIncludeSelect.setCurrentIndex(p["txtIndex"])
self.uiexSettings.optimizeCodes.setChecked(p["optimize"])
self.uiexSettings.protectCodes.setChecked(p["protect"])
self.uiexSettings.encryptCodes.setChecked(p["encrypt"])
self.uiexSettings.codehookLineEdit.setText(p["hookAddress"])
self.uiexSettings.kernelHookLineEdit.setText(p["initAddress"])
self.uiexSettings.verbositySelect.setCurrentIndex(p["verbosity"])
return True, None
def _save_session(self, saveAs=False):
if saveAs or self.sessionPath is None or self.sessionPath == "":
if self.sessionPath is None: # Just start in the home directory
fname = str(
QtWidgets.QFileDialog.getSaveFileName(
self.ui,
"Save Session",
str(Path.home()),
"GeckoLoader Session (*.gprf);;All files (*)",
)[0]
)
else: # Start in the last directory used by the user
fname = str(
QtWidgets.QFileDialog.getSaveFileName(
self.ui,
"Save Session",
str(self.dolPath.parent),
"GeckoLoader Session (*.gprf);;All files (*)",
)[0]
)
if fname == "" or fname is None: # Make sure we have something to open
return False, None
else:
self.sessionPath = Path(fname).resolve()
try:
with self.sessionPath.open("wb") as session:
p = {}
p["dolPath"] = self.ui.dolTextBox.text().strip()
p["gctFilePath"] = self.ui.gctFileTextBox.text().strip()
p["gctFolderPath"] = self.ui.gctFolderTextBox.text().strip()
p["destPath"] = self.ui.destTextBox.text().strip()
p["alloc"] = self.ui.allocLineEdit.text().strip()
p["handlerIndex"] = self.ui.handlerTypeSelect.currentIndex()
p["hookIndex"] = self.ui.hookTypeSelect.currentIndex()
p["txtIndex"] = self.ui.txtCodesIncludeSelect.currentIndex()
p["optimize"] = self.uiexSettings.optimizeCodes.isChecked()
p["protect"] = self.uiexSettings.protectCodes.isChecked()
p["encrypt"] = self.uiexSettings.encryptCodes.isChecked()
p["hookAddress"] = self.uiexSettings.codehookLineEdit.text().strip()
p["initAddress"] = self.uiexSettings.kernelHookLineEdit.text().strip()
p["verbosity"] = self.uiexSettings.verbositySelect.currentIndex()
try:
cPickle.dump(p, session)
except cPickle.PicklingError as e:
return False, str(e)
return True, None
except (IOError, PermissionError) as e:
return False, str(e)
def file_dialog_exec(self, event: Dialogs):
try:
if event == GUI.Dialogs.LOAD_DOL:
status, msg = self._open_dol()
elif event == GUI.Dialogs.LOAD_GCT:
status, msg = self._load_codes(False)
elif event == GUI.Dialogs.LOAD_FOLDER:
status, msg = self._load_codes(True)
elif event == GUI.Dialogs.LOAD_DEST:
status, msg = self._open_dest()
elif event == GUI.Dialogs.LOAD_SESSION:
status, msg = self._load_session()
elif event == GUI.Dialogs.SAVE_SESSION:
status, msg = self._save_session()
elif event == GUI.Dialogs.SAVE_SESSION_AS:
status, msg = self._save_session(True)
else:
return
except IndexError:
self.ui.set_edit_fields()
return
if status is False and msg is not None:
reply = QtWidgets.QErrorMessage(self)
reply.setWindowTitle("I/O Failure")
reply.setText(msg)
reply.setInformativeText("Please try again.")
reply.setIcon(QtWidgets.QMessageBox.Warning)
reply.setStandardButtons(QtWidgets.QMessageBox.Ok)
reply.exec_()
else:
self.ui.set_edit_fields()
def close_session(self):
self.dolPath = None
self.codePath = None
self.sessionPath = None
self.ui.dolTextBox.setText("")
self.ui.gctFileTextBox.setText("")
self.ui.gctFolderTextBox.setText("")
self.ui.destTextBox.setText("")
self.ui.allocLineEdit.setText("")
self.ui.handlerTypeSelect.setCurrentIndex(0)
self.ui.hookTypeSelect.setCurrentIndex(0)
self.ui.txtCodesIncludeSelect.setCurrentIndex(0)
self.ui.optimizeSelect.setCurrentIndex(0)
self.ui.responses.setPlainText("")
self.uiexSettings.protectCodes.setChecked(False)
self.uiexSettings.encryptCodes.setChecked(False)
self.uiexSettings.codehookLineEdit.setText("")
self.uiexSettings.kernelHookLineEdit.setText("")
self.uiexSettings.verbositySelect.setCurrentIndex(0)
self.ui.set_edit_fields()
def load_prefs(self):
datapath = get_program_folder("GeckoLoader")
try:
with (datapath / ".GeckoLoader.conf").open("rb") as f:
try:
p = cPickle.load(f)
except cPickle.UnpicklingError as e:
self.log.exception(e) # Use defaults for prefs
else:
# Input validation
if (
p.get("qtstyle") in list(QtWidgets.QStyleFactory.keys())
or p.get("qtstyle") == "Default"
):
self.prefs["qtstyle"] = p.get("qtstyle")
if p.get("darktheme") in (True, False):
self.prefs["darktheme"] = p.get("darktheme")
setCIndex = self.uiprefs.qtstyleSelect.setCurrentIndex
if self.prefs.get("qtstyle") in (None, "Default"):
setCIndex(0)
else:
setCIndex(
self.uiprefs.qtstyleSelect.findText(
self.prefs.get("qtstyle"),
flags=QtCore.Qt.MatchFixedString,
)
)
self.uiprefs.qtdarkButton.setChecked(self.prefs.get("darktheme"))
self.update_theme()
except FileNotFoundError:
self.log.warning("No preferences file found; using defaults.")
def save_prefs(self):
datapath = get_program_folder("GeckoLoader")
self.prefs["qtstyle"] = str(self.uiprefs.qtstyleSelect.currentText())
self.prefs["darktheme"] = self.uiprefs.qtdarkButton.isChecked()
try:
with (datapath / ".GeckoLoader.conf").open("wb") as f:
cPickle.dump(self.prefs, f)
except IOError as e:
self.log.exception(e)
def load_qtstyle(self, style, first_style_load=False):
self.style_log.append(
[self.app.style, self.uiprefs.qtstyleSelect.currentText()]
)
if len(self.style_log) > 2:
self.style_log.pop(0)
if style != "Default":
self.app.setStyle(style)
else:
self.app.setStyle(self.default_qtstyle)
if first_style_load:
setCIndex = self.uiprefs.qtstyleSelect.setCurrentIndex
setCIndex(
self.uiprefs.qtstyleSelect.findText(
style, flags=QtCore.Qt.MatchFixedString
)
)
def update_theme(self):
if self.uiprefs.qtdarkButton.isChecked():
self.app.setPalette(self.ui.DarkTheme)
self.load_qtstyle("Fusion", True)
self.uiprefs.qtstyleSelect.setDisabled(True)
else:
self.app.setPalette(self.ui.LightTheme)
self.load_qtstyle(self.style_log[0][1], True)
self.uiprefs.qtstyleSelect.setEnabled(True)
def display_update(self):
_outpipe = StringIO()
_errpipe = StringIO()
with redirect_stdout(_outpipe), redirect_stderr(_errpipe):
try:
self.cli.check_updates()
except SystemExit:
_status = False
else:
_status = True
icon = QtGui.QIcon()
icon.addPixmap(
QtGui.QPixmap(str(resource_path(Path("bin/icon.ico")))),
QtGui.QIcon.Normal,
QtGui.QIcon.Off,
)
if _status is False:
reply = QtWidgets.QErrorMessage()
reply.setWindowIcon(icon)
reply.setWindowTitle("Response Error")
reply.setText(self._remove_ansi(_errpipe.getvalue()))
reply.setInformativeText("Make sure you have an internet connection")
reply.setIcon(QtWidgets.QMessageBox.Warning)
reply.setStandardButtons(QtWidgets.QMessageBox.Ok)
reply.exec_()
else:
reply = QtWidgets.QMessageBox()
reply.setWindowIcon(icon)
reply.setWindowTitle("Update Info")
reply.setText(
self._remove_ansi(_outpipe.getvalue()).strip("\n")
+ "\n\nYou can find all GeckoLoader releases at:\nhttps://github.com/JoshuaMKW/GeckoLoader/releases"
)
reply.setIcon(QtWidgets.QMessageBox.Information)
reply.setStandardButtons(QtWidgets.QMessageBox.Ok)
reply.exec_()
@staticmethod
def _enforce_mask(textbox: QtWidgets.QTextEdit, mask: int, _or: int = 0):
textbox.setText(textbox.text().strip())
if len(textbox.text()) > 0:
_depth = len(hex(mask)[2:])
_address = int(textbox.text(), 16) << ((_depth - len(textbox.text())) << 2)
aligned = hex(
((_address & mask) | _or) >> ((_depth - len(textbox.text())) << 2)
)[2:].upper()
textbox.setText(aligned)
@staticmethod
def _remove_ansi(msg: str) -> str:
ansi_escape = re.compile(r"\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])")
return ansi_escape.sub("", msg)
def connect_signals(self):
self.ui.actionPreferences.triggered.connect(
lambda: self.show_dialog("Preferences")
)
self.ui.actionAbout_Qt.triggered.connect(lambda: self.show_dialog("aboutqt"))
self.ui.actionAbout_GeckoLoader.triggered.connect(
lambda: self.show_dialog("aboutGeckoLoader")
)
self.ui.actionCheck_Update.triggered.connect(lambda: self.display_update())
self.ui.actionOpen.triggered.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.LOAD_SESSION)
)
self.ui.actionClose.triggered.connect(lambda: self.close_session())
self.ui.actionSave_As.triggered.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.SAVE_SESSION_AS)
)
self.ui.actionSave.triggered.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.SAVE_SESSION)
)
self.ui.dolButton.clicked.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.LOAD_DOL)
)
self.ui.gctFileButton.clicked.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.LOAD_GCT)
)
self.ui.gctFolderButton.clicked.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.LOAD_FOLDER)
)
self.ui.destButton.clicked.connect(
lambda: self.file_dialog_exec(GUI.Dialogs.LOAD_DEST)
)
self.ui.dolTextBox.textChanged.connect(lambda: self.ui.set_edit_fields())
self.ui.gctFolderTextBox.textChanged.connect(lambda: self.ui.set_edit_fields())
self.ui.gctFileTextBox.textChanged.connect(lambda: self.ui.set_edit_fields())
self.ui.destTextBox.textChanged.connect(lambda: self.ui.set_edit_fields())
self.ui.allocLineEdit.textChanged.connect(
lambda: self._enforce_mask(self.ui.allocLineEdit, 0xFFFFFC)
)
self.ui.exOptionsButton.clicked.connect(
lambda: self.show_dialog("Advanced Settings")
)
self.ui.compileButton.clicked.connect(lambda: self._exec_api())
self.uiprefs.buttonBox.accepted.connect(self.save_prefs)
self.uiprefs.qtstyleSelect.currentIndexChanged.connect(
lambda: self.load_qtstyle(self.uiprefs.qtstyleSelect.currentText())
)
self.uiprefs.qtdarkButton.clicked.connect(lambda: self.update_theme())
self.uiexSettings.codehookLineEdit.textChanged.connect(
lambda: self._enforce_mask(
self.uiexSettings.codehookLineEdit, 0x817FFFFC, 0x80000000
)
)
self.uiexSettings.kernelHookLineEdit.textChanged.connect(
lambda: self._enforce_mask(
self.uiexSettings.kernelHookLineEdit, 0x817FFFFC, 0x80000000
)
)
def _exec_api(self):
if sys.platform == "win32":
self.ui.responses.appendPlainText(
f"| Session {self.compileCount} |".center(84, "=") + "\n"
)
else:
self.ui.responses.appendPlainText(
f"| Session {self.compileCount} |".center(76, "=") + "\n"
)
self.compileCount += 1
if self.ui.dolTextBox.isEnabled and self.ui.dolTextBox.text().strip() != "":
dol = self.ui.dolTextBox.text().strip()
else:
self.ui.responses.appendPlainText(
"DOL is missing, please add the path to your codes in the respective textbox"
+ "\n\n"
)
return
if (
self.ui.gctFileTextBox.isEnabled
and self.ui.gctFileTextBox.text().strip() != ""
):
gct = self.ui.gctFileTextBox.text().strip()
elif (
self.ui.gctFolderTextBox.isEnabled
and self.ui.gctFolderTextBox.text().strip() != ""
):
gct = self.ui.gctFolderTextBox.text().strip()
else:
self.ui.responses.appendPlainText(
"GCT is missing, please add the path to your codes in the respective textbox"
+ "\n\n"
)
return
alloc = self.ui.allocLineEdit.text().strip()
hookType = self.ui.hookTypeSelect.currentText().strip()
hookAddress = self.uiexSettings.codehookLineEdit.text().strip()
initAddress = self.uiexSettings.kernelHookLineEdit.text().strip()
txtInclude = self.ui.txtCodesIncludeSelect.currentText().strip()
optimize = self.uiexSettings.optimizeCodes.isChecked()
protect = self.uiexSettings.protectCodes.isChecked()
encrypt = self.uiexSettings.encryptCodes.isChecked()
verbosity = int(self.uiexSettings.verbositySelect.currentText().strip())
dest = self.ui.destTextBox.text().strip()
argslist = [
dol,
gct,
"-t",
txtInclude,
"--hooktype",
hookType,
]
if alloc != "":
argslist.append("-a")
argslist.append(hex(int(alloc, 16) & 0xFFFFFC)[2:].upper())
if hookAddress != "":
if int(hookAddress, 16) < 0x80000000:
self.ui.responses.appendPlainText(
"The specified code hook is invalid" + "\n"
)
return
argslist.append("--hookaddress")
argslist.append(hookAddress)
if initAddress != "":
if int(initAddress, 16) < 0x80000000:
self.ui.responses.appendPlainText(
"The specified initialization address is invalid" + "\n"
)
return
argslist.append("-i")
argslist.append(initAddress)
if dest != "":
if dest.lower().endswith(".dol") and len(dest) > 4:
argslist.append("--dest")
argslist.append(dest)
else:
self.ui.responses.appendPlainText(
"The destination file path is not a valid DOL file\n"
)
return
if optimize:
argslist.append("-o")
if protect:
argslist.append("-p")
if encrypt:
argslist.append("--encrypt")
if verbosity > 0:
argslist.append("-" + "v" * verbosity)
else: