Implement pack reading functions

This commit is contained in:
Abdelrahman Said 2023-11-02 00:12:03 +00:00
parent 7beac7e475
commit 041167d26f
2 changed files with 42 additions and 3 deletions

View File

@ -6,7 +6,7 @@
typedef struct { typedef struct {
u64 size; u64 size;
u8 *data; u8 data[];
} buf_t; } buf_t;
typedef struct { typedef struct {
@ -41,7 +41,8 @@ typedef struct asset_pack asset_pack_t;
bool create_asset_pack(const char *dirpath, const char *output); bool create_asset_pack(const char *dirpath, const char *output);
asset_pack_t *load_asset_pack(const char *filepath); 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); void close_asset_pack(asset_pack_t **pack);
#endif // !PAK_H #endif // !PAK_H

View File

@ -176,7 +176,45 @@ asset_pack_t *load_asset_pack(const char *filepath) {
return pack; 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) { void close_asset_pack(asset_pack_t **pack) {
if (!(*pack)) { if (!(*pack)) {