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