Fixed JSON loader, added some tests

This commit is contained in:
2026-06-01 15:31:22 -05:00
parent 1f3a29f89d
commit 8b2b4b7c3d
6 changed files with 593 additions and 7 deletions
+5 -4
View File
@@ -70,10 +70,11 @@ extern assetloadercallbacks_t ASSET_LOADER_CALLBACKS[ASSET_LOADER_TYPE_COUNT];
* @param loading The asset loading slot.
* @param ret The error return value to check and chain if it's an error.
*/
#define assetLoaderErrorChain(loading, ret) {\
if(errorIsNotOk(ret)) { \
loading->entry->state = ASSET_ENTRY_STATE_ERROR; \
errorChain(ret); \
#define assetLoaderErrorChain(loading, _expr) {\
errorret_t _alec = (_expr); \
if(errorIsNotOk(_alec)) { \
(loading)->entry->state = ASSET_ENTRY_STATE_ERROR; \
errorChain(_alec); \
} \
}
+5 -3
View File
@@ -27,14 +27,16 @@ errorret_t assetJsonLoaderAsync(assetloading_t *loading) {
assetLoaderErrorThrow(loading, "JSON exceeds maximum allowed size");
}
uint8_t *buffer = memoryAllocate(file->size);
size_t fileSize = (size_t)file->size;
uint8_t *buffer = memoryAllocate(fileSize);
assetLoaderErrorChain(loading, assetFileOpen(file));
assetLoaderErrorChain(loading, assetFileRead(file, buffer, file->size));
assetLoaderErrorChain(loading, assetFileRead(file, buffer, fileSize));
assertTrue(file->lastRead == file->size, "Failed to read entire JSON file.");
assetLoaderErrorChain(loading, assetFileClose(file));
assetLoaderErrorChain(loading, assetFileDispose(file));
loading->loading.json.buffer = buffer;
loading->loading.json.size = fileSize;
loading->loading.json.state = ASSET_JSON_LOADING_STATE_PARSE;
loading->entry->state = ASSET_ENTRY_STATE_PENDING_SYNC;
errorOk();
@@ -63,7 +65,7 @@ errorret_t assetJsonLoaderSync(assetloading_t *loading) {
loading->entry->data.json = yyjson_read(
(char *)buffer,
loading->loading.json.file.size,
loading->loading.json.size,
YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS
);
memoryFree(buffer);
@@ -27,6 +27,7 @@ typedef struct {
assetfile_t file;
assetjsonloadingstate_t state;
uint8_t *buffer;
size_t size;
} assetjsonloaderloading_t;
typedef yyjson_doc * assetjsonoutput_t;