Skip to content

Commit

Permalink
gltf: Propagate failure to load/parse gltf up the call stack
Browse files Browse the repository at this point in the history
Don't crash if there's a problem with a gltf file.

Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Oct 3, 2024
1 parent fe2a265 commit 67583d2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
16 changes: 15 additions & 1 deletion core/gltf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions core/scene.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 67583d2

Please sign in to comment.