X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fworld.cpp;h=121718e342573211b2a6bda916ccba6f6bbdd6b3;hb=77d00a6f464cc87c6b50cf1f3b38064a7ae9aead;hp=53fcdb716a333535360ab2242e57a94c62dd99eb;hpb=fc73efa7ff699fe3c9c237845b6f4fda0d999862;p=supertux.git diff --git a/src/world.cpp b/src/world.cpp index 53fcdb716..121718e34 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -28,8 +28,7 @@ #include "lisp/parser.hpp" #include "lisp/lisp.hpp" #include "physfs/physfs_stream.hpp" -#include "script_manager.hpp" -#include "scripting/wrapper_util.hpp" +#include "scripting/squirrel_util.hpp" #include "scripting/serialize.hpp" #include "log.hpp" #include "worldmap/worldmap.hpp" @@ -49,10 +48,12 @@ World::World() { is_levelset = true; hide_from_contribs = false; + sq_resetobject(&world_thread); } World::~World() { + sq_release(Scripting::global_vm, &world_thread); if(current_ == this) current_ = NULL; } @@ -71,7 +72,7 @@ World::set_savegame_filename(const std::string& filename) throw std::runtime_error(msg.str()); } } - + if(!PHYSFS_isDirectory(dirname.c_str())) { std::ostringstream msg; msg << "Savegame path '" << dirname << "' is not a directory"; @@ -83,7 +84,7 @@ void World::load(const std::string& filename) { basedir = FileSystem::dirname(filename); - + lisp::Parser parser; std::auto_ptr root (parser.parse(filename)); @@ -104,7 +105,7 @@ World::load(const std::string& filename) // Level info file doesn't define any levels, so read the // directory to see what we can find - + std::string path = basedir + "/"; char** files = PHYSFS_enumerateFiles(path.c_str()); if(!files) { @@ -123,10 +124,12 @@ World::load(const std::string& filename) void World::run() { + using namespace Scripting; + current_ = this; - + // create new squirrel table for persisten game state - HSQUIRRELVM vm = ScriptManager::instance->get_vm(); + HSQUIRRELVM vm = Scripting::global_vm; sq_pushroottable(vm); sq_pushstring(vm, "state", -1); @@ -136,13 +139,14 @@ World::run() sq_pop(vm, 1); load_state(); - + std::string filename = basedir + "/world.nut"; try { IFileStream in(filename); - HSQUIRRELVM new_vm = ScriptManager::instance->create_thread(); - Scripting::compile_and_run(new_vm, in, filename); + sq_release(global_vm, &world_thread); + world_thread = create_thread(global_vm); + compile_and_run(object_to_vm(world_thread), in, filename); } catch(std::exception& e) { // fallback: try to load worldmap worldmap.stwm using namespace WorldMapNS; @@ -153,16 +157,18 @@ World::run() void World::save_state() { + using namespace Scripting; + lisp::Writer writer(savegame_filename); writer.start_list("supertux-savegame"); writer.write_int("version", 1); - + using namespace WorldMapNS; if(WorldMap::current() != NULL) { std::ostringstream title; title << WorldMap::current()->get_title(); - title << " (" << WorldMap::current()->solved_level_count() + title << " (" << WorldMap::current()->solved_level_count() << "/" << WorldMap::current()->level_count() << ")"; writer.write_string("title", title.str()); } @@ -172,22 +178,24 @@ World::save_state() writer.end_list("tux"); writer.start_list("state"); - HSQUIRRELVM vm = ScriptManager::instance->get_vm(); - sq_pushroottable(vm); - sq_pushstring(vm, "state", -1); - if(SQ_SUCCEEDED(sq_get(vm, -2))) { - Scripting::save_squirrel_table(vm, -1, writer); - sq_pop(vm, 1); + + sq_pushroottable(global_vm); + sq_pushstring(global_vm, "state", -1); + if(SQ_SUCCEEDED(sq_get(global_vm, -2))) { + Scripting::save_squirrel_table(global_vm, -1, writer); + sq_pop(global_vm, 1); } - sq_pop(vm, 1); + sq_pop(global_vm, 1); writer.end_list("state"); - + writer.end_list("supertux-savegame"); } void World::load_state() { + using namespace Scripting; + try { lisp::Parser parser; std::auto_ptr root (parser.parse(savegame_filename)); @@ -209,19 +217,18 @@ World::load_state() const lisp::Lisp* state = lisp->get_lisp("state"); if(state == NULL) throw std::runtime_error("No state section in savegame"); - - HSQUIRRELVM vm = ScriptManager::instance->get_vm(); - sq_pushroottable(vm); - sq_pushstring(vm, "state", -1); - if(SQ_FAILED(sq_deleteslot(vm, -2, SQFalse))) - sq_pop(vm, 1); - - sq_pushstring(vm, "state", -1); - sq_newtable(vm); - Scripting::load_squirrel_table(vm, -1, state); - if(SQ_FAILED(sq_createslot(vm, -3))) + + 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(vm, 1); + sq_pop(global_vm, 1); } catch(std::exception& e) { log_debug << "Couldn't load savegame: " << e.what() << std::endl; }