Debian Codegraph @

common/main/cvar.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 *
7 */
8 /*
9 *
10 * Console variables
11 *
12 */
13
14 #include <cstdarg>
15 #include <map>
16 #include <stdlib.h>
17 #include <physfs.h>
18
19 #include "cmd.h"
20 #include "console.h"
21 #include "cvar.h"
22 #include "dxxerror.h"
23 #include "strutil.h"
24 #include "u_mem.h"
25 #include "game.h"
26 #include "physfsx.h"
27
28 #include "dxxsconf.h"
29 #include "compiler-range_for.h"
30
31 #define CVAR_MAX_LENGTH 1024
32
33 /* The list of cvars */
34 typedef std::map<const char *, std::reference_wrapper<cvar_t>> cvar_list_type;
35 static cvar_list_type cvar_list;
36
37 const char *cvar_t::operator=(const char *s)
38 {
39 cvar_set_cvar(this, s);
40 return s;
41 }
42
43 int cvar_t::operator=(int i) { cvar_set_cvarf(this, "%d", i); return this->intval; }
44
45 void cvar_cmd_set(unsigned long argc, const char *const *const argv)
46 {
47 char buf[CVAR_MAX_LENGTH];
48 int ret;
49
50 if (argc == 2) {
51 cvar_t *ptr;
52
53 if ((ptr = cvar_find(argv[1])))
54 con_printf(CON_NORMAL, "%s: %s", ptr->name, ptr->string.c_str());
55 else
56 con_printf(CON_NORMAL, "set: variable %s not found", argv[1]);
57 return;
58 }
59
60 if (argc == 1) {
61 range_for (const auto &i, cvar_list)
62 con_printf(CON_NORMAL, "%s: %s", i.first, i.second.get().string.c_str());
63 return;
64 }
65
66 ret = snprintf(buf, sizeof(buf), "%s", argv[2]);
67 if (ret >= CVAR_MAX_LENGTH) {
68 con_printf(CON_CRITICAL, "set: value too long (max %d characters)", CVAR_MAX_LENGTH);
69 return;
70 }
71
72 size_t position = ret;
73 for (int i = 3; i < argc; i++) {
74 ret = snprintf(&buf[position], CVAR_MAX_LENGTH - position, " %s", argv[i]);
75 position += ret;
76 if (position >= CVAR_MAX_LENGTH)
77 {
78 con_printf(CON_CRITICAL, "set: value too long (max %d characters)", CVAR_MAX_LENGTH);
79 return;
80 }
81 }
82 cvar_set(argv[1], buf);
83 }
84
85
86 void cvar_init(void)
87 {
88 cmd_addcommand("set", cvar_cmd_set, "set <name> <value>\n" " set variable <name> equal to <value>\n"
89 "set <name>\n" " show value of <name>\n"
90 "set\n" " show value of all variables");
91 }
92
93
94 cvar_t *cvar_find(const char *cvar_name)
95 {
96 const auto i = cvar_list.find(cvar_name);
97 return i == cvar_list.end() ? nullptr : &i->second.get();
98 }
99
100
101 const char *cvar_complete(const char *text)
102 {
103 uint_fast32_t len = strlen(text);
104 if (!len)
105 return NULL;
106 range_for (const auto &i, cvar_list)
107 if (!d_strnicmp(text, i.first, len))
108 return i.first;
109 return NULL;
110 }
111
112
113 /* Register a cvar */
114 void cvar_registervariable (cvar_t &cvar)
115 {
116 cvar.value = fl2f(strtod(cvar.string.c_str(), NULL));
117 cvar.intval = static_cast<int>(strtol(cvar.string.c_str(), NULL, 10));
118
119 const auto i = cvar_list.insert(cvar_list_type::value_type(cvar.name, cvar));
120 if (!i.second)
121 {
122 Int3();
123 con_printf(CON_URGENT, "cvar %s already exists!", cvar.name);
124 return;
125 }
126 /* insert at end of list */
127 }
128
129
130 /* Set a CVar's value */
131 void cvar_set_cvar(cvar_t *cvar, const char *value)
132 {
133 if (!cvar)
134 return;
135
136 cvar->string = value;
137 cvar->value = fl2f(strtod(value, NULL));
138 cvar->intval = static_cast<int>(strtol(value, NULL, 10));
139 con_printf(CON_VERBOSE, "%s: %s", cvar->name, value);
140 }
141
142
143 void cvar_set_cvarf(cvar_t *cvar, const char *fmt, ...)
144 {
145 va_list arglist;
146 char buf[CVAR_MAX_LENGTH];
147 int n;
148
149 va_start (arglist, fmt);
150 n = vsnprintf(buf, sizeof(buf), fmt, arglist);
151 va_end (arglist);
152
153 if (n < 0 || n > CVAR_MAX_LENGTH) {
154 Int3();
155 con_printf(CON_CRITICAL, "error setting cvar %s", cvar->name);
156 return;
157 }
158
159 cvar_set_cvar(cvar, buf);
160 }
161
162
163 void cvar_set(const char *cvar_name, char *value)
164 {
165 cvar_t *cvar;
166
167 cvar = cvar_find(cvar_name);
168 if (!cvar) {
169 Int3();
170 con_printf(CON_NORMAL, "cvar %s not found", cvar_name);
171 return;
172 }
173
174 if (cvar->flags & CVAR_CHEAT && !cheats_enabled())
175 {
176 con_printf(CON_NORMAL, "cvar %s is cheat protected.", cvar_name);
177 return;
178 }
179
180 cvar_set_cvar(cvar, value);
181 }
182
183
184 /* Write archive cvars to file */
185 void cvar_write(PHYSFS_File *file)
186 {
187 range_for (const auto &i, cvar_list)
188 if (i.second.get().flags & CVAR_ARCHIVE)
189 PHYSFSX_printf(file, "%s=%s\n", i.first, i.second.get().string.c_str());
190 }