From 67583d2d17f660c0b91585ee5fff91398fe6a945 Mon Sep 17 00:00:00 2001 From: Alexander Shishkin Date: Thu, 3 Oct 2024 19:44:18 +0300 Subject: [PATCH] gltf: Propagate failure to load/parse gltf up the call stack Don't crash if there's a problem with a gltf file. Signed-off-by: Alexander Shishkin --- core/gltf.c | 16 +++++++++++++++- core/scene.c | 5 +++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/gltf.c b/core/gltf.c index aef7450..dfb4cb2 100644 --- a/core/gltf.c +++ b/core/gltf.c @@ -622,14 +622,21 @@ static void gltf_onload(struct lib_handle *h, void *data) { JsonNode *nodes, *mats, *meshes, *texs, *imgs, *accrs, *bufvws, *bufs; JsonNode *scenes, *scene, *skins, *anis; - JsonNode *root = json_decode(h->buf); + JsonNode *root; struct gltf_data *gd = data; unsigned int nid; JsonNode *n; + if (h->state == RES_ERROR) { + warn("couldn't load '%s'\n", h->name); + return; + } + + root = json_decode(h->buf); dbg("loading '%s'\n", h->name); if (!root) { warn("couldn't parse '%s'\n", h->name); + h->state = RES_ERROR; return; } @@ -671,6 +678,7 @@ static void gltf_onload(struct lib_handle *h, void *data) meshes->tag, texs->tag, imgs->tag, accrs->tag, bufvws->tag, bufs->tag, anis->tag ); + h->state = RES_ERROR; return; } @@ -1148,11 +1156,17 @@ struct gltf_data *gltf_load(struct scene *scene, const char *name) { struct gltf_data *gd; struct lib_handle *lh; + enum res_state state; CHECK(gd = calloc(1, sizeof(*gd))); gd->scene = scene; lh = lib_request(RES_ASSET, name, gltf_onload, gd); + state = lh->state; ref_put(lh); + if (state == RES_ERROR) { + gltf_free(gd); + return NULL; + } return gd; } diff --git a/core/scene.c b/core/scene.c index 6491cef..b131a73 100644 --- a/core/scene.c +++ b/core/scene.c @@ -426,6 +426,11 @@ static int model_new_from_json(struct scene *scene, JsonNode *node) ref_put_last(libh); } else if (gltf) { gd = gltf_load(scene, gltf); + if (!gd) { + warn("Error loading GLTF '%s'\n", gltf); + return -1; + } + if (gltf_get_meshes(gd) > 1) { int i, root = gltf_root_mesh(gd);