summaryrefslogtreecommitdiff
path: root/src/factories/Vec3sFactory.cpp
blob: 7e62d8953c44dd0c48f686df9bf8e1b257c59005 (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
#include "Vec3sFactory.h"
#include "spdlog/spdlog.h"

#include "Companion.h"
#include "utils/Decompressor.h"
#include "utils/TorchUtils.h"

#define FORMAT_INT(x, w) std::dec << std::setfill(' ') << std::setw(w) << x

Vec3sData::Vec3sData(std::vector<Vec3s> vecs): mVecs(vecs) {
    mMaxWidth = 3;
    for(Vec3s v : vecs) {
        mMaxWidth = std::max(mMaxWidth, v.width());
    }
}

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


int GetPrecision(Vec3s v) {
    return std::max(std::max(GetPrecision(v.x), GetPrecision(v.y)), GetPrecision(v.z));
}

ExportResult Vec3sCodeExporter::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 vecData = std::static_pointer_cast<Vec3sData>(raw);
    int i = 0;

    write << "Vec3s " << symbol << "[] = {";

    int cols = 120 / (3 * vecData->mMaxWidth + 8);
    for(Vec3s v : vecData->mVecs) {
        if((i++ % cols) == 0) {
            write << "\n" << fourSpaceTab;
        }
        write << FORMAT_INT(v, vecData->mMaxWidth) << ", ";
    }

    write << "\n};\n";

    if (Companion::Instance->IsDebug()) {
        write << "// Count: " << vecData->mVecs.size() << " Vec3s\n";
    }

    return offset + vecData->mVecs.size() * sizeof(Vec3s);
}

ExportResult Vec3sBinaryExporter::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>> Vec3sFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
    std::vector<Vec3s> vecs;
    const auto count = GetSafeNode<int>(node, "count");
    auto [root, 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++) {
        auto vx = reader.ReadInt16();
        auto vy = reader.ReadInt16();
        auto vz = reader.ReadInt16();

        vecs.push_back(Vec3s(vx, vy, vz));
    }

    return std::make_shared<Vec3sData>(vecs);
}