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
|
#include "PaintingFactory.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include "utils/Decompressor.h"
#include "utils/TorchUtils.h"
#include <regex>
ExportResult SM64::PaintingHeaderExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
std::string& entryName, YAML::Node& node, std::string* replacement) {
const auto symbol = GetSafeNode(node, "symbol", entryName);
if (Companion::Instance->IsOTRMode()) {
write << "static const char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
return std::nullopt;
}
write << "extern struct Painting " << symbol << "[];\n";
return std::nullopt;
}
ExportResult SM64::PaintingCodeExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
std::string& entryName, YAML::Node& node, std::string* replacement) {
const auto symbol = GetSafeNode(node, "symbol", entryName);
const auto offset = GetSafeNode<uint32_t>(node, "offset");
auto painting = std::static_pointer_cast<SM64::Painting>(raw);
write << "struct Painting " << symbol << " = {\n";
std::stringstream textureType;
if (painting->textureType == PAINTING_IMAGE) {
textureType << "PAINTING_IMAGE";
} else if (painting->textureType == PAINTING_ENV_MAP) {
textureType << "PAINTING_ENV_MAP";
} else {
textureType << painting->textureType; // should not reach this case, throw error?
}
std::stringstream nDLSymbol;
{
auto dec = Companion::Instance->GetNodeByAddr(painting->normalDisplayList);
if (dec.has_value()) {
auto nDLNode = std::get<1>(dec.value());
nDLSymbol << GetSafeNode<std::string>(nDLNode, "symbol");
} else {
SPDLOG_WARN("Cannot find node for ptr 0x{:X}", painting->normalDisplayList);
nDLSymbol << std::hex << "0x" << painting->normalDisplayList << std::dec;
}
}
std::stringstream tMapSymbol;
{
auto dec = Companion::Instance->GetNodeByAddr(painting->textureMaps);
if (dec.has_value()) {
auto tMapNode = std::get<1>(dec.value());
tMapSymbol << GetSafeNode<std::string>(tMapNode, "symbol");
} else {
SPDLOG_WARN("Cannot find node for ptr 0x{:X}", painting->textureMaps);
tMapSymbol << std::hex << "0x" << painting->textureMaps << std::dec;
}
}
std::stringstream tArrSymbol;
{
auto dec = Companion::Instance->GetNodeByAddr(painting->textureArray);
if (dec.has_value()) {
auto tMapNode = std::get<1>(dec.value());
tArrSymbol << GetSafeNode<std::string>(tMapNode, "symbol");
} else {
SPDLOG_WARN("Cannot find node for ptr 0x{:X}", painting->textureArray);
tArrSymbol << std::hex << "0x" << painting->textureArray << std::dec;
}
}
std::stringstream rDLSymbol;
{
auto dec = Companion::Instance->GetNodeByAddr(painting->rippleDisplayList);
if (dec.has_value()) {
auto rDLNode = std::get<1>(dec.value());
rDLSymbol << GetSafeNode<std::string>(rDLNode, "symbol");
} else {
SPDLOG_WARN("Cannot find node for ptr 0x{:X}", painting->rippleDisplayList);
rDLSymbol << std::hex << "0x" << painting->rippleDisplayList << std::dec;
}
}
std::stringstream rippleTrigger;
if (painting->rippleTrigger == RIPPLE_TRIGGER_PROXIMITY) {
rippleTrigger << "RIPPLE_TRIGGER_PROXIMITY";
} else if (painting->rippleTrigger == RIPPLE_TRIGGER_CONTINUOUS) {
rippleTrigger << "RIPPLE_TRIGGER_CONTINUOUS";
} else {
rippleTrigger << painting->rippleTrigger; // should not reach this case, throw error?
}
write << fourSpaceTab << "/* id */ " << std::hex << "0x" << painting->id << ",\n";
write << fourSpaceTab << "/* Image Count */ " << std::hex << "0x" << (uint32_t)painting->imageCount << ",\n";
write << fourSpaceTab << "/* Texture Type */ " << textureType.str() << ",\n";
write << fourSpaceTab << "/* Floor Status */ " << std::hex << "0x" << (uint32_t)painting->lastFloor << ", "
<< std::hex << "0x" << (uint32_t)painting->currFloor << ", " << std::hex << "0x"
<< (uint32_t)painting->floorEntered << ",\n";
write << fourSpaceTab << "/* Ripple Status */ " << (uint32_t)painting->state << ",\n";
write << fourSpaceTab << "/* Rotation */ " << painting->pitch << ", " << painting->yaw << ",\n";
write << fourSpaceTab << "/* Position */ " << painting->posX << ", " << painting->posY << ", " << painting->posZ
<< ",\n";
write << fourSpaceTab << "/* Ripple Magnitude */ " << painting->currRippleMag << ", " << painting->passiveRippleMag
<< ", " << painting->entryRippleMag << ",\n";
write << fourSpaceTab << "/* Ripple Decay */ " << painting->rippleDecay << ", " << painting->passiveRippleDecay
<< ", " << painting->entryRippleDecay << ",\n";
write << fourSpaceTab << "/* Ripple Rate */ " << painting->currRippleRate << ", " << painting->passiveRippleRate
<< ", " << painting->entryRippleRate << ",\n";
write << fourSpaceTab << "/* Ripple Dispersion */ " << painting->dispersionFactor << ", "
<< painting->passiveDispersionFactor << ", " << painting->entryDispersionFactor << ",\n";
write << fourSpaceTab << "/* Curr Ripple Timer */ " << painting->rippleTimer << ",\n";
write << fourSpaceTab << "/* Curr Ripple x, y */ " << painting->rippleX << ", " << painting->rippleY << ",\n";
write << fourSpaceTab << "/* Normal DList */ " << nDLSymbol.str() << ",\n";
write << fourSpaceTab << "/* Texture Maps */ " << tMapSymbol.str() << ",\n";
write << fourSpaceTab << "/* Textures */ " << tArrSymbol.str() << ",\n";
write << fourSpaceTab << "/* Texture w, h */ " << std::dec << painting->textureWidth << ", "
<< painting->textureHeight << ",\n";
write << fourSpaceTab << "/* Ripple DList */ " << rDLSymbol.str() << ",\n";
write << fourSpaceTab << "/* Ripple Trigger */ " << rippleTrigger.str() << ",\n";
write << fourSpaceTab << "/* Alpha */ " << std::dec << (uint32_t)painting->alpha << ",\n";
write << fourSpaceTab << "/* Mario Below */ " << std::hex << "0x" << (uint32_t)painting->marioWasUnder << ", "
<< std::hex << "0x" << (uint32_t)painting->marioIsUnder << ", " << std::hex << "0x"
<< (uint32_t)painting->marioWentUnder << ",\n";
write << fourSpaceTab << "/* Size */ " << painting->size << ",\n";
write << "};\n";
return offset + 120;
}
ExportResult SM64::PaintingBinaryExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
std::string& entryName, YAML::Node& node, std::string* replacement) {
auto writer = LUS::BinaryWriter();
auto painting = std::static_pointer_cast<SM64::Painting>(raw);
uint32_t ptr;
WriteHeader(writer, Torch::ResourceType::Painting, 0);
writer.Write(painting->id);
writer.Write(painting->imageCount);
writer.Write(painting->textureType);
writer.Write(painting->lastFloor);
writer.Write(painting->currFloor);
writer.Write(painting->floorEntered);
writer.Write(painting->state);
writer.Write(painting->pitch);
writer.Write(painting->yaw);
writer.Write(painting->posX);
writer.Write(painting->posY);
writer.Write(painting->posZ);
writer.Write(painting->currRippleMag);
writer.Write(painting->passiveRippleMag);
writer.Write(painting->entryRippleMag);
writer.Write(painting->rippleDecay);
writer.Write(painting->passiveRippleDecay);
writer.Write(painting->entryRippleDecay);
writer.Write(painting->currRippleRate);
writer.Write(painting->passiveRippleRate);
writer.Write(painting->entryRippleRate);
writer.Write(painting->dispersionFactor);
writer.Write(painting->passiveDispersionFactor);
writer.Write(painting->entryDispersionFactor);
writer.Write(painting->rippleTimer);
writer.Write(painting->rippleX);
writer.Write(painting->rippleY);
ptr = painting->normalDisplayList;
{
auto dec = Companion::Instance->GetNodeByAddr(ptr);
if (dec.has_value()) {
uint64_t hash = CRC64(std::get<0>(dec.value()).c_str());
SPDLOG_INFO("Found DisplayList: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(dec.value()));
writer.Write(hash);
} else {
SPDLOG_WARN("Could not find DisplayList at 0x{:X}", ptr);
writer.Write((uint64_t)0);
}
}
ptr = painting->textureMaps;
{
auto dec = Companion::Instance->GetNodeByAddr(ptr);
if (dec.has_value()) {
uint64_t hash = CRC64(std::get<0>(dec.value()).c_str());
SPDLOG_INFO("Found Texture Maps: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(dec.value()));
writer.Write(hash);
} else {
SPDLOG_WARN("Could not find Texture Maps at 0x{:X}", ptr);
writer.Write((uint64_t)0);
}
}
ptr = painting->textureArray;
{
auto dec = Companion::Instance->GetNodeByAddr(ptr);
if (dec.has_value()) {
uint64_t hash = CRC64(std::get<0>(dec.value()).c_str());
SPDLOG_INFO("Found Texture Arrays: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(dec.value()));
writer.Write(hash);
} else {
SPDLOG_WARN("Could not find Texture Arrays at 0x{:X}", ptr);
writer.Write((uint64_t)0);
}
}
writer.Write(painting->textureWidth);
writer.Write(painting->textureHeight);
ptr = painting->rippleDisplayList;
{
auto dec = Companion::Instance->GetNodeByAddr(ptr);
if (dec.has_value()) {
uint64_t hash = CRC64(std::get<0>(dec.value()).c_str());
SPDLOG_INFO("Found DisplayList: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(dec.value()));
writer.Write(hash);
} else {
SPDLOG_WARN("Could not find DisplayList at 0x{:X}", ptr);
writer.Write((uint64_t)0);
}
}
writer.Write(painting->rippleTrigger);
writer.Write(painting->alpha);
writer.Write(painting->marioWasUnder);
writer.Write(painting->marioIsUnder);
writer.Write(painting->marioWentUnder);
writer.Write(painting->size);
writer.Finish(write);
return std::nullopt;
}
std::optional<std::shared_ptr<IParsedData>> SM64::PaintingFactory::parse(std::vector<uint8_t>& buffer,
YAML::Node& node) {
auto [_, segment] = Decompressor::AutoDecode(node, buffer);
LUS::BinaryReader reader(segment.data, segment.size);
reader.SetEndianness(Torch::Endianness::Big);
auto id = reader.ReadInt16();
auto imageCount = reader.ReadInt8();
auto textureType = reader.ReadInt8();
auto lastFloor = reader.ReadInt8();
auto currFloor = reader.ReadInt8();
auto floorEntered = reader.ReadInt8();
auto state = reader.ReadInt8();
auto pitch = reader.ReadFloat();
auto yaw = reader.ReadFloat();
auto posX = reader.ReadFloat();
auto posY = reader.ReadFloat();
auto posZ = reader.ReadFloat();
auto currRippleMag = reader.ReadFloat();
auto passiveRippleMag = reader.ReadFloat();
auto entryRippleMag = reader.ReadFloat();
auto rippleDecay = reader.ReadFloat();
auto passiveRippleDecay = reader.ReadFloat();
auto entryRippleDecay = reader.ReadFloat();
auto currRippleRate = reader.ReadFloat();
auto passiveRippleRate = reader.ReadFloat();
auto entryRippleRate = reader.ReadFloat();
auto dispersionFactor = reader.ReadFloat();
auto passiveDispersionFactor = reader.ReadFloat();
auto entryDispersionFactor = reader.ReadFloat();
auto rippleTimer = reader.ReadFloat();
auto rippleX = reader.ReadFloat();
auto rippleY = reader.ReadFloat();
auto normalDisplayList = reader.ReadUInt32();
auto textureMaps = reader.ReadUInt32();
auto textureArray = reader.ReadUInt32();
auto textureWidth = reader.ReadInt16();
auto textureHeight = reader.ReadInt16();
auto rippleDisplayList = reader.ReadUInt32();
auto rippleTrigger = reader.ReadInt8();
auto alpha = reader.ReadUByte();
auto marioWasUnder = reader.ReadInt8();
auto marioIsUnder = reader.ReadInt8();
auto marioWentUnder = reader.ReadInt8();
reader.ReadInt8(); // pad
reader.ReadInt8(); // pad
reader.ReadInt8(); // pad
auto size = reader.ReadFloat();
YAML::Node nDLNode;
nDLNode["type"] = "GFX";
nDLNode["offset"] = normalDisplayList;
Companion::Instance->AddAsset(nDLNode);
YAML::Node rDLNode;
rDLNode["type"] = "GFX";
rDLNode["offset"] = rippleDisplayList;
Companion::Instance->AddAsset(rDLNode);
return std::make_shared<SM64::Painting>(
id, imageCount, textureType, lastFloor, currFloor, floorEntered, state, pitch, yaw, posX, posY, posZ,
currRippleMag, passiveRippleMag, entryRippleMag, rippleDecay, passiveRippleDecay, entryRippleDecay,
currRippleRate, passiveRippleRate, entryRippleRate, dispersionFactor, passiveDispersionFactor,
entryDispersionFactor, rippleTimer, rippleX, rippleY, normalDisplayList, textureMaps, textureArray,
textureWidth, textureHeight, rippleDisplayList, rippleTrigger, alpha, marioWasUnder, marioIsUnder,
marioWentUnder, size);
}
|