tolerate missing sprites
Do not try to fulfil the promise twice in case both requests to the sprite JSON and image fail. this will crash the program. Instead, we always continue with the promise, instead of throwing an exception. This allows the program to continue parsing tiles, but without an image sprite available. This means the map will render, but without the sprite images
This commit is contained in:
@@ -54,7 +54,6 @@ Sprite::operator bool() const {
|
||||
// The reason this isn't part of the constructor is that calling shared_from_this() in
|
||||
// the constructor fails.
|
||||
void Sprite::load(Environment &env) {
|
||||
|
||||
if (!valid) {
|
||||
// Treat a non-existent sprite as a successfully loaded empty sprite.
|
||||
loadedImage = true;
|
||||
@@ -69,22 +68,22 @@ void Sprite::load(Environment &env) {
|
||||
if (res.status == Response::Successful) {
|
||||
sprite->body = res.data;
|
||||
sprite->parseJSON();
|
||||
sprite->complete();
|
||||
} else {
|
||||
Log::Warning(Event::Sprite, "Failed to load sprite info: %s", res.message.c_str());
|
||||
sprite->promise.set_exception(std::make_exception_ptr(std::runtime_error(res.message)));
|
||||
}
|
||||
sprite->loadedJSON = true;
|
||||
sprite->complete();
|
||||
});
|
||||
|
||||
env.request({ Resource::Kind::Image, spriteURL }, [sprite](const Response &res) {
|
||||
if (res.status == Response::Successful) {
|
||||
sprite->image = res.data;
|
||||
sprite->parseImage();
|
||||
sprite->complete();
|
||||
} else {
|
||||
Log::Warning(Event::Sprite, "Failed to load sprite image: %s", res.message.c_str());
|
||||
sprite->promise.set_exception(std::make_exception_ptr(std::runtime_error(res.message)));
|
||||
}
|
||||
sprite->loadedImage = true;
|
||||
sprite->complete();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -104,7 +103,6 @@ void Sprite::parseImage() {
|
||||
raster.reset();
|
||||
}
|
||||
image.clear();
|
||||
loadedImage = true;
|
||||
}
|
||||
|
||||
void Sprite::parseJSON() {
|
||||
@@ -139,8 +137,6 @@ void Sprite::parseJSON() {
|
||||
} else {
|
||||
Log::Warning(Event::Sprite, "sprite JSON root is not an object");
|
||||
}
|
||||
|
||||
loadedJSON = true;
|
||||
}
|
||||
|
||||
const SpritePosition &Sprite::getSpritePosition(const std::string& name) const {
|
||||
|
||||
Reference in New Issue
Block a user