Fixed JSON parsing throwing errors

This commit is contained in:
2024-10-06 22:46:16 -05:00
parent bf3912bb7f
commit ecb3b9c5d1
11 changed files with 129 additions and 5 deletions

View File

@ -13,7 +13,8 @@ size_t assetJsonParse(const char_t *json, assetjson_t **out) {
// We only expect whitespace or EOF here
char_t c;
do {
size_t len = strlen(json);
while(offset <= len) {
c = json[offset];
if(c == '\0') break;
if(c == ' ' || c == '\t' || c == '\n' || c == '\r') {
@ -21,7 +22,9 @@ size_t assetJsonParse(const char_t *json, assetjson_t **out) {
continue;
}
assertUnreachable("Unexpected character found after JSON data.");
} while(true);
}
assertTrue(c == '\0', "Unexpected character found after JSON data.");
assertTrue(offset == len, "Unexpected character found after JSON data.");
return offset;
}

View File

@ -21,6 +21,7 @@ void assetMapLoad(
size_t length = assetGetSize();
char_t *buffer = malloc(sizeof(char_t) * (length + 1));
size_t read = assetRead((uint8_t*)buffer, length);
buffer[length] = '\0';
assertTrue(read == length, "assetMapLoad: Failed to read map data!");
assetClose();
@ -96,9 +97,30 @@ void assetMapLoad(
entityInit(ent, type, map);
entityPositionSet(ent, x, y);
assetjson_t *val;
switch(type) {
case ENTITY_TYPE_NPC:
val = assetJsonGetObjectValue(jEnt, "name");
if(val != NULL) {
assertTrue(
val->type == ASSET_JSON_DATA_TYPE_STRING,
"assetMapLoad: NPC name is not a string!"
);
npcNameSet(&ent->npc, val->string);
}
val = assetJsonGetObjectValue(jEnt, "text");
if(val != NULL) {
assertTrue(
val->type == ASSET_JSON_DATA_TYPE_STRING,
"assetMapLoad: NPC text is not a string!"
);
npcTextSet(&ent->npc, val->string);
}
break;
case ENTITY_TYPE_SIGN:
assetjson_t *val = assetJsonGetObjectValue(jEnt, "text");
val = assetJsonGetObjectValue(jEnt, "text");
if(val != NULL) {
assertTrue(
val->type == ASSET_JSON_DATA_TYPE_STRING,