plugins/include/functions.inc
branchrefac-jit
changeset 2284 026da5437111
parent 2192 7602d7fb60d9
child 2288 15c77b21589a
     1.1 --- a/plugins/include/functions.inc	Fri Jul 11 06:51:21 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,489 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     1.8 - * =============================================================================
     1.9 - *
    1.10 - * This file is part of the SourceMod/SourcePawn SDK.
    1.11 - *
    1.12 - * This program is free software; you can redistribute it and/or modify it under
    1.13 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.14 - * Free Software Foundation.
    1.15 - * 
    1.16 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.17 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.18 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.19 - * details.
    1.20 - *
    1.21 - * You should have received a copy of the GNU General Public License along with
    1.22 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.23 - *
    1.24 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.25 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.26 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.27 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.28 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.29 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.30 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.31 - * or <http://www.sourcemod.net/license.php>.
    1.32 - *
    1.33 - * Version: $Id$
    1.34 - */
    1.35 - 
    1.36 -#define SP_PARAMFLAG_BYREF	(1<<0)	/**< Internal use only. */
    1.37 -
    1.38 -/**
    1.39 - * Describes the various ways to pass parameters to functions or forwards.
    1.40 - */
    1.41 -enum ParamType
    1.42 -{
    1.43 -	Param_Any			= 0,							/**< Any data type can be pushed */
    1.44 -	Param_Cell			= (1<<1),						/**< Only basic cells can be pushed */
    1.45 -	Param_Float			= (2<<1),						/**< Only floats can be pushed */
    1.46 -	Param_String		= (3<<1)|SP_PARAMFLAG_BYREF,	/**< Only strings can be pushed */
    1.47 -	Param_Array			= (4<<1)|SP_PARAMFLAG_BYREF,	/**< Only arrays can be pushed */
    1.48 -	Param_VarArgs		= (5<<1),						/**< Same as "..." in plugins, anything can be pushed, but it will always be byref */
    1.49 -	Param_CellByRef		= (1<<1)|SP_PARAMFLAG_BYREF,	/**< Only a cell by reference can be pushed */
    1.50 -	Param_FloatByRef	= (2<<1)|SP_PARAMFLAG_BYREF		/**< Only a float by reference can be pushed */
    1.51 -};
    1.52 -
    1.53 -/**
    1.54 - * Defines how a forward iterates through plugin functions.
    1.55 - */
    1.56 -enum ExecType
    1.57 -{
    1.58 -	ET_Ignore	= 0,	/**< Ignore all return values, return 0 */
    1.59 -	ET_Single	= 1,	/**< Only return the last exec, ignore all others */
    1.60 -	ET_Event	= 2,	/**< Acts as an event with the Actions defined in core.inc, no mid-Stops allowed, returns highest */
    1.61 -	ET_Hook		= 3		/**< Acts as a hook with the Actions defined in core.inc, mid-Stops allowed, returns highest */
    1.62 -};
    1.63 -
    1.64 -/**
    1.65 - * @section Flags that are used with Call_PushArrayEx() and Call_PushStringEx()
    1.66 - */
    1.67 - 
    1.68 -#define SM_PARAM_COPYBACK		(1<<0)		/**< Copy an array/reference back after call */
    1.69 -
    1.70 -#define SM_PARAM_STRING_UTF8	(1<<0)		/**< String should be UTF-8 handled */
    1.71 -#define SM_PARAM_STRING_COPY	(1<<1)		/**< String should be copied into the plugin */
    1.72 -#define SM_PARAM_STRING_BINARY	(1<<2)		/**< Treat the string as a binary string */
    1.73 -
    1.74 -/**
    1.75 - * @endsection
    1.76 - */
    1.77 -
    1.78 -/**
    1.79 - * @section Error codes
    1.80 - */
    1.81 -#define SP_ERROR_NONE					0	/**< No error occurred */
    1.82 -#define SP_ERROR_FILE_FORMAT			1	/**< File format unrecognized */
    1.83 -#define SP_ERROR_DECOMPRESSOR			2	/**< A decompressor was not found */
    1.84 -#define SP_ERROR_HEAPLOW				3	/**< Not enough space left on the heap */
    1.85 -#define SP_ERROR_PARAM					4	/**< Invalid parameter or parameter type */
    1.86 -#define SP_ERROR_INVALID_ADDRESS		5	/**< A memory address was not valid */
    1.87 -#define SP_ERROR_NOT_FOUND				6	/**< The object in question was not found */
    1.88 -#define SP_ERROR_INDEX					7	/**< Invalid index parameter */
    1.89 -#define SP_ERROR_STACKLOW				8	/**< Nnot enough space left on the stack */
    1.90 -#define SP_ERROR_NOTDEBUGGING			9	/**< Debug mode was not on or debug section not found */
    1.91 -#define SP_ERROR_INVALID_INSTRUCTION	10	/**< Invalid instruction was encountered */
    1.92 -#define SP_ERROR_MEMACCESS				11	/**< Invalid memory access */
    1.93 -#define SP_ERROR_STACKMIN				12	/**< Stack went beyond its minimum value */
    1.94 -#define SP_ERROR_HEAPMIN				13  /**< Heap went beyond its minimum value */
    1.95 -#define SP_ERROR_DIVIDE_BY_ZERO			14	/**< Division by zero */
    1.96 -#define SP_ERROR_ARRAY_BOUNDS			15	/**< Array index is out of bounds */
    1.97 -#define SP_ERROR_INSTRUCTION_PARAM		16	/**< Instruction had an invalid parameter */
    1.98 -#define SP_ERROR_STACKLEAK				17  /**< A native leaked an item on the stack */
    1.99 -#define SP_ERROR_HEAPLEAK				18  /**< A native leaked an item on the heap */
   1.100 -#define SP_ERROR_ARRAY_TOO_BIG			19	/**< A dynamic array is too big */
   1.101 -#define SP_ERROR_TRACKER_BOUNDS			20	/**< Tracker stack is out of bounds */
   1.102 -#define SP_ERROR_INVALID_NATIVE			21	/**< Native was pending or invalid */
   1.103 -#define SP_ERROR_PARAMS_MAX				22	/**< Maximum number of parameters reached */
   1.104 -#define SP_ERROR_NATIVE					23	/**< Error originates from a native */
   1.105 -#define SP_ERROR_NOT_RUNNABLE			24	/**< Function or plugin is not runnable */
   1.106 -#define SP_ERROR_ABORTED				25	/**< Function call was aborted */
   1.107 -
   1.108 -/**
   1.109 - * @endsection
   1.110 - */
   1.111 -
   1.112 -/**
   1.113 - * Gets a function id from a function name. 
   1.114 - *
   1.115 - * @param plugin			Handle of the plugin that contains the function.
   1.116 -							Pass INVALID_HANDLE to search in the calling plugin.
   1.117 - * @param name				Name of the function.
   1.118 - * @return 					Function id or INVALID_FUNCTION if not found.
   1.119 - * @error					Invalid or corrupt plugin handle.
   1.120 - */
   1.121 -native Function:GetFunctionByName(Handle:plugin, const String:name[]);
   1.122 -
   1.123 -/**
   1.124 - * Creates a global forward.
   1.125 - *
   1.126 - * @note The name used to create the forward is used as its public function in all target plugins.
   1.127 - * @note This is ideal for global, static forwards that are never changed.
   1.128 - * @note Global forwards cannot be cloned.
   1.129 - * @note Use CloseHandle() to destroy these.
   1.130 - *
   1.131 - * @param name				Name of public function to use in forward.
   1.132 - * @param type				Execution type to be used.
   1.133 - * @param ...				Variable number of parameter types (up to 32).
   1.134 - * @return 					Handle to new global forward.
   1.135 - * @error					More than 32 paramater types passed.
   1.136 - */
   1.137 -native Handle:CreateGlobalForward(const String:name[], ExecType:type, ParamType:...);
   1.138 -
   1.139 -/**
   1.140 - * Creates a private forward.
   1.141 - *
   1.142 - * @note No functions are automatically added. Use AddToForward() to do this.
   1.143 - * @note Private forwards can be cloned.
   1.144 - * @note Use CloseHandle() to destroy these.
   1.145 - *
   1.146 - * @param type				Execution type to be used.
   1.147 - * @param ...				Variable number of parameter types (up to 32).
   1.148 - * @return 					Handle to new private forward.
   1.149 - * @error					More than 32 paramater types passed.
   1.150 - */
   1.151 -native Handle:CreateForward(ExecType:type, ParamType:...);
   1.152 -
   1.153 -/**
   1.154 - * Returns the number of functions in a global or private forward's call list.
   1.155 - *
   1.156 - * @param fwd				Handle to global or private forward.
   1.157 - * @return					Number of functions in forward.
   1.158 - * @error					Invalid or corrupt forward handle.
   1.159 - */
   1.160 -native GetForwardFunctionCount(Handle:fwd);
   1.161 -
   1.162 -/**
   1.163 - * Adds a function to a private forward's call list.
   1.164 - *
   1.165 - * @note Cannot be used during an incompleted call.
   1.166 - *
   1.167 - * @param fwd				Handle to private forward.
   1.168 - * @param plugin			Handle of the plugin that contains the function.
   1.169 - *							Pass INVALID_HANDLE to specify the calling plugin.
   1.170 - * @param func				Function to add to forward.
   1.171 - * @return					True on success, false otherwise.
   1.172 - * @error					Invalid or corrupt private forward handle, invalid or corrupt plugin handle, or invalid function.
   1.173 - */
   1.174 -native bool:AddToForward(Handle:fwd, Handle:plugin, Function:func);
   1.175 -
   1.176 -/**
   1.177 - * Removes a function from a private forward's call list.
   1.178 - *
   1.179 - * @note Only removes one instance.
   1.180 - * @note Functions will be removed automatically if their parent plugin is unloaded.
   1.181 - *
   1.182 - * @param fwd				Handle to private forward.
   1.183 - * @param plugin			Handle of the plugin that contains the function.
   1.184 - *							Pass INVALID_HANDLE to specify the calling plugin.
   1.185 - * @param func				Function to remove from forward.
   1.186 - * @return					True on success, false otherwise.
   1.187 - * @error					Invalid or corrupt private forward handle, invalid or corrupt plugin handle, or invalid function.
   1.188 - */
   1.189 -native bool:RemoveFromForward(Handle:fwd, Handle:plugin, Function:func);
   1.190 -
   1.191 -/**
   1.192 - * Removes all instances of a plugin from a private forward's call list.
   1.193 - *
   1.194 - * @note Functions will be removed automatically if their parent plugin is unloaded.
   1.195 - *
   1.196 - * @param fwd				Handle to private forward.
   1.197 - * @param plugin			Handle of the plugin to remove instances of.
   1.198 - *							Pass INVALID_HANDLE to specify the calling plugin.
   1.199 - * @return					Number of functions removed from forward.
   1.200 - * @error					Invalid or corrupt private forward handle or invalid or corrupt plugin handle.
   1.201 - */
   1.202 -native RemoveAllFromForward(Handle:fwd, Handle:plugin);
   1.203 -
   1.204 -/**
   1.205 - * Starts a call to functions in a forward's call list.
   1.206 - *
   1.207 - * @note Cannot be used during an incompleted call.
   1.208 - *
   1.209 - * @param fwd				Handle to global or private forward.
   1.210 - * @noreturn
   1.211 - * @error					Invalid or corrupt forward handle or called before another call has completed.
   1.212 - */
   1.213 -native Call_StartForward(Handle:fwd);
   1.214 -
   1.215 -/**
   1.216 - * Starts a call to a function.
   1.217 - *
   1.218 - * @note Cannot be used during an incompleted call.
   1.219 - *
   1.220 - * @param plugin			Handle of the plugin that contains the function.
   1.221 - *							Pass INVALID_HANDLE to specify the calling plugin.
   1.222 - * @param func				Function to call.
   1.223 - * @noreturn
   1.224 - * @error					Invalid or corrupt plugin handle, invalid function, or called before another call has completed.
   1.225 - */
   1.226 -native Call_StartFunction(Handle:plugin, Function:func);
   1.227 -
   1.228 -/**
   1.229 - * Pushes a cell onto the current call.
   1.230 - *
   1.231 - * @note Cannot be used before a call has been started.
   1.232 - *
   1.233 - * @param value				Cell value to push.
   1.234 - * @noreturn
   1.235 - * @error					Called before a call has been started.
   1.236 - */
   1.237 -native Call_PushCell(any:value);
   1.238 -
   1.239 -/**
   1.240 - * Pushes a cell by reference onto the current call.
   1.241 - *
   1.242 - * @note Cannot be used before a call has been started.
   1.243 - *
   1.244 - * @param value				Cell reference to push.
   1.245 - * @noreturn
   1.246 - * @error					Called before a call has been started.
   1.247 - */
   1.248 -native Call_PushCellRef(&any:value);
   1.249 -
   1.250 -/**
   1.251 - * Pushes a float onto the current call.
   1.252 - *
   1.253 - * @note Cannot be used before a call has been started.
   1.254 - *
   1.255 - * @param value				Floating point value to push.
   1.256 - * @noreturn
   1.257 - * @error					Called before a call has been started.
   1.258 - */
   1.259 -native Call_PushFloat(Float:value);
   1.260 -
   1.261 -/**
   1.262 - * Pushes a float by reference onto the current call.
   1.263 - *
   1.264 - * @note Cannot be used before a call has been started.
   1.265 - *
   1.266 - * @param value				Floating point reference to push.
   1.267 - * @noreturn
   1.268 - * @error					Called before a call has been started.
   1.269 - */
   1.270 -native Call_PushFloatRef(&Float:value);
   1.271 -
   1.272 -/**
   1.273 - * Pushes an array onto the current call.
   1.274 - *
   1.275 - * @note Changes to array are not copied back to caller. Use PushArrayEx() to do this.
   1.276 - * @note Cannot be used before a call has been started.
   1.277 - *
   1.278 - * @param value				Array to push.
   1.279 - * @param size				Size of array.
   1.280 - * @noreturn
   1.281 - * @error					Called before a call has been started.
   1.282 - */
   1.283 -native Call_PushArray(const any:value[], size);
   1.284 -
   1.285 -/**
   1.286 - * Pushes an array onto the current call.
   1.287 - *
   1.288 - * @note Cannot be used before a call has been started.
   1.289 - *
   1.290 - * @param value				Array to push.
   1.291 - * @param size				Size of array.
   1.292 - * @param cpflags			Whether or not changes should be copied back to the input array.
   1.293 - *							See SP_PARAM_* constants for details.
   1.294 - * @noreturn
   1.295 - * @error					Called before a call has been started.
   1.296 - */
   1.297 -native Call_PushArrayEx(any:value[], size, cpflags);
   1.298 -
   1.299 -/**
   1.300 - * Pushes a string onto the current call.
   1.301 - *
   1.302 - * @note Changes to string are not copied back to caller. Use PushStringEx() to do this.
   1.303 - * @note Cannot be used before a call has been started.
   1.304 - *
   1.305 - * @param value				String to push.
   1.306 - * @noreturn
   1.307 - * @error					Called before a call has been started.
   1.308 - */
   1.309 -native Call_PushString(const String:value[]);
   1.310 -
   1.311 -/**
   1.312 - * Pushes a string onto the current call.
   1.313 - *
   1.314 - * @note Cannot be used before a call has been started.
   1.315 - *
   1.316 - * @param value				String to push.
   1.317 - * @param length			Length of string buffer.
   1.318 - * @param szflags			Flags determining how string should be handled.
   1.319 - *							See SP_PARAM_STRING_* constants for details.
   1.320 - *							The default (0) is to push ASCII.
   1.321 - * @param cpflags			Whether or not changes should be copied back to the input array.
   1.322 - *							See SP_PARAM_* constants for details.		
   1.323 - * @noreturn
   1.324 - * @error					Called before a call has been started.
   1.325 - */
   1.326 -native Call_PushStringEx(String:value[], length, szflags, cpflags);
   1.327 -
   1.328 -/**
   1.329 - * Completes a call to a function or forward's call list.
   1.330 - *
   1.331 - * @note Cannot be used before a call has been started.
   1.332 - *
   1.333 - * @param result			Return value of function or forward's call list.		
   1.334 - * @return					SP_ERROR_NONE on success, any other integer on failure.
   1.335 - * @error					Called before a call has been started.
   1.336 - */
   1.337 -native Call_Finish(&any:result=0);
   1.338 -
   1.339 -/**
   1.340 - * Cancels a call to a function or forward's call list.
   1.341 - *
   1.342 - * @note Cannot be used before a call has been started.
   1.343 - *		
   1.344 - * @noreturn
   1.345 - * @error					Called before a call has been started.
   1.346 - */
   1.347 -native Call_Cancel();
   1.348 -
   1.349 -/**
   1.350 - * Defines a native function.
   1.351 - *
   1.352 - * It is not necessary to validate the parameter count 
   1.353 - *
   1.354 - * @param plugin			Handle of the calling plugin.
   1.355 - * @param numParams			Number of parameters passed to the native.
   1.356 - * @return 					Value for the native call to return.
   1.357 - */
   1.358 -functag NativeCall public(Handle:plugin, numParams);
   1.359 -
   1.360 -/**
   1.361 - * Creates a dynamic native.  This should only be called in AskPluginLoad(), or 
   1.362 - * else you risk not having your native shared with other plugins.
   1.363 - *
   1.364 - * @param name				Name of the dynamic native; must be unique amongst
   1.365 - *							all other registered dynamic natives.
   1.366 - * @param func				Function to use as the dynamic native.
   1.367 - * @noreturn
   1.368 - */
   1.369 -native CreateNative(const String:name[], NativeCall:func);
   1.370 -
   1.371 -/**
   1.372 - * Throws an error in the calling plugin of a native, instead of your own plugin.
   1.373 - *
   1.374 - * @param error				Error code to use.
   1.375 - * @param fmt				Error message format.
   1.376 - * @param ...				Format arguments.
   1.377 - */
   1.378 -native ThrowNativeError(error, const String:fmt[], any:...);
   1.379 -
   1.380 -/**
   1.381 - * Retrieves the string length from a native parameter string.  This is useful 
   1.382 - * fetching the entire string using dynamic arrays.
   1.383 - * @note If this function succeeds, Get/SetNativeString will also succeed.
   1.384 - *
   1.385 - * @param param				Parameter number, starting from 1.
   1.386 - * @param length			Stores the length of the string.
   1.387 - * @return					SP_ERROR_NONE on success, any other integer on failure.
   1.388 - * @error					Invalid parameter number or calling from a non-native function.
   1.389 - */
   1.390 -native GetNativeStringLength(param, &length);
   1.391 -
   1.392 -/**
   1.393 - * Retrieves a string from a native parameter.
   1.394 - * @note Output conditions are undefined on failure.
   1.395 - *
   1.396 - * @param param				Parameter number, starting from 1.
   1.397 - * @param buffer			Buffer to store the string in.
   1.398 - * @param maxlength			Maximum length of the buffer.
   1.399 - * @param bytes				Optionally store the number of bytes written.
   1.400 - * @return					SP_ERROR_NONE on success, any other integer on failure.
   1.401 - * @error					Invalid parameter number or calling from a non-native function.
   1.402 - */
   1.403 -native GetNativeString(param, String:buffer[], maxlength, &bytes=0);
   1.404 -
   1.405 -/**
   1.406 - * Sets a string in a native parameter.
   1.407 - * @note Output conditions are undefined on failure.
   1.408 - *
   1.409 - * @param param				Parameter number, starting from 1.
   1.410 - * @param source			Source string to use.
   1.411 - * @param maxlength			Maximum number of bytes to write.
   1.412 - * @param utf8				If false, string will not be written
   1.413 - *							with UTF8 safety.
   1.414 - * @param bytes				Optionally store the number of bytes written.
   1.415 - * @return					SP_ERROR_NONE on success, any other integer on failure.
   1.416 - * @error					Invalid parameter number or calling from a non-native function.
   1.417 - */
   1.418 -native SetNativeString(param, const String:source[], maxlength, bool:utf8=true, &bytes=0);
   1.419 -
   1.420 -/**
   1.421 - * Gets a cell from a native parameter.
   1.422 - *
   1.423 - * @param param				Parameter number, starting from 1.
   1.424 - * @return					Cell value at the parameter number.
   1.425 - * @error					Invalid parameter number or calling from a non-native function.
   1.426 - */
   1.427 -native any:GetNativeCell(param);
   1.428 -
   1.429 -/**
   1.430 - * Gets a cell from a native parameter, by reference.
   1.431 - *
   1.432 - * @param param				Parameter number, starting from 1.
   1.433 - * @return					Cell value at the parameter number.
   1.434 - * @error					Invalid parameter number or calling from a non-native function.
   1.435 - */
   1.436 -native any:GetNativeCellRef(param);
   1.437 -
   1.438 -/**
   1.439 - * Sets a cell from a native parameter, by reference.
   1.440 - *
   1.441 - * @param param				Parameter number, starting from 1.
   1.442 - * @param value				Cell value at the parameter number to set by reference.
   1.443 - * @noreturn
   1.444 - * @error					Invalid parameter number or calling from a non-native function.
   1.445 - */
   1.446 -native SetNativeCellRef(param, any:value);
   1.447 -
   1.448 -/**
   1.449 - * Gets an array from a native parameter (always by reference).
   1.450 - *
   1.451 - * @param param				Parameter number, starting from 1.
   1.452 - * @param local				Local array to copy into.
   1.453 - * @param size				Maximum size of local array.
   1.454 - * @return					SP_ERROR_NONE on success, anything else on failure.
   1.455 - * @error					Invalid parameter number or calling from a non-native function.
   1.456 - */
   1.457 -native GetNativeArray(param, any:local[], size);
   1.458 -
   1.459 -/**
   1.460 - * Copies a local array into a native parameter array (always by reference).
   1.461 - * 
   1.462 - * @param param				Parameter number, starting from 1.
   1.463 - * @param local				Local array to copy from.
   1.464 - * @param size				Size of the local array to copy.
   1.465 - * @return					SP_ERROR_NONE on success, anything else on failure.
   1.466 - * @error					Invalid parameter number or calling from a non-native function.
   1.467 - */
   1.468 -native SetNativeArray(param, const any:local[], size);
   1.469 -
   1.470 -/**
   1.471 - * Formats a string using parameters from a native.
   1.472 - *
   1.473 - * @note All parameter indexes start at 1.
   1.474 - * @note If the input and output buffers overlap, the contents
   1.475 - *  of the output buffer at the end is undefined.
   1.476 - *
   1.477 - * @param out_param			Output parameter number to write to.  If 0, out_string is used.
   1.478 - * @param fmt_param			Format parameter number.  If 0, fmt_string is used.
   1.479 - * @param vararg_param		First variable parameter number.
   1.480 - * @param out_len			Output string buffer maximum length (always required).
   1.481 - * @param written			Optionally stores the number of bytes written.
   1.482 - * @param out_string		Output string buffer to use if out_param is not used.
   1.483 - * @param fmt_string		Format string to use if fmt_param is not used.
   1.484 - * @return					SP_ERROR_NONE on success, anything else on failure.
   1.485 - */
   1.486 -native FormatNativeString(out_param, 
   1.487 -						  fmt_param, 
   1.488 -						  vararg_param, 
   1.489 -						  out_len,
   1.490 -						  &written=0,
   1.491 -						  String:out_string[]="",
   1.492 -						  const String:fmt_string[]="");