Refactor Hyrule Magic compression functions to return std::vector<uint8_t> instead of raw pointers for improved memory management
This commit is contained in:
@@ -14,9 +14,9 @@
|
||||
namespace yaze {
|
||||
namespace gfx {
|
||||
|
||||
// Hyrule Magic
|
||||
uint8_t* HyruleMagicCompress(uint8_t const* const src, int const oldsize,
|
||||
int* const size, int const flag) {
|
||||
std::vector<uint8_t> HyruleMagicCompress(uint8_t const* const src,
|
||||
int const oldsize, int* const size,
|
||||
int const flag) {
|
||||
unsigned char* b2 =
|
||||
(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);
|
||||
*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,
|
||||
int const p_big_endian) {
|
||||
std::vector<uint8_t> HyruleMagicDecompress(uint8_t const* src, int* const size,
|
||||
int const p_big_endian) {
|
||||
unsigned char* b2 = (unsigned char*)malloc(1024);
|
||||
|
||||
int bd = 0, bs = 1024;
|
||||
@@ -291,7 +293,9 @@ uint8_t* HyruleMagicDecompress(uint8_t const* src, int* const size,
|
||||
if (size) (*size) = bd;
|
||||
|
||||
// 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 {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
@@ -13,12 +14,12 @@
|
||||
namespace yaze {
|
||||
namespace gfx {
|
||||
|
||||
// Hyrule Magic
|
||||
uint8_t* HyruleMagicCompress(uint8_t const* const src, int const oldsize,
|
||||
int* const size, int const flag);
|
||||
std::vector<uint8_t> HyruleMagicCompress(uint8_t const* const src,
|
||||
int const oldsize, int* const size,
|
||||
int const flag);
|
||||
|
||||
uint8_t* HyruleMagicDecompress(uint8_t const* src, int* const size,
|
||||
int const p_big_endian);
|
||||
std::vector<uint8_t> HyruleMagicDecompress(uint8_t const* src, int* const size,
|
||||
int const p_big_endian);
|
||||
|
||||
/**
|
||||
* @namespace yaze::gfx::lc_lz2
|
||||
|
||||
@@ -215,8 +215,11 @@ absl::Status Overworld::DecompressAllMapTiles() {
|
||||
return core::SnesToPc(p);
|
||||
};
|
||||
|
||||
uint32_t lowest = 0x0FFFFF;
|
||||
uint32_t highest = 0x0F8000;
|
||||
constexpr uint32_t kBaseLowest = 0x0FFFFF;
|
||||
constexpr uint32_t kBaseHighest = 0x0F8000;
|
||||
|
||||
uint32_t lowest = kBaseLowest;
|
||||
uint32_t highest = kBaseHighest;
|
||||
int sx = 0;
|
||||
int sy = 0;
|
||||
int c = 0;
|
||||
@@ -231,24 +234,12 @@ absl::Status Overworld::DecompressAllMapTiles() {
|
||||
if (p1 >= highest) highest = p1;
|
||||
if (p2 >= highest) highest = p2;
|
||||
|
||||
if (p1 <= lowest && p1 > 0x0F8000) lowest = p1;
|
||||
if (p2 <= lowest && p2 > 0x0F8000) lowest = p2;
|
||||
if (p1 <= lowest && p1 > kBaseHighest) lowest = p1;
|
||||
if (p2 <= lowest && p2 > kBaseHighest) lowest = p2;
|
||||
|
||||
std::vector<uint8_t> bytes, bytes2;
|
||||
int size1, size2;
|
||||
auto decomp = gfx::lc_lz2::Uncompress(rom()->data() + p2, &size1, 1);
|
||||
bytes.resize(size1);
|
||||
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);
|
||||
|
||||
auto bytes = gfx::HyruleMagicDecompress(rom()->data() + p2, &size1, 1);
|
||||
auto bytes2 = gfx::HyruleMagicDecompress(rom()->data() + p1, &size2, 1);
|
||||
OrganizeMapTiles(bytes, bytes2, i, sx, sy, ttpos);
|
||||
|
||||
sx++;
|
||||
@@ -539,26 +530,13 @@ absl::Status Overworld::SaveOverworldMaps() {
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> a, b;
|
||||
int size_a, size_b;
|
||||
// Compress single_map_1 and single_map_2
|
||||
auto a_char =
|
||||
gfx::HyruleMagicCompress(single_map_1.data(), 256, &size_a, 1);
|
||||
auto b_char =
|
||||
gfx::HyruleMagicCompress(single_map_2.data(), 256, &size_b, 1);
|
||||
if (a_char == nullptr || b_char == nullptr) {
|
||||
auto a = gfx::HyruleMagicCompress(single_map_1.data(), 256, &size_a, 1);
|
||||
auto b = gfx::HyruleMagicCompress(single_map_2.data(), 256, &size_b, 1);
|
||||
if (a.empty() || b.empty()) {
|
||||
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
|
||||
map_data_p1[i] = std::vector<uint8_t>(size_a);
|
||||
@@ -574,8 +552,8 @@ absl::Status Overworld::SaveOverworldMaps() {
|
||||
pos = kOverworldMapDataOverflow; // 0x0F8780;
|
||||
}
|
||||
|
||||
auto compare_array = [](const std::vector<uint8_t> &array1,
|
||||
const std::vector<uint8_t> &array2) -> bool {
|
||||
const auto compare_array = [](const std::vector<uint8_t> &array1,
|
||||
const std::vector<uint8_t> &array2) -> bool {
|
||||
if (array1.size() != array2.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user