2 // Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "scripting/scripting.hpp"
20 #include <sqstdblob.h>
21 #include <sqstdmath.h>
22 #include <sqstdstring.h>
26 #include "physfs/ifile_stream.hpp"
27 #include "scripting/squirrel_error.hpp"
28 #include "scripting/wrapper.hpp"
29 #include "squirrel_util.hpp"
30 #include "supertux/console.hpp"
31 #include "util/log.hpp"
34 # include "../../external/squirrel/sqdbg/sqrdbg.h"
36 HSQREMOTEDBG debugger = NULL;
42 void printfunc(HSQUIRRELVM, const char* str, ...)
46 va_start(arglist, str);
47 vsnprintf(buf, sizeof(buf), str, arglist);
48 ConsoleBuffer::output << (const char*) buf << std::flush;
56 HSQUIRRELVM global_vm = NULL;
58 Scripting::Scripting(bool enable_debugger)
60 global_vm = sq_open(64);
62 throw std::runtime_error("Couldn't initialize squirrel vm");
66 sq_enabledebuginfo(global_vm, SQTrue);
67 debugger = sq_rdbg_init(global_vm, 1234, SQFalse);
69 throw SquirrelError(global_vm, "Couldn't initialize squirrel debugger");
71 sq_enabledebuginfo(global_vm, SQTrue);
72 log_info << "Waiting for debug client..." << std::endl;
73 if(SQ_FAILED(sq_rdbg_waitforconnections(debugger)))
74 throw SquirrelError(global_vm, "Waiting for debug clients failed");
75 log_info << "debug client connected." << std::endl;
79 sq_pushroottable(global_vm);
80 if(SQ_FAILED(sqstd_register_bloblib(global_vm)))
81 throw SquirrelError(global_vm, "Couldn't register blob lib");
82 if(SQ_FAILED(sqstd_register_mathlib(global_vm)))
83 throw SquirrelError(global_vm, "Couldn't register math lib");
84 if(SQ_FAILED(sqstd_register_stringlib(global_vm)))
85 throw SquirrelError(global_vm, "Couldn't register string lib");
87 // remove rand and srand calls from sqstdmath, we'll provide our own
88 sq_pushstring(global_vm, "srand", -1);
89 sq_deleteslot(global_vm, -2, SQFalse);
90 sq_pushstring(global_vm, "rand", -1);
91 sq_deleteslot(global_vm, -2, SQFalse);
93 // register supertux API
94 register_supertux_wrapper(global_vm);
98 // register print function
99 sq_setprintfunc(global_vm, printfunc, printfunc);
100 // register default error handlers
101 sqstd_seterrorhandlers(global_vm);
103 // try to load default script
105 std::string filename = "scripts/default.nut";
106 IFileStream stream(filename);
107 scripting::compile_and_run(global_vm, stream, filename);
108 } catch(std::exception& e) {
109 log_warning << "Couldn't load default.nut: " << e.what() << std::endl;
113 Scripting::~Scripting()
116 if(debugger != NULL) {
117 sq_rdbg_shutdown(debugger);
129 Scripting::update_debugger()
133 sq_rdbg_update(debugger);
137 } // namespace scripting