plugins/include/sourcemod.inc
author David Anderson <dvander@alliedmods.net>
Mon Oct 22 03:41:05 2007 +0000 (2007-10-22)
changeset 1629 0acae34667d9
parent 1621 ac5d4c68a99a
child 1630 2f2fe925f377
permissions -rw-r--r--
fixed filenames in comments
     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 inialize 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.
   168  */
   169 forward OnAllPluginsLoaded();
   170 
   171 /**
   172  * Called when an action is going to be logged.
   173  *
   174  * @param source		Handle to the object logging the action, or INVALID_HANDLE 
   175  *						if Core is logging the action.
   176  * @param ident			Type of object logging the action (plugin, ext, or core).
   177  * @param client		Client the action is from; 0 for server, -1 if not applicable.
   178  * @param target		Client the action is targetting, or -1 if not applicable.
   179  * @param message		Message that is being logged.
   180  * @return				Plugin_Continue will cause Core to defaulty log the message.
   181  *						Plugin_Handled will stop Core from logging the message.
   182  * 						Plugin_Stop is the same as Handled, but prevents any other 
   183  *						plugins from handling the message.
   184  */
   185 forward Action:OnLogAction(Handle:source, 
   186 						   Identity:ident,
   187 						   client,
   188 						   target,
   189 						   const String:message[]);
   190 
   191 /**
   192  * Returns the calling plugin's Handle.
   193  *
   194  * @return				Handle of the calling plugin.
   195  */
   196 native Handle:GetMyHandle();
   197 
   198 /**
   199  * Returns an iterator that can be used to search through plugins.
   200  *
   201  * @return				Handle to iterate with.  Must be closed via 
   202  * 						CloseHandle().
   203  * @error				Invalid Handle.
   204  */
   205 native Handle:GetPluginIterator();
   206 
   207 /**
   208  * Returns whether there are more plugins available in the iterator.
   209  *
   210  * @param iter			Handle to the plugin iterator.
   211  * @return				True on more plugins, false otherwise.
   212  * @error				Invalid Handle.
   213  */
   214 native bool:MorePlugins(Handle:iter);
   215 
   216 /**
   217  * Returns the current plugin in the iterator and advances the iterator.
   218  *
   219  * @param iter			Handle to the plugin iterator.
   220  * @return				Current plugin the iterator is at, before
   221  *						the iterator is advanced.
   222  * @error				Invalid Handle.
   223  */
   224 native Handle:ReadPlugin(Handle:iter);
   225 
   226 /**
   227  * Returns a plugin's status.
   228  *
   229  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   230  * @return				Status code for the plugin.
   231  * @error				Invalid Handle.
   232  */
   233 native PluginStatus:GetPluginStatus(Handle:plugin);
   234 
   235 /**
   236  * Retrieves a plugin's file name relative to the plugins folder.
   237  *
   238  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   239  * @param buffer		Buffer to the store the file name.
   240  * @param maxlength		Maximum length of the name buffer.
   241  * @noreturn
   242  * @error				Invalid Handle.
   243  */
   244 native GetPluginFilename(Handle:plugin, String:buffer[], maxlength);
   245 
   246 /**
   247  * Retrieves whether or not a plugin is being debugged.
   248  *
   249  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   250  * @return				True if being debugged, false otherwise.
   251  * @error				Invalid Handle.
   252  */
   253 native bool:IsPluginDebugging(Handle:hndl);
   254 
   255 /**
   256  * Retrieves a plugin's public info.
   257  *
   258  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   259  * @param info			Plugin info property to retrieve.
   260  * @param buffer		Buffer to store info in.
   261  * @param maxlength		Maximum length of buffer.
   262  * @return				True on success, false if property is not available.
   263  * @error				Invalid Handle.
   264  */
   265 native bool:GetPluginInfo(Handle:plugin, PluginInfo:info, String:buffer[], maxlength);
   266 
   267 /**
   268  * Causes the plugin to enter a failed state.  An error will be thrown and
   269  * the plugin will be paused until it is unloaded or reloaded.
   270  *
   271  * @param string		Message to print.
   272  * @noreturn
   273  * @error				Always throws SP_ERROR_ABORT.
   274  */
   275 native SetFailState(const String:string[]);
   276 
   277 /**
   278  * Aborts the current callback and throws an error.  This function
   279  * does not return in that no code is executed following it.
   280  *
   281  * @param format		String format.
   282  * @param ...			Format arguments.
   283  * @noreturn
   284  * @error				Always!
   285  */
   286 native ThrowError(const String:fmt[], any:...);
   287 
   288 /**
   289  * Logs a plugin message to the SourceMod logs.  The log message will be 
   290  * prefixed by the plugin's logtag (filename).
   291  *
   292  * @param format		String format.
   293  * @param ...			Format arguments.
   294  * @noreturn
   295  */
   296 native LogMessage(const String:format[], any:...);
   297 
   298 /**
   299  * Logs a message to the SourceMod logs without any plugin logtag.  This is 
   300  * useful for re-routing messages from other plugins, for example, messages 
   301  * from LogAction().
   302  *
   303  * @param format		String format.
   304  * @param ...			Format arguments.
   305  * @noreturn
   306  */
   307 native LogMessageEx(const String:format[], any:...);
   308 
   309 /**
   310  * Logs a message to any file.  The log message will be in the normal 
   311  * SourceMod format, with the plugin logtag prepended.
   312  *
   313  * @param file			File to write the log message in.
   314  * @param format		String format.
   315  * @param ...			Format arguments.
   316  * @noreturn
   317  * @error				File could not be opened/written.
   318  */
   319 native LogToFile(const String:file[], const String:format[], any:...);
   320 
   321 /**
   322  * Same as LogToFile(), except no plugin logtag is prepended.
   323  *
   324  * @param file			File to write the log message in.
   325  * @param format		String format.
   326  * @param ...			Format arguments.
   327  * @noreturn
   328  * @error				File could not be opened/written.
   329  */
   330 native LogToFileEx(const String:file[], const String:format[], any:...);
   331 
   332 /**
   333  * Logs an action from a command or event whereby interception and routing may 
   334  * be important.  This is intended to be a logging version of ShowActivity().
   335  *
   336  * @param client		Client performing the action, 0 for server, or -1 if not 
   337  *						applicable.
   338  * @param target		Client being targetted, or -1 if not applicable.
   339  * @param message		Message format.
   340  * @param ...			Message formatting parameters.
   341  * @noreturn
   342  */
   343 native LogAction(client, target, const String:message[], any:...);
   344 
   345 /**
   346  * Logs a plugin error message to the SourceMod logs.
   347  *
   348  * @param format		String format.
   349  * @param ...			Format arguments.
   350  * @noreturn
   351  */
   352 native LogError(const String:format[], any:...);
   353 
   354 /**
   355  * Gets the system time as a unix timestamp.
   356  *
   357  * @param bigStamp		Optional array to store the 64bit timestamp in.
   358  * @return				32bit timestamp (number of seconds since unix epoch).
   359  */
   360 native GetTime(bigStamp[2]={0,0});
   361 
   362 /**
   363  * Produces a date and/or time string value for a timestamp.
   364  *
   365  * See this URL for valid parameters: 
   366  * http://opengroup.org/onlinepubs/007908799/xsh/strftime.html
   367  *
   368  * @param buffer		Destination string buffer.
   369  * @param maxlength		Maximum length of output string buffer.
   370  * @param format		Formatting rules (passing NULL_STRING will use the rules defined in sm_datetime_format).
   371  * @param stamp			Optional time stamp.
   372  * @noreturn
   373  * @error			Buffer too small or invalid time format.
   374  */
   375 native FormatTime(String:buffer[], maxlength, const String:format[], stamp=-1);
   376 
   377 /**
   378  * Loads a game config file.
   379  *
   380  * @param file			File to load.  The path must be relative to the 'gamedata' folder under the config folder 
   381  *					and the extension should be omitted.
   382  * @return			A handle to the game config file or INVALID_HANDLE in failure.
   383  */
   384 native Handle:LoadGameConfigFile(const String:file[]);
   385 
   386 /**
   387  * Returns an offset value.
   388  *
   389  * @param gc			Game config handle.
   390  * @param key			Key to retrieve from the offset section.
   391  * @return			An offset, or -1 on failure.
   392  */
   393 native GameConfGetOffset(Handle:gc, const String:key[]);
   394 
   395 /**
   396  * Gets the value of a key from the "Keys" section.
   397  *
   398  * @param gc			Game config handle.
   399  * @param key			Key to retrieve from the Keys section.
   400  * @param buffer		Destination string buffer.
   401  * @param maxlen		Maximum length of output string buffer.
   402  * @return				True if key existed, false otherwise.
   403  */
   404 native bool:GameConfGetKeyValue(Handle:gc, const String:key[], String:buffer[], maxlen);
   405 
   406 /**
   407  * Returns the operating system's "tick count," which is a number of 
   408  * milliseconds since the operating system loaded.  This can be used
   409  * for basic benchmarks.
   410  *
   411  * @return				Tick count in milliseconds.
   412  */
   413 native GetSysTickCount();
   414 
   415 /**
   416  * Specifies that the given config file should be executed after plugin load.
   417  * OnConfigsExecuted() will not be called until the config file has executed, 
   418  * but it will be called if the execution fails.
   419  *
   420  * @param autoCreate	If true, and the config file does not exist, such a config
   421  *						file will be automatically created and populated with
   422  *						information from the plugin's registered cvars.
   423  * @param name			Name of the config file, excluding the .cfg extension.
   424  *						If empty, <plugin.filename.cfg> is assumed.
   425  * @param folder		Folder under cfg/ to use.  By default this is "sourcemod."
   426  * @noreturn
   427  */
   428 native AutoExecConfig(bool:autoCreate=true, const String:name[]="", const String:folder[]="sourcemod");
   429 
   430 /**
   431  * Sets a native as optional, such that if it is unloaded, removed,
   432  * or otherwise non-existent, the plugin will still work.  Calling
   433  * removed natives results in a run-time error.
   434  *
   435  * @param name			Native name.
   436  * @noreturn
   437  */
   438 native MarkNativeAsOptional(const String:name[]);
   439 
   440 /**
   441  * Registers a library name for identifying as a dependency to 
   442  * other plugins.
   443  *
   444  * @param name			Library name.
   445  * @noreturn
   446  */
   447 native RegPluginLibrary(const String:name[]);
   448 
   449 /**
   450  * Returns whether a library exists.  This function should be considered 
   451  * expensive; it should only be called on plugin to determine availability 
   452  * of resources.  Use OnLibraryAdded()/OnLibraryRemoved() to detect changes 
   453  * in optional resources.
   454  *
   455  * @param name			Library name of a plugin or extension.
   456  * @return				True if exists, false otherwise.
   457  */
   458 native bool:LibraryExists(const String:name[]);
   459 
   460 /**
   461  * Returns the status of an extension, by filename.
   462  *
   463  * @param name			Extension name (like "sdktools.ext").
   464  * @param error			Optional error message buffer.
   465  * @param maxlength		Length of optional error message buffer.
   466  * @return				-2 if the extension was not found.
   467  *						-1 if the extension was found but failed to load.
   468  * 						 0 if the extension loaded but reported an error.
   469  * 						 1 if the extension is running without error.
   470  */
   471 native GetExtensionFileStatus(const String:name[], String:error[]="", maxlength=0);
   472 
   473 /**
   474  * Called after a library is added that the current plugin references 
   475  * optionally. A library is either a plugin name or extension name, as 
   476  * exposed via its include file.
   477  *
   478  * @param name			Library name.
   479  */
   480 forward OnLibraryAdded(const String:name[]);
   481 
   482 /**
   483  * Called right before a library is removed that the current plugin references 
   484  * optionally.  A library is either a plugin name or extension name, as 
   485  * exposed via its include file.
   486  *
   487  * @param name			Library name.
   488  */
   489 forward OnLibraryRemoved(const String:name[]);
   490 
   491 #include <helpers>
   492 #include <entity>
   493