-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtweaks.cpp
3788 lines (2988 loc) · 181 KB
/
tweaks.cpp
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
#include "tweaks.hpp"
#include <typeinfo>
#include <memory>
#include <algorithm>
#include <sstream>
#include <numbers>
#include <version.hpp>
#include <text_replacements.hpp>
#include <libs/tinyxml2.hpp>
#include <libs/yaml.hpp>
#include <asm/patches/asm_constants.hpp>
#include <filetypes/bflim.hpp>
#include <filetypes/bflyt.hpp>
#include <filetypes/bfres.hpp>
#include <filetypes/charts.hpp>
#include <filetypes/dzx.hpp>
#include <filetypes/elf.hpp>
#include <filetypes/util/elfUtil.hpp>
#include <filetypes/events.hpp>
#include <filetypes/jpc.hpp>
#include <filetypes/msbt.hpp>
#include <filetypes/util/msbtMacros.hpp>
#include <utility/color.hpp>
#include <utility/string.hpp>
#include <utility/file.hpp>
#include <command/Log.hpp>
#include <command/GamePath.hpp>
#include <command/RandoSession.hpp>
#define FILETYPE_ERROR_CHECK(func) { \
if(const auto error = func; error != decltype(error)::NONE) {\
ErrorLog::getInstance().log(std::string("Encountered ") + &(typeid(error).name()[5]) + " on line " TOSTRING(__LINE__)); \
return (int)TweakError::FILETYPE_ERROR; \
} \
}
#define RPX_ERROR_CHECK(func) { \
if(const ELFError error = func; error != ELFError::NONE) {\
ErrorLog::getInstance().log(std::string("Failed rpx operation on line ") + std::to_string(__LINE__)); \
return (int)TweakError::RPX_OPERATION_FAILED; \
} \
}
#define TWEAK_ERR_CHECK(func) { \
if(const TweakError error = func; error != TweakError::NONE) { \
ErrorLog::getInstance().log(std::string("Encountered error in tweak ") + #func); \
return error; \
} \
}
using eType = Utility::Endian::Type;
static std::unordered_map<std::string, uint32_t> custom_symbols;
static TweakError Load_Custom_Symbols(const fspath& file_path) {
std::string file_data;
if(Utility::getFileContents(file_path, file_data, true)) LOG_ERR_AND_RETURN(TweakError::DATA_FILE_MISSING);
YAML::Node symbols = YAML::Load(file_data);
for (const auto& symbol : symbols) {
custom_symbols[symbol.first.as<std::string>()] = symbol.second.as<uint32_t>();
}
return TweakError::NONE;
}
static TweakError Apply_Patch(const fspath& file_path) {
std::string file_data;
if (Utility::getFileContents(file_path, file_data, true)) LOG_ERR_AND_RETURN(TweakError::DATA_FILE_MISSING);
const YAML::Node patches = YAML::Load(file_data);
RandoSession::CacheEntry& entry = g_session.openGameFile("code/cking.rpx@RPX@ELF");
if(!patches["Data"] && !patches["Relocations"]) return TweakError::PATCH_MISSING_KEY;
if(patches["Data"]) {
entry.addAction([patches](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
for (const auto& patch : patches["Data"]) {
const uint32_t offset = patch.first.as<uint32_t>();
offset_t sectionOffset = elfUtil::AddressToOffset(elf, offset);
if (!sectionOffset) { //address not in section
std::string data;
for (const uint8_t& byte : patch.second.as<std::vector<uint8_t>>()) {
data += byte;
}
RPX_ERROR_CHECK(elf.extend_section(2, offset, data)); //add data at the specified offset
}
else {
for (const uint8_t& byte : patch.second.as<std::vector<uint8_t>>()) {
RPX_ERROR_CHECK(elfUtil::write_u8(elf, sectionOffset, byte));
sectionOffset.offset++; //Cycles through the bytes individually, need to increase the offset by one each time
}
}
}
return true;
});
}
if(patches["Relocations"]) {
for (const auto& relocation : patches["Relocations"]) {
if(!relocation["r_offset"].IsScalar() || !relocation["r_info"].IsScalar() || !relocation["r_addend"].IsScalar()) {
LOG_ERR_AND_RETURN(TweakError::RELOCATION_MISSING_KEY);
}
Elf32_Rela reloc;
reloc.r_offset = relocation["r_offset"].as<uint32_t>();
reloc.r_info = relocation["r_info"].as<uint32_t>();
reloc.r_addend = relocation["r_addend"].as<uint32_t>();
entry.addAction([reloc](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
if(reloc.r_offset >= 0x10000000) {
if(reloc.r_offset >= 0x1018C0C0) {
RPX_ERROR_CHECK(elfUtil::addRelocation(elf, 9, reloc)); //in the .data section, go in .rela.data
}
else {
RPX_ERROR_CHECK(elfUtil::addRelocation(elf, 8, reloc)); //in the .rodata section, go in .rela.rodata
}
}
else {
RPX_ERROR_CHECK(elfUtil::addRelocation(elf, 7, reloc)); //in the .text section, go in .rela.text
}
return true;
});
}
}
return TweakError::NONE;
}
TweakError set_new_game_starting_location(const uint8_t spawn_id, const uint8_t room_index) {
RandoSession::CacheEntry& entry = g_session.openGameFile("code/cking.rpx@RPX@ELF");
entry.addAction([spawn_id, room_index](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, 0x025B508F), room_index));
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, 0x025B50CB), room_index));
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, 0x025B5093), spawn_id));
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, 0x025B50CF), spawn_id));
return true;
});
return TweakError::NONE;
}
TweakError change_ship_starting_island(const uint8_t room_num) {
const fspath path = getRoomDzrPath("sea", room_num);
RandoSession::CacheEntry& room = g_session.openGameFile(path);
RandoSession::CacheEntry& stage = g_session.openGameFile("content/Common/Pack/first_szs_permanent.pack@SARC@sea_Stage.szs@YAZ0@SARC@Stage.bfres@BFRES@stage.dzs@DZX");
room.addAction([&stage](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(room_dzr, FileTypes::DZXFile, data)
std::vector<ChunkEntry*> ship_spawns = room_dzr.entries_by_type("SHIP");
ChunkEntry* ship_spawn_0 = nullptr;
for (ChunkEntry* spawn : ship_spawns) { //Find spawn with ID 0
if (*reinterpret_cast<uint8_t*>(&spawn->data[0xE]) == 0) ship_spawn_0 = spawn;
}
if(ship_spawn_0 == nullptr) LOG_ERR_AND_RETURN_BOOL(TweakError::MISSING_ENTITY);
stage.addAction([ship_spawn_0 = *ship_spawn_0](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(stage_dzs, FileTypes::DZXFile, data)
std::vector<ChunkEntry*> actors = stage_dzs.entries_by_type("ACTR");
for (ChunkEntry* actor : actors) {
if (std::strncmp(&actor->data[0], "Ship", 4) == 0) {
actor->data.replace(0xC, 0xC, ship_spawn_0.data, 0x0, 0xC);
actor->data.replace(0x1A, 0x2, ship_spawn_0.data, 0xC, 0x2);
actor->data.replace(0x10, 0x4, "\xC8\xF4\x24\x00"s, 0x0, 0x4); //prevent softlock on fire mountain (may be wrong offset)
break;
}
}
return true;
});
return true;
});
room.addDependent(stage.getRoot());
return TweakError::NONE;
}
TweakError make_all_text_instant() {
for (const auto& language : Text::supported_languages) {
const fspath paths[4] = {
"content/Common/Pack/permanent_2d_Us" + language + ".pack@SARC@message_msbt.szs@YAZ0@SARC@message.msbt@MSBT",
"content/Common/Pack/permanent_2d_Us" + language + ".pack@SARC@message2_msbt.szs@YAZ0@SARC@message2.msbt@MSBT",
"content/Common/Pack/permanent_2d_Us" + language + ".pack@SARC@message3_msbt.szs@YAZ0@SARC@message3.msbt@MSBT",
"content/Common/Pack/permanent_2d_Us" + language + ".pack@SARC@message4_msbt.szs@YAZ0@SARC@message4.msbt@MSBT"
};
for (const auto& path : paths) {
RandoSession::CacheEntry& entry = g_session.openGameFile(path);
entry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(msbt, FileTypes::MSBTFile, data);
for (auto& [label, message] : msbt.messages_by_label) {
std::u16string& String = message.text.message;
message.attributes.drawType = 1; //draw instant
std::u16string::size_type wait = String.find(u"\x0e\x01\x06\x02"s); //dont use macro because duration shouldnt matter
while (wait != std::u16string::npos) {
String.erase(wait, 5);
wait = String.find(u"\x0e\x01\x06\x02"s);
}
// Most of these waits are in cutscenes like the credits
// Removing them forces you to advance the text yourself which is undesirable
// So only apply it to a few instances
static const std::unordered_set<std::string> wait_dismiss_to_remove = {
// Healing Grandma text
"02034",
"02039",
"02040",
// Tingle's text
"03522",
"04316",
// Beedle's text
"03956",
// Hollo's text
"05162",
// Auction text
"07416",
// Killer Bees' text
"09935",
// Dampa's text
"12920",
// Flight Control Platform text
"10930",
};
if(wait_dismiss_to_remove.contains(label)) {
std::u16string::size_type wait_dismiss = String.find(u"\x0e\x01\x03\x02"s); //dont use macro because duration shouldnt matter
while (wait_dismiss != std::u16string::npos) {
String.erase(wait_dismiss, 5);
wait_dismiss = String.find(u"\x0e\x01\x03\x02"s);
}
}
std::u16string::size_type wait_dismiss_prompt = String.find(u"\x0e\x01\x02\x02"s); //dont use macro because duration shouldnt matter
while (wait_dismiss_prompt != std::u16string::npos) {
String.erase(wait_dismiss_prompt, 5);
wait_dismiss_prompt = String.find(u"\x0e\x01\x02\x02"s);
}
}
return true;
});
}
}
return TweakError::NONE;
}
TweakError fix_deku_leaf_model() {
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Stage/Omori_Room0.szs@YAZ0@SARC@Room0.bfres@BFRES@room.dzr");
entry.addAction([](RandoSession* session, FileType* data) -> int
{
CAST_ENTRY_TO_FILETYPE(generic, RawFile, data) //do this on the stream so it happens before location mod
FileTypes::DZXFile dzr;
LOG_AND_RETURN_BOOL_IF_ERR(dzr.loadFromBinary(generic.data));
const std::vector<ChunkEntry*> actors = dzr.entries_by_type("ACTR");
for (ChunkEntry* actor : actors) {
if (std::strncmp(&actor->data[0], "itemDek\x00", 8) == 0) actor->data = "item\x00\x00\x00\x00\x01\xFF\x02\x34\xc4\x08\x7d\x81\x45\x9d\x59\xec\xc3\xf5\x8e\xd9\x00\x00\x00\x00\x00\xff\xff\xff"s;
}
LOG_AND_RETURN_BOOL_IF_ERR(dzr.writeToStream(generic.data));
return true;
});
return TweakError::NONE;
}
TweakError allow_all_items_to_be_field_items() {
static constexpr uint32_t item_resources_list_start = 0x101E4674;
static constexpr uint32_t field_item_resources_list_start = 0x101E6A74;
const std::array<uint8_t, 172> Item_Ids_Without_Field_Model = {
0x1a, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x35, 0x36, 0x39, 0x3a, 0x3c, 0x3e, 0x3f, 0x42, 0x43, 0x4c, 0x4d, 0x4e, 0x50, 0x51, 0x52,
0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x98,
0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd,
0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xde, 0xdd, 0xdf, 0xe0, 0xe1, 0xe2,
0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe
};
const std::unordered_map<uint8_t, uint32_t> szs_name_pointers{
{0x1a, 0x1004e5d8}, {0x20, 0x1004e414}, {0x21, 0x1004ec54}, {0x22, 0x1004e578}, {0x23, 0x1004e4a8}, {0x24, 0x1004e4d0}, {0x25, 0x1004e548}, {0x26, 0x1004e658}, {0x27, 0x1004e730}, {0x28, 0x1004e4f0}, {0x29, 0x1004e498}, {0x2a, 0x1004e550}, {0x2b, 0x1004e4a0}, {0x2c, 0x1004e4d8}, {0x2d, 0x1004e6b0}, {0x2e, 0x1004e5c0}, {0x2f, 0x1004e4e8}, {0x30, 0x1004e4c8}, {0x31, 0x1004e41c}, {0x32, 0x1004e5c0},
{0x33, 0x1004e510}, {0x35, 0x1004e580}, {0x36, 0x1004e590}, {0x38, 0x1004e558}, {0x3c, 0x1004e560}, {0x3f, 0x1004e440}, {0x42, 0x1004e518}, {0x43, 0x1004e520}, {0x4c, 0x1004e4b8}, {0x4d, 0x1004e4b0}, {0x4e, 0x1004e698}, {0x50, 0x1004e430}, {0x51, 0x1004e538}, {0x52, 0x1004e530}, {0x53, 0x1004e528}, {0x54, 0x1004e5b0}, {0x55, 0x1004e5b0}, {0x56, 0x1004e5b8}, {0x57, 0x1004e5a0}, {0x58, 0x1004e5a8},
{0x59, 0x1004e598}, {0x61, 0x1004e570}, {0x62, 0x1004e600}, {0x63, 0x1004e608}, {0x64, 0x1004e610}, {0x65, 0x1004e618}, {0x66, 0x1004e620}, {0x67, 0x1004e628}, {0x68, 0x1004e630}, {0x69, 0x1004ec24}, {0x6a, 0x1004ec3c}, {0x6b, 0x1004ec48}, {0x6c, 0x1004e518}, {0x6d, 0x1004e518}, {0x6e, 0x1004e518}, {0x6f, 0x1004e518}, {0x70, 0x1004e518}, {0x71, 0x1004e518}, {0x72, 0x1004e518}, {0x77, 0x1004e434},
{0x78, 0x1004e434}, {0x79, 0x1004e638}, {0x7a, 0x1004e638}, {0x7b, 0x1004e638}, {0x7c, 0x1004e638}, {0x7d, 0x1004e638}, {0x7e, 0x1004e638}, {0x7f, 0x1004e638}, {0x80, 0x1004e638}, {0x98, 0x1004e5e0}, {0x99, 0x1004e5e8}, {0x9a, 0x1004e5f0}, {0x9b, 0x1004e5f8}, {0x9c, 0x1004e688}, {0x9d, 0x1004e500}, {0x9e, 0x1004e4f8}, {0x9f, 0x1004e658}, {0xa0, 0x1004e518}, {0xa1, 0x1004e518}, {0xa2, 0x1004e518},
{0xa3, 0x1004e660}, {0xa4, 0x1004e668}, {0xa5, 0x1004e670}, {0xa6, 0x1004e678}, {0xa7, 0x1004e680}, {0xab, 0x1004e470}, {0xac, 0x1004e478}, {0xad, 0x1004e490}, {0xae, 0x1004e4a0}, {0xaf, 0x1004e480}, {0xb0, 0x1004e488}, {0xb3, 0x1004e5d8}, {0xb4, 0x1004e5d8}, {0xb5, 0x1004e5d8}, {0xb6, 0x1004e5d8}, {0xb7, 0x1004e5d8}, {0xb8, 0x1004e5d8}, {0xb9, 0x1004e5d8}, {0xba, 0x1004e5d8}, {0xbb, 0x1004e5d8},
{0xbc, 0x1004e5d8}, {0xbd, 0x1004e5d8}, {0xbe, 0x1004e5d8}, {0xbf, 0x1004e5d8}, {0xc0, 0x1004e5d8}, {0xc1, 0x1004e5d8}, {0xc2, 0x1004e588}, {0xc3, 0x1004e588}, {0xc4, 0x1004e588}, {0xc5, 0x1004e588}, {0xc6, 0x1004e588}, {0xc7, 0x1004e588}, {0xc8, 0x1004e588}, {0xc9, 0x1004e588}, {0xca, 0x1004e588}, {0xcb, 0x1004e468}, {0xcc, 0x1004e640}, {0xcd, 0x1004e640}, {0xce, 0x1004e640}, {0xcf, 0x1004e640},
{0xd0, 0x1004e640}, {0xd1, 0x1004e640}, {0xd2, 0x1004e640}, {0xd3, 0x1004e640}, {0xd4, 0x1004e640}, {0xd5, 0x1004e640}, {0xd6, 0x1004e640}, {0xd7, 0x1004e640}, {0xd8, 0x1004e640}, {0xd9, 0x1004e640}, {0xda, 0x1004e640}, {0xdb, 0x1004e650}, {0xdc, 0x1004e468}, {0xdd, 0x1004e640}, {0xde, 0x1004e640}, {0xdf, 0x1004e640}, {0xe0, 0x1004e640}, {0xe1, 0x1004e640}, {0xe2, 0x1004e640}, {0xe3, 0x1004e640},
{0xe4, 0x1004e640}, {0xe5, 0x1004e640}, {0xe6, 0x1004e640}, {0xe7, 0x1004e640}, {0xe8, 0x1004e640}, {0xe9, 0x1004e640}, {0xea, 0x1004e640}, {0xeb, 0x1004e640}, {0xec, 0x1004e640}, {0xed, 0x1004e648}, {0xee, 0x1004e648}, {0xef, 0x1004e648}, {0xf0, 0x1004e648}, {0xf1, 0x1004e648}, {0xf2, 0x1004e648}, {0xf3, 0x1004e648}, {0xf4, 0x1004e648}, {0xf5, 0x1004e648}, {0xf6, 0x1004e648}, {0xf7, 0x1004e648},
{0xf8, 0x1004e648}, {0xf9, 0x1004e648}, {0xfa, 0x1004e638}, {0xfb, 0x1004e648}, {0xfc, 0x1004e638}, {0xfd, 0x1004e648}, {0xfe, 0x1004e638}
};
RandoSession::CacheEntry& rpx = g_session.openGameFile("code/cking.rpx@RPX@ELF");
for (const uint8_t& item_id : Item_Ids_Without_Field_Model) {
uint32_t item_resources_addr_to_fix = 0x0;
uint8_t item_id_to_copy_from = 0x0;
if (item_id == 0x39 || item_id == 0x3a || item_id == 0x3e) {
item_id_to_copy_from = 0x38;
item_resources_addr_to_fix = item_resources_list_start + item_id * 0x24;
}
else if (item_id >= 0x6d && item_id <= 0x72) {
item_id_to_copy_from = 0x22;
item_resources_addr_to_fix = item_resources_list_start + item_id * 0x24;
}
else if (item_id == 0xb1 || item_id == 0xb2) {
item_id_to_copy_from = 0x52;
item_resources_addr_to_fix = item_resources_list_start + item_id * 0x24;
}
else {
item_id_to_copy_from = item_id;
}
const uint32_t item_resources_addr_to_copy_from = item_resources_list_start + item_id_to_copy_from * 0x24;
const uint32_t field_item_resources_addr = field_item_resources_list_start + item_id * 0x1c;
uint32_t szs_name_pointer = 0;
uint32_t section_start = 0x10000000;
if (item_id == 0xAA) {
//szs_name_pointer = custom_symbols.at("hurricane_spin_item_resource_arc_name");
szs_name_pointer = szs_name_pointers.at(0x38); //issues with custom .szs currently, use sword model instead
item_resources_addr_to_fix = item_resources_list_start + item_id * 0x24;
//section_start = 0x02000000; //custom stuff only gets put in .text
//not needed because we use the sword model (would be for a custom szs)
}
else {
szs_name_pointer = szs_name_pointers.at(item_id_to_copy_from);
}
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, field_item_resources_addr), szs_name_pointer));
return true;
});
if (item_resources_addr_to_fix) {
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_resources_addr_to_fix), szs_name_pointer));
return true;
});
}
//need relocation entries so pointers work on console
Elf32_Rela relocation;
relocation.r_offset = field_item_resources_addr;
if (section_start == 0x02000000) {
relocation.r_info = 0x00000101; //need different .symtab index for .text pointer
}
else {
relocation.r_info = 0x00000201;
}
relocation.r_addend = szs_name_pointer - section_start; //needs offset into the .rodata section (.text for vscroll), subtract start address from data location
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::addRelocation(elf, 9, relocation));
return true;
});
if (item_resources_addr_to_fix) {
Elf32_Rela relocation2;
relocation2.r_offset = item_resources_addr_to_fix;
relocation2.r_info = relocation.r_info; //same as first entry
relocation2.r_addend = relocation.r_addend; //same as first entry
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::addRelocation(elf, 9, relocation2));
return true;
});
}
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
const std::vector<uint8_t> data1 = elfUtil::read_bytes(elf, elfUtil::AddressToOffset(elf, item_resources_addr_to_copy_from + 8), 0xD);
const std::vector<uint8_t> data2 = elfUtil::read_bytes(elf, elfUtil::AddressToOffset(elf, item_resources_addr_to_copy_from + 0x1C), 4);
RPX_ERROR_CHECK(elfUtil::write_bytes(elf, elfUtil::AddressToOffset(elf, field_item_resources_addr + 4), data1));
RPX_ERROR_CHECK(elfUtil::write_bytes(elf, elfUtil::AddressToOffset(elf, field_item_resources_addr + 0x14), data2));
if (item_resources_addr_to_fix) {
RPX_ERROR_CHECK(elfUtil::write_bytes(elf, elfUtil::AddressToOffset(elf, item_resources_addr_to_fix + 8), data1));
RPX_ERROR_CHECK(elfUtil::write_bytes(elf, elfUtil::AddressToOffset(elf, item_resources_addr_to_fix + 0x1C) , data2));
}
return true;
});
}
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
for (const uint32_t& address : { 0x0255220CU, 0x02552214U, 0x0255221CU, 0x02552224U, 0x0255222CU, 0x02552234U, 0x02552450U }) { //unsigned to make compiler happy
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, address), 0x60000000));
}
return true;
});
//execItemGet, mode_wait, and getYOffset had their switch cases optimized out, so their patches are a little more involved in HD
LOG_AND_RETURN_IF_ERR(Apply_Patch(Utility::get_data_path() / "asm/patch_diffs/field_items_diff.yaml"));
const uint32_t item_info_list_start = 0x101E8674;
for (unsigned int item_id = 0x00; item_id < 0xFF + 1; item_id++) {
const uint32_t item_info_entry_addr = item_info_list_start + 4 * item_id;
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
const uint8_t original_y_offset = elfUtil::read_u8(elf, elfUtil::AddressToOffset(elf, item_info_entry_addr + 1));
if (original_y_offset == 0) {
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, item_info_entry_addr + 1), 0x28));
}
uint8_t original_radius = elfUtil::read_u8(elf, elfUtil::AddressToOffset(elf, item_info_entry_addr + 2));
if (original_radius == 0) {
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, item_info_entry_addr + 2), 0x28));
}
return true;
});
}
//IMPROVEMENT: make vscroll not crash + add it to code
//WWHD changes make this much more complex ^
return TweakError::NONE;
}
TweakError remove_shop_item_forced_uniqueness_bit() {
const uint32_t shop_item_data_list_start = 0x101eaea4;
RandoSession::CacheEntry& rpx = g_session.openGameFile("code/cking.rpx@RPX@ELF");
for (const uint8_t shop_item_index : { 0x0, 0xB, 0xC, 0xD }) {
const uint32_t shop_item_data_addr = shop_item_data_list_start + shop_item_index * 0x10;
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
uint8_t buy_requirements_bitfield = elfUtil::read_u8(elf, elfUtil::AddressToOffset(elf, shop_item_data_addr + 0xC));
buy_requirements_bitfield = (buy_requirements_bitfield & (~0x2));
RPX_ERROR_CHECK(elfUtil::write_u8(elf, elfUtil::AddressToOffset(elf, shop_item_data_addr + 0xC), buy_requirements_bitfield));
return true;
});
}
return TweakError::NONE;
}
TweakError remove_ff2_cutscenes(const bool& randomize_boss_entrances) {
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Stage/M2tower_Room0.szs@YAZ0@SARC@Room0.bfres@BFRES@room.dzr@DZX");
entry.addAction([&](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(dzr, FileTypes::DZXFile, data)
std::vector<ChunkEntry*> spawns = dzr.entries_by_type("PLYR");
for (ChunkEntry* spawn : spawns) {
if (spawn->data[29] == '\x10') {
spawn->data[8] = '\xFF';
break;
}
}
if (!randomize_boss_entrances) {
std::vector<ChunkEntry*> exits = dzr.entries_by_type("SCLS");
for (ChunkEntry* exit : exits) {
if (std::strncmp(&exit->data[0], "M2ganon\x00", 8) == 0) {
exit->data = "sea\x00\x00\x00\x00\x00\x00\x01\x00\xFF"s;
break;
}
}
}
return true;
});
return TweakError::NONE;
}
TweakError make_items_progressive() {
LOG_AND_RETURN_IF_ERR(Apply_Patch(Utility::get_data_path() / "asm/patch_diffs/make_items_progressive_diff.yaml"));
const uint32_t item_get_func_pointer = 0x0001DA54; //First relevant relocation entry in .rela.data (overwrites .data section when loaded)
RandoSession::CacheEntry& rpx = g_session.openGameFile("code/cking.rpx@RPX@ELF");
rpx.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data);
for (const uint8_t sword_id : {0x38, 0x39, 0x3A, 0x3D, 0x3E}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (sword_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_sword_item_func") - 0x02000000));
}
for (const uint8_t shield_id : {0x3B, 0x3C}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (shield_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_shield_item_func") - 0x02000000));
}
for (const uint8_t bow_id : {0x27, 0x35, 0x36}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (bow_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_bow_item_func") - 0x02000000));
}
for (const uint8_t wallet_id : {0xAB, 0xAC}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (wallet_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_wallet_item_func") - 0x02000000));
}
for (const uint8_t bomb_bag_id : {0xAD, 0xAE}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (bomb_bag_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_bomb_bag_item_func") - 0x02000000));
}
for (const uint8_t quiver_id : {0xAF, 0xB0}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (quiver_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_quiver_item_func") - 0x02000000));
}
for (const uint8_t picto_id : {0x23, 0x26}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (picto_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_picto_box_item_func") - 0x02000000));
}
for (const uint8_t sail_id : {0x77, 0x78}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (sail_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_sail_item_func") - 0x02000000));
}
for (const uint8_t magic_id : {0xB1, 0xB2}) {
const uint32_t item_get_func_addr = item_get_func_pointer + (magic_id * 0xC) + 8;
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_addr, 9), custom_symbols.at("progressive_magic_meter_item_func") - 0x02000000));
}
//nop some code that sets max bombs and arrows to 30
//This avoids downgrading bomb bags or quivers
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, 0x0254e8c4), 0x60000000));
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, 0x0254e8cc), 0x60000000));
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, 0x0254e66c), 0x60000000));
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, 0x0254e674), 0x60000000));
//Modify the deku leaf to skip giving you magic
//Instead we make magic its own item that the player starts with by default
//Allows other items to use magic before getting leaf
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, 0x0254e96c), 0x60000000));
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, 0x0254e97c), 0x60000000));
return true;
});
// Add an item get message for the normal magic meter since it didn't have one in vanilla
std::unordered_map<std::string, std::u16string> messages = {
{"English", DRAW_INSTANT + u"You got " + TEXT_COLOR_RED + u"magic power" + TEXT_COLOR_DEFAULT + u"!\nNow you can use magic items!\0"s},
{"Spanish", DRAW_INSTANT + u"¡Has obtenido el " + TEXT_COLOR_RED + u"Poder Mágico" + TEXT_COLOR_DEFAULT + u"!\n¡Ahora podrás utilizar objetos mágicos!\0"s},
{"French", DRAW_INSTANT + u"Vous obtenez l'" + TEXT_COLOR_RED + u"Energie Magique" + TEXT_COLOR_DEFAULT + u"!\n" + Text::word_wrap_string(u"Vous pouvez maintenant utiliser les objets magiques!\0"s, 43)},
};
for (const auto& language : Text::supported_languages) {
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Pack/permanent_2d_Us" + language + ".pack@SARC@message_msbt.szs@YAZ0@SARC@message.msbt@MSBT");
entry.addAction([messages, language](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(msbt, FileTypes::MSBTFile, data)
const Message& to_copy = msbt.messages_by_label["00" + std::to_string(101 + 0xB2)];
const std::u16string message = messages.at(language);
msbt.addMessage("00" + std::to_string(101 + 0xB1), to_copy.attributes, to_copy.style, message);
return true;
});
}
return TweakError::NONE;
}
TweakError add_ganons_tower_warp_to_ff2() {
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Pack/szs_permanent1.pack@SARC@sea_Room1.szs@YAZ0@SARC@Room1.bfres@BFRES@room.dzr@DZX");
entry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(dzr, FileTypes::DZXFile, data)
ChunkEntry& warp = dzr.add_entity("ACTR", 1);
warp.data = "Warpmj\x00\x00\x00\x00\x00\x11\xc8\x93\x0f\xd9\x00\x00\x00\x00\xc8\x91\xf7\xfa\x00\x00\x00\x00\x00\x00\xff\xff"s;
return 1;
});
return TweakError::NONE;
}
TweakError add_chest_in_place_medli_gift() {
RandoSession::CacheEntry& stage = g_session.openGameFile("content/Common/Stage/M_Dra09_Stage.szs@YAZ0@SARC@Stage.bfres@BFRES@stage.dzs");
stage.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(generic, RawFile, data) //do this on the stream so it happens before location mod
FileTypes::DZXFile dzs;
LOG_AND_RETURN_BOOL_IF_ERR(dzs.loadFromBinary(generic.data));
ChunkEntry& chest = dzs.add_entity("TRES");
chest.data = "takara3\x00\xFF\x20\x08\x80\xc4\xca\x99\xec\x46\x54\x80\x00\x43\x83\x84\x5a\x00\x09\xcc\x16\x0f\xff\xff\xff"s;
LOG_AND_RETURN_BOOL_IF_ERR(dzs.writeToStream(generic.data));
return true;
});
RandoSession::CacheEntry& dungeon = g_session.openGameFile("content/Common/Stage/M_NewD2_Stage.szs@YAZ0@SARC@Stage.bfres@BFRES@stage.dzs@DZX");
dungeon.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(dzs, FileTypes::DZXFile, data)
ChunkEntry& dummyChest = dzs.add_entity("TRES");
dummyChest.data = "takara3\x00\xFF\x20\x08\x80\xc4\xca\x99\xec\x46\x54\x80\x00\x43\x83\x84\x5a\x00\x09\xcc\x16\x0f\xff\xff\xff"s;
return true;
});
return TweakError::NONE;
}
TweakError add_chest_in_place_queen_fairy_cutscene() {
RandoSession::CacheEntry& room = g_session.openGameFile("content/Common/Pack/szs_permanent2.pack@SARC@sea_Room9.szs@YAZ0@SARC@Room9.bfres@BFRES@room.dzr");
room.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(generic, RawFile, data) //do this on the stream so it happens before location mod
FileTypes::DZXFile dzr;
LOG_AND_RETURN_BOOL_IF_ERR(dzr.loadFromBinary(generic.data));
ChunkEntry& chest = dzr.add_entity("TRES");
chest.data = "takara3\x00\xFF\x20\x0e\x00\xc8\x2f\xcf\xc0\x44\x34\xc0\x00\xc8\x43\x4e\xc0\x00\x09\x10\x00\xa5\xff\xff\xff"s;
LOG_AND_RETURN_BOOL_IF_ERR(dzr.writeToStream(generic.data));
return true;
});
return TweakError::NONE;
}
TweakError add_more_magic_jars() {
//DRC doesn't have any magic since you wouldn't normally have Deku Leaf there
//But it can be required in the randomizer, so we make some skulls drop magic
//Same thing for arrows
{
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Stage/M_NewD2_Room2.szs@YAZ0@SARC@Room2.bfres@BFRES@room.dzr@DZX");
entry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(drc_hub, FileTypes::DZXFile, data)
std::vector<ChunkEntry*> actors = drc_hub.entries_by_type("ACTR");
std::vector<ChunkEntry*> skulls;
for (ChunkEntry* actor : actors) {
if (std::strncmp(&actor->data[0], "Odokuro\x00", 8) == 0) skulls.push_back(actor);
}
if(skulls.size() < 6) LOG_ERR_AND_RETURN_BOOL(TweakError::MISSING_ENTITY);
skulls[0]->data.replace(0x8, 0x4, "\x75\x7f\xff\x10", 0, 4); //arrows in case logic expects you to use them for BK chest
skulls[2]->data.replace(0x8, 0x4, "\x75\x7f\xff\x09", 0, 4); //small magic
skulls[5]->data.replace(0x8, 0x4, "\x75\x7f\xff\x0A", 0, 4); //large magic
return true;
});
}
{
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Stage/M_NewD2_Room10.szs@YAZ0@SARC@Room10.bfres@BFRES@room.dzr@DZX");
entry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(drc_before_boss, FileTypes::DZXFile, data)
std::vector<ChunkEntry*> actors = drc_before_boss.entries_by_type("ACTR");
std::vector<ChunkEntry*> skulls;
for (ChunkEntry* actor : actors) {
if (std::strncmp(&actor->data[0], "Odokuro\x00", 8) == 0) skulls.push_back(actor);
}
if(skulls.size() < 11) LOG_ERR_AND_RETURN_BOOL(TweakError::MISSING_ENTITY);
skulls[0]->data.replace(0x8, 0x4, "\x75\x7f\xff\x0A", 0, 4); //large magic
skulls[9]->data.replace(0x8, 0x4, "\x75\x7f\xff\x0A", 0, 4); //large magic
return true;
});
}
//The grass on the small islands behind DRI do not have guaranteed magic
//Add grass that will always drop some on each of the islands
{
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Pack/szs_permanent1.pack@SARC@sea_Room13.szs@YAZ0@SARC@Room13.bfres@BFRES@room.dzr@DZX");
entry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(dri, FileTypes::DZXFile, data)
ChunkEntry& grass1 = dri.add_entity("ACTR");
grass1.data = "kusax1\x00\x00\x00\x00\x0E\x00\x48\x4C\xC7\x80\x44\xED\x80\x00\xC8\x45\xB7\xC0\x00\x00\x00\x00\x00\x00\xFF\xFF"s; //62.50% chance of small magic, 37.50% chance of large magic
ChunkEntry& grass2 = dri.add_entity("ACTR");
grass2.data = "kusax1\x00\x00\x00\x00\x0E\x00\x48\x4C\x6D\x40\x44\xA2\x80\x00\xC8\x4D\x38\x40\x00\x00\x00\x00\x00\x00\xFF\xFF"s; //62.50% chance of small magic, 37.50% chance of large magic
return true;
});
}
//Add magic to one of the pots outside the TotG miniboss
{
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Stage/Siren_Room14.szs@YAZ0@SARC@Room14.bfres@BFRES@room.dzr@DZX");
entry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(totg, FileTypes::DZXFile, data)
std::vector<ChunkEntry*> actors = totg.entries_by_type("ACTR");
std::vector<ChunkEntry*> pots;
for (ChunkEntry* actor : actors) {
if (std::strncmp(&actor->data[0], "kotubo\x00\x00", 8) == 0) pots.push_back(actor);
}
if(pots.size() < 2) LOG_ERR_AND_RETURN_BOOL(TweakError::MISSING_ENTITY);
pots[1]->data = "\x6B\x6F\x74\x75\x62\x6F\x00\x00\x70\x7F\xFF\x0A\xC5\x6E\x20\x00\x43\x66\x00\x05\xC5\xDF\xC0\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF"s;
return true;
});
}
return TweakError::NONE;
}
TweakError modify_title_screen() {
using namespace NintendoWare::Layout;
RandoSession::CacheEntry& lytEntry = g_session.openGameFile("content/Common/Layout/Title_00.szs@YAZ0@SARC@blyt/Title_00.bflyt@BFLYT");
lytEntry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(layout, FileTypes::FLYTFile, data)
//add version number
Pane& newPane = layout.rootPane.children[0].children[1].children[3].duplicateChildPane(1); //unused version number text
newPane.pane->name = "T_Version";
newPane.pane->name.resize(0x18);
dynamic_cast<txt1*>(newPane.pane.get())->text = u"Ver " + Utility::Str::toUTF16(RANDOMIZER_VERSION) + u'\0'; //~20-21 characters per line
dynamic_cast<txt1*>(newPane.pane.get())->fontIndex = 0;
dynamic_cast<txt1*>(newPane.pane.get())->restrictedLen = dynamic_cast<txt1*>(newPane.pane.get())->text.length() * 2; //includes null
dynamic_cast<txt1*>(newPane.pane.get())->lineAlignment = txt1::LineAlignment::CENTER;
//update "HD" image position
layout.rootPane.children[0].children[1].children[1].children[0].pane->translation.X = -165.0f;
layout.rootPane.children[0].children[1].children[1].children[0].pane->translation.Y = 12.0f;
//update subtitle size/position
layout.rootPane.children[0].children[1].children[1].children[1].children[0].pane->translation.Y = -30.0f;
layout.rootPane.children[0].children[1].children[1].children[1].children[0].pane->height = 120.0f;
//update subtitle mask size/position
layout.rootPane.children[0].children[1].children[1].children[1].children[1].pane->translation.Y = -30.0f;
layout.rootPane.children[0].children[1].children[1].children[1].children[1].pane->height = 120.0f;
return true;
});
//update "The Legend of Zelda" texture
RandoSession::CacheEntry& tEntry = g_session.openGameFile("content/Common/Layout/Title_00.szs@YAZ0@SARC@timg/TitleLogoZelda_00^l.bflim@BFLIM");
tEntry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(title, FileTypes::FLIMFile, data)
FILETYPE_ERROR_CHECK(title.replaceWithDDS(Utility::get_data_path() / "assets/Title.dds", GX2TileMode::GX2_TILE_MODE_DEFAULT, 0, true));
return true;
});
//update "The Wind Waker" texture
RandoSession::CacheEntry& sEntry = g_session.openGameFile("content/Common/Layout/Title_00.szs@YAZ0@SARC@timg/TitleLogoWindwaker_00^l.bflim@BFLIM");
sEntry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(subtitle, FileTypes::FLIMFile, data)
FILETYPE_ERROR_CHECK(subtitle.replaceWithDDS(Utility::get_data_path() / "assets/Subtitle.dds", GX2TileMode::GX2_TILE_MODE_DEFAULT, 0, true));
return true;
});
//update mask for "The Wind Waker" texture
RandoSession::CacheEntry& mEntry = g_session.openGameFile("content/Common/Layout/Title_00.szs@YAZ0@SARC@timg/TitleLogoWindwakerMask_00^s.bflim@BFLIM");
mEntry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(mask, FileTypes::FLIMFile, data)
FILETYPE_ERROR_CHECK(mask.replaceWithDDS(Utility::get_data_path() / "assets/SubtitleMask.dds", GX2TileMode::GX2_TILE_MODE_DEFAULT, 0, false));
return true;
});
//update sparkle size/position
RandoSession::CacheEntry& rpx = g_session.openGameFile("code/cking.rpx@RPX@ELF");
rpx.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::write_float(elf, elfUtil::AddressToOffset(elf, 0x101F7048), 1.4f)); //scale
RPX_ERROR_CHECK(elfUtil::write_float(elf, elfUtil::AddressToOffset(elf, 0x101F7044), 2.25f)); //possibly particle size, JP changes it for its larger title text
RPX_ERROR_CHECK(elfUtil::write_float(elf, elfUtil::AddressToOffset(elf, 0x10108280), -38.0f)); //vertical position
return true;
});
return TweakError::NONE;
}
TweakError update_name_and_icon() {
if(!g_session.copyToGameFile(Utility::get_data_path() / "assets/iconTex.tga", "meta/iconTex.tga", /*resourceFile = */ true)) LOG_ERR_AND_RETURN(TweakError::FILE_COPY_FAILED);
RandoSession::CacheEntry& metaEntry = g_session.openGameFile("meta/meta.xml");
metaEntry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(generic, RawFile, data)\
std::stringstream& metaStream = generic.data;
tinyxml2::XMLDocument meta;
meta.Parse(metaStream.str().c_str(), metaStream.str().size());
tinyxml2::XMLElement* metaRoot = meta.RootElement();
metaRoot->FirstChildElement("longname_en")->SetText("THE LEGEND OF ZELDA\nThe Wind Waker HD Randomizer");
metaRoot->FirstChildElement("longname_fr")->SetText("THE LEGEND OF ZELDA\nThe Wind Waker HD Randomizer");
metaRoot->FirstChildElement("longname_es")->SetText("THE LEGEND OF ZELDA\nThe Wind Waker HD Randomizer");
metaRoot->FirstChildElement("longname_pt")->SetText("THE LEGEND OF ZELDA\nThe Wind Waker HD Randomizer");
metaRoot->FirstChildElement("shortname_en")->SetText("The Wind Waker HD Randomizer");
metaRoot->FirstChildElement("shortname_fr")->SetText("The Wind Waker HD Randomizer");
metaRoot->FirstChildElement("shortname_es")->SetText("The Wind Waker HD Randomizer");
metaRoot->FirstChildElement("shortname_pt")->SetText("The Wind Waker HD Randomizer");
//change the title ID so it gets its own channel when repacked
metaRoot->FirstChildElement("title_id")->SetText("0005000010143599");
tinyxml2::XMLPrinter printer;
meta.Print(&printer);
metaStream.str(printer.CStr());
return true;
});
RandoSession::CacheEntry& appEntry = g_session.openGameFile("code/app.xml");
appEntry.addAction([](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(generic, RawFile, data)\
std::stringstream& appStream = generic.data;
tinyxml2::XMLDocument app;
app.Parse(appStream.str().c_str(), appStream.str().size());
tinyxml2::XMLElement* appRoot = app.RootElement();
appRoot->FirstChildElement("title_id")->SetText("0005000010143599");
tinyxml2::XMLPrinter printer;
app.Print(&printer);
appStream.str(printer.CStr());
return true;
});
return TweakError::NONE;
}
TweakError allow_dungeon_items_to_appear_anywhere(World& world) {
struct dungeon_item_info {
const std::string short_name;
const std::string base_item_name;
const GameItem item_value;
};
const uint32_t item_get_func_pointer = 0x0001DA54; //First relevant relocation entry in .rela.data (overwrites .data section when loaded)
const uint32_t item_resources_list_start = 0x101E4674;
const uint32_t field_item_resources_list_start = 0x101E6A74;
const uint32_t item_info_list_start = 0x101E8674;
const std::unordered_map<std::string, std::string> dungeon_names = {
{"DRC", "Dragon Roost Cavern"},
{"FW", "Forbidden Woods"},
{"TotG", "Tower of the Gods"},
{"FF", "Forsaken Fortress"},
{"ET", "Earth Temple"},
{"WT", "Wind Temple"}
};
const std::unordered_map<std::string, uint8_t> item_name_to_id = {
{"Small Key", 0x15},
{"Dungeon Map", 0x4C},
{"Compass", 0x4D},
{"Big Key", 0x4E}
};
const std::array<dungeon_item_info, 22> dungeon_items = {{
{"DRC", "Small Key", GameItem::DRCSmallKey},
{"DRC", "Big Key", GameItem::DRCBigKey},
{"DRC", "Dungeon Map", GameItem::DRCDungeonMap},
{"DRC", "Compass", GameItem::DRCCompass},
{"FW", "Small Key", GameItem::FWSmallKey},
{"FW", "Big Key", GameItem::FWBigKey},
{"FW", "Dungeon Map", GameItem::FWDungeonMap},
{"FW", "Compass", GameItem::FWCompass},
{"TotG", "Small Key", GameItem::TotGSmallKey},
{"TotG", "Big Key", GameItem::TotGBigKey},
{"TotG", "Dungeon Map", GameItem::TotGDungeonMap},
{"TotG", "Compass", GameItem::TotGCompass},
{"FF", "Dungeon Map", GameItem::FFDungeonMap},
{"FF", "Compass", GameItem::FFCompass},
{"ET", "Small Key", GameItem::ETSmallKey},
{"ET", "Big Key", GameItem::ETBigKey},
{"ET", "Dungeon Map", GameItem::ETDungeonMap},
{"ET", "Compass", GameItem::ETCompass},
{"WT", "Small Key", GameItem::WTSmallKey},
{"WT", "Big Key", GameItem::WTBigKey},
{"WT", "Dungeon Map", GameItem::WTDungeonMap},
{"WT", "Compass", GameItem::WTCompass}
}};
const std::unordered_map<GameItem, std::string> itemToFunc = {
{GameItem::DRCSmallKey, "drc_small_key_item_get_func"},
{GameItem::DRCBigKey, "drc_big_key_item_get_func"},
{GameItem::DRCDungeonMap, "drc_dungeon_map_item_get_func"},
{GameItem::DRCCompass, "drc_compass_item_get_func"},
{GameItem::FWSmallKey, "fw_small_key_item_get_func"},
{GameItem::FWBigKey, "fw_big_key_item_get_func"},
{GameItem::FWDungeonMap, "fw_dungeon_map_item_get_func"},
{GameItem::FWCompass, "fw_compass_item_get_func"},
{GameItem::TotGSmallKey, "totg_small_key_item_get_func"},
{GameItem::TotGBigKey, "totg_big_key_item_get_func"},
{GameItem::TotGDungeonMap, "totg_dungeon_map_item_get_func"},
{GameItem::TotGCompass, "totg_compass_item_get_func"},
{GameItem::FFDungeonMap, "ff_dungeon_map_item_get_func"},
{GameItem::FFCompass, "ff_compass_item_get_func"},
{GameItem::ETSmallKey, "et_small_key_item_get_func"},
{GameItem::ETBigKey, "et_big_key_item_get_func"},
{GameItem::ETDungeonMap, "et_dungeon_map_item_get_func"},
{GameItem::ETCompass, "et_compass_item_get_func"},
{GameItem::WTSmallKey, "wt_small_key_item_get_func"},
{GameItem::WTBigKey, "wt_big_key_item_get_func"},
{GameItem::WTDungeonMap, "wt_dungeon_map_item_get_func"},
{GameItem::WTCompass, "wt_compass_item_get_func"},
};
const std::unordered_map<uint8_t, uint32_t> szs_name_pointers = {
{0x15, 0x1004E448},
{0x4C, 0x1004E4b8},
{0x4D, 0x1004E4b0},
{0x4E, 0x1004E698}
};
const std::unordered_map<std::string, std::u16string> messageBegin = {
{"English", u"You got "},
{"Spanish", u"¡Has conseguido "},
{"French", u"Vous obtenez "},
};
RandoSession::CacheEntry& rpx = g_session.openGameFile("code/cking.rpx@RPX@ELF");
for (const dungeon_item_info& item_data : dungeon_items) {
const std::string item_name = item_data.short_name + " " + item_data.base_item_name;
const uint8_t item_id = static_cast<uint8_t>(item_data.item_value);
const uint8_t base_item_id = item_name_to_id.at(item_data.base_item_name);
const std::string dungeon_name = dungeon_names.at(item_data.short_name);
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)
RPX_ERROR_CHECK(elfUtil::write_u32(elf, elfUtil::AddressToOffset(elf, item_get_func_pointer + (0xC * item_id) + 0x8, 9), custom_symbols.at(itemToFunc.at(item_data.item_value)) - 0x02000000)); //write to the relocation entries
return true;
});
// Get the item out here, since trying to get it in the action causing a seg fault sometimes
auto dungeon_item = world.getItem(dungeon_name + " " + item_data.base_item_name);
for (const auto& language : Text::supported_languages) {
RandoSession::CacheEntry& entry = g_session.openGameFile("content/Common/Pack/permanent_2d_Us" + language + ".pack@SARC@message_msbt.szs@YAZ0@SARC@message.msbt@MSBT");
entry.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(msbt, FileTypes::MSBTFile, data)
const uint32_t message_id = 101 + item_id;
const Message& to_copy = msbt.messages_by_label["00" + std::to_string(101 + base_item_id)];
std::u16string message = messageBegin.at(language) + dungeon_item.getUTF16Name(language, Text::Type::PRETTY) + u"!"s + TEXT_END;
message = Text::word_wrap_string(message, 39);
msbt.addMessage("00" + std::to_string(message_id), to_copy.attributes, to_copy.style, message);
return true;
});
}
const uint32_t item_resources_addr_to_copy_from = item_resources_list_start + base_item_id * 0x24;
const uint32_t field_item_resources_addr_to_copy_from = field_item_resources_list_start + base_item_id * 0x1C;
const uint32_t item_resources_addr = item_resources_list_start + item_id * 0x24;
const uint32_t field_item_resources_addr = field_item_resources_list_start + item_id * 0x1C;
const uint32_t szs_name_pointer = szs_name_pointers.at(base_item_id);
rpx.addAction([=](RandoSession* session, FileType* data) -> int {
CAST_ENTRY_TO_FILETYPE(elf, FileTypes::ELF, data)