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
|
#include "OoTCutsceneFactory.h"
#include "OoTSceneUtils.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include <set>
namespace OoT {
// Camera commands use variable-length entries terminated by a 0xFF marker byte.
bool CutsceneSerializer::IsCameraCmd(uint32_t id) {
return id == 1 || id == 2 || id == 5 || id == 6;
}
// These commands use 0x0C-byte entries instead of the standard 0x30.
bool CutsceneSerializer::IsSmallEntryCmd(uint32_t id) {
return id == 0x09 || id == 0x13 || id == 0x8C;
}
// Single-entry commands (transition: 0x2D, destination: 0x3E8).
bool CutsceneSerializer::IsSingleEntryCmd(uint32_t id) {
return id == 0x2D || id == 0x3E8;
}
// Commands that are in the valid range (0x0E–0x90) but not implemented.
const std::set<uint32_t> CutsceneSerializer::sUnimplementedCmds = {
0x0B, 0x0C, 0x0D, 0x14, 0x15, 0x16, 0x1A, 0x1B, 0x1C, 0x20, 0x21, 0x38, 0x3B, 0x3D,
0x47, 0x49, 0x5B, 0x5C, 0x5F, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
0x6D, 0x70, 0x71, 0x7A
};
bool CutsceneSerializer::IsHandledCmd(uint32_t cid) {
// Basic commands (camera, misc, rumble)
if (cid >= 0x01 && cid <= 0x0A) return true;
// Transition and destination
if (IsSingleEntryCmd(cid)) return true;
// Text, time, and non-actor-cue commands
if (cid == 0x13 || cid == 0x56 || cid == 0x57 || cid == 0x7C || cid == 0x8C) return true;
// Actor cue commands (0x0E–0x90 range, excluding known unimplemented ones)
if (cid >= 0x0E && cid <= 0x90 && !sUnimplementedCmds.count(cid)) return true;
return false;
}
// Cutscene serialization — shared with OoTSceneFactory's SetCutscenes handler.
// Walk through cutscene commands to determine total byte size.
// Returns 0 if the data is corrupt or too small.
uint32_t CutsceneSerializer::CalculateSize(std::vector<uint8_t>& buffer, uint32_t segAddr) {
auto reader = ReadSubArray(buffer, segAddr, 0x10000);
uint32_t endOffset = reader.GetLength();
// Need at least numCommands + endFrame
if (endOffset < 8) return 0;
uint32_t numCommands = reader.ReadUInt32();
reader.ReadUInt32(); // endFrame
for (uint32_t cmd = 0; cmd < numCommands; cmd++) {
// Each command is at least 8 bytes (id + entryCount)
if (reader.GetBaseAddress() + 8 > endOffset) return 0;
uint32_t cmdId = reader.ReadUInt32();
if (cmdId == 0xFFFFFFFF) break; // end marker
uint32_t entryCount = reader.ReadUInt32();
if (IsCameraCmd(cmdId)) {
// Camera commands have an extra uint32 before the entry list
if (reader.GetBaseAddress() + 4 > endOffset) return 0;
reader.ReadUInt32();
for (uint32_t ci = 0; ci < 1000; ci++) {
// Each camera entry is 0x10 bytes
if (reader.GetBaseAddress() + 0x10 > endOffset) return 0;
uint8_t marker = reader.ReadUByte();
reader.Seek(reader.GetBaseAddress() + 0x0F, LUS::SeekOffsetType::Start);
if (marker == 0xFF) break; // end of camera entries
}
continue;
}
if (IsSingleEntryCmd(cmdId)) {
// Single entry is 0x08 bytes
if (reader.GetBaseAddress() + 0x08 > endOffset) return 0;
reader.Seek(reader.GetBaseAddress() + 0x08, LUS::SeekOffsetType::Start);
continue;
}
// Default: fixed-size entries (0x0C for rumble/text/time, 0x30 for everything else)
uint32_t entrySize = IsSmallEntryCmd(cmdId) ? 0x0C : 0x30;
uint32_t dataSize = entryCount * entrySize;
// Ensure all entries fit within the cutscene data
if (reader.GetBaseAddress() + dataSize > endOffset) return 0;
reader.Seek(reader.GetBaseAddress() + dataSize, LUS::SeekOffsetType::Start);
}
// Read the end marker (0xFFFFFFFF + 0x00000000) to get final size
if (reader.GetBaseAddress() + 8 > endOffset) return 0;
reader.ReadUInt32();
reader.ReadUInt32();
return reader.GetBaseAddress();
}
void CutsceneSerializer::WriteCameraCmd(LUS::BinaryReader& reader, LUS::BinaryWriter& w) {
uint16_t startFrame = reader.ReadUInt16();
uint16_t endFrame = reader.ReadUInt16();
w.Write(CS_CMD_HH(startFrame, endFrame));
uint16_t unk1 = reader.ReadUInt16();
uint16_t unk2 = reader.ReadUInt16();
w.Write(CS_CMD_HH(unk1, unk2));
while (true) {
int8_t marker = reader.ReadInt8();
int8_t interpType = reader.ReadInt8();
int16_t numPointsForFrame = reader.ReadInt16();
float viewAngle = reader.ReadFloat();
int16_t posX = reader.ReadInt16();
int16_t posY = reader.ReadInt16();
int16_t posZ = reader.ReadInt16();
int16_t unused = reader.ReadInt16();
w.Write(CS_CMD_BBH(marker, interpType, numPointsForFrame));
w.Write(viewAngle);
w.Write(CS_CMD_HH(posX, posY));
w.Write(CS_CMD_HH(posZ, unused));
if ((uint8_t)marker == 0xFF) break;
}
}
void CutsceneSerializer::WriteSingleEntryCmd(LUS::BinaryReader& reader, LUS::BinaryWriter& w) {
reader.ReadUInt32(); // skip original count
w.Write(static_cast<uint32_t>(1));
uint16_t startFrame = reader.ReadUInt16();
uint16_t endFrame = reader.ReadUInt16();
uint16_t type = reader.ReadUInt16();
reader.ReadUInt16(); // padding
w.Write(CS_CMD_HH(startFrame, endFrame));
w.Write(CS_CMD_HH(type, type));
}
// Helper: check if a command uses actor cue format (0x30-byte entries with rotation fields)
static bool IsActorCueCmd(uint32_t cid) {
return cid != 0x03 && cid != 0x04 && cid != 0x56 && cid != 0x57 && cid != 0x7C;
}
void CutsceneSerializer::WriteEntryCountCmd(uint32_t cid, LUS::BinaryReader& reader, LUS::BinaryWriter& w) {
uint32_t entryCount = reader.ReadUInt32();
w.Write(entryCount);
for (uint32_t i = 0; i < entryCount; i++) {
// All entry types share a common header
uint16_t base = reader.ReadUInt16();
uint16_t startFrame = reader.ReadUInt16();
uint16_t endFrame = reader.ReadUInt16();
w.Write(CS_CMD_HH(base, startFrame));
if (cid == 0x09) {
// Rumble (0x0C bytes)
uint8_t sourceStrength = reader.ReadUByte();
uint8_t duration = reader.ReadUByte();
uint8_t decreaseRate = reader.ReadUByte();
uint8_t unk = reader.ReadUByte();
uint16_t unkA = reader.ReadUInt16();
w.Write(CS_CMD_HBB(endFrame, sourceStrength, duration));
w.Write(CS_CMD_BBH(decreaseRate, unk, unkA));
continue;
}
if (cid == 0x13) {
// Text (0x0C bytes)
uint16_t type = reader.ReadUInt16();
uint16_t textId1 = reader.ReadUInt16();
uint16_t textId2 = reader.ReadUInt16();
w.Write(CS_CMD_HH(endFrame, type));
w.Write(CS_CMD_HH(textId1, textId2));
continue;
}
if (cid == 0x8C) {
// Time (0x0C bytes)
uint8_t hour = reader.ReadUByte();
uint8_t minute = reader.ReadUByte();
reader.ReadUInt32(); // padding
w.Write(CS_CMD_HBB(endFrame, hour, minute));
w.Write(static_cast<uint32_t>(0));
continue;
}
// Actor cue (0x30 bytes)
if (IsActorCueCmd(cid)) {
w.Write(CS_CMD_HH(endFrame, reader.ReadUInt16()));
uint16_t rotY = reader.ReadUInt16();
uint16_t rotZ = reader.ReadUInt16();
w.Write(CS_CMD_HH(rotY, rotZ));
for (int j = 0; j < 9; j++) w.Write(reader.ReadUInt32());
continue;
}
// Non-actor-cue (0x30 bytes, commands 0x03, 0x04, 0x56, 0x57, 0x7C)
w.Write(CS_CMD_HH(endFrame, reader.ReadUInt16()));
for (int j = 0; j < 10; j++) w.Write(reader.ReadUInt32());
}
}
std::vector<char> CutsceneSerializer::Serialize(std::vector<uint8_t>& buffer, uint32_t segAddr) {
auto size = CalculateSize(buffer, segAddr);
// Size of 0 means corrupt or empty cutscene data
if (size == 0) return {};
return Write(buffer, segAddr, size);
}
std::vector<char> CutsceneSerializer::Write(std::vector<uint8_t>& buffer, uint32_t segAddr, uint32_t size) {
auto csReader = ReadSubArray(buffer, segAddr, size);
// Use the actual bytes available (ReadSubArray may return less than requested)
size = csReader.GetLength();
LUS::BinaryWriter w;
BaseExporter::WriteHeader(w, Torch::ResourceType::OoTCutscene, 0);
// Write a placeholder for the cutscene word count.
// We don't know the final count until all commands are serialized,
// so we'll seek back and fill this in at the end.
uint32_t wordCountPos = w.GetStream()->GetLength();
w.Write(static_cast<uint32_t>(0));
// Used to calculate word count at the end
uint32_t dataStartPos = w.GetStream()->GetLength();
uint32_t numCmds = csReader.ReadUInt32();
uint32_t endFrame = csReader.ReadUInt32();
w.Write(numCmds);
w.Write(endFrame);
for (uint32_t ci = 0; ci < numCmds && csReader.GetBaseAddress() + 8 <= size; ci++) {
uint32_t cid = csReader.ReadUInt32();
// End marker
if (cid == 0xFFFFFFFF) break;
// Skip unhandled commands (entryCount * 0x30 bytes)
if (!IsHandledCmd(cid)) {
uint32_t entryCount = csReader.ReadUInt32();
uint32_t dataSize = entryCount * 0x30;
if (csReader.GetBaseAddress() + dataSize <= size)
csReader.Seek(csReader.GetBaseAddress() + dataSize, LUS::SeekOffsetType::Start);
continue;
}
// Write command ID, then serialize its data
w.Write(cid);
if (IsCameraCmd(cid)) {
WriteCameraCmd(csReader, w);
continue;
}
if (IsSingleEntryCmd(cid)) {
WriteSingleEntryCmd(csReader, w);
continue;
}
// Default: entry-count-based commands
WriteEntryCountCmd(cid, csReader, w);
}
// Write end marker
w.Write(static_cast<uint32_t>(0xFFFFFFFF));
w.Write(static_cast<uint32_t>(0));
// Fill in the reserved word count now that we know the final size
uint32_t dataEndPos = w.GetStream()->GetLength();
w.Seek(wordCountPos, LUS::SeekOffsetType::Start);
w.Write(static_cast<uint32_t>((dataEndPos - dataStartPos) / 4));
w.Seek(dataEndPos, LUS::SeekOffsetType::Start);
std::stringstream ss;
w.Finish(ss);
std::string str = ss.str();
return std::vector<char>(str.begin(), str.end());
}
class OoTCutsceneData : public IParsedData {
public:
std::vector<char> mBinary;
OoTCutsceneData(std::vector<char> data) : mBinary(std::move(data)) {}
};
std::optional<std::shared_ptr<IParsedData>> OoTCutsceneFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
auto data = CutsceneSerializer::Serialize(buffer, GetSafeNode<uint32_t>(node, "offset"));
if (data.empty()) {
SPDLOG_WARN("OoTCutsceneFactory: Failed to serialize cutscene");
return std::nullopt;
}
return std::make_shared<OoTCutsceneData>(std::move(data));
}
ExportResult OoTCutsceneBinaryExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
std::string& entryName, YAML::Node& node, std::string* replacement) {
auto cs = std::static_pointer_cast<OoTCutsceneData>(raw);
write.write(cs->mBinary.data(), cs->mBinary.size());
return std::nullopt;
}
} // namespace OoT
|