From 9b9a5f911e966693ceee7819fa27bb293c1bd1fe Mon Sep 17 00:00:00 2001 From: mathnerd314 Date: Wed, 3 Mar 2010 06:54:58 +0000 Subject: [PATCH] Check that savegame exists before trying to load it. git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6527 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- src/supertux/world.cpp | 70 ++++++++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/supertux/world.cpp b/src/supertux/world.cpp index 2df22f37f..60fe2092d 100644 --- a/src/supertux/world.cpp +++ b/src/supertux/world.cpp @@ -197,41 +197,43 @@ World::load_state() { using namespace scripting; - try { - lisp::Parser parser; - const lisp::Lisp* root = parser.parse(savegame_filename); - - const lisp::Lisp* lisp = root->get_lisp("supertux-savegame"); - if(lisp == NULL) - throw std::runtime_error("file is not a supertux-savegame file"); - - int version = 1; - lisp->get("version", version); - if(version != 1) - throw std::runtime_error("incompatible savegame version"); - - const lisp::Lisp* tux = lisp->get_lisp("tux"); - if(tux == NULL) - throw std::runtime_error("No tux section in savegame"); - player_status->read(*tux); - - const lisp::Lisp* state = lisp->get_lisp("state"); - if(state == NULL) - throw std::runtime_error("No state section in savegame"); - - sq_pushroottable(global_vm); - sq_pushstring(global_vm, "state", -1); - if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse))) + if(PHYSFS_exists(savegame_filename)) { + try { + lisp::Parser parser; + const lisp::Lisp* root = parser.parse(savegame_filename); + + const lisp::Lisp* lisp = root->get_lisp("supertux-savegame"); + if(lisp == NULL) + throw std::runtime_error("file is not a supertux-savegame file"); + + int version = 1; + lisp->get("version", version); + if(version != 1) + throw std::runtime_error("incompatible savegame version"); + + const lisp::Lisp* tux = lisp->get_lisp("tux"); + if(tux == NULL) + throw std::runtime_error("No tux section in savegame"); + player_status->read(*tux); + + const lisp::Lisp* state = lisp->get_lisp("state"); + if(state == NULL) + throw std::runtime_error("No state section in savegame"); + + sq_pushroottable(global_vm); + sq_pushstring(global_vm, "state", -1); + if(SQ_FAILED(sq_deleteslot(global_vm, -2, SQFalse))) + sq_pop(global_vm, 1); + + sq_pushstring(global_vm, "state", -1); + sq_newtable(global_vm); + load_squirrel_table(global_vm, -1, *state); + if(SQ_FAILED(sq_createslot(global_vm, -3))) + throw std::runtime_error("Couldn't create state table"); sq_pop(global_vm, 1); - - sq_pushstring(global_vm, "state", -1); - sq_newtable(global_vm); - load_squirrel_table(global_vm, -1, *state); - if(SQ_FAILED(sq_createslot(global_vm, -3))) - throw std::runtime_error("Couldn't create state table"); - sq_pop(global_vm, 1); - } catch(std::exception& e) { - log_debug << "Couldn't load savegame: " << e.what() << std::endl; + } catch(std::exception& e) { + log_fatal << "Couldn't load savegame: " << e.what() << std::endl; + } } } -- 2.11.0