core/ForwardSys.h
author David Anderson <dvander@alliedmods.net>
Sat Dec 19 18:16:41 2009 -0800 (2009-12-19)
changeset 2883 1d7b415cef73
parent 2411 8dfaa3d730b5
child 2890 54f2a62d6198
permissions -rw-r--r--
Add ability to change dontBroadcast status on hooked events (bug 3886, r=ds).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #ifndef _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
    33 #define _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
    34 
    35 #include <IForwardSys.h>
    36 #include <IPluginSys.h>
    37 #include "sm_globals.h"
    38 #include <sh_list.h>
    39 #include <sh_stack.h>
    40 #include "sourcemod.h"
    41 
    42 using namespace SourceHook;
    43 
    44 typedef List<IPluginFunction *>::iterator FuncIter;
    45 
    46 /* :TODO: a global name max define for sourcepawn, should mirror compiler's sNAMEMAX */
    47 #define FORWARDS_NAME_MAX		64
    48 
    49 struct ByrefInfo
    50 {
    51 	unsigned int cells;
    52 	cell_t *orig_addr;
    53 	int flags;
    54 	int sz_flags;
    55 };
    56 
    57 struct FwdParamInfo
    58 {
    59 	cell_t val;
    60 	ByrefInfo byref;
    61 	ParamType pushedas;
    62 };
    63 
    64 class SourceMod::IForwardFilter
    65 {
    66 public:
    67 	virtual void Preprocess(IPluginFunction *fun, FwdParamInfo *params)
    68 	{
    69 	}
    70 };
    71 
    72 class CForward : public IChangeableForward
    73 {
    74 public: //ICallable
    75 	virtual int PushCell(cell_t cell);
    76 	virtual int PushCellByRef(cell_t *cell, int flags);
    77 	virtual int PushFloat(float number);
    78 	virtual int PushFloatByRef(float *number, int flags);
    79 	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
    80 	virtual int PushString(const char *string);
    81 	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
    82 	virtual void Cancel();
    83 public: //IForward
    84 	virtual const char *GetForwardName();
    85 	virtual unsigned int GetFunctionCount();
    86 	virtual ExecType GetExecType();
    87 	virtual int Execute(cell_t *result, IForwardFilter *filter);
    88 public: //IChangeableForward
    89 	virtual bool RemoveFunction(IPluginFunction *func);
    90 	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
    91 	virtual bool AddFunction(IPluginFunction *func);
    92 	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
    93 	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
    94 public:
    95 	static CForward *CreateForward(const char *name, 
    96 								   ExecType et, 
    97 								   unsigned int num_params, 
    98 								   const ParamType *types, 
    99 								   va_list ap);
   100 private:
   101 	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
   102 	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
   103 	inline int SetError(int err)
   104 	{
   105 		m_errstate = err;
   106 		return err;
   107 	}
   108 protected:
   109 	/* :TODO: I want a caching list type here.
   110 	 * Destroying these things and using new/delete for their members feels bad.
   111 	 */
   112 	mutable List<IPluginFunction *> m_functions;
   113 	mutable List<IPluginFunction *> m_paused;
   114 
   115 	/* Type and name information */
   116 	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
   117 	ParamType m_types[SP_MAX_EXEC_PARAMS];
   118 	char m_name[FORWARDS_NAME_MAX+1];
   119 	unsigned int m_numparams;
   120 	unsigned int m_varargs;
   121 	ExecType m_ExecType;
   122 
   123 	/* State information */
   124 	unsigned int m_curparam;
   125 	int m_errstate;
   126 };
   127 
   128 class CForwardManager : 
   129 	public IForwardManager,
   130 	public IPluginsListener,
   131 	public SMGlobalClass
   132 {
   133 	friend class CForward;
   134 public:
   135 	~CForwardManager();
   136 public: //IForwardManager
   137 	IForward *CreateForward(const char *name, 
   138 		ExecType et, 
   139 		unsigned int num_params, 
   140 		const ParamType *types, 
   141 		...);
   142 	IChangeableForward *CreateForwardEx(const char *name, 
   143 		ExecType et, 
   144 		int num_params, 
   145 		const ParamType *types, 
   146 		...);
   147 	IForward *FindForward(const char *name, IChangeableForward **ifchng);
   148 	void ReleaseForward(IForward *forward);
   149 public: //IPluginsListener
   150 	void OnPluginLoaded(IPlugin *plugin);
   151 	void OnPluginUnloaded(IPlugin *plugin);
   152 	void OnPluginPauseChange(IPlugin *plugin, bool paused);
   153 public: //SMGlobalClass
   154 	void OnSourceModAllInitialized();
   155 protected:
   156 	CForward *ForwardMake();
   157 	void ForwardFree(CForward *fwd);
   158 private:
   159 	CStack<CForward *> m_FreeForwards;
   160 	List<CForward *> m_managed;
   161 	List<CForward *> m_unmanaged;
   162 };
   163 
   164 extern CForwardManager g_Forwards;
   165 
   166 #endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_