Skip to content

Commit

Permalink
Scripting|libdoomsday: Added native "Console" module
Browse files Browse the repository at this point in the history
The built-in Console module provides access to the old-fashioned
console features via Doomsday Script.

Added bindings for reading and writing console variables.
  • Loading branch information
skyjake committed Sep 17, 2015
1 parent 5a26a04 commit e759db2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
10 changes: 10 additions & 0 deletions doomsday/apps/libdoomsday/src/console/exec.cpp
Expand Up @@ -99,6 +99,8 @@ D_CMD(InspectMobj);
D_CMD(DebugCrash);
D_CMD(DebugError);

void initVariableBindings(Binder &);

static int executeSubCmd(const char *subCmd, byte src, dd_bool isNetCmd);
static void Con_SplitIntoSubCommands(char const *command,
timespan_t markerOffset, byte src,
Expand All @@ -109,6 +111,7 @@ static void Con_ClearExecBuffer(void);
byte ConsoleSilent = false;

static dd_bool ConsoleInited; // Has Con_Init() been called?
static Binder consoleBinder;
static execbuff_t * exBuff;
static int exBuffSize;
static execbuff_t * curExec;
Expand Down Expand Up @@ -251,6 +254,13 @@ dd_bool Con_Init(void)

LOG_SCR_VERBOSE("Initializing the console...");

// Doomsday Script bindings to access console features.
/// @todo Some of these should become obsolete once cvars/cmds are moved to
/// DS records.
consoleBinder.initNew();
initVariableBindings(consoleBinder);
App::scriptSystem().addNativeModule("Console", consoleBinder.module());

exBuff = NULL;
exBuffSize = 0;

Expand Down
77 changes: 77 additions & 0 deletions doomsday/apps/libdoomsday/src/console/var.cpp
Expand Up @@ -24,6 +24,7 @@

#include <de/memory.h>
#include <de/PathTree>
#include <de/Function>

using namespace de;

Expand Down Expand Up @@ -614,6 +615,82 @@ void Con_AddKnownWordsForVariables()
addVariableToKnownWords);
}

static Value *Function_Console_Get(Context &, Function::ArgumentValues const &args)
{
String const name = args.at(0)->asText();
cvar_t *var = Con_FindVariable(name.toUtf8());
if(!var)
{
throw Error("Function_Console_Get",
QString("Unknown console variable: %1").arg(name));
}
switch(var->type)
{
case CVT_BYTE:
return new NumberValue(CVar_Byte(var));

case CVT_INT:
return new NumberValue(CVar_Integer(var));

case CVT_FLOAT:
return new NumberValue(CVar_Float(var));

case CVT_CHARPTR: ///< ptr points to a char*, which points to the string.
return new TextValue(CVar_String(var));

case CVT_URIPTR:
return new TextValue(CVar_Uri(var).asText());

default:
break;
}
return nullptr;
}

static Value *Function_Console_Set(Context &, Function::ArgumentValues const &args)
{
String const name = args.at(0)->asText();
cvar_t *var = Con_FindVariable(name.toUtf8());
if(!var)
{
throw Error("Function_Console_Set",
QString("Unknown console variable: %1").arg(name));
}

Value const &value = *args.at(1);
switch(var->type)
{
case CVT_BYTE:
case CVT_INT:
CVar_SetInteger(var, value.asInt());
break;

case CVT_FLOAT:
CVar_SetFloat(var, value.asNumber());
break;

case CVT_CHARPTR: ///< ptr points to a char*, which points to the string.
CVar_SetString(var, value.asText().toUtf8());
break;

case CVT_URIPTR:
CVar_SetUri(var, de::Uri(value.asText()));
break;

default:
break;
}

return nullptr;
}

void initVariableBindings(Binder &binder)
{
binder
<< DENG2_FUNC(Console_Get, "get", "name")
<< DENG2_FUNC(Console_Set, "set", "name" << "value");
}

#ifdef DENG_DEBUG
typedef struct {
uint count;
Expand Down

0 comments on commit e759db2

Please sign in to comment.