-
Notifications
You must be signed in to change notification settings - Fork 7
/
levelInfoWebserver.py
2648 lines (2273 loc) · 90.7 KB
/
levelInfoWebserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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 pathlib
import asyncio
import time
from threading import Thread
import os
import json
import orjson
import os
import time
from binascii import hexlify
from struct import pack
from PIL import Image
import zlib
import base64
import io
from fastapi import FastAPI
from fastapi.responses import Response, ORJSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.middleware.cors import CORSMiddleware
from gen3_switchgame import Gen3Switchgame
from nintendo import switch
from nintendo.switch import dauth, aauth, baas, dragons
from nintendo.nex import backend, authentication, settings, datastore_smm2 as datastore
from anynet import http
from enum import IntEnum
# https://github.com/kinnay/NintendoClients/blob/ab2b63a05c28e0939f1e93f2c576e3d7ca9db416/nintendo/games.py
SMM2_GAME_SERVER_ID = 0x22306D00
SMM2_TITLE_ID = 0x01009B90006DC000
SMM2_LATEST_VERSION = 0x70000
SMM2_ACCESS_KEY = "fdf6617f"
SMM2_NEX_VERSION = 40605
SMM2_CLIENT_VERSION = 60
import logging
logging.basicConfig(level=logging.INFO)
args = {}
with open("webserver_args.json") as f:
args = json.load(f)
if args["system_version"] is None:
print("System version not set")
print("Error")
exit(1)
else:
SYSTEM_VERSION = args["system_version"]
if args["user_id"] is None:
print("User ID not set")
print("Error")
exit(1)
else:
BAAS_USER_ID = int(args["user_id"], 16)
if args["password"] is None:
print("Password not set")
print("Error")
exit(1)
else:
BAAS_PASSWORD = args["password"]
if args["country"] is None:
print("Country not set")
print("Error")
exit(1)
else:
BAAS_COUNTRY = args["country"]
if args["keys"] is None:
print("Prod.keys not set")
print("Error")
exit(1)
else:
keys = switch.load_keys(args["keys"])
if args["prodinfo"] is None:
print("Prodinfo not set")
print("Error")
exit(1)
else:
info = switch.ProdInfo(keys, args["prodinfo"])
if args["elicense_id"] is None:
print("Elicense ID not set")
print("Error")
exit(1)
else:
ELICENSE_ID = args["elicense_id"]
if args["na_id"] is None:
print("NA ID not set")
print("Error")
exit(1)
else:
NA_ID = int(args["na_id"], 16)
# Used for scraping
debug_enabled = False
if os.environ.get("SERVER_DEBUG_ENABLED") != None:
print("Server debug enabled")
debug_enabled = True
GameStyles = {
0: "SMB1",
1: "SMB3",
2: "SMW",
3: "NSMBU",
4: "SM3DW"
}
Difficulties = {
0: "Easy",
1: "Normal",
2: "Expert",
3: "Super expert"
}
CourseThemes = {
0: "Overworld",
1: "Underground",
2: "Castle",
3: "Airship",
4: "Underwater",
5: "Ghost house",
6: "Snow",
7: "Desert",
8: "Sky",
9: "Forest"
}
TagNames = {
0: "None",
1: "Standard",
2: "Puzzle solving",
3: "Speedrun",
4: "Autoscroll",
5: "Auto mario",
6: "Short and sweet",
7: "Multiplayer versus",
8: "Themed",
9: "Music",
10: "Art",
11: "Technical",
12: "Shooter",
13: "Boss battle",
14: "Single player",
15: "Link"
}
Regions = {
0: "Asia",
1: "Americas",
2: "Europe",
3: "Other"
}
BadgeTypes = {
0: "Maker Points (All-Time)",
1: "Endless Challenge (Easy)",
2: "Endless Challenge (Normal)",
3: "Endless Challenge (Expert)",
4: "Endless Challenge (Super Expert)",
5: "Multiplayer Versus",
6: "Number of Clears",
7: "Number of First Clears",
8: "Number of World Records",
9: "Maker Points (Weekly)"
}
BadgeRanks = {
6: "Bronze",
5: "Silver",
4: "Gold",
3: "Bronze Ribbon",
2: "Silver Ribbon",
1: "Gold Ribbon"
}
CommentType = {
0: "Custom Image",
1: "Text",
2: "Reaction Image"
}
CommentReactionImage = {
0: "Nice!",
1: "Good stuff!",
2: "So tough...",
3: "EASY",
4: "Seriously?!",
5: "Wow!",
6: "Cool idea!",
7: "SPEEDRUN!",
8: "How?!",
9: "Be careful!",
10: "So close!",
11: "Beat it!"
}
CommentReactionFace = {
0: "Normal",
16: "Wink",
1: "Happy",
4: "Surprised",
18: "Scared",
3: "Confused"
}
MultiplayerVersusRanks = {
1: "D",
2: "C",
3: "B",
4: "A",
5: "S",
6: "S+"
}
ClearConditions = {
137525990: "Reach the goal without landing after leaving the ground.",
199585683: "Reach the goal after defeating at least/all (n) Mechakoopa(s).",
272349836: "Reach the goal after defeating at least/all (n) Cheep Cheep(s).",
375673178: "Reach the goal without taking damage.",
426197923: "Reach the goal as Boomerang Mario.",
436833616: "Reach the goal while wearing a Shoe.",
713979835: "Reach the goal as Fire Mario.",
744927294: "Reach the goal as Frog Mario.",
751004331: "Reach the goal after defeating at least/all (n) Larry(s).",
900050759: "Reach the goal as Raccoon Mario.",
947659466: "Reach the goal after defeating at least/all (n) Blooper(s).",
976173462: "Reach the goal as Propeller Mario.",
994686866: "Reach the goal while wearing a Propeller Box.",
998904081: "Reach the goal after defeating at least/all (n) Spike(s).",
1008094897: "Reach the goal after defeating at least/all (n) Boom Boom(s).",
1051433633: "Reach the goal while holding a Koopa Shell.",
1061233896: "Reach the goal after defeating at least/all (n) Porcupuffer(s).",
1062253843: "Reach the goal after defeating at least/all (n) Charvaargh(s).",
1079889509: "Reach the goal after defeating at least/all (n) Bullet Bill(s).",
1080535886: "Reach the goal after defeating at least/all (n) Bully/Bullies.",
1151250770: "Reach the goal while wearing a Goomba Mask.",
1182464856: "Reach the goal after defeating at least/all (n) Hop-Chops.",
1219761531: "Reach the goal while holding a Red POW Block. OR Reach the goal after activating at least/all (n) Red POW Block(s).",
1221661152: "Reach the goal after defeating at least/all (n) Bob-omb(s).",
1259427138: "Reach the goal after defeating at least/all (n) Spiny/Spinies.",
1268255615: "Reach the goal after defeating at least/all (n) Bowser(s)/Meowser(s).",
1279580818: "Reach the goal after defeating at least/all (n) Ant Trooper(s).",
1283945123: "Reach the goal on a Lakitu's Cloud.",
1344044032: "Reach the goal after defeating at least/all (n) Boo(s).",
1425973877: "Reach the goal after defeating at least/all (n) Roy(s).",
1429902736: "Reach the goal while holding a Trampoline.",
1431944825: "Reach the goal after defeating at least/all (n) Morton(s).",
1446467058: "Reach the goal after defeating at least/all (n) Fish Bone(s).",
1510495760: "Reach the goal after defeating at least/all (n) Monty Mole(s).",
1656179347: "Reach the goal after picking up at least/all (n) 1-Up Mushroom(s).",
1665820273: "Reach the goal after defeating at least/all (n) Hammer Bro(s.).",
1676924210: "Reach the goal after hitting at least/all (n) P Switch(es). OR Reach the goal while holding a P Switch.",
1715960804: "Reach the goal after activating at least/all (n) POW Block(s). OR Reach the goal while holding a POW Block.",
1724036958: "Reach the goal after defeating at least/all (n) Angry Sun(s).",
1730095541: "Reach the goal after defeating at least/all (n) Pokey(s).",
1780278293: "Reach the goal as Superball Mario.",
1839897151: "Reach the goal after defeating at least/all (n) Pom Pom(s).",
1969299694: "Reach the goal after defeating at least/all (n) Peepa(s).",
2035052211: "Reach the goal after defeating at least/all (n) Lakitu(s).",
2038503215: "Reach the goal after defeating at least/all (n) Lemmy(s).",
2048033177: "Reach the goal after defeating at least/all (n) Lava Bubble(s).",
2076496776: "Reach the goal while wearing a Bullet Bill Mask.",
2089161429: "Reach the goal as Big Mario.",
2111528319: "Reach the goal as Cat Mario.",
2131209407: "Reach the goal after defeating at least/all (n) Goomba(s)/Galoomba(s).",
2139645066: "Reach the goal after defeating at least/all (n) Thwomp(s).",
2259346429: "Reach the goal after defeating at least/all (n) Iggy(s).",
2549654281: "Reach the goal while wearing a Dry Bones Shell.",
2694559007: "Reach the goal after defeating at least/all (n) Sledge Bro(s.).",
2746139466: "Reach the goal after defeating at least/all (n) Rocky Wrench(es).",
2749601092: "Reach the goal after grabbing at least/all (n) 50-Coin(s).",
2855236681: "Reach the goal as Flying Squirrel Mario.",
3036298571: "Reach the goal as Buzzy Mario.",
3074433106: "Reach the goal as Builder Mario.",
3146932243: "Reach the goal as Cape Mario.",
3174413484: "Reach the goal after defeating at least/all (n) Wendy(s).",
3206222275: "Reach the goal while wearing a Cannon Box.",
3314955857: "Reach the goal as Link.",
3342591980: "Reach the goal while you have Super Star invincibility.",
3346433512: "Reach the goal after defeating at least/all (n) Goombrat(s)/Goombud(s).",
3348058176: "Reach the goal after grabbing at least/all (n) 10-Coin(s).",
3353006607: "Reach the goal after defeating at least/all (n) Buzzy Beetle(s).",
3392229961: "Reach the goal after defeating at least/all (n) Bowser Jr.(s).",
3437308486: "Reach the goal after defeating at least/all (n) Koopa Troopa(s).",
3459144213: "Reach the goal after defeating at least/all (n) Chain Chomp(s).",
3466227835: "Reach the goal after defeating at least/all (n) Muncher(s).",
3481362698: "Reach the goal after defeating at least/all (n) Wiggler(s).",
3513732174: "Reach the goal as SMB2 Mario.",
3649647177: "Reach the goal in a Koopa Clown Car/Junior Clown Car.",
3725246406: "Reach the goal as Spiny Mario.",
3730243509: "Reach the goal in a Koopa Troopa Car.",
3748075486: "Reach the goal after defeating at least/all (n) Piranha Plant(s)/Jumping Piranha Plant(s).",
3797704544: "Reach the goal after defeating at least/all (n) Dry Bones.",
3824561269: "Reach the goal after defeating at least/all (n) Stingby/Stingbies.",
3833342952: "Reach the goal after defeating at least/all (n) Piranha Creeper(s).",
3842179831: "Reach the goal after defeating at least/all (n) Fire Piranha Plant(s).",
3874680510: "Reach the goal after breaking at least/all (n) Crates(s).",
3974581191: "Reach the goal after defeating at least/all (n) Ludwig(s).",
3977257962: "Reach the goal as Super Mario.",
4042480826: "Reach the goal after defeating at least/all (n) Skipsqueak(s).",
4116396131: "Reach the goal after grabbing at least/all (n) Coin(s).",
4117878280: "Reach the goal after defeating at least/all (n) Magikoopa(s).",
4122555074: "Reach the goal after grabbing at least/all (n) 30-Coin(s).",
4153835197: "Reach the goal as Balloon Mario.",
4172105156: "Reach the goal while wearing a Red POW Box.",
4209535561: "Reach the Goal while riding Yoshi.",
4269094462: "Reach the goal after defeating at least/all (n) Spike Top(s).",
4293354249: "Reach the goal after defeating at least/all (n) Banzai Bill(s)."
}
UserPose = {
0: "Normal",
15: "Fidgety",
17: "Annoyed",
18: "Buoyant",
19: "Thrilled",
20: "Let's go!",
21: "Hello!",
29: "Show-Off",
31: "Cutesy",
39: "Hyped!"
}
UserHat = {
0: "None",
1: "Mario Cap",
2: "Luigi Cap",
4: "Mushroom Hairclip",
5: "Bowser Headpiece",
8: "Princess Peach Wig",
11: "Builder Hard Hat",
12: "Bowser Jr. Headpiece",
13: "Pipe Hat",
15: "Cat Mario Headgear",
16: "Propeller Mario Helmet",
17: "Cheep Cheep Hat",
18: "Yoshi Hat",
21: "Faceplant",
22: "Toad Cap",
23: "Shy Cap",
24: "Magikoopa Hat",
25: "Fancy Top Hat",
26: "Doctor Headgear",
27: "Rocky Wrench Manhold Lid",
28: "Super Star Barrette",
29: "Rosalina Wig",
30: "Fried-Chicken Headgear",
31: "Royal Crown",
32: "Edamame Barrette",
33: "Superball Mario Hat",
34: "Robot Cap",
35: "Frog Cap",
36: "Cheetah Headgear",
37: "Ninji Cap",
38: "Super Acorn Hat",
39: "Pokey Hat",
40: "Snow Pokey Hat"
}
UserShirt = {
0: "Nintendo Shirt",
1: "Mario Outfit",
2: "Luigi Outfit",
3: "Super Mushroom Shirt",
5: "Blockstripe Shirt",
8: "Bowser Suit",
12: "Builder Mario Outfit",
13: "Princess Peach Dress",
16: "Nintendo Uniform",
17: "Fireworks Shirt",
19: "Refreshing Shirt",
21: "Reset Dress",
22: "Thwomp Suit",
23: "Slobbery Shirt",
26: "Cat Suit",
27: "Propeller Mario Clothes",
28: "Banzai Bill Shirt",
29: "Staredown Shirt",
31: "Yoshi Suit",
33: "Midnight Dress",
34: "Magikoopa Robes",
35: "Doctor Coat",
37: "Chomp-Dog Shirt",
38: "Fish Bone Shirt",
40: "Toad Outfit",
41: "Googoo Onesie",
42: "Matrimony Dress",
43: "Fancy Tuxedo",
44: "Koopa Troopa Suit",
45: "Laughing Shirt",
46: "Running Shirt",
47: "Rosalina Dress",
49: "Angry Sun Shirt",
50: "Fried-Chicken Hoodie",
51: "? Block Hoodie",
52: "Edamame Camisole",
53: "I-Like-You Camisole",
54: "White Tanktop",
55: "Hot Hot Shirt",
56: "Royal Attire",
57: "Superball Mario Suit",
59: "Partrick Shirt",
60: "Robot Suit",
61: "Superb Suit",
62: "Yamamura Shirt",
63: "Princess Peach Tennis Outfit",
64: "1-Up Hoodie",
65: "Cheetah Tanktop",
66: "Cheetah Suit",
67: "Ninji Shirt",
68: "Ninji Garb",
69: "Dash Block Hoodie",
70: "Fire Mario Shirt",
71: "Raccoon Mario Shirt",
72: "Cape Mario Shirt",
73: "Flying Squirrel Mario Shirt",
74: "Cat Mario Shirt",
75: "World Wear",
76: "Koopaling Hawaiian Shirt",
77: "Frog Mario Raincoat",
78: "Phanto Hoodie"
}
UserPants = {
0: "Black Short-Shorts",
1: "Denim Jeans",
5: "Denim Skirt",
8: "Pipe Skirt",
9: "Skull Skirt",
10: "Burner Skirt",
11: "Cloudwalker",
12: "Platform Skirt",
13: "Parent-and-Child Skirt",
17: "Mario Swim Trunks",
22: "Wind-Up Shoe",
23: "Hoverclown",
24: "Big-Spender Shorts",
25: "Shorts of Doom!",
26: "Doorduroys",
27: "Antsy Corduroys",
28: "Bouncy Skirt",
29: "Stingby Skirt",
31: "Super Star Flares",
32: "Cheetah Runners",
33: "Ninji Slacks"
}
UserIsOutfit = {
0: False,
1: True,
2: True,
3: False,
5: False,
8: True,
12: True,
13: True,
16: False,
17: False,
19: False,
21: True,
22: True,
23: False,
26: True,
27: True,
28: False,
29: False,
31: True,
33: True,
34: True,
35: True,
37: False,
38: False,
40: True,
41: True,
42: True,
43: True,
44: True,
45: False,
46: False,
47: True,
49: False,
50: False,
51: False,
52: False,
53: False,
54: False,
55: False,
56: True,
57: True,
59: False,
60: True,
61: True,
62: False,
63: True,
64: False,
65: False,
66: True,
67: False,
68: True,
69: False,
70: False,
71: False,
72: False,
73: False,
74: False,
75: True,
76: False,
77: True,
78: False
}
SuperWorldPlanetType = {
0: "Earth",
1: "Moon",
2: "Sand",
3: "Green",
4: "Ice",
5: "Ringed",
6: "Red",
7: "Spiral"
}
class CourseRequestType(IntEnum):
course_id = 1
courses_endless_mode = 2
courses_latest = 3
courses_point_ranking = 4
data_ids = 5
data_ids_no_stop = 6
search = 7
posted = 8
liked = 9
played = 10
first_cleared = 11
world_record = 12
class ServerDataTypes(IntEnum):
level_thumbnail = 2
entire_level_thumbnail = 3
custom_comment_image = 10
ninji_ghost_replay = 40
world_map_thumbnails = 50
class ServerDataTypeHeader:
headers = None
last_updated = 0
expiration = 0
data_type = 0
def __init__(self, type):
self.data_type = type
async def refresh(self, store):
headers_info = await store.get_req_get_info_headers_info(self.data_type)
self.headers = {h.key: h.value for h in headers_info.headers}
self.expiration = headers_info.expiration * 1000
self.last_updated = milliseconds_since_epoch()
async def refresh_if_needed(self, store):
if (milliseconds_since_epoch() - self.last_updated) > (self.expiration - 1000):
await self.refresh(store)
async def request_url(self, url, store):
if (milliseconds_since_epoch() - self.last_updated) > (self.expiration - 1000):
if store == None:
return False
else:
await self.refresh(store)
response = await http.get(url, headers=self.headers)
response.raise_if_error()
return response.body
class ServerHeaders:
level_thumbnail = ServerDataTypeHeader(ServerDataTypes.level_thumbnail)
entire_level_thumbnail = ServerDataTypeHeader(ServerDataTypes.entire_level_thumbnail)
custom_comment_image = ServerDataTypeHeader(ServerDataTypes.custom_comment_image)
ninji_ghost_replay = ServerDataTypeHeader(ServerDataTypes.ninji_ghost_replay)
world_map_thumbnails = ServerDataTypeHeader(ServerDataTypes.world_map_thumbnails)
async def download_thumbnail(store, url, filename, data_type, save = True):
# if data_type == ServerDataTypes.level_thumbnail:
# body = await ServerHeaders.level_thumbnail.request_url(url, store)
# if body == False:
# return False
# else:
# image = Image.open(io.BytesIO(body))
# if save:
# image.save(filename, optimize=True, quality=95)
# return True
# else:
# image_bytes = io.BytesIO()
# image.save(image_bytes, optimize=True, quality=95, format="jpeg")
# return image_bytes.getvalue()
#
# if data_type == ServerDataTypes.entire_level_thumbnail:
# body = await ServerHeaders.entire_level_thumbnail.request_url(url, store)
# if body == False:
# return False
# else:
# image = Image.open(io.BytesIO(body))
# if save:
# image.save(filename, optimize=True, quality=95)
# return True
# else:
# image_bytes = io.BytesIO()
# image.save(image_bytes, optimize=True, quality=95, format="jpeg")
# return image_bytes.getvalue()
# Intentionally disable optimizing so that header info
if data_type == ServerDataTypes.level_thumbnail:
body = await ServerHeaders.level_thumbnail.request_url(url, store)
if body == False:
return False
else:
if save:
with open(filename, "wb") as f:
f.write(body)
return True
else:
return body
if data_type == ServerDataTypes.entire_level_thumbnail:
body = await ServerHeaders.entire_level_thumbnail.request_url(url, store)
if body == False:
return False
else:
if save:
with open(filename, "wb") as f:
f.write(body)
return True
else:
return body
def format_time(milliseconds):
seconds = (milliseconds // 1000) % 60
minutes = (milliseconds // 1000) // 60
milliseconds = milliseconds % 1000
return "%02i:%02i.%03i" % (minutes, seconds, milliseconds)
def in_cache(course_id):
level_info_path = pathlib.Path("cache/level_info/%s" % course_id)
return level_info_path.exists()
def in_user_cache(maker_id):
user_info_path = pathlib.Path("cache/user_info/%s" % maker_id)
return user_info_path.exists()
def ninji_ghosts_in_cache(ninji_data_id, time, num, include_replay_files):
ninji_ghosts_path = pathlib.Path("cache/ninji_ghosts/%s_%s_%s_%i" % (str(ninji_data_id), str(time), num, include_replay_files))
return ninji_ghosts_path.exists()
def ninji_ghost_replay_in_cache(replay_id):
ninji_ghost_replay_path = pathlib.Path("cache/ninji_ghost_replays/%s.replay" % replay_id)
return ninji_ghost_replay_path.exists()
def invalid_level(course_info):
if "name" in course_info or "courses" in course_info or "comments" in course_info or "players" in course_info or "deaths" in course_info or "super_worlds" in course_info:
return False
else:
return True
def invalid_ninji_ghosts(ghosts_info):
if "ghosts" in ghosts_info:
return False
else:
return True
def correct_course_id(course_id):
return course_id.translate({ord('-'): None, ord(' '): None}).upper()
def invalid_course_id_length(course_id):
if len(course_id) != 9:
return True
charset = "0123456789BCDFGHJKLMNPQRSTVWXY"
for char in course_id:
if not char in charset:
return True
return False
def difficulty_string_to_num(difficulty):
if difficulty == "e":
return 0
if difficulty == "n":
return 1
if difficulty == "ex":
return 2
if difficulty == "sex":
return 3
return -1
def region_string_to_list(regions):
regions_list = []
if "j" in regions:
regions_list.append(0)
if "u" in regions:
regions_list.append(1)
if "e" in regions:
regions_list.append(2)
if "a" in regions:
regions_list.append(3)
return regions_list
def course_id_to_dataid(id):
# https://github.com/kinnay/NintendoClients/wiki/Data-Store-Codes#super-mario-maker-2
course_id = id[::-1]
charset = "0123456789BCDFGHJKLMNPQRSTVWXY"
number = 0
for char in course_id:
number = number * 30 + charset.index(char)
left_side = number
left_side = left_side << 34
left_side_replace_mask = 0b1111111111110000000000000000000000000000000000
number = number ^ ((number ^ left_side) & left_side_replace_mask)
number = number >> 14
number = number ^ 0b00010110100000001110000001111100
return number
def is_maker_id(id):
# https://github.com/kinnay/NintendoClients/wiki/Data-Store-Codes#super-mario-maker-2
course_id = id[::-1]
charset = "0123456789BCDFGHJKLMNPQRSTVWXY"
number = 0
for char in course_id:
number = number * 30 + charset.index(char)
if number & 8192:
return True
return False
def get_mii_data(data):
# Based on https://github.com/HEYimHeroic/mii2studio/blob/master/mii2studio.py
user_mii = Gen3Switchgame.from_bytes(data)
mii_values = [
user_mii.facial_hair_color,
user_mii.facial_hair_beard,
user_mii.body_weight,
user_mii.eye_stretch,
user_mii.eye_color,
user_mii.eye_rotation,
user_mii.eye_size,
user_mii.eye_type,
user_mii.eye_horizontal,
user_mii.eye_vertical,
user_mii.eyebrow_stretch,
user_mii.eyebrow_color,
user_mii.eyebrow_rotation,
user_mii.eyebrow_size,
user_mii.eyebrow_type,
user_mii.eyebrow_horizontal,
user_mii.eyebrow_vertical,
user_mii.face_color,
user_mii.face_makeup,
user_mii.face_type,
user_mii.face_wrinkles,
user_mii.favorite_color,
user_mii.gender,
user_mii.glasses_color,
user_mii.glasses_size,
user_mii.glasses_type,
user_mii.glasses_vertical,
user_mii.hair_color,
user_mii.hair_flip,
user_mii.hair_type,
user_mii.body_height,
user_mii.mole_size,
user_mii.mole_enable,
user_mii.mole_horizontal,
user_mii.mole_vertical,
user_mii.mouth_stretch,
user_mii.mouth_color,
user_mii.mouth_size,
user_mii.mouth_type,
user_mii.mouth_vertical,
user_mii.facial_hair_size,
user_mii.facial_hair_mustache,
user_mii.facial_hair_vertical,
user_mii.nose_size,
user_mii.nose_type,
user_mii.nose_vertical
]
mii_data = b"00"
mii_bytes = ""
n = 256
for v in mii_values:
n = (7 + (v ^ n)) % 256
mii_data += hexlify(pack(">B", n))
mii_bytes += hexlify(pack(">B", v)).decode("ascii")
url = "https://studio.mii.nintendo.com/miis/image.png?data=" + mii_data.decode("utf-8") + "&type=face&width=512&instanceCount=1"
return [url, mii_bytes]
async def obtain_course_info(course_id, store, noCaching = False):
param = datastore.GetUserOrCourseParam()
param.code = course_id
param.course_option = datastore.CourseOption.ALL
# Download a specific course
course_info_json = await get_course_info_json(CourseRequestType.course_id, param, store, noCaching)
return course_info_json
async def obtain_user_info(maker_id, store, noCaching = False, save = True):
param = datastore.GetUserOrCourseParam()
param.code = maker_id
param.user_option = datastore.UserOption.ALL
loc = "cache/user_info/%s" % maker_id
# Prepare directories
os.makedirs(os.path.dirname(loc), exist_ok=True)
user_info_path = pathlib.Path(loc)
if user_info_path.exists() and not noCaching:
with open(loc, mode="rb") as f:
return orjson.loads(zlib.decompress(f.read()))
else:
if not is_maker_id(maker_id):
with open(loc, mode="wb+") as f:
f.write(zlib.compress(('{"error": "Code corresponds to a level", "maker_id": "%s"}' % maker_id).encode("UTF8")))
return {"error": "Code corresponds to a level", "maker_id": maker_id}
else:
try:
response = await store.get_user_or_course(param)
except:
# Save (the empty) level info to json
print("maker_id " + maker_id + " is invalid")
with open(loc, mode="wb+") as f:
f.write(zlib.compress(('{"error": "No user with that ID", "maker_id": "%s"}' % maker_id).encode("UTF8")))
return {"error": "No user with that ID", "maker_id": maker_id}
ret = {}
add_user_info_json(response.user, ret)
if save:
with open(loc, mode="wb+") as f:
f.write(zlib.compress(orjson.dumps(ret)))
return ret
async def obtain_course_infos(course_ids, store):
# Convert each course_id to a data_id
data_ids = []
for id in course_ids:
data_ids.append(course_id_to_dataid(id))
param = datastore.GetCoursesParam()
param.data_ids = data_ids
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.data_ids, param, store)
if invalid_level(courses_info_json):
return {"error": "No course with that ID", "course_id": course_ids[data_ids.index(courses_info_json["data_id"])]}
return courses_info_json
async def search_endless_courses(count, difficulty, store):
param = datastore.SearchCoursesEndlessModeParam()
param.count = count
param.difficulty = difficulty
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.courses_endless_mode, param, store)
return courses_info_json
async def search_latest_courses(size, store):
param = datastore.SearchCoursesLatestParam()
param.range.offset = 0
param.range.size = size
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.courses_latest, param, store)
return courses_info_json
async def search_courses_point_ranking(size, difficulty, rejectRegions, store):
param = datastore.SearchCoursesPointRankingParam()
param.range.offset = 0
param.range.size = size
param.difficulty = difficulty
param.reject_regions = rejectRegions
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.courses_point_ranking, param, store)
return courses_info_json
async def get_courses_data_id(data_ids, store):
param = datastore.GetCoursesParam()
param.data_ids = data_ids
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.data_ids_no_stop, param, store)
return courses_info_json
async def get_courses_posted(size, pid, store):
param = datastore.SearchCoursesPostedByParam()
param.range.offset = 0
param.range.size = size
param.pids = [pid]
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.posted, param, store)
return courses_info_json
async def get_courses_liked(size, pid, store):
param = datastore.SearchCoursesPositiveRatedByParam()
param.count = size
param.pid = pid
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.liked, param, store)
return courses_info_json
async def get_courses_played(size, pid, store):
param = datastore.SearchCoursesPlayedByParam()
param.count = size
param.pid = pid
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.played, param, store)
return courses_info_json
async def get_courses_first_cleared(size, pid, store):
param = datastore.SearchCoursesFirstClearParam()
param.range.offset = 0
param.range.size = size
param.pid = pid
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.first_cleared, param, store)
return courses_info_json
async def get_courses_world_record(size, pid, store):
param = datastore.SearchCoursesBestTimeParam()
param.range.offset = 0
param.range.size = size
param.pid = pid
param.option = datastore.CourseOption.ALL
courses_info_json = await get_course_info_json(CourseRequestType.world_record, param, store)
return courses_info_json
def add_user_info_json(user, json_dict):
json_dict["region"] = user.region
json_dict["region_name"] = Regions[user.region]
json_dict["code"] = user.code
json_dict["pid"] = user.pid
json_dict["name"] = user.name
json_dict["country"] = user.country
json_dict["last_active"] = user.last_active.timestamp()
json_dict["last_active_pretty"] = str(user.last_active)
if len(user.unk2) != 0:
mii_info = get_mii_data(user.unk2)
if debug_enabled:
json_dict["mii_data"] = user.unk2
else:
json_dict["mii_data"] = base64.b64encode(user.unk2).decode("ascii")
json_dict["mii_image"] = mii_info[0]
json_dict["mii_studio_code"] = mii_info[1]
wearing_outfit = UserIsOutfit[user.unk1.unk3]
json_dict["pose"] = user.unk1.unk1
json_dict["hat"] = user.unk1.unk2
json_dict["shirt"] = user.unk1.unk3
json_dict["pants"] = user.unk1.unk4
json_dict["pose_name"] = UserPose[user.unk1.unk1]
json_dict["hat_name"] = UserHat[user.unk1.unk2]
json_dict["shirt_name"] = UserShirt[user.unk1.unk3]
if user.unk1.unk4 == 0 and wearing_outfit:
json_dict["pants_name"] = "None"
else:
json_dict["pants_name"] = UserPants[user.unk1.unk4]
json_dict["wearing_outfit"] = wearing_outfit
if len(user.play_stats) == 4:
json_dict["courses_played"] = user.play_stats[0]
json_dict["courses_cleared"] = user.play_stats[2]
json_dict["courses_attempted"] = user.play_stats[1]
json_dict["courses_deaths"] = user.play_stats[3]
if len(user.maker_stats) == 2:
json_dict["likes"] = user.maker_stats[0]
json_dict["maker_points"] = user.maker_stats[1]
if len(user.endless_challenge_high_scores) == 4:
json_dict["easy_highscore"] = user.endless_challenge_high_scores[0]
json_dict["normal_highscore"] = user.endless_challenge_high_scores[1]
json_dict["expert_highscore"] = user.endless_challenge_high_scores[2]
json_dict["super_expert_highscore"] = user.endless_challenge_high_scores[3]