Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More file reader changes #868

Merged
merged 2 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 18 additions & 56 deletions Backends/System/Android/Sources/kinc/backend/system.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,6 @@ const char *kinc_gamepad_product_name(int gamepad) {

#include <kinc/io/filereader.h>

static char *externalFilesDir = NULL;

#define CLASS_NAME "android/app/NativeActivity"

void initAndroidFileReader(void) {
Expand All @@ -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) {
Expand Down
29 changes: 12 additions & 17 deletions Sources/kinc/io/filereader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#include <kinc/global.h>

#if defined(KORE_SONY) || defined(KORE_SWITCH)
#include <kinc/backend/FileReaderImpl.h>
#endif

#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
Expand Down Expand Up @@ -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;

/// <summary>
Expand All @@ -63,7 +55,8 @@ KINC_FUNC bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *fil
/// <param name="reader">The reader to initialize for reading</param>
/// <param name="data">A pointer to the memory area to read</param>
/// <param name="size">The size of the memory area</param>
KINC_FUNC void kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size);
/// <returns>This function always returns true</returns>
KINC_FUNC bool kinc_file_reader_from_memory(kinc_file_reader_t *reader, void *data, size_t size);

/// <summary>
/// Closes a file.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
Loading