summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorMegaMech <MegaMech@users.noreply.github.com>2024-03-08 13:24:58 -0700
committerGitHub <noreply@github.com>2024-03-08 14:24:58 -0600
commitf06ed7388df76f09d8fbda765acea5d2923553d4 (patch)
treedfb2d060b88a3d6fd62178a669307c2aa9973b10 /src/utils
parentcfd2ec35ce03a36c10b005b952f765f035c8b836 (diff)
Detect Compression Type and Improve Yaml Structure (#21)
* Auto detect mio0 * Altered decompressor * Fix bug * Bug fix * fix * debugging * fix debug * Remove logging * Fix spacing * node change * Cleanup * Track compression offset using global var * Implementation * Renames * Simplification * Fixes * Fix debugging output * yaml config segments uses array style * Fix to_hex zero fill * Bug fixes --------- Co-authored-by: = <=>
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/Decompressor.cpp81
-rw-r--r--src/utils/Decompressor.h6
-rw-r--r--src/utils/TorchUtils.cpp2
-rw-r--r--src/utils/TorchUtils.h2
4 files changed, 39 insertions, 52 deletions
diff --git a/src/utils/Decompressor.cpp b/src/utils/Decompressor.cpp
index b4ca7a4..6d41910 100644
--- a/src/utils/Decompressor.cpp
+++ b/src/utils/Decompressor.cpp
@@ -36,46 +36,53 @@ DataChunk* Decompressor::Decode(const std::vector<uint8_t>& buffer, const uint32
}
DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer, std::optional<size_t> manualSize) {
+
if(!node["offset"]){
throw std::runtime_error("Failed to find offset");
}
- auto offset = node["offset"].as<uint32_t>();
-
- if(node["mio0"]){
- const auto mio0 = node["mio0"].as<uint32_t>();
-
- offset = TranslateAddr(offset, IS_SEGMENTED(offset));
+ auto offset = node["offset"].as<uint32_t>();
- auto decoded = Decode(buffer, offset, CompressionType::MIO0);
- auto size = node["size"] ? node["size"].as<size_t>() : manualSize.value_or(decoded->size - mio0);
-
- return {
- .root = decoded,
- .segment = { decoded->data + mio0, size }
- };
- }
+ CompressionType type = Companion::Instance->GetCurrCompressionType();
- if(node["yay0"]){
- throw std::runtime_error("Yay0 not implemented");
- }
+ switch(type) {
+ case CompressionType::MIO0:
+ {
+ auto fileOffset = TranslateAddr(offset, true);
+ offset = IS_SEGMENTED(offset) ? SEGMENT_OFFSET(offset) : offset;
- if(node["yaz0"]){
- throw std::runtime_error("Yaz0 not implemented");
+ 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.");
+ break;
+ case CompressionType::YAZ0:
+ throw std::runtime_error("Found compressed yaz0 segment.\nDecompression of yaz0 has not been implemented yet.");
+ break;
+ case CompressionType::None:
+ {
+ 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 }
+ };
+ }
}
- offset = TranslateAddr(offset);
-
- return {
- .root = nullptr,
- .segment = { buffer.data() + offset, node["size"] ? node["size"].as<size_t>() : manualSize.value_or(buffer.size() - offset) }
- };
+ 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.");
}
uint32_t Decompressor::TranslateAddr(uint32_t addr, bool baseAddress){
if(IS_SEGMENTED(addr)){
- const auto segment = Companion::Instance->GetSegmentedAddr(SEGMENT_NUMBER(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;
@@ -89,7 +96,7 @@ uint32_t Decompressor::TranslateAddr(uint32_t addr, bool baseAddress){
bool Decompressor::IsSegmented(uint32_t addr) {
if(IS_SEGMENTED(addr)){
- const auto segment = Companion::Instance->GetSegmentedAddr(SEGMENT_NUMBER(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));
@@ -102,24 +109,6 @@ bool Decompressor::IsSegmented(uint32_t addr) {
return false;
}
-bool Decompressor::IsCompressed(YAML::Node& node) {
- return node["mio0"] || node["yay0"] || node["yaz0"];
-}
-
-void Decompressor::CopyCompression(YAML::Node& from, YAML::Node& to){
- if(from["mio0"]){
- to["mio0"] = from["mio0"];
- }
-
- if(from["yay0"]){
- to["yay0"] = from["yay0"];
- }
-
- if(from["yaz0"]){
- to["yaz0"] = from["yaz0"];
- }
-}
-
void Decompressor::ClearCache() {
for(auto& [key, value] : gCachedChunks){
delete value->data;
diff --git a/src/utils/Decompressor.h b/src/utils/Decompressor.h
index 718b726..4e84cde 100644
--- a/src/utils/Decompressor.h
+++ b/src/utils/Decompressor.h
@@ -9,8 +9,8 @@
enum class CompressionType {
MIO0,
- Yay0,
- Yaz0,
+ YAY0,
+ YAZ0,
None,
};
@@ -31,8 +31,6 @@ public:
static DecompressedData AutoDecode(YAML::Node& node, std::vector<uint8_t>& buffer, std::optional<size_t> size = std::nullopt);
static uint32_t TranslateAddr(uint32_t addr, bool baseAddress = false);
static bool IsSegmented(uint32_t addr);
- static bool IsCompressed(YAML::Node& node);
- static void CopyCompression(YAML::Node& from, YAML::Node& to);
static void ClearCache();
}; \ No newline at end of file
diff --git a/src/utils/TorchUtils.cpp b/src/utils/TorchUtils.cpp
index d63fa0b..5dc4621 100644
--- a/src/utils/TorchUtils.cpp
+++ b/src/utils/TorchUtils.cpp
@@ -7,7 +7,7 @@
uint32_t Torch::translate(const uint32_t offset) {
if(SEGMENT_NUMBER(offset) > 0x01) {
auto segment = SEGMENT_NUMBER(offset);
- const auto addr = Companion::Instance->GetSegmentedAddr(segment);
+ const auto addr = Companion::Instance->GetFileOffsetFromSegmentedAddr(segment);
if(!addr.has_value()) {
SPDLOG_ERROR("Segment data missing from game config\nPlease add an entry for segment {}", segment);
throw std::runtime_error("Failed to find offset");
diff --git a/src/utils/TorchUtils.h b/src/utils/TorchUtils.h
index 36ce46d..271ab98 100644
--- a/src/utils/TorchUtils.h
+++ b/src/utils/TorchUtils.h
@@ -14,7 +14,7 @@ std::string to_hex(T number, const bool append0x = true) {
if(append0x) {
stream << "0x";
}
- stream << std::setfill ('0') << std::setw(sizeof(T)*2)
+ stream << std::setfill ('0')
<< std::hex << number;
auto format = stream.str();