Refactor asset loading
This commit is contained in:
@@ -9,22 +9,23 @@
|
||||
#include "util/memory.h"
|
||||
#include "util/string.h"
|
||||
#include "assert/assert.h"
|
||||
#include "asset/assettype.h"
|
||||
|
||||
errorret_t assetInit(void) {
|
||||
memoryZero(&ASSET_MANAGER, sizeof(assetmanager_t));
|
||||
|
||||
// Default system path, intended to be overridden by the paltform
|
||||
stringCopy(ASSET_MANAGER.systemPath, ".", FILENAME_MAX);
|
||||
memoryZero(&ASSET, sizeof(asset_t));
|
||||
|
||||
// Default system path, intended to be overridden by the platform
|
||||
stringCopy(ASSET.systemPath, ".", FILENAME_MAX);
|
||||
|
||||
// Open zip file
|
||||
char_t searchPath[FILENAME_MAX];
|
||||
char_t **path = ASSET_SEARCH_PATHS;
|
||||
const char_t **path = ASSET_SEARCH_PATHS;
|
||||
do {
|
||||
sprintf(
|
||||
searchPath,
|
||||
*path,
|
||||
ASSET_MANAGER.systemPath,
|
||||
ASSET_ASSET_FILE
|
||||
ASSET.systemPath,
|
||||
ASSET_FILE
|
||||
);
|
||||
|
||||
// Try open
|
||||
@@ -39,13 +40,61 @@ errorret_t assetInit(void) {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
erroret_t assetLoad(const char_t *filename, void *output) {
|
||||
errorret_t assetLoad(const char_t *filename, void *output) {
|
||||
assertStrLenMax(filename, FILENAME_MAX, "Filename too long.");
|
||||
assertNotNull(output, "Output pointer cannot be NULL.");
|
||||
|
||||
// Try to open the file
|
||||
zip_file_t *file = zip_fopen(ASSET.zip, filename, 0);
|
||||
if(file == NULL) {
|
||||
errorThrow("Failed to open asset file: %s", filename);
|
||||
}
|
||||
|
||||
// Read the header.
|
||||
assetheader_t header;
|
||||
memoryZero(&header, sizeof(assetheader_t));
|
||||
zip_int64_t bytesRead = zip_fread(file, &header, sizeof(assetheader_t));
|
||||
if(bytesRead != sizeof(assetheader_t)) {
|
||||
zip_fclose(file);
|
||||
errorThrow("Failed to read asset header for: %s", filename);
|
||||
}
|
||||
|
||||
// Find the asset type based on the header
|
||||
const assettypedef_t *def = NULL;
|
||||
for(uint_fast8_t i = 0; i < ASSET_TYPE_COUNT; i++) {
|
||||
if(ASSET_TYPE_DEFINITIONS[i].header == NULL) continue;
|
||||
if(strcmp(header.header, ASSET_TYPE_DEFINITIONS[i].header) != 0) continue;
|
||||
def = &ASSET_TYPE_DEFINITIONS[i];
|
||||
break;
|
||||
}
|
||||
if(def == NULL) {
|
||||
zip_fclose(file);
|
||||
errorThrow("Unknown asset type for file: %s", filename);
|
||||
}
|
||||
|
||||
// We found the asset type, now load the asset data
|
||||
assertNotNull(def->load, "Asset load function cannot be NULL.");
|
||||
void *data = memoryAllocate(def->dataSize);
|
||||
bytesRead = zip_fread(file, data, def->dataSize);
|
||||
if(bytesRead == 0 || bytesRead > def->dataSize) {
|
||||
memoryFree(data);
|
||||
zip_fclose(file);
|
||||
errorThrow("Failed to read asset data for file: %s", filename);
|
||||
}
|
||||
|
||||
// Close the file now we have the data
|
||||
zip_fclose(file);
|
||||
|
||||
// Pass to the asset type loader
|
||||
errorret_t ret = def->load(data, output);
|
||||
memoryFree(data);
|
||||
errorChain(ret);
|
||||
errorOk();
|
||||
}
|
||||
|
||||
void assetDispose(void) {
|
||||
if(ASSET_MANAGER.zip != NULL) {
|
||||
zip_close(ASSET_MANAGER.zip);
|
||||
ASSET_MANAGER.zip = NULL;
|
||||
if(ASSET.zip != NULL) {
|
||||
zip_close(ASSET.zip);
|
||||
ASSET.zip = NULL;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user