From f183f851ef256f09f40f7fbd2c3c17ed4dcbed86 Mon Sep 17 00:00:00 2001 From: julesgrc0 Date: Thu, 21 Mar 2024 10:16:24 +0100 Subject: [PATCH] add compression for terrain files --- src/stdafx.h | 1 + src/terrain/terrain_data.c | 77 ++++++++++++++++++++++++++++---------- src/unpack/unpack.h | 1 + 3 files changed, 60 insertions(+), 19 deletions(-) diff --git a/src/stdafx.h b/src/stdafx.h index 1613def..ab8536e 100644 --- a/src/stdafx.h +++ b/src/stdafx.h @@ -52,6 +52,7 @@ #define WISPY_LINUX #include +#include #include #elif defined(__ANDROID__) diff --git a/src/terrain/terrain_data.c b/src/terrain/terrain_data.c index fc24e71..26835ee 100644 --- a/src/terrain/terrain_data.c +++ b/src/terrain/terrain_data.c @@ -8,10 +8,10 @@ char *get_terrain_path_folder() { path[0] = 0; #if defined(WISPY_ANDROID) - strcat(path, GetAndroidApp()->activity->internalDataPath); - strcat(path, "/"); + strcat(path, GetAndroidApp()->activity->internalDataPath); + strcat(path, "/"); #else - strcat(path, GetApplicationDirectory()); + strcat(path, GetApplicationDirectory()); #endif strcat(path, TERRAIN_FOLDER); return path; @@ -32,7 +32,7 @@ char *get_terrain_path_chunk(unsigned int position) { return NULL; } - char position_str[12]; // log10(2^32) + 1 + 1 + char position_str[12]; // log10(2^32) + 1 (path) + 1 (end-of-string) sprintf(position_str, "/%u", position); strcat(path, position_str); @@ -49,22 +49,36 @@ bool read_chunk_file(unsigned int position, w_block *blocks) { return false; } - int size = 0; - char *data = LoadFileData(chunk_path, &size); - - if (size != CHUNK_W * CHUNK_H) { - UnloadFileData(data); + int in_size = 0; + char *in_buffer = LoadFileData(chunk_path, &in_size); + if (in_buffer == NULL) { + free(chunk_path); + return false; + } + size_t out_size = CHUNK_W * CHUNK_H; + char *out_buffer = malloc(out_size); + if (out_buffer == NULL) { + UnloadFileData(in_buffer); + free(chunk_path); + return false; + } + if (uncompress((Bytef *)out_buffer, (uLongf *)&out_size, + (const Bytef *)in_buffer, (uLong)in_size) != Z_OK) { + UnloadFileData(in_buffer); + free(out_buffer); + free(chunk_path); return false; } for (unsigned int i = 0; i < CHUNK_W * CHUNK_H; i++) { blocks[i] = (w_block){ - .is_background = BLOCK_IS_BACKGROUND(data[i]), - .type = BLOCK_TYPE(data[i]), + .is_background = BLOCK_IS_BACKGROUND(out_buffer[i]), + .type = BLOCK_TYPE(out_buffer[i]), }; } - UnloadFileData(data); + UnloadFileData(in_buffer); + free(out_buffer); free(chunk_path); return true; } @@ -75,23 +89,48 @@ bool write_chunk_file(unsigned int position, w_block *blocks) { return false; } - char *data = malloc(CHUNK_W * CHUNK_H); - if (data == NULL) { + size_t in_size = CHUNK_W * CHUNK_H; + char *in_buffer = malloc(in_size); + if (in_buffer == NULL) { free(chunk_path); return false; } - for (unsigned int i = 0; i < CHUNK_W * CHUNK_H; i++) { + size_t out_size = in_size; + char *out_buffer = malloc(out_size); + if (out_buffer == NULL) { + free(chunk_path); + free(in_buffer); + return false; + } + + for (unsigned int i = 0; i < in_size; i++) { if (blocks[i].is_background) { - data[i] = BLOCK_SET_BACKGROUND(data[i]); + in_buffer[i] = BLOCK_SET_BACKGROUND(in_buffer[i]); } else { - data[i] = blocks[i].type; + in_buffer[i] = blocks[i].type; } } - SaveFileData(chunk_path, data, CHUNK_W * CHUNK_H); + if (compress((Bytef *)out_buffer, (uLongf *)&out_size, + (const Bytef *)in_buffer, (uLong)in_size) != Z_OK) { + free(chunk_path); + free(in_buffer); + return false; + } + + void *tmp = realloc(out_buffer, out_size); + if (tmp == NULL) { + free(chunk_path); + free(in_buffer); + free(out_buffer); + return false; + } + out_buffer = tmp; + + SaveFileData(chunk_path, out_buffer, (int)out_size); free(chunk_path); - free(data); + free(in_buffer); return true; } \ No newline at end of file diff --git a/src/unpack/unpack.h b/src/unpack/unpack.h index 1154368..2bff9ec 100644 --- a/src/unpack/unpack.h +++ b/src/unpack/unpack.h @@ -3,6 +3,7 @@ #include "../stdafx.h" #define MAX_UNCOMPRESSE_TRY 4 + #if defined(WISPY_WINDOWS) #define RESOURCE_NAME "ASSETS_PACK" #elif defined(WISPY_LINUX)