summaryrefslogtreecommitdiff
path: root/src/factories/DisplayListFactory.cpp
blob: ec28ed071c82fbb1ae44dd2499ba3cd2318eefd6 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "DisplayListFactory.h"
#include "DisplayListOverrides.h"
#include "utils/MIODecoder.h"
#include "spdlog/spdlog.h"
#include "Companion.h"
#include <gfxd.h>
#include <fstream>

#define C0(pos, width) ((w0 >> (pos)) & ((1U << width) - 1))

template< typename T >
std::string to_hex(T number, const bool append0x = true) {
    std::stringstream stream;
    if(append0x) {
        stream << "0x";
    }
    stream << std::setfill ('0') << std::setw(sizeof(T)*2)
           << std::hex << number;

    auto format = stream.str();
    std::transform(format.begin(), format.end(), format.begin(), ::toupper);
    return format;
}

void GFXDSetGBIVersion(){
    switch (Companion::Instance->GetGBIVersion()) {
        case GBIVersion::f3d:
            gfxd_target(gfxd_f3d);
            break;
        case GBIVersion::f3dex:
            gfxd_target(gfxd_f3dex);
            break;
        case GBIVersion::f3db:
            gfxd_target(gfxd_f3db);
            break;
        case GBIVersion::f3dex2:
            gfxd_target(gfxd_f3dex2);
            break;
        case GBIVersion::f3dexb:
            gfxd_target(gfxd_f3dexb);
            break;
    }
}

void DListHeaderExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement) {
    const auto symbol = node["symbol"] ? node["symbol"].as<std::string>() : entryName;

    if(Companion::Instance->IsOTRMode()){
        write << "static const char " << symbol << "[] = \"__OTR__" << (*replacement) << "\";\n\n";
        return;
    }

    write << "extern Gfx " << symbol << "[];\n";
}

void DListCodeExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
    const auto cmds = std::static_pointer_cast<DListData>(raw)->mGfxs;
    const auto symbol = node["symbol"].as<std::string>();
    const auto offset = node["offset"].as<uint32_t>();
    char out[0xFFFF] = {0};

    gfxd_input_buffer(cmds.data(), sizeof(uint32_t) * cmds.size());
    gfxd_output_buffer(out, sizeof(out));

    gfxd_endian(gfxd_endian_host, sizeof(uint32_t));
    gfxd_macro_fn([] {
        auto gfx = static_cast<const Gfx*>(gfxd_macro_data());
        auto opcode = (gfx->words.w0 >> 24) & 0xFF;

        gfxd_puts(fourSpaceTab);

        #define QUAD 0xB5
        #define TRI2 0xB1

        switch(opcode) {

            // For mk64 only
            case QUAD:
                if (Companion::Instance->GetGBIMinorVersion() == GBIMinorVersion::Mk64) {
                    GFXDOverride::Quadrangle(gfx);
                } else {
                    gfxd_macro_dflt();
                }
                break;
            // Prevents mix and matching of quadrangle commands. Forces 2TRI only. 
            case TRI2:
                GFXDOverride::Triangle2(gfx);
                break;
            default:
                gfxd_macro_dflt();
                break;
        }

        gfxd_puts(",\n");
        return 0;
    });

    gfxd_vtx_callback(GFXDOverride::Vtx);
    gfxd_timg_callback(GFXDOverride::Texture);
    gfxd_dl_callback(GFXDOverride::DisplayList);
    gfxd_lightsn_callback(GFXDOverride::Light);
    GFXDSetGBIVersion();

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

    gfxd_puts(("Gfx " + symbol + "[] = {\n").c_str());
    gfxd_execute();
    gfxd_puts("};\n\n");

    write << std::string(out);
}

void DebugDisplayList(uint32_t w0, uint32_t w1){
    uint32_t dlist[] = {w0, w1};
    gfxd_input_buffer(dlist, sizeof(dlist));
    gfxd_output_fd(fileno(stdout));
    gfxd_endian(gfxd_endian_host, sizeof(uint32_t));
    gfxd_macro_fn([](){
        gfxd_puts("> ");
        gfxd_macro_dflt();
        gfxd_puts("\n");
        return 0;
    });
    gfxd_vtx_callback(GFXDOverride::Vtx);
    gfxd_timg_callback(GFXDOverride::Texture);
    gfxd_dl_callback(GFXDOverride::DisplayList);
    //gfxd_light_callback(GFXDOverride::Light);
    GFXDSetGBIVersion();
    gfxd_execute();
    int bp = 0;
}

void DListBinaryExporter::Export(std::ostream &write, std::shared_ptr<IParsedData> raw, std::string& entryName, YAML::Node &node, std::string* replacement ) {
    auto cmds = std::static_pointer_cast<DListData>(raw)->mGfxs;
    auto writer = LUS::BinaryWriter();

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

    while (writer.GetBaseAddress() % 8 != 0)
        writer.Write(static_cast<uint8_t>(0xFF));

    auto bhash = CRC64((*replacement).c_str());
    writer.Write(static_cast<uint32_t>((G_MARKER << 24)));
    writer.Write(0xBEEFBEEF);
    writer.Write(static_cast<uint32_t>(bhash >> 32));
    writer.Write(static_cast<uint32_t>(bhash & 0xFFFFFFFF));

#ifndef _WIN32
#define case case (uint8_t)
#endif

    for(size_t i = 0; i < cmds.size(); i+=2){
        auto w0 = cmds[i];
        auto w1 = cmds[i + 1];
        uint8_t opcode = w0 >> 24;

        switch (opcode) {
            case G_VTX: {
                auto nvtx = (C0(0, 16)) / sizeof(Vtx);
                auto didx = C0(16, 4);
                uint32_t ptr = SEGMENT_OFFSET(w1);

                if(const auto decl = Companion::Instance->GetNodeByAddr(ptr); decl.has_value()){
                    uint64_t hash = CRC64(std::get<0>(decl.value()).c_str());
                    SPDLOG_INFO("Found vtx: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(decl.value()));

                    // TODO: Find a better way to do this because its going to break on other games
                    Gfx value = gsSPVertexOTR(didx, nvtx, 0);

                    SPDLOG_INFO("gsSPVertex({}, {}, 0x{:X})", nvtx, didx, ptr);

                    w0 = value.words.w0;
                    w1 = value.words.w1;

                    writer.Write(w0);
                    writer.Write(w1);

                    w0 = hash >> 32;
                    w1 = hash & 0xFFFFFFFF;
                } else {
                    SPDLOG_WARN("Warning: Could not find vtx at 0x{:X}", ptr);
                }
                break;
            }
            case G_DL: {
                auto ptr = SEGMENT_OFFSET(w1);
                auto dec = Companion::Instance->GetNodeByAddr(ptr);

                Gfx value = gsSPDisplayListOTRHash(ptr);
                w0 = value.words.w0;
                w1 = value.words.w1;

                writer.Write(w0);
                writer.Write(w1);

                if(dec.has_value()){
                    uint64_t hash = CRC64(std::get<0>(dec.value()).c_str());
                    SPDLOG_INFO("Found display list: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(dec.value()));
                    w0 = hash >> 32;
                    w1 = hash & 0xFFFFFFFF;
                } else {
                    SPDLOG_WARN("Warning: Could not find display list at 0x{:X}", ptr);
                }
                break;
            }
            case G_SETTIMG: {
                auto ptr = SEGMENT_OFFSET(w1);
                auto dec = Companion::Instance->GetNodeByAddr(ptr);

                Gfx value = gsDPSetTextureImage(C0(21, 3), C0(19, 2), C0(0, 10), ptr);
                w0 = value.words.w0 & 0x00FFFFFF;
                w0 += (G_SETTIMG_OTR_HASH << 24);
                w1 = value.words.w1;

                writer.Write(w0);
                writer.Write(w1);

                if(dec.has_value()){
                    uint64_t hash = CRC64(std::get<0>(dec.value()).c_str());
                    SPDLOG_INFO("Found texture: 0x{:X} Hash: 0x{:X} Path: {}", ptr, hash, std::get<0>(dec.value()));
                    w0 = hash >> 32;
                    w1 = hash & 0xFFFFFFFF;
                } else {
                    SPDLOG_WARN("Warning: Could not find texture at 0x{:X}", ptr);
                }
                break;
            }
            default: break;
        }

        writer.Write(w0);
        writer.Write(w1);
    }

#undef case

    writer.Finish(write);
}

std::optional<std::shared_ptr<IParsedData>> DListFactory::parse(std::vector<uint8_t>& buffer, YAML::Node& node) {
    const auto gbi = Companion::Instance->GetGBIVersion();

    const auto mio0 = node["mio0"].as<uint32_t>();
    const auto offset = node["offset"].as<int32_t>();
    auto decoded = MIO0Decoder::Decode(buffer, mio0);

    LUS::BinaryReader reader(decoded.data(), decoded.size());
    reader.SetEndianness(LUS::Endianness::Big);
    reader.Seek(offset, LUS::SeekOffsetType::Start);

    std::vector<uint32_t> gfxs;
    auto processing = true;

    while (processing){
        auto w0 = reader.ReadUInt32();
        auto w1 = reader.ReadUInt32();

        uint8_t opcode = w0 >> 24;
        gfxs.push_back(w0);
        gfxs.push_back(w1);

        switch (opcode) {
            case static_cast<uint8_t>(G_ENDDL):
                processing = false;
                break;
            case static_cast<uint8_t>(G_DL): {
                auto ptr = SEGMENT_OFFSET(w1);
                auto dec = Companion::Instance->GetNodeByAddr(ptr);

                if(!dec.has_value()){
                    SPDLOG_INFO("Addr to Display list command at 0x{:X} not in yaml, autogenerating  it", w1);
                    auto addr = Companion::Instance->GetSegmentedAddr(SEGMENT_NUMBER(w1));
                    if(!addr.has_value()) {
                        SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}", SEGMENT_NUMBER(w1));
                        break;
                    }

                    auto rom = Companion::Instance->GetRomData();
                    auto factory = Companion::Instance->GetFactory("GFX")->get();
                    std::string output = Companion::Instance->NormalizeAsset("seg" + std::to_string(SEGMENT_NUMBER(w1)) +"_dl_" + to_hex(w1, false));
                    YAML::Node dl;
                    dl["type"] = "GFX";
                    dl["mio0"] = addr.value();
                    dl["offset"] = ptr;
                    dl["symbol"] = output;
                    auto result = factory->parse(rom, dl);

                    if(!result.has_value()){
                        break;
                    }

                    Companion::Instance->RegisterAsset(output, dl);
                } else {
                    SPDLOG_WARN("Warning: Could not find display list at 0x{:X}", ptr);
                }
                break;
            }
            // Lights1
            case static_cast<uint8_t>(G_MOVEMEM): {
                uint32_t ptr = SEGMENT_OFFSET(w1);

                // Ignore if not a Lights1 command
                if ( ( (w0 >> 16) & 0xFF ) != G_MV_L1) {
                    break;
                }

                if(const auto decl = Companion::Instance->GetNodeByAddr(ptr); !decl.has_value()){
                    SPDLOG_INFO("Addr to Lights1 command at 0x{:X} not in yaml, autogenerating  it", w1);
                    auto addr = Companion::Instance->GetSegmentedAddr(SEGMENT_NUMBER(w1));
                    if(!addr.has_value()) {
                        SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}", SEGMENT_NUMBER(w1));
                        break;
                    }
                    auto rom = Companion::Instance->GetRomData();
                    auto factory = Companion::Instance->GetFactory("LIGHTS")->get();
                    std::string output = Companion::Instance->NormalizeAsset("seg" + std::to_string(SEGMENT_NUMBER(w1)) +"_lights1_" + to_hex(w1, false));
                    YAML::Node light;
                    light["type"] = "lights";
                    light["mio0"] = addr.value();
                    light["offset"] = ptr;
                    light["symbol"] = output;
                    auto result = factory->parse(rom, light);
                    if(result.has_value()){
                        Companion::Instance->RegisterAsset(output, light);
                    }
                }
                break;
            }
            case static_cast<uint8_t>(G_VTX): {
                uint32_t nvtx;

                switch (gbi) {
                    case GBIVersion::f3dex2:
                        nvtx = C0(12, 8);
                        break;
                    case GBIVersion::f3dex:
                    case GBIVersion::f3dexb:
                        nvtx = C0(10, 6);
                        break;
                    default:
                        nvtx = (C0(0, 16)) / sizeof(Vtx);
                        break;
                }

                uint32_t ptr = SEGMENT_OFFSET(w1);

                if(const auto decl = Companion::Instance->GetNodeByAddr(ptr); !decl.has_value()){
                    SPDLOG_INFO("Addr to Vtx array at 0x{:X} not in yaml, autogenerating  it", w1);
                    auto addr = Companion::Instance->GetSegmentedAddr(SEGMENT_NUMBER(w1));
                    if(!addr.has_value()) {
                        SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}", SEGMENT_NUMBER(w1));
                        break;
                    }

                    auto rom = Companion::Instance->GetRomData();
                    auto factory = Companion::Instance->GetFactory("VTX")->get();
                    std::string output = Companion::Instance->NormalizeAsset("seg" + std::to_string(SEGMENT_NUMBER(w1)) +"_vtx_" + to_hex(w1, false));
                    YAML::Node vtx;
                    vtx["type"] = "VTX";
                    vtx["mio0"] = addr.value();
                    vtx["offset"] = ptr;
                    vtx["count"] = nvtx;
                    vtx["symbol"] = output;
                    auto result = factory->parse(rom, vtx);
                    if(result.has_value()){
                        Companion::Instance->RegisterAsset(output, vtx);
                    }
                }
                break;
            }
        }
    }

    return std::make_shared<DListData>(gfxs);
}