plugins/include/functions.inc
author David Anderson <dvander@alliedmods.net>
Sun Nov 25 18:50:13 2007 +0000 (2007-11-25)
changeset 1725 ea875bcbc286
parent 1331 268429d2c9e4
child 1926 295f585cc4f5
permissions -rw-r--r--
small clarification to param2 in MenuAction_End
     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 #define SP_PARAMFLAG_BYREF	(1<<0)	/**< Internal use only. */
    34 
    35 /**
    36  * Describes the various ways to pass parameters to functions or forwards.
    37  */
    38 enum ParamType
    39 {
    40 	Param_Any			= 0,							/**< Any data type can be pushed */
    41 	Param_Cell			= (1<<1),						/**< Only basic cells can be pushed */
    42 	Param_Float			= (2<<1),						/**< Only floats can be pushed */
    43 	Param_String		= (3<<1)|SP_PARAMFLAG_BYREF,	/**< Only strings can be pushed */
    44 	Param_Array			= (4<<1)|SP_PARAMFLAG_BYREF,	/**< Only arrays can be pushed */
    45 	Param_VarArgs		= (5<<1),						/**< Same as "..." in plugins, anything can be pushed, but it will always be byref */
    46 	Param_CellByRef		= (1<<1)|SP_PARAMFLAG_BYREF,	/**< Only a cell by reference can be pushed */
    47 	Param_FloatByRef	= (2<<1)|SP_PARAMFLAG_BYREF		/**< Only a float by reference can be pushed */
    48 };
    49 
    50 /**
    51  * Defines how a forward iterates through plugin functions.
    52  */
    53 enum ExecType
    54 {
    55 	ET_Ignore	= 0,	/**< Ignore all return values, return 0 */
    56 	ET_Single	= 1,	/**< Only return the last exec, ignore all others */
    57 	ET_Event	= 2,	/**< Acts as an event with the Actions defined in core.inc, no mid-Stops allowed, returns highest */
    58 	ET_Hook		= 3		/**< Acts as a hook with the Actions defined in core.inc, mid-Stops allowed, returns highest */
    59 };
    60 
    61 /**
    62  * @section Flags that are used with Call_PushArrayEx() and Call_PushStringEx()
    63  */
    64 #define SM_PARAM_COPYBACK		(1<<0)		/**< Copy an array/reference back after call */
    65 #define SM_PARAM_STRING_UTF8	(1<<0)		/**< String should be UTF-8 handled */
    66 #define SM_PARAM_STRING_COPY	(1<<1)		/**< String should be copied into the plugin */
    67 
    68 /**
    69  * @endsection
    70  */
    71 
    72 /**
    73  * @section Error codes
    74  */
    75 #define SP_ERROR_NONE					0	/**< No error occurred */
    76 #define SP_ERROR_FILE_FORMAT			1	/**< File format unrecognized */
    77 #define SP_ERROR_DECOMPRESSOR			2	/**< A decompressor was not found */
    78 #define SP_ERROR_HEAPLOW				3	/**< Not enough space left on the heap */
    79 #define SP_ERROR_PARAM					4	/**< Invalid parameter or parameter type */
    80 #define SP_ERROR_INVALID_ADDRESS		5	/**< A memory address was not valid */
    81 #define SP_ERROR_NOT_FOUND				6	/**< The object in question was not found */
    82 #define SP_ERROR_INDEX					7	/**< Invalid index parameter */
    83 #define SP_ERROR_STACKLOW				8	/**< Nnot enough space left on the stack */
    84 #define SP_ERROR_NOTDEBUGGING			9	/**< Debug mode was not on or debug section not found */
    85 #define SP_ERROR_INVALID_INSTRUCTION	10	/**< Invalid instruction was encountered */
    86 #define SP_ERROR_MEMACCESS				11	/**< Invalid memory access */
    87 #define SP_ERROR_STACKMIN				12	/**< Stack went beyond its minimum value */
    88 #define SP_ERROR_HEAPMIN				13  /**< Heap went beyond its minimum value */
    89 #define SP_ERROR_DIVIDE_BY_ZERO			14	/**< Division by zero */
    90 #define SP_ERROR_ARRAY_BOUNDS			15	/**< Array index is out of bounds */
    91 #define SP_ERROR_INSTRUCTION_PARAM		16	/**< Instruction had an invalid parameter */
    92 #define SP_ERROR_STACKLEAK				17  /**< A native leaked an item on the stack */
    93 #define SP_ERROR_HEAPLEAK				18  /**< A native leaked an item on the heap */
    94 #define SP_ERROR_ARRAY_TOO_BIG			19	/**< A dynamic array is too big */
    95 #define SP_ERROR_TRACKER_BOUNDS			20	/**< Tracker stack is out of bounds */
    96 #define SP_ERROR_INVALID_NATIVE			21	/**< Native was pending or invalid */
    97 #define SP_ERROR_PARAMS_MAX				22	/**< Maximum number of parameters reached */
    98 #define SP_ERROR_NATIVE					23	/**< Error originates from a native */
    99 #define SP_ERROR_NOT_RUNNABLE			24	/**< Function or plugin is not runnable */
   100 #define SP_ERROR_ABORTED				25	/**< Function call was aborted */
   101 
   102 /**
   103  * @endsection
   104  */
   105 
   106 /**
   107  * Gets a function id from a function name. 
   108  *
   109  * @param plugin			Handle of the plugin that contains the function.
   110 							Pass INVALID_HANDLE to search in the calling plugin.
   111  * @param name				Name of the function.
   112  * @return 					Function id or INVALID_FUNCTION if not found.
   113  * @error					Invalid or corrupt plugin handle.
   114  */
   115 native Function:GetFunctionByName(Handle:plugin, const String:name[]);
   116 
   117 /**
   118  * Creates a global forward.
   119  *
   120  * @note The name used to create the forward is used as its public function in all target plugins.
   121  * @note This is ideal for global, static forwards that are never changed.
   122  * @note Global forwards cannot be cloned.
   123  * @note Use CloseHandle() to destroy these.
   124  *
   125  * @param name				Name of public function to use in forward.
   126  * @param type				Execution type to be used.
   127  * @param ...				Variable number of parameter types (up to 32).
   128  * @return 					Handle to new global forward.
   129  * @error					More than 32 paramater types passed.
   130  */
   131 native Handle:CreateGlobalForward(const String:name[], ExecType:type, ParamType:...);
   132 
   133 /**
   134  * Creates a private forward.
   135  *
   136  * @note No functions are automatically added. Use AddToForward() to do this.
   137  * @note Private forwards can be cloned.
   138  * @note Use CloseHandle() to destroy these.
   139  *
   140  * @param type				Execution type to be used.
   141  * @param ...				Variable number of parameter types (up to 32).
   142  * @return 					Handle to new private forward.
   143  * @error					More than 32 paramater types passed.
   144  */
   145 native Handle:CreateForward(ExecType:type, ParamType:...);
   146 
   147 /**
   148  * Returns the number of functions in a global or private forward's call list.
   149  *
   150  * @param fwd				Handle to global or private forward.
   151  * @return					Number of functions in forward.
   152  * @error					Invalid or corrupt forward handle.
   153  */
   154 native GetForwardFunctionCount(Handle:fwd);
   155 
   156 /**
   157  * Adds a function to a private forward's call list.
   158  *
   159  * @note Cannot be used during an incompleted call.
   160  *
   161  * @param fwd				Handle to private forward.
   162  * @param plugin			Handle of the plugin that contains the function.
   163  *							Pass INVALID_HANDLE to specify the calling plugin.
   164  * @param func				Function to add to forward.
   165  * @return					True on success, false otherwise.
   166  * @error					Invalid or corrupt private forward handle, invalid or corrupt plugin handle, or invalid function.
   167  */
   168 native bool:AddToForward(Handle:fwd, Handle:plugin, Function:func);
   169 
   170 /**
   171  * Removes a function from a private forward's call list.
   172  *
   173  * @note Only removes one instance.
   174  * @note Functions will be removed automatically if their parent plugin is unloaded.
   175  *
   176  * @param fwd				Handle to private forward.
   177  * @param plugin			Handle of the plugin that contains the function.
   178  *							Pass INVALID_HANDLE to specify the calling plugin.
   179  * @param func				Function to remove from forward.
   180  * @return					True on success, false otherwise.
   181  * @error					Invalid or corrupt private forward handle, invalid or corrupt plugin handle, or invalid function.
   182  */
   183 native bool:RemoveFromForward(Handle:fwd, Handle:plugin, Function:func);
   184 
   185 /**
   186  * Removes all instances of a plugin from a private forward's call list.
   187  *
   188  * @note Functions will be removed automatically if their parent plugin is unloaded.
   189  *
   190  * @param fwd				Handle to private forward.
   191  * @param plugin			Handle of the plugin to remove instances of.
   192  *							Pass INVALID_HANDLE to specify the calling plugin.
   193  * @return					Number of functions removed from forward.
   194  * @error					Invalid or corrupt private forward handle or invalid or corrupt plugin handle.
   195  */
   196 native RemoveAllFromForward(Handle:fwd, Handle:plugin);
   197 
   198 /**
   199  * Starts a call to functions in a forward's call list.
   200  *
   201  * @note Cannot be used during an incompleted call.
   202  *
   203  * @param fwd				Handle to global or private forward.
   204  * @noreturn
   205  * @error					Invalid or corrupt forward handle or called before another call has completed.
   206  */
   207 native Call_StartForward(Handle:fwd);
   208 
   209 /**
   210  * Starts a call to a function.
   211  *
   212  * @note Cannot be used during an incompleted call.
   213  *
   214  * @param plugin			Handle of the plugin that contains the function.
   215  *							Pass INVALID_HANDLE to specify the calling plugin.
   216  * @param func				Function to call.
   217  * @noreturn
   218  * @error					Invalid or corrupt plugin handle, invalid function, or called before another call has completed.
   219  */
   220 native Call_StartFunction(Handle:plugin, Function:func);
   221 
   222 /**
   223  * Pushes a cell onto the current call.
   224  *
   225  * @note Cannot be used before a call has been started.
   226  *
   227  * @param value				Cell value to push.
   228  * @noreturn
   229  * @error					Called before a call has been started.
   230  */
   231 native Call_PushCell(any:value);
   232 
   233 /**
   234  * Pushes a cell by reference onto the current call.
   235  *
   236  * @note Cannot be used before a call has been started.
   237  *
   238  * @param value				Cell reference to push.
   239  * @noreturn
   240  * @error					Called before a call has been started.
   241  */
   242 native Call_PushCellRef(&any:value);
   243 
   244 /**
   245  * Pushes a float onto the current call.
   246  *
   247  * @note Cannot be used before a call has been started.
   248  *
   249  * @param value				Floating point value to push.
   250  * @noreturn
   251  * @error					Called before a call has been started.
   252  */
   253 native Call_PushFloat(Float:value);
   254 
   255 /**
   256  * Pushes a float by reference onto the current call.
   257  *
   258  * @note Cannot be used before a call has been started.
   259  *
   260  * @param value				Floating point reference to push.
   261  * @noreturn
   262  * @error					Called before a call has been started.
   263  */
   264 native Call_PushFloatRef(&Float:value);
   265 
   266 /**
   267  * Pushes an array onto the current call.
   268  *
   269  * @note Changes to array are not copied back to caller. Use PushArrayEx() to do this.
   270  * @note Cannot be used before a call has been started.
   271  *
   272  * @param value				Array to push.
   273  * @param size				Size of array.
   274  * @noreturn
   275  * @error					Called before a call has been started.
   276  */
   277 native Call_PushArray(const any:value[], size);
   278 
   279 /**
   280  * Pushes an array onto the current call.
   281  *
   282  * @note Cannot be used before a call has been started.
   283  *
   284  * @param value				Array to push.
   285  * @param size				Size of array.
   286  * @param cpflags			Whether or not changes should be copied back to the input array.
   287  *							See SP_PARAM_* constants for details.
   288  * @noreturn
   289  * @error					Called before a call has been started.
   290  */
   291 native Call_PushArrayEx(any:value[], size, cpflags);
   292 
   293 /**
   294  * Pushes a string onto the current call.
   295  *
   296  * @note Changes to string are not copied back to caller. Use PushStringEx() to do this.
   297  * @note Cannot be used before a call has been started.
   298  *
   299  * @param value				String to push.
   300  * @noreturn
   301  * @error					Called before a call has been started.
   302  */
   303 native Call_PushString(const String:value[]);
   304 
   305 /**
   306  * Pushes a string onto the current call.
   307  *
   308  * @note Cannot be used before a call has been started.
   309  *
   310  * @param value				String to push.
   311  * @param length			Length of string buffer.
   312  * @param szflags			Flags determining how string should be handled.
   313  *							See SP_PARAM_STRING_* constants for details.
   314  * @param cpflags			Whether or not changes should be copied back to the input array.
   315  *							See SP_PARAM_* constants for details.		
   316  * @noreturn
   317  * @error					Called before a call has been started.
   318  */
   319 native Call_PushStringEx(String:value[], length, szflags, cpflags);
   320 
   321 /**
   322  * Completes a call to a function or forward's call list.
   323  *
   324  * @note Cannot be used before a call has been started.
   325  *
   326  * @param result			Return value of function or forward's call list.		
   327  * @return					SP_ERROR_NONE on success, any other integer on failure.
   328  * @error					Called before a call has been started.
   329  */
   330 native Call_Finish(&any:result=0);
   331 
   332 /**
   333  * Cancels a call to a function or forward's call list.
   334  *
   335  * @note Cannot be used before a call has been started.
   336  *		
   337  * @noreturn
   338  * @error					Called before a call has been started.
   339  */
   340 native Call_Cancel();
   341 
   342 /**
   343  * Defines a native function.
   344  *
   345  * It is not necessary to validate the parameter count 
   346  *
   347  * @param plugin			Handle of the calling plugin.
   348  * @param numParams			Number of parameters passed to the native.
   349  * @return 					Value for the native call to return.
   350  */
   351 functag NativeCall public(Handle:plugin, numParams);
   352 
   353 /**
   354  * Creates a dynamic native.  This should only be called in AskPluginLoad(), or 
   355  * else you risk not having your native shared with other plugins.
   356  *
   357  * @param name				Name of the dynamic native; must be unique amongst
   358  *							all other registered dynamic natives.
   359  * @param func				Function to use as the dynamic native.
   360  * @noreturn
   361  */
   362 native CreateNative(const String:name[], NativeCall:func);
   363 
   364 /**
   365  * Throws an error in the calling plugin of a native, instead of your own plugin.
   366  *
   367  * @param error				Error code to use.
   368  * @param fmt				Error message format.
   369  * @param ...				Format arguments.
   370  */
   371 native ThrowNativeError(error, const String:fmt[], any:...);
   372 
   373 /**
   374  * Retrieves the string length from a native parameter string.  This is useful 
   375  * fetching the entire string using dynamic arrays.
   376  * @note If this function succeeds, Get/SetNativeString will also succeed.
   377  *
   378  * @param param				Parameter number, starting from 1.
   379  * @param length			Stores the length of the string.
   380  * @return					SP_ERROR_NONE on success, any other integer on failure.
   381  * @error					Invalid parameter number or calling from a non-native function.
   382  */
   383 native GetNativeStringLength(param, &length);
   384 
   385 /**
   386  * Retrieves a string from a native parameter.
   387  * @note Output conditions are undefined on failure.
   388  *
   389  * @param param				Parameter number, starting from 1.
   390  * @param buffer			Buffer to store the string in.
   391  * @param maxlength			Maximum length of the buffer.
   392  * @param bytes				Optionally store the number of bytes written.
   393  * @return					SP_ERROR_NONE on success, any other integer on failure.
   394  * @error					Invalid parameter number or calling from a non-native function.
   395  */
   396 native GetNativeString(param, String:buffer[], maxlength, &bytes=0);
   397 
   398 /**
   399  * Sets a string in a native parameter.
   400  * @note Output conditions are undefined on failure.
   401  *
   402  * @param param				Parameter number, starting from 1.
   403  * @param source			Source string to use.
   404  * @param maxlength			Maximum number of bytes to write.
   405  * @param utf8				If false, string will not be written
   406  *							with UTF8 safety.
   407  * @param bytes				Optionally store the number of bytes written.
   408  * @return					SP_ERROR_NONE on success, any other integer on failure.
   409  * @error					Invalid parameter number or calling from a non-native function.
   410  */
   411 native SetNativeString(param, const String:source[], maxlength, bool:utf8=true, &bytes=0);
   412 
   413 /**
   414  * Gets a cell from a native parameter.
   415  *
   416  * @param param				Parameter number, starting from 1.
   417  * @return					Cell value at the parameter number.
   418  * @error					Invalid parameter number or calling from a non-native function.
   419  */
   420 native any:GetNativeCell(param);
   421 
   422 /**
   423  * Gets a cell from a native parameter, by reference.
   424  *
   425  * @param param				Parameter number, starting from 1.
   426  * @return					Cell value at the parameter number.
   427  * @error					Invalid parameter number or calling from a non-native function.
   428  */
   429 native any:GetNativeCellRef(param);
   430 
   431 /**
   432  * Sets a cell from a native parameter, by reference.
   433  *
   434  * @param param				Parameter number, starting from 1.
   435  * @param value				Cell value at the parameter number to set by reference.
   436  * @noreturn
   437  * @error					Invalid parameter number or calling from a non-native function.
   438  */
   439 native SetNativeCellRef(param, any:value);
   440 
   441 /**
   442  * Gets an array from a native parameter (always by reference).
   443  *
   444  * @param param				Parameter number, starting from 1.
   445  * @param local				Local array to copy into.
   446  * @param size				Maximum size of local array.
   447  * @return					SP_ERROR_NONE on success, anything else on failure.
   448  * @error					Invalid parameter number or calling from a non-native function.
   449  */
   450 native GetNativeArray(param, any:local[], size);
   451 
   452 /**
   453  * Copies a local array into a native parameter array (always by reference).
   454  * 
   455  * @param param				Parameter number, starting from 1.
   456  * @param local				Local array to copy from.
   457  * @param size				Size of the local array to copy.
   458  * @return					SP_ERROR_NONE on success, anything else on failure.
   459  * @error					Invalid parameter number or calling from a non-native function.
   460  */
   461 native SetNativeArray(param, const any:local[], size);
   462 
   463 /**
   464  * Formats a string using parameters from a native.
   465  *
   466  * @note All parameter indexes start at 1.
   467  * @note If the input and output buffers overlap, the contents
   468  *  of the output buffer at the end is undefined.
   469  *
   470  * @param out_param			Output parameter number to write to.  If 0, out_string is used.
   471  * @param fmt_param			Format parameter number.  If 0, fmt_string is used.
   472  * @param vararg_param		First variable parameter number.
   473  * @param out_len			Output string buffer maximum length (always required).
   474  * @param written			Optionally stores the number of bytes written.
   475  * @param out_string		Output string buffer to use if out_param is not used.
   476  * @param fmt_string		Format string to use if fmt_param is not used.
   477  * @return					SP_ERROR_NONE on success, anything else on failure.
   478  */
   479 native FormatNativeString(out_param, 
   480 						  fmt_param, 
   481 						  vararg_param, 
   482 						  out_len,
   483 						  &written=0,
   484 						  String:out_string[]="",
   485 						  const String:fmt_string[]="");
   486 
   487 /**
   488  * Backwards compatibility stock - use GetNativeCell or GetNativeCellRef
   489  *
   490  * @deprecated
   491  */
   492 #pragma deprecated Use GetNativeCell() or GetNativeCellRef() instead
   493 stock Float:GetNativeFloat(param, bool:byref)
   494 {
   495 	if (!byref)
   496 	{
   497 		return Float:GetNativeCell(param);
   498 	} else {
   499 		return Float:GetNativeCellRef(param);
   500 	}
   501 }
   502 
   503 /**
   504  * Backwards compatibility stock - use GetNativeCell or GetNativeCellRef
   505  *
   506  * @deprecated
   507  */
   508 #pragma deprecated Use GetNativeCell() or GetNativeCellRef() instead
   509 stock Handle:GetNativeHandle(param, bool:byref)
   510 {
   511 	if (!byref)
   512 	{
   513 		return Handle:GetNativeCell(param);
   514 	} else {
   515 		return Handle:GetNativeCellRef(param);
   516 	}
   517 }