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
|
#include "Decompressor.h"
#include <stdexcept>
#include "spdlog/spdlog.h"
#include <Companion.h>
extern "C" {
#include <libmio0/mio0.h>
}
std::unordered_map<uint32_t, DataChunk*> gCachedChunks;
DataChunk* Decompressor::Decode(const std::vector<uint8_t>& buffer, const uint32_t offset, const CompressionType type) {
if(gCachedChunks.contains(offset)){
return gCachedChunks[offset];
}
const unsigned char* in_buf = buffer.data() + offset;
SPDLOG_INFO("Offset: 0x{:X}", 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);
gCachedChunks[offset] = new DataChunk{ decompressed, head.dest_size };
return gCachedChunks[offset];
}
default:
throw std::runtime_error("Unknown compression type");
}
}
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();
// Extract compressed assets. Do not run if the offset is 0x0 as that is handled in the below switch statement.
if (offset != 0x0) {
auto fileOffset = TranslateAddr(offset, true);
auto assetPtr = ASSET_PTR(offset);
auto gameSize = Companion::Instance->GetRomData().size();
LUS::BinaryReader reader((char*)buffer.data(), gameSize);
reader.SetEndianness(LUS::Endianness::Big);
reader.Seek(fileOffset + assetPtr, LUS::SeekOffsetType::Start);
std::string mio0 = reader.ReadCString();
if (mio0.compare("MIO0")) {
auto fileOffset = TranslateAddr(offset, true);
offset = ASSET_PTR(offset);
auto decoded = Decode(buffer, fileOffset + offset, CompressionType::MIO0);
auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(decoded->size);
return {
.root = decoded,
.segment = { decoded->data, size }
};
}
}
// Extract compressed files
switch(type) {
case CompressionType::MIO0:
{
auto fileOffset = TranslateAddr(offset, true);
offset = ASSET_PTR(offset);
auto decoded = Decode(buffer, fileOffset, CompressionType::MIO0);
auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(decoded->size - offset);
return {
.root = decoded,
.segment = { decoded->data + offset, size }
};
}
case CompressionType::YAY0:
throw std::runtime_error("Found compressed yay0 segment.\nDecompression of yay0 has not been implemented yet.");
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
{
auto fileOffset = TranslateAddr(offset);
auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(buffer.size() - fileOffset);
return {
.root = nullptr,
.segment = { buffer.data() + fileOffset, size }
};
}
}
throw std::runtime_error("Auto decode could not find a compression type nor uncompressed segment.\nThis is one of those issues that should never really happen.");
}
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()) {
SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}", SEGMENT_NUMBER(addr));
return 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 + (addr - vram.addr);
}
}
return addr;
}
bool Decompressor::IsSegmented(uint32_t addr) {
if(IS_SEGMENTED(addr)){
const auto segment = Companion::Instance->GetFileOffsetFromSegmentedAddr(SEGMENT_NUMBER(addr));
if(!segment.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() {
for(auto& [key, value] : gCachedChunks){
delete value->data;
}
gCachedChunks.clear();
}
|