-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·1646 lines (1350 loc) · 69.7 KB
/
main.py
File metadata and controls
executable file
·1646 lines (1350 loc) · 69.7 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
#!/usr/bin/env python
# Do some basic imports, and set up logging to catch ImportError.
import inspect
import locale
import logging
import os
import sys
if (__name__ == "__main__"):
global dir_self
locale.setlocale(locale.LC_ALL, "")
# Tkinter chokes on non-US locales with commas for decimal points.
# http://bugs.python.org/issue10647
# Fixed in Python 3.2.
#
locale.setlocale(locale.LC_NUMERIC, "C") # Use period for numbers.
# Get the un-symlinked, absolute path to this module.
dir_self = os.path.realpath(os.path.abspath(os.path.split(inspect.getfile( inspect.currentframe() ))[0]))
if (dir_self not in sys.path): sys.path.insert(0, dir_self)
# Go to this module's dir.
os.chdir(dir_self)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logstream_handler = logging.StreamHandler()
logstream_formatter = logging.Formatter("%(levelname)s: %(message)s")
logstream_handler.setFormatter(logstream_formatter)
logstream_handler.setLevel(logging.INFO)
logger.addHandler(logstream_handler)
logfile_handler = logging.FileHandler(os.path.join(dir_self, "modman-log.txt"), mode="w")
logfile_formatter = logging.Formatter("%(asctime)s %(levelname)s: %(message)s", "%Y-%m-%d %H:%M:%S")
logfile_handler.setFormatter(logfile_formatter)
logger.addHandler(logfile_handler)
# __main__ stuff is continued at the end of this file.
# Import everything else (tkinter may be absent in some environments).
try:
import codecs
import errno
import fnmatch
import hashlib
import io
import platform
import re
import shutil as sh
import subprocess
import tempfile as tf
import threading
import webbrowser
import zipfile as zf
import xml.parsers.expat
# Modules that changed in Python 3.x.
# Use loops for brevity, which means passing vars into __import__().
# And alias the modules by directly setting the globals() dict.
for (old_name, new_name) in [("ConfigParser", "configparser"),
("Queue", "queue"),
("Tkinter", "tkinter")]:
try:
# import new_name
globals()[new_name] = __import__(new_name, globals(), locals(), [])
except (ImportError) as err:
# import old_name as new_name
globals()[new_name] = __import__(old_name, globals(), locals(), [])
for (old_thing, new_thing, alias) in [("tkMessageBox", "messagebox", "msgbox"),
("tkFileDialog", "filedialog", "filedlg")]:
try:
# from tkinter import new_thing as alias
globals()[alias] = getattr(__import__("tkinter", globals(), locals(), [new_thing]), new_thing)
except (ImportError) as err:
# import old_thing as alias
globals()[alias] = __import__(old_thing, globals(), locals(), [])
globals()["tk"] = globals()["tkinter"] # Mimic import tkinter as tk
# Modules bundled with this script.
from lib import cleanup
from lib import ftldat
from lib import global_config
from lib import imageinfo
from lib import killable_threading
from lib import moddb
from lib import tkHyperlinkManager
except (Exception) as err:
logging.exception(err)
sys.exit(1)
class RootWindow(tk.Tk):
def __init__(self, master, *args, **kwargs):
tk.Tk.__init__(self, master, *args, **kwargs)
# Pseudo enum constants.
self.ACTIONS = ["ACTION_CONFIG", "ACTION_SHOW_MAIN_WINDOW",
"ACTION_ADD_MOD_HASH", "ACTION_SET_MODDB",
"ACTION_SPAWN_FTL",
"ACTION_DIE"]
for x in self.ACTIONS: setattr(self, x, x)
self._event_queue = queue.Queue()
self.done = False # Indicates to other threads that mainloop() ended.
self._main_window = None
self.mod_hashes = {} # Map mod_name to mod_hash for db lookups.
self.mod_db = moddb.create_default_db()
# Add a right-click clipboard menu to
# all text fields and text areas.
self._clpbrd_menu = tk.Menu(self, tearoff=0)
self._clpbrd_menu.add_command(label="Cut")
self._clpbrd_menu.add_command(label="Copy")
self._clpbrd_menu.add_command(label="Paste")
def show_clpbrd_menu(e):
w = e.widget
edit_choice_state = "normal"
try:
if (w.cget("state") == "disabled"): edit_choice_state = "disabled"
except (Exception) as err:
pass
self._clpbrd_menu.entryconfigure("Cut", command=lambda: w.event_generate("<<Cut>>"), state=edit_choice_state)
self._clpbrd_menu.entryconfigure("Copy", command=lambda: w.event_generate("<<Copy>>"))
self._clpbrd_menu.entryconfigure("Paste", command=lambda: w.event_generate("<<Paste>>"), state=edit_choice_state)
self._clpbrd_menu.tk.call("tk_popup", self._clpbrd_menu, e.x_root, e.y_root)
self.bind_class("Entry", "<Button-3><ButtonRelease-3>", show_clpbrd_menu)
self.bind_class("Text", "<Button-3><ButtonRelease-3>", show_clpbrd_menu)
self.wm_protocol("WM_DELETE_WINDOW", self._on_delete)
def poll_queue():
self._process_event_queue(None)
self._poll_queue_alarm_id = self.after(100, self._poll_queue)
self._poll_queue_alarm_id = None
self._poll_queue = poll_queue
self._poll_queue()
def _on_delete(self):
if (self._poll_queue_alarm_id is not None):
self.after_cancel(self._poll_queue_alarm_id)
self._root().quit()
def _process_event_queue(self, event):
"""Processes every pending event on the queue."""
# With after() polling, always use get_nowait() to avoid blocking.
func_or_name, arg_dict = None, None
while (True):
try:
func_or_name, arg_dict = self._event_queue.get_nowait()
except (queue.Empty) as err:
break
else:
self._process_event(func_or_name, arg_dict)
def _process_event(self, func_or_name, arg_dict):
"""Processes events queued via invoke_later()."""
def check_args(args):
for arg in args:
if (arg not in arg_dict):
logging.error("Missing %s arg queued to %s %s." % (arg, self.__class__.__name__, func_or_name))
return False
return True
if (hasattr(func_or_name, "__call__")):
func_or_name(arg_dict)
elif (func_or_name == self.ACTION_CONFIG):
check_args(["write_config", "config_parser", "prompts", "next_func"])
if ("ftl_dats_path" in arg_dict["prompts"]):
if (global_config.dir_res):
if (not msgbox.askyesno(global_config.APP_NAME, "FTL resources were found in:\n%s\nIs this correct?" % global_config.dir_res)):
global_config.dir_res = None
if (not global_config.dir_res):
logging.debug("FTL dats path was not located automatically. Prompting user for location.")
global_config.dir_res = prompt_for_ftl_path()
if (global_config.dir_res):
arg_dict["config_parser"].set("settings", "ftl_dats_path", global_config.dir_res)
arg_dict["write_config"] = True
logging.info("FTL dats located at: %s" % global_config.dir_res)
if (not global_config.dir_res):
logging.debug("No FTL dats path found, exiting.")
sys.exit(1)
if ("update_catalog" in arg_dict["prompts"]):
global_config.update_catalog = msgbox.askyesno(global_config.APP_NAME, "Would you like GMM to periodically download\ndescriptions for the latest mods?\n\nYou can change this later in modman.ini.")
arg_dict["config_parser"].set("settings", "update_catalog", ("1" if (global_config.update_catalog is True) else "0"))
arg_dict["write_config"] = True
arg_dict["next_func"]({"write_config":arg_dict["write_config"], "config_parser":arg_dict["config_parser"]})
elif (func_or_name == self.ACTION_SHOW_MAIN_WINDOW):
check_args(["mod_names", "next_func"])
if (not self._main_window):
self._main_window = MainWindow(master=self, title=global_config.APP_NAME, mod_names=arg_dict["mod_names"], next_func=arg_dict["next_func"])
self._main_window.resizable("yes", "yes")
self._main_window.center_window()
elif (func_or_name == self.ACTION_ADD_MOD_HASH):
check_args(["mod_name", "mod_hash"])
self.mod_hashes[arg_dict["mod_name"]] = arg_dict["mod_hash"]
elif (func_or_name == self.ACTION_SET_MODDB):
check_args(["new_moddb"])
self.mod_db = arg_dict["new_moddb"]
elif (func_or_name == self.ACTION_SPAWN_FTL):
check_args(["ftl_exe_path", "message"])
if (arg_dict["ftl_exe_path"]):
message = "%s Run FTL now?" % (arg_dict["message"] if (arg_dict["message"]) else "")
if (msgbox.askyesno(global_config.APP_NAME, message)):
if (platform.system() == "Darwin" and os.path.isdir(arg_dict["ftl_exe_path"])
and os.path.isfile(os.path.join(arg_dict["ftl_exe_path"], "Contents", "Info.plist"))):
# This is an app bundle.
logging.info("Running FTL Bundle...")
subprocess.Popen(["open", "-a", arg_dict["ftl_exe_path"]], cwd=os.path.dirname(arg_dict["ftl_exe_path"]))
else:
logging.info("Running FTL...")
subprocess.Popen([arg_dict["ftl_exe_path"]], cwd=os.path.dirname(arg_dict["ftl_exe_path"]))
elif (arg_dict["message"]):
msgbox.showinfo(global_config.APP_NAME, arg_dict["message"])
elif (func_or_name == self.ACTION_DIE):
# Destruction awaits. Nothing more to do.
with self._event_queue.mutex:
self._event_queue.queue.clear()
self._root().destroy()
def invoke_later(self, func_or_name, arg_dict):
"""Schedules an action to occur in this thread. (thread-safe)"""
self._event_queue.put((func_or_name, arg_dict))
# The queue will be polled eventually by an after() alarm.
class MainWindow(tk.Toplevel):
def __init__(self, master, *args, **kwargs):
self.custom_args = {"title":None, "mod_names":[], "next_func":None}
for k in self.custom_args.keys():
if (k in kwargs):
self.custom_args[k] = kwargs[k]
del kwargs[k]
tk.Toplevel.__init__(self, master, *args, **kwargs)
if (self.custom_args["title"]): self.wm_title(self.custom_args["title"])
self.button_padx = "2m"
self.button_pady = "1m"
self._prev_selection = set()
self._mouse_press_list_index = None
self._reordered_mods = None
self._pending_mods = None
self.resizable(False, False)
# Our topmost frame is called root_frame.
root_frame = tk.Frame(self)
root_frame.pack(fill="both", expand="yes")
# Top frame (container).
top_frame = tk.Frame(root_frame)
top_frame.pack(side="top", fill="both", expand="yes")
# Top-left frame (mod list).
left_frame = tk.Frame(top_frame, #background="red",
borderwidth=1, relief="ridge",
width=50, height=250)
left_frame.pack(side="left", fill="both", expand="yes")
# Top-right frame (buttons).
right_frame = tk.Frame(top_frame, width=250)
right_frame.pack(side="right", fill="y", expand="no")
# Bottom frame (mod descriptions).
bottom_frame = tk.Frame(root_frame,
borderwidth=3, relief="ridge",
height=50)
bottom_frame.pack(side="top", fill="both", expand="yes")
# Add a listbox to hold the mod names.
self._mod_listbox = tk.Listbox(left_frame, selectmode="multiple", exportselection=0,
width=30, height=1) # Height readjusts itself for the button frame
self._mod_listbox.pack(side="left", fill="both", expand="yes")
self._mod_list_scroll = tk.Scrollbar(left_frame, orient="vertical")
self._mod_list_scroll.pack(side="right", fill="y")
self._mod_listbox.bind("<<ListboxSelect>>", self._on_listbox_select)
self._mod_listbox.bind("<Double-Button-1>", self._on_listbox_mouse_double_clicked)
self._mod_listbox.bind("<Button-1>", self._on_listbox_mouse_pressed)
self._mod_listbox.bind("<B1-Motion>", self._on_listbox_mouse_dragged)
self._mod_listbox.configure(yscrollcommand=self._mod_list_scroll.set)
self._mod_list_scroll.configure(command=self._mod_listbox.yview)
# Add textbox at bottom to hold mod information.
self._desc_scroll = tk.Scrollbar(bottom_frame, orient="vertical")
self._desc_scroll.pack(side="right", fill="y")
self._desc_area = tk.Text(bottom_frame, width=60, height=11, wrap="word")
self._desc_area.pack(fill="both", expand="yes")
self._desc_area.configure(yscrollcommand=self._desc_scroll.set)
self._desc_scroll.configure(command=self._desc_area.yview)
# Set formating tags.
self._desc_area.tag_configure("title", font="helvetica 20 bold")
self._hyperman = tkHyperlinkManager.HyperlinkManager(self._desc_area)
self._statusbar = tk.Label(root_frame, borderwidth="1", relief="sunken",
anchor="w", font="helvetica 9")
self._statusbar.pack(fill="x", expand="no", pady=("3","0"))
# Add the buttons to the buttons frame.
self._patch_btn = tk.Button(right_frame, text="Patch")
self._patch_btn.configure(padx=self.button_padx, pady=self.button_pady)
self._patch_btn.pack(side="top", fill="x")
self._patch_btn.configure(command=self._patch)
self._patch_btn.bind("<Return>", lambda e: self._patch())
self._set_status_help(self._patch_btn, "Incorporate all selected mods into the game.")
self._toggle_all_btn = tk.Button(right_frame, text="Toggle All")
self._toggle_all_btn.configure(padx=self.button_padx, pady=self.button_pady)
self._toggle_all_btn.pack(side="top", fill="x")
self._toggle_all_btn.configure(command=self._toggle_all)
self._toggle_all_btn.bind("<Return>", lambda e: self._toggle_all())
self._set_status_help(self._toggle_all_btn, "Select all mods, or none.")
self._validate_btn = tk.Button(right_frame, text="Validate")
self._validate_btn.configure(padx=self.button_padx, pady=self.button_pady)
self._validate_btn.pack(side="top", fill="x")
self._validate_btn.configure(command=self._validate_selected_mod)
self._validate_btn.bind("<Return>", lambda e: self._validate_selected_mod())
self._set_status_help(self._validate_btn, "Check selected mods for problems.")
self._about_btn = tk.Button(right_frame, text="About")
self._about_btn.configure(padx=self.button_padx, pady=self.button_pady)
self._about_btn.pack(side="top", fill="x")
self._about_btn.configure(command=self._show_app_description)
self._about_btn.bind("<Return>", lambda e: self._show_app_description())
self._set_status_help(self._about_btn, "Show info about this program.")
self.wm_protocol("WM_DELETE_WINDOW", self._destroy) # Intercept window manager closing.
# Fill the list with all available mods.
for mod_name in self.custom_args["mod_names"]:
self._add_mod(mod_name, False)
self._show_app_description()
self._patch_btn.focus_force()
def _set_status_help(self, widget, message):
"""Adds mouse-enter and mouse-leave triggers to show a statusbar message."""
def f(e):
if (hasattr(e.widget, "cget") and e.widget.cget("state") != "disabled"):
self.set_status_text(message)
widget.bind("<Enter>", f)
widget.bind("<Leave>", lambda e: self.set_status_text(""))
def _on_listbox_select(self, event):
current_selection = self._mod_listbox.curselection()
new_selection = [x for x in current_selection if (x not in self._prev_selection)]
self._prev_selection = set(current_selection)
if (len(new_selection) > 0):
mod_name = self._mod_listbox.get(new_selection[0])
self.show_mod_description(mod_name)
def _on_listbox_mouse_double_clicked(self, event):
clicked_index = self._mod_listbox.nearest(event.y)
if (self._mod_listbox.selection_includes(clicked_index)):
self._mod_listbox.selection_clear(clicked_index)
else:
self._mod_listbox.selection_set(clicked_index)
# The above methods won't fire a <<ListboxSelect>> event.
# But the single click handler will have fired already.
self._on_listbox_select(None)
return "break" # Consume the event.
def _on_listbox_mouse_pressed(self, event):
self._mouse_press_list_index = self._mod_listbox.nearest(event.y)
# Override normal selection: show a mod's description on single-click.
self._mod_listbox.activate(self._mouse_press_list_index)
self._mod_listbox.focus_force()
mod_name = self._mod_listbox.get(self._mouse_press_list_index)
self.show_mod_description(mod_name)
# A custom double-click handler to do list selection instead.
return "break" # Consume the event.
def _on_listbox_mouse_dragged(self, event):
if (self._mouse_press_list_index is None): return
current_selection = [int(i) for i in self._mod_listbox.curselection()]
n = self._mod_listbox.nearest(event.y)
if (n < self._mouse_press_list_index):
x = self._mod_listbox.get(n)
self._mod_listbox.delete(n)
self._mod_listbox.insert(n+1, x)
if ((n) in current_selection): self._mod_listbox.selection_set(n+1)
self._mouse_press_list_index = n
elif (n > self._mouse_press_list_index):
x = self._mod_listbox.get(n)
self._mod_listbox.delete(n)
self._mod_listbox.insert(n-1, x)
if ((n) in current_selection): self._mod_listbox.selection_set(n-1)
self._mouse_press_list_index = n
def _add_mod(self, modname, selected):
"""Add a mod name to the list."""
newitem = self._mod_listbox.insert(tk.END, modname)
if (selected):
self._mod_listbox.selection_set(newitem)
def _set_description(self, title, author=None, version=None, url=None, description=None):
"""Sets the currently displayed mod description."""
self._desc_area.configure(state="normal")
self._desc_area.delete("1.0", tk.END)
self._hyperman.reset()
self._desc_area.insert(tk.END, (title +"\n"), "title")
first = True
if (author):
self._desc_area.insert(tk.END, "%sby %s" % (("" if (first) else " "), author))
first = False
if (version):
self._desc_area.insert(tk.END, "%s(version %s)" % (("" if (first) else " "), str(version)))
first = False
if (not first):
self._desc_area.insert(tk.END, "\n")
if (url):
self._desc_area.insert(tk.END, "Website: ")
if (re.match("^(?:https?|ftp)://", url)):
link_callback = lambda : webbrowser.open(url, new=2)
self._desc_area.insert(tk.END, "Link", self._hyperman.add(link_callback))
else:
self._desc_area.insert(tk.END, "%s" % url)
self._desc_area.insert(tk.END, "\n")
self._desc_area.insert(tk.END, "\n")
if (description):
self._desc_area.insert(tk.END, description)
else:
self._desc_area.insert(tk.END, "No description.")
self._desc_area.configure(state="disabled")
def show_mod_description(self, mod_name):
if (mod_name in self._root().mod_hashes):
mod_hash = self._root().mod_hashes[mod_name]
mod_info = self._root().mod_db.get_mod_info(hash=mod_hash)
if (mod_info is not None):
# Don't assume solely hash searching today will guarantee
# the hash in among results' versions tomorrow.
mod_ver = mod_info.get_version(mod_hash)
if (mod_ver is None):
mod_ver = "???"
self._set_description(mod_info.get_title(), author=mod_info.get_author(), version=mod_ver, url=mod_info.get_url(), description=mod_info.get_desc())
else:
desc = "No info is available for the selected mod.\n\n"
desc += "If it's stable, please let the GMM devs know\n"
desc += "where you found it and include this md5 hash:\n"
desc += str(mod_hash) +"\n"
self._set_description(mod_name, description=desc)
logging.info("No info for selected mod: %s (%s)." % (mod_name, mod_hash))
else:
self._set_description(mod_name, description="The selected mod has not been identified by its hash yet.\nTry again in a few seconds.")
def _patch(self):
# Remember the names to return in _on_delete().
self._reordered_mods = self._mod_listbox.get(0, tk.END)
self._pending_mods = [self._mod_listbox.get(n) for n in self._mod_listbox.curselection()]
self._destroy()
def _toggle_all(self):
"""Select all mods, or none if all are already selected."""
current_selection = self._mod_listbox.curselection()
if (len(current_selection) == self._mod_listbox.size()):
self._mod_listbox.selection_clear(0, tk.END)
else:
self._mod_listbox.selection_set(0, tk.END)
def _validate_selected_mod(self):
"""Checks the selected mod for invalid XML."""
mod_names = [self._mod_listbox.get(n) for n in self._mod_listbox.curselection()]
result = ""
any_invalid = False
for mod_name in mod_names:
mod_path = find_mod(mod_name)
if (mod_path):
mod_result, mod_valid = validate_mod(mod_path)
result += mod_result
result += "\n"
if (not mod_valid):
any_invalid = True
if (not result):
result = "No mods were checked."
elif (any_invalid):
result += "FTL itself can tolerate lots of errors and still run. "
result += "But invalid XML may break tools that do proper parsing, "
result += "and it hinders the development of new tools.\n"
self._set_description("Results", description=result)
def _show_app_description(self):
"""Shows info about this program."""
self._set_description("Grognak's Mod Manager", author="Grognak", version=global_config.APP_VERSION, url=global_config.APP_URL, description="Thanks for using GMM.\nMake sure to visit the forum for updates!")
def set_status_text(self, message):
"""Sets the text in the status bar."""
self._statusbar.configure(text=message)
def center_window(self):
"""Centers this window on the screen.
Mostly. Window manager decoration and the menubar aren't factored in.
"""
# An event-driven call to this would go nuts with plain update().
self.update_idletasks() # Make window width/height methods work.
xp = (self.winfo_screenwidth()//2) - (self.winfo_width()//2)
yp = (self.winfo_screenheight()//2) - (self.winfo_height()//2)
self.geometry("+%d+%d" % (xp, yp))
def _on_delete(self):
# The window manager closed this window.
self._destroy()
def _destroy(self):
"""Destroys this window, but triggers a callback first."""
# If patch was clicked, names will be returned. Otherwise None.
if (self.custom_args["next_func"]):
self._root().invoke_later(self.custom_args["next_func"], {"all_mods":self._reordered_mods, "selected_mods":self._pending_mods})
self.destroy()
def append_xml_file(src_path, dst_path):
"""Semi-intelligently appends XML from one file (src) onto another (dst).
UTF-8 BOMs will be pruned.
Any XML declaration in the source will be pruned.
The destination's declaration will be replaced with one for utf-8.
This assumes the encoding of both files can be decoded as utf-8 (ascii's fine).
CR-LF line endings will be normalized to LF for reading, then written as CR-LF.
Any uncommented <gmm:blueprintListAppend> tag in the src will be removed, and
a new blueprintList will be added, including names from both the src's tag and
from the last list in dst that had the same name. FTL will honor this combined
list instead.
"""
src_text = ""
dst_text = ""
xml_decl_ptn = re.compile("<[?]xml version=\"1.0\" encoding=\"[^\"]+?\"[?]>\n*")
def get_text(f):
f_bytes = f.read()
if (f_bytes.startswith(codecs.BOM_UTF8)):
f_bytes = f_bytes[len(codecs.BOM_UTF8):]
f_text = f_bytes.decode("utf-8")
f_text = re.sub("\r?\n", "\n", f_text)
return f_text
with open(src_path, "rb") as src_file:
src_text = get_text(src_file)
src_text = xml_decl_ptn.sub("", src_text) # Prune any declaration.
if (src_text == ""):
return # Nothing to append.
if (os.path.isfile(dst_path)):
with open(dst_path, "rb") as dst_file:
dst_text = get_text(dst_file)
dst_text = xml_decl_ptn.sub("", dst_text) # Prune any declaration.
# Handle <gmm:blueprintListAppend>, if present.
# A new list will be added with entries from both dst and src.
bp_list_ptn = re.compile("(?s)[ \t]*<blueprintList\\sname\\s?=\\s?\"([^\"]+)\"[^>]*>\n?(.*?)</blueprintList>\n?")
bp_append_ptn = re.compile("(?s)[ \t]*<gmm:blueprintListAppend\\sname\\s?=\\s?\"([^\"]+)\"[^>]*>\n?(.*?)</gmm:blueprintListAppend>\n?")
bp_name_ptn = re.compile("<name>([^>]+)</name>")
if (bp_append_ptn.search(src_text) is not None):
dst_bp_lists = {} # Map original list names to their content.
src_bp_lists = {} # Map append list names to their content.
def replacer(m):
last_comment_start = dst_text.rfind("<!--", 0, m.start())
if (last_comment_start == -1 or dst_text.rfind("-->", last_comment_start, m.start()) > last_comment_start):
list_name = m.group(1)
list_content = re.sub("(?s)<!--.*?-->", "", m.group(2))
list_entries = []
for m in bp_name_ptn.finditer(list_content):
list_entries.append(m.group(1))
dst_bp_lists[list_name] = list_entries
return m.group(0) # Leave text as-is.
_ = re.sub(bp_list_ptn, replacer, dst_text)
def replacer(m):
last_comment_start = src_text.rfind("<!--", 0, m.start())
if (last_comment_start == -1 or src_text.rfind("-->", last_comment_start, m.start()) > last_comment_start):
list_name = m.group(1)
list_content = re.sub("(?s)<!--.*?-->", "", m.group(2))
list_entries = []
for m in bp_name_ptn.finditer(list_content):
list_entries.append(m.group(1))
src_bp_lists[list_name] = (src_bp_lists[list_name] if (list_name in src_bp_lists) else []) + list_entries
return "" # Remove the tag.
else:
return m.group(0) # Leave text as-is.
src_text = re.sub(bp_append_ptn, replacer, src_text)
for (name, entries) in src_bp_lists.items():
src_text += "\n<blueprintList name=\"%s\">\n" % name
for entry in ((dst_bp_lists[name] if (name in dst_bp_lists) else []) + entries):
src_text += " <name>%s</name>\n" % entry
src_text += "</blueprintList>\n"
# Write the combined text.
with open(dst_path, "wb") as dst_file:
new_text = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
new_text += dst_text +"\n\n<!-- Appended by GMM -->\n\n"+ src_text +"\n"
new_text = re.sub("\n", "\r\n", new_text)
dst_file.write(new_text.encode("utf-8"))
def append_file(src_path, dst_path):
"""Blindly appends bytes from one file onto another."""
src_file = None
dst_file = None
try:
src_file = open(src_path, "rb")
dst_file = open(dst_path, "ab")
dst_file.write(src_file.read())
finally:
for f in [src_file, dst_file]:
if (f is not None):
f.close()
def merge_file(src_path, dst_path):
pass
def packdat(unpack_dir, dat_path):
logging.info("")
logging.info("Repacking %s" % os.path.basename(dat_path))
logging.info("Listing files to pack...")
s = [()]
files = []
while s:
current = s.pop()
for child in os.listdir(os.path.join(unpack_dir, *current)):
full_path = os.path.join(unpack_dir, *(current + (child,)))
if (os.path.isfile(full_path)):
files.append(current + (child,))
elif (os.path.isdir(full_path)):
s.append(current + (child,))
logging.info("Creating datfile...")
index_size = len(files)
packer = ftldat.FTLPack(open(dat_path, "wb"), create=True, index_size=index_size)
logging.info("Packing...")
for _file in files:
full_path = os.path.join(unpack_dir, *_file)
size = os.stat(full_path).st_size
with open(full_path, "rb") as f:
packer.add(ftldat.ftl_path_join(*_file), f, size)
def unpackdat(dat_path, unpack_dir):
logging.info("Unpacking %s..." % os.path.basename(dat_path))
unpacker = ftldat.FTLPack(open(dat_path, "rb"))
for (i, filename, size, offset) in unpacker.list_metadata():
target = os.path.join(unpack_dir, filename)
if (not os.path.exists(os.path.dirname(target))):
os.makedirs(os.path.dirname(target))
with open(target, "wb") as f:
unpacker.extract_to(filename, f)
def is_dats_path_valid(dats_path):
"""Returns True if the path exists and contains data.dat."""
return (os.path.isdir(dats_path) and os.path.isfile(os.path.join(dats_path, "data.dat")))
def find_ftl_path():
"""Returns a valid guessed path to FTL resources, or None."""
steam_chunks = ["Steam","steamapps","common","FTL Faster Than Light","resources"];
gog_chunks = ["GOG.com","Faster Than Light","resources"];
home_path = os.path.expanduser("~")
xdg_data_home = os.getenv("XDG_DATA_HOME")
if (not xdg_data_home):
xdg_data_home = os.path.join(home_path, *[".local","share"])
candidates = []
# Windows - Steam
candidates.append(os.path.join(os.getenv("ProgramFiles(x86)", ""), *steam_chunks))
candidates.append(os.path.join(os.getenv("ProgramFiles", ""), *steam_chunks))
# Windows - GOG
candidates.append(os.path.join(os.getenv("ProgramFiles(x86)", ""), *gog_chunks))
candidates.append(os.path.join(os.getenv("ProgramFiles", ""), *gog_chunks))
# Linux - Steam
candidates.append(os.path.join(xdg_data_home, *["Steam","SteamApps","common","FTL Faster Than Light","data","resources"]))
# OSX - Steam
candidates.append(os.path.join(home_path, *["Library","Application Support","Steam","SteamApps","common","FTL Faster Than Light","FTL.app","Contents","Resources"]))
# OSX
candidates.append(os.path.join("/", *["Applications","FTL.app","Contents","Resources"]))
for c in candidates:
if (is_dats_path_valid(c)):
return c
return None
def prompt_for_ftl_path():
"""Returns a path to FTL resources chosen by the user, or None.
This should be called from a tkinter gui mainloop.
"""
message = ""
message += "The path to FTL's resources could not be guessed.\n\n";
message += "You will now be prompted to locate FTL manually.\n";
message += "Select '(FTL dir)/resources/data.dat'.\n";
message += "Or 'FTL.app', if you're on OSX.";
msgbox.showinfo(global_config.APP_NAME, message)
result = filedlg.askopenfilename(title="Find data.dat or FTL.app",
filetypes=[("data.dat or OSX Bundle", ("*.dat","*.app")), ("All Files", "*.*")])
if (result):
if (os.path.basename(result) == "data.dat"):
result = os.path.split(result)[0]
elif (os.path.splitext(result)[1] == ".app" and os.path.isdir(result)):
if (os.path.isdir(os.path.join(result, *["Contents","Resources"]))):
result = os.path.join(result, *["Contents","Resources"])
if (result and is_dats_path_valid(result)):
return result
return None
def find_mod(mod_name):
"""Returns the full path to a mod file, or None."""
suffixes = ["", ".ftl"]
if (global_config.allowzip): suffixes.append(".zip")
for suffix in suffixes:
tmp_path = os.path.join(global_config.dir_mods, mod_name + suffix)
if (os.path.isfile(tmp_path)):
return tmp_path
logging.debug("Failed to find mod file by name: %s" % mod_name)
return None
def load_modorder():
"""Reads the modorder, syncs it with existing files, and returns it."""
modorder_lines = []
try:
# Translate mod names to full filenames, temporarily.
with open(os.path.join(global_config.dir_mods, "modorder.txt"), "r") as modorder_file:
modorder_lines = modorder_file.readlines()
modorder_lines = [line.strip() for line in modorder_lines]
modorder_lines = [find_mod(line) for line in modorder_lines]
modorder_lines = [line for line in modorder_lines if (line is not None)]
modorder_lines = [os.path.basename(line) for line in modorder_lines]
except (IOError) as err:
# Ignore "No such file/dir."
if (err.errno == errno.ENOENT): pass
else: raise
mod_exts = ["ftl"]
if (global_config.allowzip): mod_exts.append("zip")
# Get a list of full filenames.
mod_filenames = []
for ext in mod_exts:
ext_filenames = [ f for f in os.listdir(global_config.dir_mods) if (fnmatch.fnmatch(f, "*."+ext)) ]
ext_filenames = [os.path.basename(f) for f in ext_filenames]
mod_filenames.extend(ext_filenames)
# Purge modorder lines that have no corresponding filename.
dead_mods = [f for f in modorder_lines if (f not in mod_filenames)]
for f in dead_mods:
modorder_lines.remove(f)
logging.info("Removed %s" % f)
# Append filenames not mentioned in the modorder.
new_mods = [f for f in mod_filenames if (f not in modorder_lines)]
for f in new_mods:
modorder_lines.append(f)
logging.info("Added %s" % f)
# Strip extensions to get mod_names.
ext_ptn = re.compile("[.](?:"+ ("|".join(mod_exts)) +")$", flags=re.I)
modorder_lines = [ext_ptn.sub("", f) for f in modorder_lines]
return modorder_lines
def save_modorder(modorder_lines):
with open(os.path.join(global_config.dir_mods, "modorder.txt"), "w") as modorder_file:
modorder_file.write("\n".join(modorder_lines) +"\n")
def hash_file(path, blocksize=65536):
"""Returns an md5 hash string based on a file's contents."""
hasher = hashlib.md5()
with open(path, "rb") as f:
buf = f.read(blocksize)
while len(buf) > 0:
hasher.update(buf)
buf = f.read(blocksize)
return hasher.hexdigest()
def patch_dats(selected_mods, keep_alive_func=None, sleep_func=None):
"""Backs up, clobbers, unpacks, merges, and finally packs dats.
:param selected_mods: A list of mod names to install.
:param keep_alive_func: Optional replacement to get an abort boolean.
:param sleep_func: Optional replacement to sleep N seconds.
:return: True if successful, False otherwise.
"""
if (keep_alive_func is None): keep_alive_func = global_config.keeping_alive
if (sleep_func is None): sleep_func = global_config.nap
# Get full paths from mod names.
mod_list = [find_mod(mod_name) for mod_name in selected_mods]
mod_list = [mod_path for mod_path in mod_list if (mod_path is not None)]
data_dat_path = os.path.join(global_config.dir_res, "data.dat")
resource_dat_path = os.path.join(global_config.dir_res, "resource.dat")
data_bak_path = os.path.join(global_config.dir_backup, "data.dat.bak")
resource_bak_path = os.path.join(global_config.dir_backup, "resource.dat.bak")
data_unp_path = None
resource_unp_path = None
tmp = None
modded_items = [] # Tracks changed files in case they're clobbered.
try:
# Create backup dats, if necessary.
for (dat_path, bak_path) in [(data_dat_path,data_bak_path), (resource_dat_path,resource_bak_path)]:
if (not os.path.isfile(bak_path)):
logging.info("Backing up %s" % os.path.basename(dat_path))
sh.copy2(dat_path, bak_path)
if (not keep_alive_func()): return False
if (len(mod_list) == 0):
# No mods. Skip the repacking.
# Just clobber current dat files with their respective backups.
for (dat_path, bak_path) in [(data_dat_path,data_bak_path), (resource_dat_path,resource_bak_path)]:
sh.copy2(bak_path, dat_path)
return True
# Use temp folders for unpacking.
data_unp_path = tf.mkdtemp()
resource_unp_path = tf.mkdtemp()
unp_map = {}
for x in ["data"]:
unp_map[x] = data_unp_path
for x in ["audio", "fonts", "img"]:
unp_map[x] = resource_unp_path
# Extract both of the backup dats.
unpackdat(data_bak_path, data_unp_path)
unpackdat(resource_bak_path, resource_unp_path)
vanilla_items = []
vanilla_items.extend(list(ftldat.FolderPack(data_unp_path).list()))
vanilla_items.extend(list(ftldat.FolderPack(resource_unp_path).list()))
vanilla_items_lower = [x.lower() for x in vanilla_items]
def check_case(path):
"""Compares mods' files' paths to vanilla paths.
Both should be relative to the unpack dir, with forward slashes.
"""
path_lower = path.lower()
if (path not in vanilla_items and path_lower in vanilla_items_lower):
logging.warning("Modded file's case doesn't match vanilla path: \"%s\" vs \"%s\"" % (path, vanilla_items[vanilla_items_lower.index(path_lower)]))
# Extract each mod into a temp dir and merge into unpacked dat dirs.
for mod_path in mod_list:
if (not keep_alive_func()): return False
try:
logging.info("")
logging.info("Installing mod: %s" % os.path.basename(mod_path))
tmp = tf.mkdtemp()
# Python 2.6 didn't support with+ZipFile. :/
mod_zip = None
try:
# Unzip everything into a temporary folder.
mod_zip = zf.ZipFile(mod_path, "r")
for item in mod_zip.namelist():
if (item.endswith("/")):
path = os.path.join(tmp, item)
if (not os.path.exists(path)):
os.makedirs(path)
else:
mod_zip.extract(item, tmp)
finally:
if (mod_zip is not None):
mod_zip.close()
# Go through each directory in the mod.
for directory in os.listdir(tmp):
if (directory in unp_map):
logging.info("Merging folder: %s" % directory)
unpack_dir = unp_map[directory]
for root, dirs, files in os.walk(os.path.join(tmp, directory)):
for d in dirs:
path = os.path.join(unpack_dir, root[len(tmp)+1:], d)
if (not os.path.exists(path)):
os.makedirs(path)
for f in files:
# A path to f's parent, relative to the unpack dir, with forward slashes.
packed_path = re.sub("/*$", "/", re.sub("\\\\", "/", root[len(tmp)+1:]))
if (f.endswith(".xml.append")):
dst_filename = f[:-len(".append")]
check_case(packed_path + dst_filename)
append_xml_file(os.path.join(root, f), os.path.join(unpack_dir, root[len(tmp)+1:], dst_filename))
modded_items.append(packed_path + dst_filename)
elif (f.endswith(".append.xml")):
dst_filename = f[:-len(".append.xml")]+".xml"
check_case(packed_path + dst_filename)
append_xml_file(os.path.join(root, f), os.path.join(unpack_dir, root[len(tmp)+1:], dst_filename))
modded_items.append(packed_path + dst_filename)
elif (f.endswith(".append")):
dst_filename = f[:-len(".append")]
check_case(packed_path + dst_filename)
append_file(os.path.join(root, f), os.path.join(unpack_dir, root[len(tmp)+1:], dst_filename))
modded_items.append(packed_path + dst_filename)
elif (f.endswith(".merge")):
dst_filename = f[:-len(".merge")]
check_case(packed_path + dst_filename)
merge_file(os.path.join(root, f), os.path.join(unpack_dir, root[len(tmp)+1:], dst_filename))
elif (f.endswith("merge.xml")):
dst_filename = f[:-len(".merge.xml")]+".xml"
check_case(packed_path + dst_filename)
merge_file(os.path.join(root, f), os.path.join(unpack_dir, root[len(tmp)+1:], dst_filename))
else:
dst_filename = f
check_case(packed_path + dst_filename)
if ((packed_path + dst_filename) in modded_items):
logging.warning("Clobbering earlier mods: %s" % (packed_path + dst_filename))
sh.copy2(os.path.join(root, f), os.path.join(unpack_dir, root[len(tmp)+1:], dst_filename))
modded_items.append(packed_path + dst_filename)
else:
logging.warning("Unsupported folder: %s" % directory)
modded_items = sorted(set(modded_items)) # Prune duplicates.
finally:
# Clean up temporary mod folder's contents.
if (tmp is not None):
sh.rmtree(tmp, ignore_errors=True)
tmp = None
if (not keep_alive_func()): return False
# All the mods are installed, so repack the files.
packdat(data_unp_path, data_dat_path)
packdat(resource_unp_path, resource_dat_path)
return True # The finally block will still run.
finally:
# Delete unpack folders.
for unpack_dir in [data_unp_path, resource_unp_path]:
if (not unpack_dir): continue
sh.rmtree(unpack_dir, ignore_errors=True)
if (os.path.exists(unpack_dir)):
logging.warning("Failed to delete unpack folder: %s" % unpack_dir)