X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fscripting%2Fsquirrel_util.cpp;h=ffbbf2cb2de7213f8d589979b3129195125e7c55;hb=06bfaaa9b62fd3cefbdba8149f4a885951167705;hp=5743f75ffd6e8b13c39aaf62a512cb4d1027761f;hpb=bc25f6fa2616df7e905fed68ceb4e3ebc186a623;p=supertux.git diff --git a/src/scripting/squirrel_util.cpp b/src/scripting/squirrel_util.cpp index 5743f75ff..ffbbf2cb2 100644 --- a/src/scripting/squirrel_util.cpp +++ b/src/scripting/squirrel_util.cpp @@ -25,107 +25,8 @@ #include #include -#include "physfs/ifile_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, 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 -} - std::string squirrel2string(HSQUIRRELVM v, SQInteger i) { std::ostringstream os; @@ -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: { @@ -311,7 +213,7 @@ void print_squirrel_stack(HSQUIRRELVM v) 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;