From 9f3672ce1dd658f7b3d2c3f3273a595f76ffb795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Sun, 16 Nov 2014 22:01:36 +0200 Subject: [PATCH] Console|libdoomsday: Rudimentary access to de::Config variables 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.) --- .../include/doomsday/console/cmd.h | 20 ++++++++ doomsday/libdoomsday/src/console/cmd.cpp | 47 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/doomsday/libdoomsday/include/doomsday/console/cmd.h b/doomsday/libdoomsday/include/doomsday/console/cmd.h index 01b68d6820..f847291416 100644 --- a/doomsday/libdoomsday/include/doomsday/console/cmd.h +++ b/doomsday/libdoomsday/include/doomsday/console/cmd.h @@ -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 diff --git a/doomsday/libdoomsday/src/console/cmd.cpp b/doomsday/libdoomsday/src/console/cmd.cpp index 9ccb537f63..b3751639d6 100644 --- a/doomsday/libdoomsday/src/console/cmd.cpp +++ b/doomsday/libdoomsday/src/console/cmd.cpp @@ -24,6 +24,8 @@ #include #include #include +#include +#include using namespace de; @@ -35,6 +37,8 @@ static blockset_t *ccmdBlockSet; /// Running total of the number of uniquely-named commands. static uint numUniqueNamedCCmds; +static QMap mappedConfigVariables; + void Con_InitCommands() { ccmdListHead = 0; @@ -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()) + { + 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); +}