Skip to content

Commit

Permalink
Console|libdoomsday: Rudimentary access to de::Config variables
Browse files Browse the repository at this point in the history
This commit introduces a simple mechanism that allows the user to
interactively access de::Config variables as if they were traditional
cvars. The allowed operations are checking the value of the variable
(having it printed in the console), or setting the value (number or
text only).

This is not a long term solution, though. When the console is based on
Doomsday Script, this mechanism should become unnecessary and obsolete.
(Although some form of mapping between traditional cvar identifiers and
Config variables is still needed.)
  • Loading branch information
skyjake committed Nov 16, 2014
1 parent ac415f1 commit 9f3672c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
20 changes: 20 additions & 0 deletions doomsday/libdoomsday/include/doomsday/console/cmd.h
Expand Up @@ -98,4 +98,24 @@ LIBDOOMSDAY_PUBLIC void Con_PrintCommandUsage(ccmd_t const *ccmd, bool allOverlo
*/
LIBDOOMSDAY_PUBLIC de::String Con_CmdUsageAsStyledText(ccmd_t const *ccmd);

/**
* Defines a console command that behaves like a console variable but accesses
* the data of a de::Config variable.
*
* The purpose of this mechanism is to provide a backwards compatible way to
* access config variables.
*
* @note In the future, when the console uses Doomsday Script for executing commands,
* this kind of mapping should be much easier since one can just create a reference to
* the real variable and access it pretty much normally.
*
* @param consoleName Name of the console command ("cvar").
* @param opts Type template when setting the value (using the
* ccmdtemplate_t argument template format).
* @param configVariable Name of the de::Config variable.
*/
LIBDOOMSDAY_PUBLIC void Con_AddMappedConfigVariable(char const *consoleName,
char const *opts,
de::String const &configVariable);

#endif // LIBDOOMSDAY_CONSOLE_CMD_H
47 changes: 47 additions & 0 deletions doomsday/libdoomsday/src/console/cmd.cpp
Expand Up @@ -24,6 +24,8 @@
#include <de/memoryzone.h>
#include <de/c_wrapper.h>
#include <de/Log>
#include <de/App>
#include <QMap>

using namespace de;

Expand All @@ -35,6 +37,8 @@ static blockset_t *ccmdBlockSet;
/// Running total of the number of uniquely-named commands.
static uint numUniqueNamedCCmds;

static QMap<String, String> mappedConfigVariables;

void Con_InitCommands()
{
ccmdListHead = 0;
Expand Down Expand Up @@ -397,3 +401,46 @@ String Con_CmdAsStyledText(ccmd_t *cmd)
return String(_E(b) "%1" _E(.)).arg(cmd->name);
}
}

D_CMD(MappedConfigVariable)
{
DENG_UNUSED(src);

// Look up the variable.
auto const found = mappedConfigVariables.constFind(argv[0]);
DENG2_ASSERT(found != mappedConfigVariables.constEnd()); // mapping must be defined

Variable &var = App::config().names()[found.value()];

if(argc == 1)
{
// No argumnets, just print the current value.
LOG_SCR_MSG(_E(b) "%s" _E(.) " = " _E(>) "%s " _E(l)_E(C) "[Config.%s]")
<< argv[0]
<< var.value().asText()
<< found.value();
}
else if(argc > 1)
{
// Retain the current type of the Config variable (numeric or text).
if(var.value().maybeAs<TextValue>())
{
var.set(new TextValue(argv[1]));
}
else
{
var.set(new NumberValue(String(argv[1]).toDouble()));
}
}
return true;
}

void Con_AddMappedConfigVariable(char const *consoleName, char const *opts, String const &configVariable)
{
DENG2_ASSERT(!mappedConfigVariables.contains(consoleName)); // redefining not handled

mappedConfigVariables.insert(consoleName, configVariable);

C_CMD(consoleName, "", MappedConfigVariable);
C_CMD(consoleName, opts, MappedConfigVariable);
}

0 comments on commit 9f3672c

Please sign in to comment.