Compare commits

...

5 Commits

5 changed files with 122 additions and 7 deletions

34
compile
View File

@ -22,6 +22,7 @@ FG_BR_WHITE="\033[97"
CC=clang
CFLAGS="-Wall -Werror -pedantic -Iinclude "
PCKR_SRC="\
src/path_utils.c \
src/argparse.c \
@ -30,14 +31,24 @@ PCKR_SRC="\
src/pak.c \
src/pckr.c \
src/main.c"
PCKR_TEST_SRC="\
src/path_utils.c \
src/io.c \
src/pckr_test.c"
PCKR_TEST_OUT="pckr_test"
PAKRD_TEST_SRC="\
src/path_utils.c \
src/io.c \
src/darr.c \
src/pak.c \
src/pakrd_test.c"
PAKRD_TEST_OUT="pakrd_test"
BUILD_TYPE="debug"
while [[ $# -gt 0 ]]; do
case $1 in
-r|--release)
@ -51,6 +62,7 @@ while [[ $# -gt 0 ]]; do
esac
done
if [[ $BUILD_TYPE == "debug" ]]; then
CFLAGS+="-g "
PCKR_OUT="pckr"
@ -59,14 +71,32 @@ else
PCKR_OUT="../proj/pckr"
fi
# Build pckr executable
(set -x ; $CC $CFLAGS $PCKR_SRC -o $PCKR_OUT)
# Build pckr_test executable
(set -x ; $CC $CFLAGS $PCKR_TEST_SRC -o $PCKR_TEST_OUT)
# Run pckr test
# Build pakrd_test executable
(set -x ; $CC $CFLAGS $PAKRD_TEST_SRC -o $PAKRD_TEST_OUT)
# Create test pak
./pckr ./test_assets ./assets
(./$PCKR_TEST_OUT && echo -e "\npckr_test [${FG_BR_GREEN}m${BOLD}m SUCCESS $CLEAR]") || echo -e "\npckr_test [${FG_BR_RED}m${BOLD}m FAILURE $CLEAR]"
# Run pckr_test
(./$PCKR_TEST_OUT && echo -e "\npckr_test [${FG_BR_GREEN}m${BOLD}m SUCCESS $CLEAR]") || echo -e "\npckr_test [${FG_BR_RED}m${BOLD}m FAILURE $CLEAR]"
# Run pakrd_test
(./$PAKRD_TEST_OUT && echo -e "pakrd_test [${FG_BR_GREEN}m${BOLD}m SUCCESS $CLEAR]") || echo -e "pakrd_test [${FG_BR_RED}m${BOLD}m FAILURE $CLEAR]"
# Clean test assets
rm ./assets.pak
rm pckr_test
rm pakrd_test

View File

@ -4,6 +4,11 @@
#include "aliases.h"
#include <stdbool.h>
typedef struct {
u64 size;
u8 *data;
} buf_t;
typedef struct {
u64 offset;
u64 length;
@ -32,6 +37,11 @@ typedef struct {
toc_t toc;
} pak_t;
typedef struct pak_read pak_read_t;
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

View File

@ -13,6 +13,14 @@
#define PAK_PATCH 0
#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);
void write_toc_entry(toc_entry_t *entry, FILE *fp);
void write_toc_entries(const toc_t *toc, toc_entry_t **entries, FILE *fp);
@ -77,8 +85,6 @@ bool create_asset_pack(const char *dirpath, const char *output) {
if (result.fp) {
++(pak.toc.count);
offset += sizeof(pak_entry_t);
toc_entry_t *toc_entry = create_toc_entry(result.name, offset);
pak_entry_t *pak_entry = create_pak_entry_from_file(result.fp);
@ -87,7 +93,7 @@ bool create_asset_pack(const char *dirpath, const char *output) {
}
pak.toc.size += sizeof(toc_entry_t) + toc_entry->length;
offset += pak_entry->size;
offset += sizeof(pak_entry_t) + pak_entry->size;
darr_add(&toc_entries_darr, (void *)toc_entry);
darr_add(&pak_entries_darr, (void *)pak_entry);
@ -113,6 +119,64 @@ RETURN_CREATE_ASSET_PAK:
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) {
u64 length = strlen(name);
u64 size = sizeof(toc_entry_t) + length + 1;

12
src/pakrd_test.c Normal file
View File

@ -0,0 +1,12 @@
#include "pak.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
pak_read_t *pack = load_asset_pack("assets.pak");
if (!pack) {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View File

@ -124,8 +124,7 @@ i32 main(i32 argc, char *argv[]) {
pos = ftell(fp);
u64 new_pos =
toc_entries[i]->offset + sizeof(pak_t) + pack.toc.size - sizeof(u64);
u64 new_pos = toc_entries[i]->offset + sizeof(pak_t) + pack.toc.size;
fseek(fp, new_pos, SEEK_SET);