summaryrefslogtreecommitdiff
path: root/src/factories/oot/OoTSceneUtils.cpp
blob: ee0a30113e6cb6a6f5cad3d6ee6cd3d0b255b2b6 (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
#include "OoTSceneUtils.h"

namespace OoT {

LUS::BinaryReader ReadSubArray(std::vector<uint8_t>& buffer, uint32_t segAddr, uint32_t size) {
    YAML::Node node;
    node["offset"] = Companion::Instance->PatchVirtualAddr(segAddr);
    auto raw = Decompressor::AutoDecode(node, buffer, size);
    LUS::BinaryReader reader(raw.segment.data, raw.segment.size);
    reader.SetEndianness(Torch::Endianness::Big);
    return reader;
}

std::string ResolvePointer(uint32_t ptr) {
    if (ptr == 0) return "";
    ptr = Companion::Instance->PatchVirtualAddr(ptr);
    auto result = Companion::Instance->GetStringByAddr(ptr);
    if (result.has_value()) return result.value();
    return "";
}

std::string MakeAssetName(const std::string& baseName, const std::string& suffix, uint32_t offset) {
    std::ostringstream ss;
    ss << baseName << suffix << "_" << std::uppercase << std::hex
       << std::setfill('0') << std::setw(6) << offset;
    return ss.str();
}

std::vector<char> SerializePathways(std::vector<uint8_t>& buffer,
                                    const std::vector<std::pair<uint8_t, uint32_t>>& pathways,
                                    uint32_t writeCount, uint32_t repeats) {
    LUS::BinaryWriter w;
    BaseExporter::WriteHeader(w, Torch::ResourceType::OoTPath, 0);
    w.Write(static_cast<uint32_t>(writeCount));
    for (uint32_t r = 0; r < repeats; r++) {
        for (auto& [np, ptsAddr] : pathways) {
            w.Write(static_cast<uint32_t>(np));
            auto ptReader = ReadSubArray(buffer, ptsAddr, np * 6);
            for (uint8_t k = 0; k < np; k++) {
                w.Write(ptReader.ReadInt16());
                w.Write(ptReader.ReadInt16());
                w.Write(ptReader.ReadInt16());
            }
        }
    }
    std::stringstream ss;
    w.Finish(ss);
    std::string str = ss.str();
    return std::vector<char>(str.begin(), str.end());
}

} // namespace OoT