3b1a1d4b3931e06fae0997f0534b123577e754a9
[supertux.git] / src / scripting / script_interpreter.cpp
1 #include <config.h>
2
3 #include "script_interpreter.hpp"
4
5 #include <stdarg.h>
6 #include <stdexcept>
7 #include <sstream>
8 #include <fstream>
9 #include <sqstdio.h>
10 #include <sqstdaux.h>
11 #include <sqstdblob.h>
12 #include <sqstdsystem.h>
13 #include <sqstdmath.h>
14 #include <sqstdstring.h>
15
16 #include "wrapper.hpp"
17 #include "wrapper_util.hpp"
18 #include "sector.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"
29
30 static void printfunc(HSQUIRRELVM, const char* str, ...)
31 {
32   va_list arglist;
33   va_start(arglist, str);
34   vprintf(str, arglist);
35   va_end(arglist);
36 }
37
38 ScriptInterpreter* ScriptInterpreter::_current = 0;
39
40 ScriptInterpreter::ScriptInterpreter(const std::string& new_working_directory)
41   : working_directory(new_working_directory), sound(0), level(0)
42 {
43   v = sq_open(1024);
44   if(v == 0)
45     throw std::runtime_error("Couldn't initialize squirrel vm");
46
47   // register default error handlers
48   sqstd_seterrorhandlers(v);
49   // register squirrel libs
50   sq_pushroottable(v);
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");
61
62   // register print function
63   sq_setprintfunc(v, printfunc);
64   
65   // register supertux API
66   register_supertux_wrapper(v);
67
68   // expose some "global" objects
69   sound = new Scripting::Sound();
70   expose_object(sound, "Sound", "Sound");  
71   
72   level = new Scripting::Level();
73   expose_object(level, "Level", "Level");
74 }
75
76 void
77 ScriptInterpreter::register_sector(Sector* sector)
78 {
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);
85     if(!scripted_object)
86       continue;
87     
88     expose_object(scripted_object, scripted_object->get_name(), 
89         "ScriptedObject");
90   }
91   
92   TextObject* text_object = new TextObject();
93   sector->add_object(text_object);
94   Scripting::Text* text = static_cast<Scripting::Text*> (text_object);
95   expose_object(text, "Text", "Text");
96   
97   DisplayEffect* display_effect = new DisplayEffect();
98   sector->add_object(display_effect);
99   Scripting::DisplayEffect* display_effect_api
100     = static_cast<Scripting::DisplayEffect*> (display_effect);
101   expose_object(display_effect_api, "DisplayEffect", "DisplayEffect");
102 }
103
104 ScriptInterpreter::~ScriptInterpreter()
105 {
106   sq_close(v);
107   delete sound;
108   delete level;
109 }
110
111 static SQInteger squirrel_read_char(SQUserPointer file)
112 {
113   std::istream* in = reinterpret_cast<std::istream*> (file);
114   char c = in->get();
115   if(in->eof())
116     return 0;    
117   return c;
118 }
119
120 void
121 ScriptInterpreter::run_script(std::istream& in, const std::string& sourcename,
122         bool remove_when_terminated)
123 {
124   printf("Stackbefore:\n");
125   print_squirrel_stack(v);
126   if(sq_compile(v, squirrel_read_char, &in, sourcename.c_str(), true) < 0)
127     throw SquirrelError(v, "Couldn't parse script");
128  
129   _current = this;
130   sq_push(v, -2);
131   if(sq_call(v, 1, false) < 0)
132     throw SquirrelError(v, "Couldn't start script");
133   _current = 0;
134   if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
135     if(remove_when_terminated) {
136       remove_me();
137     }
138     printf("ended.\n");
139     // remove closure from stack
140     sq_pop(v, 1);
141   }
142   printf("After:\n");
143   print_squirrel_stack(v);
144 }
145
146 void
147 ScriptInterpreter::expose_object(void* object, const std::string& name,
148                                  const std::string& type)
149 {
150   // part1 of registration of the instance in the root table
151   sq_pushroottable(v);
152   sq_pushstring(v, name.c_str(), -1);
153
154   // resolve class name
155   sq_pushroottable(v);
156   sq_pushstring(v, type.c_str(), -1);
157   if(sq_get(v, -2) < 0) {
158     std::ostringstream msg;
159     msg << "Couldn't resolve squirrel type '" << type << "'.";
160     throw std::runtime_error(msg.str());
161   }
162   sq_remove(v, -2); // remove roottable
163   
164   // create an instance and set pointer to c++ object
165   if(sq_createinstance(v, -1) < 0 || sq_setinstanceup(v, -1, object)) {
166     std::ostringstream msg;
167     msg << "Couldn't setup squirrel instance for object '"
168         << name << "' of type '" << type << "'.";
169     throw SquirrelError(v, msg.str());
170   }
171   
172   sq_remove(v, -2); // remove class from stack
173   
174   // part2 of registration of the instance in the root table
175   if(sq_createslot(v, -3) < 0)
176     throw SquirrelError(v, "Couldn't register object in squirrel root table");    sq_pop(v, 1);
177 }
178
179 void
180 ScriptInterpreter::set_wakeup_time(float seconds)
181 {
182   wakeup_timer.start(seconds);
183 }
184
185 void
186 ScriptInterpreter::update(float )
187 {
188   if(!wakeup_timer.check())
189     return;
190   
191   _current = this;
192   if(sq_wakeupvm(v, false, false) < 0)
193     throw SquirrelError(v, "Couldn't resume script");
194   _current = 0;
195   if(sq_getvmstate(v) != SQ_VMSTATE_SUSPENDED) {
196     printf("script ended...\n");
197     remove_me();
198   }
199 }
200
201 void
202 ScriptInterpreter::draw(DrawingContext& )
203 {
204 }
205
206 void
207 ScriptInterpreter::add_script_object(Sector* sector, const std::string& name,
208     const std::string& script)
209 {
210   try {
211     std::string workdir = GameSession::current()->get_working_directory();
212     std::auto_ptr<ScriptInterpreter> interpreter(
213                 new ScriptInterpreter(workdir));
214     interpreter->register_sector(sector);
215
216     // load default.nut file if it exists
217     try {
218       std::string filename = workdir + "/default.nut";
219       IFileStream in(filename);
220       interpreter->run_script(in, filename, false);
221     } catch(std::exception& e) {
222       // nothing
223     }
224         
225     std::istringstream in(script);
226     interpreter->run_script(in, name);
227     sector->add_object(interpreter.release());
228   } catch(std::exception& e) {
229     std::cerr << "Couldn't start '" << name << "' script: " << e.what() << "\n";
230   }
231 }
232