Skip to content

Commit

Permalink
Merge branch 'main' of ssh://github.com/Kode/Kinc
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Mar 12, 2024
2 parents bc75276 + fa2c4c2 commit 39e8605
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 136 deletions.
87 changes: 26 additions & 61 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,52 @@ 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 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)
{
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 bool kinc_aasset_reader_seek(kinc_file_reader_t *reader, size_t pos) {
AAsset_seek((struct AAsset *)reader->data, pos, SEEK_SET);
return true;
}

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);

reader->data = fopen(filepath, "rb");
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);
return true;
}
else {
char filepath[1001];
bool isAbsolute = filename[0] == '/';
if (isAbsolute) {
strcpy(filepath, filename);
}
else {
strcpy(filepath, externalFilesDir);
strcat(filepath, "/");
strcat(filepath, filename);
}
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;
}

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

0 comments on commit 39e8605

Please sign in to comment.