From 5d0a282ba4e7d6ea816478e8037d0dc0e93f5eac Mon Sep 17 00:00:00 2001 From: MegaMech Date: Mon, 29 Apr 2024 21:35:29 -0600 Subject: Decompress every asset --- src/utils/Decompressor.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'src/utils') diff --git a/src/utils/Decompressor.cpp b/src/utils/Decompressor.cpp index 1dd326e..8b7347c 100644 --- a/src/utils/Decompressor.cpp +++ b/src/utils/Decompressor.cpp @@ -18,6 +18,7 @@ DataChunk* Decompressor::Decode(const std::vector& buffer, const uint32 const unsigned char* in_buf = buffer.data() + offset; + SPDLOG_INFO("Offset: 0x{:X}", offset); switch (type) { case CompressionType::MIO0: { mio0_header_t head; @@ -36,15 +37,36 @@ DataChunk* Decompressor::Decode(const std::vector& buffer, const uint32 } DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector& buffer, std::optional manualSize) { + auto offset = GetSafeNode(node, "offset"); - if(!node["offset"]){ - throw std::runtime_error("Failed to find offset"); - } + CompressionType type = Companion::Instance->GetCurrCompressionType(); - auto offset = node["offset"].as(); - 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() : manualSize.value_or(decoded->size); + return { + .root = decoded, + .segment = { decoded->data, size } + }; + } + } + // Extract compressed files switch(type) { case CompressionType::MIO0: { @@ -62,7 +84,7 @@ DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector 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: + case CompressionType::None: // The data does not have compression { auto fileOffset = TranslateAddr(offset); -- cgit v1.2.3