Populate the toc_entries in asset_pack_t

This commit is contained in:
Abdelrahman Said 2023-10-24 00:40:35 +01:00
parent 07daa2ca19
commit 31d78bfcd3

View File

@ -132,7 +132,7 @@ asset_pack_t *load_asset_pack(const char *filepath) {
pack->fp = fopen(full_path, "rb"); pack->fp = fopen(full_path, "rb");
if (!(pack->fp)) { if (!(pack->fp)) {
free(pack); close_asset_pack(&pack);
return NULL; return NULL;
} }
@ -145,16 +145,29 @@ asset_pack_t *load_asset_pack(const char *filepath) {
pack->toc = (u8 *)malloc(toc_size); pack->toc = (u8 *)malloc(toc_size);
if (!(pack->toc)) { if (!(pack->toc)) {
fclose(pack->fp); close_asset_pack(&pack);
free(pack);
return NULL; return NULL;
} }
fread((void *)(pack->toc), toc_size, 1, pack->fp); fread((void *)(pack->toc), toc_size, 1, pack->fp);
// TODO (Abdelrahman): Populate the entries array u64 entries_size = sizeof(toc_entry_t *) * pack->header.toc.count;
pack->toc_entries = (toc_entry_t **)malloc(entries_size);
if (!(pack->toc_entries)) {
close_asset_pack(&pack);
return NULL;
}
toc_entry_t *next = (toc_entry_t *)pack->toc;
for (u64 i = 0; i < pack->header.toc.count; ++i) {
pack->toc_entries[i] = next;
next = (toc_entry_t *)((u8 *)next + sizeof(toc_entry_t) + next->length);
}
pack->assets_offset = ftell(pack->fp); pack->assets_offset = ftell(pack->fp);
@ -170,6 +183,10 @@ void close_asset_pack(asset_pack_t **pack) {
return; return;
} }
if ((*pack)->toc_entries) {
free((*pack)->toc_entries);
}
if ((*pack)->toc) { if ((*pack)->toc) {
free((*pack)->toc); free((*pack)->toc);
} }