plugins/include/sourcemod.inc
author Borja Ferrer <faluco@alliedmods.net>
Mon Oct 01 21:20:30 2007 +0000 (2007-10-01)
changeset 1515 3e112467aacd
parent 1423 e73b3563f254
child 1572 adbc41dc9cd0
permissions -rw-r--r--
fixed amb991, dispatchkeyvalue natives had wrong offsets
     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 
    74 /**
    75  * Declare this as a struct in your plugin to expose its information.
    76  * Example:
    77  *
    78  * public Plugin:myinfo =
    79  * {
    80  *    name = "My Plugin",
    81  *    //etc
    82  * };
    83  */
    84 public Plugin:myinfo;
    85  
    86 /**
    87  * Called when the plugin is fully initialized and all known external references are resolved.
    88  * This is called even if the plugin type is "private."
    89  * @note Errors in this function will cause the plugin to stop running.
    90  *
    91  * @noreturn
    92  */
    93 forward OnPluginStart();
    94  
    95 /**
    96  * Called before OnPluginStart, in case the plugin wants to check for load failure.
    97  * This is called even if the plugin type is "private."  Any natives from modules are 
    98  * not available at this point.  Thus, this forward should only be used for explicit 
    99  * pre-emptive things, such as adding dynamic natives, or setting certain types of load filters.
   100  * 
   101  * @note It is not safe to call externally resolved natives until OnPluginStart().
   102  * @note Any sort of RTE in this function will cause the plugin to fail loading.
   103  *
   104  * @param myself	Handle to the plugin.
   105  * @param late		Whether or not the plugin was loaded "late" (after map load).
   106  * @param error		Error message buffer in case load failed.
   107  * @param err_max	Maximum number of characters for error message buffer.
   108  * @return			True if load success, false otherwise.
   109  */
   110 forward bool:AskPluginLoad(Handle:myself, bool:late, String:error[], err_max);
   111 
   112 /**
   113  * Called when the plugin is about to be unloaded.
   114  *
   115  * @noreturn
   116  */
   117 forward OnPluginEnd();
   118 
   119 /**
   120  * Called when the plugin's pause status is changing.
   121  *
   122  * @param pause			True if the plugin is being paused, false otherwise.
   123  * @noreturn
   124  */
   125 forward OnPluginPauseChange(bool:pause);
   126 
   127 /**
   128  * Called before every server frame.  Note that you should avoid
   129  * doing expensive computations here, and you should declare large
   130  * local arrays using 'decl' instead of 'new'.
   131  */
   132 forward OnGameFrame();
   133 
   134 /**
   135  * Called when the map is loaded.
   136  *
   137  * @note This used to be OnServerLoad(), which is now deprecated.
   138  * Plugins still using the old forward will work.
   139  */
   140 forward OnMapStart();
   141 
   142 /**
   143  * Called right before a map ends.
   144  */
   145 forward OnMapEnd();
   146 
   147 /**
   148  * Called when the map has loaded, servercfgfile (server.cfg) has been 
   149  * executed, and all plugin configs are done executing.  This is the best
   150  * place to inialize plugin functions which are based on cvar data.  
   151  *
   152  * @note This will always be called once and only once per map.  It will be 
   153  * called after OnMapStart().
   154  *
   155  * @noreturn
   156  */
   157 forward OnConfigsExecuted();
   158 
   159 /**
   160  * @deprecated Use OnConfigsExecuted() instead.
   161  */
   162 #pragma deprecated Use OnConfigsExecuted() instead
   163 forward OnServerCfg();
   164 
   165 /**
   166  * Called after all plugins have been loaded.
   167  */
   168 forward OnAllPluginsLoaded();
   169 
   170 /**
   171  * Called when an action is going to be logged.
   172  *
   173  * @param source		Handle to the object logging the action, or INVALID_HANDLE 
   174  *						if Core is logging the action.
   175  * @param ident			Type of object logging the action (plugin, ext, or core).
   176  * @param client		Client the action is from; 0 for server, -1 if not applicable.
   177  * @param target		Client the action is targetting, or -1 if not applicable.
   178  * @param message		Message that is being logged.
   179  * @return				Plugin_Continue will cause Core to defaulty log the message.
   180  *						Plugin_Handled will stop Core from logging the message.
   181  * 						Plugin_Stop is the same as Handled, but prevents any other 
   182  *						plugins from handling the message.
   183  */
   184 forward Action:OnLogAction(Handle:source, 
   185 						   Identity:ident,
   186 						   client,
   187 						   target,
   188 						   const String:message[]);
   189 
   190 /**
   191  * Returns the calling plugin's Handle.
   192  *
   193  * @return				Handle of the calling plugin.
   194  */
   195 native Handle:GetMyHandle();
   196 
   197 /**
   198  * Returns an iterator that can be used to search through plugins.
   199  *
   200  * @return				Handle to iterate with.  Must be closed via 
   201  * 						CloseHandle().
   202  * @error				Invalid Handle.
   203  */
   204 native Handle:GetPluginIterator();
   205 
   206 /**
   207  * Returns whether there are more plugins available in the iterator.
   208  *
   209  * @param iter			Handle to the plugin iterator.
   210  * @return				True on more plugins, false otherwise.
   211  * @error				Invalid Handle.
   212  */
   213 native bool:MorePlugins(Handle:iter);
   214 
   215 /**
   216  * Returns the current plugin in the iterator and advances the iterator.
   217  *
   218  * @param iter			Handle to the plugin iterator.
   219  * @return				Current plugin the iterator is at, before
   220  *						the iterator is advanced.
   221  * @error				Invalid Handle.
   222  */
   223 native Handle:ReadPlugin(Handle:iter);
   224 
   225 /**
   226  * Returns a plugin's status.
   227  *
   228  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   229  * @return				Status code for the plugin.
   230  * @error				Invalid Handle.
   231  */
   232 native PluginStatus:GetPluginStatus(Handle:plugin);
   233 
   234 /**
   235  * Retrieves a plugin's file name relative to the plugins folder.
   236  *
   237  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   238  * @param buffer		Buffer to the store the file name.
   239  * @param maxlength		Maximum length of the name buffer.
   240  * @noreturn
   241  * @error				Invalid Handle.
   242  */
   243 native GetPluginFilename(Handle:plugin, String:buffer[], maxlength);
   244 
   245 /**
   246  * Retrieves whether or not a plugin is being debugged.
   247  *
   248  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   249  * @return				True if being debugged, false otherwise.
   250  * @error				Invalid Handle.
   251  */
   252 native bool:IsPluginDebugging(Handle:hndl);
   253 
   254 /**
   255  * Retrieves a plugin's public info.
   256  *
   257  * @param plugin		Plugin Handle (INVALID_HANDLE uses the calling plugin).
   258  * @param info			Plugin info property to retrieve.
   259  * @param buffer		Buffer to store info in.
   260  * @param maxlength		Maximum length of buffer.
   261  * @return				True on success, false if property is not available.
   262  * @error				Invalid Handle.
   263  */
   264 native bool:GetPluginInfo(Handle:plugin, PluginInfo:info, String:buffer[], maxlength);
   265 
   266 /**
   267  * Causes the plugin to enter a failed state.  An error will be thrown and
   268  * the plugin will be paused until it is unloaded or reloaded.
   269  *
   270  * @param string		Message to print.
   271  * @noreturn
   272  * @error				Always throws SP_ERROR_ABORT.
   273  */
   274 native SetFailState(const String:string[]);
   275 
   276 /**
   277  * Aborts the current callback and throws an error.  This function
   278  * does not return in that no code is executed following it.
   279  *
   280  * @param format		String format.
   281  * @param ...			Format arguments.
   282  * @noreturn
   283  * @error				Always!
   284  */
   285 native ThrowError(const String:fmt[], any:...);
   286 
   287 /**
   288  * Logs a plugin message to the SourceMod logs.  The log message will be 
   289  * prefixed by the plugin's logtag (filename).
   290  *
   291  * @param format		String format.
   292  * @param ...			Format arguments.
   293  * @noreturn
   294  */
   295 native LogMessage(const String:format[], any:...);
   296 
   297 /**
   298  * Logs a message to the SourceMod logs without any plugin logtag.  This is 
   299  * useful for re-routing messages from other plugins, for example, messages 
   300  * from LogAction().
   301  *
   302  * @param format		String format.
   303  * @param ...			Format arguments.
   304  * @noreturn
   305  */
   306 native LogMessageEx(const String:format[], any:...);
   307 
   308 /**
   309  * Logs a message to any file.  The log message will be in the normal 
   310  * SourceMod format, with the plugin logtag prepended.
   311  *
   312  * @param file			File to write the log message in.
   313  * @param format		String format.
   314  * @param ...			Format arguments.
   315  * @noreturn
   316  * @error				File could not be opened/written.
   317  */
   318 native LogToFile(const String:file[], const String:format[], any:...);
   319 
   320 /**
   321  * Same as LogToFile(), except no plugin logtag is prepended.
   322  *
   323  * @param file			File to write the log message in.
   324  * @param format		String format.
   325  * @param ...			Format arguments.
   326  * @noreturn
   327  * @error				File could not be opened/written.
   328  */
   329 native LogToFileEx(const String:file[], const String:format[], any:...);
   330 
   331 /**
   332  * Logs an action from a command or event whereby interception and routing may 
   333  * be important.  This is intended to be a logging version of ShowActivity().
   334  *
   335  * @param client		Client performing the action, 0 for server, or -1 if not 
   336  *						applicable.
   337  * @param target		Client being targetted, or -1 if not applicable.
   338  * @param message		Message format.
   339  * @param ...			Message formatting parameters.
   340  * @noreturn
   341  */
   342 native LogAction(client, target, const String:message[], any:...);
   343 
   344 /**
   345  * Logs a plugin error message to the SourceMod logs.
   346  *
   347  * @param format		String format.
   348  * @param ...			Format arguments.
   349  * @noreturn
   350  */
   351 native LogError(const String:format[], any:...);
   352 
   353 /**
   354  * Gets the system time as a unix timestamp.
   355  *
   356  * @param bigStamp		Optional array to store the 64bit timestamp in.
   357  * @return				32bit timestamp (number of seconds since unix epoch).
   358  */
   359 native GetTime(bigStamp[2]={0,0});
   360 
   361 /**
   362  * Produces a date and/or time string value for a timestamp.
   363  *
   364  * See this URL for valid parameters: 
   365  * http://opengroup.org/onlinepubs/007908799/xsh/strftime.html
   366  *
   367  * @param buffer		Destination string buffer.
   368  * @param maxlength		Maximum length of output string buffer.
   369  * @param format		Formatting rules (passing NULL_STRING will use the rules defined in sm_datetime_format).
   370  * @param stamp			Optional time stamp.
   371  * @noreturn
   372  * @error			Buffer too small or invalid time format.
   373  */
   374 native FormatTime(String:buffer[], maxlength, const String:format[], stamp=-1);
   375 
   376 /**
   377  * Loads a game config file.
   378  *
   379  * @param file			File to load.  The path must be relative to the 'gamedata' folder under the config folder 
   380  *					and the extension should be omitted.
   381  * @return			A handle to the game config file or INVALID_HANDLE in failure.
   382  */
   383 native Handle:LoadGameConfigFile(const String:file[]);
   384 
   385 /**
   386  * Returns an offset value.
   387  *
   388  * @param gc			Game config handle.
   389  * @param key			Key to retrieve from the offset section.
   390  * @return			An offset, or -1 on failure.
   391  */
   392 native GameConfGetOffset(Handle:gc, const String:key[]);
   393 
   394 /**
   395  * Gets the value of a key from the "Keys" section.
   396  *
   397  * @param gc			Game config handle.
   398  * @param key			Key to retrieve from the Keys section.
   399  * @param buffer		Destination string buffer.
   400  * @param maxlen		Maximum length of output string buffer.
   401  * @return				True if key existed, false otherwise.
   402  */
   403 native bool:GameConfGetKeyValue(Handle:gc, const String:key[], String:buffer[], maxlen);
   404 
   405 /**
   406  * Returns the operating system's "tick count," which is a number of 
   407  * milliseconds since the operating system loaded.  This can be used
   408  * for basic benchmarks.
   409  *
   410  * @return				Tick count in milliseconds.
   411  */
   412 native GetSysTickCount();
   413 
   414 /**
   415  * Specifies that the given config file should be executed after plugin load.
   416  * OnConfigsExecuted() will not be called until the config file has executed, 
   417  * but it will be called if the execution fails.
   418  *
   419  * @param autoCreate	If true, and the config file does not exist, such a config
   420  *						file will be automatically created and populated with
   421  *						information from the plugin's registered cvars.
   422  * @param name			Name of the config file, excluding the .cfg extension.
   423  *						If empty, <plugin.filename.cfg> is assumed.
   424  * @param folder		Folder under cfg/ to use.  By default this is "sourcemod."
   425  * @noreturn
   426  */
   427 native AutoExecConfig(bool:autoCreate=true, const String:name[]="", const String:folder[]="sourcemod");
   428 
   429 /**
   430  * Sets a native as optional, such that if it is unloaded, removed,
   431  * or otherwise non-existent, the plugin will still work.  Calling
   432  * removed natives results in a run-time error.
   433  *
   434  * @param name			Native name.
   435  * @noreturn
   436  */
   437 native MarkNativeAsOptional(const String:name[]);
   438 
   439 /**
   440  * Registers a library name for identifying as a dependency to 
   441  * other plugins.
   442  *
   443  * @param name			Library name.
   444  * @noreturn
   445  */
   446 native RegPluginLibrary(const String:name[]);
   447 
   448 /**
   449  * Returns whether a library exists.
   450  *
   451  * @param name			Library name.
   452  * @return				True if exists, false otherwise.
   453  */
   454 native bool:LibraryExists(const String:name[]);
   455 
   456 /**
   457  * Returns the status of an extension, by filename.
   458  *
   459  * @param name			Extension name (like "sdktools.ext").
   460  * @param error			Optional error message buffer.
   461  * @param maxlength		Length of optional error message buffer.
   462  * @return				-2 if the extension was not found.
   463  *						-1 if the extension was found but failed to load.
   464  * 						 0 if the extension loaded but reported an error.
   465  * 						 1 if the extension is running without error.
   466  */
   467 native GetExtensionFileStatus(const String:name[], String:error[]="", maxlength=0);
   468 
   469 /**
   470  * Called after a library (plugin) is added that the 
   471  * current plugin references optionally.
   472  *
   473  * @param name			Library name.
   474  */
   475 forward OnLibraryAdded(const String:name[]);
   476 
   477 /**
   478  * Called right before a library (plugin) is removed that the 
   479  * current plugin references optionally.
   480  *
   481  * @param name			Library name.
   482  */
   483 forward OnLibraryRemoved(const String:name[]);
   484 
   485 #include <helpers>
   486 #include <entity>
   487