From 3eebd9d5aaeef4adfa62d95cd6f4f60b917a48c2 Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Tue, 2 Apr 2024 08:32:39 -0600 Subject: Removed header, values and indices with just offset --- src/factories/sm64/AnimationFactory.cpp | 23 ++++++++++++----------- src/utils/Decompressor.cpp | 7 +++++++ src/utils/Decompressor.h | 6 ++++++ 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/factories/sm64/AnimationFactory.cpp b/src/factories/sm64/AnimationFactory.cpp index 1b4070d..690263d 100644 --- a/src/factories/sm64/AnimationFactory.cpp +++ b/src/factories/sm64/AnimationFactory.cpp @@ -1,5 +1,8 @@ #include "AnimationFactory.h" #include "spdlog/spdlog.h" +#include + +#define ANIMINDEX_COUNT(boneCount) (sizeof(boneCount) + 1) * 6 * sizeof(uint16_t) ExportResult SM64::AnimationBinaryExporter::Export(std::ostream &write, std::shared_ptr raw, std::string& entryName, YAML::Node &node, std::string* replacement ) { auto writer = LUS::BinaryWriter(); @@ -29,11 +32,9 @@ ExportResult SM64::AnimationBinaryExporter::Export(std::ostream &write, std::sha } std::optional> SM64::AnimationFactory::parse(std::vector& buffer, YAML::Node& node) { - auto headerNode = GetSafeNode(node, "header"); - auto valuesNode = GetSafeNode(node, "values"); - auto indexNode = GetSafeNode(node, "indices"); + auto offset = node["offset"]; - LUS::BinaryReader header(reinterpret_cast(buffer.data()) + GetSafeNode(headerNode, "offset"), GetSafeNode(headerNode, "size")); + LUS::BinaryReader header = Decompressor::AutoDecode(node, buffer).GetReader(); header.SetEndianness(LUS::Endianness::Big); auto flags = header.ReadInt16(); @@ -42,23 +43,23 @@ std::optional> SM64::AnimationFactory::parse(std::v auto loopStart = header.ReadInt16(); auto loopEnd = header.ReadInt16(); auto unusedBoneCount = header.ReadInt16(); - header.ReadUInt32(); - header.ReadUInt32(); + auto valuesAddr = header.ReadUInt32(); + auto indexAddr = header.ReadUInt32(); auto length = header.ReadUInt32(); - LUS::BinaryReader indices(reinterpret_cast(buffer.data()) + GetSafeNode(indexNode, "offset"), GetSafeNode(indexNode, "size")); + size_t indexLength = ANIMINDEX_COUNT(unusedBoneCount); + LUS::BinaryReader indices = Decompressor::AutoDecode(indexAddr, indexLength * sizeof(uint16_t), buffer).GetReader(); indices.SetEndianness(LUS::Endianness::Big); - size_t indexLength = GetSafeNode(indexNode, "size") / sizeof(int16_t); std::vector indicesData; for (size_t i = 0; i < indexLength; i++) { indicesData.push_back(indices.ReadInt16()); } - LUS::BinaryReader values(reinterpret_cast(buffer.data()) + GetSafeNode(valuesNode, "offset"), GetSafeNode(valuesNode, "size")); + size_t valuesSize = (indexAddr - valuesAddr); + LUS::BinaryReader values = Decompressor::AutoDecode(valuesAddr, valuesSize, buffer).GetReader(); values.SetEndianness(LUS::Endianness::Big); - size_t entries = GetSafeNode(valuesNode, "size") / sizeof(uint16_t); std::vector valuesData; - for (size_t i = 0; i < entries; i++) { + for (size_t i = 0; i < valuesSize / sizeof(uint16_t); i++) { valuesData.push_back(values.ReadUInt16()); } diff --git a/src/utils/Decompressor.cpp b/src/utils/Decompressor.cpp index 0fc806e..9570ab3 100644 --- a/src/utils/Decompressor.cpp +++ b/src/utils/Decompressor.cpp @@ -78,6 +78,13 @@ DecompressedData Decompressor::AutoDecode(YAML::Node& node, std::vector 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, std::vector& 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)); diff --git a/src/utils/Decompressor.h b/src/utils/Decompressor.h index 0cb5a01..d7671a4 100644 --- a/src/utils/Decompressor.h +++ b/src/utils/Decompressor.h @@ -6,6 +6,7 @@ #include #include #include +#include "BinaryReader.h" enum class CompressionType { None, @@ -23,12 +24,17 @@ struct DataChunk { struct DecompressedData { DataChunk* root; DataChunk segment; + + LUS::BinaryReader GetReader() { + return LUS::BinaryReader(reinterpret_cast(segment.data), segment.size); + } }; class Decompressor { public: static DataChunk* Decode(const std::vector& buffer, uint32_t offset, CompressionType type); static DecompressedData AutoDecode(YAML::Node& node, std::vector& buffer, std::optional size = std::nullopt); + static DecompressedData AutoDecode(uint32_t offset, std::optional size, std::vector& buffer); static uint32_t TranslateAddr(uint32_t addr, bool baseAddress = false); static bool IsSegmented(uint32_t addr); -- cgit v1.2.3