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
|
#include "TriangleFactory.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include "utils/Decompressor.h"
#include "utils/TorchUtils.h"
#include <regex>
#define NUM(x, w) std::dec << std::setfill(' ') << std::setw(w) << x
#define FORMAT_FLOAT(x, w, p) std::dec << std::setfill(' ') << std::fixed << std::setprecision(p) << std::setw(w) << x
#define STRIP_SEGMENT(offset) (IS_SEGMENTED(offset) ? SEGMENT_OFFSET(offset) : (offset))
// SF64::TriangleData::TriangleData(std::vector<Vec3s> tris, std::vector<std::vector<Vec3f>> meshes): mTris(tris), mMeshes(meshes) {
// }
SF64::TriangleData::TriangleData(std::vector<Vec3s> tris, std::vector<YAML::Node> meshNodes): mTris(tris), mMeshNodes(meshNodes) {
}
void SF64::TriangleHeaderExporter::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;
}
write << "extern Triangle " << symbol << "[];\n";
}
void SF64::TriangleCodeExporter::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 triData = std::static_pointer_cast<SF64::TriangleData>(raw);
const auto meshSize = GetSafeNode(triData->mMeshNodes[0], "count", 0);
auto off = offset;
auto segment = 0;
int i;
if(IS_SEGMENTED(off)) {
off = SEGMENT_OFFSET(off);
}
if (Companion::Instance->IsDebug()) {
write << "// 0x" << std::uppercase << std::hex << STRIP_SEGMENT(offset) << "\n";
}
write << "Triangle " << symbol << "[] = {";
int width = std::log10(meshSize) + 1;
i = 0;
for(Vec3s tri : triData->mTris) {
if((i++ % 6) == 0) {
write << "\n" << fourSpaceTab;
}
write << NUM(tri, width) << ", ";
}
write << "\n};\n";
if (Companion::Instance->IsDebug()) {
write << "// Triangle count: " << triData->mTris.size() << "\n";
write << "// 0x" << std::uppercase << std::hex << off + triData->mTris.size() * sizeof(Vec3s) << "\n";
}
write << "\n";
// auto meshOffset = offset + triData->mTris.size() * sizeof(Vec3s);
// for(int j = 0; j < triData->mMeshes.size(); j++) {
// std::string indexTag = "";
// std::ostringstream offsetStr;
// std::string meshSymbol;
// if(triData->mMeshes.size() != 1) {
// indexTag = std::to_string(j) + "_";
// }
// offsetStr << std::uppercase << std::hex << STRIP_SEGMENT(meshOffset);
// if(!node["mesh_symbol"]) {
// meshSymbol = symbol + "_mesh_" + indexTag + offsetStr.str();
// } else {
// meshSymbol = GetSafeNode<std::string>(node, "mesh_symbol");
// if(!ReplaceOffset(meshSymbol, meshOffset)) {
// meshSymbol += indexTag;
// }
// }
// if (Companion::Instance->IsDebug()) {
// write << "// 0x" << std::uppercase << std::hex << off << "\n";
// }
// write << "Vec3f " << meshSymbol << "[] = {";
// i = 0;
// for(Vec3f vtx : triData->mMeshes[j]) {
// if((i++ % 4) == 0) {
// write << "\n" << fourSpaceTab;
// }
// write << NUM(vtx, 5) << ", ";
// }
// write << "\n};\n";
// off += triData->mMeshes[j].size() * sizeof(Vec3f);
// if (Companion::Instance->IsDebug()) {
// write << "// Mesh vertex count: " << triData->mMeshes[j].size() << "\n";
// write << "// 0x" << std::uppercase << std::hex << off << "\n";
// }
// write << "\n";
// }
}
void SF64::TriangleBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
}
std::optional<std::shared_ptr<IParsedData>> SF64::TriangleFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
const auto offset = GetSafeNode<uint32_t>(node, "offset");
const auto count = GetSafeNode<uint32_t>(node, "count");
const auto meshCount = GetSafeNode<uint32_t>(node, "mesh_count", 1);
std::vector<Vec3s> tris;
std::vector<YAML::Node> meshNodes;
int meshSize = 0;
auto [_, segment] = Decompressor::AutoDecode(node, buffer, count * sizeof(Vec3s));
LUS::BinaryReader reader(segment.data, segment.size);
reader.SetEndianness(LUS::Endianness::Big);
for(int i = 0; i < count; i++) {
Vec3s tri;
tri.x = reader.ReadInt16();
meshSize = std::max(meshSize, (int)tri.x);
tri.y = reader.ReadInt16();
meshSize = std::max(meshSize, (int)tri.y);
tri.z = reader.ReadInt16();
meshSize = std::max(meshSize, (int)tri.z);
tris.push_back(tri);
}
meshSize++;
auto meshOffset = GetSafeNode<uint32_t>(node, "mesh_offset", offset + count * sizeof(Vec3s));
for(int j = 0; j < meshCount; j++) {
YAML::Node meshNode;
if(node["mesh_symbol"]) {
auto meshSymbol = GetSafeNode<std::string>(node, "mesh_symbol");
if (meshSymbol.find("OFFSET") == std::string::npos) {
if(meshCount > 1) {
meshSymbol += "_" + std::to_string(j);
}
} else {
std::ostringstream offsetSeg;
std::string replaceOff = "OFFSET";
std::string off;
offsetSeg << std::uppercase << std::hex << meshOffset;
off = offsetSeg.str();
meshSymbol = std::regex_replace(meshSymbol, std::regex(R"(OFFSET)"), offsetSeg.str());
// std::replace(meshSymbol.begin(), meshSymbol.end(), replaceOff, off);
}
meshNode["symbol"] = meshSymbol;
}
meshNode["type"] = "VEC3F";
meshNode["count"] = meshSize;
meshNode["offset"] = meshOffset;
meshNode = Companion::Instance->AddAsset(meshNode).value();
meshNodes.push_back(meshNode);
meshOffset += meshSize * sizeof(Vec3f);
}
// YAML::Node meshNode;
// meshNode["offset"] = meshOffset;
// auto [__, meshSegment] = Decompressor::AutoDecode(meshNode, buffer, meshSize * meshCount * sizeof(Vec3f));
// LUS::BinaryReader meshReader(meshSegment.data, meshSegment.size);
// meshReader.SetEndianness(LUS::Endianness::Big);
// std::vector<std::vector<Vec3f>> meshes;
// for(int j = 0; j < meshCount; j++) {
// std::vector<Vec3f> mesh;
// Vec3f vtx;
// for(int i = 0; i < meshSize; i++) {
// vtx.x = meshReader.ReadFloat();
// vtx.y = meshReader.ReadFloat();
// vtx.z = meshReader.ReadFloat();
// mesh.push_back(vtx);
// }
// meshes.push_back(mesh);
// }
return std::make_shared<SF64::TriangleData>(tris, meshNodes);
}
|