home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / framework / CVarSystem.h < prev    next >
C/C++ Source or Header  |  2005-11-14  |  16KB  |  408 lines

  1. // Copyright (C) 2004 Id Software, Inc.
  2. //
  3.  
  4. #ifndef __CVARSYSTEM_H__
  5. #define __CVARSYSTEM_H__
  6.  
  7. /*
  8. ===============================================================================
  9.  
  10.     Console Variables (CVars) are used to hold scalar or string variables
  11.     that can be changed or displayed at the console as well as accessed
  12.     directly in code.
  13.  
  14.     CVars are mostly used to hold settings that can be changed from the
  15.     console or saved to and loaded from configuration files. CVars are also
  16.     occasionally used to communicate information between different modules
  17.     of the program.
  18.  
  19.     CVars are restricted from having the same names as console commands to
  20.     keep the console interface from being ambiguous.
  21.  
  22.     CVars can be accessed from the console in three ways:
  23.     cvarName            prints the current value
  24.     cvarName X            sets the value to X if the variable exists
  25.     set cvarName X        as above, but creates the CVar if not present
  26.  
  27.     CVars may be declared in the global namespace, in classes and in functions.
  28.     However declarations in classes and functions should always be static to
  29.     save space and time. Making CVars static does not change their
  30.     functionality due to their global nature.
  31.  
  32.     CVars should be contructed only through one of the constructors with name,
  33.     value, flags and description. The name, value and description parameters
  34.     to the constructor have to be static strings, do not use va() or the like
  35.     functions returning a string.
  36.  
  37.     CVars may be declared multiple times using the same name string. However,
  38.     they will all reference the same value and changing the value of one CVar
  39.     changes the value of all CVars with the same name.
  40.  
  41.     CVars should always be declared with the correct type flag: CVAR_BOOL,
  42.     CVAR_INTEGER or CVAR_FLOAT. If no such flag is specified the CVar
  43.     defaults to type string. If the CVAR_BOOL flag is used there is no need
  44.     to specify an argument auto-completion function because the CVar gets
  45.     one assigned automatically.
  46.  
  47.     CVars are automatically range checked based on their type and any min/max
  48.     or valid string set specified in the constructor.
  49.  
  50.     CVars are always considered cheats except when CVAR_NOCHEAT, CVAR_INIT,
  51.     CVAR_ROM, CVAR_ARCHIVE, CVAR_USERINFO, CVAR_SERVERINFO, CVAR_NETWORKSYNC
  52.     is set.
  53.  
  54. ===============================================================================
  55. */
  56.  
  57. typedef enum {
  58.     CVAR_ALL                = -1,        // all flags
  59.     CVAR_BOOL                = BIT(0),    // variable is a boolean
  60.     CVAR_INTEGER            = BIT(1),    // variable is an integer
  61.     CVAR_FLOAT                = BIT(2),    // variable is a float
  62.     CVAR_SYSTEM                = BIT(3),    // system variable
  63.     CVAR_RENDERER            = BIT(4),    // renderer variable
  64.     CVAR_SOUND                = BIT(5),    // sound variable
  65.     CVAR_GUI                = BIT(6),    // gui variable
  66.     CVAR_GAME                = BIT(7),    // game variable
  67.     CVAR_TOOL                = BIT(8),    // tool variable
  68.     CVAR_USERINFO            = BIT(9),    // sent to servers, available to menu
  69.     CVAR_SERVERINFO            = BIT(10),    // sent from servers, available to menu
  70.     CVAR_NETWORKSYNC        = BIT(11),    // cvar is synced from the server to clients
  71.     CVAR_STATIC                = BIT(12),    // statically declared, not user created
  72.     CVAR_CHEAT                = BIT(13),    // variable is considered a cheat
  73.     CVAR_NOCHEAT            = BIT(14),    // variable is not considered a cheat
  74.     CVAR_INIT                = BIT(15),    // can only be set from the command-line
  75.     CVAR_ROM                = BIT(16),    // display only, cannot be set by user at all
  76.     CVAR_ARCHIVE            = BIT(17),    // set to cause it to be saved to a config file
  77.     CVAR_MODIFIED            = BIT(18),    // set when the variable is modified
  78.     CVAR_INFO                = BIT(19)    // sent as part of the MOTD packet
  79. } cvarFlags_t;
  80.  
  81.  
  82. /*
  83. ===============================================================================
  84.  
  85.     idCVar
  86.  
  87. ===============================================================================
  88. */
  89.  
  90. class idCVar {
  91. public:
  92.                             // Never use the default constructor.
  93.                             idCVar( void ) { assert( typeid( this ) != typeid( idCVar ) ); }
  94.  
  95.                             // Always use one of the following constructors.
  96.                             idCVar( const char *name, const char *value, int flags, const char *description,
  97.                                     argCompletion_t valueCompletion = NULL );
  98.                             idCVar( const char *name, const char *value, int flags, const char *description,
  99.                                     float valueMin, float valueMax, argCompletion_t valueCompletion = NULL );
  100.                             idCVar( const char *name, const char *value, int flags, const char *description,
  101.                                     const char **valueStrings, argCompletion_t valueCompletion = NULL );
  102.  
  103.     virtual                    ~idCVar( void ) {}
  104.  
  105.     const char *            GetName( void ) const { return internalVar->name; }
  106.     int                        GetFlags( void ) const { return internalVar->flags; }
  107.     const char *            GetDescription( void ) const { return internalVar->description; }
  108.     float                    GetMinValue( void ) const { return internalVar->valueMin; }
  109.     float                    GetMaxValue( void ) const { return internalVar->valueMax; }
  110.     const char **            GetValueStrings( void ) const { return valueStrings; }
  111.     argCompletion_t            GetValueCompletion( void ) const { return valueCompletion; }
  112.  
  113.     bool                    IsModified( void ) const { return ( internalVar->flags & CVAR_MODIFIED ) != 0; }
  114.     void                    SetModified( void ) { internalVar->flags |= CVAR_MODIFIED; }
  115.     void                    ClearModified( void ) { internalVar->flags &= ~CVAR_MODIFIED; }
  116.  
  117.     const char *            GetString( void ) const { return internalVar->value; }
  118.     bool                    GetBool( void ) const { return ( internalVar->integerValue != 0 ); }
  119.     int                        GetInteger( void ) const { return internalVar->integerValue; }
  120.     float                    GetFloat( void ) const { return internalVar->floatValue; }
  121.  
  122.     void                    SetString( const char *value ) { internalVar->InternalSetString( value ); }
  123.     void                    SetBool( const bool value ) { internalVar->InternalSetBool( value ); }
  124.     void                    SetInteger( const int value ) { internalVar->InternalSetInteger( value ); }
  125.     void                    SetFloat( const float value ) { internalVar->InternalSetFloat( value ); }
  126.  
  127.     void                    SetInternalVar( idCVar *cvar ) { internalVar = cvar; }
  128.  
  129.     static void                RegisterStaticVars( void );
  130.  
  131. protected:
  132.     const char *            name;                    // name
  133.     const char *            value;                    // value
  134.     const char *            description;            // description
  135.     int                        flags;                    // CVAR_? flags
  136.     float                    valueMin;                // minimum value
  137.     float                    valueMax;                // maximum value
  138.     const char **            valueStrings;            // valid value strings
  139.     argCompletion_t            valueCompletion;        // value auto-completion function
  140.     int                        integerValue;            // atoi( string )
  141.     float                    floatValue;                // atof( value )
  142.     idCVar *                internalVar;            // internal cvar
  143.     idCVar *                next;                    // next statically declared cvar
  144.  
  145. private:
  146.     void                    Init( const char *name, const char *value, int flags, const char *description,
  147.                                     float valueMin, float valueMax, const char **valueStrings, argCompletion_t valueCompletion );
  148.  
  149.     virtual void            InternalSetString( const char *newValue ) {}
  150.     virtual void            InternalSetBool( const bool newValue ) {}
  151.     virtual void            InternalSetInteger( const int newValue ) {}
  152.     virtual void            InternalSetFloat( const float newValue ) {}
  153.  
  154.     static idCVar *            staticVars;
  155. };
  156.  
  157. ID_INLINE idCVar::idCVar( const char *name, const char *value, int flags, const char *description,
  158.                             argCompletion_t valueCompletion ) {
  159.     if ( !valueCompletion && ( flags & CVAR_BOOL ) ) {
  160.         valueCompletion = idCmdSystem::ArgCompletion_Boolean;
  161.     }
  162.     Init( name, value, flags, description, 1, -1, NULL, valueCompletion );
  163. }
  164.  
  165. ID_INLINE idCVar::idCVar( const char *name, const char *value, int flags, const char *description,
  166.                             float valueMin, float valueMax, argCompletion_t valueCompletion ) {
  167.     Init( name, value, flags, description, valueMin, valueMax, NULL, valueCompletion );
  168. }
  169.  
  170. ID_INLINE idCVar::idCVar( const char *name, const char *value, int flags, const char *description,
  171.                             const char **valueStrings, argCompletion_t valueCompletion ) {
  172.     Init( name, value, flags, description, 1, -1, valueStrings, valueCompletion );
  173. }
  174.  
  175. // RAVEN BEGIN
  176. // rjohnson: new help system for cvar ui
  177.  
  178. /*
  179. ===============================================================================
  180.  
  181.     idCVarHelp
  182.  
  183. ===============================================================================
  184. */
  185.  
  186. typedef enum {
  187.     CVARHELP_ALL            = -1,        // all categories
  188.     CVARHELP_GAME            = BIT(0),    // game menu
  189.     CVARHELP_RENDERER        = BIT(1),    // renderer menu
  190.     CVARHELP_PHYSICS        = BIT(2),    // physics menu
  191.     CVARHELP_SOUND            = BIT(3),    // sound menu
  192.     CVARHELP_AI                = BIT(4),    // AI menu
  193. } cvarHelpCategory_t;
  194.  
  195.  
  196. class idCVarHelp {
  197. public:
  198.                                 // Never use the default constructor.
  199.                                 idCVarHelp( void ) { assert( typeid(this) != typeid(idCVarHelp) ); }
  200.  
  201.                                 // Always use one of the following constructors.
  202.                                 idCVarHelp( const char *cvarName, const char *friendlyName, const char *choices, const char *values, cvarHelpCategory_t category );
  203.  
  204.                                 idCVarHelp( const idCVarHelp © );
  205.  
  206.     const char *                GetCVarName( void ) const { return cvarName; }    
  207.     const char *                GetFriendlyName( void ) const { return friendlyName; }    
  208.     const char *                GetChoices( void ) const { return choices; }    
  209.     const char *                GetValues( void ) const { return values; }    
  210.     const cvarHelpCategory_t    GetCategory( void ) const { return category; }    
  211.     const idCVarHelp *            GetNext( void ) const { return next; }
  212.  
  213.     void                        SetNext( const idCVarHelp *value ) { next = value; }
  214.  
  215.     static void                    RegisterStatics( void );
  216.  
  217. private:
  218.     const char *                cvarName;                // the cvar this help belongs to
  219.     const char *                friendlyName;            // a textual name for the cvar impaired
  220.     const char *                choices;                // a textual list of choices for the cvar impaired
  221.     const char *                values;                    // the list of values that goes with the choices
  222.     cvarHelpCategory_t            category;                // the category(s) this cvar should appear under
  223.     const idCVarHelp *            next;                    // next statically declared cvar helper
  224.  
  225.     static idCVarHelp *            staticCVarHelps;
  226.     static idCVarHelp *            staticCVarHelpsTail;
  227. };
  228.  
  229. ID_INLINE idCVarHelp::idCVarHelp( const idCVarHelp © ) {
  230.     cvarName = copy.cvarName;
  231.     friendlyName = copy.friendlyName;
  232.     choices = copy.choices;
  233.     values = copy.values;
  234.     category = copy.category;
  235.     next = NULL;
  236. }
  237.  
  238. // RAVEN END
  239.  
  240. /*
  241. ===============================================================================
  242.  
  243.     idCVarSystem
  244.  
  245. ===============================================================================
  246. */
  247.  
  248. class idCVarSystem {
  249. public:
  250.     virtual                    ~idCVarSystem( void ) {}
  251.  
  252.     virtual void            Init( void ) = 0;
  253.     virtual void            Shutdown( void ) = 0;
  254.     virtual bool            IsInitialized( void ) const = 0;
  255.  
  256.                             // Registers a CVar.
  257.     virtual void            Register( idCVar *cvar ) = 0;
  258.  
  259. // RAVEN BEGIN
  260. // rjohnson: new help system for cvar ui
  261.                             // Registers a CVarHelp.
  262.     virtual void            Register( const idCVarHelp *cvarHelp ) = 0;
  263.     virtual idCVarHelp *    GetHelps( cvarHelpCategory_t category ) = 0;
  264. // RAVEN END
  265.  
  266.                             // Finds the CVar with the given name.
  267.                             // Returns NULL if there is no CVar with the given name.
  268.     virtual idCVar *        Find( const char *name ) = 0;
  269.  
  270.                             // Sets the value of a CVar by name.
  271.     virtual void            SetCVarString( const char *name, const char *value, int flags = 0 ) = 0;
  272.     virtual void            SetCVarBool( const char *name, const bool value, int flags = 0 ) = 0;
  273.     virtual void            SetCVarInteger( const char *name, const int value, int flags = 0 ) = 0;
  274.     virtual void            SetCVarFloat( const char *name, const float value, int flags = 0 ) = 0;
  275.  
  276.                             // Gets the value of a CVar by name.
  277.     virtual const char *    GetCVarString( const char *name ) const = 0;
  278.     virtual bool            GetCVarBool( const char *name ) const = 0;
  279.     virtual int                GetCVarInteger( const char *name ) const = 0;
  280.     virtual float            GetCVarFloat( const char *name ) const = 0;
  281.  
  282.                             // Called by the command system when argv(0) doesn't match a known command.
  283.                             // Returns true if argv(0) is a variable reference and prints or changes the CVar.
  284.     virtual bool            Command( const idCmdArgs &args ) = 0;
  285.  
  286.                             // Command and argument completion using callback for each valid string.
  287.     virtual void            CommandCompletion( void(*callback)( const char *s ) ) = 0;
  288.     virtual void            ArgCompletion( const char *cmdString, void(*callback)( const char *s ) ) = 0;
  289.  
  290.                             // Sets/gets/clears modified flags that tell what kind of CVars have changed.
  291.     virtual void            SetModifiedFlags( int flags ) = 0;
  292.     virtual int                GetModifiedFlags( void ) const = 0;
  293.     virtual void            ClearModifiedFlags( int flags ) = 0;
  294.  
  295.                             // Resets variables with one of the given flags set.
  296.     virtual void            ResetFlaggedVariables( int flags ) = 0;
  297.  
  298.                             // Removes auto-completion from the flagged variables.
  299.     virtual void            RemoveFlaggedAutoCompletion( int flags ) = 0;
  300.  
  301.                             // Writes variables with one of the given flags set to the given file.
  302.     virtual void            WriteFlaggedVariables( int flags, const char *setCmd, idFile *f ) const = 0;
  303. // RAVEN BEGIN
  304. // nrausch: memory cvar support
  305.     virtual unsigned int    WriteFlaggedVariables( int flags, const char *setCmd, byte *buf, unsigned int bufSize ) const = 0;
  306.     virtual void            ApplyFlaggedVariables( byte *buf, unsigned int bufSize ) = 0;
  307.     virtual idStr            WriteFlaggedVariables( int flags ) const = 0;
  308. // RAVEN END
  309.  
  310.                             // Moves CVars to and from dictionaries.
  311.     virtual const idDict *    MoveCVarsToDict( int flags ) const = 0;
  312.     virtual void            SetCVarsFromDict( const idDict &dict ) = 0;
  313. };
  314.  
  315. extern idCVarSystem *        cvarSystem;
  316.  
  317.  
  318. /*
  319. ===============================================================================
  320.  
  321.     CVar Registration
  322.  
  323.     Each DLL using CVars has to declare a private copy of the static variable
  324.     idCVar::staticVars like this: idCVar * idCVar::staticVars = NULL;
  325.     Furthermore idCVar::RegisterStaticVars() has to be called after the
  326.     cvarSystem pointer is set when the DLL is first initialized.
  327.  
  328. ===============================================================================
  329. */
  330.  
  331. ID_INLINE void idCVar::Init( const char *name, const char *value, int flags, const char *description,
  332.                             float valueMin, float valueMax, const char **valueStrings, argCompletion_t valueCompletion ) {
  333.     this->name = name;
  334.     this->value = value;
  335.     this->flags = flags;
  336.     this->description = description;
  337.     this->flags = flags | CVAR_STATIC;
  338.     this->valueMin = valueMin;
  339.     this->valueMax = valueMax;
  340.     this->valueStrings = valueStrings;
  341.     this->valueCompletion = valueCompletion;
  342.     this->integerValue = 0;
  343.     this->floatValue = 0.0f;
  344.     this->internalVar = this;
  345.     if ( staticVars != (idCVar *)0xFFFFFFFF ) {
  346.         this->next = staticVars;
  347.         staticVars = this;
  348.     } else {
  349.         cvarSystem->Register( this );
  350.     }
  351. }
  352.  
  353. ID_INLINE void idCVar::RegisterStaticVars( void ) {
  354. // RAVEN BEGIN
  355. // jnewquist: Tag scope and callees to track allocations using "new".
  356.     MEM_SCOPED_TAG(tag,MA_CVAR);
  357. // RAVEN END
  358.     if ( staticVars != (idCVar *)0xFFFFFFFF ) {
  359.         for ( idCVar *cvar = staticVars; cvar; cvar = cvar->next ) {
  360.             cvarSystem->Register( cvar );
  361.         }
  362.         staticVars = (idCVar *)0xFFFFFFFF;
  363.     }
  364. }
  365.  
  366. // RAVEN BEGIN
  367. // rjohnson: new help system for cvar ui
  368. ID_INLINE idCVarHelp::idCVarHelp( const char *cvarName, const char *friendlyName, const char *choices, const char *values, cvarHelpCategory_t category ) {
  369. // RAVEN BEGIN
  370. // jnewquist: Tag scope and callees to track allocations using "new".
  371.     MEM_SCOPED_TAG(tag,MA_CVAR);
  372. // RAVEN END
  373.     this->cvarName = cvarName;
  374.     this->friendlyName = friendlyName;
  375.     this->choices = choices;
  376.     this->values = values;
  377.     this->category = category;
  378.     this->next = NULL;
  379.  
  380.     if ( staticCVarHelps != (idCVarHelp *)0xFFFFFFFF ) {
  381.         if ( !staticCVarHelpsTail ) {
  382.             staticCVarHelps = this;
  383.         } else {
  384.             staticCVarHelpsTail->next = this;
  385.         }
  386.         staticCVarHelpsTail = this;
  387.         staticCVarHelpsTail->next = NULL;
  388.     } else {
  389.         cvarSystem->Register( this );
  390.     }
  391. }
  392.  
  393. ID_INLINE void idCVarHelp::RegisterStatics( void ) {
  394. // RAVEN BEGIN
  395. // jnewquist: Tag scope and callees to track allocations using "new".
  396.     MEM_SCOPED_TAG(tag,MA_CVAR);
  397. // RAVEN END
  398.     if ( staticCVarHelps != (idCVarHelp *)0xFFFFFFFF ) {
  399.         for ( const idCVarHelp *cvarHelp = staticCVarHelps; cvarHelp; cvarHelp = cvarHelp->next ) {
  400.             cvarSystem->Register( cvarHelp );
  401.         }
  402.         staticCVarHelps = (idCVarHelp *)0xFFFFFFFF;
  403.     }
  404. }
  405. // RAVEN END
  406.  
  407. #endif /* !__CVARSYSTEM_H__ */
  408.