summaryrefslogtreecommitdiff
path: root/src/factories/sf64/ColPolyFactory.cpp
blob: d4ecbfd5e1e9d6c67b6182c71e2d9391139d7ff3 (plain)
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
#include "ColPolyFactory.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

SF64::ColPolyData::ColPolyData(std::vector<SF64::CollisionPoly> polys, std::vector<YAML::Node> meshNodes): mPolys(polys), mMeshNodes(meshNodes) {

}

ExportResult SF64::ColPolyHeaderExporter::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 CollisionPoly " << symbol << "[];\n";
    return std::nullopt;
}

ExportResult SF64::ColPolyCodeExporter::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 colpolys = std::static_pointer_cast<SF64::ColPolyData>(raw);
    const auto meshSize = GetSafeNode(colpolys->mMeshNodes[0], "count", 0);

    write << "CollisionPoly " << symbol << "[] = {";
    int width = std::log10(meshSize) + 1;

    for(SF64::CollisionPoly poly : colpolys->mPolys) {
        write << "\n" << fourSpaceTab;
        write << "{ " << NUM(poly.tri, width) << ",  ";
        if (poly.unk_06 == 0) {
            write << "0,  ";
        } else {
            SPDLOG_INFO("SF64:COLPOLY alert: Nonzero value of unk_06");
            write << "/* ALERT: NONZERO */ " << poly.unk_06 << ",  ";
        }
        write << NUM(poly.norm, 6) << ",  ";
        if(poly.unk_0E != 0) {
            SPDLOG_ERROR("SF64:COLPOLY error: Nonzero value found in padding");
            write << "/* ALERT: NONZERO PAD */ ";
        }
        write << NUM(poly.dist, 9) << "}, ";
    }

    write << "\n};\n";
    uint32_t endOffset = ASSET_PTR(offset) + colpolys->mPolys.size() * sizeof(SF64::CollisionPoly);
    if (Companion::Instance->IsDebug()) {
        write << "// CollisionPoly count: " << colpolys->mPolys.size() << "\n";
        write << "// 0x" << std::uppercase << std::hex << endOffset << "\n";
    }

    write << "\n";

    return endOffset;
}

ExportResult SF64::ColPolyBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
    return std::nullopt;
}

std::optional<std::shared_ptr<IParsedData>> SF64::ColPolyFactory::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<SF64::CollisionPoly> polys;
    std::vector<YAML::Node> meshNodes;
    int meshSize = 0;
    auto [_, segment] = Decompressor::AutoDecode(node, buffer, count * sizeof(SF64::CollisionPoly));
    LUS::BinaryReader reader(segment.data, segment.size);
    reader.SetEndianness(LUS::Endianness::Big);

    for(int i = 0; i < count; i++) {
        int16_t v0 = reader.ReadInt16();
        meshSize = std::max(meshSize, (int)v0);
        int16_t v1 = reader.ReadInt16();
        meshSize = std::max(meshSize, (int)v1);
        int16_t v2 = reader.ReadInt16();
        meshSize = std::max(meshSize, (int)v2);
        int16_t pad1 = reader.ReadInt16();
        int16_t nx = reader.ReadInt16();
        int16_t ny = reader.ReadInt16();
        int16_t nz = reader.ReadInt16();
        int16_t pad2 = reader.ReadInt16();
        int32_t dist = reader.ReadInt32();

        polys.push_back(CollisionPoly({{v0, v1, v2}, pad1, {nx, ny, nz}, pad2, dist}));
    }
    meshSize++;
    auto meshOffset = GetSafeNode<uint32_t>(node, "mesh_offset", offset + count * sizeof(SF64::CollisionPoly));
    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;
                offsetSeg << std::uppercase << std::hex << meshOffset;

                meshSymbol = std::regex_replace(meshSymbol, std::regex(R"(OFFSET)"), offsetSeg.str());
            }
            meshNode["symbol"] = meshSymbol;
        }
        meshNode["type"] = "VEC3S";
        meshNode["count"] = meshSize;
        meshNode["offset"] = meshOffset;

        meshNode = Companion::Instance->AddAsset(meshNode).value();

        meshNodes.push_back(meshNode);
        meshOffset += meshSize * sizeof(Vec3s);
    }

    return std::make_shared<SF64::ColPolyData>(polys, meshNodes);
}