summaryrefslogtreecommitdiff
path: root/src/factories/mk64/TrackSections.cpp
blob: 85aa03822c6c25308af2591ca0ed4026963e5768 (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
#include "TrackSections.h"

#include "Companion.h"
#include "utils/Decompressor.h"
#include <spdlog/spdlog.h>

#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::TrackSectionsHeaderExporter::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 ALIGN_ASSET(2) char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
        return std::nullopt;
    }

    write << "extern TrackSections " << symbol << "[];\n";
    return std::nullopt;
}

ExportResult MK64::TrackSectionsCodeExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
                                                     std::string& entryName, YAML::Node& node,
                                                     std::string* replacement) {
    auto sections = std::static_pointer_cast<TrackSectionsData>(raw)->mSecs;
    const auto symbol = GetSafeNode(node, "symbol", entryName);
    const auto offset = GetSafeNode<uint32_t>(node, "offset");

    if (Companion::Instance->IsDebug()) {
        write << "// 0x" << std::hex << std::uppercase << offset << "\n";
    }

    write << "TrackSections " << symbol << "[] = {\n";

    for (int i = 0; i < sections.size(); i++) {
        auto entry = sections[i];

        auto crc = entry.crc;
        auto surf = entry.surfaceType;
        auto sect = entry.sectionId;
        auto flags = entry.flags;

        if (i <= sections.size() - 1) {
            write << fourSpaceTab;
        }

        // { addr, surface, section, flags },
        write << "{" << NUM(crc) << ", " << NUM(surf) << ", " << NUM(sect) << ", " << NUM(flags) << "},\n";
    }
    write << "};\n";

    return offset + sections.size() * sizeof(MK64::TrackSections);
}

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

    WriteHeader(writer, Torch::ResourceType::TrackSection, 0);
    writer.Write((uint32_t)sections->mSecs.size());
    for (auto entry : sections->mSecs) {
        auto dec = Companion::Instance->GetSafeStringByAddr(entry.crc, "GFX");
        if (!dec.has_value()) {
            SPDLOG_WARN("Could not find gfx at 0x{:X}", entry.crc);
            writer.Write(entry.crc);
        } else {
            uint64_t hash = CRC64(dec.value().c_str());
            writer.Write(hash);
        }
        writer.Write(entry.surfaceType);
        writer.Write(entry.sectionId);
        writer.Write(entry.flags);
    }

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

std::optional<std::shared_ptr<IParsedData>> MK64::TrackSectionsFactory::parse(std::vector<uint8_t>& buffer,
                                                                              YAML::Node& node) {
    auto count = GetSafeNode<size_t>(node, "count");

    // On-disk format: uint32_t addr + int8_t surf + int8_t sect + uint16_t flags = 8 bytes
    // Note: sizeof(MK64::TrackSections) is 16 bytes due to uint64_t crc + padding
    constexpr size_t kOnDiskTrackSectionSize = sizeof(uint32_t) + sizeof(int8_t) + sizeof(int8_t) + sizeof(uint16_t);
    auto [_, segment] = Decompressor::AutoDecode(node, buffer);
    LUS::BinaryReader reader(segment.data, count * kOnDiskTrackSectionSize);

    reader.SetEndianness(Torch::Endianness::Big);
    std::vector<MK64::TrackSections> sections;

    for (size_t i = 0; i < count; i++) {
        auto addr = reader.ReadUInt32();
        auto surf = reader.ReadInt8();
        auto sect = reader.ReadInt8();
        auto flags = reader.ReadUInt16();

        sections.push_back(MK64::TrackSections({
            addr,
            surf,
            sect,
            flags,
        }));
    }

    return std::make_shared<TrackSectionsData>(sections);
}