dxx-rebirth/common/main/cvar.cpp

191 lines
4.2 KiB
C++
Raw Normal View History

2015-02-11 07:35:44 +00:00
/*
* This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
2015-02-11 07:35:44 +00:00
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*
*/
/*
*
* Console variables
*
*/
2015-06-07 16:20:46 +00:00
#include <cstdarg>
2015-06-07 16:20:46 +00:00
#include <map>
2015-02-11 07:35:44 +00:00
#include <stdlib.h>
#include <physfs.h>
#include "cmd.h"
2015-02-11 07:35:44 +00:00
#include "console.h"
2015-06-07 16:20:46 +00:00
#include "cvar.h"
2015-02-11 07:35:44 +00:00
#include "dxxerror.h"
#include "strutil.h"
#include "u_mem.h"
#include "game.h"
2015-06-07 16:20:46 +00:00
#include "physfsx.h"
2015-02-11 07:35:44 +00:00
2015-06-07 16:20:46 +00:00
#include "dxxsconf.h"
#include "compiler-range_for.h"
2015-02-11 07:35:44 +00:00
#define CVAR_MAX_LENGTH 1024
/* The list of cvars */
2015-06-07 16:20:46 +00:00
typedef std::map<const char *, std::reference_wrapper<cvar_t>> cvar_list_type;
static cvar_list_type cvar_list;
2015-02-11 07:35:44 +00:00
const char *cvar_t::operator=(const char *s)
{
cvar_set_cvar(this, s);
2015-06-07 16:20:46 +00:00
return s;
}
int cvar_t::operator=(int i) { cvar_set_cvarf(this, "%d", i); return this->intval; }
2015-06-07 16:20:47 +00:00
void cvar_cmd_set(unsigned long argc, const char *const *const argv)
2015-02-11 07:35:44 +00:00
{
char buf[CVAR_MAX_LENGTH];
2015-11-26 02:56:54 +00:00
int ret;
2015-02-12 01:13:33 +00:00
2015-02-11 07:35:44 +00:00
if (argc == 2) {
cvar_t *ptr;
if ((ptr = cvar_find(argv[1])))
2015-06-07 16:20:46 +00:00
con_printf(CON_NORMAL, "%s: %s", ptr->name, ptr->string.c_str());
2015-02-11 07:35:44 +00:00
else
2015-02-12 06:59:33 +00:00
con_printf(CON_NORMAL, "set: variable %s not found", argv[1]);
2015-02-11 07:35:44 +00:00
return;
}
if (argc == 1) {
2015-06-07 16:20:46 +00:00
range_for (const auto &i, cvar_list)
con_printf(CON_NORMAL, "%s: %s", i.first, i.second.get().string.c_str());
2015-02-11 07:35:44 +00:00
return;
}
ret = snprintf(buf, sizeof(buf), "%s", argv[2]);
2015-02-11 07:35:44 +00:00
if (ret >= CVAR_MAX_LENGTH) {
2015-02-12 06:59:33 +00:00
con_printf(CON_CRITICAL, "set: value too long (max %d characters)", CVAR_MAX_LENGTH);
2015-02-11 07:35:44 +00:00
return;
}
size_t position = ret;
2015-11-26 02:56:54 +00:00
for (int i = 3; i < argc; i++) {
ret = snprintf(&buf[position], CVAR_MAX_LENGTH - position, " %s", argv[i]);
position += ret;
if (position >= CVAR_MAX_LENGTH)
{
2015-02-12 06:59:33 +00:00
con_printf(CON_CRITICAL, "set: value too long (max %d characters)", CVAR_MAX_LENGTH);
2015-02-11 07:35:44 +00:00
return;
}
}
cvar_set(argv[1], buf);
}
void cvar_init(void)
{
2015-02-12 01:13:33 +00:00
cmd_addcommand("set", cvar_cmd_set, "set <name> <value>\n" " set variable <name> equal to <value>\n"
"set <name>\n" " show value of <name>\n"
2015-02-12 06:59:33 +00:00
"set\n" " show value of all variables");
2015-02-11 07:35:44 +00:00
}
2015-02-24 05:31:34 +00:00
cvar_t *cvar_find(const char *cvar_name)
2015-02-11 07:35:44 +00:00
{
2015-06-07 16:20:46 +00:00
const auto i = cvar_list.find(cvar_name);
return i == cvar_list.end() ? nullptr : &i->second.get();
2015-02-11 07:35:44 +00:00
}
const char *cvar_complete(const char *text)
2015-02-11 07:35:44 +00:00
{
uint_fast32_t len = strlen(text);
2015-02-11 07:35:44 +00:00
if (!len)
return NULL;
2015-06-07 16:20:46 +00:00
range_for (const auto &i, cvar_list)
if (!d_strnicmp(text, i.first, len))
return i.first;
2015-02-11 07:35:44 +00:00
return NULL;
}
/* Register a cvar */
2015-06-07 16:20:47 +00:00
void cvar_registervariable (cvar_t &cvar)
2015-02-11 07:35:44 +00:00
{
2015-06-07 16:20:47 +00:00
cvar.value = fl2f(strtod(cvar.string.c_str(), NULL));
cvar.intval = static_cast<int>(strtol(cvar.string.c_str(), NULL, 10));
2015-02-22 02:42:59 +00:00
2015-06-07 16:20:47 +00:00
const auto i = cvar_list.insert(cvar_list_type::value_type(cvar.name, cvar));
2015-06-07 16:20:46 +00:00
if (!i.second)
2015-02-24 01:10:36 +00:00
{
Int3();
2015-06-07 16:20:47 +00:00
con_printf(CON_URGENT, "cvar %s already exists!", cvar.name);
2015-02-24 01:10:36 +00:00
return;
}
/* insert at end of list */
2015-02-11 07:35:44 +00:00
}
/* Set a CVar's value */
void cvar_set_cvar(cvar_t *cvar, const char *value)
2015-02-11 07:35:44 +00:00
{
if (!cvar)
return;
2015-06-07 16:20:46 +00:00
cvar->string = value;
cvar->value = fl2f(strtod(value, NULL));
cvar->intval = static_cast<int>(strtol(value, NULL, 10));
con_printf(CON_VERBOSE, "%s: %s", cvar->name, value);
2015-02-11 07:35:44 +00:00
}
2015-02-12 06:34:18 +00:00
void cvar_set_cvarf(cvar_t *cvar, const char *fmt, ...)
2015-02-11 07:35:44 +00:00
{
va_list arglist;
char buf[CVAR_MAX_LENGTH];
int n;
va_start (arglist, fmt);
n = vsnprintf(buf, sizeof(buf), fmt, arglist);
2015-02-11 07:35:44 +00:00
va_end (arglist);
2015-04-25 20:42:39 +00:00
if (n < 0 || n > CVAR_MAX_LENGTH) {
Int3();
con_printf(CON_CRITICAL, "error setting cvar %s", cvar->name);
return;
}
2015-02-11 07:35:44 +00:00
cvar_set_cvar(cvar, buf);
}
2015-02-24 05:31:34 +00:00
void cvar_set(const char *cvar_name, char *value)
2015-02-11 07:35:44 +00:00
{
cvar_t *cvar;
cvar = cvar_find(cvar_name);
if (!cvar) {
Int3();
2015-02-12 06:59:33 +00:00
con_printf(CON_NORMAL, "cvar %s not found", cvar_name);
2015-02-11 07:35:44 +00:00
return;
}
2015-07-18 03:49:47 +00:00
if (cvar->flags & CVAR_CHEAT && !cheats_enabled())
{
2015-04-25 20:48:11 +00:00
con_printf(CON_NORMAL, "cvar %s is cheat protected.", cvar_name);
return;
}
2015-02-11 07:35:44 +00:00
cvar_set_cvar(cvar, value);
}
/* Write archive cvars to file */
void cvar_write(PHYSFS_File *file)
2015-02-11 07:35:44 +00:00
{
2015-06-07 16:20:46 +00:00
range_for (const auto &i, cvar_list)
if (i.second.get().flags & CVAR_ARCHIVE)
PHYSFSX_printf(file, "%s=%s\n", i.first, i.second.get().string.c_str());
2015-02-11 07:35:44 +00:00
}