dxx-rebirth/similar/main/cvar.cpp

203 lines
4 KiB
C++
Raw Normal View History

2015-02-11 07:35:44 +00:00
/*
* This file is part of the DXX-Rebirth project <http://www.dxx-rebirth.com/>.
* 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
*
*/
#include <stdlib.h>
#include <float.h>
#include <physfs.h>
#include "console.h"
#include "dxxerror.h"
#include "strutil.h"
#include "u_mem.h"
2015-02-22 02:42:59 +00:00
#include "hash.h"
#include "game.h"
2015-02-11 07:35:44 +00:00
#define CVAR_MAX_LENGTH 1024
2015-02-22 02:42:59 +00:00
#define CVAR_MAX_CVARS 1024
2015-02-11 07:35:44 +00:00
/* The list of cvars */
2015-02-22 02:42:59 +00:00
hashtable cvar_hash;
cvar_t *cvar_list[CVAR_MAX_CVARS];
int Num_cvars;
2015-02-11 07:35:44 +00:00
static void cvar_free(void)
{
2015-02-22 02:42:59 +00:00
while (Num_cvars--)
d_free(cvar_list[Num_cvars]->string);
2015-02-11 07:35:44 +00:00
}
void cvar_cmd_set(int argc, char **argv)
{
char buf[CVAR_MAX_LENGTH];
int ret, i;
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-02-12 06:59:33 +00:00
con_printf(CON_NORMAL, "%s: %s", ptr->name, ptr->string);
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-02-22 02:42:59 +00:00
for (i = 0; i < Num_cvars; i++)
con_printf(CON_NORMAL, "%s: %s", cvar_list[i]->name, cvar_list[i]->string);
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;
}
for (i = 3; i < argc; i++) {
ret = snprintf(buf, CVAR_MAX_LENGTH, "%s %s", buf, argv[i]);
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;
}
}
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-12 01:13:33 +00:00
2015-02-11 07:35:44 +00:00
atexit(cvar_free);
}
cvar_t *cvar_find(char *cvar_name)
{
2015-02-22 02:42:59 +00:00
int i;
i = hashtable_search( &cvar_hash, cvar_name );
if ( i < 0 )
return NULL;
return cvar_list[i];
2015-02-11 07:35:44 +00:00
}
2015-02-12 06:34:18 +00:00
const char *cvar_complete(char *text)
2015-02-11 07:35:44 +00:00
{
2015-02-22 02:42:59 +00:00
int i;
uint_fast32_t len = strlen(text);
2015-02-11 07:35:44 +00:00
if (!len)
return NULL;
2015-02-22 02:42:59 +00:00
for (i = 0; i < Num_cvars; i++)
if (!d_strnicmp(text, cvar_list[i]->name, len))
return cvar_list[i]->name;
2015-02-11 07:35:44 +00:00
return NULL;
}
/* Register a cvar */
void cvar_registervariable (cvar_t *cvar)
{
char *stringval;
2015-02-22 02:42:59 +00:00
int i;
2015-02-12 06:34:18 +00:00
2015-02-11 07:35:44 +00:00
Assert(cvar != NULL);
stringval = cvar->string;
cvar->string = d_strdup(stringval);
2015-02-12 06:34:18 +00:00
cvar->value = fl2f(strtod(cvar->string, NULL));
cvar->intval = static_cast<int>(strtol(cvar->string, NULL, 10));
2015-02-22 02:42:59 +00:00
2015-02-11 07:35:44 +00:00
/* insert at end of list */
2015-02-22 02:42:59 +00:00
for (i = 0; i < Num_cvars; i++)
Assert(d_stricmp(cvar->name, cvar_list[i]->name));
hashtable_insert(&cvar_hash, cvar->name, Num_cvars);
cvar_list[Num_cvars++] = cvar;
2015-02-11 07:35:44 +00:00
}
/* Set a CVar's value */
void cvar_set_cvar(cvar_t *cvar, char *value)
{
if (!cvar)
return;
d_free(cvar->string);
cvar->string = d_strdup(value);
cvar->value = fl2f(strtod(cvar->string, NULL));
cvar->intval = static_cast<int>(strtol(cvar->string, NULL, 10));
2015-02-12 06:59:33 +00:00
con_printf(CON_VERBOSE, "%s: %s", cvar->name, cvar->string);
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);
Assert(!(n < 0 || n > CVAR_MAX_LENGTH));
cvar_set_cvar(cvar, buf);
}
void cvar_set (char *cvar_name, char *value)
{
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;
}
if (cvar->flags & CVAR_CHEAT && !cheats.enabled)
{
con_printf(CON_NORMAL, "cvar %s is cheat protected.\n", 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-22 02:42:59 +00:00
int i;
for (i = 0; i < Num_cvars; i++)
if (cvar_list[i]->flags & CVAR_ARCHIVE)
2015-02-22 02:42:59 +00:00
PHYSFSX_printf(file, "%s=%s\n", cvar_list[i]->name, cvar_list[i]->string);
2015-02-11 07:35:44 +00:00
}