summaryrefslogtreecommitdiff
path: root/src/factories/oot/OoTSkeletonFactory.cpp
blob: 70bf13208445218fe98a1b0383b7dd7b71e6c061 (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
#include "OoTSkeletonFactory.h"
#include "OoTSkeletonTypes.h"
#include "OoTSceneUtils.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include "utils/Decompressor.h"

namespace OoT {

std::optional<std::shared_ptr<IParsedData>> OoTSkeletonFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
    auto skelTypeStr = GetSafeNode<std::string>(node, "skel_type");
    auto limbTypeStr = GetSafeNode<std::string>(node, "limb_type");
    auto skelType = ParseSkeletonType(skelTypeStr);
    auto limbType = ParseLimbType(limbTypeStr);

    size_t skelSize = (skelType == OoTSkeletonType::Flex) ? 0x0C : 0x08;
    auto [_, segment] = Decompressor::AutoDecode(node, buffer, skelSize);
    LUS::BinaryReader reader(segment.data, segment.size);
    reader.SetEndianness(Torch::Endianness::Big);

    uint32_t limbsArrayAddr = reader.ReadUInt32();
    limbsArrayAddr = Companion::Instance->PatchVirtualAddr(limbsArrayAddr);
    uint8_t limbCount = reader.ReadUByte();
    uint8_t dListCount = 0;

    if (skelType == OoTSkeletonType::Flex) {
        reader.Seek(8, LUS::SeekOffsetType::Start);
        dListCount = reader.ReadUByte();
    }

    auto symbol = GetSafeNode<std::string>(node, "symbol");
    std::vector<std::string> limbPaths;
    if (limbsArrayAddr != 0 && limbCount > 0) {
        YAML::Node limbTableNode;
        limbTableNode["offset"] = limbsArrayAddr;
        auto limbTableRaw = Decompressor::AutoDecode(limbTableNode, buffer, limbCount * 4);
        LUS::BinaryReader limbTableReader(limbTableRaw.segment.data, limbTableRaw.segment.size);
        limbTableReader.SetEndianness(Torch::Endianness::Big);

        for (uint8_t i = 0; i < limbCount; i++) {
            uint32_t limbAddr = limbTableReader.ReadUInt32();
            limbAddr = Companion::Instance->PatchVirtualAddr(limbAddr);
            std::string limbPath = ResolvePointer(limbAddr);
            if (limbPath.empty() && limbAddr != 0) {
                SPDLOG_WARN("Undeclared limb at 0x{:08X} — YAML enrichment incomplete", limbAddr);
            }
            limbPaths.push_back(limbPath);
        }
    }

    if (limbsArrayAddr != 0) {
        auto limbTablePath = ResolvePointer(limbsArrayAddr);
        if (limbTablePath.empty()) {
            SPDLOG_WARN("Undeclared limb table at 0x{:08X} — YAML enrichment incomplete", limbsArrayAddr);
        }
    }

    auto skel = std::make_shared<OoTSkeletonData>();
    skel->skelType = skelType;
    skel->limbType = limbType;
    skel->limbCount = limbCount;
    skel->dListCount = dListCount;
    skel->limbPaths = std::move(limbPaths);

    return skel;
}

ExportResult OoTSkeletonBinaryExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
                                                std::string& entryName, YAML::Node& node,
                                                std::string* replacement) {
    auto writer = LUS::BinaryWriter();
    auto skel = std::static_pointer_cast<OoTSkeletonData>(raw);

    WriteHeader(writer, Torch::ResourceType::OoTSkeleton, 0);

    writer.Write(static_cast<uint8_t>(skel->skelType));
    writer.Write(static_cast<uint8_t>(skel->limbType));
    writer.Write(static_cast<uint32_t>(skel->limbCount));
    writer.Write(static_cast<uint32_t>(skel->dListCount));
    writer.Write(static_cast<uint8_t>(skel->limbType));
    writer.Write(static_cast<uint32_t>(skel->limbPaths.size()));

    for (auto& path : skel->limbPaths) {
        writer.Write(path);
    }

    writer.Finish(write);
    return std::nullopt;
}

} // namespace OoT