Add initial support for reading an asset pack

This commit is contained in:
Abdelrahman Said 2023-10-22 23:50:22 +01:00
parent c0d9858b97
commit 276867f76a
2 changed files with 76 additions and 0 deletions

View File

@ -4,6 +4,11 @@
#include "aliases.h" #include "aliases.h"
#include <stdbool.h> #include <stdbool.h>
typedef struct {
u64 size;
u8 *data;
} buf_t;
typedef struct { typedef struct {
u64 offset; u64 offset;
u64 length; u64 length;
@ -32,6 +37,11 @@ typedef struct {
toc_t toc; toc_t toc;
} pak_t; } pak_t;
typedef struct pak_read pak_read_t;
bool create_asset_pack(const char *dirpath, const char *output); bool create_asset_pack(const char *dirpath, const char *output);
pak_read_t *load_asset_pack(const char *filepath);
// buf_t read_file_from_pack(pak_read_t *pack, const char *filename);
void close_asset_pack(pak_read_t **pack);
#endif // !PAK_H #endif // !PAK_H

View File

@ -13,6 +13,14 @@
#define PAK_PATCH 0 #define PAK_PATCH 0
#define PAK_EXT ".pak" #define PAK_EXT ".pak"
struct pak_read {
pak_t header;
u8 *buf;
u8 *toc;
u64 assets_length;
u8 *assets;
};
toc_entry_t *create_toc_entry(const char *name, u64 offset); toc_entry_t *create_toc_entry(const char *name, u64 offset);
void write_toc_entry(toc_entry_t *entry, FILE *fp); void write_toc_entry(toc_entry_t *entry, FILE *fp);
void write_toc_entries(const toc_t *toc, toc_entry_t **entries, FILE *fp); void write_toc_entries(const toc_t *toc, toc_entry_t **entries, FILE *fp);
@ -111,6 +119,64 @@ RETURN_CREATE_ASSET_PAK:
return created; return created;
} }
pak_read_t *load_asset_pack(const char *filepath) {
char full_path[PATH_MAX] = {0};
realpath(filepath, full_path);
pak_read_t *pack = NULL;
FILE *fp = fopen(full_path, "rb");
if (!fp) {
goto RETURN_LOAD_ASSET_PACK;
}
pack = (pak_read_t *)malloc(sizeof(pak_read_t));
if (!pack) {
goto CLOSE_FILE_LOAD_ASSET_PACK;
}
u64 file_size = get_file_length(fp);
fread((void *)(&(pack->header)), sizeof(pak_t), 1, fp);
u64 data_size = file_size - sizeof(pak_t);
pack->buf = (u8 *)malloc(data_size);
if (!(pack->buf)) {
free(pack);
pack = NULL;
goto CLOSE_FILE_LOAD_ASSET_PACK;
}
fread((void *)(pack->buf), data_size, 1, fp);
pack->toc = pack->buf;
pack->assets_length = data_size - pack->header.toc.size;
pack->assets = pack->buf + pack->header.toc.size;
CLOSE_FILE_LOAD_ASSET_PACK:
fclose(fp);
RETURN_LOAD_ASSET_PACK:
return pack;
}
// buf_t read_file_from_pack(pak_read_t *pack, const char *filename) {}
void close_asset_pack(pak_read_t **pack) {
if (!(*pack)) {
return;
}
free(*pack);
*pack = NULL;
}
toc_entry_t *create_toc_entry(const char *name, u64 offset) { toc_entry_t *create_toc_entry(const char *name, u64 offset) {
u64 length = strlen(name); u64 length = strlen(name);
u64 size = sizeof(toc_entry_t) + length + 1; u64 size = sizeof(toc_entry_t) + length + 1;