diff --git a/Sources/kinc/io/filereader.h b/Sources/kinc/io/filereader.h index 5fcec8c06..9f1714dfe 100644 --- a/Sources/kinc/io/filereader.h +++ b/Sources/kinc/io/filereader.h @@ -34,6 +34,7 @@ typedef struct __sFILE FILE; 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; @@ -56,6 +57,14 @@ typedef struct kinc_file_reader { /// Whether the file could be opened KINC_FUNC bool kinc_file_reader_open(kinc_file_reader_t *reader, const char *filepath, int type); +/// +/// Opens a memory area for reading using the file reader API. +/// +/// 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); + /// /// Closes a file. /// @@ -198,6 +207,36 @@ char *kinc_internal_get_files_location(void); #include #endif +static void memory_close_callback(kinc_file_reader_t *reader) { +} + +static size_t memory_read_callback(kinc_file_reader_t *reader, void *data, size_t size) { + size_t read_size = reader->size - reader->offset < size ? reader->size - reader->offset : size; + memcpy(data, (uint8_t *)reader->data + reader->offset, read_size); + reader->offset += read_size; + return read_size; +} + +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) { + reader->offset = pos; +} + +void 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; + reader->pos = memory_pos_callback; + reader->seek = memory_seek_callback; + reader->close = memory_close_callback; +} + #ifndef KORE_CONSOLE #ifdef KORE_IOS