Refactor Hyrule Magic compression functions to return std::vector<uint8_t> instead of raw pointers for improved memory management

This commit is contained in:
scawful
2024-12-31 18:32:25 -05:00
parent 308eb4b287
commit a54143cbab
3 changed files with 31 additions and 48 deletions

View File

@@ -14,9 +14,9 @@
namespace yaze { namespace yaze {
namespace gfx { namespace gfx {
// Hyrule Magic std::vector<uint8_t> HyruleMagicCompress(uint8_t const* const src,
uint8_t* HyruleMagicCompress(uint8_t const* const src, int const oldsize, int const oldsize, int* const size,
int* const size, int const flag) { int const flag) {
unsigned char* b2 = unsigned char* b2 =
(unsigned char*)malloc(0x1000); // allocate a 2^12 sized buffer (unsigned char*)malloc(0x1000); // allocate a 2^12 sized buffer
@@ -158,11 +158,13 @@ uint8_t* HyruleMagicCompress(uint8_t const* const src, int const oldsize,
b2 = (unsigned char*)realloc(b2, bd); b2 = (unsigned char*)realloc(b2, bd);
*size = bd; *size = bd;
return b2; std::vector<uint8_t> compressed_data(b2, b2 + bd);
free(b2);
return compressed_data;
} }
uint8_t* HyruleMagicDecompress(uint8_t const* src, int* const size, std::vector<uint8_t> HyruleMagicDecompress(uint8_t const* src, int* const size,
int const p_big_endian) { int const p_big_endian) {
unsigned char* b2 = (unsigned char*)malloc(1024); unsigned char* b2 = (unsigned char*)malloc(1024);
int bd = 0, bs = 1024; int bd = 0, bs = 1024;
@@ -291,7 +293,9 @@ uint8_t* HyruleMagicDecompress(uint8_t const* src, int* const size,
if (size) (*size) = bd; if (size) (*size) = bd;
// return the unsigned char* buffer b2, which contains the uncompressed data. // return the unsigned char* buffer b2, which contains the uncompressed data.
return b2; std::vector<uint8_t> decompressed_data(b2, b2 + bd);
free(b2);
return decompressed_data;
} }
namespace lc_lz2 { namespace lc_lz2 {

View File

@@ -3,6 +3,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector>
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
@@ -13,12 +14,12 @@
namespace yaze { namespace yaze {
namespace gfx { namespace gfx {
// Hyrule Magic std::vector<uint8_t> HyruleMagicCompress(uint8_t const* const src,
uint8_t* HyruleMagicCompress(uint8_t const* const src, int const oldsize, int const oldsize, int* const size,
int* const size, int const flag); int const flag);
uint8_t* HyruleMagicDecompress(uint8_t const* src, int* const size, std::vector<uint8_t> HyruleMagicDecompress(uint8_t const* src, int* const size,
int const p_big_endian); int const p_big_endian);
/** /**
* @namespace yaze::gfx::lc_lz2 * @namespace yaze::gfx::lc_lz2

View File

@@ -215,8 +215,11 @@ absl::Status Overworld::DecompressAllMapTiles() {
return core::SnesToPc(p); return core::SnesToPc(p);
}; };
uint32_t lowest = 0x0FFFFF; constexpr uint32_t kBaseLowest = 0x0FFFFF;
uint32_t highest = 0x0F8000; constexpr uint32_t kBaseHighest = 0x0F8000;
uint32_t lowest = kBaseLowest;
uint32_t highest = kBaseHighest;
int sx = 0; int sx = 0;
int sy = 0; int sy = 0;
int c = 0; int c = 0;
@@ -231,24 +234,12 @@ absl::Status Overworld::DecompressAllMapTiles() {
if (p1 >= highest) highest = p1; if (p1 >= highest) highest = p1;
if (p2 >= highest) highest = p2; if (p2 >= highest) highest = p2;
if (p1 <= lowest && p1 > 0x0F8000) lowest = p1; if (p1 <= lowest && p1 > kBaseHighest) lowest = p1;
if (p2 <= lowest && p2 > 0x0F8000) lowest = p2; if (p2 <= lowest && p2 > kBaseHighest) lowest = p2;
std::vector<uint8_t> bytes, bytes2;
int size1, size2; int size1, size2;
auto decomp = gfx::lc_lz2::Uncompress(rom()->data() + p2, &size1, 1); auto bytes = gfx::HyruleMagicDecompress(rom()->data() + p2, &size1, 1);
bytes.resize(size1); auto bytes2 = gfx::HyruleMagicDecompress(rom()->data() + p1, &size2, 1);
for (int j = 0; j < size1; j++) {
bytes[j] = decomp[j];
}
free(decomp);
decomp = gfx::lc_lz2::Uncompress(rom()->data() + p1, &size2, 1);
bytes2.resize(size2);
for (int j = 0; j < size2; j++) {
bytes2[j] = decomp[j];
}
free(decomp);
OrganizeMapTiles(bytes, bytes2, i, sx, sy, ttpos); OrganizeMapTiles(bytes, bytes2, i, sx, sy, ttpos);
sx++; sx++;
@@ -539,26 +530,13 @@ absl::Status Overworld::SaveOverworldMaps() {
} }
} }
std::vector<uint8_t> a, b;
int size_a, size_b; int size_a, size_b;
// Compress single_map_1 and single_map_2 // Compress single_map_1 and single_map_2
auto a_char = auto a = gfx::HyruleMagicCompress(single_map_1.data(), 256, &size_a, 1);
gfx::HyruleMagicCompress(single_map_1.data(), 256, &size_a, 1); auto b = gfx::HyruleMagicCompress(single_map_2.data(), 256, &size_b, 1);
auto b_char = if (a.empty() || b.empty()) {
gfx::HyruleMagicCompress(single_map_2.data(), 256, &size_b, 1);
if (a_char == nullptr || b_char == nullptr) {
return absl::AbortedError("Error compressing map gfx."); return absl::AbortedError("Error compressing map gfx.");
} }
// Copy the compressed data to a and b
a.resize(size_a);
b.resize(size_b);
// Copy the arrays manually
for (int k = 0; k < size_a; k++) {
a[k] = a_char[k];
}
for (int k = 0; k < size_b; k++) {
b[k] = b_char[k];
}
// Save compressed data and pointers // Save compressed data and pointers
map_data_p1[i] = std::vector<uint8_t>(size_a); map_data_p1[i] = std::vector<uint8_t>(size_a);
@@ -574,8 +552,8 @@ absl::Status Overworld::SaveOverworldMaps() {
pos = kOverworldMapDataOverflow; // 0x0F8780; pos = kOverworldMapDataOverflow; // 0x0F8780;
} }
auto compare_array = [](const std::vector<uint8_t> &array1, const auto compare_array = [](const std::vector<uint8_t> &array1,
const std::vector<uint8_t> &array2) -> bool { const std::vector<uint8_t> &array2) -> bool {
if (array1.size() != array2.size()) { if (array1.size() != array2.size()) {
return false; return false;
} }