X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fscripting%2Fsquirrel_util.cpp;h=ffbbf2cb2de7213f8d589979b3129195125e7c55;hb=06bfaaa9b62fd3cefbdba8149f4a885951167705;hp=0352d3901901b9ded5e9184ca7696282564278bd;hpb=08813a74da6ac1fd045a105e4e8105f1d7f716f0;p=supertux.git diff --git a/src/scripting/squirrel_util.cpp b/src/scripting/squirrel_util.cpp index 0352d3901..ffbbf2cb2 100644 --- a/src/scripting/squirrel_util.cpp +++ b/src/scripting/squirrel_util.cpp @@ -25,106 +25,7 @@ #include #include -#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) { @@ -136,11 +37,12 @@ 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: { @@ -308,10 +210,10 @@ void print_squirrel_stack(HSQUIRRELVM v) printf("--------------------------------------------------------------\n"); } -static SQInteger squirrel_read_char(SQUserPointer file) +SQInteger squirrel_read_char(SQUserPointer file) { std::istream* in = reinterpret_cast (file); - char c = in->get(); + int c = in->get(); if(in->eof()) return 0; return c; @@ -387,7 +289,7 @@ void store_float(HSQUIRRELVM vm, const char* name, float val) 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) @@ -395,7 +297,7 @@ 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) @@ -403,7 +305,7 @@ 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) @@ -411,7 +313,7 @@ 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) @@ -443,14 +345,14 @@ float read_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); @@ -463,14 +365,14 @@ int read_int(HSQUIRRELVM vm, const char* name) 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); @@ -483,14 +385,14 @@ std::string read_string(HSQUIRRELVM vm, const char* name) 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); @@ -503,14 +405,14 @@ bool read_bool(HSQUIRRELVM vm, const char* name) 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);