From 7a90d2d38f667ff13b4882ca78d5f8520859f0bc Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 27 Aug 2025 07:46:11 -0500 Subject: [PATCH] prog --- src/asset/asset.c | 38 ++++++++++++++++++++++++++++---------- src/asset/asset.h | 14 +++++++++++++- src/engine/engine.c | 4 ++-- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/src/asset/asset.c b/src/asset/asset.c index 6ff2737..b8ee780 100644 --- a/src/asset/asset.c +++ b/src/asset/asset.c @@ -64,6 +64,14 @@ void assetLoad(const char_t *filename) { ASSET.state != ASSET_STATE_LOADING, "Asset system is already loading an asset." ); + + // Determine the asset map type we are expecting based on the extension. + const assetmap_t *expected = &ASSET_MAP[0]; + do { + if(stringEndsWith(filename, expected->extension)) break; + expected++; + } while(expected->extension); + assertNotNull(expected->extension, "Unknown asset type."); // Pass off to the thread to begin loading. ASSET.errorState = ERROR_STATE_INIT; @@ -71,8 +79,6 @@ void assetLoad(const char_t *filename) { stringCopy(ASSET.filename, filename, ASSET_FILENAME_MAX); memoryZero(&ASSET.data, sizeof(ASSET.data)); - consolePrint("Loading asset: %s", filename); - // For the sake of testing I'm going to do blocking load, fun stuff. zip_file_t *file = zip_fopen(ASSET.zip, filename, 0); if(file == NULL) { @@ -85,8 +91,6 @@ void assetLoad(const char_t *filename) { return; } - consolePrint("Opened asset: %s", filename); - // Determine length of the file (uncompressed) struct zip_stat st; if(zip_stat(ASSET.zip, filename, 0, &st) != 0) { @@ -100,7 +104,6 @@ void assetLoad(const char_t *filename) { return; } - printf("Asset size: %d\n", (int)st.size); if(st.size > sizeof(ASSET.data.palette)) { zip_fclose(file); ASSET.error = errorCreate( @@ -117,11 +120,26 @@ void assetLoad(const char_t *filename) { zip_fread(file, &ASSET.data, st.size); zip_fclose(file); ASSET.state = ASSET_STATE_LOADED; - consolePrint("First 3 bytes: %c%c%c\n", - ASSET.data.header[0], - ASSET.data.header[1], - ASSET.data.header[2] - ); + + if(memoryCompare( + ASSET.data.header, expected->header, ASSET_HEADER_LENGTH + ) != 0) { + ASSET.state = ASSET_STATE_ERROR; + ASSET.error = errorCreate( + &ASSET.errorState, + "Asset header mismatch: %s (got %c%c%c, expected %c%c%c)", + filename, + ASSET.data.header[0], + ASSET.data.header[1], + ASSET.data.header[2], + expected->header[0], + expected->header[1], + expected->header[2] + ); + return; + } + + printf("Loaded asset: %s\n", filename); } void assetDispose(void) { diff --git a/src/asset/asset.h b/src/asset/asset.h index b6d654f..86f3ca6 100644 --- a/src/asset/asset.h +++ b/src/asset/asset.h @@ -14,6 +14,7 @@ #define ASSET_COUNT_MAX 128 #define ASSET_FILENAME_MAX 128 +#define ASSET_HEADER_LENGTH 3 #if ASSET_TYPE == wad #else @@ -21,7 +22,7 @@ #endif typedef struct { - char_t header[3]; + char_t header[ASSET_HEADER_LENGTH]; union { assetpalette_t palette; assettileset_t tileset; @@ -47,6 +48,11 @@ typedef struct { assetdata_t data; } asset_t; +typedef struct { + const char_t *header; + const char_t *extension; +} assetmap_t; + static const char_t ASSET_SEARCH_PATHS[][FILENAME_MAX] = { "%s/%s", "./%s", @@ -56,6 +62,12 @@ static const char_t ASSET_SEARCH_PATHS[][FILENAME_MAX] = { "../data/%s", }; +static const assetmap_t ASSET_MAP[] = { + { "DPF", "test.palette.dpf" }, + { "DPT", "test.tileset.dpt" }, + { NULL, NULL } +}; + #define ASSET_SEARCH_PATHS_COUNT (\ sizeof(ASSET_SEARCH_PATHS) / FILENAME_MAX\ ) diff --git a/src/engine/engine.c b/src/engine/engine.c index c36f6c1..4b03637 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -29,8 +29,8 @@ errorret_t engineInit(void) { errorChain(assetInit()); errorChain(displayInit()); - assetLoad("test.palette.dp123123"); - // assetLoad("test.palette.dpf"); + // assetLoad("test.palette.dp123123"); + assetLoad("test.palette.dpf"); if(ASSET.state == ASSET_STATE_ERROR) errorChain(ASSET.error); sceneTestAdd();