From 27bd74d0661005d01f3dfc79e8903a56cef96c73 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 6 Mar 2024 22:58:27 +0100 Subject: [PATCH 1/5] [Android] use kinc_internal_set_files_location() instead of a global variable --- Backends/System/Android/Sources/kinc/backend/system.c.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Backends/System/Android/Sources/kinc/backend/system.c.h b/Backends/System/Android/Sources/kinc/backend/system.c.h index 5e4fc3492..df950c4d2 100644 --- a/Backends/System/Android/Sources/kinc/backend/system.c.h +++ b/Backends/System/Android/Sources/kinc/backend/system.c.h @@ -1221,8 +1221,6 @@ const char *kinc_gamepad_product_name(int gamepad) { #include -static char *externalFilesDir = NULL; - #define CLASS_NAME "android/app/NativeActivity" void initAndroidFileReader(void) { @@ -1243,8 +1241,9 @@ void initAndroidFileReader(void) { jstring jPath = (*env)->CallObjectMethod(env, file, getPath); const char *path = (*env)->GetStringUTFChars(env, jPath, NULL); - externalFilesDir = malloc(strlen(path) + 1); + char *externalFilesDir = malloc(strlen(path) + 1); strcpy(externalFilesDir, path); + kinc_internal_set_files_location(externalFilesDir); (*env)->ReleaseStringUTFChars(env, jPath, path); (*env)->DeleteLocalRef(env, jPath); @@ -1297,7 +1296,7 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int strcpy(filepath, filename); } else { - strcpy(filepath, externalFilesDir); + strcpy(filepath, kinc_internal_get_files_location()); strcat(filepath, "/"); strcat(filepath, filename); } From e0a2dd7d1f0e625f8b350554c593bfd60c32964d Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 7 Mar 2024 00:40:40 +0100 Subject: [PATCH 2/5] File reader improvements - the logic in kinc_file_reader_open() is now available on all platforms under the name kinc_internal_file_reader_open() - by default kinc_file_reader_open() calls kinc_internal_file_reader_open() except on Android and consoles - get rid of FileReaderImpl.h and kinc_file_reader_impl_t which are no longer required - remove a lot of duplicated code in kinc_file_reader_open on Android --- .../Android/Sources/kinc/backend/system.c.h | 69 +++++-------------- Sources/kinc/io/filereader.h | 29 ++++---- 2 files changed, 28 insertions(+), 70 deletions(-) diff --git a/Backends/System/Android/Sources/kinc/backend/system.c.h b/Backends/System/Android/Sources/kinc/backend/system.c.h index df950c4d2..5dabf87da 100644 --- a/Backends/System/Android/Sources/kinc/backend/system.c.h +++ b/Backends/System/Android/Sources/kinc/backend/system.c.h @@ -1250,77 +1250,40 @@ void initAndroidFileReader(void) { (*activity->vm)->DetachCurrentThread(activity->vm); } -static void kinc_aasset_reader_close(kinc_file_reader_t *reader) -{ +static void kinc_aasset_reader_close(kinc_file_reader_t *reader) { AAsset_close((struct AAsset *)reader->data); } -static size_t kinc_aasset_reader_read(kinc_file_reader_t *reader, void *data, size_t size) -{ +static size_t kinc_aasset_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { return AAsset_read((struct AAsset *)reader->data, data, size); } -static size_t kinc_aasset_reader_pos(kinc_file_reader_t *reader) -{ +static size_t kinc_aasset_reader_pos(kinc_file_reader_t *reader) { return (size_t)AAsset_seek((struct AAsset *)reader->data, 0, SEEK_CUR); } -static void kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos) -{ +static void kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos) { AAsset_seek((struct AAsset *)reader->data, pos, SEEK_SET); } bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { - memset(reader, 0, sizeof(kinc_file_reader_t)); - reader->type = type; - - if (type == KINC_FILE_TYPE_SAVE) { - char filepath[1001]; - - strcpy(filepath, kinc_internal_save_path()); - strcat(filepath, filename); + if (kinc_internal_file_reader_open(reader, filename, type)) { + return true; + } - reader->data = fopen(filepath, "rb"); - if (reader->data == NULL) { + if (type == KINC_FILE_TYPE_ASSET) { + reader->data = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM); + if (reader->data == NULL) return false; - } - fseek((FILE *)reader->data, 0, SEEK_END); - reader->size = ftell((FILE *)reader->data); - fseek((FILE *)reader->data, 0, SEEK_SET); + reader->size = AAsset_getLength((struct AAsset *)reader->data); + reader->close = kinc_aasset_reader_close; + reader->read = kinc_aasset_reader_read; + reader->pos = kinc_aasset_reader_pos; + reader->seek = kinc_aasset_reader_seek; return true; } - else { - char filepath[1001]; - bool isAbsolute = filename[0] == '/'; - if (isAbsolute) { - strcpy(filepath, filename); - } - else { - strcpy(filepath, kinc_internal_get_files_location()); - strcat(filepath, "/"); - strcat(filepath, filename); - } - FILE *stream = fopen(filepath, "rb"); - if (stream != NULL) { - reader->data = stream; - fseek(stream, 0, SEEK_END); - reader->size = ftell(stream); - fseek(stream, 0, SEEK_SET); - return true; - } - else { - reader->data = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM); - if (reader->data == NULL) - return false; - reader->size = AAsset_getLength((struct AAsset *)reader->data); - reader->close = kinc_aasset_reader_close; - reader->read = kinc_aasset_reader_read; - reader->pos = kinc_aasset_reader_pos; - reader->seek = kinc_aasset_reader_seek; - return true; - } - } + return false; } int kinc_cpu_cores(void) { diff --git a/Sources/kinc/io/filereader.h b/Sources/kinc/io/filereader.h index 9f1714dfe..6e5fdb0dd 100644 --- a/Sources/kinc/io/filereader.h +++ b/Sources/kinc/io/filereader.h @@ -2,10 +2,6 @@ #include -#if defined(KORE_SONY) || defined(KORE_SWITCH) -#include -#endif - #include #include #include @@ -42,10 +38,6 @@ typedef struct kinc_file_reader { size_t (*read)(struct kinc_file_reader *reader, void *data, size_t size); size_t (*pos)(struct kinc_file_reader *reader); void (*seek)(struct kinc_file_reader *reader, size_t pos); - -#if defined(KORE_SONY) || defined(KORE_SWITCH) - kinc_file_reader_impl_t impl; -#endif } kinc_file_reader_t; /// @@ -63,7 +55,8 @@ KINC_FUNC bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *fil /// The reader to initialize for reading /// A pointer to the memory area to read /// The size of the memory area -KINC_FUNC void kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size); +/// This function always returns true +KINC_FUNC bool kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size); /// /// Closes a file. @@ -183,6 +176,7 @@ KINC_FUNC int8_t kinc_read_s8(uint8_t *data); void kinc_internal_set_files_location(char *dir); char *kinc_internal_get_files_location(void); +bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type); #ifdef KINC_IMPLEMENTATION_IO #define KINC_IMPLEMENTATION @@ -225,7 +219,7 @@ static void memory_seek_callback(kinc_file_reader_t *reader, size_t pos) { reader->offset = pos; } -void kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size) +bool kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size) { memset(reader, 0, sizeof(kinc_file_reader_t)); reader->type = KINC_FILE_TYPE_ASSET; @@ -235,10 +229,9 @@ void kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t reader->pos = memory_pos_callback; reader->seek = memory_seek_callback; reader->close = memory_close_callback; + return true; } -#ifndef KORE_CONSOLE - #ifdef KORE_IOS const char *iphonegetresourcepath(void); #endif @@ -276,8 +269,7 @@ char *kinc_internal_get_files_location(void) { void kinc_internal_uwp_installed_location_path(char *path); #endif -#ifndef KORE_ANDROID -bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { +bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { memset(reader, 0, sizeof(kinc_file_reader_t)); reader->type = type; @@ -319,7 +311,7 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int strcat(filepath, "\\"); strcat(filepath, filename); #endif -#ifdef KORE_LINUX +#if defined(KORE_LINUX) || defined(KORE_ANDROID) if (type == KINC_FILE_TYPE_SAVE) { strcpy(filepath, kinc_internal_save_path()); strcat(filepath, filename); @@ -386,6 +378,11 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int #endif return true; } + +#if !defined(KORE_ANDROID) && !defined(KORE_CONSOLE) +bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { + return kinc_internal_file_reader_open(reader, filename, type); +} #endif size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { @@ -449,8 +446,6 @@ size_t kinc_file_reader_size(kinc_file_reader_t *reader) { return reader->size; } -#endif // KORE_CONSOLE - float kinc_read_f32le(uint8_t *data) { #ifdef KORE_LITTLE_ENDIAN // speed optimization return *(float *)data; From c7dd80f8c92fd693ac6292da54cae30a429fbd26 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 8 Mar 2024 12:56:50 +0100 Subject: [PATCH 3/5] Make the standard file reader implementation callback-based --- Sources/kinc/io/filereader.h | 100 +++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 45 deletions(-) diff --git a/Sources/kinc/io/filereader.h b/Sources/kinc/io/filereader.h index 6e5fdb0dd..012b840d4 100644 --- a/Sources/kinc/io/filereader.h +++ b/Sources/kinc/io/filereader.h @@ -269,6 +269,51 @@ char *kinc_internal_get_files_location(void) { void kinc_internal_uwp_installed_location_path(char *path); #endif +#if defined(KORE_WINDOWS) +static size_t kinc_libc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { + DWORD readBytes = 0; + if (ReadFile(reader->data, data, (DWORD)size, &readBytes, NULL)) { + return (size_t)readBytes; + } + else { + return 0; + } +} + +static void kinc_libc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { + // TODO: make this 64-bit compliant + SetFilePointer(reader->data, (LONG)pos, NULL, FILE_BEGIN); +} + +static void kinc_libc_file_reader_close(kinc_file_reader_t *reader) { + CloseHandle(reader->data); +} + +static size_t kinc_libc_file_reader_pos(kinc_file_reader_t *reader) { + // TODO: make this 64-bit compliant + return (size_t)SetFilePointer(reader->data, 0, NULL, FILE_CURRENT); +} +#else +static size_t kinc_libc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { + return fread(data, 1, size, (FILE *)reader->data); +} + +static void kinc_libc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { + fseek((FILE *)reader->data, pos, SEEK_SET); +} + +static void kinc_libc_file_reader_close(kinc_file_reader_t *reader) { + if (reader->data != NULL) { + fclose((FILE *)reader->data); + reader->data = NULL; + } +} + +static size_t kinc_libc_file_reader_pos(kinc_file_reader_t *reader) { + return ftell((FILE *)reader->data); +} +#endif + bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { memset(reader, 0, sizeof(kinc_file_reader_t)); reader->type = type; @@ -376,6 +421,12 @@ bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *file reader->size = ftell((FILE *)reader->data); fseek((FILE *)reader->data, 0, SEEK_SET); #endif + + reader->read = kinc_libc_file_reader_read; + reader->seek = kinc_libc_file_reader_seek; + reader->close = kinc_libc_file_reader_close; + reader->pos = kinc_libc_file_reader_pos; + return true; } @@ -386,60 +437,19 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int #endif size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { - if (reader->read != NULL) { - return reader->read(reader, data, size); - } -#if defined(KORE_WINDOWS) - DWORD readBytes = 0; - if (ReadFile(reader->data, data, (DWORD)size, &readBytes, NULL)) { - return (size_t)readBytes; - } - else { - return 0; - } -#else - return fread(data, 1, size, (FILE *)reader->data); -#endif + return reader->read(reader, data, size); } void kinc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { - if (reader->seek != NULL) { - reader->seek(reader, pos); - return; - } -#if defined(KORE_WINDOWS) - // TODO: make this 64-bit compliant - SetFilePointer(reader->data, (LONG)pos, NULL, FILE_BEGIN); -#else - fseek((FILE *)reader->data, pos, SEEK_SET); -#endif + reader->seek(reader, pos); } void kinc_file_reader_close(kinc_file_reader_t *reader) { - if (reader->close != NULL) { - reader->close(reader); - return; - } -#if defined(KORE_WINDOWS) - CloseHandle(reader->data); -#else - if (reader->data != NULL) { - fclose((FILE *)reader->data); - reader->data = NULL; - } -#endif + reader->close(reader); } size_t kinc_file_reader_pos(kinc_file_reader_t *reader) { - if (reader->pos != NULL) { - return reader->pos(reader); - } -#if defined(KORE_WINDOWS) - // TODO: make this 64-bit compliant - return (size_t)SetFilePointer(reader->data, 0, NULL, FILE_CURRENT); -#else - return ftell((FILE *)reader->data); -#endif + return reader->pos(reader); } size_t kinc_file_reader_size(kinc_file_reader_t *reader) { From 186545644105d798c988030ba6ab056cdefa0747 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 8 Mar 2024 13:08:44 +0100 Subject: [PATCH 4/5] Add kinc_file_reader_set_callback() function for user-provided filesystems --- .../Android/Sources/kinc/backend/system.c.h | 35 ++++++++++--------- Sources/kinc/io/filereader.h | 26 ++++++++++---- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/Backends/System/Android/Sources/kinc/backend/system.c.h b/Backends/System/Android/Sources/kinc/backend/system.c.h index 5dabf87da..aa02b89c5 100644 --- a/Backends/System/Android/Sources/kinc/backend/system.c.h +++ b/Backends/System/Android/Sources/kinc/backend/system.c.h @@ -1266,24 +1266,25 @@ static void kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos) { AAsset_seek((struct AAsset *)reader->data, pos, SEEK_SET); } -bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { - if (kinc_internal_file_reader_open(reader, filename, type)) { - return true; - } - - if (type == KINC_FILE_TYPE_ASSET) { - reader->data = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM); - if (reader->data == NULL) - return false; - reader->size = AAsset_getLength((struct AAsset *)reader->data); - reader->close = kinc_aasset_reader_close; - reader->read = kinc_aasset_reader_read; - reader->pos = kinc_aasset_reader_pos; - reader->seek = kinc_aasset_reader_seek; - return true; - } +static bool kinc_aasset_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { + if (type != KINC_FILE_TYPE_ASSET) + return false; + reader->data = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM); + if (reader->data == NULL) + return false; + reader->size = AAsset_getLength((struct AAsset *)reader->data); + reader->close = kinc_aasset_reader_close; + reader->read = kinc_aasset_reader_read; + reader->pos = kinc_aasset_reader_pos; + reader->seek = kinc_aasset_reader_seek; + return true; +} - return false; +bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { + memset(reader, 0, sizeof(*reader)); + return kinc_internal_file_reader_callback(reader, filename, type) || + kinc_internal_file_reader_open(reader, filename, type) || + kinc_aasset_reader_open(reader, filename, type); } int kinc_cpu_cores(void) { diff --git a/Sources/kinc/io/filereader.h b/Sources/kinc/io/filereader.h index 012b840d4..e1b04d76a 100644 --- a/Sources/kinc/io/filereader.h +++ b/Sources/kinc/io/filereader.h @@ -31,8 +31,6 @@ typedef struct kinc_file_reader { void *data; // A file handle or a more complex structure size_t size; size_t offset; // Needed by some implementations - int type; - bool mounted; void (*close)(struct kinc_file_reader *reader); size_t (*read)(struct kinc_file_reader *reader, void *data, size_t size); @@ -58,6 +56,12 @@ KINC_FUNC bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *fil /// This function always returns true KINC_FUNC bool kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size); +/// +/// Registers a file reader callback. +/// +/// The function to call when opening a file +KINC_FUNC void kinc_file_reader_set_callback(bool (*callback)(kinc_file_reader_t *reader, const char *filename, int type)); + /// /// Closes a file. /// @@ -176,6 +180,7 @@ KINC_FUNC int8_t kinc_read_s8(uint8_t *data); void kinc_internal_set_files_location(char *dir); char *kinc_internal_get_files_location(void); +bool kinc_internal_file_reader_callback(kinc_file_reader_t *reader, const char *filename, int type); bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type); #ifdef KINC_IMPLEMENTATION_IO @@ -222,7 +227,6 @@ static void memory_seek_callback(kinc_file_reader_t *reader, size_t pos) { bool kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size) { memset(reader, 0, sizeof(kinc_file_reader_t)); - reader->type = KINC_FILE_TYPE_ASSET; reader->data = data; reader->size = size; reader->read = memory_read_callback; @@ -253,6 +257,7 @@ const char *macgetresourcepath(void); #endif static char *fileslocation = NULL; +static bool *file_reader_callback(kinc_file_reader_t *reader, const char *filename, int type) = NULL; #ifdef KORE_WINDOWS static wchar_t wfilepath[1001]; #endif @@ -265,6 +270,10 @@ char *kinc_internal_get_files_location(void) { return fileslocation; } +bool kinc_internal_file_reader_callback(kinc_file_reader_t *reader, const char *filename, int type) { + return file_reader_callback ? file_reader_callback(reader, filename, type) : false; +} + #ifdef KORE_WINDOWSAPP void kinc_internal_uwp_installed_location_path(char *path); #endif @@ -315,9 +324,6 @@ static size_t kinc_libc_file_reader_pos(kinc_file_reader_t *reader) { #endif bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { - memset(reader, 0, sizeof(kinc_file_reader_t)); - reader->type = type; - char filepath[1001]; #ifdef KORE_IOS strcpy(filepath, type == KINC_FILE_TYPE_SAVE ? kinc_internal_save_path() : iphonegetresourcepath()); @@ -432,10 +438,16 @@ bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *file #if !defined(KORE_ANDROID) && !defined(KORE_CONSOLE) bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { - return kinc_internal_file_reader_open(reader, filename, type); + memset(reader, 0, sizeof(*reader)); + return kinc_internal_file_reader_callback(reader, filename, type) || + kinc_internal_file_reader_open(reader, filename, type); } #endif +void kinc_file_reader_set_callback(bool (*callback)(kinc_file_reader_t *reader, const char *filename, int type)) { + file_reader_callback = callback; +} + size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { return reader->read(reader, data, size); } From 47c86e1054f27bf3fd6e9174a2fb6f8c887ada95 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 8 Mar 2024 15:50:08 +0100 Subject: [PATCH 5/5] Allow file reader functions to signal failure --- .../Android/Sources/kinc/backend/system.c.h | 6 ++- Sources/kinc/io/filereader.h | 38 +++++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/Backends/System/Android/Sources/kinc/backend/system.c.h b/Backends/System/Android/Sources/kinc/backend/system.c.h index aa02b89c5..ffb7e7573 100644 --- a/Backends/System/Android/Sources/kinc/backend/system.c.h +++ b/Backends/System/Android/Sources/kinc/backend/system.c.h @@ -1250,8 +1250,9 @@ void initAndroidFileReader(void) { (*activity->vm)->DetachCurrentThread(activity->vm); } -static void kinc_aasset_reader_close(kinc_file_reader_t *reader) { +static bool kinc_aasset_reader_close(kinc_file_reader_t *reader) { AAsset_close((struct AAsset *)reader->data); + return true; } static size_t kinc_aasset_reader_read(kinc_file_reader_t *reader, void *data, size_t size) { @@ -1262,8 +1263,9 @@ static size_t kinc_aasset_reader_pos(kinc_file_reader_t *reader) { return (size_t)AAsset_seek((struct AAsset *)reader->data, 0, SEEK_CUR); } -static void kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos) { +static bool kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos) { AAsset_seek((struct AAsset *)reader->data, pos, SEEK_SET); + return true; } static bool kinc_aasset_reader_open(kinc_file_reader_t *reader, const char *filename, int type) { diff --git a/Sources/kinc/io/filereader.h b/Sources/kinc/io/filereader.h index e1b04d76a..18a924fe3 100644 --- a/Sources/kinc/io/filereader.h +++ b/Sources/kinc/io/filereader.h @@ -32,10 +32,10 @@ typedef struct kinc_file_reader { size_t size; size_t offset; // Needed by some implementations - void (*close)(struct kinc_file_reader *reader); + bool (*close)(struct kinc_file_reader *reader); size_t (*read)(struct kinc_file_reader *reader, void *data, size_t size); size_t (*pos)(struct kinc_file_reader *reader); - void (*seek)(struct kinc_file_reader *reader, size_t pos); + bool (*seek)(struct kinc_file_reader *reader, size_t pos); } kinc_file_reader_t; /// @@ -66,7 +66,8 @@ KINC_FUNC void kinc_file_reader_set_callback(bool (*callback)(kinc_file_reader_t /// Closes a file. /// /// The file to close -KINC_FUNC void kinc_file_reader_close(kinc_file_reader_t *reader); +/// Whether the file could be closed +KINC_FUNC bool kinc_file_reader_close(kinc_file_reader_t *reader); /// /// Reads data from a file starting from the current reading-position and increases the reading-position accordingly. @@ -96,7 +97,8 @@ KINC_FUNC size_t kinc_file_reader_pos(kinc_file_reader_t *reader); /// /// The reader which's reading-position to set /// The reading-position to set -KINC_FUNC void kinc_file_reader_seek(kinc_file_reader_t *reader, size_t pos); +/// Whether the reading position could be set +KINC_FUNC bool kinc_file_reader_seek(kinc_file_reader_t *reader, size_t pos); /// /// Interprets four bytes starting at the provided pointer as a little-endian float. @@ -206,7 +208,8 @@ bool kinc_internal_file_reader_open(kinc_file_reader_t *reader, const char *file #include #endif -static void memory_close_callback(kinc_file_reader_t *reader) { +static bool memory_close_callback(kinc_file_reader_t *reader) { + return true; } static size_t memory_read_callback(kinc_file_reader_t *reader, void *data, size_t size) { @@ -220,8 +223,9 @@ static size_t memory_pos_callback(kinc_file_reader_t *reader) { return reader->offset; } -static void memory_seek_callback(kinc_file_reader_t *reader, size_t pos) { +static bool memory_seek_callback(kinc_file_reader_t *reader, size_t pos) { reader->offset = pos; + return true; } bool kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size) @@ -257,7 +261,7 @@ const char *macgetresourcepath(void); #endif static char *fileslocation = NULL; -static bool *file_reader_callback(kinc_file_reader_t *reader, const char *filename, int type) = NULL; +static bool (*file_reader_callback)(kinc_file_reader_t *reader, const char *filename, int type) = NULL; #ifdef KORE_WINDOWS static wchar_t wfilepath[1001]; #endif @@ -289,13 +293,15 @@ static size_t kinc_libc_file_reader_read(kinc_file_reader_t *reader, void *data, } } -static void kinc_libc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { +static bool kinc_libc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { // TODO: make this 64-bit compliant SetFilePointer(reader->data, (LONG)pos, NULL, FILE_BEGIN); + return true; } -static void kinc_libc_file_reader_close(kinc_file_reader_t *reader) { +static bool kinc_libc_file_reader_close(kinc_file_reader_t *reader) { CloseHandle(reader->data); + return true; } static size_t kinc_libc_file_reader_pos(kinc_file_reader_t *reader) { @@ -307,15 +313,17 @@ static size_t kinc_libc_file_reader_read(kinc_file_reader_t *reader, void *data, return fread(data, 1, size, (FILE *)reader->data); } -static void kinc_libc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { +static bool kinc_libc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { fseek((FILE *)reader->data, pos, SEEK_SET); + return true; } -static void kinc_libc_file_reader_close(kinc_file_reader_t *reader) { +static bool kinc_libc_file_reader_close(kinc_file_reader_t *reader) { if (reader->data != NULL) { fclose((FILE *)reader->data); reader->data = NULL; } + return true; } static size_t kinc_libc_file_reader_pos(kinc_file_reader_t *reader) { @@ -452,12 +460,12 @@ size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size return reader->read(reader, data, size); } -void kinc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { - reader->seek(reader, pos); +bool kinc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) { + return reader->seek(reader, pos); } -void kinc_file_reader_close(kinc_file_reader_t *reader) { - reader->close(reader); +bool kinc_file_reader_close(kinc_file_reader_t *reader) { + return reader->close(reader); } size_t kinc_file_reader_pos(kinc_file_reader_t *reader) {