From a22ebbc00f160332271537c1e939baf766add7df Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Mon, 20 Jan 2025 00:21:44 +0100 Subject: [PATCH] fixed getting file size of BD caveset file inside Android APK file --- src/game_bd/bd_caveset.c | 16 ++++++++-------- src/libgame/misc.c | 23 +++++++++++++++++++++++ src/libgame/misc.h | 1 + 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/game_bd/bd_caveset.c b/src/game_bd/bd_caveset.c index 37bdd547..8ebbe269 100644 --- a/src/game_bd/bd_caveset.c +++ b/src/game_bd/bd_caveset.c @@ -599,22 +599,22 @@ static void caveset_name_set_from_filename(char *filename) */ boolean gd_caveset_load_from_file(char *filename) { + size_t filesize = getSizeOfFile(filename); size_t length; char *buf; List *new_caveset; - struct stat st; File *file; - if (stat(filename, &st) != 0) + if (filesize < 0) { - Warn("cannot stat() file"); + Warn("cannot get size of file '%s'", filename); return FALSE; } - if (st.st_size > 1048576) + if (filesize > 1048576) { - Warn("file bigger than 1MiB, refusing to load"); + Warn("cannot load file '%s' (bigger than 1 MB)", filename); return FALSE; } @@ -626,13 +626,13 @@ boolean gd_caveset_load_from_file(char *filename) return FALSE; } - buf = checked_malloc(st.st_size + 1); - length = readFile(file, buf, 1, st.st_size); + buf = checked_malloc(filesize + 1); + length = readFile(file, buf, 1, filesize); buf[length] = '\0'; closeFile(file); - if (length < st.st_size) + if (length < filesize) { Warn("cannot read file '%s'", filename); diff --git a/src/libgame/misc.c b/src/libgame/misc.c index 26c7a0e1..3ab4f57c 100644 --- a/src/libgame/misc.c +++ b/src/libgame/misc.c @@ -3364,6 +3364,29 @@ boolean touchFile(const char *filename) return TRUE; } +size_t getSizeOfFile(const char *filename) +{ + struct stat file_status; + + if (stat(filename, &file_status) == 0) + return file_status.st_size; + +#if defined(PLATFORM_ANDROID) + SDL_RWops *rw = SDL_RWFromFile(filename, MODE_READ); + + if (rw != NULL) + { + int size = SDL_RWsize(rw); + + SDL_RWclose(rw); + + return size; + } +#endif + + return -1; +} + // ---------------------------------------------------------------------------- // functions for directory handling diff --git a/src/libgame/misc.h b/src/libgame/misc.h index 81d27d22..aa582738 100644 --- a/src/libgame/misc.h +++ b/src/libgame/misc.h @@ -291,6 +291,7 @@ int getByteFromFile(File *); char *getStringFromFile(File *, char *, int); int copyFile(const char *, const char *); boolean touchFile(const char *); +size_t getSizeOfFile(const char *); Directory *openDirectory(char *); int closeDirectory(Directory *); -- 2.34.1