plugins/include/console.inc
author Scott Ehlert <ds@alliedmods.net>
Tue May 01 05:29:37 2007 +0000 (2007-05-01)
changeset 714 93b19876407e
parent 708 8383bd9c7092
child 815 8946ca39d52b
permissions -rw-r--r--
- Removed GetConVarMin/Max() and replaced them with GetConVarBounds()
- Added SetConVarBounds() to set convar constraints after convar has already been created
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _console_included
    17  #endinput
    18 #endif
    19 #define _console_included
    20 
    21 /**
    22  * Console variable bound values used with Get/SetConVarBounds()
    23  */
    24 enum ConVarBounds
    25 {
    26 	ConVarBound_Upper = 0,
    27 	ConVarBound_Lower
    28 };
    29 
    30 /**
    31  * Console variable query helper values.
    32  */
    33 enum QueryCookie
    34 {
    35 	QUERYCOOKIE_FAILED = 0,
    36 }
    37 
    38 /**
    39  * Console variable query result values.
    40  */
    41 enum ConVarQueryResult
    42 {
    43 	ConVarQuery_Okay = 0,				/**< Retrieval of client convar value was successful. */
    44 	ConVarQuery_NotFound,				/**< Client convar was not found. */
    45 	ConVarQuery_NotValid,				/**< A console command with the same name was found, but there is no convar. */
    46 	ConVarQuery_Protected				/**< Client convar was found, but it is protected. The server cannot retrieve its value. */
    47 };
    48 
    49 /**
    50  * @section Flags for console commands and console variables.  The descriptions 
    51  * for each constant come directly from the Source SDK.
    52  */
    53 #define FCVAR_NONE				0		/**< The default, no flags at all */
    54 #define FCVAR_UNREGISTERED		(1<<0)	/**< If this is set, don't add to linked list, etc. */
    55 #define FCVAR_LAUNCHER			(1<<1)	/**< Defined by launcher. */
    56 #define FCVAR_GAMEDLL			(1<<2)	/**< Defined by the game DLL. */
    57 #define FCVAR_CLIENTDLL			(1<<3)	/**< Defined by the client DLL. */
    58 #define FCVAR_MATERIAL_SYSTEM	(1<<4)	/**< Defined by the material system. */
    59 #define FCVAR_PROTECTED			(1<<5)	/**< It's a server cvar, but we don't send the data since it's a password, etc. Sends 1 if it's not bland/zero, 0 otherwise as value. */
    60 #define FCVAR_SPONLY			(1<<6)	/**< This cvar cannot be changed by clients connected to a multiplayer server. */
    61 #define	FCVAR_ARCHIVE			(1<<7)	/**< Set to cause it to be saved to vars.rc */
    62 #define	FCVAR_NOTIFY			(1<<8)	/**< Notifies players when changed. */
    63 #define	FCVAR_USERINFO			(1<<9)	/**< Changes the client's info string. */
    64 #define FCVAR_PRINTABLEONLY		(1<<10)	/**< This cvar's string cannot contain unprintable characters (e.g., used for player name, etc.) */
    65 #define FCVAR_UNLOGGED			(1<<11)	/**< If this is a FCVAR_SERVER, don't log changes to the log file / console if we are creating a log */
    66 #define FCVAR_NEVER_AS_STRING	(1<<12)	/**< Never try to print that cvar. */
    67 #define FCVAR_REPLICATED		(1<<13)	/**< Server setting enforced on clients. */
    68 #define FCVAR_CHEAT				(1<<14)	/**< Only useable in singleplayer / debug / multiplayer & sv_cheats */
    69 #define FCVAR_STUDIORENDER		(1<<15)	/**< Defined by the studiorender system. */
    70 #define FCVAR_DEMO				(1<<16)	/**< Record this cvar when starting a demo file. */
    71 #define FCVAR_DONTRECORD		(1<<17)	/**< Don't record these command in demo files. */
    72 #define FCVAR_PLUGIN			(1<<18)	/**< Defined by a 3rd party plugin. */
    73 #define FCVAR_DATACACHE			(1<<19)	/**< Defined by the datacache system. */
    74 #define FCVAR_TOOLSYSTEM		(1<<20)	/**< Defined by an IToolSystem library */
    75 #define FCVAR_FILESYSTEM		(1<<21)	/**< Defined by the file system. */
    76 #define FCVAR_NOT_CONNECTED		(1<<22)	/**< Cvar cannot be changed by a client that is connected to a server. */
    77 #define FCVAR_SOUNDSYSTEM		(1<<23)	/**< Defined by the soundsystem library. */
    78 #define FCVAR_ARCHIVE_XBOX		(1<<24)	/**< Cvar written to config.cfg on the Xbox. */
    79 #define FCVAR_INPUTSYSTEM		(1<<25)	/**< Defined by the inputsystem DLL. */
    80 #define FCVAR_NETWORKSYSTEM		(1<<26)	/**< Defined by the network system. */
    81 #define FCVAR_VPHYSICS			(1<<27)	/**< Defined by vphysics. */
    82 
    83 /**
    84  * @endsection
    85  */
    86 
    87 /**
    88  * Executes a server command as if it were on the server console (or RCON)
    89  *
    90  * @param format		Formatting rules.
    91  * @param ...			Variable number of format parameters.
    92  * @noreturn
    93  */
    94 native ServerCommand(const String:format[], any:...);
    95 
    96 /**
    97  * Inserts a server command at the beginning of the server command buffer.
    98  *
    99  * @param format		Formatting rules.
   100  * @param ...			Variable number of format parameters.
   101  * @noreturn
   102  */
   103 native InsertServerCommand(const String:format[], any:...);
   104 
   105 /**
   106  * Executes every command in the server's command buffer, rather than once per frame.
   107  *
   108  * @noreturn
   109  */
   110 native ServerExecute();
   111 
   112 /**
   113  * Executes a client command.  Note that this will not work on clients unless
   114  * they have cl_restrict_server_commands set to 0.
   115  *
   116  * @param client		Index of the client.
   117  * @param fmt			Format of the client command.
   118  * @param ...			Format parameters/
   119  * @noreturn
   120  * @error				Invalid client index, or client not connected.
   121  */
   122 native ClientCommand(client, const String:fmt[], any:...);
   123 
   124 /**
   125  * Sends a message to the server console.
   126  *
   127  * @param format		Formatting rules.
   128  * @param ...			Variable number of format parameters.
   129  * @noreturn
   130  */
   131 native PrintToServer(const String:format[], any:...);
   132 
   133 /**
   134  * Sends a message to a client's console.
   135  *
   136  * @param client		Player index.
   137  * @param format		Formatting rules.
   138  * @param ...			Variable number of format parameters.
   139  * @noreturn
   140  * @error			If the client is not connected an error will be thrown.
   141  */
   142 native PrintToConsole(client, const String:format[], any:...);
   143 
   144 /**
   145  * Called when a server-only command is invoked.  
   146  *
   147  * @params args			Number of arguments that were in the argument string.
   148  * @return				An Action value.  Not handling the command
   149  *						means that Source will report it as "not found."
   150  */
   151 functag SrvCmd Action:public(args);
   152 
   153 /**
   154  * Creates a server-only console command, or hooks an already existing one.  
   155  *
   156  * @param cmd			Name of the command to hook or create.
   157  * @param callback		A function to use as a callback for when the command is invoked.
   158  * @param description	Optional description to use for command creation.
   159  * @param flags			Optional flags to use for command creation.
   160  * @noreturn
   161  * @error				Command name is the same as an existing convar.
   162  */
   163 native RegServerCmd(const String:cmd[], SrvCmd:callback, const String:description[]="", flags=0);
   164 
   165 /**
   166  * Called when a generic console command is invoked.
   167  *
   168  * @param client		Index of the client, or 0 from the server.
   169  * @param args			Number of arguments that were in the argument string.
   170  * @return				An Action value.  Not handling the command
   171  *						means that Source will report it as "not found."
   172  */
   173 functag ConCmd Action:public(client, args);
   174 
   175 /**
   176  * Creates a console command, or hooks an already existing one.
   177  *
   178  * @param cmd			Name of the command to hook or create.
   179  * @param callback		A function to use as a callback for when the command is invoked.
   180  * @param description	Optional description to use for command creation.
   181  * @param flags			Optional flags to use for command creation.
   182  * @noreturn
   183  * @error				Command name is the same as an existing convar.
   184  */
   185 native RegConsoleCmd(const String:cmd[], ConCmd:callback, const String:description[]="", flags=0);
   186 
   187 /**
   188  * Creates a console command as an administrative command.  If the command does not exist,
   189  * it is created.  When this command is invoked, the access rights of the player are 
   190  * automatically checked before allowing it to continue.
   191  *
   192  * @param cmd			String containing command to register.
   193  * @param callback		A function to use as a callback for when the command is invoked.
   194  * @param adminflags	Administrative flags (bitstring) to use for permissions.
   195  * @param group			String containing the command group to use.  If empty,
   196  * 						the plugin's filename will be used instead.
   197  * @param description	Optional description to use for help.
   198  * @param flags			Optional console flags.
   199  * @noreturn
   200  * @error				Command name is the same as an existing convar.
   201  */
   202 native RegAdminCmd(const String:cmd[],
   203 					ConCmd:callback,
   204 					adminflags,
   205 					const String:description[]="",
   206 					const String:group[]="",
   207 					flags=0);
   208 					
   209 /**
   210  * Returns the number of arguments from the current console or server command.
   211  * @note Unlike the HL2 engine call, this does not include the command itself.
   212  *
   213  * @return				Number of arguments to the current command.
   214  */
   215 native GetCmdArgs();
   216 
   217 /**
   218  * Retrieves a command argument given its index, from the current console or 
   219  * server command.
   220  * @note Argument indexes start at 1; 0 retrieves the command name.
   221  *
   222  * @param argnum		Argument number to retrieve.
   223  * @param buffer		Buffer to use for storing the string.
   224  * @param maxlength		Maximum length of the buffer.
   225  * @noreturn
   226  */
   227 native GetCmdArg(argnum, String:buffer[], maxlength);
   228 
   229 /**
   230  * Retrieves the entire command argument string in one lump from the current 
   231  * console or server command.
   232  *
   233  * @param buffer		Buffer to use for storing the string.
   234  * @param maxlength		Maximum length of the buffer.
   235  * @noreturn
   236  */
   237 native GetCmdArgString(String:buffer[], maxlength);
   238 
   239 /**
   240  * Creates a new console variable.
   241  *
   242  * @param name			Name of new convar.
   243  * @param defaultValue	String containing the default value of new convar.
   244  * @param description	Optional description of the convar.
   245  * @param flags			Optional bitstring of flags determining how the convar should be handled. See FCVAR_* constants for more details.
   246  * @param hasMin		Optional boolean that determines if the convar has a minimum value.
   247  * @param min			Minimum floating point value that the convar can have if hasMin is true.
   248  * @param hasMax		Optional boolean that determines if the convar has a maximum value.
   249  * @param max			Maximum floating point value that the convar can have if hasMax is true.
   250  * @return				A handle to the newly created convar. If the convar already exists, INVALID_HANDLE is returned.
   251  * @error				Convar name is blank or is the same as an existing console command.
   252  */
   253 native Handle:CreateConVar(const String:name[], const String:defaultValue[], const String:description[]="", flags=0, bool:hasMin=false, Float:min=0.0, bool:hasMax=false, Float:max=0.0);
   254 
   255 /**
   256  * Searches for a console variable.
   257  *
   258  * @param name			Name of convar to find.
   259  * @return				A handle to the convar if it is found. INVALID_HANDLE otherwise.
   260  */
   261 native Handle:FindConVar(const String:name[]);
   262 
   263 /**
   264  * Called when a console variable's value is changed.
   265  *
   266  * @param convar		Handle to the convar that was changed.
   267  * @param oldValue		String containing the value of the convar before it was changed.
   268  * @param newValue		String containing the new value of the convar.
   269  * @noreturn
   270  */
   271 functag ConVarChanged public(Handle:convar, const String:oldValue[], const String:newValue[]);
   272 
   273 /**
   274  * Creates a hook for when a console variable's value is changed.
   275  *
   276  * @param convar		Handle to the convar.
   277  * @param callback		An OnConVarChanged function pointer.
   278  * @noreturn
   279  * @error				Invalid or corrupt Handle or invalid callback function.
   280  */
   281 native HookConVarChange(Handle:convar, ConVarChanged:callback);
   282 
   283 /**
   284  * Removes a hook for when a console variable's value is changed.
   285  *
   286  * @param convar		Handle to the convar.
   287  * @param callback		An OnConVarChanged function pointer.
   288  * @noreturn
   289  * @error				Invalid or corrupt Handle, invalid callback function, or no active hook on convar.
   290  */
   291 native UnhookConVarChange(Handle:convar, ConVarChanged:callback);
   292 
   293 /**
   294  * Returns the boolean value of a console variable.
   295  *
   296  * @param convar		Handle to the convar.
   297  * @return				The boolean value of the convar.
   298  * @error				Invalid or corrupt Handle.
   299  */
   300 native bool:GetConVarBool(Handle:convar);
   301 
   302 /**
   303  * Sets the boolean value of a console variable.
   304  *
   305  * @param convar		Handle to the convar.
   306  * @param value			New boolean value.
   307  * @noreturn
   308  * @error				Invalid or corrupt Handle.
   309  */
   310 native SetConVarBool(Handle:convar, bool:value);
   311 
   312 /**
   313  * Returns the integer value of a console variable.
   314  *
   315  * @param convar		Handle to the convar.
   316  * @return				The integer value of the convar.
   317  * @error				Invalid or corrupt Handle.
   318  */
   319 native GetConVarInt(Handle:convar);
   320 
   321 /**
   322  * Sets the integer value of a console variable.
   323  *
   324  * @param convar		Handle to the convar.
   325  * @param value			New integer value.
   326  * @noreturn
   327  * @error				Invalid or corrupt Handle.
   328  */
   329 native SetConVarInt(Handle:convar, value);
   330 
   331 /**
   332  * Returns the floating point value of a console variable.
   333  *
   334  * @param convar		Handle to the convar.
   335  * @return				The floating point value of the convar.
   336  * @error				Invalid or corrupt Handle.
   337  */
   338 native Float:GetConVarFloat(Handle:convar);
   339 
   340 /**
   341  * Sets the floating point value of a console variable.
   342  *
   343  * @param convar		Handle to the convar.
   344  * @param value			New floating point value.
   345  * @noreturn
   346  * @error				Invalid or corrupt Handle.
   347  */
   348 native SetConVarFloat(Handle:convar, Float:value);
   349 
   350 /**
   351  * Retrieves the string value of a console variable.
   352  *
   353  * @param convar		Handle to the convar.
   354  * @param value			Buffer to store the value of the convar.
   355  * @param maxlength		Maximum length of string buffer.
   356  * @noreturn
   357  * @error				Invalid or corrupt Handle.     
   358  */
   359 native GetConVarString(Handle:convar, String:value[], maxlength);
   360 
   361 /**
   362  * Sets the string value of a console variable.
   363  *
   364  * @param convar		Handle to the convar.
   365  * @param value			New string value.
   366  * @noreturn
   367  * @error				Invalid or corrupt Handle.
   368  */
   369 native SetConVarString(Handle:convar, const String:value[]);
   370 
   371 /**
   372  * Returns the bitstring of flags on a console variable.
   373  *
   374  * @param convar		Handle to the convar.
   375  * @return				A bitstring containing the FCVAR_* flags that are enabled.
   376  * @error				Invalid or corrupt Handle.
   377  */
   378 native GetConVarFlags(Handle:convar);
   379 
   380 /**
   381  * Sets the bitstring of flags on a console variable.
   382  *
   383  * @param convar		Handle to the convar.
   384  * @param flags			A bitstring containing the FCVAR_* flags to enable.
   385  * @noreturn
   386  * @error				Invalid or corrupt Handle.
   387  */
   388 native SetConVarFlags(Handle:convar, flags);
   389 
   390 /**
   391  * Retrieves the specified bound of a console variable.
   392  *
   393  * @param convar		Handle to the convar.
   394  * @param type			Type of bound to retrieve, ConVarBound_Lower or ConVarBound_Upper.
   395  * @param value			By-reference cell to store the specified floating point bound value.
   396  * @return				True if the convar has the specified bound set, false otherwise.
   397  * @error				Invalid or corrupt Handle.
   398  */
   399 native bool:GetConVarBounds(Handle:convar, ConVarBounds:type, &Float:value);
   400 
   401 /**
   402  * Sets the specified bound of a console variable.
   403  *
   404  * @param convar		Handle to the convar.
   405  * @param type			Type of bound to set, ConVarBound_Lower or ConVarBound_Upper
   406  * @param set			If set to true, convar will use specified bound. If false, bound will be removed.
   407  * @param value			Floating point value to use as the specified bound.
   408  * @noreturn
   409  * @error				Invalid or corrupt Handle.
   410  */
   411 native SetConVarBounds(Handle:convar, ConVarBounds:type, bool:set, Float:value=0.0);
   412 
   413 /**
   414  * Retrieves the name of a console variable.
   415  *
   416  * @param convar		Handle to the convar.
   417  * @param value			Buffer to store the name of the convar.
   418  * @param maxlength		Maximum length of string buffer.
   419  * @noreturn
   420  * @error				Invalid or corrupt Handle.     
   421  */
   422 native GetConVarName(Handle:convar, const String:name[], maxlength);
   423 
   424 /**
   425  * Resets the console variable to its default value.
   426  *
   427  * @param convar		Handle to the convar.
   428  * @noreturn
   429  * @error				Invalid or corrupt Handle.
   430  */
   431 native ResetConVar(Handle:convar);
   432 
   433 funcenum ConVarQueryFinished
   434 {	
   435 	/**
   436 	 * Called when a query to retrieve a client's console variable has finished.
   437 	 *
   438 	 * @param cookie		Unique identifier of query.
   439 	 * @param client		Player index.
   440 	 * @param result		Result of query that tells one whether or not query was successful.
   441 	 *						See ConVarQueryResult enum for more details.
   442 	 * @param convarName	Name of client convar that was queried.
   443 	 * @param convarValue	Value of client convar that was queried if successful. This will be "" if it was not.
   444 	 * @param value			Value that was passed when query was started.
   445 	 * @noreturn
   446 	 */
   447 	public(QueryCookie:cookie, client, ConVarQueryResult:result, const String:cvarName[], const String:cvarValue[], any:value),
   448 	
   449 	/**
   450 	 * Called when a query to retrieve a client's console variable has finished.
   451 	 *
   452 	 * @param cookie		Unique identifier of query.
   453 	 * @param client		Player index.
   454 	 * @param result		Result of query that tells one whether or not query was successful.
   455 	 *						See ConVarQueryResult enum for more details.
   456 	 * @param convarName	Name of client convar that was queried.
   457 	 * @param convarValue	Value of client convar that was queried if successful. This will be "" if it was not.
   458 	 * @noreturn
   459 	 */
   460 	public(QueryCookie:cookie, client, ConVarQueryResult:result, const String:cvarName[], const String:cvarValue[])
   461 };
   462 
   463 /**
   464  * Starts a query to retrieve the value of a client's console variable.
   465  *
   466  * @param client		Player index.
   467  * @param name			Name of client convar to query.
   468  * @param callback		A function to use as a callback when the query has finished.
   469  * @param value			Optional value to pass to the callback function.
   470  * @return				A cookie that uniquely identifies the query. 
   471  *						Returns QUERYCOOKIE_FAILED on failure, such as when used on a bot.
   472  */
   473 native QueryCookie:QueryClientConVar(client, const String:cvarName[], ConVarQueryFinished:callback, any:value=0);