3 #include "script_interpreter.h"
10 #include <sqstdblob.h>
11 #include <sqstdsystem.h>
12 #include <sqstdmath.h>
13 #include <sqstdstring.h>
16 #include "wrapper_util.h"
18 #include "file_system.h"
19 #include "game_session.h"
20 #include "object/text_object.h"
21 #include "object/scripted_object.h"
22 #include "object/display_effect.h"
23 #include "scripting/sound.h"
24 #include "scripting/scripted_object.h"
25 #include "scripting/display_effect.h"
27 static void printfunc(HSQUIRRELVM, const char* str, ...)
30 va_start(arglist, str);
31 vprintf(str, arglist);
35 ScriptInterpreter* ScriptInterpreter::_current = 0;
37 ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory)
38 : working_directory(new_working_directory), sound(0), level(0)
42 throw std::runtime_error("Couldn't initialize squirrel vm");
44 // register default error handlers
45 sqstd_seterrorhandlers(v);
46 // register squirrel libs
48 if(sqstd_register_bloblib(v) < 0)
49 throw SquirrelError(v, "Couldn't register blob lib");
50 if(sqstd_register_iolib(v) < 0)
51 throw SquirrelError(v, "Couldn't register io lib");
52 if(sqstd_register_systemlib(v) < 0)
53 throw SquirrelError(v, "Couldn't register system lib");
54 if(sqstd_register_mathlib(v) < 0)
55 throw SquirrelError(v, "Couldn't register math lib");
56 if(sqstd_register_stringlib(v) < 0)
57 throw SquirrelError(v, "Couldn't register string lib");
59 // register print function
60 sq_setprintfunc(v, printfunc);
62 // register supertux API
63 register_functions(v, supertux_global_functions);
64 register_classes(v, supertux_classes);
66 // expose some "global" objects
67 sound = new Scripting::Sound();
68 expose_object(sound, "Sound", "Sound");
70 level = new Scripting::Level();
71 expose_object(level, "Level", "Level");
75 ScriptInterpreter::register_sector(Sector* sector)
77 // expose ScriptedObjects to the script
78 for(Sector::GameObjects::iterator i = sector->gameobjects.begin();
79 i != sector->gameobjects.end(); ++i) {
80 GameObject* object = *i;
81 Scripting::ScriptedObject* scripted_object
82 = dynamic_cast<Scripting::ScriptedObject*> (object);
86 expose_object(scripted_object, scripted_object->get_name(),
90 TextObject* text_object = new TextObject();
91 sector->add_object(text_object);
92 Scripting::Text* text = static_cast<Scripting::Text*> (text_object);
93 expose_object(text, "Text", "Text");
95 DisplayEffect* display_effect = new DisplayEffect();
96 sector->add_object(display_effect);
97 Scripting::DisplayEffect* display_effect_api
98 = static_cast<Scripting::DisplayEffect*> (display_effect);
99 expose_object(display_effect_api, "DisplayEffect", "DisplayEffect");
102 ScriptInterpreter::~ScriptInterpreter()
109 static SQInteger squirrel_read_char(SQUserPointer file)
111 std::istream* in = reinterpret_cast<std::istream*> (file);
120 ScriptInterpreter::load_script(std::istream& in, const std::string& sourcename)
122 if(sq_compile(v, squirrel_read_char, &in, sourcename.c_str(), true) < 0)
123 throw SquirrelError(v, "Couldn't parse script");
127 ScriptInterpreter::start_script()
131 if(sq_call(v, 1, false) < 0)
132 throw SquirrelError(v, "Couldn't start script");
134 if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
135 printf("script ended...\n");
141 ScriptInterpreter::expose_object(void* object, const std::string& name,
142 const std::string& type)
144 // part1 of registration of the instance in the root table
146 sq_pushstring(v, name.c_str(), -1);
148 // resolve class name
150 sq_pushstring(v, type.c_str(), -1);
151 if(sq_get(v, -2) < 0) {
152 std::ostringstream msg;
153 msg << "Couldn't resolve squirrel type '" << type << "'.";
154 throw std::runtime_error(msg.str());
156 sq_remove(v, -2); // remove roottable
158 // create an instance and set pointer to c++ object
159 if(sq_createinstance(v, -1) < 0 || sq_setinstanceup(v, -1, object)) {
160 std::ostringstream msg;
161 msg << "Couldn't setup squirrel instance for object '"
162 << name << "' of type '" << type << "'.";
163 throw SquirrelError(v, msg.str());
166 sq_remove(v, -2); // remove class from stack
168 // part2 of registration of the instance in the root table
169 if(sq_createslot(v, -3) < 0)
170 throw SquirrelError(v, "Couldn't register object in squirrel root table"); sq_pop(v, 1);
174 ScriptInterpreter::set_wakeup_time(float seconds)
176 wakeup_timer.start(seconds);
180 ScriptInterpreter::update(float )
182 if(!wakeup_timer.check())
186 if(sq_wakeupvm(v, false, false) < 0)
187 throw SquirrelError(v, "Couldn't resume script");
189 if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
190 printf("script ended...\n");
196 ScriptInterpreter::draw(DrawingContext& )
201 ScriptInterpreter::add_script_object(Sector* sector, const std::string& name,
202 const std::string& script)
205 std::auto_ptr<ScriptInterpreter> interpreter
206 (new ScriptInterpreter(GameSession::current()->get_working_directory()));
207 interpreter->register_sector(sector);
208 std::istringstream in(script);
209 interpreter->load_script(in, name);
210 interpreter->start_script();
211 sector->add_object(interpreter.release());
212 } catch(std::exception& e) {
213 std::cerr << "Couldn't start '" << name << "' script: " << e.what() << "\n";