Skip to content

Commit

Permalink
Added Scripting class to bundle up all the Squirrel init code
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 17, 2014
1 parent 7532d47 commit 04270f0
Show file tree
Hide file tree
Showing 14 changed files with 231 additions and 144 deletions.
139 changes: 139 additions & 0 deletions src/scripting/scripting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// SuperTux
// Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "scripting/scripting.hpp"

#include <sqstdaux.h>
#include <sqstdblob.h>
#include <sqstdmath.h>
#include <sqstdstring.h>
#include <stdarg.h>
#include <stdio.h>

#include "physfs/ifile_stream.hpp"
#include "scripting/squirrel_error.hpp"
#include "scripting/wrapper.hpp"
#include "squirrel_util.hpp"
#include "supertux/console.hpp"
#include "util/log.hpp"

#ifdef ENABLE_SQDBG
# include "../../external/squirrel/sqdbg/sqrdbg.h"
namespace {
HSQREMOTEDBG debugger = NULL;
} // namespace
#endif

namespace {

void printfunc(HSQUIRRELVM, const char* str, ...)
{
char buf[4096];
va_list arglist;
va_start(arglist, str);
vsnprintf(buf, sizeof(buf), str, arglist);
ConsoleBuffer::output << (const char*) buf << std::flush;
va_end(arglist);
}

} // namespace

namespace scripting {

HSQUIRRELVM global_vm = NULL;

Scripting::Scripting(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;
}
}

Scripting::~Scripting()
{
#ifdef ENABLE_SQDBG
if(debugger != NULL) {
sq_rdbg_shutdown(debugger);
debugger = NULL;
}
#endif

if (global_vm)
sq_close(global_vm);

global_vm = NULL;
}

void
Scripting::update_debugger()
{
#ifdef ENABLE_SQDBG
if(debugger != NULL)
sq_rdbg_update(debugger);
#endif
}

} // namespace scripting

/* EOF */
46 changes: 46 additions & 0 deletions src/scripting/scripting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SuperTux
// Copyright (C) 2014 Ingo Ruhnke <grumbel@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_SCRIPTING_SCRIPTING_HPP
#define HEADER_SUPERTUX_SCRIPTING_SCRIPTING_HPP

#include <squirrel.h>

#include "util/currenton.hpp"

namespace scripting {

extern HSQUIRRELVM global_vm;

class Scripting : public Currenton<Scripting>
{
private:
public:
Scripting(bool enable_debugger);
~Scripting();

void update_debugger();

private:
Scripting(const Scripting&) = delete;
Scripting& operator=(const Scripting&) = delete;
};

} // namespace scripting

#endif

/* EOF */
99 changes: 0 additions & 99 deletions src/scripting/squirrel_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,107 +25,8 @@
#include <sqstdstring.h>
#include <stdarg.h>

#include "physfs/ifile_stream.hpp"
#include "supertux/console.hpp"
#include "util/log.hpp"

#ifdef ENABLE_SQDBG
# include "../../external/squirrel/sqdbg/sqrdbg.h"
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);
vsnprintf(buf, sizeof(buf), str, arglist);
ConsoleBuffer::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;
Expand Down
6 changes: 0 additions & 6 deletions src/scripting/squirrel_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@

namespace scripting {

extern HSQUIRRELVM global_vm;

void init_squirrel(bool enable_debugger);
void exit_squirrel();
void update_debugger();

std::string squirrel2string(HSQUIRRELVM vm, SQInteger i);
void print_squirrel_stack(HSQUIRRELVM vm);

Expand Down
1 change: 1 addition & 0 deletions src/scripting/thread_queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "scripting/thread_queue.hpp"

#include "scripting/scripting.hpp"
#include "scripting/squirrel_util.hpp"
#include "util/log.hpp"

Expand Down
1 change: 1 addition & 0 deletions src/scripting/time_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>

#include "scripting/scripting.hpp"
#include "scripting/squirrel_util.hpp"
#include "scripting/time_scheduler.hpp"
#include "util/log.hpp"
Expand Down
1 change: 1 addition & 0 deletions src/supertux/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <iostream>

#include "physfs/ifile_stream.hpp"
#include "scripting/scripting.hpp"
#include "scripting/squirrel_util.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
Expand Down
2 changes: 2 additions & 0 deletions src/supertux/game_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "object/endsequence_walkright.hpp"
#include "object/level_time.hpp"
#include "object/player.hpp"
#include "scripting/scripting.hpp"
#include "scripting/squirrel_util.hpp"
#include "scripting/squirrel_util.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
Expand Down
Loading

0 comments on commit 04270f0

Please sign in to comment.