forked from RoadrunnerWMC/Level-Info-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel_info_editor.py
More file actions
1963 lines (1637 loc) · 72.8 KB
/
level_info_editor.py
File metadata and controls
1963 lines (1637 loc) · 72.8 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
# -*- coding: utf-8 -*-
# Level Info Editor - Edits NewerSMBW's LevelInfo.bin
# Version 1.5-utf-1.2
# Copyright (C) 2013-2021 RoadrunnerWMC, 2021-2022 Asu-chan, 2023-2024 @wakanameko2
# This file is part of Level Info Editor.
# Level Info Editor is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# Level Info Editor is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with Level Info Editor. If not, see <http://www.gnu.org/licenses/>.
# level_info_editor.py
# This is the main executable for Level Info Editor
################################################################
################################################################
AppName = 'Level Info Editor'
version = '1.5-UTF-1.2'
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
import os
# setup
STpath = os.path.dirname(__file__) + '/data.txt' # get setting datacd path
if not (os.path.isfile(STpath)): # make data.txt if couldn't found it
with open(STpath, 'w') as UserData:
UserData.write("ENL")
SFile = open(STpath, 'r') # open setting file
lines = SFile.readlines() # read setting file lines
print (lines)
#===========about Setting File(/data.txt)===========#
# EN -> MSLANG = 'English' L -> VMode = 'Light' #
# JP -> MSLANG = 'Japanese' D -> VMode = 'Dark' #
# example: ENL -> English & Light Mode #
#===================================================#
with open(STpath, "r") as f: # load language
for line in f:
if "EN" in line:
MSLANG = 'English'
print('Selected EN!')
else:
MSLANG = 'Japanese'
print('Selected JP!')
with open(STpath, "r") as f: # load appearance
for line in f:
if "L" in line:
VMode = 'Light'
print('Selected Light!')
else:
VMode = 'Dark'
print('Selected Dark!')
StyleSheet = '''
QWidget {
background-color: #111111;
color: #FEFFFE;
}
QVBoxLayout {
background-color: #111111;
color: #FEFFFE;
}
QMainWindow {
background-color: #15202B;
color: #FEFFFE;
}
QFileDialog {
background-color: #15202B;
color: #15202B;
}
QListWidget {
background-color: #15202b;
}
QGroupBox {
background-color: #111111;
}
QDialogButtonBox {
background-color: #111111;
}
QPlainTextEdit {
background-color: #15202b;
}
QDialog {
background-color: #111111;
}
QLabel {
background-color: #111111;
}
QSpinBox {
background-color: #15202b;
}
QHBoxLayout {
background-color: #111111;
}
QComboBox {
background-color: #15202b;
}
QCheckBox {
background-color: #111111;
}
QFormLayout {
background-color: #111111;
}
QLineEdit {
background-color: #15202b;
}
QGridLayout {
background-color: #111111;
}
QTabWidget {
background-color: #111111;
}
'''
# end of setup
class WorldInfo():
"""Class that represents a world"""
def __init__(self):
"""Initialises the WorldInfo"""
self.WorldNumber = None
self.HasL = False
self.HasR = False
self.LName = ''
self.RName = ''
self.Levels = []
def toPyObject(self):
"""Py2/Py3 compatibility"""
return self
def setWorldNumber(self, number=None):
"""Sets the world number (None is a valid value)"""
self.WorldNumber = number
def setLeftHalf(self, exists):
"""Turns the left half on or off"""
self.HasL = exists
def setRightHalf(self, exists):
"""Turns the right half on or off"""
self.HasR = exists
def setLeftName(self, name):
"""Sets the left half name"""
self.LName = name
def setRightName(self, name):
"""Sets the right half name"""
self.RName = name
class LevelInfo():
"""Class that represents a level"""
def __init__(self):
"""Initialises the LevelInfo"""
self.name = ''
self.FileW = 0
self.FileL = 0
self.DisplayW = 0
self.DisplayL = 0
self.IsLevel = True
self.NormalExit = False
self.SecretExit = False
self.RightSide = False
self.Vignette = 0
self.HideTimer = False
def toPyObject(self):
"""Py2/Py3 compatibility"""
return self
def setName(self, name):
"""Sets the level name"""
self.name = name
def setFileNameW(self, world):
"""Sets the filename (world number)"""
self.FileW = world
def setFileNameL(self, level):
"""Sets the filename (level number)"""
self.FileL = level
def setDisplayNameW(self, world):
"""Sets the Display name (world number)"""
self.DisplayW = world
def setDisplayNameL(self, level):
"""Sets the Display name (level number)"""
self.DisplayL = level
def setFlags(self, flags):
"""Sets some flags"""
self.IsLevel = ((flags >> 1) & 1 == 1)
self.NormalExit = ((flags >> 4) & 1 == 1)
self.SecretExit = ((flags >> 5) & 1 == 1)
self.RightSide = ((flags >> 10) & 1 == 1)
self.HideTimer = ((flags >> 8) & 1 == 1)
def getFlags(self):
"""Returns an int which represent the two bytes the flags are encoded in"""
b1 = 0
b2 = 0
if self.IsLevel: b1 += 0x02
if self.NormalExit: b1 += 0x10
if self.SecretExit: b1 += 0x20
if self.RightSide: b2 += 0x04
if self.HideTimer: b2 += 0x01
return (b2 << 8) | b1
def setVignette(self, vig):
"""Sets the Vignette (vignette number)"""
self.Vignette = vig
class LevelInfoFile():
"""Class that represents LevelInfo.bin"""
def __init__(self, rawdata=None, isOld=False):
"""Initialises the LevelInfoFile"""
if rawdata == None: self.initAsEmpty()
else: self.initFromData(rawdata, isOld)
def initAsEmpty(self):
"""Sets all the variables to their defaults"""
self.worlds = []
self.comments = ''
def initFromData(self, rawdata, isOld):
"""Initialises the LevelInfoFile from raw binary data"""
global levelNamesOffset
# Py2 opens binary files as strings; convert it to bytes
if sys.version[0] == '2':
new = []
for char in rawdata: new.append(ord(char))
rawdata = new
else:
new = []
for i in rawdata: new.append(i)
rawdata = new
# Check for the file header
if rawdata[0] != ord('N'): return
if rawdata[1] != ord('W'): return
if rawdata[2] != ord('R'): return
if rawdata[3] != ord('p'): return
# Load the world data offsets
numberOfWorlds = rawdata[7]
worldOffs = []
for worldNum in range(numberOfWorlds):
off = (rawdata[10 + (4*worldNum)] << 8) | rawdata[11 + (4*worldNum)]
worldOffs.append(off)
# Load the worlds
minTextOffset = 0xFFFF
worlds = []
for offset in worldOffs:
numberOfLevels = rawdata[offset + 3]
worldData = rawdata[offset+4: (offset+4) + (12*numberOfLevels)]
# Make a world and add levels/headers to it
world = WorldInfo()
headers = []
for levelOff in range(int(len(worldData)/12)):
levelData = worldData[levelOff*12:(levelOff*12) + 12]
# Find the text
textOffset = (levelData[10] << 8) | levelData[11]
if textOffset < minTextOffset: minTextOffset = textOffset
textLength = levelData[4]
text = ''
if isOld == True:
for char in rawdata[textOffset:textOffset + textLength]:
char = char + 0x30
if char > 255: char -= 256
text += chr(char)
else:
i = False
preChar = 0
for char in rawdata[textOffset:textOffset + (textLength*2)]:
if i == False:
preChar = char
else:
# char = char + 0x30
# if char > 255: char -= 256
newChar = (preChar << 8) | char
text += chr(newChar)
i = not i
# Add header info or levels
if levelData[3] >= 100:
# It's a world header
world.setWorldNumber(levelData[2])
if levelData[3] == 100:
world.setLeftHalf(True)
world.setLeftName(text)
else:
world.setRightHalf(True)
world.setRightName(text)
else:
# It's a real level
level = LevelInfo()
level.setName(text)
level.setFileNameW(levelData[0] + 1)
level.setFileNameL(levelData[1] + 1)
level.setDisplayNameW(levelData[2])
level.setDisplayNameL(levelData[3])
flags = (levelData[6] << 8) | levelData[7]
level.setFlags(flags)
level.setVignette(levelData[5])
# Add it to world
world.Levels.append(level)
# Add it to worlds
worlds.append(world)
# Assign worlds to self.worlds
self.worlds = worlds
# Get the comments
start = self.GetCommentsOffset()
end = minTextOffset - 1 # collected this while in the self.worlds populating loop
self.comments = ''
for i in range(start, end):
self.comments += chr(rawdata[i])
def GetCommentsOffset(self):
"""Calculates the offset of the comments based on worlds and levels"""
# I'm trying to make this as easy-to-read as possible.
offset = 0
offset += 4 # "NWRp" text
offset += 4 # Number of Worlds bytes
for world in self.worlds:
offset += 4 # Offset to the world data in the file header
offset += 4 # Number of Levels bytes
if world.HasL: offset += 12 # data for that takes 12 bytes
if world.HasR: offset += 12 # same as above
for level in world.Levels:
offset += 12 # Each level is 12 bytes
return offset
def save(self):
"""Returns bytes that can be saved back to a LevelInfo.bin file"""
result = []
TextStart = self.GetCommentsOffset() + len(self.comments) + 1
# First things first - add "NWRp"
result.append(ord('N'))
result.append(ord('W'))
result.append(ord('R'))
result.append(ord('p'))
# Add the Number of Worlds value (we'll only worry about the last 2 bytes)
NumOfWorlds = len(self.worlds)
result.append((NumOfWorlds >> 24) & 0xFF)
result.append((NumOfWorlds >> 16) & 0xFF)
result.append((NumOfWorlds >> 8) & 0xFF)
result.append((NumOfWorlds >> 0) & 0xFF)
# Add blank spaces for each world value
for w in self.worlds:
for i in range(4): result.append(0)
# Add worlds and world-offsets at the same time
CurrentOffset = len(result)
CurrentTextOffset = int(TextStart) # make a new int
Text = []
WorldNumber = 0
for world in self.worlds:
# Set the World Offset start value to CurrentOffset
result[ 8+(WorldNumber*4)] = (CurrentOffset >> 24) & 0xFF
result[ 9+(WorldNumber*4)] = (CurrentOffset >> 16) & 0xFF
result[10+(WorldNumber*4)] = (CurrentOffset >> 8) & 0xFF
result[11+(WorldNumber*4)] = (CurrentOffset >> 0) & 0xFF
# Create a place to store some world info
worldData = []
# Add the Number of Levels value
num = len(world.Levels)
if world.HasL: num += 1
if world.HasR: num += 1
worldData.append((num >> 24) & 0xFF)
worldData.append((num >> 16) & 0xFF)
worldData.append((num >> 8) & 0xFF)
worldData.append((num >> 0) & 0xFF)
# Add data to worldData for each world half
for exists, name in zip((world.HasL, world.HasR), ('L', 'R')):
if not exists: continue
WName = eval('world.%sName' % name)
worldData.append(0x62) # filename: 98-98
worldData.append(0x62)
worldData.append(world.WorldNumber) # display name: WN-100
worldData.append(0x64 if name =='L' else 0x65)
worldData.append(len(WName))
worldData.append(0x00)
worldData.append(0x00 if name == 'L' else 0x04)
worldData.append(0x00)
worldData.append(0x00)
worldData.append(0x00)
worldData.append((CurrentTextOffset >> 8) & 0xFF)
worldData.append((CurrentTextOffset >> 0) & 0xFF)
CurrentTextOffset += (len(WName) + 1) * 2
# print("length: %X -> %X %X" % (len(level.name), ((len(level.name) * 2) + 1), ((len(level.name) + 1) * 2)))
for char in WName: Text.append(ord(char))
Text.append(0x00)
# Add data to worldData for each levels
for level in world.Levels:
flags = level.getFlags()
worldData.append(level.FileW - 1)
worldData.append(level.FileL - 1)
worldData.append(level.DisplayW)
worldData.append(level.DisplayL)
worldData.append(len(level.name))
worldData.append(level.Vignette)
worldData.append((flags >> 8) & 0xFF)
worldData.append((flags >> 0) & 0xFF)
worldData.append(0x00)
worldData.append(0x00)
worldData.append((CurrentTextOffset >> 8) & 0xFF)
worldData.append((CurrentTextOffset >> 0) & 0xFF)
CurrentTextOffset += (len(level.name) + 1) * 2
# print("length: %X -> %X %X" % (len(level.name), ((len(level.name) * 2) + 1), ((len(level.name) + 1) * 2)))
for char in level.name: Text.append(ord(char))
Text.append(0x00)
# Add worldData to result
for i in worldData:
result.append(i)
CurrentOffset += 1
# Get ready for the next world
WorldNumber += 1
# Add the comments
for char in self.comments: result.append(ord(char))
result.append(0x00)
# Add text
for char in Text:
# char = char - 0x30
# if char < 0: char += 256
# result.append(char)
result.append((char >> 8) & 0xFF)
result.append((char >> 0) & 0xFF)
# Py2 saves binary files as strings
if sys.version[0] == '2':
string = ''
for i in result: string += chr(i)
return string
else:
return bytes(result)
def exportTXT(self):
"""Returns bytes that can be saved back to a LevelInfo.bin file"""
#resultxt = "Newer Level Info Editor v1.5 - Prankster Comets Edition\n"
resultxt = "Newer Level Info Editor v1.5-UTF by @wakanameko2\n"
result = []
TextStart = self.GetCommentsOffset() + len(self.comments) + 1
# First things first - add "NWRp"
result.append(ord('N'))
result.append(ord('W'))
result.append(ord('R'))
result.append(ord('p'))
# Add the Number of Worlds value (we'll only worry about the last 2 bytes)
NumOfWorlds = len(self.worlds)
resultxt += "WorldCount: {}\n".format(NumOfWorlds)
result.append((NumOfWorlds >> 24) & 0xFF)
result.append((NumOfWorlds >> 16) & 0xFF)
result.append((NumOfWorlds >> 8) & 0xFF)
result.append((NumOfWorlds >> 0) & 0xFF)
# Add blank spaces for each world value
for w in self.worlds:
for i in range(4): result.append(0)
# Add worlds and world-offsets at the same time
CurrentOffset = len(result)
CurrentTextOffset = int(TextStart) # make a new int
Text = []
WorldNumber = 0
for world in self.worlds:
resultxt += "World {}:\n".format(WorldNumber)
# Set the World Offset start value to CurrentOffset
result[ 8+(WorldNumber*4)] = (CurrentOffset >> 24) & 0xFF
result[ 9+(WorldNumber*4)] = (CurrentOffset >> 16) & 0xFF
result[10+(WorldNumber*4)] = (CurrentOffset >> 8) & 0xFF
result[11+(WorldNumber*4)] = (CurrentOffset >> 0) & 0xFF
# Create a place to store some world info
worldData = []
# Add the Number of Levels value
num = len(world.Levels)
if world.HasL: num += 1
if world.HasR: num += 1
worldData.append((num >> 24) & 0xFF)
worldData.append((num >> 16) & 0xFF)
worldData.append((num >> 8) & 0xFF)
worldData.append((num >> 0) & 0xFF)
# Add data to worldData for each world half
for exists, name in zip((world.HasL, world.HasR), ('L', 'R')):
if not exists: continue
WName = eval('world.%sName' % name)
worldData.append(0x62) # filename: 98-98
worldData.append(0x62)
worldData.append(world.WorldNumber) # display name: WN-100
worldData.append(0x64 if name =='L' else 0x65)
worldData.append(len(WName))
worldData.append(0x00)
worldData.append(0x00 if name == 'L' else 0x04)
worldData.append(0x00)
worldData.append(0x00)
worldData.append(0x00)
worldData.append((CurrentTextOffset >> 8) & 0xFF)
worldData.append((CurrentTextOffset >> 0) & 0xFF)
CurrentTextOffset += (len(WName) + 1) * 2
# print("length: %X -> %X %X" % (len(level.name), ((len(level.name) * 2) + 1), ((len(level.name) + 1) * 2)))
for char in WName: Text.append(ord(char))
Text.append(0x00)
# Add data to worldData for each levels
for level in world.Levels:
# levelTable = ['A', 'B', 'C', 'D', 'E', '15', '16', '17', '18', '19', 'A', 'Tower', 'GH', 'Castle', 'Tower(Cannon)', 'FCastle', 'Railroad', 'Airship', 'Peach', 'Yoshi House', 'HouseS', 'HouseU', 'Anchor', 'Coin', 'B', 'C', 'Music House', 'Shop', 'Challenge', 'Red Switch', 'Blue Switch', 'Yellow Switch', 'Green Switch']
if MSLANG == 'English':
levelTable = ['A', 'B', 'C', 'D', 'E', '15', '16', '17', '18', '19', 'A', 'Tower', 'GH', 'Castle', 'Tower(Cannon)', 'FCastle', 'Railroad', 'Airship', 'Peach', 'Yoshi House', 'HouseS', 'HouseU', 'Anchor', 'Coin', 'B', 'C', 'Music House', 'Shop', 'Challenge', 'Red Switch', 'Blue Switch', 'Yellow Switch', 'Green Switch']
else:
levelTable = ['A', 'B', 'C', 'D', 'E', '15', '16', '17', '18', '19', 'A', '塔', 'お化け屋敷', '城', '砦(大砲)', 'クッパ城', '汽車', '飛行船', 'ピーチ城', 'ヨッシーの家', 'HouseS', 'HouseU', '碇', 'Coin', 'B', 'C', 'ミュージックハウス', 'ショップ', 'チャレンジハウス', '赤スイッチ', '青スイッチ', '黄色スイッチ', '緑スイッチ']
levelS = str(level.DisplayL) # レベル番号を文字列として処理
if level.DisplayL >= 10 and level.DisplayL <= 41: # 表示されるレベルの番号がXX~XXの間の場合
levelS = levelTable[level.DisplayL-10] # levelTableの(表示されるレベル名-20)番目の名前を持ってきますね(23番の場合はCastle)(該当のレベル名が存在しない場合、クラッシュします。)
# 830行付近にも同様の処理があります。
resultxt += "\t{}-{}: {}\n".format(level.DisplayW, levelS, level.name)
flags = level.getFlags()
worldData.append(level.FileW - 1)
worldData.append(level.FileL - 1)
worldData.append(level.DisplayW)
worldData.append(level.DisplayL)
worldData.append(len(level.name))
worldData.append(level.Vignette)
worldData.append((flags >> 8) & 0xFF)
worldData.append((flags >> 0) & 0xFF)
worldData.append(0x00)
worldData.append(0x00)
worldData.append((CurrentTextOffset >> 8) & 0xFF)
worldData.append((CurrentTextOffset >> 0) & 0xFF)
CurrentTextOffset += (len(level.name) + 1) * 2
# print("length: %X -> %X %X" % (len(level.name), ((len(level.name) * 2) + 1), ((len(level.name) + 1) * 2)))
for char in level.name: Text.append(ord(char))
Text.append(0x00)
# Add worldData to result
for i in worldData:
result.append(i)
CurrentOffset += 1
# Get ready for the next world
resultxt += "\n"
WorldNumber += 1
# Add the comments
for char in self.comments: result.append(ord(char))
result.append(0x00)
# Add text
for char in Text:
# char = char - 0x30
# if char < 0: char += 256
# result.append(char)
result.append((char >> 8) & 0xFF)
result.append((char >> 0) & 0xFF)
return resultxt
# Py2 saves binary files as strings
if sys.version[0] == '2':
string = ''
for i in result: string += chr(i)
return string
else:
return bytes(result)
########################################################################
########################################################################
########################################################################
########################################################################
# Drag-and-Drop Picker
class DNDPicker(QtWidgets.QListWidget):
"""A list widget which calls a function when an item's been moved"""
def __init__(self, handler):
QtWidgets.QListWidget.__init__(self)
self.handler = handler
self.setDragDropMode(QtWidgets.QListWidget.InternalMove)
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
def dropEvent(self, event):
QtWidgets.QListWidget.dropEvent(self, event)
self.handler()
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
class LevelInfoViewer(QtWidgets.QWidget):
"""Widget that views level info"""
def __init__(self):
"""Initialises the widget"""
QtWidgets.QWidget.__init__(self)
self.file = LevelInfoFile()
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
# Create the Worlds widgets
if MSLANG == 'English':
WorldBox = QtWidgets.QGroupBox('Worlds')
self.WorldPicker = DNDPicker(self.HandleWDragDrop)
self.WABtn = QtWidgets.QPushButton('Add')
self.WRBtn = QtWidgets.QPushButton('Remove')
# Add some tooltips
self.WABtn.setToolTip('<b>Add:</b><br>Adds a world to the file.')
self.WRBtn.setToolTip('<b>Remove:</b><br>Removes the currently selected world from the file.')
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
else:
WorldBox = QtWidgets.QGroupBox('ワールド')
self.WorldPicker = DNDPicker(self.HandleWDragDrop)
self.WABtn = QtWidgets.QPushButton('追加')
self.WRBtn = QtWidgets.QPushButton('削除')
self.WABtn.setToolTip('<b>追加:</b><br>ワールドを追加します。')
self.WRBtn.setToolTip('<b>削除:</b><br>選択したワールドを削除します。')
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
# Disable some
self.WRBtn.setEnabled(False)
# Connect them to handlers
self.WorldPicker.currentItemChanged.connect(self.HandleWorldSel)
self.WABtn.clicked.connect(self.HandleWA)
self.WRBtn.clicked.connect(self.HandleWR)
# Make a layout
L = QtWidgets.QGridLayout()
L.addWidget(self.WorldPicker, 0, 0, 1, 2)
L.addWidget(self.WABtn, 1, 0)
L.addWidget(self.WRBtn, 1, 1)
WorldBox.setLayout(L)
# Create the World Options widget
self.WorldEdit = WorldOptionsEditor()
self.WorldEdit.dataChanged.connect(self.HandleWorldDatChange)
# Create the Levels widgets
LevelBox = QtWidgets.QWidget()
self.LevelPicker = DNDPicker(self.HandleLDragDrop)
self.LevelEdit = LevelEditor()
if MSLANG == 'English':
self.LABtn = QtWidgets.QPushButton('Add')
self.LRBtn = QtWidgets.QPushButton('Remove')
# Add some tooltips
self.LABtn.setToolTip('<b>Add:</b><br>Adds a level to the currently selected world.')
self.LRBtn.setToolTip('<b>Remove:</b><br>Removes the currently selected level from the world.')
else:
self.LABtn = QtWidgets.QPushButton('追加')
self.LRBtn = QtWidgets.QPushButton('削除')
# Add some tooltips
self.LABtn.setToolTip('<b>追加:</b><br>ステージを追加します。')
self.LRBtn.setToolTip('<b>削除:</b><br>選択したステージを削除します。')
# Disable some
self.LABtn.setEnabled(False)
self.LRBtn.setEnabled(False)
# Connect them to handlers
self.LevelPicker.currentItemChanged.connect(self.HandleLevelSel)
self.LevelEdit.dataChanged.connect(self.HandleLevelDatChange)
self.LevelEdit.navRequest.connect(self.HandleLevelNavRequest)
self.LABtn.clicked.connect(self.HandleLA)
self.LRBtn.clicked.connect(self.HandleLR)
# Make a layout
L = QtWidgets.QGridLayout()
L.addWidget(self.LevelPicker, 0, 0, 1, 2)
L.addWidget(self.LABtn, 1, 0)
L.addWidget(self.LRBtn, 1, 1)
L.addWidget(self.LevelEdit, 2, 0, 1, 2)
LevelBox.setLayout(L)
# Create the Comments editor and layout
self.CommentsBox = QtWidgets.QWidget()
if MSLANG =='English':
label = QtWidgets.QLabel('You can add comments to the file here:\n(You can\'t type 2byte charactors :\'( )')
else:
label = QtWidgets.QLabel('ここにコメントを書き込めます。(2バイト文字を除く):')
self.CommentsEdit = QtWidgets.QPlainTextEdit()
self.CommentsEdit.textChanged.connect(self.HandleCommentsChanged)
STL = QtWidgets.QVBoxLayout(self)
STL.addWidget(label)
STL.addWidget(self.CommentsEdit)
self.CommentsBox.setLayout(STL)
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
# Create the tab widget
tab = QtWidgets.QTabWidget(self)
if MSLANG == 'English':
tab.addTab(self.WorldEdit, 'World Options')
tab.addTab(LevelBox, 'Levels')
tab.addTab(self.CommentsBox, 'Comments')
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
else:
tab.addTab(self.WorldEdit, 'ワールド設定')
tab.addTab(LevelBox, 'ステージ')
tab.addTab(self.CommentsBox, 'コメント')
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
# Make a main layout
L = QtWidgets.QHBoxLayout(self)
L.addWidget(WorldBox)
L.addWidget(tab)
self.setLayout(L)
if VMode == 'Dark':
self.setStyleSheet(StyleSheet)
else:
pass
def setFile(self, file):
"""Changes the file to view"""
self.file = file
self.WorldPicker.clear()
self.LevelPicker.clear()
# Add worlds
for world in self.file.worlds:
item = QtWidgets.QListWidgetItem() # self.UpdateNames will add the name
item.setData(QtCore.Qt.UserRole, world)
self.WorldPicker.addItem(item)
# Add comments
self.CommentsEdit.setPlainText(self.file.comments)
# Update world names
self.UpdateNames()
def UpdateNames(self):
"""Updates item names in all 3 item-picker widgets"""
# WorldPicker
for item in self.WorldPicker.findItems('', QtCore.Qt.MatchContains):
world = item.data(QtCore.Qt.UserRole)
text = 'World '
if world.WorldNumber == None: text += '(unknown)'
else: text += str(world.WorldNumber)
item.setText(text)
# LevelPicker
for item in self.LevelPicker.findItems('', QtCore.Qt.MatchContains):
level = item.data(QtCore.Qt.UserRole)
# levelTable = ['A', 'B', 'C', 'D', 'E', '15', '16', '17', '18', '19', 'A', 'Tower', 'GH', 'Castle', 'Cannon', 'FCastle', 'Railroad', 'Airship', 'Peach', 'Yoshi House', 'HouseS', 'HouseU', 'Anchor', 'Coin', 'B', 'C', 'Music House', 'Shop', 'Challenge', 'Red Switch', 'Blue Switch', 'Yellow Switch', 'Green Switch']
if MSLANG == 'English':
levelTable = ['A', 'B', 'C', 'D', 'E', '15', '16', '17', '18', '19', 'A', 'Tower', 'GH', 'Castle', 'Tower(Cannon)', 'FCastle', 'Railroad', 'Airship', 'Peach', 'Yoshi House', 'HouseS', 'HouseU', 'Anchor', 'Coin', 'B', 'C', 'Music House', 'Shop', 'Challenge', 'Red Switch', 'Blue Switch', 'Yellow Switch', 'Green Switch']
else:
levelTable = ['A', 'B', 'C', 'D', 'E', '15', '16', '17', '18', '19', 'A', '塔', 'お化け屋敷', '城', '砦(大砲)', 'クッパ城', '汽車', '飛行船', 'ピーチ城', 'ヨッシーの家', 'HouseS', 'HouseU', '碇', 'Coin', 'B', 'C', 'ミュージックハウス', 'ショップ', 'チャレンジハウス', '赤スイッチ', '青スイッチ', '黄色スイッチ', '緑スイッチ']
levelS = str(level.DisplayL)
if level.DisplayL >= 10 and level.DisplayL <= 41:
levelS = levelTable[level.DisplayL-10]
item.setText(str(level.DisplayW) + '-' + levelS + ': ' + str(level.name))
def saveFile(self):
"""Returns the file in saved form"""
return self.file.save() # self.file does this for us
def exportTXTFile(self):
"""Returns the file in txt form"""
return self.file.exportTXT() # self.file does this for us
# World Functions
def HandleWorldSel(self):
"""Handles the user picking a world"""
self.WorldEdit.clear()
self.LevelPicker.clear()
self.LevelEdit.clear()
# Get the current item (it's None if nothing's selected)
currentItem = self.WorldPicker.currentItem()
# Enable/disable buttons
if currentItem == None:
self.WRBtn.setEnabled(False)
self.LABtn.setEnabled(False)
self.LRBtn.setEnabled(False)
else:
index = self.WorldPicker.indexFromItem(currentItem).row()
numberOfItems = self.WorldPicker.count()
self.WRBtn.setEnabled(True)
self.LABtn.setEnabled(True)
# Get the world
if currentItem == None: return
world = currentItem.data(QtCore.Qt.UserRole)
# Set up the World Options Editor
self.WorldEdit.setWorld(world)
# Add levels to self.LevelPicker
for level in world.Levels:
item = QtWidgets.QListWidgetItem(level.name)
item.setData(QtCore.Qt.UserRole, level)
self.LevelPicker.addItem(item)
self.LevelPicker.setCurrentRow(0)
def HandleWA(self):
"""Handles Add World button clicks"""
world = WorldInfo()
text = 'World (unknown)'
# Add it to self.file and self.WorldPicker
self.file.worlds.append(world)
item = QtWidgets.QListWidgetItem(text)
item.setData(QtCore.Qt.UserRole, world)
self.WorldPicker.addItem(item)
self.WorldPicker.scrollToItem(item)
item.setSelected(True)
self.UpdateNames()
def HandleWR(self):
"""Handles Remove World button clicks"""
item = self.WorldPicker.currentItem()
world = item.data(QtCore.Qt.UserRole)
# Remove it from file and the picker
self.file.worlds.remove(world)
self.WorldPicker.takeItem(self.WorldPicker.row(item))
self.UpdateNames()
def HandleWDragDrop(self):
"""Handles dragging-and-dropping in the World Picker"""
newWorlds = []
for item in self.WorldPicker.findItems('', QtCore.Qt.MatchContains):
world = item.data(QtCore.Qt.UserRole)
newWorlds.append(world)
self.file.Worlds = newWorlds
self.UpdateNames()
def HandleWorldDatChange(self):
"""Handles the user changing world data"""
self.UpdateNames()
# Level Functions
def HandleLevelSel(self):
"""Handles the user picking a level"""
self.LevelEdit.clear()
# Get the current item (it's None if nothing's selected)
currentItem = self.LevelPicker.currentItem()
# Enable/disable buttons
if currentItem == None:
self.LRBtn.setEnabled(False)
else:
index = self.LevelPicker.indexFromItem(currentItem).row()
numberOfItems = self.LevelPicker.count()
self.LRBtn.setEnabled(True)
# Get the level
if currentItem == None: return
level = currentItem.data(QtCore.Qt.UserRole)
# Set LevelEdit to edit it
self.LevelEdit.setLevel(level)
def HandleLevelDatChange(self):
"""Handles the user changing level data"""
self.UpdateNames()
def HandleLevelNavRequest(self, isUp, refocusWidget):
"""Handles the user pressing PgUp or PgDn to switch between levels"""
currentRow = self.LevelPicker.currentRow()
newRow = currentRow + (-1 if isUp else 1)
newRow = max(0, newRow)
newRow = min(newRow, self.LevelPicker.count() - 1)
if newRow != currentRow:
self.LevelPicker.setCurrentRow(newRow)
refocusWidget.setFocus(True)
def HandleLA(self):
"""Handles Add Level button clicks"""
level = LevelInfo()
text = 'New Level'
level.setName(text)
item = QtWidgets.QListWidgetItem(text)
item.setData(QtCore.Qt.UserRole, level)
# Add it to the current world and self.LevelPicker
w = self.WorldPicker.currentItem().data(QtCore.Qt.UserRole)
w.Levels.append(level)
self.LevelPicker.addItem(item)
self.LevelPicker.scrollToItem(item)
item.setSelected(True)
self.UpdateNames()
def HandleLR(self):
"""Handles Remove Level button clicks"""
item = self.LevelPicker.currentItem()
level = item.data(QtCore.Qt.UserRole)
# Remove it from file and the picker
w = self.WorldPicker.currentItem().data(QtCore.Qt.UserRole)
w.Levels.remove(level)
self.LevelPicker.takeItem(self.LevelPicker.row(item))