4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include <sqstdmath.h>
26 #include <sqstdblob.h>
27 #include <sqstdstring.h>
30 #include "squirrel_util.hpp"
33 #include "physfs/physfs_stream.hpp"
34 #include "random_generator.hpp"
37 #include <sqdbg/sqrdbg.h>
39 static HSQREMOTEDBG debugger = NULL;
45 HSQUIRRELVM global_vm = NULL;
47 static void printfunc(HSQUIRRELVM, const char* str, ...)
51 va_start(arglist, str);
52 vsprintf(buf, str, arglist);
53 Console::output << (const char*) buf << std::flush;
57 void init_squirrel(bool enable_debugger)
59 global_vm = sq_open(64);
61 throw std::runtime_error("Couldn't initialize squirrel vm");
65 sq_enabledebuginfo(global_vm, SQTrue);
66 debugger = sq_rdbg_init(global_vm, 1234, SQFalse);
68 throw SquirrelError(global_vm, "Couldn't initialize squirrel debugger");
70 sq_enabledebuginfo(global_vm, SQTrue);
71 log_info << "Waiting for debug client..." << std::endl;
72 if(SQ_FAILED(sq_rdbg_waitforconnections(debugger)))
73 throw SquirrelError(global_vm, "Waiting for debug clients failed");
74 log_info << "debug client connected." << std::endl;
78 sq_pushroottable(global_vm);
79 if(SQ_FAILED(sqstd_register_bloblib(global_vm)))
80 throw SquirrelError(global_vm, "Couldn't register blob lib");
81 if(SQ_FAILED(sqstd_register_mathlib(global_vm)))
82 throw SquirrelError(global_vm, "Couldn't register math lib");
83 if(SQ_FAILED(sqstd_register_stringlib(global_vm)))
84 throw SquirrelError(global_vm, "Couldn't register string lib");
86 // remove rand and srand calls from sqstdmath, we'll provide our own
87 sq_pushstring(global_vm, "srand", -1);
88 sq_deleteslot(global_vm, -2, SQFalse);
89 sq_pushstring(global_vm, "rand", -1);
90 sq_deleteslot(global_vm, -2, SQFalse);
92 // register supertux API
93 register_supertux_wrapper(global_vm);
95 // TODO remove this at some point... it should just be functions not an object
96 expose_object(global_vm, -1, new Scripting::Level(), "Level", true);
100 // register print function
101 sq_setprintfunc(global_vm, printfunc);
102 // register default error handlers
103 sqstd_seterrorhandlers(global_vm);
105 // try to load default script
107 std::string filename = "scripts/default.nut";
108 IFileStream stream(filename);
109 Scripting::compile_and_run(global_vm, stream, filename);
110 } catch(std::exception& e) {
111 log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
118 if(debugger != NULL) {
119 sq_rdbg_shutdown(debugger);
130 void update_debugger()
134 sq_rdbg_update(debugger);
138 std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
140 std::ostringstream os;
141 switch(sq_gettype(v, i))
148 sq_getbool(v, i, &p);
157 sq_getinteger(v, i, &val);
163 sq_getfloat(v, i, &val);
169 sq_getstring(v, i, &val);
170 os << "\"" << val << "\"";
176 sq_pushnull(v); //null iterator
177 while(SQ_SUCCEEDED(sq_next(v,i-1)))
184 //here -1 is the value and -2 is the key
185 os << squirrel2string(v, -2) << " => "
186 << squirrel2string(v, -1);
188 sq_pop(v,2); //pops key and val before the nex iteration
197 sq_pushnull(v); //null iterator
198 while(SQ_SUCCEEDED(sq_next(v,i-1)))
205 //here -1 is the value and -2 is the key
206 // we ignore the key, since that is just the index in an array
207 os << squirrel2string(v, -1);
209 sq_pop(v,2); //pops key and val before the nex iteration
221 case OT_NATIVECLOSURE:
222 os << "<native closure>";
249 void print_squirrel_stack(HSQUIRRELVM v)
251 printf("--------------------------------------------------------------\n");
252 int count = sq_gettop(v);
253 for(int i = 1; i <= count; ++i) {
255 switch(sq_gettype(v, i))
262 sq_getinteger(v, i, &val);
263 printf("integer (%d)", static_cast<int> (val));
268 sq_getfloat(v, i, &val);
269 printf("float (%f)", val);
274 sq_getstring(v, i, &val);
275 printf("string (%s)", val);
288 printf("closure(function)");
290 case OT_NATIVECLOSURE:
291 printf("native closure(C function)");
297 printf("userpointer");
312 printf("unknown?!?");
317 printf("--------------------------------------------------------------\n");
320 static SQInteger squirrel_read_char(SQUserPointer file)
322 std::istream* in = reinterpret_cast<std::istream*> (file);
329 void compile_script(HSQUIRRELVM vm, std::istream& in, const std::string& sourcename)
331 if(SQ_FAILED(sq_compile(vm, squirrel_read_char, &in, sourcename.c_str(), true)))
332 throw SquirrelError(vm, "Couldn't parse script");
335 void compile_and_run(HSQUIRRELVM vm, std::istream& in,
336 const std::string& sourcename)
338 compile_script(vm, in, sourcename);
340 SQInteger oldtop = sq_gettop(vm);
343 sq_pushroottable(vm);
344 if(SQ_FAILED(sq_call(vm, 1, SQFalse, SQTrue)))
345 throw SquirrelError(vm, "Couldn't start script");
347 sq_settop(vm, oldtop);
351 // we can remove the closure in case the script was not suspended
352 if(sq_getvmstate(vm) != SQ_VMSTATE_SUSPENDED) {
353 sq_settop(vm, oldtop-1);
357 HSQOBJECT create_thread(HSQUIRRELVM vm)
359 HSQUIRRELVM new_vm = sq_newthread(vm, 64);
361 throw SquirrelError(vm, "Couldn't create new VM");
364 sq_resetobject(&vm_object);
365 if(SQ_FAILED(sq_getstackobj(vm, -1, &vm_object)))
366 throw SquirrelError(vm, "Couldn't get squirrel thread from stack");
367 sq_addref(vm, &vm_object);
374 HSQOBJECT vm_to_object(HSQUIRRELVM vm)
377 sq_resetobject(&object);
378 object._unVal.pThread = vm;
379 object._type = OT_THREAD;
384 HSQUIRRELVM object_to_vm(HSQOBJECT object)
386 if(object._type != OT_THREAD)
389 return object._unVal.pThread;
392 // begin: serialization functions
394 void store_float(HSQUIRRELVM vm, const char* name, float val)
396 sq_pushstring(vm, name, -1);
397 sq_pushfloat(vm, val);
398 if(SQ_FAILED(sq_createslot(vm, -3)))
399 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
402 void store_int(HSQUIRRELVM vm, const char* name, int val)
404 sq_pushstring(vm, name, -1);
405 sq_pushinteger(vm, val);
406 if(SQ_FAILED(sq_createslot(vm, -3)))
407 throw Scripting::SquirrelError(vm, "Couldn't add int value to table");
410 void store_string(HSQUIRRELVM vm, const char* name, const std::string& val)
412 sq_pushstring(vm, name, -1);
413 sq_pushstring(vm, val.c_str(), val.length());
414 if(SQ_FAILED(sq_createslot(vm, -3)))
415 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
418 void store_bool(HSQUIRRELVM vm, const char* name, bool val)
420 sq_pushstring(vm, name, -1);
421 sq_pushbool(vm, val ? SQTrue : SQFalse);
422 if(SQ_FAILED(sq_createslot(vm, -3)))
423 throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
426 bool has_float(HSQUIRRELVM vm, const char* name)
428 sq_pushstring(vm, name, -1);
429 if (SQ_FAILED(sq_get(vm, -2))) return false;
434 bool has_int(HSQUIRRELVM vm, const char* name)
436 return has_float(vm, name);
439 bool has_string(HSQUIRRELVM vm, const char* name)
441 return has_float(vm, name);
444 bool has_bool(HSQUIRRELVM vm, const char* name)
446 return has_float(vm, name);
449 float read_float(HSQUIRRELVM vm, const char* name)
451 sq_pushstring(vm, name, -1);
452 if(SQ_FAILED(sq_get(vm, -2))) {
453 std::ostringstream msg;
454 msg << "Couldn't get float value for '" << name << "' from table";
455 throw Scripting::SquirrelError(vm, msg.str());
459 if(SQ_FAILED(sq_getfloat(vm, -1, &result))) {
460 std::ostringstream msg;
461 msg << "Couldn't get float value for '" << name << "' from table";
462 throw Scripting::SquirrelError(vm, msg.str());
469 int read_int(HSQUIRRELVM vm, const char* name)
471 sq_pushstring(vm, name, -1);
472 if(SQ_FAILED(sq_get(vm, -2))) {
473 std::ostringstream msg;
474 msg << "Couldn't get int value for '" << name << "' from table";
475 throw Scripting::SquirrelError(vm, msg.str());
479 if(SQ_FAILED(sq_getinteger(vm, -1, &result))) {
480 std::ostringstream msg;
481 msg << "Couldn't get int value for '" << name << "' from table";
482 throw Scripting::SquirrelError(vm, msg.str());
490 std::string read_string(HSQUIRRELVM vm, const char* name)
492 sq_pushstring(vm, name, -1);
493 if(SQ_FAILED(sq_get(vm, -2))) {
494 std::ostringstream msg;
495 msg << "Couldn't get string value for '" << name << "' from table";
496 throw Scripting::SquirrelError(vm, msg.str());
500 if(SQ_FAILED(sq_getstring(vm, -1, &result))) {
501 std::ostringstream msg;
502 msg << "Couldn't get string value for '" << name << "' from table";
503 throw Scripting::SquirrelError(vm, msg.str());
507 return std::string(result);
510 bool read_bool(HSQUIRRELVM vm, const char* name)
512 sq_pushstring(vm, name, -1);
513 if(SQ_FAILED(sq_get(vm, -2))) {
514 std::ostringstream msg;
515 msg << "Couldn't get bool value for '" << name << "' from table";
516 throw Scripting::SquirrelError(vm, msg.str());
520 if(SQ_FAILED(sq_getbool(vm, -1, &result))) {
521 std::ostringstream msg;
522 msg << "Couldn't get bool value for '" << name << "' from table";
523 throw Scripting::SquirrelError(vm, msg.str());
527 return result == SQTrue;
530 bool get_float(HSQUIRRELVM vm, const char* name, float& val) {
531 if (!has_float(vm, name)) return false;
532 val = read_float(vm, name);
536 bool get_int(HSQUIRRELVM vm, const char* name, int& val) {
537 if (!has_int(vm, name)) return false;
538 val = read_int(vm, name);
542 bool get_string(HSQUIRRELVM vm, const char* name, std::string& val) {
543 if (!has_string(vm, name)) return false;
544 val = read_string(vm, name);
548 bool get_bool(HSQUIRRELVM vm, const char* name, bool& val) {
549 if (!has_bool(vm, name)) return false;
550 val = read_bool(vm, name);
554 // end: serialization functions