#include "aliases.h" #include "io.h" #include "pak.h" #include "test_utils.h" #include #include #include #include #include i32 main(i32 argc, char *argv[]) { char filepath[PATH_MAX] = {0}; realpath("assets.pak", filepath); FILE *fp = fopen(filepath, "rb"); if (!fp) { printf("Failed to open file\n"); return EXIT_FAILURE; } asset_t assets[] = { (asset_t){.name = "file01", .contents = "Hello\n"}, (asset_t){.name = "name.txt", .contents = "Abdelrahman\n"}, (asset_t){.name = "file02", .contents = "This is the second file\n"}, }; u64 expected_pak_size = sizeof(pak_t); u64 expected_count = ARRLEN(assets); u64 expected_entries_size = 0; for (u64 i = 0; i < expected_count; ++i) { u64 name_length = strlen(assets[i].name) + 1; u64 contents_length = strlen(assets[i].contents) + 1; expected_pak_size += name_length + contents_length + sizeof(toc_entry_t) + sizeof(pak_entry_t); expected_entries_size += name_length + sizeof(toc_entry_t); } u64 real_size = get_file_length(fp); if (expected_pak_size != real_size) { printf("Real size (%zu) doesn't match expected size (%zu)\n", real_size, expected_pak_size); fclose(fp); return EXIT_FAILURE; } pak_t pack = {0}; fread((void *)&pack, sizeof(pak_t), 1, fp); if (pack.toc.count != expected_count) { printf("Pack file count (%zu) doesn't match expected count (%zu)\n", pack.toc.count, expected_count); fclose(fp); return EXIT_FAILURE; } if (pack.toc.size != expected_entries_size) { printf( "Table of contents real size (%zu) doesn't match expected size (%zu)\n", pack.toc.size, expected_entries_size); fclose(fp); return EXIT_FAILURE; } toc_entry_t *toc_entries[pack.toc.count]; memset((void *)toc_entries, 0, sizeof(toc_entry_t *) * pack.toc.count); pak_entry_t *pak_entries[pack.toc.count]; memset((void *)pak_entries, 0, sizeof(pak_entry_t *) * pack.toc.count); bool match = true; for (u64 i = 0; i < pack.toc.count; ++i) { u64 pos = ftell(fp); u64 data[2] = {0}; fread(data, sizeof(u64) * ARRLEN(data), 1, fp); u64 toc_entry_size = sizeof(toc_entry_t) + data[1]; toc_entries[i] = malloc(toc_entry_size); if (!(toc_entries[i])) { printf("TOC entry allocation failed\n"); fclose(fp); return EXIT_FAILURE; } fseek(fp, pos, SEEK_SET); fread(toc_entries[i], toc_entry_size, 1, fp); i64 entry_in_assets = -1; for (u64 j = 0; j < pack.toc.count; ++j) { if (STREQ((toc_entries[i])->name, assets[j].name)) { match = true; entry_in_assets = j; break; } else { match = false; } } pos = ftell(fp); u64 new_pos = toc_entries[i]->offset + sizeof(pak_t) + pack.toc.size; fseek(fp, new_pos, SEEK_SET); u64 entry_size = 0; fread(&entry_size, sizeof(u64), 1, fp); u64 pak_entry_size = entry_size + sizeof(pak_entry_t); pak_entries[i] = malloc(pak_entry_size); if (!(pak_entries[i])) { printf("PAK entry allocation failed\n"); fclose(fp); return EXIT_FAILURE; } fread(pak_entries[i]->data, entry_size, 1, fp); if (!(STREQ((char *)(pak_entries[i]->data), assets[entry_in_assets].contents))) { printf("%s, %s\n", (char *)(pak_entries[i]->data), assets[entry_in_assets].contents); match = false; break; } fseek(fp, pos, SEEK_SET); } for (u64 i = 0; i < pack.toc.count; ++i) { if (toc_entries[i]) { free(toc_entries[i]); } if (pak_entries[i]) { free(pak_entries[i]); } } fclose(fp); if (!match) { printf("Pack contents doesn't match expected contents\n"); return EXIT_FAILURE; } return EXIT_SUCCESS; }