Xonotic
cvar.qh File Reference
#include "nil.qh"
#include "progname.qh"
#include "static.qh"
+ Include dependency graph for cvar.qh:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define __AUTOCVAR(file, archive, var, type, desc, default)
 
#define _AUTOCVAR(...)   EVAL__AUTOCVAR(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))
 
#define AUTOCVAR(...)   _AUTOCVAR(false, __VA_ARGS__)
 
#define AUTOCVAR_5(file, archive, var, type, desc)   __AUTOCVAR(file, archive, var, type, desc, default_##type)
 
#define AUTOCVAR_6(file, archive, var, type, default, desc)   __AUTOCVAR(file, archive, var, type, desc, default)
 
#define AUTOCVAR_SAVE(...)   _AUTOCVAR(true, __VA_ARGS__)
 
#define BINOP(op, len, expr)
 
#define EVAL__AUTOCVAR(...)   __VA_ARGS__
 
#define repr_cvar_bool(x)   ((x) ? "1" : "0")
 
#define repr_cvar_float(x)   (ftos(x))
 
#define repr_cvar_int(x)   (ftos(x))
 
#define repr_cvar_string(x)   (x)
 
#define repr_cvar_vector(x)   (sprintf("%v", x))
 
#define X(expr)
 

Functions

ERASEABLE bool cvar_value_issafe (string s)
 
ERASEABLE bool expr_evaluate (string s)
 Evaluate an expression of the form: [+ | -]? [var[op]val | [op]var | val | var] ... More...
 
ERASEABLE string MakeConsoleSafe (string input)
 escape the string to make it safe for consoles More...
 
ERASEABLE void RegisterCvars (void(string name, string def, string desc, bool archive, string file) f)
 
ERASEABLE void RegisterCvars_Save (string name, string def, string desc, bool archive, string file)
 
ERASEABLE void RegisterCvars_Set (string name, string def, string desc, bool archive, string file)
 
 STATIC_INIT_LATE (Cvars)
 

Variables

const noref bool default_bool = false
 
const noref float default_float = 0
 
const noref int default_int = 0
 
const noref string default_string = ""
 
const noref vector default_vector = '0 0 0'
 
int RegisterCvars_Save_fd
 

Macro Definition Documentation

◆ __AUTOCVAR

#define __AUTOCVAR (   file,
  archive,
  var,
  type,
  desc,
  default 
)
Value:
ACCUMULATE void RegisterCvars(void(string, string, string, bool, string) f) \
{ \
f( #var, repr_cvar_##type(default), desc, archive, file); \
} \
type autocvar_##var = default
#define ACCUMULATE
Definition: _all.inc:29
ERASEABLE void RegisterCvars(void(string name, string def, string desc, bool archive, string file) f)
Definition: cvar.qh:8

Definition at line 148 of file cvar.qh.

◆ _AUTOCVAR

#define _AUTOCVAR (   ...)    EVAL__AUTOCVAR(OVERLOAD(AUTOCVAR, __FILE__, __VA_ARGS__))

Definition at line 158 of file cvar.qh.

◆ AUTOCVAR

#define AUTOCVAR (   ...)    _AUTOCVAR(false, __VA_ARGS__)

Definition at line 161 of file cvar.qh.

◆ AUTOCVAR_5

#define AUTOCVAR_5 (   file,
  archive,
  var,
  type,
  desc 
)    __AUTOCVAR(file, archive, var, type, desc, default_##type)

Definition at line 154 of file cvar.qh.

◆ AUTOCVAR_6

#define AUTOCVAR_6 (   file,
  archive,
  var,
  type,
  default,
  desc 
)    __AUTOCVAR(file, archive, var, type, desc, default)

Definition at line 156 of file cvar.qh.

◆ AUTOCVAR_SAVE

#define AUTOCVAR_SAVE (   ...)    _AUTOCVAR(true, __VA_ARGS__)

Definition at line 160 of file cvar.qh.

◆ BINOP

#define BINOP (   op,
  len,
  expr 
)
Value:
if ((o = strstrofs(s, op, 0)) >= 0) { \
k = substring(s, 0, o); \
v = substring(s, o + len, -1); \
X(expr); \
}
#define strstrofs
Definition: dpextensions.qh:42

Referenced by expr_evaluate().

◆ EVAL__AUTOCVAR

#define EVAL__AUTOCVAR (   ...)    __VA_ARGS__

Definition at line 159 of file cvar.qh.

◆ repr_cvar_bool

#define repr_cvar_bool (   x)    ((x) ? "1" : "0")

Definition at line 136 of file cvar.qh.

◆ repr_cvar_float

#define repr_cvar_float (   x)    (ftos(x))

Definition at line 138 of file cvar.qh.

◆ repr_cvar_int

#define repr_cvar_int (   x)    (ftos(x))

Definition at line 137 of file cvar.qh.

◆ repr_cvar_string

#define repr_cvar_string (   x)    (x)

Definition at line 139 of file cvar.qh.

◆ repr_cvar_vector

#define repr_cvar_vector (   x)    (sprintf("%v", x))

Definition at line 140 of file cvar.qh.

◆ X

#define X (   expr)
Value:
if (expr) \
continue; \
expr_fail = true; \
break;

Referenced by expr_evaluate().

Function Documentation

◆ cvar_value_issafe()

ERASEABLE bool cvar_value_issafe ( string  s)

Definition at line 11 of file cvar.qh.

References ERASEABLE, and strstrofs.

Referenced by _MapInfo_Parse_Settemp(), InitGameplayMode(), MapInfo_Get_ByName_NoFallbacks(), and spawnfunc().

12 {
13  if (strstrofs(s, "\"", 0) >= 0) return false;
14  if (strstrofs(s, "\\", 0) >= 0) return false;
15  if (strstrofs(s, ";", 0) >= 0) return false;
16  if (strstrofs(s, "$", 0) >= 0) return false;
17  if (strstrofs(s, "\r", 0) >= 0) return false;
18  if (strstrofs(s, "\n", 0) >= 0) return false;
19  return true;
20 }
#define strstrofs
Definition: dpextensions.qh:42
+ Here is the caller graph for this function:

◆ expr_evaluate()

ERASEABLE bool expr_evaluate ( string  s)

Evaluate an expression of the form: [+ | -]? [var[op]val | [op]var | val | var] ...

+: all must match. this is the default -: one must NOT match

var>x var<x var>="">=x var<=x var==x var!=x var===x var!==x

Definition at line 48 of file cvar.qh.

References argv(), BINOP, cvar(), cvar_string(), ERASEABLE, ftos(), stof(), str2chr, substring(), tokenize_console, v, and X.

Referenced by SV_OnEntityPreSpawnFunction().

49 {
50  bool ret = false;
51  if (str2chr(s, 0) == '+') {
52  s = substring(s, 1, -1);
53  } else if (str2chr(s, 0) == '-') {
54  ret = true;
55  s = substring(s, 1, -1);
56  }
57  bool expr_fail = false;
58  for (int i = 0, n = tokenize_console(s); i < n; ++i) {
59  int o;
60  string k, v;
61  s = argv(i);
62  #define X(expr) \
63  if (expr) \
64  continue; \
65  expr_fail = true; \
66  break;
67 
68  #define BINOP(op, len, expr) \
69  if ((o = strstrofs(s, op, 0)) >= 0) { \
70  k = substring(s, 0, o); \
71  v = substring(s, o + len, -1); \
72  X(expr); \
73  }
74  BINOP(">=", 2, cvar(k) >= stof(v));
75  BINOP("<=", 2, cvar(k) <= stof(v));
76  BINOP(">", 1, cvar(k) > stof(v));
77  BINOP("<", 1, cvar(k) < stof(v));
78  BINOP("==", 2, cvar(k) == stof(v));
79  BINOP("!=", 2, cvar(k) != stof(v));
80  BINOP("===", 3, cvar_string(k) == v);
81  BINOP("!==", 3, cvar_string(k) != v);
82  {
83  k = s;
84  bool b = true;
85  if (str2chr(k, 0) == '!') {
86  k = substring(s, 1, -1);
87  b = false;
88  }
89  float f = stof(k);
90  bool isnum = ftos(f) == k;
91  X(boolean(isnum ? f : cvar(k)) == b);
92  }
93  #undef BINOP
94  #undef X
95  }
96  if (!expr_fail) {
97  ret = !ret;
98  }
99  // now ret is true if we want to keep the item, and false if we want to get rid of it
100  return ret;
101 }
#define str2chr
Definition: dpextensions.qh:45
#define BINOP(op, len, expr)
#define tokenize_console
Definition: dpextensions.qh:24
vector v
Definition: ent_cs.qc:116
#define X(expr)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ MakeConsoleSafe()

ERASEABLE string MakeConsoleSafe ( string  input)

escape the string to make it safe for consoles

Definition at line 24 of file cvar.qh.

References ERASEABLE.

Referenced by GameCommand_adminmsg(), Local_Notification(), Local_Notification_sprintf(), Local_Notification_WOVA(), and RegisterCvars_Set().

25 {
26  input = strreplace("\n", "", input);
27  input = strreplace("\\", "\\\\", input);
28  input = strreplace("$", "$$", input);
29  input = strreplace("\"", "\\\"", input);
30  return input;
31 }
+ Here is the caller graph for this function:

◆ RegisterCvars()

ERASEABLE void RegisterCvars ( void(string name, string def, string desc, bool archive, string file)  f)

Definition at line 8 of file cvar.qh.

References ERASEABLE.

Referenced by STATIC_INIT_LATE().

8 {}
+ Here is the caller graph for this function:

◆ RegisterCvars_Save()

ERASEABLE void RegisterCvars_Save ( string  name,
string  def,
string  desc,
bool  archive,
string  file 
)

Definition at line 113 of file cvar.qh.

References fputs().

Referenced by STATIC_INIT_LATE().

114 {
115  if (!archive) return;
116  fputs(RegisterCvars_Save_fd, sprintf("seta %s \"%s\"\n", name, def));
117 }
int RegisterCvars_Save_fd
Definition: cvar.qh:111
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ RegisterCvars_Set()

ERASEABLE void RegisterCvars_Set ( string  name,
string  def,
string  desc,
bool  archive,
string  file 
)

Definition at line 104 of file cvar.qh.

References localcmd, and MakeConsoleSafe().

Referenced by STATIC_INIT_LATE().

105 {
106  localcmd(sprintf("\nset %1$s \"$%1$s\" \"%2$s\"\n", name, MakeConsoleSafe(desc)));
107  if (archive)
108  localcmd(sprintf("\nseta %1$s \"$%1$s\"\n", name));
109 }
ERASEABLE string MakeConsoleSafe(string input)
escape the string to make it safe for consoles
Definition: cvar.qh:24
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ STATIC_INIT_LATE()

STATIC_INIT_LATE ( Cvars  )

Definition at line 119 of file cvar.qh.

References fclose(), FILE_WRITE, fopen(), RegisterCvars(), RegisterCvars_Save(), and RegisterCvars_Set().

120 {
122  RegisterCvars_Save_fd = fopen(sprintf("default%s.cfg", PROGNAME), FILE_WRITE);
123  if (RegisterCvars_Save_fd >= 0)
124  {
127  }
128 }
ERASEABLE void RegisterCvars(void(string name, string def, string desc, bool archive, string file) f)
Definition: cvar.qh:8
ERASEABLE void RegisterCvars_Save(string name, string def, string desc, bool archive, string file)
Definition: cvar.qh:113
int RegisterCvars_Save_fd
Definition: cvar.qh:111
ERASEABLE void RegisterCvars_Set(string name, string def, string desc, bool archive, string file)
Definition: cvar.qh:104
const float FILE_WRITE
Definition: csprogsdefs.qc:233
+ Here is the call graph for this function:

Variable Documentation

◆ default_bool

const noref bool default_bool = false

Definition at line 130 of file cvar.qh.

◆ default_float

const noref float default_float = 0

Definition at line 132 of file cvar.qh.

◆ default_int

const noref int default_int = 0

Definition at line 131 of file cvar.qh.

◆ default_string

const noref string default_string = ""

Definition at line 133 of file cvar.qh.

◆ default_vector

const noref vector default_vector = '0 0 0'

Definition at line 134 of file cvar.qh.

◆ RegisterCvars_Save_fd

int RegisterCvars_Save_fd

Definition at line 111 of file cvar.qh.