#include <sqstdstring.h>
#include <stdarg.h>
-#include "physfs/physfs_stream.hpp"
-#include "supertux/console.hpp"
-#include "util/log.hpp"
-
-#ifdef ENABLE_SQDBG
-
-static HSQREMOTEDBG debugger = NULL;
-#endif
-
-namespace Scripting {
-
-HSQUIRRELVM global_vm = NULL;
-
-static void printfunc(HSQUIRRELVM, const char* str, ...)
-{
- char buf[4096];
- va_list arglist;
- va_start(arglist, str);
- vsprintf(buf, str, arglist);
- Console::output << (const char*) buf << std::flush;
- va_end(arglist);
-}
-
-void init_squirrel(bool enable_debugger)
-{
- global_vm = sq_open(64);
- if(global_vm == NULL)
- throw std::runtime_error("Couldn't initialize squirrel vm");
-
- if(enable_debugger) {
-#ifdef ENABLE_SQDBG
- sq_enabledebuginfo(global_vm, SQTrue);
- debugger = sq_rdbg_init(global_vm, 1234, SQFalse);
- if(debugger == NULL)
- throw SquirrelError(global_vm, "Couldn't initialize squirrel debugger");
-
- sq_enabledebuginfo(global_vm, SQTrue);
- log_info << "Waiting for debug client..." << std::endl;
- if(SQ_FAILED(sq_rdbg_waitforconnections(debugger)))
- throw SquirrelError(global_vm, "Waiting for debug clients failed");
- log_info << "debug client connected." << std::endl;
-#endif
- }
-
- sq_pushroottable(global_vm);
- if(SQ_FAILED(sqstd_register_bloblib(global_vm)))
- throw SquirrelError(global_vm, "Couldn't register blob lib");
- if(SQ_FAILED(sqstd_register_mathlib(global_vm)))
- throw SquirrelError(global_vm, "Couldn't register math lib");
- if(SQ_FAILED(sqstd_register_stringlib(global_vm)))
- throw SquirrelError(global_vm, "Couldn't register string lib");
-
- // remove rand and srand calls from sqstdmath, we'll provide our own
- sq_pushstring(global_vm, "srand", -1);
- sq_deleteslot(global_vm, -2, SQFalse);
- sq_pushstring(global_vm, "rand", -1);
- sq_deleteslot(global_vm, -2, SQFalse);
-
- // register supertux API
- register_supertux_wrapper(global_vm);
-
- sq_pop(global_vm, 1);
-
- // register print function
- sq_setprintfunc(global_vm, printfunc);
- // register default error handlers
- sqstd_seterrorhandlers(global_vm);
-
- // try to load default script
- try {
- std::string filename = "scripts/default.nut";
- IFileStream stream(filename);
- Scripting::compile_and_run(global_vm, stream, filename);
- } catch(std::exception& e) {
- log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
- }
-}
-
-void exit_squirrel()
-{
-#ifdef ENABLE_SQDBG
- if(debugger != NULL) {
- sq_rdbg_shutdown(debugger);
- debugger = NULL;
- }
-#endif
-
- if (global_vm)
- sq_close(global_vm);
-
- global_vm = NULL;
-}
-
-void update_debugger()
-{
-#ifdef ENABLE_SQDBG
- if(debugger != NULL)
- sq_rdbg_update(debugger);
-#endif
-}
+namespace scripting {
std::string squirrel2string(HSQUIRRELVM v, SQInteger i)
{
break;
case OT_BOOL: {
SQBool p;
- sq_getbool(v, i, &p);
- if (p)
- os << "true";
- else
- os << "false";
+ if (SQ_SUCCEEDED(sq_getbool(v, i, &p))) {
+ if (p)
+ os << "true";
+ else
+ os << "false";
+ }
break;
}
case OT_INTEGER: {
printf("--------------------------------------------------------------\n");
}
-static SQInteger squirrel_read_char(SQUserPointer file)
+SQInteger squirrel_read_char(SQUserPointer file)
{
std::istream* in = reinterpret_cast<std::istream*> (file);
- char c = in->get();
+ int c = in->get();
if(in->eof())
return 0;
return c;
sq_pushstring(vm, name, -1);
sq_pushfloat(vm, val);
if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
+ throw scripting::SquirrelError(vm, "Couldn't add float value to table");
}
void store_int(HSQUIRRELVM vm, const char* name, int val)
sq_pushstring(vm, name, -1);
sq_pushinteger(vm, val);
if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add int value to table");
+ throw scripting::SquirrelError(vm, "Couldn't add int value to table");
}
void store_string(HSQUIRRELVM vm, const char* name, const std::string& val)
sq_pushstring(vm, name, -1);
sq_pushstring(vm, val.c_str(), val.length());
if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
+ throw scripting::SquirrelError(vm, "Couldn't add float value to table");
}
void store_bool(HSQUIRRELVM vm, const char* name, bool val)
sq_pushstring(vm, name, -1);
sq_pushbool(vm, val ? SQTrue : SQFalse);
if(SQ_FAILED(sq_createslot(vm, -3)))
- throw Scripting::SquirrelError(vm, "Couldn't add float value to table");
+ throw scripting::SquirrelError(vm, "Couldn't add float value to table");
}
bool has_float(HSQUIRRELVM vm, const char* name)
if(SQ_FAILED(sq_get(vm, -2))) {
std::ostringstream msg;
msg << "Couldn't get float value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
float result;
if(SQ_FAILED(sq_getfloat(vm, -1, &result))) {
std::ostringstream msg;
msg << "Couldn't get float value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
sq_pop(vm, 1);
if(SQ_FAILED(sq_get(vm, -2))) {
std::ostringstream msg;
msg << "Couldn't get int value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
SQInteger result;
if(SQ_FAILED(sq_getinteger(vm, -1, &result))) {
std::ostringstream msg;
msg << "Couldn't get int value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
sq_pop(vm, 1);
if(SQ_FAILED(sq_get(vm, -2))) {
std::ostringstream msg;
msg << "Couldn't get string value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
const char* result;
if(SQ_FAILED(sq_getstring(vm, -1, &result))) {
std::ostringstream msg;
msg << "Couldn't get string value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
sq_pop(vm, 1);
if(SQ_FAILED(sq_get(vm, -2))) {
std::ostringstream msg;
msg << "Couldn't get bool value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
SQBool result;
if(SQ_FAILED(sq_getbool(vm, -1, &result))) {
std::ostringstream msg;
msg << "Couldn't get bool value for '" << name << "' from table";
- throw Scripting::SquirrelError(vm, msg.str());
+ throw scripting::SquirrelError(vm, msg.str());
}
sq_pop(vm, 1);