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
|
#include "OoTSceneCommandWriter.h"
#include "AliasManager.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include "utils/Decompressor.h"
#include "factories/DisplayListFactory.h"
#include <sstream>
#include <iomanip>
namespace OoT {
SceneCommand SceneCommandWriter::Write(uint32_t w0, uint32_t w1, SceneWriteContext& ctx) {
uint8_t cmdID = (w0 >> 24) & 0xFF;
uint8_t cmdArg1 = (w0 >> 16) & 0xFF;
uint32_t cmdArg2 = w1;
SceneCommand cmd;
cmd.cmdID = cmdID;
LUS::BinaryWriter cmdWriter;
switch (cmdID) {
case SetWind: {
WriteSetWind(cmdWriter, w0, w1);
break;
}
case SetTimeSettings: {
WriteSetTimeSettings(cmdWriter, w1);
break;
}
case SetSkyboxModifier: {
WriteSetSkyboxModifier(cmdWriter, w1);
break;
}
case SetEchoSettings: {
WriteSetEchoSettings(cmdWriter, w1);
break;
}
case SetSoundSettings: {
WriteSetSoundSettings(cmdWriter, cmdArg1, w1);
break;
}
case SetSkyboxSettings: {
WriteSetSkyboxSettings(cmdWriter, cmdArg1, w1);
break;
}
case SetRoomBehavior: {
WriteSetRoomBehavior(cmdWriter, cmdArg1, cmdArg2);
break;
}
case SetCameraSettings: {
WriteSetCameraSettings(cmdWriter, cmdArg1, cmdArg2);
break;
}
case SetSpecialObjects: {
WriteSetSpecialObjects(cmdWriter, cmdArg1, w1);
break;
}
case SetStartPositionList: {
WriteSetStartPositionList(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetActorList: {
WriteSetActorList(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetTransitionActorList: {
WriteSetTransitionActorList(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetEntranceList: {
WriteSetEntranceList(cmdWriter, cmdArg2, ctx);
break;
}
case SetObjectList: {
WriteSetObjectList(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetLightingSettings: {
WriteSetLightingSettings(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetLightList: {
WriteSetLightList(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetExitList: {
WriteSetExitList(cmdWriter, cmdArg2, ctx);
break;
}
case SetRoomList: {
WriteSetRoomList(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetCollisionHeader: {
WriteSetCollisionHeader(cmdWriter, cmdArg2);
break;
}
case SetMesh: {
WriteSetMesh(cmdWriter, cmdArg2, ctx);
break;
}
case SetCsCamera: {
WriteSetCsCamera(cmdWriter, cmdArg1, cmdArg2, ctx);
break;
}
case SetPathways: {
WriteSetPathways(cmdWriter, cmdArg2, ctx);
break;
}
case SetCutscenes: {
WriteSetCutscenes(cmdWriter, cmdArg2, ctx);
break;
}
case SetAlternateHeaders: {
WriteSetAlternateHeaders(cmdWriter, cmdArg2, ctx);
break;
}
case EndMarker: {
break;
}
default: {
SPDLOG_WARN("Scene: Unhandled command 0x{:02X}", cmdID);
break;
}
}
std::stringstream ss;
cmdWriter.Finish(ss);
std::string str = ss.str();
cmd.data = std::vector<uint8_t>(str.begin(), str.end());
return cmd;
}
void SceneCommandWriter::WriteSetStartPositionList(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 16);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadInt16()); // actorNum
w.Write(sub.ReadInt16()); // posX
w.Write(sub.ReadInt16()); // posY
w.Write(sub.ReadInt16()); // posZ
w.Write(sub.ReadInt16()); // rotX
w.Write(sub.ReadInt16()); // rotY
w.Write(sub.ReadInt16()); // rotZ
w.Write(sub.ReadInt16()); // params
}
}
void SceneCommandWriter::WriteSetActorList(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 16);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadInt16()); // actorNum
w.Write(sub.ReadInt16()); // posX
w.Write(sub.ReadInt16()); // posY
w.Write(sub.ReadInt16()); // posZ
w.Write(sub.ReadInt16()); // rotX
w.Write(sub.ReadInt16()); // rotY
w.Write(sub.ReadInt16()); // rotZ
w.Write(sub.ReadInt16()); // params
}
// Create 0-byte ActorEntry companion file (matches OTRExporter behavior).
if (cmdArg2 != 0 && count > 0) {
uint32_t actorOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(cmdArg2));
std::string actorSymbol = MakeAssetName(ctx.baseName, "ActorEntry", actorOffset);
Companion::Instance->RegisterCompanionFile(actorSymbol, std::vector<char>{});
}
}
void SceneCommandWriter::WriteSetWind(LUS::BinaryWriter& w, uint32_t w0, uint32_t w1) {
w.Write(static_cast<uint8_t>((w1 >> 24) & 0xFF)); // windWest
w.Write(static_cast<uint8_t>((w1 >> 16) & 0xFF)); // windVertical
w.Write(static_cast<uint8_t>((w1 >> 8) & 0xFF)); // windSouth
w.Write(static_cast<uint8_t>(w1 & 0xFF)); // clothFlappingStrength
}
void SceneCommandWriter::WriteSetTimeSettings(LUS::BinaryWriter& w, uint32_t w1) {
w.Write(static_cast<uint8_t>((w1 >> 24) & 0xFF)); // hour
w.Write(static_cast<uint8_t>((w1 >> 16) & 0xFF)); // min
w.Write(static_cast<uint8_t>((w1 >> 8) & 0xFF)); // unk
}
void SceneCommandWriter::WriteSetSkyboxModifier(LUS::BinaryWriter& w, uint32_t w1) {
w.Write(static_cast<uint8_t>((w1 >> 24) & 0xFF)); // disableSky
w.Write(static_cast<uint8_t>((w1 >> 16) & 0xFF)); // disableSunMoon
}
void SceneCommandWriter::WriteSetEchoSettings(LUS::BinaryWriter& w, uint32_t w1) {
w.Write(static_cast<uint8_t>(w1 & 0xFF)); // echo
}
void SceneCommandWriter::WriteSetSoundSettings(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t w1) {
w.Write(static_cast<uint8_t>(cmdArg1)); // reverb
w.Write(static_cast<uint8_t>((w1 >> 8) & 0xFF)); // nightTimeSFX
w.Write(static_cast<uint8_t>(w1 & 0xFF)); // musicSequence
}
void SceneCommandWriter::WriteSetSkyboxSettings(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t w1) {
w.Write(static_cast<uint8_t>(cmdArg1)); // unk1
w.Write(static_cast<uint8_t>((w1 >> 24) & 0xFF)); // skyboxNumber
w.Write(static_cast<uint8_t>((w1 >> 16) & 0xFF)); // cloudsType
w.Write(static_cast<uint8_t>((w1 >> 8) & 0xFF)); // isIndoors
}
void SceneCommandWriter::WriteSetRoomBehavior(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2) {
w.Write(static_cast<uint8_t>(cmdArg1)); // gameplayFlags
w.Write(cmdArg2); // gameplayFlags2
}
void SceneCommandWriter::WriteSetCameraSettings(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2) {
w.Write(static_cast<uint8_t>(cmdArg1)); // cameraMovement
w.Write(cmdArg2); // mapHighlight
}
void SceneCommandWriter::WriteSetSpecialObjects(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t w1) {
w.Write(static_cast<uint8_t>(cmdArg1)); // elfMessage
w.Write(static_cast<uint16_t>(w1 & 0xFFFF)); // globalObject
}
void SceneCommandWriter::WriteSetTransitionActorList(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 16);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadInt8()); // frontObjectRoom
w.Write(sub.ReadInt8()); // frontTransitionReaction
w.Write(sub.ReadInt8()); // backObjectRoom
w.Write(sub.ReadInt8()); // backTransitionReaction
w.Write(sub.ReadInt16()); // actorNum
w.Write(sub.ReadInt16()); // posX
w.Write(sub.ReadInt16()); // posY
w.Write(sub.ReadInt16()); // posZ
w.Write(sub.ReadInt16()); // rotY
w.Write(sub.ReadInt16()); // initVar
}
}
void SceneCommandWriter::WriteSetEntranceList(LUS::BinaryWriter& w, uint32_t cmdArg2, SceneWriteContext& ctx) {
uint32_t count = GetNeighborSize(ctx.knownAddrs, cmdArg2, 2);
if (count == 0) count = 1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 2);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadUByte()); // startPositionIndex
w.Write(sub.ReadUByte()); // roomToLoad
}
}
void SceneCommandWriter::WriteSetObjectList(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 2);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadUInt16());
}
}
void SceneCommandWriter::WriteSetLightingSettings(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 22);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
for (int j = 0; j < 18; j++) {
w.Write(sub.ReadUByte());
}
w.Write(sub.ReadUInt16()); // unk
w.Write(sub.ReadUInt16()); // drawDistance
}
}
void SceneCommandWriter::WriteSetLightList(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 14);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadUByte()); // type
w.Write(sub.ReadInt16()); // x
w.Write(sub.ReadInt16()); // y
w.Write(sub.ReadInt16()); // z
w.Write(sub.ReadUByte()); // r
w.Write(sub.ReadUByte()); // g
w.Write(sub.ReadUByte()); // b
w.Write(sub.ReadUByte()); // drawGlow
w.Write(sub.ReadInt16()); // radius
}
}
void SceneCommandWriter::WriteSetExitList(LUS::BinaryWriter& w, uint32_t cmdArg2, SceneWriteContext& ctx) {
uint32_t count = GetNeighborSize(ctx.knownAddrs, cmdArg2, 2);
if (count == 0) count = 1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 2);
w.Write(static_cast<uint32_t>(count));
for (uint32_t i = 0; i < count; i++) {
w.Write(sub.ReadUInt16());
}
}
void SceneCommandWriter::WriteSetRoomList(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
uint32_t count = cmdArg1;
auto sub = ReadSubArray(ctx.buffer, cmdArg2, count * 8);
w.Write(static_cast<uint32_t>(count));
// Derive scene base name from current directory (e.g. "scenes/nonmq/bdan_scene" → "bdan")
std::string sceneBase;
auto lastSlash = ctx.currentDir.rfind('/');
sceneBase = (lastSlash != std::string::npos) ? ctx.currentDir.substr(lastSlash + 1) : ctx.currentDir;
std::string roomBase = sceneBase;
if (roomBase.size() > 6 && roomBase.substr(roomBase.size() - 6) == "_scene") {
roomBase = roomBase.substr(0, roomBase.size() - 6);
}
for (uint32_t i = 0; i < count; i++) {
std::string roomName = ctx.currentDir + "/" + roomBase + "_room_" + std::to_string(i);
w.Write(roomName);
w.Write(sub.ReadUInt32()); // virtualAddressStart
w.Write(sub.ReadUInt32()); // virtualAddressEnd
}
}
void SceneCommandWriter::WriteSetCollisionHeader(LUS::BinaryWriter& w, uint32_t cmdArg2) {
uint32_t colAddr = Companion::Instance->PatchVirtualAddr(cmdArg2);
auto resolved = ResolvePointer(cmdArg2);
if (resolved.empty()) {
SPDLOG_WARN("Undeclared collision at 0x{:08X} — YAML enrichment incomplete", colAddr);
}
w.Write(resolved);
}
void SceneCommandWriter::WriteSetMesh(LUS::BinaryWriter& w, uint32_t cmdArg2, SceneWriteContext& ctx) {
uint8_t meshData = 0;
auto meshReader = ReadSubArray(ctx.buffer, cmdArg2, 12);
uint8_t meshHeaderType = meshReader.ReadUByte();
w.Write(meshData);
w.Write(meshHeaderType);
DeferredVtx::BeginDefer();
if (meshHeaderType == 0 || meshHeaderType == 2) {
uint32_t num = meshReader.ReadUByte();
meshReader.ReadUInt16(); // padding
uint32_t polyStart = meshReader.ReadUInt32();
w.Write(static_cast<uint8_t>(num));
uint32_t entrySize = (meshHeaderType == 2) ? 16 : 8;
auto polyReader = ReadSubArray(ctx.buffer, polyStart, num * entrySize);
for (uint32_t i = 0; i < num; i++) {
uint8_t polyType = 0;
int16_t cx = 0, cy = 0, cz = 0;
int16_t unk_06 = 0;
if (meshHeaderType == 2) {
polyType = 2;
cx = polyReader.ReadInt16();
cy = polyReader.ReadInt16();
cz = polyReader.ReadInt16();
unk_06 = polyReader.ReadInt16();
}
uint32_t opaAddr = polyReader.ReadUInt32();
uint32_t xluAddr = polyReader.ReadUInt32();
w.Write(polyType);
if (polyType == 2) {
w.Write(cx);
w.Write(cy);
w.Write(cz);
w.Write(unk_06);
}
std::string opaPath;
if (opaAddr != 0) {
uint32_t opaOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(opaAddr));
std::string opaSymbol = MakeAssetName(ctx.entryName, "DL", opaOffset);
opaPath = ResolveGfxWithAlias(opaAddr, opaSymbol, ctx.currentDir);
}
w.Write(opaPath.empty() ? std::string("") : opaPath);
std::string xluPath;
if (xluAddr != 0) {
uint32_t xluOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(xluAddr));
std::string xluSymbol = MakeAssetName(ctx.entryName, "DL", xluOffset);
xluPath = ResolveGfxWithAlias(xluAddr, xluSymbol, ctx.currentDir);
}
w.Write(xluPath.empty() ? std::string("") : xluPath);
}
} else if (meshHeaderType == 1) {
uint8_t format = meshReader.ReadUByte();
meshReader.ReadUInt16(); // padding
uint32_t polyDListAddr = meshReader.ReadUInt32();
w.Write(format);
auto pdlReader = ReadSubArray(ctx.buffer, polyDListAddr, 8);
uint32_t opaAddr = pdlReader.ReadUInt32();
uint32_t xluAddr = pdlReader.ReadUInt32();
std::string opaPath, xluPath;
if (opaAddr != 0) {
uint32_t opaOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(opaAddr));
std::string opaSymbol = MakeAssetName(ctx.entryName, "DL", opaOffset);
opaPath = ResolveGfxWithAlias(opaAddr, opaSymbol, ctx.currentDir);
}
if (xluAddr != 0) {
uint32_t xluOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(xluAddr));
std::string xluSymbol = MakeAssetName(ctx.entryName, "DL", xluOffset);
xluPath = ResolveGfxWithAlias(xluAddr, xluSymbol, ctx.currentDir);
}
w.Write(opaPath.empty() ? std::string("") : opaPath);
w.Write(xluPath.empty() ? std::string("") : xluPath);
if (format == 2) {
auto mhReader = ReadSubArray(ctx.buffer, cmdArg2 + 0x08, 8);
uint32_t count = mhReader.ReadUByte();
mhReader.ReadUByte(); mhReader.ReadUByte(); mhReader.ReadUByte();
uint32_t multiAddr = mhReader.ReadUInt32();
w.Write(static_cast<uint32_t>(count));
auto bgReader = ReadSubArray(ctx.buffer, multiAddr, count * 28);
for (uint32_t i = 0; i < count; i++) {
uint16_t unk_00 = bgReader.ReadUInt16();
uint8_t id = bgReader.ReadUByte();
bgReader.ReadUByte();
uint32_t source = bgReader.ReadUInt32();
uint32_t unk_0C = bgReader.ReadUInt32();
uint32_t tlut = bgReader.ReadUInt32();
uint16_t width = bgReader.ReadUInt16();
uint16_t height = bgReader.ReadUInt16();
uint8_t fmt = bgReader.ReadUByte();
uint8_t siz = bgReader.ReadUByte();
uint16_t mode0 = bgReader.ReadUInt16();
uint16_t tlutCount = bgReader.ReadUInt16();
bgReader.ReadUInt16();
w.Write(unk_00);
w.Write(id);
uint32_t bgOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(source));
std::string bgSymbol = MakeAssetName(ctx.baseName, "Background", bgOffset);
std::string bgPath = ResolvePointer(source);
if (bgPath.empty()) {
bgPath = ctx.currentDir + "/" + bgSymbol;
}
w.Write(bgPath);
w.Write(unk_0C); w.Write(tlut);
w.Write(width); w.Write(height);
w.Write(fmt); w.Write(siz);
w.Write(mode0); w.Write(tlutCount);
CreateBackgroundCompanion(ctx.buffer, source, bgSymbol);
}
} else {
w.Write(static_cast<uint32_t>(1));
w.Write(static_cast<uint16_t>(0)); // unk_00
w.Write(static_cast<uint8_t>(0)); // id
auto bgReader = ReadSubArray(ctx.buffer, cmdArg2 + 0x08, 24);
uint32_t source = bgReader.ReadUInt32();
uint32_t unk_0C = bgReader.ReadUInt32();
uint32_t tlut = bgReader.ReadUInt32();
uint16_t width = bgReader.ReadUInt16();
uint16_t height = bgReader.ReadUInt16();
uint8_t fmt = bgReader.ReadUByte();
uint8_t siz = bgReader.ReadUByte();
uint16_t mode0 = bgReader.ReadUInt16();
uint16_t tlutCount = bgReader.ReadUInt16();
uint32_t bgOffset = SEGMENT_OFFSET(Companion::Instance->PatchVirtualAddr(source));
std::string bgSymbol = MakeAssetName(ctx.baseName, "Background", bgOffset);
std::string bgPath = ResolvePointer(source);
if (bgPath.empty()) {
bgPath = ctx.currentDir + "/" + bgSymbol;
}
w.Write(bgPath);
w.Write(unk_0C); w.Write(tlut);
w.Write(width); w.Write(height);
w.Write(fmt); w.Write(siz);
w.Write(mode0); w.Write(tlutCount);
CreateBackgroundCompanion(ctx.buffer, source, bgSymbol);
}
if (polyDListAddr != 0) {
w.Write(static_cast<uint8_t>(meshHeaderType));
w.Write(opaPath.empty() ? std::string("") : opaPath);
w.Write(xluPath.empty() ? std::string("") : xluPath);
}
}
}
void SceneCommandWriter::WriteSetCsCamera(LUS::BinaryWriter& w, uint8_t cmdArg1, uint32_t cmdArg2,
SceneWriteContext& ctx) {
auto camReader = ReadSubArray(ctx.buffer, cmdArg2, 0x1000);
uint32_t numCameras = cmdArg1;
w.Write(numCameras);
uint32_t firstPointsAddr = 0;
struct CamEntry { int16_t type; int16_t numPoints; uint32_t segOff; };
std::vector<CamEntry> cameras;
for (uint32_t i = 0; i < numCameras; i++) {
CamEntry c;
c.type = camReader.ReadInt16();
c.numPoints = camReader.ReadInt16();
c.segOff = camReader.ReadUInt32();
if (i == 0) firstPointsAddr = c.segOff;
cameras.push_back(c);
}
for (auto& c : cameras) {
w.Write(c.type);
w.Write(c.numPoints);
auto pointReader = ReadSubArray(ctx.buffer, c.segOff, c.numPoints * 6);
for (int16_t j = 0; j < c.numPoints; j++) {
w.Write(pointReader.ReadInt16());
w.Write(pointReader.ReadInt16());
w.Write(pointReader.ReadInt16());
}
}
}
void SceneCommandWriter::WriteSetPathways(LUS::BinaryWriter& w, uint32_t cmdArg2, SceneWriteContext& ctx) {
uint8_t pathSeg = (cmdArg2 >> 24) & 0xFF;
uint32_t maxPaths = GetNeighborSize(ctx.knownAddrs, cmdArg2, 8);
if (maxPaths == 0) maxPaths = 256;
auto pathReader = ReadSubArray(ctx.buffer, cmdArg2, maxPaths * 8);
std::vector<std::pair<uint8_t, uint32_t>> pathways;
for (uint32_t i = 0; i < maxPaths; i++) {
uint8_t np = pathReader.ReadUByte();
pathReader.ReadUByte(); pathReader.ReadUByte(); pathReader.ReadUByte();
uint32_t ptsAddr = pathReader.ReadUInt32();
if (ptsAddr == 0 || !IS_SEGMENTED(ptsAddr) || ((ptsAddr >> 24) & 0xFF) != pathSeg) {
break;
}
pathways.push_back({np, ptsAddr});
}
if (pathways.empty()) pathways.push_back({0, 0});
auto existingPath = ResolvePointer(cmdArg2);
bool hasPreExistingResource = !existingPath.empty();
if (!hasPreExistingResource && ctx.isAltHeader && pathways.size() > 1) {
pathways.erase(pathways.begin() + 1, pathways.end());
}
bool doubled = hasPreExistingResource && (pathways.size() > 1);
uint32_t writeCount = doubled ? pathways.size() * 2 : pathways.size();
w.Write(static_cast<uint32_t>(writeCount));
uint32_t repeats = doubled ? 2 : 1;
for (uint32_t r = 0; r < repeats; r++) {
for (uint32_t i = 0; i < pathways.size(); i++) {
auto [np, ptsAddr] = pathways[i];
uint32_t pointOffset = SEGMENT_OFFSET(ptsAddr);
std::string pathSymbol = MakeAssetName(ctx.baseName, "PathwayList", pointOffset);
std::string pathPath = ctx.currentDir + "/" + pathSymbol;
w.Write(pathPath);
}
}
for (uint32_t i = 0; i < pathways.size(); i++) {
auto [np, ptsAddr] = pathways[i];
uint32_t pointOffset = SEGMENT_OFFSET(ptsAddr);
std::string pathSymbol = MakeAssetName(ctx.baseName, "PathwayList", pointOffset);
auto pathData = SerializePathways(ctx.buffer, pathways, writeCount, repeats);
Companion::Instance->RegisterCompanionFile(pathSymbol, pathData);
}
}
void SceneCommandWriter::WriteSetCutscenes(LUS::BinaryWriter& w, uint32_t cmdArg2, SceneWriteContext& ctx) {
std::string csSymbol;
uint32_t csAddr = Companion::Instance->PatchVirtualAddr(cmdArg2);
auto resolved = ResolvePointer(cmdArg2);
if (resolved.empty()) {
uint32_t csOffset = SEGMENT_OFFSET(csAddr);
csSymbol = MakeAssetName(ctx.entryName, "CutsceneData", csOffset);
resolved = ctx.currentDir + "/" + csSymbol;
} else {
csSymbol = resolved.substr(resolved.rfind('/') + 1);
}
w.Write(resolved);
auto csData = CutsceneSerializer::Serialize(ctx.buffer, cmdArg2);
if (csData.empty()) {
SPDLOG_WARN("Scene: Skipping cutscene {} due to parse failure", csSymbol);
}
Companion::Instance->RegisterCompanionFile(csSymbol, csData);
}
void SceneCommandWriter::WriteSetAlternateHeaders(LUS::BinaryWriter& w, uint32_t cmdArg2, SceneWriteContext& ctx) {
// An uninitialized alt-header pointer with a bogus segment resolves out of
// bounds. OTRExporter emits no alternate headers in that case; match it
// rather than reading from an invalid address.
auto altSegBase = Companion::Instance->GetFileOffsetFromSegmentedAddr(SEGMENT_NUMBER(cmdArg2));
uint32_t altFileOffset = altSegBase.has_value()
? altSegBase.value() + SEGMENT_OFFSET(cmdArg2)
: cmdArg2;
if (altFileOffset >= ctx.buffer.size()) {
SPDLOG_WARN("Scene: skipping alternate headers for {}, unresolvable pointer 0x{:X}",
ctx.entryName, cmdArg2);
w.Write(static_cast<uint32_t>(0));
return;
}
uint32_t maxHeaders = GetNeighborSize(ctx.knownAddrs, cmdArg2, 4);
if (maxHeaders == 0) maxHeaders = 16;
auto hdrReader = ReadSubArray(ctx.buffer, cmdArg2, maxHeaders * 4);
std::vector<uint32_t> headers;
for (uint32_t i = 0; i < maxHeaders; i++) {
uint32_t seg = hdrReader.ReadUInt32();
headers.push_back(seg);
}
w.Write(static_cast<uint32_t>(headers.size()));
for (auto seg : headers) {
if (seg == 0) {
w.Write(std::string(""));
} else {
uint32_t offset = SEGMENT_OFFSET(seg);
std::string setSymbol = MakeAssetName(ctx.entryName, "Set", offset);
std::string setPath = ctx.currentDir + "/" + setSymbol;
w.Write(setPath);
ctx.pendingAltHeaders.push_back({seg, setSymbol});
}
}
}
void SceneCommandWriter::CreateBackgroundCompanion(std::vector<uint8_t>& buffer, uint32_t source,
const std::string& bgSymbol) {
if (source == 0) return;
uint32_t bgDataSize = 320 * 240 * 2;
auto bgDataReader = ReadSubArray(buffer, source, bgDataSize);
LUS::BinaryWriter bgWriter;
BaseExporter::WriteHeader(bgWriter, Torch::ResourceType::OoTBackground, 0);
bgWriter.Write(static_cast<uint32_t>(bgDataSize));
for (uint32_t b = 0; b < bgDataSize; b++) {
bgWriter.Write(bgDataReader.ReadUByte());
}
std::stringstream bgSS;
bgWriter.Finish(bgSS);
std::string bgStr = bgSS.str();
Companion::Instance->RegisterCompanionFile(
bgSymbol, std::vector<char>(bgStr.begin(), bgStr.end()));
}
std::string SceneCommandWriter::ResolveGfxPointer(uint32_t ptr, const std::string& symbol) {
if (ptr == 0) return "";
ptr = Companion::Instance->PatchVirtualAddr(ptr);
auto result = Companion::Instance->GetStringByAddr(ptr);
if (result.has_value()) return result.value();
SPDLOG_WARN("Scene: Could not resolve GFX pointer 0x{:08X} ({}) — YAML enrichment incomplete", ptr, symbol);
return "";
}
std::string SceneCommandWriter::ResolveGfxWithAlias(uint32_t ptr, const std::string& symbol,
const std::string& currentDir) {
std::string path = ResolveGfxPointer(ptr, symbol);
if (path.empty()) return "";
std::string expectedPath = currentDir + "/" + symbol;
if (path != expectedPath) {
AliasManager::Instance->Register(path, expectedPath);
}
return path;
}
uint32_t SceneCommandWriter::GetNeighborSize(const std::set<uint32_t>& knownAddrs, uint32_t segAddr,
uint32_t entrySize) {
uint32_t addr = SEGMENT_OFFSET(segAddr);
auto it = knownAddrs.upper_bound(addr);
if (it != knownAddrs.end()) {
return (*it - addr) / entrySize;
}
return 0;
}
} // namespace OoT
|