]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cvar.h
Merge branch 'master' into Cloudwalk/cvar_overhaul
[xonotic/darkplaces.git] / cvar.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3 Copyright (C) 2006-2021 DarkPlaces contributors
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 */
21 // cvar.h
22
23 /*
24
25 cvar_t variables are used to hold scalar or string variables that can be changed or displayed at the console or prog code as well as accessed directly
26 in C code.
27
28 it is sufficient to initialize a cvar_t with just the first two fields, or
29 you can add a ,true flag for variables that you want saved to the configuration
30 file when the game is quit:
31
32 cvar_t  r_draworder = {"r_draworder","1"};
33 cvar_t  scr_screensize = {"screensize","1",true};
34
35 Cvars must be registered before use, or they will have a 0 value instead of the float interpretation of the string.  Generally, all cvar_t declarations should be registered in the apropriate init function before any console commands are executed:
36 Cvar_RegisterVariable (&host_framerate);
37
38
39 C code usually just references a cvar in place:
40 if ( r_draworder.value )
41
42 It could optionally ask for the value to be looked up for a string name:
43 if (Cvar_VariableValue ("r_draworder"))
44
45 Interpreted prog code can access cvars with the cvar(name) or
46 cvar_set (name, value) internal functions:
47 teamplay = cvar("teamplay");
48 cvar_set ("registered", "1");
49
50 The user can access cvars from the console in two ways:
51 r_draworder                     prints the current value
52 r_draworder 0           sets the current value to 0
53 Cvars are restricted from having the same names as commands to keep this
54 interface from being ambiguous.
55 */
56
57 #ifndef CVAR_H
58 #define CVAR_H
59
60 #include "qtypes.h"
61 #include "qdefs.h"
62 #include "com_list.h"
63
64 struct cmd_state_s;
65 struct qfile_s;
66
67 typedef struct cvar_s
68 {
69         int flags;
70
71         const char *name;
72
73         const char *string;
74         const char *description;
75         int integer;
76         float value;
77         float vector[3];
78
79         const char *defstring;
80
81         void (*callback)(struct cvar_s *var);
82
83         int globaldefindex[3];
84         int globaldefindex_stringno[3];
85
86         int hashindex; // Index in hash table, to avoid looking up a cvar twice when unlinking
87
88         struct cvar_s *initstate; // snapshot of cvar during init
89
90         struct cvar_s *parent, *hnext;
91
92         llist_t list, vlist;
93 } cvar_t;
94
95 typedef struct cvar_state_s
96 {
97         struct mempool_s *mempool;
98         cvar_t *vars;
99         cvar_t *hashtable[CVAR_HASHSIZE];
100 }
101 cvar_state_t;
102
103 extern cvar_state_t cvars_all;
104 extern cvar_state_t cvars_null; // used by cmd_serverfromclient which intentionally has no cvars available
105
106 void Cvar_RegisterVirtual(cvar_t *variable, const char *name );
107
108 void Cvar_RegisterCallback(cvar_t *variable, void (*callback)(cvar_t *));
109
110 /// registers a cvar that already has the name, string, and optionally the
111 /// archive elements set.
112 void Cvar_RegisterVariable(cvar_t *variable);
113
114 qbool Cvar_Readonly (cvar_t *var, const char *cmd_name);
115
116 void Cvar_Callback(cvar_t *var);
117
118 /// equivelant to "<name> <variable>" typed at the console
119 void Cvar_Set (cvar_state_t *cvars, const char *var_name, const char *value);
120
121 /// expands value to a string and calls Cvar_Set
122 void Cvar_SetValue (cvar_state_t *cvars, const char *var_name, float value);
123
124 void Cvar_SetQuick (cvar_t *var, const char *value);
125 void Cvar_SetValueQuick (cvar_t *var, float value);
126
127 float Cvar_VariableValueOr (cvar_state_t *cvars, const char *var_name, float def, int neededflags);
128 // returns def if not defined
129
130 float Cvar_VariableValue (cvar_state_t *cvars, const char *var_name, int neededflags);
131 // returns 0 if not defined or non numeric
132
133 const char *Cvar_VariableStringOr (cvar_state_t *cvars, const char *var_name, const char *def, int neededflags);
134 // returns def if not defined
135
136 const char *Cvar_VariableString (cvar_state_t *cvars, const char *var_name, int neededflags);
137 // returns an empty string if not defined
138
139 const char *Cvar_VariableDefString (cvar_state_t *cvars, const char *var_name, int neededflags);
140 // returns an empty string if not defined
141
142 const char *Cvar_VariableDescription (cvar_state_t *cvars, const char *var_name, int neededflags);
143 // returns an empty string if not defined
144
145 const char *Cvar_CompleteVariable (cvar_state_t *cvars, const char *partial, int neededflags);
146 // attempts to match a partial variable name for command line completion
147 // returns NULL if nothing fits
148
149 void Cvar_PrintHelp(cvar_t *cvar, const char *name, qbool full);
150
151 void Cvar_CompleteCvarPrint (cvar_state_t *cvars, const char *partial, int neededflags);
152
153 qbool Cvar_Command (struct cmd_state_s *cmd);
154 // called by Cmd_ExecuteString when Cmd_Argv(cmd, 0) doesn't match a known
155 // command.  Returns true if the command was a variable reference that
156 // was handled. (print or change)
157
158 void Cvar_SaveInitState(cvar_state_t *cvars);
159 void Cvar_RestoreInitState(cvar_state_t *cvars);
160
161 void Cvar_UnlockDefaults(struct cmd_state_s *cmd);
162 void Cvar_LockDefaults_f(struct cmd_state_s *cmd);
163 void Cvar_ResetToDefaults_All_f(struct cmd_state_s *cmd);
164 void Cvar_ResetToDefaults_NoSaveOnly_f(struct cmd_state_s *cmd);
165 void Cvar_ResetToDefaults_SaveOnly_f(struct cmd_state_s *cmd);
166
167 void Cvar_WriteVariables (cvar_state_t *cvars, struct qfile_s *f);
168 // Writes lines containing "set variable value" for all variables
169 // with the archive flag set to true.
170
171 cvar_t *Cvar_FindVar(cvar_state_t *cvars, const char *var_name, int neededflags);
172 cvar_t *Cvar_FindVarAfter(cvar_state_t *cvars, const char *prev_var_name, int neededflags);
173
174 int Cvar_CompleteCountPossible(cvar_state_t *cvars, const char *partial, int neededflags);
175 const char **Cvar_CompleteBuildList(cvar_state_t *cvars, const char *partial, int neededflags);
176 // Added by EvilTypeGuy - functions for tab completion system
177 // Thanks to Fett erich@heintz.com
178 // Thanks to taniwha
179
180 /// Prints a list of Cvars including a count of them to the user console
181 /// Referenced in cmd.c in Cmd_Init hence it's inclusion here.
182 /// Added by EvilTypeGuy eviltypeguy@qeradiant.com
183 /// Thanks to Matthias "Maddes" Buecher, http://www.inside3d.com/qip/
184 void Cvar_List_f(struct cmd_state_s *cmd);
185
186 void Cvar_Set_f(struct cmd_state_s *cmd);
187 void Cvar_SetA_f(struct cmd_state_s *cmd);
188 void Cvar_Del_f(struct cmd_state_s *cmd);
189 // commands to create new cvars (or set existing ones)
190 // seta creates an archived cvar (saved to config)
191
192 /// allocates a cvar by name and returns its address,
193 /// or merely sets its value if it already exists.
194 cvar_t *Cvar_Get(cvar_state_t *cvars, const char *name, const char *value, int flags, const char *newdescription);
195
196 extern const char *cvar_dummy_description; // ALWAYS the same pointer
197
198 void Cvar_UpdateAllAutoCvars(cvar_state_t *cvars); // updates ALL autocvars of the active prog to the cvar values (savegame loading)
199
200 #ifdef FILLALLCVARSWITHRUBBISH
201 void Cvar_FillAll_f(cmd_state_t *cmd);
202 #endif /* FILLALLCVARSWITHRUBBISH */
203
204 #endif
205