Rename pak_read_t to asset_pack_t and update load_asset_pack
This commit is contained in:
parent
9fa96f9712
commit
7fd7bc4dfa
@ -37,11 +37,11 @@ typedef struct {
|
||||
toc_t toc;
|
||||
} pak_t;
|
||||
|
||||
typedef struct pak_read pak_read_t;
|
||||
typedef struct asset_pack asset_pack_t;
|
||||
|
||||
bool create_asset_pack(const char *dirpath, const char *output);
|
||||
pak_read_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);
|
||||
void close_asset_pack(pak_read_t **pack);
|
||||
void close_asset_pack(asset_pack_t **pack);
|
||||
|
||||
#endif // !PAK_H
|
||||
|
68
src/pak.c
68
src/pak.c
@ -13,12 +13,13 @@
|
||||
#define PAK_PATCH 0
|
||||
#define PAK_EXT ".pak"
|
||||
|
||||
struct pak_read {
|
||||
struct asset_pack {
|
||||
pak_t header;
|
||||
u8 *buf;
|
||||
FILE *fp;
|
||||
u8 *toc;
|
||||
toc_entry_t **toc_entries;
|
||||
u64 assets_length;
|
||||
u8 *assets;
|
||||
u64 assets_offset;
|
||||
};
|
||||
|
||||
toc_entry_t *create_toc_entry(const char *name, u64 offset);
|
||||
@ -119,61 +120,64 @@ RETURN_CREATE_ASSET_PAK:
|
||||
return created;
|
||||
}
|
||||
|
||||
pak_read_t *load_asset_pack(const char *filepath) {
|
||||
asset_pack_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));
|
||||
asset_pack_t *pack = (asset_pack_t *)malloc(sizeof(asset_pack_t));
|
||||
if (!pack) {
|
||||
goto CLOSE_FILE_LOAD_ASSET_PACK;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
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)) {
|
||||
pack->fp = fopen(full_path, "rb");
|
||||
if (!(pack->fp)) {
|
||||
free(pack);
|
||||
|
||||
pack = NULL;
|
||||
|
||||
goto CLOSE_FILE_LOAD_ASSET_PACK;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fread((void *)(pack->buf), data_size, 1, fp);
|
||||
u64 file_size = get_file_length(pack->fp);
|
||||
|
||||
pack->toc = pack->buf;
|
||||
fread((void *)(&(pack->header)), sizeof(pak_t), 1, pack->fp);
|
||||
|
||||
pack->assets_length = data_size - pack->header.toc.size;
|
||||
u64 toc_size = pack->header.toc.size;
|
||||
|
||||
pack->assets = pack->buf + pack->header.toc.size;
|
||||
pack->toc = (u8 *)malloc(toc_size);
|
||||
if (!(pack->toc)) {
|
||||
fclose(pack->fp);
|
||||
|
||||
CLOSE_FILE_LOAD_ASSET_PACK:
|
||||
fclose(fp);
|
||||
free(pack);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
fread((void *)(pack->toc), toc_size, 1, pack->fp);
|
||||
|
||||
// TODO (Abdelrahman): Populate the entries array
|
||||
|
||||
pack->assets_offset = ftell(pack->fp);
|
||||
|
||||
pack->assets_length = file_size - pack->assets_offset;
|
||||
|
||||
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) {
|
||||
void close_asset_pack(asset_pack_t **pack) {
|
||||
if (!(*pack)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((*pack)->toc) {
|
||||
free((*pack)->toc);
|
||||
}
|
||||
|
||||
fclose((*pack)->fp);
|
||||
|
||||
free(*pack);
|
||||
|
||||
*pack = NULL;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
pak_read_t *pack = load_asset_pack("assets.pak");
|
||||
asset_pack_t *pack = load_asset_pack("assets.pak");
|
||||
if (!pack) {
|
||||
printf("Failed to load asset pack");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user