plugins/include/sourcemod.inc
author David Anderson <dvander@alliedmods.net>
Tue Jan 22 16:47:10 2008 +0000 (2008-01-22)
changeset 1853 b046cf080755
parent 1833 3bb2c9cb63d2
child 1869 49274af1f67c
permissions -rw-r--r--
fire requires timebomb
     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.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32 
    33 #if defined _sourcemod_included
    34  #endinput
    35 #endif
    36 #define _sourcemod_included
    37 
    38 /**
    39  * Plugin public information.
    40  */
    41 struct Plugin
    42 {
    43    const String:name[],			/**< Plugin Name */
    44    const String:description[],	/**< Plugin Description */
    45    const String:author[],		/**< Plugin Author */
    46    const String:version[],		/**< Plugin Version */
    47    const String:url[],			/**< Plugin URL */
    48 };
    49 
    50 #include <core>
    51 #include <float>
    52 #include <vector>
    53 #include <string>
    54 #include <handles>
    55 #include <functions>
    56 #include <files>
    57 #include <timers>
    58 #include <admin>
    59 #include <dbi>
    60 #include <lang>
    61 #include <sorting>
    62 #include <textparse>
    63 #include <clients>
    64 #include <console>
    65 #include <events>
    66 #include <bitbuffer>
    67 #include <usermessages>
    68 #include <keyvalues>
    69 #include <menus>
    70 #include <halflife>
    71 #include <adt>
    72 #include <banning>
    73 #include <commandfilters>
    74 
    75 /**
    76  * Declare this as a struct in your plugin to expose its information.
    77  * Example:
    78  *
    79  * public Plugin:myinfo =
    80  * {
    81  *    name = "My Plugin",
    82  *    //etc
    83  * };
    84  */
    85 public Plugin:myinfo;
    86  
    87 /**
    88  * Called when the plugin is fully initialized and all known external references are resolved.
    89  * This is called even if the plugin type is "private."
    90  * @note Errors in this function will cause the plugin to stop running.
    91  *
    92  * @noreturn
    93  */
    94 forward OnPluginStart();
    95  
    96 /**
    97  * Called before OnPluginStart, in case the plugin wants to check for load failure.
    98  * This is called even if the plugin type is "private."  Any natives from modules are 
    99  * not available at this point.  Thus, this forward should only be used for explicit 
   100  * pre-emptive things, such as adding dynamic natives, or setting certain types of load filters.
   101  * 
   102  * @note It is not safe to call externally resolved natives until OnPluginStart().
   103  * @note Any sort of RTE in this function will cause the plugin to fail loading.
   104  *
   105  * @param myself	Handle to the plugin.
   106  * @param late		Whether or not the plugin was loaded "late" (after map load).
   107  * @param error		Error message buffer in case load failed.
   108  * @param err_max	Maximum number of characters for error message buffer.
   109  * @return			True if load success, false otherwise.
   110  */
   111 forward bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max);
   112 
   113 /**
   114  * Called when the plugin is about to be unloaded.
   115  *
   116  * @noreturn
   117  */
   118 forward OnPluginEnd();
   119 
   120 /**
   121  * Called when the plugin's pause status is changing.
   122  *
   123  * @param pause			True if the plugin is being paused, false otherwise.
   124  * @noreturn
   125  */
   126 forward OnPluginPauseChange(bool:pause);
   127 
   128 /**
   129  * Called before every server frame.  Note that you should avoid
   130  * doing expensive computations here, and you should declare large
   131  * local arrays using 'decl' instead of 'new'.
   132  */
   133 forward OnGameFrame();
   134 
   135 /**
   136  * Called when the map is loaded.
   137  *
   138  * @note This used to be OnServerLoad(), which is now deprecated.
   139  * Plugins still using the old forward will work.
   140  */
   141 forward OnMapStart();
   142 
   143 /**
   144  * Called right before a map ends.
   145  */
   146 forward OnMapEnd();
   147 
   148 /**
   149  * Called when the map has loaded, servercfgfile (server.cfg) has been 
   150  * executed, and all plugin configs are done executing.  This is the best
   151  * place to initialize plugin functions which are based on cvar data.  
   152  *
   153  * @note This will always be called once and only once per map.  It will be 
   154  * called after OnMapStart().
   155  *
   156  * @noreturn
   157  */
   158 forward OnConfigsExecuted();
   159 
   160 /**
   161  * @deprecated Use OnConfigsExecuted() instead.
   162  */
   163 #pragma deprecated Use OnConfigsExecuted() instead
   164 forward OnServerCfg();
   165 
   166 /**
   167  * Called after all plugins have been loaded.  This is called once for 
   168  * every plugin.  If a plugin late loads, it will be called immediately 
   169  * after OnPluginStart().
   170  */
   171 forward OnAllPluginsLoaded();
   172 
   173 /**
   174  * Called when an action is going to be logged.
   175  *
   176  * @param source		Handle to the object logging the action, or INVALID_HANDLE 
   177  *						if Core is logging the action.
   178  * @param ident			Type of object logging the action (plugin, ext, or core).
   179  * @param client		Client the action is from; 0 for server, -1 if not applicable.
   180  * @param target		Client the action is targetting, or -1 if not applicable.
   181  * @param message		Message that is being logged.
   182  * @return				Plugin_Continue will cause Core to defaulty log the message.
   183  *						Plugin_Handled will stop Core from logging the message.
   184  * 						Plugin_Stop is the same as Handled, but prevents any other 
   185  *						plugins from handling the message.
   186  */
   187 forward Action:OnLogAction(Handle:source, 
   188 						   Identity:ident,
   189 						   client,
   190 						   target,
   191 						   const String:message[]);
   192 
   193 /**
   194  * Returns the calling plugin's Handle.
   195  *
   196  * @return				Handle of the calling plugin.
   197  */
   198 native Handle:GetMyHandle();
   199 
   200 /**
   201  * Returns an iterator that can be used to search through plugins.
   202  *
   203  * @return				Handle to iterate with.  Must be closed via 
   204  * 						CloseHandle().
   205  * @error				Invalid Handle.
   206  */
   207 native Handle:GetPluginIterator();
   208 
   209 /**
   210  * Returns whether there are more plugins available in the iterator.
   211  *
   212  * @param iter			Handle to the plugin iterator.
   213  * @return				True on more plugins, false otherwise.
   214  * @error				Invalid Handle.
   215  */
   216 native bool:MorePlugins(Handle:iter);
   217 
   218 /**
   219  * Returns the current plugin in the iterator and advances the iterator.
   220  *
   221  * @param iter			Handle to the plugin iterator.
   222  * @return				Current plugin the iterator is at, before
   223  *						the iterator is advanced.
   224  * @error				Invalid Handle.
   225  */
   226 native Handle:ReadPlugin(Handle:iter);
   227 
   228 /**
   229  * Returns a plugin's status.
   230  *
   231  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   232  * @return				Status code for the plugin.
   233  * @error				Invalid Handle.
   234  */
   235 native PluginStatus:GetPluginStatus(Handle:plugin);
   236 
   237 /**
   238  * Retrieves a plugin's file name relative to the plugins folder.
   239  *
   240  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   241  * @param buffer		Buffer to the store the file name.
   242  * @param maxlength		Maximum length of the name buffer.
   243  * @noreturn
   244  * @error				Invalid Handle.
   245  */
   246 native GetPluginFilename(Handle:plugin, String:buffer[], maxlength);
   247 
   248 /**
   249  * Retrieves whether or not a plugin is being debugged.
   250  *
   251  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   252  * @return				True if being debugged, false otherwise.
   253  * @error				Invalid Handle.
   254  */
   255 native bool:IsPluginDebugging(Handle:hndl);
   256 
   257 /**
   258  * Retrieves a plugin's public info.
   259  *
   260  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   261  * @param info			Plugin info property to retrieve.
   262  * @param buffer		Buffer to store info in.
   263  * @param maxlength		Maximum length of buffer.
   264  * @return				True on success, false if property is not available.
   265  * @error				Invalid Handle.
   266  */
   267 native bool:GetPluginInfo(Handle:plugin, PluginInfo:info, String:buffer[], maxlength);
   268 
   269 /**
   270  * Finds a plugin by its order in the list from the "plugins list" server 
   271  * "sm" command.  You should not use this function to loop through all plugins, 
   272  * use the iterator instead.  Looping through all plugins using this native 
   273  * is O(n^2), whereas using the iterator is O(n).
   274  *
   275  * @param order_num		Number of the plugin as it appears in "sm plugins list".
   276  * @return				Plugin Handle on success, INVALID_HANDLE if no plugin 
   277  *						matches the given number.
   278  */
   279 native Handle:FindPluginByNumber(order_num);
   280 
   281 /**
   282  * Causes the plugin to enter a failed state.  An error will be thrown and
   283  * the plugin will be paused until it is unloaded or reloaded.
   284  *
   285  * For backwards compatibility, if no extra arguments are passed, no 
   286  * formatting is applied.  If one or more additional arguments is passed, 
   287  * the string is formatted using Format().  If any errors are encountered 
   288  * during formatting, both the format specifier string and an additional 
   289  * error message are written.
   290  *
   291  * This function does not return, and no further code in the plugin is 
   292  * executed.
   293  *
   294  * @param string		Format specifier string.
   295  * @param ...			Formatting arguments.
   296  * @noreturn
   297  * @error				Always throws SP_ERROR_ABORT.
   298  */
   299 native SetFailState(const String:string[], any:...);
   300 
   301 /**
   302  * Aborts the current callback and throws an error.  This function
   303  * does not return in that no code is executed following it.
   304  *
   305  * @param format		String format.
   306  * @param ...			Format arguments.
   307  * @noreturn
   308  * @error				Always!
   309  */
   310 native ThrowError(const String:fmt[], any:...);
   311 
   312 /**
   313  * Logs a plugin message to the SourceMod logs.  The log message will be 
   314  * prefixed by the plugin's logtag (filename).
   315  *
   316  * @param format		String format.
   317  * @param ...			Format arguments.
   318  * @noreturn
   319  */
   320 native LogMessage(const String:format[], any:...);
   321 
   322 /**
   323  * Logs a message to the SourceMod logs without any plugin logtag.  This is 
   324  * useful for re-routing messages from other plugins, for example, messages 
   325  * from LogAction().
   326  *
   327  * @param format		String format.
   328  * @param ...			Format arguments.
   329  * @noreturn
   330  */
   331 native LogMessageEx(const String:format[], any:...);
   332 
   333 /**
   334  * Logs a message to any file.  The log message will be in the normal 
   335  * SourceMod format, with the plugin logtag prepended.
   336  *
   337  * @param file			File to write the log message in.
   338  * @param format		String format.
   339  * @param ...			Format arguments.
   340  * @noreturn
   341  * @error				File could not be opened/written.
   342  */
   343 native LogToFile(const String:file[], const String:format[], any:...);
   344 
   345 /**
   346  * Same as LogToFile(), except no plugin logtag is prepended.
   347  *
   348  * @param file			File to write the log message in.
   349  * @param format		String format.
   350  * @param ...			Format arguments.
   351  * @noreturn
   352  * @error				File could not be opened/written.
   353  */
   354 native LogToFileEx(const String:file[], const String:format[], any:...);
   355 
   356 /**
   357  * Logs an action from a command or event whereby interception and routing may 
   358  * be important.  This is intended to be a logging version of ShowActivity().
   359  *
   360  * @param client		Client performing the action, 0 for server, or -1 if not 
   361  *						applicable.
   362  * @param target		Client being targetted, or -1 if not applicable.
   363  * @param message		Message format.
   364  * @param ...			Message formatting parameters.
   365  * @noreturn
   366  */
   367 native LogAction(client, target, const String:message[], any:...);
   368 
   369 /**
   370  * Logs a plugin error message to the SourceMod logs.
   371  *
   372  * @param format		String format.
   373  * @param ...			Format arguments.
   374  * @noreturn
   375  */
   376 native LogError(const String:format[], any:...);
   377 
   378 /**
   379  * Gets the system time as a unix timestamp.
   380  *
   381  * @param bigStamp		Optional array to store the 64bit timestamp in.
   382  * @return				32bit timestamp (number of seconds since unix epoch).
   383  */
   384 native GetTime(bigStamp[2]={0,0});
   385 
   386 /**
   387  * Produces a date and/or time string value for a timestamp.
   388  *
   389  * See this URL for valid parameters: 
   390  * http://opengroup.org/onlinepubs/007908799/xsh/strftime.html
   391  *
   392  * @param buffer		Destination string buffer.
   393  * @param maxlength		Maximum length of output string buffer.
   394  * @param format		Formatting rules (passing NULL_STRING will use the rules defined in sm_datetime_format).
   395  * @param stamp			Optional time stamp.
   396  * @noreturn
   397  * @error			Buffer too small or invalid time format.
   398  */
   399 native FormatTime(String:buffer[], maxlength, const String:format[], stamp=-1);
   400 
   401 /**
   402  * Loads a game config file.
   403  *
   404  * @param file			File to load.  The path must be relative to the 'gamedata' folder under the config folder 
   405  *					and the extension should be omitted.
   406  * @return			A handle to the game config file or INVALID_HANDLE in failure.
   407  */
   408 native Handle:LoadGameConfigFile(const String:file[]);
   409 
   410 /**
   411  * Returns an offset value.
   412  *
   413  * @param gc			Game config handle.
   414  * @param key			Key to retrieve from the offset section.
   415  * @return			An offset, or -1 on failure.
   416  */
   417 native GameConfGetOffset(Handle:gc, const String:key[]);
   418 
   419 /**
   420  * Gets the value of a key from the "Keys" section.
   421  *
   422  * @param gc			Game config handle.
   423  * @param key			Key to retrieve from the Keys section.
   424  * @param buffer		Destination string buffer.
   425  * @param maxlen		Maximum length of output string buffer.
   426  * @return				True if key existed, false otherwise.
   427  */
   428 native bool:GameConfGetKeyValue(Handle:gc, const String:key[], String:buffer[], maxlen);
   429 
   430 /**
   431  * Returns the operating system's "tick count," which is a number of 
   432  * milliseconds since the operating system loaded.  This can be used
   433  * for basic benchmarks.
   434  *
   435  * @return				Tick count in milliseconds.
   436  */
   437 native GetSysTickCount();
   438 
   439 /**
   440  * Specifies that the given config file should be executed after plugin load.
   441  * OnConfigsExecuted() will not be called until the config file has executed, 
   442  * but it will be called if the execution fails.
   443  *
   444  * @param autoCreate	If true, and the config file does not exist, such a config
   445  *						file will be automatically created and populated with
   446  *						information from the plugin's registered cvars.
   447  * @param name			Name of the config file, excluding the .cfg extension.
   448  *						If empty, <plugin.filename.cfg> is assumed.
   449  * @param folder		Folder under cfg/ to use.  By default this is "sourcemod."
   450  * @noreturn
   451  */
   452 native AutoExecConfig(bool:autoCreate=true, const String:name[]="", const String:folder[]="sourcemod");
   453 
   454 /**
   455  * Sets a native as optional, such that if it is unloaded, removed,
   456  * or otherwise non-existent, the plugin will still work.  Calling
   457  * removed natives results in a run-time error.
   458  *
   459  * @param name			Native name.
   460  * @noreturn
   461  */
   462 native MarkNativeAsOptional(const String:name[]);
   463 
   464 /**
   465  * Registers a library name for identifying as a dependency to 
   466  * other plugins.
   467  *
   468  * @param name			Library name.
   469  * @noreturn
   470  */
   471 native RegPluginLibrary(const String:name[]);
   472 
   473 /**
   474  * Returns whether a library exists.  This function should be considered 
   475  * expensive; it should only be called on plugin to determine availability 
   476  * of resources.  Use OnLibraryAdded()/OnLibraryRemoved() to detect changes 
   477  * in optional resources.
   478  *
   479  * @param name			Library name of a plugin or extension.
   480  * @return				True if exists, false otherwise.
   481  */
   482 native bool:LibraryExists(const String:name[]);
   483 
   484 /**
   485  * Returns the status of an extension, by filename.
   486  *
   487  * @param name			Extension name (like "sdktools.ext").
   488  * @param error			Optional error message buffer.
   489  * @param maxlength		Length of optional error message buffer.
   490  * @return				-2 if the extension was not found.
   491  *						-1 if the extension was found but failed to load.
   492  * 						 0 if the extension loaded but reported an error.
   493  * 						 1 if the extension is running without error.
   494  */
   495 native GetExtensionFileStatus(const String:name[], String:error[]="", maxlength=0);
   496 
   497 /**
   498  * Called after a library is added that the current plugin references 
   499  * optionally. A library is either a plugin name or extension name, as 
   500  * exposed via its include file.
   501  *
   502  * @param name			Library name.
   503  */
   504 forward OnLibraryAdded(const String:name[]);
   505 
   506 /**
   507  * Called right before a library is removed that the current plugin references 
   508  * optionally.  A library is either a plugin name or extension name, as 
   509  * exposed via its include file.
   510  *
   511  * @param name			Library name.
   512  */
   513 forward OnLibraryRemoved(const String:name[]);
   514 
   515 #define MAPLIST_FLAG_MAPSFOLDER		(1<<0)		/**< On failure, use all maps in the maps folder. */
   516 #define MAPLIST_FLAG_CLEARARRAY		(1<<1)		/**< If an input array is specified, clear it before adding. */
   517 #define MAPLIST_FLAG_NO_DEFAULT		(1<<2)		/**< Do not read "default" or "mapcyclefile" on failure. */
   518 
   519 /**
   520  * Loads a map list to an ADT Array.
   521  *
   522  * A map list is a list of maps from a file.  SourceMod allows easy configuration of 
   523  * maplists through addons/sourcemod/configs/maplists.cfg.  Each entry is given a 
   524  * name and a file (for example, "rtv" => "rtv.cfg"), or a name and a redirection 
   525  * (for example, "rtv" => "default").  This native will read a map list entry, 
   526  * cache the file, and return the list of maps it holds.  
   527  *
   528  * Serial change numbers are used to identify if a map list has changed.  Thus, if 
   529  * you pass a serial change number and it's equal to what SourceMod currently knows 
   530  * about the map list, then SourceMod won't reparse the file.
   531  *
   532  * If the maps end up being read from the maps folder (MAPLIST_FLAG_MAPSFOLDER), they 
   533  * are automatically sorted in alphabetical, ascending order.
   534  *
   535  * Arrays created by this function are temporary and must be freed via CloseHandle().
   536  * Modifying arrays created by this function will not affect future return values or 
   537  * or the contents of arrays returned to other plugins.
   538  *
   539  * @param array			Array to store the map list.  If INVALID_HANDLE, a new blank 
   540  *						array will be created.  The blocksize should be at least 16; 
   541  *						otherwise results may be truncated.  Items are added to the array 
   542  *						as strings.  The array is never checked for duplicates, and it is 
   543  *						not read beforehand.  Only the serial number is used to detect 
   544  *						changes.
   545  * @param serial		Serial number to identify last known map list change.  If -1, the 
   546  *						the value will not be checked.  If the map list has since changed, 
   547  *						the serial is updated (even if -1 was passed).  If there is an error 
   548  *						finding a valid maplist, then the serial is set to -1.
   549  * @param str			Config name, or "default" for the default map list.  Config names 
   550  *						should be somewhat descriptive.  For example, the admin menu uses 
   551  *						a config name of "admin menu".  The list names can be configured 
   552  *						by users in addons/sourcemod/configs/maplists.cfg.
   553  * @param flags			MAPLIST_FLAG flags.
   554  * @return				On failure:
   555  *						 INVALID_HANDLE is returned, the serial is set to -1, and the input 
   556  *						 array (if any) is left unchanged.
   557  *						On no change:
   558  						 INVALID_HANDLE is returned, the serial is unchanged, and the input 
   559  						 array (if any) is left unchanged.
   560  *						On success:
   561  *						 A valid array Handle is returned, containing at least one map string.  
   562  *						 If an array was passed, the return value is equal to the passed Array 
   563  *						 Handle.  If the passed array was not cleared, it will have grown by at 
   564  *						 least one item.  The serial number is updated to a positive number.
   565  * @error				Invalid array Handle that is not INVALID_HANDLE.
   566  */
   567 native Handle:ReadMapList(Handle:array=INVALID_HANDLE,
   568 						  &serial=-1,
   569 						  const String:str[]="default",
   570 						  flags=MAPLIST_FLAG_CLEARARRAY);
   571 						  
   572 /**
   573  * Makes a compatibility binding for map lists.  For example, if a function previously used 
   574  * "clam.cfg" for map lists, this function will insert a "fake" binding to "clam.cfg" that 
   575  * will be overridden if it's in the maplists.cfg file.
   576  *
   577  * @param name			Configuration name that would be used with ReadMapList().
   578  * @param file			Default file to use.
   579  * @noreturn
   580  */
   581 native SetMapListCompatBind(const String:name[], const String:file[]);
   582 
   583 #include <helpers>
   584 #include <entity>
   585 #include <entity_prop_stocks>
   586