-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmm_build.rtl
More file actions
763 lines (629 loc) · 17.3 KB
/
mm_build.rtl
File metadata and controls
763 lines (629 loc) · 17.3 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
/************************************************************
* /Zel/ & z64.me zzrtl script for injecting assets into MM *
************************************************************/
/* MM U */
enum {
TBL_OBJECT = 0x00C58C80 // object table
, TBL_OBJECT_ENTRIES = 643 // object count
, TBL_ACTOR = 0x00C45510 // actor overlay table
, TBL_ACTOR_ENTRIES = 690 // actor overlay count
, TBL_PARTICLE = 0x00C449E0 // particle overlay table
, TBL_PARTICLE_ENTRIES = 39 // particle overlay count
, TBL_SCENE = 0x00C5A1E0 // scene table
, TBL_SCENE_ENTRIES = 113 // scene count
, TBL_DMA = 0x0001A500 // dma table
, TBL_DMA_ENTRIES = 1568 // dma entries
, VRAM_CODE = 0x800A5AC0 // vram start of code
, VROM_CODE = 0x00B3C000 // vrom start of code
, VROM_PLAYER = 0x00CA7F00 // vrom start of player_actor
, DMA_ICON_ITEM_FIELD = 10 // icon_item_field_static
, DMA_ICON_ITEM_DUNGEON // icon_item_dungeon_static
, DMA_ICON_ITEM_GAMEOVER // icon_item_gameover_static
, DMA_MAP_NAME_STATIC // map_name_static
, DMA_UNK_14 // [?]
, DMA_MAP_I_STATIC = 15 // map_i_static
, DMA_UNK_23 = 23 // [?]
, DMA_UNK_24
, DMA_CODE = 31 // code
, DMA_ACTOR_FIRST = 39 // En_Test
, DMA_ACTOR_LAST = 648 // En_Rsn
, DMA_OBJECT_FIRST = 649 // gameplay_keep
, DMA_OBJECT_LAST = 1113 // object_gi_mask13
, DMA_EXCEPT = 1127 // clock face days
, DMA_SCENE_FIRST = 1137 // Z2_20SICHITAI2
, DMA_SCENE_LAST = 1537 // Z2_ALLEY_room_0
, DMA_UNUSED_FIRST = 1538 // bump_texture_static
, DMA_UNUSED_LAST = 1567 // null
, ACTID_LINK = 0 // link's actor id
};
enum bool
{
false = 0
, true = 1
, compress = 1
};
/* helper function that zeroes out skipped table entries */
void
skip_test(struct rom *rom, int index, int *last, int table, int stride)
{
/* skipped one or more entries between invocations */
if (index > *last + 1)
{
/* advance to first skipped entry */
*last = *last + 1;
/* zero out skipped entries */
memset(
rom.raw(table + *last * stride)
, 0
, (index - *last) * stride
);
}
/* update last known index */
*last = index;
}
/* process a single header, updating room command's contents */
/* returns non-zero if header or provided offset (ofs) is invalid */
int
scene_header_rooms(
struct rom *rom
, char *room_ptrs
, int room_ct
, int start
, int sz
, int ofs
)
{
int rseg;
char *b;
char *b_end;
/* entries pointing to 0 are valid, but not processed */
if (!ofs)
return 0;
/* want only lower 3 bytes of ofs from this point on */
rseg = ofs >> 24;
ofs = ofs & 0xFFFFFF;
/* invalid offset conditions */
if (
rseg != 0x02 /* invalid ram segment */
|| (ofs & 3) /* unaligned pointer */
|| ofs + 8 > sz /* pointer exceeds file */
)
return 1;
b = rom.raw(start + ofs);
b_end = rom.raw(start + sz);
/* search scene header for room command (0x04) */
while (b < b_end && *b != 0x04)
{
/* end loop conditions */
if (
*b == 0x14 /* end command */
|| u8(*b) >= 0x20 /* invalid command */
)
b = b_end;
/* advance to next header command */
b = b + 8;
}
/* failed to locate room command, or it is invalid */
if (
b >= b_end /* no room command found */
|| b[1] != room_ct /* invalid room count */
|| get16(b+2) /* bytes 2, 3 aren't 0 */
|| b[4] != 0x02 /* invalid ram segment */
|| (b[7] & 3) /* unaligned pointer */
|| get24(b+5) + b[1]*8 > sz /* pointer exceeds file */
)
return 1;
/* overwrite room pointers */
memcpy(rom.raw(start + get24(b+5)), room_ptrs, room_ct * 8);
/* debug output */
//printf(" > valid header %08X\n", ofs | (rseg << 24));
/* valid header */
return 0;
}
void
scene(struct rom *rom)
{
struct folder *list;
struct folder *room;
struct conf *conf;
char *room_ptrs;
char *scene;
char *name;
char *b;
char *b_end;
int start;
int end;
int sz;
int index;
int msg_id;
int config;
int room_ct;
int index_last;
/* allocate space for 256 room pointers */
room_ptrs = malloc(256 * 4 * 2);
/* scenes aligned 0x1000 in retail */
rom.align(0x1000);
/* enter and parse folder */
dir_enter("scene");
list = list.new(0);
if (!list.count())
die("scene folder is empty");
index_last = -1;
/* repeats for every item in the list */
while (list.remaining())
{
/* initialize variables */
index = list.index();
name = list.name();
/* display progress */
printf("\r""scene %d/%d: ", index, list.max());
/* ensure we don't exceed table limit */
if (index >= TBL_SCENE_ENTRIES)
die("scene table limit exceeded");
/* enter directory of list entry */
dir_enter(name);
/* parse conf */
conf = conf.new("conf.txt", "list");
msg_id = conf.get_int("msg_id");
config = conf.get_int("config");
conf.free();
/* attempt to inject file */
scene = rom.inject("*.zscene", compress);
start = rom.file_start();
end = rom.file_end();
sz = end - start;
/* if scene exists, process title card and rooms */
if (scene)
{
/* inject rooms */
room = room.new("*.zmap");
if (!room.count())
die("no rooms found");
if (room.count() > 127)
die("too many (%d) rooms", room.count());
room_ct = 0;
while (room.remaining())
{
/* injection failure is unacceptable */
if (!rom.inject(room.name(), compress))
die("'%s' injection failure", room.name());
/* add to room pointer list */
put32(room_ptrs + room_ct * 8 , rom.file_start());
put32(room_ptrs + room_ct * 8 + 4, rom.file_end() );
/* proceed to next room */
room_ct++;
room.next();
}
room.free();
/* update room pointers in first header */
if (
scene_header_rooms(
rom
, room_ptrs
, room_ct
, start
, sz
, 0x02000000
)
)
die("invalid header");
/* locate alternate header command (0x18) */
b = rom.raw(start);
b_end = b + sz;
while (b < b_end && *b != 0x18)
{
/* end loop conditions */
if (
*b == 0x14 /* end command */
|| u8(*b) >= 0x20 /* invalid command */
)
b = b_end;
/* advance to next header command */
b = b + 8;
}
/* alternate header command found */
if (b < b_end)
{
/* invalid ram segment */
if (b[4] != 0x02)
die("0x18 cmd: invalid ram segment %02X", b[4]);
/* invalid offset */
if (
(b[7] & 3) /* unaligned pointer */
|| get24(b+5) + 4 > sz /* pointer exceeds file */
)
die("0x18 cmd: invalid offset %08X", get32(b+4));
/* grab alternate header list */
b = rom.raw(start + get24(b+5));
/* process list */
while (b < b_end)
{
/* scene header failed in some way; end of list */
if (
scene_header_rooms(
rom
, room_ptrs
, room_ct
, start
, sz
, get32(b)
)
)
b = b_end;
/* advance to next entry */
b = b + 4;
}
}
}
/* zero out any skipped entries */
skip_test(rom, index, &index_last, TBL_SCENE, 20);
/* go to table entry */
rom.seek(TBL_SCENE + index * 16);
/* write its table entry */
rom.write32(start); /* vrom start */
rom.write32(end); /* vrom end */
rom.write16(msg_id); /* area name text */
rom.write8(0); /* padding */
rom.write8(config); /* texture animation */
rom.write32(0); /* padding */
/* leave directory */
dir_leave();
/* go to next entry in list */
list.next();
}
/* cleanup */
free(room_ptrs);
list.free();
dir_leave();
printf("success!\n");
}
void
archive(struct rom *rom, char *codec)
{
char *filename;
char *outname;
char *name;
int dma;
int i;
/* allocate buffer */
outname = malloc(64);
/* archive filenames */
filename =
"map_i_static\0" /* dungeon room minimaps */
"map_grand_static\0" /* overworld minimaps */
"item_name_static\0" /* pause menu item names */
"spot_name_static\0" /* pause menu location names */
"icon_item_static\0" /* icons + pause menu assets */
"icon_item_24_static\0" /* quest status, dungeon icons */
"unused\0" /* unused/deleted archive */
"icon_bomber_static\0" /* bomber's notebook icons */
;
/* enter and parse folder */
dir_enter("archive");
i = 0;
while (i < 8)
{
name = substring(filename, i);
/* if name is not "unused" */
if (strcmp(name, "unused"))
{
/* create out file name with extension */
sprintf(outname, "%s.yar", name);
/* inject file file */
dma = i + DMA_MAP_I_STATIC;
rom.inject_dma(outname, false, dma);
/* if codec is not yaz, it must be rearchived */
if (strcmp(codec, "yaz"))
{
printf("begin rearchiving %s...\n", name);
rom.dma_rearchive_one(dma, "yaz", codec, true);
}
}
/* advance to next */
i++;
}
/* cleanup */
free(outname);
dir_leave();
}
void
object(struct rom *rom)
{
struct folder *list;
char *name;
int index_last;
int index;
/* objects aligned 0x1000 in retail */
rom.align(0x1000);
/* enter and parse folder */
dir_enter("object");
list = list.new(0);
if (!list.count())
die("object folder is empty");
index_last = -1;
/* repeats for every item in the list */
while (list.remaining())
{
/* initialize variables */
index = list.index();
name = list.name();
/* display progress */
printf("\r""object %d/%d: ", index, list.max());
/* ensure we don't exceed table limit */
if (index >= TBL_OBJECT_ENTRIES)
die("object table limit exceeded");
/* enter directory of list entry */
dir_enter(name);
/* attempt to inject file */
rom.inject("*.zobj", compress);
/* zero out any skipped entries */
skip_test(rom, index, &index_last, TBL_OBJECT, 8);
/* go to table entry */
rom.seek(TBL_OBJECT + index * 8);
/* write its table entry */
/* if rom.inject() failed, file offsets return 0 */
rom.write32(rom.file_start());
rom.write32(rom.file_end());
/* leave directory */
dir_leave();
/* go to next entry in list */
list.next();
}
/* cleanup */
list.free();
dir_leave();
printf("success!\n");
}
void
actor(struct rom *rom)
{
struct folder *list;
struct conf *conf;
char *name;
char *ovl;
char *ss;
int index_last;
int index;
int vram;
int vram_end;
int ivar;
int atype;
/* actors aligned 0x10 in retail */
rom.align(0x10);
/* enter and parse folder */
dir_enter("actor");
list = list.new(0);
if (!list.count())
die("actor folder is empty");
index_last = -1;
/* repeats for every item in the list */
while (list.remaining())
{
/* initialize variables */
index = list.index();
name = list.name();
/* display progress */
printf("\r""actor %d/%d: ", index, list.max());
/* ensure we don't exceed table limit */
if (index >= TBL_ACTOR_ENTRIES)
die("actor table limit exceeded");
/* enter directory of list entry */
dir_enter(name);
/* open conf */
conf = conf.new("conf.txt", "list");
/* attempt to inject file */
ovl = rom.inject("*.zovl", compress);
/* overlay loaded successfully */
if (ovl)
{
/* calculate overlay size */
vram_end = ovl_vram_sz(ovl, rom.file_sz());
/* fall back to DEAD BEEF method if no ivar in conf */
if (!conf.exists("ivar"))
{
/* locate initialization variables */
ss =
find_bytes_stride(
ovl
, rom.file_sz()
, "DEAD****************BEEF"
, 4
, true /* fatal error if more than one occurrence */
);
/* failed to find any */
if (!ss)
die("failed to locate DEAD BEEF");
/* initialization variable offset relative to overlay */
ivar = ss - ovl;
/* overwrite 0xDEAD with index */
put16(ss, index);
/* overwrite 0xBEEF with 0x0000 */
put16(ss + 10, 0);
}
}
/* no overlay found */
else
{
vram_end = 0;
ivar = 0;
}
/* parse conf */
vram = conf.get_int("vram");
atype = conf.get_int("allocation");
if (conf.exists("ivar"))
ivar = conf.get_int("ivar"); // ivar override
else
ivar = vram + ivar; // make relative to vram
conf.free();
/* make overlay size relative to vram, thus vram_end */
vram_end = vram + vram_end;
/* zero out any skipped entries */
skip_test(rom, index, &index_last, TBL_ACTOR, 32);
/* go to table entry */
rom.seek(TBL_ACTOR + index * 32);
/* write its table entry */
/* if rom.inject() failed, file offsets return 0 */
rom.write32(rom.file_start()); /* vrom start */
rom.write32(rom.file_end()); /* vrom end */
rom.write32(vram); /* vram start */
rom.write32(vram_end); /* vram end */
rom.write32(0); /* ram address */
rom.write32(ivar); /* init vars */
rom.write32(0); /* name addr */
rom.write16(atype); /* alloc type */
/* leave directory */
dir_leave();
/* go to next entry in list */
list.next();
}
/* cleanup */
list.free();
dir_leave();
printf("success!\n");
}
void
particle(struct rom *rom)
{
struct folder *list;
struct conf *conf;
char *name;
char *ovl;
char *ss;
int index_last;
int index;
int vram;
int vram_end;
int ivar;
int unk;
/* particles aligned 0x10 in retail */
rom.align(0x10);
/* enter and parse folder */
dir_enter("particle");
list = list.new(0);
if (!list.count())
die("particle folder is empty");
index_last = -1;
/* repeats for every item in the list */
while (list.remaining())
{
/* initialize variables */
index = list.index();
name = list.name();
/* display progress */
printf("\r""particle %d/%d: ", index, list.max());
/* ensure we don't exceed table limit */
if (index >= TBL_PARTICLE_ENTRIES)
die("particle table limit exceeded");
/* enter directory of list entry */
dir_enter(name);
/* attempt to inject file */
ovl = rom.inject("*.zovl", compress);
/* overlay loaded successfully */
if (ovl)
{
/* parse conf */
conf = conf.new("conf.txt", "list");
vram = conf.get_int("vram");
unk = conf.get_int("unknown");
conf.free();
/* calculate vram end */
vram_end = vram + ovl_vram_sz(ovl, rom.file_sz());
/* locate initialization variables (tuna method) */
ss =
find_text_stride(
ovl
, rom.file_sz()
, "tuna"
, 4
, true /* fatal error if more than one occurrence */
);
/* failed to find any */
if (!ss)
die("failed to locate 'tuna'");
/* note initialization variable offset */
ivar = vram + (ss - ovl);
/* overwrite 'tuna' with index */
put32(ss, index);
}
/* no overlay found */
else
{
vram = 0;
vram_end = 0;
unk = 0;
ivar = 0;
}
/* zero out any skipped entries */
skip_test(rom, index, &index_last, TBL_PARTICLE, 28);
/* go to table entry */
rom.seek(TBL_PARTICLE + index * 28);
/* write its table entry */
/* if rom.inject() failed, file offsets return 0 */
rom.write32(rom.file_start()); /* vrom start */
rom.write32(rom.file_end()); /* vrom end */
rom.write32(vram); /* vram start */
rom.write32(vram_end); /* vram end */
rom.write32(0); /* ram address */
rom.write32(ivar); /* init vars */
rom.write32(unk); /* unknown */
/* leave directory */
dir_leave();
/* go to next entry in list */
list.next();
}
/* cleanup */
list.free();
dir_leave();
printf("success!\n");
}
int
main(int argc, char **argv)
{
struct rom *rom;
struct conf *rpl;
char *name;
char *comp;
char *codec;
int i;
/* load .zzrpl */
rpl = rpl.new("*.zzrpl", "list");
name = rpl.get_str("baserom");
/* set compression codec */
codec = "yaz";
/* load base rom */
rom = rom.new(name);
/* compressed roms are < 64 mb */
if (rom.size() < 0x04000000)
die("cannot inject into compressed rom, what have you done");
/* dma table (dmadata) offset and number of entries */
rom.dma(TBL_DMA, TBL_DMA_ENTRIES);
/* mark files for compression */
rom.dma_compress(DMA_ICON_ITEM_FIELD, DMA_UNK_14 , true);
rom.dma_compress(DMA_UNK_23 , DMA_UNK_24 , true);
rom.dma_compress(DMA_CODE , TBL_DMA_ENTRIES, true);
/* compression must be disabled on this file */
rom.dma_compress_one(DMA_EXCEPT, false);
/* mark files (and dma entries) for overwriting */
rom.dma_queue(DMA_ACTOR_FIRST , DMA_ACTOR_LAST ); /* actors, fx */
rom.dma_queue(DMA_OBJECT_FIRST, DMA_OBJECT_LAST); /* objects */
rom.dma_queue(DMA_SCENE_FIRST , DMA_SCENE_LAST ); /* scenes */
rom.dma_queue(DMA_UNUSED_FIRST, DMA_UNUSED_LAST); /* unused */
/* finished initializing dma settings */
rom.dma_ready();
/* construct rom */
archive(rom, codec);
actor(rom);
particle(rom);
object(rom);
scene(rom);
/* write built rom */
rom.save("build.z64");
/* compress rom */
rom.compress(codec, 32);
/* write compressed rom */
rom.save(new_string("build-", codec, ".z64", 0));
/* cleanup */
rom.free();
rpl.free();
return 0;
}