3 #include "script_interpreter.hpp"
11 #include <sqstdblob.h>
12 #include <sqstdsystem.h>
13 #include <sqstdmath.h>
14 #include <sqstdstring.h>
16 #include "wrapper.hpp"
17 #include "wrapper_util.hpp"
19 #include "file_system.hpp"
20 #include "game_session.hpp"
21 #include "resources.hpp"
22 #include "physfs/physfs_stream.hpp"
23 #include "object/text_object.hpp"
24 #include "object/scripted_object.hpp"
25 #include "object/display_effect.hpp"
26 #include "scripting/sound.hpp"
27 #include "scripting/scripted_object.hpp"
28 #include "scripting/display_effect.hpp"
30 static void printfunc(HSQUIRRELVM, const char* str, ...)
33 va_start(arglist, str);
34 vprintf(str, arglist);
38 ScriptInterpreter* ScriptInterpreter::_current = 0;
40 ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory)
41 : working_directory(new_working_directory), sound(0), level(0)
45 throw std::runtime_error("Couldn't initialize squirrel vm");
47 // register default error handlers
48 sqstd_seterrorhandlers(v);
49 // register squirrel libs
51 if(sqstd_register_bloblib(v) < 0)
52 throw SquirrelError(v, "Couldn't register blob lib");
53 if(sqstd_register_iolib(v) < 0)
54 throw SquirrelError(v, "Couldn't register io lib");
55 if(sqstd_register_systemlib(v) < 0)
56 throw SquirrelError(v, "Couldn't register system lib");
57 if(sqstd_register_mathlib(v) < 0)
58 throw SquirrelError(v, "Couldn't register math lib");
59 if(sqstd_register_stringlib(v) < 0)
60 throw SquirrelError(v, "Couldn't register string lib");
62 // register print function
63 sq_setprintfunc(v, printfunc);
65 // register supertux API
66 SquirrelWrapper::register_supertux_wrapper(v);
68 // expose some "global" objects
69 sound = new Scripting::Sound();
70 expose_object(sound, "Sound");
72 level = new Scripting::Level();
73 expose_object(level, "Level");
77 ScriptInterpreter::register_sector(Sector* sector)
79 // expose ScriptedObjects to the script
80 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
81 i != sector->gameobjects.end(); ++i) {
82 GameObject* object = *i;
83 Scripting::ScriptedObject* scripted_object
84 = dynamic_cast<Scripting::ScriptedObject*> (object);
88 expose_object(scripted_object, scripted_object->get_name());
91 TextObject* text_object = new TextObject();
92 sector->add_object(text_object);
93 Scripting::Text* text = static_cast<Scripting::Text*> (text_object);
94 expose_object(text, "Text");
96 DisplayEffect* display_effect = new DisplayEffect();
97 sector->add_object(display_effect);
98 Scripting::DisplayEffect* display_effect_api
99 = static_cast<Scripting::DisplayEffect*> (display_effect);
100 expose_object(display_effect_api, "DisplayEffect");
103 ScriptInterpreter::~ScriptInterpreter()
110 static SQInteger squirrel_read_char(SQUserPointer file)
112 std::istream* in = reinterpret_cast<std::istream*> (file);
120 ScriptInterpreter::run_script(std::istream& in, const std::string& sourcename,
121 bool remove_when_terminated)
123 printf("Stackbefore:\n");
124 print_squirrel_stack(v);
125 if(sq_compile(v, squirrel_read_char, &in, sourcename.c_str(), true) < 0)
126 throw SquirrelError(v, "Couldn't parse script");
130 if(sq_call(v, 1, false) < 0)
131 throw SquirrelError(v, "Couldn't start script");
133 if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
134 if(remove_when_terminated) {
138 // remove closure from stack
142 print_squirrel_stack(v);
146 ScriptInterpreter::set_wakeup_time(float seconds)
148 wakeup_timer.start(seconds);
152 ScriptInterpreter::update(float )
154 if(!wakeup_timer.check())
158 if(sq_wakeupvm(v, false, false) < 0)
159 throw SquirrelError(v, "Couldn't resume script");
161 if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
162 printf("script ended...\n");
168 ScriptInterpreter::draw(DrawingContext& )
173 ScriptInterpreter::add_script_object(Sector* sector, const std::string& name,
174 const std::string& script)
177 std::string workdir = GameSession::current()->get_working_directory();
178 std::auto_ptr<ScriptInterpreter> interpreter(
179 new ScriptInterpreter(workdir));
180 interpreter->register_sector(sector);
182 // load default.nut file if it exists
184 std::string filename = workdir + "/default.nut";
185 IFileStream in(filename);
186 interpreter->run_script(in, filename, false);
187 } catch(std::exception& e) {
191 std::istringstream in(script);
192 interpreter->run_script(in, name);
193 sector->add_object(interpreter.release());
194 } catch(std::exception& e) {
195 std::cerr << "Couldn't start '" << name << "' script: " << e.what() << "\n";