Asset moved some code around

This commit is contained in:
2026-04-03 14:41:38 -05:00
parent da1a5a3f1b
commit 9ec21f85a0
3 changed files with 66 additions and 23 deletions

View File

@@ -41,30 +41,11 @@ errorret_t assetLoad(
assertStrLenMax(filename, FILENAME_MAX, "Filename too long."); assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
assertNotNull(output, "Output pointer cannot be NULL."); assertNotNull(output, "Output pointer cannot be NULL.");
assertNotNull(loader, "Asset file loader cannot be NULL."); assertNotNull(loader, "Asset file loader cannot be NULL.");
assetfile_t file = {
.filename = filename,
.params = params,
.output = output,
.zipFile = NULL,
.size = 0
};
// Get file size of the asset.
zip_stat_init(&file.stat);
if(!zip_stat(ASSET.zip, filename, 0, &file.stat) == 0) {
errorThrow("Failed to stat asset file: %s", filename);
}
// Minimum file size.
file.size = (zip_int64_t)file.stat.size;
if(file.size <= 0) {
errorThrow("Asset file is empty: %s", filename);
}
assetfile_t file;
errorChain(assetFileInit(&file, filename, params, output));
errorChain(loader(&file)); errorChain(loader(&file));
assertNull(file.zipFile, "Asset file loader did not close the file."); errorChain(assetFileDispose(&file));
errorOk(); errorOk();
} }

View File

@@ -7,6 +7,34 @@
#include "asset/asset.h" #include "asset/asset.h"
#include "assert/assert.h" #include "assert/assert.h"
#include "util/memory.h"
errorret_t assetFileInit(
assetfile_t *file,
const char_t *filename,
void *params,
void *output
) {
memoryZero(file, sizeof(assetfile_t));
file->filename = filename;
file->params = params;
file->output = output;
// Stat the file
zip_stat_init(&file->stat);
if(!zip_stat(ASSET.zip, filename, 0, &file->stat) == 0) {
errorThrow("Failed to stat asset file: %s", filename);
}
// Minimum file size.
file->size = (zip_int64_t)file->stat.size;
if(file->size <= 0) {
errorThrow("Invalid asset file size: %s", filename);
}
errorOk();
}
errorret_t assetFileOpen(assetfile_t *file) { errorret_t assetFileOpen(assetfile_t *file) {
assertNotNull(file, "Asset file cannot be NULL."); assertNotNull(file, "Asset file cannot be NULL.");
@@ -50,4 +78,12 @@ errorret_t assetFileClose(assetfile_t *file) {
file->zipFile = NULL; file->zipFile = NULL;
file->position = 0; file->position = 0;
errorOk(); errorOk();
}
errorret_t assetFileDispose(assetfile_t *file) {
if(file->zipFile != NULL) {
errorChain(assetFileClose(file));
}
memoryZero(file, sizeof(assetfile_t));
errorOk();
} }

View File

@@ -25,6 +25,23 @@ typedef struct assetfile_s {
zip_file_t *zipFile; zip_file_t *zipFile;
} assetfile_t; } assetfile_t;
/**
* Initializes the asset file structure in preparation for loading. This will
* stat the file but not open the handle.
*
* @param file The asset file structure to initialize.
* @param filename The name of the asset file to load.
* @param params Optional loader params.
* @param output Output pointer for the loader.
* @return Error indicating success or failure.
*/
errorret_t assetFileInit(
assetfile_t *file,
const char_t *filename,
void *params,
void *output
);
/** /**
* Opens the asset file for reading. After opening the loader is responsible * Opens the asset file for reading. After opening the loader is responsible
* for closing the file. * for closing the file.
@@ -54,4 +71,13 @@ errorret_t assetFileRead(
* @param file The asset file to close. * @param file The asset file to close.
* @return An error code if the file could not be closed properly. * @return An error code if the file could not be closed properly.
*/ */
errorret_t assetFileClose(assetfile_t *file); errorret_t assetFileClose(assetfile_t *file);
/**
* Disposes the asset file structure, closing any open handles and zeroing
* out the structure.
*
* @param file The asset file to dispose.
* @return An error code if the file could not be disposed properly.
*/
errorret_t assetFileDispose(assetfile_t *file);