summaryrefslogtreecommitdiff
path: root/src/factories/bk64/DemoInputFactory.cpp
blob: df0b1db4d6050e073d49afd4ee8706654dbe1dbb (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "DemoInputFactory.h"

#include "Companion.h"
#include "spdlog/spdlog.h"
#include "types/RawBuffer.h"
#include "utils/Decompressor.h"
#include "utils/TorchUtils.h"

#include "BKEmitText.h"

namespace BK64 {

ExportResult DemoInputCodeExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
                                           std::string& entryName, YAML::Node& node, std::string* replacement) {
    auto offset = GetSafeNode<uint32_t>(node, "offset");
    auto demoInput = std::static_pointer_cast<DemoInputData>(raw);
    const auto symbol = GetSafeNode(node, "symbol", entryName);

    write << "DemoFileHeader " << symbol << " = {\n";

    write << fourSpaceTab << demoInput->mInputs.size() * sizeof(ControllerInput) << ",\n";

    for (const auto& input : demoInput->mInputs) {
        write << fourSpaceTab << "{ ";
        write << (int32_t)input.stickX << ", " << (int32_t)input.stickY << ", 0x" << FORMAT_HEX(input.buttons, 4)
              << ", " << (uint32_t)input.frames << ", " << (uint32_t)input.unkFlag;
        write << " },\n";
    }

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

    return offset;
}

ExportResult BK64::DemoInputBinaryExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
                                                   std::string& entryName, YAML::Node& node, std::string* replacement) {
    auto writer = LUS::BinaryWriter();
    const auto demoInput = std::static_pointer_cast<DemoInputData>(raw);

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

    writer.Write((uint32_t)demoInput->mInputs.size());
    for (const auto& input : demoInput->mInputs) {
        writer.Write(input.stickX);
        writer.Write(input.stickY);
        writer.Write(input.buttons);
        writer.Write(input.frames);
        writer.Write(input.unkFlag);
    }

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

ExportResult BK64::DemoInputModdingExporter::Export(std::ostream& write, std::shared_ptr<IParsedData> raw,
                                                    std::string& entryName, YAML::Node& node,
                                                    std::string* replacement) {
    const auto demoInput = std::static_pointer_cast<DemoInputData>(raw);
    const auto symbol = GetSafeNode(node, "symbol", entryName);

    *replacement += ".yaml";

    YAML::Emitter out;
    out << YAML::BeginMap;
    out << YAML::Key << symbol;
    out << YAML::Value;
    out.SetIndent(2);

    out << YAML::BeginMap;
    out << YAML::Key << "ControllerInputs" << YAML::Value;

    out << YAML::BeginSeq;
    for (const auto& input : demoInput->mInputs) {
        out << YAML::BeginMap;
        out << YAML::Key << "StickX" << YAML::Value << (int32_t)input.stickX;
        out << YAML::Key << "StickY" << YAML::Value << (int32_t)input.stickY;
        out << YAML::Key << "Buttons" << YAML::Value << YAML_HEX(input.buttons);
        out << YAML::Key << "Frames" << YAML::Value << (uint32_t)input.frames;
        out << YAML::Key << "UnkFlag" << YAML::Value << (uint32_t)input.unkFlag;
        out << YAML::EndMap;
    }
    out << YAML::EndSeq;
    out << YAML::EndMap;
    out << YAML::EndMap;

    write.write(out.c_str(), out.size());

    return std::nullopt;
}

std::optional<std::shared_ptr<IParsedData>> DemoInputFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
    auto [_, segment] = Decompressor::AutoDecode(node, buffer);
    LUS::BinaryReader reader(segment.data, segment.size);
    reader.SetEndianness(Torch::Endianness::Big);
    const auto symbol = GetSafeNode<std::string>(node, "symbol");

    SPDLOG_INFO("START SYMBOL {}", symbol);

    if (segment.size < 4) {
        return std::make_shared<DemoInputData>(std::vector<ControllerInput>());
    }

    auto size = reader.ReadUInt32();

    std::vector<ControllerInput> inputs;

    for (uint32_t i = 0; i < size / sizeof(ControllerInput); i++) {
        ControllerInput input;

        input.stickX = reader.ReadInt8();
        input.stickY = reader.ReadInt8();
        input.buttons = reader.ReadUInt16();
        input.frames = reader.ReadUByte();
        input.unkFlag = reader.ReadUByte();

        inputs.push_back(input);
    }

    return std::make_shared<DemoInputData>(inputs);
}

std::optional<std::shared_ptr<IParsedData>> DemoInputFactory::parse_modding(std::vector<uint8_t>& buffer,
                                                                            YAML::Node& node) {
    const YAML::Node info = YamlEntry(buffer, GetSafeNode<std::string>(node, "symbol"));

    auto controllerInfo = info["ControllerInputs"];

    std::vector<ControllerInput> inputs;

    for (YAML::iterator it = controllerInfo.begin(); it != controllerInfo.end(); ++it) {
        ControllerInput input;

        auto inputInfo = *it;

        input.stickX = inputInfo["StickX"].as<int32_t>();
        input.stickY = inputInfo["StickY"].as<int32_t>();
        input.buttons = inputInfo["Buttons"].as<uint16_t>();
        input.frames = inputInfo["Frames"].as<uint32_t>();
        input.unkFlag = inputInfo["UnkFlag"].as<uint32_t>();

        inputs.push_back(input);
    }

    return std::make_shared<DemoInputData>(inputs);
}

} // namespace BK64