About to implement load strategy

This commit is contained in:
2025-11-08 08:32:21 -06:00
parent 9f88374627
commit cf2aacd75b
10 changed files with 80 additions and 40 deletions

View File

@@ -62,9 +62,10 @@ errorret_t assetLoad(const char_t *filename, void *output) {
// 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];
const assettypedef_t *cmp = &ASSET_TYPE_DEFINITIONS[i];
if(cmp->header == NULL) continue;
if(strcmp(header.header, cmp->header) != 0) continue;
def = cmp;
break;
}
if(def == NULL) {
@@ -73,22 +74,33 @@ errorret_t assetLoad(const char_t *filename, void *output) {
}
// 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);
switch(def->loadStrategy) {
case ASSET_LOAD_STRAT_ENTIRE:
assertNotNull(def->entire, "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->entire(data, output);
memoryFree(data);
errorChain(ret);
break;
case ASSET_LOAD_STRAT_TEST:
assertUnreachable("Asset load strategy not implemented yet.");
default:
assertUnreachable("Unknown asset load strategy.");
}
// 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();
}