plugins/include/sourcemod.inc
author David Anderson <dvander@alliedmods.net>
Sat May 24 06:48:35 2008 +0000 (2008-05-24)
changeset 2072 82d3186b9fa4
parent 2065 e2e5445a2f9b
child 2077 3c6fe374e8b7
permissions -rw-r--r--
added quick tool for fetching dlls of mdmps
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 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 <logging>
    58 #include <timers>
    59 #include <admin>
    60 #include <dbi>
    61 #include <lang>
    62 #include <sorting>
    63 #include <textparse>
    64 #include <clients>
    65 #include <console>
    66 #include <events>
    67 #include <bitbuffer>
    68 #include <usermessages>
    69 #include <keyvalues>
    70 #include <menus>
    71 #include <halflife>
    72 #include <adt>
    73 #include <banning>
    74 #include <commandfilters>
    75 
    76 /**
    77  * Declare this as a struct in your plugin to expose its information.
    78  * Example:
    79  *
    80  * public Plugin:myinfo =
    81  * {
    82  *    name = "My Plugin",
    83  *    //etc
    84  * };
    85  */
    86 public Plugin:myinfo;
    87  
    88 /**
    89  * Called when the plugin is fully initialized and all known external references 
    90  * are resolved. This is only called once in the lifetime of the plugin, and is 
    91  * paired with OnPluginEnd().
    92  *
    93  * If any run-time error is thrown during this callback, the plugin will be marked 
    94  * as failed.
    95  *
    96  * It is not necessary to close any handles or remove hooks in this function.  
    97  * SourceMod guarantees that plugin shutdown automatically and correctly releases 
    98  * all resources.
    99  *
   100  * @noreturn
   101  */
   102 forward OnPluginStart();
   103  
   104 /**
   105  * Called before OnPluginStart, in case the plugin wants to check for load failure.
   106  * This is called even if the plugin type is "private."  Any natives from modules are 
   107  * not available at this point.  Thus, this forward should only be used for explicit 
   108  * pre-emptive things, such as adding dynamic natives, or setting certain types of load filters.
   109  * 
   110  * @note It is not safe to call externally resolved natives until OnPluginStart().
   111  * @note Any sort of RTE in this function will cause the plugin to fail loading.
   112  * @note You MUST remember to return SOMETHING here.  The act of not returning is 
   113  *       equivalent to returning 0 (false).  If you forgot to return, it will 
   114  *       cause your plugin to not load with a very strange error message.
   115  *
   116  * @param myself	Handle to the plugin.
   117  * @param late		Whether or not the plugin was loaded "late" (after map load).
   118  * @param error		Error message buffer in case load failed.
   119  * @param err_max	Maximum number of characters for error message buffer.
   120  * @return			True if load success, false otherwise.
   121  */
   122 forward bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max);
   123 
   124 /**
   125  * Called when the plugin is about to be unloaded.
   126  *
   127  * @noreturn
   128  */
   129 forward OnPluginEnd();
   130 
   131 /**
   132  * Called when the plugin's pause status is changing.
   133  *
   134  * @param pause			True if the plugin is being paused, false otherwise.
   135  * @noreturn
   136  */
   137 forward OnPluginPauseChange(bool:pause);
   138 
   139 /**
   140  * Called before every server frame.  Note that you should avoid
   141  * doing expensive computations here, and you should declare large
   142  * local arrays using 'decl' instead of 'new'.
   143  */
   144 forward OnGameFrame();
   145 
   146 /**
   147  * Called when the map is loaded.
   148  *
   149  * @note This used to be OnServerLoad(), which is now deprecated.
   150  * Plugins still using the old forward will work.
   151  */
   152 forward OnMapStart();
   153 
   154 /**
   155  * Called right before a map ends.
   156  */
   157 forward OnMapEnd();
   158 
   159 /**
   160  * Called when the map has loaded, servercfgfile (server.cfg) has been 
   161  * executed, and all plugin configs are done executing.  This is the best
   162  * place to initialize plugin functions which are based on cvar data.  
   163  *
   164  * @note This will always be called once and only once per map.  It will be 
   165  * called after OnMapStart().
   166  *
   167  * @noreturn
   168  */
   169 forward OnConfigsExecuted();
   170 
   171 /**
   172  * This is called once, right after OnMapStart() but any time before 
   173  * OnConfigsExecuted().  It is called after the "exec sourcemod.cfg" 
   174  * command and all AutoExecConfig() exec commands have been added to 
   175  * the ServerCommand() buffer.
   176  *
   177  * If you need to load per-map settings that override default values, 
   178  * adding commands to the ServerCommand() buffer here will guarantee 
   179  * that they're set before OnConfigsExecuted().
   180  *
   181  * Unlike OnMapStart() and OnConfigsExecuted(), this is not called on 
   182  * late loads that occur after OnMapStart().
   183  *
   184  * @noreturn
   185  */
   186 forward OnAutoConfigsBuffered();
   187 
   188 /**
   189  * @deprecated Use OnConfigsExecuted() instead.
   190  */
   191 #pragma deprecated Use OnConfigsExecuted() instead
   192 forward OnServerCfg();
   193 
   194 /**
   195  * Called after all plugins have been loaded.  This is called once for 
   196  * every plugin.  If a plugin late loads, it will be called immediately 
   197  * after OnPluginStart().
   198  */
   199 forward OnAllPluginsLoaded();
   200 
   201 /**
   202  * Returns the calling plugin's Handle.
   203  *
   204  * @return				Handle of the calling plugin.
   205  */
   206 native Handle:GetMyHandle();
   207 
   208 /**
   209  * Returns an iterator that can be used to search through plugins.
   210  *
   211  * @return				Handle to iterate with.  Must be closed via 
   212  * 						CloseHandle().
   213  * @error				Invalid Handle.
   214  */
   215 native Handle:GetPluginIterator();
   216 
   217 /**
   218  * Returns whether there are more plugins available in the iterator.
   219  *
   220  * @param iter			Handle to the plugin iterator.
   221  * @return				True on more plugins, false otherwise.
   222  * @error				Invalid Handle.
   223  */
   224 native bool:MorePlugins(Handle:iter);
   225 
   226 /**
   227  * Returns the current plugin in the iterator and advances the iterator.
   228  *
   229  * @param iter			Handle to the plugin iterator.
   230  * @return				Current plugin the iterator is at, before
   231  *						the iterator is advanced.
   232  * @error				Invalid Handle.
   233  */
   234 native Handle:ReadPlugin(Handle:iter);
   235 
   236 /**
   237  * Returns a plugin's status.
   238  *
   239  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   240  * @return				Status code for the plugin.
   241  * @error				Invalid Handle.
   242  */
   243 native PluginStatus:GetPluginStatus(Handle:plugin);
   244 
   245 /**
   246  * Retrieves a plugin's file name relative to the plugins folder.
   247  *
   248  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   249  * @param buffer		Buffer to the store the file name.
   250  * @param maxlength		Maximum length of the name buffer.
   251  * @noreturn
   252  * @error				Invalid Handle.
   253  */
   254 native GetPluginFilename(Handle:plugin, String:buffer[], maxlength);
   255 
   256 /**
   257  * Retrieves whether or not a plugin is being debugged.
   258  *
   259  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   260  * @return				True if being debugged, false otherwise.
   261  * @error				Invalid Handle.
   262  */
   263 native bool:IsPluginDebugging(Handle:hndl);
   264 
   265 /**
   266  * Retrieves a plugin's public info.
   267  *
   268  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   269  * @param info			Plugin info property to retrieve.
   270  * @param buffer		Buffer to store info in.
   271  * @param maxlength		Maximum length of buffer.
   272  * @return				True on success, false if property is not available.
   273  * @error				Invalid Handle.
   274  */
   275 native bool:GetPluginInfo(Handle:plugin, PluginInfo:info, String:buffer[], maxlength);
   276 
   277 /**
   278  * Finds a plugin by its order in the list from the "plugins list" server 
   279  * "sm" command.  You should not use this function to loop through all plugins, 
   280  * use the iterator instead.  Looping through all plugins using this native 
   281  * is O(n^2), whereas using the iterator is O(n).
   282  *
   283  * @param order_num		Number of the plugin as it appears in "sm plugins list".
   284  * @return				Plugin Handle on success, INVALID_HANDLE if no plugin 
   285  *						matches the given number.
   286  */
   287 native Handle:FindPluginByNumber(order_num);
   288 
   289 /**
   290  * Causes the plugin to enter a failed state.  An error will be thrown and
   291  * the plugin will be paused until it is unloaded or reloaded.
   292  *
   293  * For backwards compatibility, if no extra arguments are passed, no 
   294  * formatting is applied.  If one or more additional arguments is passed, 
   295  * the string is formatted using Format().  If any errors are encountered 
   296  * during formatting, both the format specifier string and an additional 
   297  * error message are written.
   298  *
   299  * This function does not return, and no further code in the plugin is 
   300  * executed.
   301  *
   302  * @param string		Format specifier string.
   303  * @param ...			Formatting arguments.
   304  * @noreturn
   305  * @error				Always throws SP_ERROR_ABORT.
   306  */
   307 native SetFailState(const String:string[], any:...);
   308 
   309 /**
   310  * Aborts the current callback and throws an error.  This function
   311  * does not return in that no code is executed following it.
   312  *
   313  * @param format		String format.
   314  * @param ...			Format arguments.
   315  * @noreturn
   316  * @error				Always!
   317  */
   318 native ThrowError(const String:fmt[], any:...);
   319 
   320 /**
   321  * Gets the system time as a unix timestamp.
   322  *
   323  * @param bigStamp		Optional array to store the 64bit timestamp in.
   324  * @return				32bit timestamp (number of seconds since unix epoch).
   325  */
   326 native GetTime(bigStamp[2]={0,0});
   327 
   328 /**
   329  * Produces a date and/or time string value for a timestamp.
   330  *
   331  * See this URL for valid parameters: 
   332  * http://cplusplus.com/reference/clibrary/ctime/strftime.html
   333  *
   334  * @param buffer		Destination string buffer.
   335  * @param maxlength		Maximum length of output string buffer.
   336  * @param format		Formatting rules (passing NULL_STRING will use the rules defined in sm_datetime_format).
   337  * @param stamp			Optional time stamp.
   338  * @noreturn
   339  * @error			Buffer too small or invalid time format.
   340  */
   341 native FormatTime(String:buffer[], maxlength, const String:format[], stamp=-1);
   342 
   343 /**
   344  * Loads a game config file.
   345  *
   346  * @param file			File to load.  The path must be relative to the 'gamedata' folder under the config folder 
   347  *					and the extension should be omitted.
   348  * @return			A handle to the game config file or INVALID_HANDLE in failure.
   349  */
   350 native Handle:LoadGameConfigFile(const String:file[]);
   351 
   352 /**
   353  * Returns an offset value.
   354  *
   355  * @param gc			Game config handle.
   356  * @param key			Key to retrieve from the offset section.
   357  * @return			An offset, or -1 on failure.
   358  */
   359 native GameConfGetOffset(Handle:gc, const String:key[]);
   360 
   361 /**
   362  * Gets the value of a key from the "Keys" section.
   363  *
   364  * @param gc			Game config handle.
   365  * @param key			Key to retrieve from the Keys section.
   366  * @param buffer		Destination string buffer.
   367  * @param maxlen		Maximum length of output string buffer.
   368  * @return				True if key existed, false otherwise.
   369  */
   370 native bool:GameConfGetKeyValue(Handle:gc, const String:key[], String:buffer[], maxlen);
   371 
   372 /**
   373  * Returns the operating system's "tick count," which is a number of 
   374  * milliseconds since the operating system loaded.  This can be used
   375  * for basic benchmarks.
   376  *
   377  * @return				Tick count in milliseconds.
   378  */
   379 native GetSysTickCount();
   380 
   381 /**
   382  * Specifies that the given config file should be executed after plugin load.
   383  * OnConfigsExecuted() will not be called until the config file has executed, 
   384  * but it will be called if the execution fails.
   385  *
   386  * @param autoCreate	If true, and the config file does not exist, such a config
   387  *						file will be automatically created and populated with
   388  *						information from the plugin's registered cvars.
   389  * @param name			Name of the config file, excluding the .cfg extension.
   390  *						If empty, <plugin.filename.cfg> is assumed.
   391  * @param folder		Folder under cfg/ to use.  By default this is "sourcemod."
   392  * @noreturn
   393  */
   394 native AutoExecConfig(bool:autoCreate=true, const String:name[]="", const String:folder[]="sourcemod");
   395 
   396 /**
   397  * Sets a native as optional, such that if it is unloaded, removed,
   398  * or otherwise non-existent, the plugin will still work.  Calling
   399  * removed natives results in a run-time error.
   400  *
   401  * @param name			Native name.
   402  * @noreturn
   403  */
   404 native MarkNativeAsOptional(const String:name[]);
   405 
   406 /**
   407  * Registers a library name for identifying as a dependency to 
   408  * other plugins.
   409  *
   410  * @param name			Library name.
   411  * @noreturn
   412  */
   413 native RegPluginLibrary(const String:name[]);
   414 
   415 /**
   416  * Returns whether a library exists.  This function should be considered 
   417  * expensive; it should only be called on plugin to determine availability 
   418  * of resources.  Use OnLibraryAdded()/OnLibraryRemoved() to detect changes 
   419  * in optional resources.
   420  *
   421  * @param name			Library name of a plugin or extension.
   422  * @return				True if exists, false otherwise.
   423  */
   424 native bool:LibraryExists(const String:name[]);
   425 
   426 /**
   427  * Returns the status of an extension, by filename.
   428  *
   429  * @param name			Extension name (like "sdktools.ext").
   430  * @param error			Optional error message buffer.
   431  * @param maxlength		Length of optional error message buffer.
   432  * @return				-2 if the extension was not found.
   433  *						-1 if the extension was found but failed to load.
   434  * 						 0 if the extension loaded but reported an error.
   435  * 						 1 if the extension is running without error.
   436  */
   437 native GetExtensionFileStatus(const String:name[], String:error[]="", maxlength=0);
   438 
   439 /**
   440  * Called after a library is added that the current plugin references 
   441  * optionally. A library is either a plugin name or extension name, as 
   442  * exposed via its include file.
   443  *
   444  * @param name			Library name.
   445  */
   446 forward OnLibraryAdded(const String:name[]);
   447 
   448 /**
   449  * Called right before a library is removed that the current plugin references 
   450  * optionally.  A library is either a plugin name or extension name, as 
   451  * exposed via its include file.
   452  *
   453  * @param name			Library name.
   454  */
   455 forward OnLibraryRemoved(const String:name[]);
   456 
   457 #define MAPLIST_FLAG_MAPSFOLDER		(1<<0)		/**< On failure, use all maps in the maps folder. */
   458 #define MAPLIST_FLAG_CLEARARRAY		(1<<1)		/**< If an input array is specified, clear it before adding. */
   459 #define MAPLIST_FLAG_NO_DEFAULT		(1<<2)		/**< Do not read "default" or "mapcyclefile" on failure. */
   460 
   461 /**
   462  * Loads a map list to an ADT Array.
   463  *
   464  * A map list is a list of maps from a file.  SourceMod allows easy configuration of 
   465  * maplists through addons/sourcemod/configs/maplists.cfg.  Each entry is given a 
   466  * name and a file (for example, "rtv" => "rtv.cfg"), or a name and a redirection 
   467  * (for example, "rtv" => "default").  This native will read a map list entry, 
   468  * cache the file, and return the list of maps it holds.  
   469  *
   470  * Serial change numbers are used to identify if a map list has changed.  Thus, if 
   471  * you pass a serial change number and it's equal to what SourceMod currently knows 
   472  * about the map list, then SourceMod won't reparse the file.
   473  *
   474  * If the maps end up being read from the maps folder (MAPLIST_FLAG_MAPSFOLDER), they 
   475  * are automatically sorted in alphabetical, ascending order.
   476  *
   477  * Arrays created by this function are temporary and must be freed via CloseHandle().
   478  * Modifying arrays created by this function will not affect future return values or 
   479  * or the contents of arrays returned to other plugins.
   480  *
   481  * @param array			Array to store the map list.  If INVALID_HANDLE, a new blank 
   482  *						array will be created.  The blocksize should be at least 16; 
   483  *						otherwise results may be truncated.  Items are added to the array 
   484  *						as strings.  The array is never checked for duplicates, and it is 
   485  *						not read beforehand.  Only the serial number is used to detect 
   486  *						changes.
   487  * @param serial		Serial number to identify last known map list change.  If -1, the 
   488  *						the value will not be checked.  If the map list has since changed, 
   489  *						the serial is updated (even if -1 was passed).  If there is an error 
   490  *						finding a valid maplist, then the serial is set to -1.
   491  * @param str			Config name, or "default" for the default map list.  Config names 
   492  *						should be somewhat descriptive.  For example, the admin menu uses 
   493  *						a config name of "admin menu".  The list names can be configured 
   494  *						by users in addons/sourcemod/configs/maplists.cfg.
   495  * @param flags			MAPLIST_FLAG flags.
   496  * @return				On failure:
   497  *						 INVALID_HANDLE is returned, the serial is set to -1, and the input 
   498  *						 array (if any) is left unchanged.
   499  *						On no change:
   500  						 INVALID_HANDLE is returned, the serial is unchanged, and the input 
   501  						 array (if any) is left unchanged.
   502  *						On success:
   503  *						 A valid array Handle is returned, containing at least one map string.  
   504  *						 If an array was passed, the return value is equal to the passed Array 
   505  *						 Handle.  If the passed array was not cleared, it will have grown by at 
   506  *						 least one item.  The serial number is updated to a positive number.
   507  * @error				Invalid array Handle that is not INVALID_HANDLE.
   508  */
   509 native Handle:ReadMapList(Handle:array=INVALID_HANDLE,
   510 						  &serial=-1,
   511 						  const String:str[]="default",
   512 						  flags=MAPLIST_FLAG_CLEARARRAY);
   513 						  
   514 /**
   515  * Makes a compatibility binding for map lists.  For example, if a function previously used 
   516  * "clam.cfg" for map lists, this function will insert a "fake" binding to "clam.cfg" that 
   517  * will be overridden if it's in the maplists.cfg file.
   518  *
   519  * @param name			Configuration name that would be used with ReadMapList().
   520  * @param file			Default file to use.
   521  * @noreturn
   522  */
   523 native SetMapListCompatBind(const String:name[], const String:file[]);
   524 
   525 /**
   526  * Called when a client has sent chat text.  This must return either true or 
   527  * false to indicate that a client is or is not spamming the server.
   528  *
   529  * The return value is a hint only.  Core or another plugin may decide 
   530  * otherwise.
   531  *
   532  * @param client		Client index.  The server (0) will never be passed.
   533  * @return				True if client is spamming the server, false otherwise.
   534  */
   535 forward bool:OnClientFloodCheck(client);
   536 
   537 /**
   538  * Called after a client's flood check has been computed.  This can be used 
   539  * by antiflood algorithms to decay/increase flooding weights.
   540  *
   541  * Since the result from "OnClientFloodCheck" isn't guaranteed to be the 
   542  * final result, it is generally a good idea to use this to play with other 
   543  * algorithms nicely.
   544  *
   545  * @param client		Client index.  The server (0) will never be passed.
   546  * @param blocked		True if client flooded last "say", false otherwise.
   547  * @noreturn
   548  */
   549 forward OnClientFloodResult(client, bool:blocked);
   550 
   551 #include <helpers>
   552 #include <entity>
   553 #include <entity_prop_stocks>
   554