From 041167d26fdcce1203f99698ecd5a1709d6870ba Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Thu, 2 Nov 2023 00:12:03 +0000 Subject: [PATCH] Implement pack reading functions --- include/pak.h | 5 +++-- src/pak.c | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/include/pak.h b/include/pak.h index 28bac05..12e4cb5 100644 --- a/include/pak.h +++ b/include/pak.h @@ -6,7 +6,7 @@ typedef struct { u64 size; - u8 *data; + u8 data[]; } buf_t; typedef struct { @@ -41,7 +41,8 @@ typedef struct asset_pack asset_pack_t; bool create_asset_pack(const char *dirpath, const char *output); asset_pack_t *load_asset_pack(const char *filepath); -// buf_t read_file_from_pack(pak_read_t *pack, const char *filename); +buf_t *read_file_from_pack(asset_pack_t *pack, const char *filename); +void close_pack_file(buf_t **buf); void close_asset_pack(asset_pack_t **pack); #endif // !PAK_H diff --git a/src/pak.c b/src/pak.c index 9bcca28..d81ab62 100644 --- a/src/pak.c +++ b/src/pak.c @@ -176,7 +176,45 @@ asset_pack_t *load_asset_pack(const char *filepath) { return pack; } -// buf_t read_file_from_pack(pak_read_t *pack, const char *filename) {} +buf_t *read_file_from_pack(asset_pack_t *pack, const char *filename) { + toc_entry_t *entry = NULL; + buf_t *output = NULL; + + for (u64 i = 0; i < pack->header.toc.count; ++i) { + entry = pack->toc_entries[i]; + + if (STRNEQ(filename, entry->name, entry->length)) { + fseek(pack->fp, pack->assets_offset + entry->offset, SEEK_SET); + + u64 size = 0; + fread(&size, sizeof(u64), 1, pack->fp); + + output = (buf_t *)malloc(sizeof(buf_t) + size); + if (!output) { + break; + } + + output->size = size; + + fread(&(output->data), size, 1, pack->fp); + + break; + } + } + + return output; +} + +void close_pack_file(buf_t **buf) { + buf_t *b = *buf; + + if (!b || b->size == 0) { + return; + } + + free(*buf); + *buf = NULL; +} void close_asset_pack(asset_pack_t **pack) { if (!(*pack)) {