diff --git a/Backends/System/Android/Sources/kinc/backend/system.c.h b/Backends/System/Android/Sources/kinc/backend/system.c.h index 5e4fc3492..5dabf87da 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,85 +1241,49 @@ 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); (*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, externalFilesDir); - 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;