public/IForwardSys.h
author David Anderson <dvander@alliedmods.net>
Mon Aug 12 00:24:02 2013 +0100 (2013-08-12)
changeset 3993 d06d6dacfa18
parent 2289 44045ead61cd
child 4139 de7d1cd00703
permissions -rw-r--r--
Remove InfoVars, reassign DAT from EBP to ESI to preserve C++ stacktraces in JIT code (bug 5844 part 10, r=ds).
dvander@372
     1
/**
dvander@375
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1226
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@372
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@379
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1226
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1226
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@372
    28
 *
dvander@372
    29
 * Version: $Id$
dvander@372
    30
 */
dvander@372
    31
dvander@324
    32
#ifndef _INCLUDE_SOURCEMOD_FORWARDINTERFACE_H_
dvander@324
    33
#define _INCLUDE_SOURCEMOD_FORWARDINTERFACE_H_
dvander@324
    34
dvander@372
    35
/**
dvander@372
    36
 * @file IForwardSys.h
dvander@372
    37
 * @brief Defines the interface for managing collections ("forwards") of plugin calls.
dvander@372
    38
 *
dvander@372
    39
 *   The Forward System is responsible for managing automated collections of IPluginFunctions.  
dvander@372
    40
 * It thus provides wrappers to calling many functions at once.  There are two types of such 
dvander@372
    41
 * wrappers: Managed and Unmanaged.  Confusingly, these terms refer to whether the user manages
dvander@372
    42
 * the forwards, not Core.  Managed forwards are completely managed by the user, and are custom
dvander@372
    43
 * editable collections.  Unmanaged forwards are the opposite, and will only work on a single global
dvander@372
    44
 * function name in all plugins.
dvander@372
    45
 */
dvander@372
    46
dvander@324
    47
#include <IPluginSys.h>
dvander@327
    48
#include <sp_vm_api.h>
dvander@327
    49
dvander@327
    50
using namespace SourcePawn;
dvander@324
    51
dvander@324
    52
#define SMINTERFACE_FORWARDMANAGER_NAME		"IForwardManager"
ds@2182
    53
#define SMINTERFACE_FORWARDMANAGER_VERSION	3
dvander@324
    54
dvander@372
    55
/*
dvander@324
    56
 * There is some very important documentation at the bottom of this file.
dvander@324
    57
 * Readers interested in knowing more about the forward system, scrolling down is a must!
dvander@324
    58
 */
dvander@324
    59
dvander@324
    60
namespace SourceMod
dvander@324
    61
{
dvander@372
    62
	/**
dvander@372
    63
	 * @brief Defines the event hook result types plugins can return.
dvander@372
    64
	 */
dvander@324
    65
	enum ResultType
dvander@324
    66
	{
dvander@372
    67
		Pl_Continue = 0,	/**< No result */
faluco@1735
    68
		Pl_Changed = 1,		/**< Inputs or outputs have been overridden with new values */
dvander@481
    69
		Pl_Handled = 3,		/**< Result was handled, stop at the end */
dvander@481
    70
		Pl_Stop = 4,		/**< Result was handled, stop now */
dvander@324
    71
	};
dvander@324
    72
dvander@372
    73
	/**
dvander@372
    74
	 * @brief Defines how a forward iterates through plugin functions.
dvander@372
    75
	 */
dvander@324
    76
	enum ExecType
dvander@324
    77
	{
dvander@372
    78
		ET_Ignore = 0,		/**< Ignore all return values, return 0 */
dvander@372
    79
		ET_Single = 1,		/**< Only return the last exec, ignore all others */
dvander@372
    80
		ET_Event = 2,		/**< Acts as an event with the ResultTypes above, no mid-Stops allowed, returns highest */
dvander@372
    81
		ET_Hook = 3,		/**< Acts as a hook with the ResultTypes above, mid-Stops allowed, returns highest */
ds@2181
    82
		ET_LowEvent = 4,	/**< Same as ET_Event except that it returns the lowest value */
dvander@324
    83
	};
dvander@324
    84
dvander@324
    85
	class IForward;
ds@2181
    86
	class IForwardFilter;
dvander@324
    87
dvander@324
    88
	/**
dvander@372
    89
	 * @brief Unmanaged Forward, abstracts calling multiple functions as "forwards," or collections of functions.
dvander@324
    90
	 * 
dvander@372
    91
	 *  Parameters should be pushed in forward order, unlike the virtual machine/IPluginContext order.
dvander@372
    92
	 * Some functions are repeated in here because their documentation differs from their IPluginFunction equivalents.
dvander@372
    93
	 * Missing are the Push functions, whose only doc change is that they throw SP_ERROR_PARAM on type mismatches.
dvander@324
    94
	 */
dvander@324
    95
	class IForward : public ICallable
dvander@324
    96
	{
dvander@324
    97
	public:
dvander@372
    98
		/** Virtual Destructor */
dvander@324
    99
		virtual ~IForward()
dvander@324
   100
		{
dvander@324
   101
		}
dvander@324
   102
	public:
dvander@324
   103
		/**
dvander@324
   104
		 * @brief Returns the name of the forward.
dvander@324
   105
		 *
dvander@324
   106
		 * @return			Forward name.
dvander@324
   107
		 */
dvander@615
   108
		virtual const char *GetForwardName() =0;
dvander@324
   109
dvander@324
   110
		/**
dvander@324
   111
		 * @brief Returns the number of functions in this forward.
dvander@324
   112
		 *
dvander@324
   113
		 * @return			Number of functions in forward.
dvander@324
   114
		 */
dvander@615
   115
		virtual unsigned int GetFunctionCount() =0;
dvander@324
   116
dvander@324
   117
		/**
dvander@324
   118
		 * @brief Returns the method of multi-calling this forward has.
dvander@324
   119
		 *
dvander@324
   120
		 * @return			ExecType of the forward.
dvander@324
   121
		 */
dvander@615
   122
		virtual ExecType GetExecType() =0;
dvander@324
   123
dvander@324
   124
		/**
dvander@324
   125
		 * @brief Executes the forward.
dvander@324
   126
		 *
dvander@447
   127
		 * @param result		Optional pointer to store result in.
ds@2181
   128
		 * @param filter		Do not use.
dvander@324
   129
		 * @return				Error code, if any.
dvander@324
   130
		 */
dvander@324
   131
		virtual int Execute(cell_t *result, IForwardFilter *filter=NULL) =0;
dvander@324
   132
dvander@324
   133
		/**
dvander@324
   134
		 * @brief Pushes an array of cells onto the current call.  Different rules than ICallable.
dvander@324
   135
		 * NOTE: On Execute, the pointer passed will be modified according to the copyback rule.
dvander@324
   136
		 *
dvander@324
   137
		 * @param inarray	Array to copy.  Cannot be NULL, unlike ICallable's version.
dvander@324
   138
		 * @param cells		Number of cells to allocate and optionally read from the input array.
dvander@324
   139
		 * @param flags		Whether or not changes should be copied back to the input array.
dvander@324
   140
		 * @return			Error code, if any.
dvander@324
   141
		 */
dvander@589
   142
		virtual int PushArray(cell_t *inarray, unsigned int cells, int flags=0) =0;
dvander@324
   143
	};
dvander@324
   144
dvander@372
   145
	/**
dvander@372
   146
	 * @brief Managed Forward, same as IForward, except the collection can be modified.
dvander@372
   147
	 */
dvander@324
   148
	class IChangeableForward : public IForward
dvander@324
   149
	{
dvander@324
   150
	public:
dvander@324
   151
		/** 
dvander@324
   152
		 * @brief Removes a function from the call list.
dvander@324
   153
		 * NOTE: Only removes one instance.
dvander@324
   154
		 *
dvander@324
   155
		 * @param func		Function to remove.
dvander@324
   156
		 * @return			Whether or not the function was removed.
dvander@324
   157
		 */
dvander@324
   158
		virtual bool RemoveFunction(IPluginFunction *func) =0;
dvander@324
   159
dvander@324
   160
		/** 
dvander@324
   161
		 * @brief Removes all instances of a plugin from the call list.
dvander@324
   162
		 *
dvander@324
   163
		 * @param plugin	Plugin to remove instances of.
dvander@324
   164
		 * @return			Number of functions removed therein.
dvander@324
   165
		 */
dvander@324
   166
		virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin) =0;
dvander@324
   167
dvander@324
   168
		/**
dvander@324
   169
		 * @brief Adds a function to the call list.
ds@729
   170
		 * NOTE: Cannot be used during an incomplete call.
dvander@324
   171
		 * NOTE: If used during a call, function is temporarily queued until calls are over.
ds@729
   172
		 * NOTE: Adding multiple copies of the same function is illegal.
dvander@324
   173
		 *
dvander@324
   174
		 * @param func		Function to add.
dvander@324
   175
		 * @return			True on success, otherwise false.
dvander@324
   176
		 */
dvander@324
   177
		virtual bool AddFunction(IPluginFunction *func) =0;
dvander@324
   178
dvander@324
   179
		/**
dvander@324
   180
		 * @brief Adds a function to the call list.
ds@729
   181
		 * NOTE: Cannot be used during an incomplete call.
dvander@324
   182
		 * NOTE: If used during a call, function is temporarily queued until calls are over.
dvander@324
   183
		 *
dvander@324
   184
		 * @param ctx		Context to use as a look-up.
dvander@372
   185
		 * @param index		Function id to add.
dvander@324
   186
		 * @return			True on success, otherwise false.
dvander@324
   187
		 */
dvander@327
   188
		virtual bool AddFunction(IPluginContext *ctx, funcid_t index) =0;
ds@621
   189
ds@621
   190
		/** 
faluco@682
   191
		 * @brief Removes a function from the call list.
faluco@682
   192
		 * NOTE: Only removes one instance.
faluco@682
   193
		 *
faluco@682
   194
		 * @param ctx		Context to use as a look-up.
faluco@682
   195
		 * @param index		Function id to add.
faluco@682
   196
		 * @return			Whether or not the function was removed.
faluco@682
   197
		 */
ds@621
   198
		virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index) =0;
dvander@324
   199
	};
dvander@324
   200
dvander@324
   201
	#define SP_PARAMTYPE_ANY	0
dvander@324
   202
	#define SP_PARAMFLAG_BYREF	(1<<0)
dvander@324
   203
	#define SP_PARAMTYPE_CELL	(1<<1)
dvander@324
   204
	#define SP_PARAMTYPE_FLOAT	(2<<1)
dvander@324
   205
	#define SP_PARAMTYPE_STRING	(3<<1)|SP_PARAMFLAG_BYREF
dvander@324
   206
	#define SP_PARAMTYPE_ARRAY	(4<<1)|SP_PARAMFLAG_BYREF
dvander@324
   207
	#define SP_PARAMTYPE_VARARG	(5<<1)
dvander@324
   208
dvander@372
   209
	/**
dvander@372
   210
	 * @brief Describes the various ways to pass parameters to plugins.
dvander@372
   211
	 */
dvander@324
   212
	enum ParamType
dvander@324
   213
	{
dvander@372
   214
		Param_Any = SP_PARAMTYPE_ANY,				/**< Any data type can be pushed */
dvander@372
   215
		Param_Cell = SP_PARAMTYPE_CELL,				/**< Only basic cells can be pushed */
dvander@372
   216
		Param_Float = SP_PARAMTYPE_FLOAT,			/**< Only floats can be pushed */
dvander@372
   217
		Param_String = SP_PARAMTYPE_STRING,			/**< Only strings can be pushed */
dvander@372
   218
		Param_Array = SP_PARAMTYPE_ARRAY,			/**< Only arrays can be pushed */
dvander@372
   219
		Param_VarArgs = SP_PARAMTYPE_VARARG,		/**< Same as "..." in plugins, anything can be pushed, but it will always be byref */
dvander@372
   220
		Param_CellByRef = SP_PARAMTYPE_CELL|SP_PARAMFLAG_BYREF,		/**< Only a cell by reference can be pushed */
dvander@372
   221
		Param_FloatByRef = SP_PARAMTYPE_FLOAT|SP_PARAMFLAG_BYREF,	/**< Only a float by reference can be pushed */
dvander@324
   222
	};
dvander@324
   223
	
dvander@372
   224
	/**
dvander@372
   225
	 * @brief Provides functions for creating/destroying managed and unmanaged forwards.
dvander@372
   226
	 */
dvander@324
   227
	class IForwardManager : public SMInterface
dvander@324
   228
	{
dvander@324
   229
	public:
dvander@324
   230
		virtual const char *GetInterfaceName()
dvander@324
   231
		{
dvander@324
   232
			return SMINTERFACE_FORWARDMANAGER_NAME;
dvander@324
   233
		}
dvander@324
   234
		virtual unsigned int GetInterfaceVersion()
dvander@324
   235
		{
dvander@324
   236
			return SMINTERFACE_FORWARDMANAGER_VERSION;
dvander@324
   237
		}
dvander@589
   238
		virtual bool IsVersionCompatible(unsigned int version)
dvander@589
   239
		{
dvander@589
   240
			if (version < 2 || version > GetInterfaceVersion())
dvander@589
   241
			{
dvander@589
   242
				return false;
dvander@589
   243
			}
dvander@589
   244
			return true;
dvander@589
   245
		}
dvander@324
   246
	public:
dvander@324
   247
		/**
dvander@324
   248
		 * @brief Creates a managed forward.  This forward exists globally.
dvander@324
   249
		 *  The name used to create the forward is used as its public function in all target plugins.
dvander@324
   250
		 *  As new non-private plugins become loaded or unloaded, they will be automatically added
dvander@324
   251
		 * or removed.  This is ideal for global, static forwards that are never changed.
dvander@324
   252
		 *
dvander@324
   253
		 * @param name			Name of public function to use in forward.
dvander@324
   254
		 * @param et			Execution type to be used.
dvander@324
   255
		 * @param num_params	Number of parameter this function will have.
dvander@324
   256
		 *						NOTE: For varargs, this should include the vararg parameter.
dvander@324
   257
		 * @param types			Array of type information about each parameter.  If NULL, types
dvander@324
   258
		 *						are read off the vararg stream.
dvander@324
   259
		 * @param ...			If types is NULL, num_params ParamTypes should be pushed.
dvander@324
   260
		 * @return				A new IForward on success, NULL if type combination is impossible.
dvander@324
   261
		 */
dvander@324
   262
		virtual IForward *CreateForward(const char *name, 
dvander@324
   263
										ExecType et, 
dvander@324
   264
										unsigned int num_params, 
ds@559
   265
										const ParamType *types, 
dvander@324
   266
										...) =0;
dvander@324
   267
dvander@324
   268
		/**
dvander@324
   269
		 * @brief Creates an unmanaged forward.  This forward exists privately.
dvander@324
   270
		 * Unlike managed forwards, no functions are ever added by the Manager.
dvander@324
   271
		 * However, functions will be removed automatically if their parent plugin is unloaded.
dvander@324
   272
		 *
dvander@324
   273
		 * @param name			Name of forward (unused except for lookup, can be NULL for anonymous).
dvander@324
   274
		 * @param et			Execution type to be used.
dvander@324
   275
		 * @param num_params	Number of parameter this function will have.
dvander@324
   276
		 *						NOTE: For varargs, this should include the vararg parameter.
dvander@324
   277
		 * @param types			Array of type information about each parameter.  If NULL, types
dvander@324
   278
		 *						are read off the vararg stream.
dvander@324
   279
		 * @param ...			If types is NULL, num_params ParamTypes should be pushed.
dvander@324
   280
		 * @return				A new IChangeableForward on success, NULL if type combination is impossible.
dvander@324
   281
		 */
dvander@324
   282
		virtual IChangeableForward *CreateForwardEx(const char *name, 
dvander@324
   283
													ExecType et, 
dvander@324
   284
													int num_params, 
ds@559
   285
													const ParamType *types, 
dvander@324
   286
													...) =0;
dvander@324
   287
dvander@324
   288
		/**
dvander@324
   289
		 * @brief Finds a forward by name.  Does not return anonymous forwards (named NULL or "").
dvander@324
   290
		 *
dvander@324
   291
		 * @param name			Name of forward.
dvander@324
   292
		 * @param ifchng		Optionally store either NULL or an IChangeableForward pointer
dvander@324
   293
		 *                      depending on type of forward.
dvander@324
   294
		 * @return				IForward pointer, or NULL if none found matching the name.
dvander@324
   295
		 */
dvander@324
   296
		virtual IForward *FindForward(const char *name, IChangeableForward **ifchng) =0;
dvander@324
   297
dvander@324
   298
		/**
dvander@324
   299
		 * @brief Frees and destroys a forward object.
dvander@324
   300
		 *
dvander@324
   301
		 * @param forward		An IForward created by CreateForward() or CreateForwardEx().
dvander@324
   302
		 */
dvander@324
   303
		virtual void ReleaseForward(IForward *forward) =0;
dvander@324
   304
	};
ds@432
   305
}
dvander@324
   306
dvander@372
   307
/*
dvander@324
   308
 *  In the AMX Mod X model of forwarding, each forward contained a list of pairs, each pair containing
dvander@324
   309
 * a function ID and an AMX structure.  The forward structure itself did very little but hold parameter types.
dvander@324
   310
 * An execution call worked like this:
dvander@324
   311
 *  - executeForward() took in a function id and a list of parameters
dvander@324
   312
 *  - for each contained plugin:
dvander@324
   313
 *   - the list of parameters was preprocessed and pushed
dvander@324
   314
 *   - the call was made
dvander@324
   315
 *   - the list was freed and copybacks were made
dvander@324
   316
 *  - return
dvander@324
   317
 * 
dvander@324
   318
 *  The advantages to this is that the system is very easy to implement, and it's fast. The disadvantage is 
dvander@324
   319
 * varargs tend to be very unforgiving and inflexible, and thus weird problems arose with casting.  You also 
dvander@324
   320
 * lose flexibility, type checking, and the ability to reasonably use variable arguments lists in the VM.
dvander@324
   321
 *
dvander@324
   322
 *  SourceMod replaces this forward system with a far more advanced, but a bit bulkier one. The idea is that 
dvander@324
   323
 * each plugin has a table of functions, and each function is an ICallable object.  As well as being an ICallable, 
dvander@324
   324
 * each function is an IPluginFunction.  An ICallable simply describes the process of adding parameters to a 
dvander@324
   325
 * function call.  An IPluginFunction describes the process of actually calling a function and performing allocation, 
dvander@324
   326
 * copybacks, and deallocations.
dvander@324
   327
 *
dvander@324
   328
 * A very powerful forward system emerges: a Forward is just a collection of IPluginFunctions.  Thus, the same 
dvander@324
   329
 * API can be easily wrapped around a simple list, and it will look transparent to the user.
dvander@324
   330
 * Advantages:
dvander@324
   331
 * 1) "SP Forwards" from AMX Mod X are simply IPluginFunctions without a collection.
dvander@324
   332
 * 2) Forwards are function based, rather than plugin based, and are thus far more flexible at runtime..
dvander@324
   333
 * 3) [2] Individual functions can be paused and more than one function from the same plugin can be hooked.
dvander@324
   334
 * 4) [2] One hook type that used to map to many SP Forwards can now be centralized as one Forward.
dvander@324
   335
 *    This helps alleviate messes like Fakemeta.
dvander@324
   336
 * 5) Parameter pushing is type-checked and allows for variable arguments.
dvander@324
   337
 *
dvander@324
   338
 *  Note that while #2,3,4 could be added to AMX Mod X, the real binding property is #1, which makes the system
dvander@324
   339
 * object oriented, rather than AMX Mod X, which hides the objects behind static functions.  It is entirely a design
dvander@324
   340
 * issue, rather than a usability one.  The interesting part is when it gets to implementation, which has to cache
dvander@324
   341
 * parameter pushing until execution.  Without this, multiple function calls can be started across one plugin, which
dvander@324
   342
 * will result in heap corruption given SourcePawn's implementation.
dvander@324
   343
 *
dvander@324
   344
 * Observe the new calling process:
dvander@324
   345
 * - Each parameter is pushed into a local cache using the ICallable interface.
dvander@324
   346
 * - For each function in the collection:
dvander@324
   347
 *  - Each parameter is decoded and -pushed into the function.
dvander@324
   348
 *  - The call is made.
dvander@324
   349
 * - Return
dvander@324
   350
 *
dvander@324
   351
 *  Astute readers will note the (minor) problems:
dvander@324
   352
 * 1) More memory is used.  Specifically, rather than N params of memory, you now have N params * M plugins.
dvander@324
   353
 *    This is because, again, parameters are cached both per-function and per-forward.
dvander@324
   354
 * 2) There are slightly more calls going around: one extra call for each parameter, since each push is manual.
dvander@324
   355
 *
dvander@324
   356
 * HISTORICAL NOTES:
dvander@324
   357
 *  There used to be a # about copy backs.  
dvander@324
   358
 *  Note that originally, the Forward implementation was a thin wrapper around IForwards.  It did not cache pushes,
dvander@324
   359
 * and instead immediately fired them to each internal plugin.  This was to allow users to know that pointers would 
dvander@324
   360
 * be immediately resolved.  Unfortunately, this became extremely burdensome on the API and exposed many problems,
dvander@324
   361
 * the major (and breaking) one was that two separate Function objects cannot be in a calling process on the same 
dvander@324
   362
 * plugin at once. (:TODO: perhaps prevent that in the IPlugin object?) This is because heap functions lose their order
ds@432
   363
 * and become impossible to re-arrange without some global heap tracking mechanism.  It also made iterative copy backs 
dvander@324
   364
 * for arrays/references overwhelmingly complex, since each plugin had to have its memory back-patched for each copy.
dvander@324
   365
 *  Therefore, this was scrapped for cached parameters (current implementation), which is the implementation AMX Mod X 
dvander@324
   366
 * uses.  It is both faster and works better.
dvander@324
   367
 */
dvander@324
   368
dvander@324
   369
#endif //_INCLUDE_SOURCEMOD_FORWARDINTERFACE_H_