summaryrefslogtreecommitdiff
path: root/src/factories/sm64/DictionaryFactory.cpp
blob: 63d9f3dfb9a50518c676b456d048524b76e46851 (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
#include "DictionaryFactory.h"

ExportResult SM64::DictionaryBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
    auto writer = LUS::BinaryWriter();
    const auto data = std::static_pointer_cast<DictionaryData>(raw);

    WriteHeader(writer, LUS::ResourceType::Dictionary, 0);

    writer.Write(static_cast<uint32_t>(data->mDictionary.size()));
    for(auto& [key, value] : data->mDictionary){
        writer.Write(key);
        writer.Write(static_cast<uint32_t>(value.size()));
        writer.Write(reinterpret_cast<char*>(value.data()), value.size());
    }
    writer.Finish(write);
    return std::nullopt;
}

std::optional<std::shared_ptr<IParsedData>> SM64::DictionaryFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& data) {
    std::unordered_map<std::string, std::vector<uint8_t>> dictionary;

    for (auto it = data["keys"].begin(); it != data["keys"].end(); ++it) {
        auto key = it->first.as<std::string>();
        auto offset = it->second.as<size_t>();

        std::vector<uint8_t> text;
        const auto bytes = buffer.data();

        while(bytes[offset] != 0xFF){
            auto c = bytes[offset++];
            text.push_back(c);
        }

        text.push_back(0xFF);
        dictionary[key] = text;
    }

    return std::make_shared<DictionaryData>(dictionary);
}