CVarSystem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __CVARSYSTEM_H__
  21. #define __CVARSYSTEM_H__
  22. /*
  23. ===============================================================================
  24. Console Variables (CVars) are used to hold scalar or string variables
  25. that can be changed or displayed at the console as well as accessed
  26. directly in code.
  27. CVars are mostly used to hold settings that can be changed from the
  28. console or saved to and loaded from configuration files. CVars are also
  29. occasionally used to communicate information between different modules
  30. of the program.
  31. CVars are restricted from having the same names as console commands to
  32. keep the console interface from being ambiguous.
  33. CVars can be accessed from the console in three ways:
  34. cvarName prints the current value
  35. cvarName X sets the value to X if the variable exists
  36. set cvarName X as above, but creates the CVar if not present
  37. CVars may be declared in the global namespace, in classes and in functions.
  38. However declarations in classes and functions should always be static to
  39. save space and time. Making CVars static does not change their
  40. functionality due to their global nature.
  41. CVars should be contructed only through one of the constructors with name,
  42. value, flags and description. The name, value and description parameters
  43. to the constructor have to be static strings, do not use va() or the like
  44. functions returning a string.
  45. CVars may be declared multiple times using the same name string. However,
  46. they will all reference the same value and changing the value of one CVar
  47. changes the value of all CVars with the same name.
  48. CVars should always be declared with the correct type flag: CVAR_BOOL,
  49. CVAR_INTEGER or CVAR_FLOAT. If no such flag is specified the CVar
  50. defaults to type string. If the CVAR_BOOL flag is used there is no need
  51. to specify an argument auto-completion function because the CVar gets
  52. one assigned automatically.
  53. CVars are automatically range checked based on their type and any min/max
  54. or valid string set specified in the constructor.
  55. CVars are always considered cheats except when CVAR_NOCHEAT, CVAR_INIT,
  56. CVAR_ROM, CVAR_ARCHIVE, CVAR_SERVERINFO, CVAR_NETWORKSYNC
  57. is set.
  58. ===============================================================================
  59. */
  60. typedef enum {
  61. CVAR_ALL = -1, // all flags
  62. CVAR_BOOL = BIT(0), // variable is a boolean
  63. CVAR_INTEGER = BIT(1), // variable is an integer
  64. CVAR_FLOAT = BIT(2), // variable is a float
  65. CVAR_SYSTEM = BIT(3), // system variable
  66. CVAR_RENDERER = BIT(4), // renderer variable
  67. CVAR_SOUND = BIT(5), // sound variable
  68. CVAR_GUI = BIT(6), // gui variable
  69. CVAR_GAME = BIT(7), // game variable
  70. CVAR_TOOL = BIT(8), // tool variable
  71. CVAR_SERVERINFO = BIT(10), // sent from servers, available to menu
  72. CVAR_NETWORKSYNC = BIT(11), // cvar is synced from the server to clients
  73. CVAR_STATIC = BIT(12), // statically declared, not user created
  74. CVAR_CHEAT = BIT(13), // variable is considered a cheat
  75. CVAR_NOCHEAT = BIT(14), // variable is not considered a cheat
  76. CVAR_INIT = BIT(15), // can only be set from the command-line
  77. CVAR_ROM = BIT(16), // display only, cannot be set by user at all
  78. CVAR_ARCHIVE = BIT(17), // set to cause it to be saved to a config file
  79. CVAR_MODIFIED = BIT(18) // set when the variable is modified
  80. } cvarFlags_t;
  81. /*
  82. ===============================================================================
  83. idCVar
  84. ===============================================================================
  85. */
  86. class idCVar {
  87. public:
  88. // Never use the default constructor.
  89. idCVar() { assert( typeid( this ) != typeid( idCVar ) ); }
  90. // Always use one of the following constructors.
  91. idCVar( const char *name, const char *value, int flags, const char *description,
  92. argCompletion_t valueCompletion = NULL );
  93. idCVar( const char *name, const char *value, int flags, const char *description,
  94. float valueMin, float valueMax, argCompletion_t valueCompletion = NULL );
  95. idCVar( const char *name, const char *value, int flags, const char *description,
  96. const char **valueStrings, argCompletion_t valueCompletion = NULL );
  97. virtual ~idCVar() {}
  98. const char * GetName() const { return internalVar->name; }
  99. int GetFlags() const { return internalVar->flags; }
  100. const char * GetDescription() const { return internalVar->description; }
  101. float GetMinValue() const { return internalVar->valueMin; }
  102. float GetMaxValue() const { return internalVar->valueMax; }
  103. const char ** GetValueStrings() const { return valueStrings; }
  104. argCompletion_t GetValueCompletion() const { return valueCompletion; }
  105. bool IsModified() const { return ( internalVar->flags & CVAR_MODIFIED ) != 0; }
  106. void SetModified() { internalVar->flags |= CVAR_MODIFIED; }
  107. void ClearModified() { internalVar->flags &= ~CVAR_MODIFIED; }
  108. const char * GetDefaultString() const { return internalVar->InternalGetResetString(); }
  109. const char * GetString() const { return internalVar->value; }
  110. bool GetBool() const { return ( internalVar->integerValue != 0 ); }
  111. int GetInteger() const { return internalVar->integerValue; }
  112. float GetFloat() const { return internalVar->floatValue; }
  113. void SetString( const char *value ) { internalVar->InternalSetString( value ); }
  114. void SetBool( const bool value ) { internalVar->InternalSetBool( value ); }
  115. void SetInteger( const int value ) { internalVar->InternalSetInteger( value ); }
  116. void SetFloat( const float value ) { internalVar->InternalSetFloat( value ); }
  117. void SetInternalVar( idCVar *cvar ) { internalVar = cvar; }
  118. static void RegisterStaticVars();
  119. protected:
  120. const char * name; // name
  121. const char * value; // value
  122. const char * description; // description
  123. int flags; // CVAR_? flags
  124. float valueMin; // minimum value
  125. float valueMax; // maximum value
  126. const char ** valueStrings; // valid value strings
  127. argCompletion_t valueCompletion; // value auto-completion function
  128. int integerValue; // atoi( string )
  129. float floatValue; // atof( value )
  130. idCVar * internalVar; // internal cvar
  131. idCVar * next; // next statically declared cvar
  132. private:
  133. void Init( const char *name, const char *value, int flags, const char *description,
  134. float valueMin, float valueMax, const char **valueStrings, argCompletion_t valueCompletion );
  135. virtual void InternalSetString( const char *newValue ) {}
  136. virtual void InternalSetBool( const bool newValue ) {}
  137. virtual void InternalSetInteger( const int newValue ) {}
  138. virtual void InternalSetFloat( const float newValue ) {}
  139. virtual const char * InternalGetResetString() const { return value; }
  140. static idCVar * staticVars;
  141. };
  142. ID_INLINE idCVar::idCVar( const char *name, const char *value, int flags, const char *description,
  143. argCompletion_t valueCompletion ) {
  144. if ( !valueCompletion && ( flags & CVAR_BOOL ) ) {
  145. valueCompletion = idCmdSystem::ArgCompletion_Boolean;
  146. }
  147. Init( name, value, flags, description, 1, -1, NULL, valueCompletion );
  148. }
  149. ID_INLINE idCVar::idCVar( const char *name, const char *value, int flags, const char *description,
  150. float valueMin, float valueMax, argCompletion_t valueCompletion ) {
  151. Init( name, value, flags, description, valueMin, valueMax, NULL, valueCompletion );
  152. }
  153. ID_INLINE idCVar::idCVar( const char *name, const char *value, int flags, const char *description,
  154. const char **valueStrings, argCompletion_t valueCompletion ) {
  155. Init( name, value, flags, description, 1, -1, valueStrings, valueCompletion );
  156. }
  157. /*
  158. ===============================================================================
  159. idCVarSystem
  160. ===============================================================================
  161. */
  162. class idCVarSystem {
  163. public:
  164. virtual ~idCVarSystem() {}
  165. virtual void Init() = 0;
  166. virtual void Shutdown() = 0;
  167. virtual bool IsInitialized() const = 0;
  168. // Registers a CVar.
  169. virtual void Register( idCVar *cvar ) = 0;
  170. // Finds the CVar with the given name.
  171. // Returns NULL if there is no CVar with the given name.
  172. virtual idCVar * Find( const char *name ) = 0;
  173. // Sets the value of a CVar by name.
  174. virtual void SetCVarString( const char *name, const char *value, int flags = 0 ) = 0;
  175. virtual void SetCVarBool( const char *name, const bool value, int flags = 0 ) = 0;
  176. virtual void SetCVarInteger( const char *name, const int value, int flags = 0 ) = 0;
  177. virtual void SetCVarFloat( const char *name, const float value, int flags = 0 ) = 0;
  178. // Gets the value of a CVar by name.
  179. virtual const char * GetCVarString( const char *name ) const = 0;
  180. virtual bool GetCVarBool( const char *name ) const = 0;
  181. virtual int GetCVarInteger( const char *name ) const = 0;
  182. virtual float GetCVarFloat( const char *name ) const = 0;
  183. // Called by the command system when argv(0) doesn't match a known command.
  184. // Returns true if argv(0) is a variable reference and prints or changes the CVar.
  185. virtual bool Command( const idCmdArgs &args ) = 0;
  186. // Command and argument completion using callback for each valid string.
  187. virtual void CommandCompletion( void(*callback)( const char *s ) ) = 0;
  188. virtual void ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
  189. // Sets/gets/clears modified flags that tell what kind of CVars have changed.
  190. virtual void SetModifiedFlags( int flags ) = 0;
  191. virtual int GetModifiedFlags() const = 0;
  192. virtual void ClearModifiedFlags( int flags ) = 0;
  193. // Resets variables with one of the given flags set.
  194. virtual void ResetFlaggedVariables( int flags ) = 0;
  195. // Removes auto-completion from the flagged variables.
  196. virtual void RemoveFlaggedAutoCompletion( int flags ) = 0;
  197. // Writes variables with one of the given flags set to the given file.
  198. virtual void WriteFlaggedVariables( int flags, const char *setCmd, idFile *f ) const = 0;
  199. // Moves CVars to and from dictionaries.
  200. virtual void MoveCVarsToDict( int flags, idDict & dict, bool onlyModified = false ) const = 0;
  201. virtual void SetCVarsFromDict( const idDict &dict ) = 0;
  202. };
  203. extern idCVarSystem * cvarSystem;
  204. /*
  205. ===============================================================================
  206. CVar Registration
  207. Each DLL using CVars has to declare a private copy of the static variable
  208. idCVar::staticVars like this: idCVar * idCVar::staticVars = NULL;
  209. Furthermore idCVar::RegisterStaticVars() has to be called after the
  210. cvarSystem pointer is set when the DLL is first initialized.
  211. ===============================================================================
  212. */
  213. ID_INLINE void idCVar::Init( const char *name, const char *value, int flags, const char *description,
  214. float valueMin, float valueMax, const char **valueStrings, argCompletion_t valueCompletion ) {
  215. this->name = name;
  216. this->value = value;
  217. this->flags = flags;
  218. this->description = description;
  219. this->flags = flags | CVAR_STATIC;
  220. this->valueMin = valueMin;
  221. this->valueMax = valueMax;
  222. this->valueStrings = valueStrings;
  223. this->valueCompletion = valueCompletion;
  224. this->integerValue = 0;
  225. this->floatValue = 0.0f;
  226. this->internalVar = this;
  227. if ( staticVars != (idCVar *)0xFFFFFFFF ) {
  228. this->next = staticVars;
  229. staticVars = this;
  230. } else {
  231. cvarSystem->Register( this );
  232. }
  233. }
  234. ID_INLINE void idCVar::RegisterStaticVars() {
  235. if ( staticVars != (idCVar *)0xFFFFFFFF ) {
  236. for ( idCVar *cvar = staticVars; cvar; cvar = cvar->next ) {
  237. cvarSystem->Register( cvar );
  238. }
  239. staticVars = (idCVar *)0xFFFFFFFF;
  240. }
  241. }
  242. #endif /* !__CVARSYSTEM_H__ */