#include "CourseVtx.h" #include "Companion.h" #include "utils/Decompressor.h" #include #define NUM(x) std::dec << std::setfill(' ') << std::setw(6) << x #define COL(c) "0x" << std::hex << std::setw(2) << std::setfill('0') << c ExportResult MK64::CourseVtxHeaderExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { const auto symbol = GetSafeNode(node, "symbol", entryName); if (Companion::Instance->IsOTRMode()) { write << "static const ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n"; return std::nullopt; } write << "extern CourseVtx " << symbol << "[];\n"; return std::nullopt; } ExportResult MK64::CourseVtxCodeExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { auto vtx = std::static_pointer_cast(raw)->mVtxs; const auto symbol = GetSafeNode(node, "symbol", entryName); const auto offset = GetSafeNode(node, "offset"); write << "CourseVtx " << symbol << "[] = {\n"; for (int i = 0; i < vtx.size(); ++i) { auto v = vtx[i]; auto x = v.ob[0]; auto y = v.ob[1]; auto z = v.ob[2]; auto tc1 = v.tc[0]; auto tc2 = v.tc[1]; auto c1 = (uint16_t)v.cn[0]; auto c2 = (uint16_t)v.cn[1]; auto c3 = (uint16_t)v.cn[2]; auto c4 = (uint16_t)v.cn[3]; if (i <= vtx.size() - 1) { write << fourSpaceTab; } // {{{ x, y, z }, { tc1, tc2 }, { c1, c2, c3, c4 }}} write << "{{{" << NUM(x) << ", " << NUM(y) << ", " << NUM(z) << "}, {" << NUM(tc1) << ", " << NUM(tc2) << "}, {" << COL(c1) << ", " << COL(c2) << ", " << COL(c3) << ", " << COL(c4) << "}}},\n"; } write << "};\n"; return offset + vtx.size() * sizeof(CourseVtx); } ExportResult MK64::CourseVtxBinaryExporter::Export(std::ostream& write, std::shared_ptr raw, std::string& entryName, YAML::Node& node, std::string* replacement) { auto vtx = std::static_pointer_cast(raw); auto writer = LUS::BinaryWriter(); WriteHeader(writer, Torch::ResourceType::CourseVertex, 0); writer.Write((uint32_t)vtx->mVtxs.size()); for (auto v : vtx->mVtxs) { writer.Write(v.ob[0]); writer.Write(v.ob[1]); writer.Write(v.ob[2]); writer.Write(v.tc[0]); writer.Write(v.tc[1]); writer.Write(v.cn[0]); writer.Write(v.cn[1]); writer.Write(v.cn[2]); writer.Write(v.cn[3]); } writer.Finish(write); return std::nullopt; } std::optional> MK64::CourseVtxFactory::parse(std::vector& buffer, YAML::Node& node) { auto count = GetSafeNode(node, "count"); auto [_, segment] = Decompressor::AutoDecode(node, buffer, count * sizeof(CourseVtx)); LUS::BinaryReader reader(segment.data, segment.size); // Limit count to actual available vertices in the decompressed data auto actualCount = std::min(count, segment.size / sizeof(CourseVtx)); reader.SetEndianness(Torch::Endianness::Big); std::vector vertices; for (size_t i = 0; i < actualCount; i++) { auto x = reader.ReadInt16(); auto y = reader.ReadInt16(); auto z = reader.ReadInt16(); auto tc1 = reader.ReadInt16(); auto tc2 = reader.ReadInt16(); auto cn1 = reader.ReadUByte(); auto cn2 = reader.ReadUByte(); auto cn3 = reader.ReadUByte(); auto cn4 = reader.ReadUByte(); uint16_t flags = cn1 & 3; flags |= (cn2 << 2) & 0xC; vertices.push_back( VtxRaw({ { x, y, z }, flags, { tc1, tc2 }, { (uint8_t)(cn1 & 0xfc), (uint8_t)(cn2 & 0xfc), cn3, 0xff } })); } return std::make_shared(vertices); }