-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTrackerUtils.py
More file actions
1083 lines (903 loc) · 38.2 KB
/
TrackerUtils.py
File metadata and controls
1083 lines (903 loc) · 38.2 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
import os
import re
import shutil
import datetime
import xml.etree.ElementTree as ET
import Constants
CURRENT_LICENSE = "CC_BY-NC_4"
MAX_SECONDARY_CREDIT = 2
PHASE_INCOMPLETE = 0
PHASE_EXISTS = 1
PHASE_FULL = 2
class CreditEvent:
"""
A class for handling credit in credit history
"""
def __init__(self, datetime, name, old, license, changed):
self.datetime = datetime
self.name = name
self.old = old
self.license = license
self.changed = changed
def getStatusEmoji(chosen_node, asset_type):
pending = chosen_node.__dict__[asset_type+"_pending"]
added = chosen_node.__dict__[asset_type + "_credit"].primary != ""
complete = chosen_node.__dict__[asset_type+"_complete"]
required = chosen_node.__dict__[asset_type+"_required"]
if complete > PHASE_EXISTS: # star
return "\u2B50"
elif len(pending) > 0:
if complete > PHASE_INCOMPLETE: # interrobang
return "\u2049"
else: # question
return "\u2754"
elif added:
if len(pending) > 0: # interrobang
return "\u2049"
else:
if complete > PHASE_INCOMPLETE: # checkmark
return "\u2705"
else: # white circle
return "\u26AA"
else:
if required: # X mark
return "\u274C"
else: # black circle
return "\u26AB"
def getCreditEntries(path):
credits = getFileCredits(path)
found_names = {}
credit_strings = []
for credit in credits:
credit_id = credit.name
if credit.old == "OLD":
continue
if credit_id not in found_names:
credit_strings.append(credit_id)
found_names[credit_id] = True
return credit_strings
def hasExistingCredits(cur_credits, orig_author, diff):
for credit in cur_credits:
if credit.name == orig_author:
event_diffs = credit.changed.split(',')
if diff in event_diffs:
return True
return False
def getFileCredits(path):
id_list = []
credit_path = os.path.join(path, Constants.CREDIT_TXT)
if os.path.exists(credit_path):
with open(credit_path, 'r', encoding='utf-8') as txt:
for line in txt:
credit = line.strip().split('\t')
if len(credit) >= 5:
id_list.append(CreditEvent(credit[0], credit[1], credit[2], credit[3], credit[4]))
else:
raise BaseException("Invalid credit line “{}” at {}".format(line, credit_path))
return id_list
def appendCredits(path, id, diff, is_old):
if diff == '':
diff = '"'
status = "CUR"
if is_old:
status = "OLD"
with open(os.path.join(path, Constants.CREDIT_TXT), 'a+', encoding='utf-8') as txt:
txt.write("{0}\t{1}\t{2}\t{3}\t{4}\n".format(str(datetime.datetime.utcnow()), id, status, CURRENT_LICENSE, diff))
def mergeCredits(path_from, path_to):
id_list = []
with open(path_to, 'r', encoding='utf-8') as txt:
for line in txt:
credit = line.strip().split('\t')
id_list.append(CreditEvent(credit[0], credit[1], credit[2], credit[3], credit[4]))
with open(path_from, 'r', encoding='utf-8') as txt:
for line in txt:
credit = line.strip().split('\t')
id_list.append(CreditEvent(credit[0], credit[1], credit[2], credit[3], credit[4]))
id_list = sorted(id_list, key=lambda x: x.datetime)
with open(path_to, 'w', encoding='utf-8') as txt:
for credit in id_list:
txt.write("{0}\t{1}\t{2}\t{3}\t{4}\n".format(credit.datetime, credit.name, credit.old, credit.license, credit.changed))
def shiftCredits(fullPath):
id_list = []
with open(fullPath, 'r', encoding='utf-8') as txt:
for line in txt:
id_list.append(line.strip().split('\t'))
for idx in range(len(id_list)):
if id_list[idx][1] == "CHUNSOFT":
id_list[idx][3] = "Unspecified"
with open(fullPath, 'w', encoding='utf-8') as txt:
for entry in id_list:
txt.write(entry[0] + "\t" + entry[1] + "\t" + entry[2] + "\t" + entry[3] + "\t" + entry[4] + "\n")
def deleteCredits(path, id):
id_list = []
fullPath = os.path.join(path, Constants.CREDIT_TXT)
with open(fullPath, 'r', encoding='utf-8') as txt:
for line in txt:
id_list.append(line.strip().split('\t'))
for entry in id_list:
if entry[1] == id:
entry[2] = "OLD"
with open(fullPath, 'w', encoding='utf-8') as txt:
for entry in id_list:
txt.write(entry[0] + "\t" + entry[1] + "\t" + entry[2] + "\t" + entry[3] + "\t" + entry[4] + "\n")
class CreditEntry:
"""
A class for determining a contributor's contribution
"""
def __init__(self, name, contact):
self.name = name
self.sprites = False
self.portraits = False
self.contact = contact
class CreditCompileEntry:
"""
A class for determining a contributor's contribution
"""
def __init__(self, name, contact):
self.name = name
self.sprite = {}
self.portrait = {}
self.contact = contact
class TrackerNode:
name: str
modreward: bool
def __init__(self, node_dict):
temp_list = [i for i in node_dict]
temp_list = sorted(temp_list)
main_dict = { }
for key in temp_list:
main_dict[key] = node_dict[key]
self.__dict__ = main_dict
if "sprite_talk" not in self.__dict__:
self.sprite_talk = {}
self.portrait_talk = {}
self.sprite_credit = CreditNode(node_dict["sprite_credit"])
self.portrait_credit = CreditNode(node_dict["portrait_credit"])
sub_dict = { }
for key in self.subgroups:
sub_dict[key] = TrackerNode(self.subgroups[key])
self.subgroups = sub_dict
def getDict(self):
node_dict = { }
for k in self.__dict__:
node_dict[k] = self.__dict__[k]
node_dict["sprite_credit"] = self.sprite_credit.getDict()
node_dict["portrait_credit"] = self.portrait_credit.getDict()
sub_dict = { }
for sub_idx in self.subgroups:
sub_dict[sub_idx] = self.subgroups[sub_idx].getDict()
node_dict["subgroups"] = sub_dict
return node_dict
class CreditNode:
def __init__(self, node_dict):
temp_list = [i for i in node_dict]
temp_list = sorted(temp_list)
main_dict = { }
for key in temp_list:
main_dict[key] = node_dict[key]
self.__dict__ = main_dict
def getDict(self):
node_dict = { }
for k in self.__dict__:
node_dict[k] = self.__dict__[k]
return node_dict
def loadNameFile(name_path):
name_dict = { }
first = True
with open(name_path, encoding='utf-8') as txt:
for line in txt:
if first:
first = False
continue
cols = line[:-1].split('\t')
name_dict[cols[1]] = CreditEntry(cols[0], cols[2])
return name_dict
def initCreditDict():
credit_dict = { }
credit_dict["primary"] = ""
credit_dict["secondary"] = []
credit_dict["total"] = 0
return credit_dict
def initSubNode(name, canon):
sub_dict = { }
sub_dict["name"] = name
sub_dict["canon"] = canon
sub_dict["modreward"] = not canon
sub_dict["portrait_complete"] = 0
sub_dict["portrait_credit"] = initCreditDict()
sub_dict["portrait_link"] = ""
sub_dict["portrait_files"] = {}
sub_dict["portrait_bounty"] = {}
sub_dict["portrait_modified"] = ""
sub_dict["portrait_pending"] = {}
sub_dict["portrait_recolor_link"] = ""
sub_dict["portrait_required"] = False
sub_dict["portrait_talk"] = {}
sub_dict["sprite_complete"] = 0
sub_dict["sprite_credit"] = initCreditDict()
sub_dict["sprite_files"] = {}
sub_dict["sprite_bounty"] = {}
sub_dict["sprite_link"] = ""
sub_dict["sprite_modified"] = ""
sub_dict["sprite_pending"] = {}
sub_dict["sprite_recolor_link"] = ""
sub_dict["sprite_required"] = False
sub_dict["sprite_talk"] = {}
sub_dict["subgroups"] = {}
return TrackerNode(sub_dict)
def getCurrentCompletion(orig_dict, dict, prefix):
for orig_file in orig_dict.__dict__[prefix + "_files"]:
if orig_file not in dict.__dict__[prefix + "_files"]:
return PHASE_INCOMPLETE
if prefix == "sprite":
completion = PHASE_FULL
while completion > PHASE_INCOMPLETE:
has_all = True
for idx in Constants.COMPLETION_ACTIONS[completion]:
file = Constants.ACTIONS[idx]
if file not in dict.__dict__[prefix + "_files"]:
has_all = False
break
if has_all:
break
completion -= 1
return completion
else:
completion = PHASE_FULL
search_flip = False
for file in dict.__dict__[prefix + "_files"]:
if file.endswith("^"):
search_flip = True
break
while completion > PHASE_INCOMPLETE:
has_all = True
for idx in Constants.COMPLETION_EMOTIONS[completion]:
file = Constants.EMOTIONS[idx]
if file not in dict.__dict__[prefix + "_files"]:
has_all = False
break
if search_flip:
file_flip = file + "^"
if file_flip not in dict.__dict__[prefix + "_files"]:
has_all = False
break
if has_all:
break
completion -= 1
return completion
def updateFiles(dict, species_path, prefix):
file_list = {}
if prefix == "sprite":
if os.path.exists(os.path.join(species_path, Constants.MULTI_SHEET_XML)):
tree = ET.parse(os.path.join(species_path, Constants.MULTI_SHEET_XML))
root = tree.getroot()
anims_node = root.find('Anims')
for anim_node in anims_node.iter('Anim'):
name = anim_node.find('Name').text
file_list[name] = True
else:
for inFile in os.listdir(species_path):
if inFile.endswith(".png"):
file, _ = os.path.splitext(inFile)
file_list[file] = True
for file in file_list:
if file not in dict.__dict__[prefix + "_files"]:
dict.__dict__[prefix + "_files"][file] = False
to_remove = []
for file in dict.__dict__[prefix + "_files"]:
if file not in file_list:
to_remove.append(file)
for file in to_remove:
if dict.__dict__[prefix + "_files"][file]:
print("Locked file no longer exists: {0} {1}".format(species_path, file))
else:
del dict.__dict__[prefix + "_files"][file]
def updateCreditFromEntries(credit_data, credit_entries):
# updates just the total count and the secondary
# the primary author is user-defined
credit_data.total = len(credit_entries)
credit_data.secondary.clear()
for credit_entry in reversed(credit_entries):
if credit_entry != credit_data.primary:
credit_data.secondary.insert(0, credit_entry)
if len(credit_data.secondary) >= MAX_SECONDARY_CREDIT:
break
def fileSystemToJson(dict, species_path, prefix, tier):
# get last modify date of everything that isn't credits.txt or dirs
last_modify = ""
for inFile in os.listdir(species_path):
fullPath = os.path.join(species_path, inFile)
if os.path.isdir(fullPath):
if inFile not in dict.subgroups:
# init name if applicable
if tier == 1:
if inFile == "0000":
dict.subgroups[inFile] = initSubNode("", dict.canon)
else:
dict.subgroups[inFile] = initSubNode("Form" + inFile, dict.canon)
elif tier == 2:
if inFile == "0001":
dict.subgroups[inFile] = initSubNode("Shiny", dict.canon)
else:
dict.subgroups[inFile] = initSubNode("", dict.canon)
elif tier == 3:
if inFile == "0001":
dict.subgroups[inFile] = initSubNode("Male", dict.canon)
elif inFile == "0002":
dict.subgroups[inFile] = initSubNode("Female", dict.canon)
else:
dict.subgroups[inFile] = initSubNode("", dict.canon)
fileSystemToJson(dict.subgroups[inFile], fullPath, prefix, tier + 1)
elif inFile == Constants.CREDIT_TXT:
#shiftCredits(fullPath)
credit_entries = getCreditEntries(species_path)
credit_data = dict.__dict__[prefix + "_credit"]
updateCreditFromEntries(credit_data, credit_entries)
else:
modify_datetime = datetime.datetime.utcfromtimestamp(os.path.getmtime(fullPath))
if str(modify_datetime) > last_modify:
last_modify = str(modify_datetime)
updateFiles(dict, species_path, prefix)
updated = False
if dict.__dict__[prefix + "_modified"] < last_modify:
dict.__dict__[prefix + "_modified"] = last_modify
updated = True
# the link always starts off blank, or is set to blank when last-modified is updated
if updated:
dict.__dict__[prefix + "_link"] = ""
def isDataPopulated(sub_dict, check_sprite = True, check_portrait = True, recursive = True):
if check_sprite and sub_dict.sprite_credit.primary != "":
return True
if check_portrait and sub_dict.portrait_credit.primary != "":
return True
if recursive:
for sub_idx in sub_dict.subgroups:
if isDataPopulated(sub_dict.subgroups[sub_idx], check_sprite, check_portrait, recursive):
return True
return False
def deleteData(tracker_dict, portrait_path, sprite_path, idx):
next_idx = "{:04d}".format(int(idx) + 1)
while next_idx in tracker_dict:
# replace with the index in front
tracker_dict[idx] = tracker_dict[next_idx]
# replace the path with the index in front
if os.path.exists(os.path.join(portrait_path, idx)):
shutil.rmtree(os.path.join(portrait_path, idx))
if os.path.exists(os.path.join(sprite_path, idx)):
shutil.rmtree(os.path.join(sprite_path, idx))
if os.path.exists(os.path.join(portrait_path, next_idx)):
shutil.move(os.path.join(portrait_path, next_idx), os.path.join(portrait_path, idx))
if os.path.exists(os.path.join(sprite_path, next_idx)):
shutil.move(os.path.join(sprite_path, next_idx), os.path.join(sprite_path, idx))
idx = next_idx
next_idx = "{:04d}".format(int(idx) + 1)
del tracker_dict[idx]
if os.path.exists(os.path.join(portrait_path, idx)):
shutil.rmtree(os.path.join(portrait_path, idx))
if os.path.exists(os.path.join(sprite_path, idx)):
shutil.rmtree(os.path.join(sprite_path, idx))
def getIdxName(tracker_dict, full_idx):
if len(full_idx) == 0:
return []
if full_idx[0] not in tracker_dict:
return []
node = tracker_dict[full_idx[0]]
if node.name == "":
return getIdxName(node.subgroups, full_idx[1:])
else:
return [node.name] + getIdxName(node.subgroups, full_idx[1:])
def findSlotIdx(sub_dict, name):
for idx in sub_dict:
if sub_dict[idx].name.lower() == name.lower():
return idx
return None
def iterateTracker(tracker_dict, func, full_idx):
for idx in tracker_dict:
node = tracker_dict[idx]
full_idx.append(idx)
func(full_idx)
sub_dict = node.subgroups
iterateTracker(sub_dict, func, full_idx)
full_idx.pop()
def isTrackerIdxEqual(idx1, idx2):
if len(idx1) != len(idx2):
return False
for ii in range(len(idx1)):
if idx1[ii] != idx2[ii]:
return False
return True
def findFullTrackerIdx(tracker_dict, name_args, depth):
# base case
if depth >= len(name_args):
return []
# recursive case
blank_idx = None
for idx in tracker_dict:
if tracker_dict[idx].name.lower() == name_args[depth].lower():
sub_dict = tracker_dict[idx].subgroups
full_idx = findFullTrackerIdx(sub_dict, name_args, depth+1)
if full_idx is None:
return None
else:
return [idx] + full_idx
elif tracker_dict[idx].name == "":
blank_idx = idx
# didn't find any name matches, check if the base name is blank
if blank_idx is not None:
full_idx = findFullTrackerIdx(tracker_dict[blank_idx].subgroups, name_args, depth)
if full_idx is None:
return None
else:
return [blank_idx] + full_idx
# otherwise, it means we just can't find it
return None
def isShinyIdx(full_idx):
if len(full_idx) < 3:
return False
return full_idx[2] == "0001"
def createShinyIdx(full_idx, shiny):
new_idx = [i for i in full_idx]
if len(new_idx) < 3 and not shiny:
return new_idx
while len(new_idx) < 3:
new_idx.append("0000")
if shiny:
new_idx[2] = "0001"
else:
new_idx[2] = "0000"
while len(new_idx) > 1 and new_idx[-1] == "0000":
new_idx.pop()
return new_idx
def getNodeFromIdx(tracker_dict, full_idx, depth):
if full_idx is None:
return None
if len(full_idx) == 0:
return None
if full_idx[depth] not in tracker_dict:
return None
# base case
node = tracker_dict[full_idx[depth]]
if depth == len(full_idx) - 1:
return node
# recursive case, kind of weird
return getNodeFromIdx(node.subgroups, full_idx, depth+1)
def getStatsFromFilename(filename):
# attempt to parse the filename to a destination
file, ext = os.path.splitext(filename)
name_idx = file.split("-")
prefix = name_idx[0].split("_")
asset_type = prefix[0]
recolor = "_".join(prefix[1:])
if asset_type != "sprite" and asset_type != "portrait":
return False, None, None, None
if recolor != "" and recolor != "recolor":
return False, None, None, None
if len(name_idx) < 2:
return False, None, None, None
full_idx = name_idx[1:]
return True, full_idx, asset_type, recolor == "recolor"
def genderDiffExists(form_dict, asset_type, gender):
if "0000" not in form_dict.subgroups:
return False
normal_dict = form_dict.subgroups["0000"].subgroups
gender_idx = findSlotIdx(normal_dict, gender)
if gender_idx is not None:
gender_dict = normal_dict[gender_idx]
if gender_dict.__dict__[asset_type + "_required"]:
return True
return False
def genderDiffPopulated(form_dict, asset_type):
if "0000" not in form_dict.subgroups:
return False
normal_dict = form_dict.subgroups["0000"].subgroups
genders = ["Male", "Female"]
for gender in genders:
gender_idx = findSlotIdx(normal_dict, gender)
if gender_idx is not None:
gender_dict = normal_dict[gender_idx]
if gender_dict.__dict__[asset_type + "_credit"].primary != "":
return True
return False
def createGenderDiff(form_dict, asset_type, gender_name):
if "0000" not in form_dict.subgroups:
form_dict.subgroups["0000"] = initSubNode("", form_dict.canon)
normal_dict = form_dict.subgroups["0000"]
createShinyGenderDiff(normal_dict, asset_type, gender_name)
shiny_dict = form_dict.subgroups["0001"]
createShinyGenderDiff(shiny_dict, asset_type, gender_name)
def createShinyGenderDiff(color_dict, asset_type, gender_name):
gender_idx = findSlotIdx(color_dict.subgroups, gender_name)
if gender_idx is None:
gender_dict = initSubNode(gender_name, color_dict.canon)
if gender_name == "Male":
color_dict.subgroups["0001"] = gender_dict
else:
color_dict.subgroups["0002"] = gender_dict
else:
gender_dict = color_dict.subgroups[gender_idx]
gender_dict.__dict__[asset_type + "_required"] = True
def removeGenderDiff(form_dict, asset_type):
normal_dict = form_dict.subgroups["0000"]
nothing_left = removeColorGenderDiff(normal_dict, asset_type, "Male")
nothing_left &= removeColorGenderDiff(normal_dict, asset_type, "Female")
if nothing_left:
del form_dict.subgroups["0000"]
shiny_dict = form_dict.subgroups["0001"]
removeColorGenderDiff(shiny_dict, asset_type, "Male")
removeColorGenderDiff(shiny_dict, asset_type, "Female")
def removeColorGenderDiff(color_dict, asset_type, gender):
# return whether or not the gender was fully deleted
gender_idx = findSlotIdx(color_dict.subgroups, gender)
if gender_idx is None:
return True
gender_dict = color_dict.subgroups[gender_idx]
gender_dict.__dict__[asset_type + "_required"] = False
if not gender_dict.__dict__["sprite_required"] and not gender_dict.__dict__["portrait_required"]:
del color_dict.subgroups[gender_idx]
return True
return False
def createFormNode(name, canon):
forme_dict = initSubNode(name, canon)
forme_dict.sprite_required = True
forme_dict.portrait_required = True
shiny_dict = initSubNode("Shiny", canon)
forme_dict.subgroups["0001"] = shiny_dict
shiny_dict.sprite_required = True
shiny_dict.portrait_required = True
return forme_dict
def createSpeciesNode(name):
sub_dict = initSubNode(name, True)
sub_dict.sprite_required = True
sub_dict.portrait_required = True
forme_dict = initSubNode("", True)
sub_dict.subgroups["0000"] = forme_dict
shiny_dict = initSubNode("Shiny", True)
forme_dict.subgroups["0001"] = shiny_dict
shiny_dict.sprite_required = True
shiny_dict.portrait_required = True
return sub_dict
def clearSubmissions(node):
node.sprite_pending = { }
node.portrait_pending = { }
for sub_idx in node.subgroups:
clearSubmissions(node.subgroups[sub_idx])
"""
Name operations
"""
def updateCreditCompilation(name_path, credit_dict):
with open(name_path, 'w+', encoding='utf-8') as txt:
txt.write("All custom graphics not originating from official PMD games are licensed under Attribution-NonCommercial 4.0 International http://creativecommons.org/licenses/by-nc/4.0/.\n")
txt.write("All graphics referred to in this file can be found in http://sprites.pmdcollab.org/\n\n")
for handle in credit_dict:
if len(credit_dict[handle].sprite) > 0 or len(credit_dict[handle].portrait) > 0:
name_components = []
if len(credit_dict[handle].name):
name_components.append(credit_dict[handle].name)
if handle.startswith("<@!"):
name_components.append("Discord:{0}".format(handle))
if len(credit_dict[handle].contact):
name_components.append("Contact:{0}".format(credit_dict[handle].contact))
txt.write("{0}\n".format("\t".join(name_components)))
if len(credit_dict[handle].portrait) > 0:
txt.write("\tPortrait:\n")
# for each portrait
for id_key in credit_dict[handle].portrait:
all_parts = []
for subpart in credit_dict[handle].portrait[id_key]:
all_parts.append(subpart)
if len(all_parts) > 0:
txt.write("\t\t{0}: {1}\n".format(id_key, ",".join(all_parts)))
if len(credit_dict[handle].sprite) > 0:
txt.write("\tSprite:\n")
# for each sprite
for id_key in credit_dict[handle].sprite:
all_parts = []
for subpart in credit_dict[handle].sprite[id_key]:
all_parts.append(subpart)
if len(all_parts) > 0:
txt.write("\t\t{0}: {1}\n".format(id_key, ",".join(all_parts)))
txt.write("\n")
def updateCompilationStats(name_dict, dict, species_path, prefix, form_name_list, credit_dict):
# generate the form name
form_name = " ".join([i for i in form_name_list if i != ""])
# is there a credits txt? read it
credits = getFileCredits(species_path)
# for each entry, update the credit dict
for credit in credits:
if credit.old == "CUR":
# add entry if not existing
if credit.name not in credit_dict:
if credit.name in name_dict:
credit_dict[credit.name] = CreditCompileEntry(name_dict[credit.name].name, name_dict[credit.name].contact)
else:
credit_dict[credit.name] = CreditCompileEntry("", "")
compile_entry = credit_dict[credit.name]
asset_dict = compile_entry.__dict__[prefix]
if form_name not in asset_dict:
asset_dict[form_name] = {}
subpart_dict = asset_dict[form_name]
subpart_list = credit.changed.split(',')
for subpart in subpart_list:
subpart_dict[subpart] = True
for sub_dict in dict.subgroups:
form_name_list.append(dict.subgroups[sub_dict].name)
updateCompilationStats(name_dict, dict.subgroups[sub_dict], os.path.join(species_path, sub_dict), prefix, form_name_list, credit_dict)
form_name_list.pop()
def updateNameFile(name_path, name_dict, include_all):
with open(name_path, 'w+', encoding='utf-8') as txt:
txt.write("Name\tDiscord\tContact\n")
for handle in name_dict:
if include_all or name_dict[handle].sprites or name_dict[handle].portraits:
txt.write("{0}\t{1}\t{2}\n".format(name_dict[handle].name, handle, name_dict[handle].contact))
def addNameStat(name_dict, name, portrait):
if name != "":
if name not in name_dict:
name_dict[name] = CreditEntry("", "")
if not portrait:
name_dict[name].sprites = True
else:
name_dict[name].portraits = True
def updateNameStats(name_dict, dict):
addNameStat(name_dict, dict.sprite_credit.primary, False)
for secondary in dict.sprite_credit.secondary:
addNameStat(name_dict, secondary, False)
addNameStat(name_dict, dict.portrait_credit.primary, True)
for secondary in dict.portrait_credit.secondary:
addNameStat(name_dict, secondary, True)
for sub_dict in dict.subgroups:
updateNameStats(name_dict, dict.subgroups[sub_dict])
def renameJsonCredits(dict, old_name, new_name):
if dict.sprite_credit.primary == old_name:
dict.sprite_credit.primary = new_name
for idx in range(len(dict.sprite_credit.secondary)):
if dict.sprite_credit.secondary[idx] == old_name:
dict.sprite_credit.secondary[idx] = new_name
if dict.portrait_credit.primary == old_name:
dict.portrait_credit.primary = new_name
for idx in range(len(dict.portrait_credit.secondary)):
if dict.portrait_credit.secondary[idx] == old_name:
dict.portrait_credit.secondary[idx] = new_name
for sub_dict in dict.subgroups:
renameJsonCredits(dict.subgroups[sub_dict], old_name, new_name)
def renameFileCredits(species_path, old_name, new_name):
# renames all mentions of an author to a different author
for inFile in os.listdir(species_path):
fullPath = os.path.join(species_path, inFile)
if os.path.isdir(fullPath):
renameFileCredits(fullPath, old_name, new_name)
elif inFile == Constants.CREDIT_TXT:
id_list = []
with open(fullPath, 'r', encoding='utf-8') as txt:
for line in txt:
id_list.append(line.strip().split('\t'))
for entry in id_list:
if entry[1] == old_name:
entry[1] = new_name
with open(fullPath, 'w', encoding='utf-8') as txt:
for entry in id_list:
txt.write(entry[0] + "\t" + entry[1] + "\t" + entry[2] + "\t" + entry[3] + "\t" + entry[4] + "\n")
def getDirFromIdx(base_path, asset_type, full_idx):
full_arr = [base_path, asset_type] + full_idx
return os.path.join(*full_arr)
def moveNodeFiles(dir_from, dir_to, merge_credit, is_dir):
cur_files = os.listdir(dir_from)
for file in cur_files:
# exclude tmp as it is a special folder name for temp files
if file == "tmp":
continue
full_base_path = os.path.join(dir_from, file)
if merge_credit and file == Constants.CREDIT_TXT:
#mergeCredits(full_base_path, os.path.join(dir_to, file))
#os.remove(full_base_path)
shutil.move(full_base_path, os.path.join(dir_to, file))
elif os.path.isdir(full_base_path) == is_dir:
shutil.move(full_base_path, os.path.join(dir_to, file))
def copyNodeFiles(dir_from, dir_to, is_dir):
cur_files = os.listdir(dir_from)
for file in cur_files:
full_base_path = os.path.join(dir_from, file)
if os.path.isdir(full_base_path) == is_dir:
shutil.copy(full_base_path, os.path.join(dir_to, file))
def deleteNodeFiles(dir_to, include_credit):
cur_files = os.listdir(dir_to)
for file in cur_files:
# exclude tmp as it is a special folder name for temp files
if file == "tmp":
continue
full_base_path = os.path.join(dir_to, file)
if os.path.isdir(full_base_path):
continue
if include_credit or file != Constants.CREDIT_TXT:
os.remove(full_base_path)
def clearCache(chosen_node, recursive):
chosen_node.sprite_link = ""
chosen_node.portrait_link = ""
chosen_node.sprite_recolor_link = ""
chosen_node.portrait_recolor_link = ""
if recursive:
for subgroup in chosen_node.subgroups:
clearCache(chosen_node.subgroups[subgroup], recursive)
def setNodeModReward(chosen_node, modreward, recursive):
if chosen_node.name != "":
chosen_node.modreward = modreward
if recursive:
for subgroup in chosen_node.subgroups:
setNodeModReward(chosen_node.subgroups[subgroup], modreward, recursive)
def swapNodeMiscFeatures(node_from, node_to):
for key in node_from.__dict__:
if key.startswith("sprite"):
pass
elif key.startswith("portrait"):
pass
elif key == "canon" or key == "modreward" or key == "subgroups":
pass
else:
tmp = node_to.__dict__[key]
node_to.__dict__[key] = node_from.__dict__[key]
node_from.__dict__[key] = tmp
def swapNodeAssetFeatures(node_from, node_to, asset_type):
for key in node_from.__dict__:
if key.startswith(asset_type):
tmp = node_to.__dict__[key]
node_to.__dict__[key] = node_from.__dict__[key]
node_from.__dict__[key] = tmp
def copyNodeAssetFeatures(node_from, node_to, asset_type):
for key in node_from.__dict__:
if key.startswith(asset_type):
node_to.__dict__[key] = node_from.__dict__[key]
def replaceNodeAssetFeatures(node_from, node_to, asset_type):
node_new = initSubNode("", False)
for key in node_from.__dict__:
if key.startswith(asset_type):
if key == asset_type + "_files":
# pass in keys and set to false, only if new keys are introduced
for file_key in node_from.__dict__[key]:
if file_key not in node_to.__dict__[key]:
node_to.__dict__[key][file_key] = False
elif key == asset_type + "_talk":
# do not overwrite
pass
elif key == asset_type + "_bounty":
for status_key in node_from.__dict__[key]:
if status_key not in node_to.__dict__[key]:
node_to.__dict__[key][status_key] = 0
node_to.__dict__[key][status_key] = node_to.__dict__[key][status_key] + node_from.__dict__[key][status_key]
elif key == "canon" or key == "modreward":
# do not overwrite canon
pass
else:
node_to.__dict__[key] = node_from.__dict__[key]
node_from.__dict__[key] = node_new.__dict__[key]
def swapFolderPaths(base_path, tracker, asset_type, full_idx_from, full_idx_to):
# swap the nodes in tracker, don't do it recursively
chosen_node_from = getNodeFromIdx(tracker, full_idx_from, 0)
chosen_node_to = getNodeFromIdx(tracker, full_idx_to, 0)
swapNodeAssetFeatures(chosen_node_from, chosen_node_to, asset_type)
# prepare to swap textures
gen_path_from = getDirFromIdx(base_path, asset_type, full_idx_from)
gen_path_to = getDirFromIdx(base_path, asset_type, full_idx_to)
if not os.path.exists(gen_path_from):
os.makedirs(gen_path_from, exist_ok=True)
if not os.path.exists(gen_path_to):
os.makedirs(gen_path_to, exist_ok=True)
# move textures to a temp folder
gen_path_tmp = os.path.join(gen_path_from, "tmp")
os.makedirs(gen_path_tmp, exist_ok=True)
moveNodeFiles(gen_path_from, gen_path_tmp, False, False)
# swap the folders
moveNodeFiles(gen_path_to, gen_path_from, False, False)
moveNodeFiles(gen_path_tmp, gen_path_to, False, False)
shutil.rmtree(gen_path_tmp)
def copyFolderPaths(base_path, tracker, asset_type, full_idx_from, full_idx_to):
# swap the nodes in tracker, don't do it recursively
chosen_node_from = getNodeFromIdx(tracker, full_idx_from, 0)
chosen_node_to = getNodeFromIdx(tracker, full_idx_to, 0)
copyNodeAssetFeatures(chosen_node_from, chosen_node_to, asset_type)
# prepare to copy textures
gen_path_from = getDirFromIdx(base_path, asset_type, full_idx_from)
gen_path_to = getDirFromIdx(base_path, asset_type, full_idx_to)
if not os.path.exists(gen_path_to):
os.makedirs(gen_path_to, exist_ok=True)
# copy the folders
copyNodeFiles(gen_path_from, gen_path_to, False)
def replaceFolderPaths(base_path, tracker, asset_type, full_idx_from, full_idx_to):
# swap the nodes in tracker, don't do it recursively
chosen_node_from = getNodeFromIdx(tracker, full_idx_from, 0)
chosen_node_to = getNodeFromIdx(tracker, full_idx_to, 0)
replaceNodeAssetFeatures(chosen_node_from, chosen_node_to, asset_type)
# prepare to swap textures
gen_path_from = getDirFromIdx(base_path, asset_type, full_idx_from)
gen_path_to = getDirFromIdx(base_path, asset_type, full_idx_to)
if not os.path.exists(gen_path_from):
os.makedirs(gen_path_from, exist_ok=True)
if not os.path.exists(gen_path_to):
os.makedirs(gen_path_to, exist_ok=True)
# swap the folders
deleteNodeFiles(gen_path_to, False)
moveNodeFiles(gen_path_from, gen_path_to, True, False)
def swapNodeMiscCanon(chosen_node_from, chosen_node_to):
for key in chosen_node_from.__dict__:
if key == "canon" or key == "modreward":
tmp = chosen_node_to.__dict__[key]
chosen_node_to.__dict__[key] = chosen_node_from.__dict__[key]
chosen_node_from.__dict__[key] = tmp
for sub_id in chosen_node_from.subgroups:
if sub_id in chosen_node_to.subgroups:
sub_from = chosen_node_from.subgroups[sub_id]
sub_to = chosen_node_from.subgroups[sub_id]
swapNodeMiscCanon(sub_from, sub_to)
def swapAllSubNodes(base_path, tracker, full_idx_from, full_idx_to):
# swap the subnode objects
chosen_node_from = getNodeFromIdx(tracker, full_idx_from, 0)
chosen_node_to = getNodeFromIdx(tracker, full_idx_to, 0)
tmp = chosen_node_from.subgroups
chosen_node_from.subgroups = chosen_node_to.subgroups
chosen_node_to.subgroups = tmp
# swap back the canon-ness and modreward aspect
for sub_id in chosen_node_from.subgroups: