summaryrefslogtreecommitdiff
path: root/src/utils/Decompressor.cpp
blob: c591f6c096f8b9410d29e5c9911e9eb61148432e (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
378
#include "Decompressor.h"
#include "TorchUtils.h"

#include <stdexcept>
#include <mutex>
#include "spdlog/spdlog.h"
#include <Companion.h>

extern "C" {
#include <libmio0/mio0.h>
#include <libyay0/yay0.h>
#include <libyay0/yay1.h>
#include <libmio0/tkmk00.h>
}

#include <bk_zip/bk_unzip.h>

std::unordered_map<uint32_t, DataChunk*> gCachedChunks;
std::mutex gDecompCacheMutex;

DataChunk* Decompressor::Decode(const std::vector<uint8_t>& buffer, const uint32_t offset, const CompressionType type,
                                const uint32_t in_size, bool ignoreCache) {

    {
        std::lock_guard<std::mutex> lock(gDecompCacheMutex);
        if (!ignoreCache && Torch::contains(gCachedChunks, offset)) {
            return gCachedChunks[offset];
        }
    }

    const unsigned char* in_buf = buffer.data() + offset;

    switch (type) {
        case CompressionType::MIO0: {
            mio0_header_t head;
            if (!mio0_decode_header(in_buf, &head)) {
                throw std::runtime_error("Failed to decode MIO0 header");
            }

            const auto decompressed = new uint8_t[head.dest_size];
            mio0_decode(in_buf, decompressed, nullptr);
            {
                std::lock_guard<std::mutex> lock(gDecompCacheMutex);
                gCachedChunks[offset] = new DataChunk{ decompressed, head.dest_size };
                return gCachedChunks[offset];
            }
        }
        case CompressionType::YAY0: {
            uint32_t size = 0;
            uint8_t* decompressed = yay0_decode(in_buf, &size);

            if (!decompressed) {
                throw std::runtime_error("Failed to decode YAY0");
            }

            {
                std::lock_guard<std::mutex> lock(gDecompCacheMutex);
                gCachedChunks[offset] = new DataChunk{ decompressed, size };
                return gCachedChunks[offset];
            }
        }
        case CompressionType::YAY1: {
            uint32_t size = 0;
            uint8_t* decompressed = yay1_decode(in_buf, &size);

            if (!decompressed) {
                throw std::runtime_error("Failed to decode YAY1");
            }

            {
                std::lock_guard<std::mutex> lock(gDecompCacheMutex);
                gCachedChunks[offset] = new DataChunk{ decompressed, size };
                return gCachedChunks[offset];
            }
        }
        case CompressionType::BKZIP: {
            uint32_t size = in_size;
            try {
                uint8_t* decompressed = BK64::bk_unzip(in_buf, &size);

                if (!decompressed) {
                    throw std::runtime_error("bk_unzip returned null");
                }

                {
                    std::lock_guard<std::mutex> lock(gDecompCacheMutex);
                    gCachedChunks[offset] = new DataChunk{ decompressed, size };
                    return gCachedChunks[offset];
                }
            } catch (const std::exception& e) {
                throw std::runtime_error(std::string(e.what()) + " (ROM offset 0x" + Torch::to_hex(offset, false) +
                                         " compressed size 0x" + Torch::to_hex(in_size, false) + ")");
            }
        }
        default:
            throw std::runtime_error("Unknown compression type");
    }
}

DataChunk* Decompressor::DecodeTKMK00(const std::vector<uint8_t>& buffer, const uint32_t offset, const uint32_t size,
                                      const uint32_t alpha) {
    {
        std::lock_guard<std::mutex> lock(gDecompCacheMutex);
        if (Torch::contains(gCachedChunks, offset)) {
            return gCachedChunks[offset];
        }
    }

    const uint8_t* in_buf = buffer.data() + offset;

    const auto decompressed = new uint8_t[size];
    const auto rgba = new uint8_t[size];
    tkmk00_decode(in_buf, decompressed, rgba, alpha);
    {
        std::lock_guard<std::mutex> lock(gDecompCacheMutex);
        gCachedChunks[offset] = new DataChunk{ rgba, size };
        return gCachedChunks[offset];
    }
}

DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer,
                                          std::optional<size_t> manualSize) {
    auto offset = GetSafeNode<uint32_t>(node, "offset");

    CompressionType type = Companion::Instance->GetCurrCompressionType();

    auto fileOffset = TranslateAddr(offset, true);
    auto offsetFromFile = TranslateAddr(offset, false) - fileOffset;

    // Check if an asset in a yaml file is mio0 compressed and extract.
    if (node["mio0"]) {
        auto assetPtr = ASSET_PTR(offset);
        auto gameSize = Companion::Instance->GetRomData().size();

        auto decoded = Decode(buffer, fileOffset + offsetFromFile, CompressionType::MIO0);
        size_t decodedSize = decoded->size - offset;
        size_t size;

        if (node["size"]) {
            size = node["size"].as<size_t>();
        } else if (manualSize.has_value()) {
            size = manualSize.value();
        } else {
            size = decodedSize;
        }

        if (size > decodedSize) {
            SPDLOG_WARN("Requested size 0x{:X} exceeds decoded MIO0 asset size 0x{:X} at offset 0x{:X}. Reducing to "
                        "available size.",
                        size, decodedSize, assetPtr);
            size = decodedSize;
        }

        return { .root = decoded, .segment = { decoded->data, size } };
    }

    // Check if an asset in a yaml file is tkmk00 compressed and extract (mk64).
    if (node["tkmk00"]) {
        const auto alpha = GetSafeNode<uint32_t>(node, "alpha");
        const auto width = GetSafeNode<uint32_t>(node, "width");
        const auto height = GetSafeNode<uint32_t>(node, "height");
        const auto textureSize = width * height * 2;

        auto fileOffset = TranslateAddr(offset, true);
        offset = ASSET_PTR(offset);

        auto assetPtr = fileOffset + offset;
        auto decoded = DecodeTKMK00(buffer, assetPtr, textureSize, alpha);
        size_t decodedSize = decoded->size - offset;
        size_t size;

        if (node["size"]) {
            size = node["size"].as<size_t>();
        } else if (manualSize.has_value()) {
            size = manualSize.value();
        } else {
            size = decodedSize;
        }

        if (size > decodedSize) {
            SPDLOG_WARN("Requested size 0x{:X} exceeds decoded TKMK00 asset size 0x{:X} at offset 0x{:X}. Reducing to "
                        "available size.",
                        size, decodedSize, assetPtr);
            size = decodedSize;
        }
        return { .root = decoded, .segment = { decoded->data, size } };
    }
    if (node["bkzip"]) {
        const auto compressedSize = GetSafeNode<uint32_t>(node, "compressed_size");
        auto decoded = Decode(buffer, fileOffset + offsetFromFile, CompressionType::BKZIP, compressedSize);
        auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(decoded->size);
        return { .root = decoded, .segment = { decoded->data, size } };
    }

    // Extract a compressed file which contains many assets.
    switch (type) {
        case CompressionType::YAY0:
        case CompressionType::YAY1:
        case CompressionType::MIO0: {
            offset = ASSET_PTR(offset);

            auto decoded = Decode(buffer, fileOffset, type);
            auto availableSize = decoded->size - offset;
            size_t size;

            if (node["size"]) {
                size = node["size"].as<size_t>();
            } else if (manualSize.has_value()) {
                size = manualSize.value();
            } else {
                size = availableSize;
            }

            if (size > availableSize) {
                SPDLOG_WARN("Requested size 0x{:X} exceeds decoded asset size 0x{:X} at offset 0x{:X}. Reducing to "
                            "available size.",
                            size, availableSize, fileOffset);
                size = availableSize;
            }

            return { .root = decoded, .segment = { decoded->data + offset, size } };
        }
        case CompressionType::BKZIP: {
            const auto sizeEntry = Companion::Instance->GetCurrentCompressedSize();

            if (!sizeEntry.has_value()) {
                throw std::runtime_error("Auto decode missing file compressed size.");
            }

            auto decoded = Decode(buffer, fileOffset, type, sizeEntry.value());
            // Rom data can reference offsets past the decoded file; clamp instead of
            // letting decoded->size - offsetFromFile underflow into a giant bogus segment.
            if (offsetFromFile > decoded->size) {
                SPDLOG_WARN("AutoDecode BKZIP: offset 0x{:X} lies past decoded size 0x{:X}; clamping to empty segment",
                            offsetFromFile, decoded->size);
                offsetFromFile = decoded->size;
            }
            auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(decoded->size - offsetFromFile);
            if (size > decoded->size - offsetFromFile) {
                SPDLOG_WARN("AutoDecode BKZIP: requested size 0x{:X} exceeds available 0x{:X}; reducing", size,
                            decoded->size - offsetFromFile);
                size = decoded->size - offsetFromFile;
            }
            SPDLOG_INFO("AutoDecode BKZIP: offset=0x{:X} fileOffset=0x{:X} offsetFromFile=0x{:X} compSize=0x{:X} "
                        "decodedSize=0x{:X} segSize=0x{:X}",
                        offset, fileOffset, offsetFromFile, sizeEntry.value(), decoded->size, size);
            return { .root = decoded, .segment = { decoded->data + offsetFromFile, size } };
        }
        case CompressionType::YAZ0:
            throw std::runtime_error(
                "Found compressed yaz0 segment.\nDecompression of yaz0 has not been implemented yet.");
        case CompressionType::None: // The data does not have compression
        {
            fileOffset = TranslateAddr(offset, false);

            // An offset past the file must not underflow availableSize.
            if (fileOffset > buffer.size()) {
                SPDLOG_WARN("AutoDecode: offset 0x{:X} lies past file size 0x{:X}; clamping to empty segment",
                            fileOffset, buffer.size());
                fileOffset = buffer.size();
            }

            auto availableSize = buffer.size() - fileOffset;
            size_t size;

            if (node["size"]) {
                size = node["size"].as<size_t>();
            } else if (manualSize.has_value()) {
                size = manualSize.value();
            } else {
                size = availableSize;
            }

            if (size > availableSize) {
                SPDLOG_WARN("Requested size 0x{:X} exceeds available asset size 0x{:X} at offset 0x{:X}. Reducing to "
                            "available size.",
                            size, availableSize, fileOffset);
                size = availableSize;
            }

            return { .root = nullptr, .segment = { buffer.data() + fileOffset, size } };
        }
        default:
            throw std::runtime_error("Auto decode could not find a supported compression type.");
    }
}

DecompressedData Decompressor::AutoDecode(uint32_t offset, std::optional<size_t> size, std::vector<uint8_t>& buffer) {
    YAML::Node node;
    node["offset"] = offset;

    return AutoDecode(node, buffer, size);
}

uint32_t Decompressor::TranslateAddr(uint32_t addr, bool baseAddress) {
    if (IS_SEGMENTED(addr)) {
        const auto segment = Companion::Instance->GetFileOffsetFromSegmentedAddr(SEGMENT_NUMBER(addr));
        if (!segment.has_value()) {
            const auto compressedSegmentPair =
                Companion::Instance->GetFileOffsetFromCompressedSegmentedAddr(SEGMENT_NUMBER(addr));
            if (!compressedSegmentPair.has_value()) {
                SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}",
                             SEGMENT_NUMBER(addr));
                return 0;
            }
            return compressedSegmentPair.value().first +
                   (!baseAddress ? (compressedSegmentPair.value().second + SEGMENT_OFFSET(addr)) : 0);
        }

        return segment.value() + (!baseAddress ? SEGMENT_OFFSET(addr) : 0);
    }

    const auto vramEntry = Companion::Instance->GetCurrentVRAM();

    if (vramEntry.has_value()) {
        const auto vram = vramEntry.value();

        if (addr >= vram.addr) {
            return vram.offset + (!baseAddress ? (addr - vram.addr) : 0);
        }
    }

    return addr;
}

CompressionType Decompressor::GetCompressionType(std::vector<uint8_t>& buffer, const uint32_t offset) {
    if (offset) {
        LUS::BinaryReader reader((char*)buffer.data() + offset, sizeof(uint32_t));
        reader.SetEndianness(Torch::Endianness::Big);

        const std::string header = reader.ReadCString();

        // Check if a compressed header exists
        if (header == "MIO0") {
            return CompressionType::MIO0;
        }

        if (header == "Yay0" || header == "PERS") {
            return CompressionType::YAY0;
        }

        if (header == "Yay1") {
            return CompressionType::YAY1;
        }

        if (header == "Yaz0") {
            return CompressionType::YAZ0;
        }
    }
    return CompressionType::None;
}

bool Decompressor::IsSegmented(uint32_t addr) {
    if (IS_SEGMENTED(addr)) {
        const auto segment = Companion::Instance->GetFileOffsetFromSegmentedAddr(SEGMENT_NUMBER(addr));

        if (!segment.has_value()) {
            const auto compressedSegmentPair =
                Companion::Instance->GetFileOffsetFromCompressedSegmentedAddr(SEGMENT_NUMBER(addr));
            if (!compressedSegmentPair.has_value()) {
                SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}",
                             SEGMENT_NUMBER(addr));
                return false;
            }
        }
        return true;
    }

    return false;
}

void Decompressor::ClearCache() {
    std::lock_guard<std::mutex> lock(gDecompCacheMutex);
    for (auto& [key, value] : gCachedChunks) {
        delete[] value->data;
    }
    gCachedChunks.clear();
}