Skip to content

Commit

Permalink
[Android] hide AAsset reader implementation behind callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
samhocevar committed Mar 6, 2024
1 parent c5e87a6 commit 82f0c07
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 53 deletions.
48 changes: 36 additions & 12 deletions Backends/System/Android/Sources/kinc/backend/system.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -1251,22 +1251,43 @@ void initAndroidFileReader(void) {
(*activity->vm)->DetachCurrentThread(activity->vm);
}

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)
{
return AAsset_read((struct AAsset *)reader->data, data, size);
}

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)
{
AAsset_seek((struct AAsset *)reader->data, pos, SEEK_SET);
}

bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int type) {
reader->file = NULL;
reader->aasset = false;
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->file = fopen(filepath, "rb");
if (reader->file == NULL) {
reader->data = fopen(filepath, "rb");
if (reader->data == NULL) {
return false;
}
fseek(reader->file, 0, SEEK_END);
reader->size = ftell(reader->file);
fseek(reader->file, 0, SEEK_SET);
fseek((FILE *)reader->data, 0, SEEK_END);
reader->size = ftell((FILE *)reader->data);
fseek((FILE *)reader->data, 0, SEEK_SET);
return true;
}
else {
Expand All @@ -1283,18 +1304,21 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int

FILE *stream = fopen(filepath, "rb");
if (stream != NULL) {
reader->file = stream;
reader->data = stream;
fseek(stream, 0, SEEK_END);
reader->size = ftell(stream);
fseek(stream, 0, SEEK_SET);
return true;
}
else {
reader->file = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM);
if (reader->file == NULL)
reader->data = AAssetManager_open(kinc_android_get_asset_manager(), filename, AASSET_MODE_RANDOM);
if (reader->data == NULL)
return false;
reader->size = AAsset_getLength(reader->asset);
reader->aasset = true;
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;
}
}
Expand Down
78 changes: 37 additions & 41 deletions Sources/kinc/io/filereader.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ typedef struct __sFILE FILE;
#define KINC_FILE_TYPE_SAVE 1

typedef struct kinc_file_reader {
void *file;
void *data; // A file handle or a more complex structure
size_t size;
int type;
bool mounted;
#ifdef KORE_ANDROID
bool aasset;
#elif defined(KORE_SONY) || defined(KORE_SWITCH)

void (*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);

#if defined(KORE_SONY) || defined(KORE_SWITCH)
kinc_file_reader_impl_t impl;
#endif
} kinc_file_reader_t;
Expand Down Expand Up @@ -322,99 +326,91 @@ bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filename, int

#ifdef KORE_WINDOWS
MultiByteToWideChar(CP_UTF8, 0, filepath, -1, wfilepath, 1000);
reader->file = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (reader->file == INVALID_HANDLE_VALUE) {
reader->data = CreateFileW(wfilepath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (reader->data == INVALID_HANDLE_VALUE) {
return false;
}
#else
reader->file = fopen(filepath, "rb");
if (reader->file == NULL) {
reader->data = fopen(filepath, "rb");
if (reader->data == NULL) {
return false;
}
#endif

#ifdef KORE_WINDOWS
// TODO: make this 64-bit compliant
reader->size = (size_t)GetFileSize(reader->file, NULL);
reader->size = (size_t)GetFileSize(reader->data, NULL);
#else
fseek((FILE *)reader->file, 0, SEEK_END);
reader->size = ftell((FILE *)reader->file);
fseek((FILE *)reader->file, 0, SEEK_SET);
fseek((FILE *)reader->data, 0, SEEK_END);
reader->size = ftell((FILE *)reader->data);
fseek((FILE *)reader->data, 0, SEEK_SET);
#endif
return true;
}
#endif

size_t kinc_file_reader_read(kinc_file_reader_t *reader, void *data, size_t size) {
#ifdef KORE_ANDROID
if (reader->aasset) {
return AAsset_read((struct AAsset *)reader->file, data, size);
if (reader->read != NULL) {
return reader->read(reader, data, size);
}
#endif
#if defined(KORE_WINDOWS)
DWORD readBytes = 0;
if (ReadFile(reader->file, data, (DWORD)size, &readBytes, NULL)) {
if (ReadFile(reader->data, data, (DWORD)size, &readBytes, NULL)) {
return (size_t)readBytes;
}
else {
return 0;
}
#else
return fread(data, 1, size, (FILE *)reader->file);
return fread(data, 1, size, (FILE *)reader->data);
#endif
}

void kinc_file_reader_seek(kinc_file_reader_t *reader, size_t pos) {
#ifdef KORE_ANDROID
if (reader->aasset) {
AAsset_seek((struct AAsset *)reader->file, pos, SEEK_SET);
if (reader->seek != NULL) {
reader->seek(reader, pos);
return;
}
#endif
#if defined(KORE_WINDOWS)
// TODO: make this 64-bit compliant
SetFilePointer(reader->file, (LONG)pos, NULL, FILE_BEGIN);
SetFilePointer(reader->data, (LONG)pos, NULL, FILE_BEGIN);
#else
fseek((FILE *)reader->file, pos, SEEK_SET);
fseek((FILE *)reader->data, pos, SEEK_SET);
#endif
}

void kinc_file_reader_close(kinc_file_reader_t *reader) {
#ifdef KORE_ANDROID
if (reader->aasset) {
AAsset_close((struct AAsset *)reader->file);
reader->file = NULL;
if (reader->close != NULL) {
reader->close(reader);
return;
}
#endif
#if defined(KORE_WINDOWS)
CloseHandle(reader->file);
CloseHandle(reader->data);
#else
if (reader->file == NULL) {
return;
if (reader->data != NULL) {
fclose((FILE *)reader->data);
reader->data = NULL;
}
fclose((FILE *)reader->file);
reader->file = NULL;
#endif
}

size_t kinc_file_reader_pos(kinc_file_reader_t *reader) {
#ifdef KORE_ANDROID
if (reader->aasset) {
return (size_t)AAsset_seek((struct AAsset *)reader->file, 0, SEEK_CUR);
if (reader->pos != NULL) {
return reader->pos(reader);
}
#endif
#if defined(KORE_WINDOWS)
// TODO: make this 64-bit compliant
return (size_t)SetFilePointer(reader->file, 0, NULL, FILE_CURRENT);
return (size_t)SetFilePointer(reader->data, 0, NULL, FILE_CURRENT);
#else
return ftell((FILE *)reader->file);
return ftell((FILE *)reader->data);
#endif
}

size_t kinc_file_reader_size(kinc_file_reader_t *reader) {
return reader->size;
}

#endif
#endif // KORE_CONSOLE

float kinc_read_f32le(uint8_t *data) {
#ifdef KORE_LITTLE_ENDIAN // speed optimization
Expand Down

0 comments on commit 82f0c07

Please sign in to comment.