core/ForwardSys.h
changeset 2411 8dfaa3d730b5
parent 2289 44045ead61cd
child 2883 1d7b415cef73
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/ForwardSys.h	Sun Nov 16 16:06:18 2008 -0800
     1.3 @@ -0,0 +1,158 @@
     1.4 +/**
     1.5 + * vim: set ts=4 :
     1.6 + * =============================================================================
     1.7 + * SourceMod
     1.8 + * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
     1.9 + * =============================================================================
    1.10 + *
    1.11 + * This program is free software; you can redistribute it and/or modify it under
    1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 + * Free Software Foundation.
    1.14 + * 
    1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 + * details.
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License along with
    1.21 + * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 + *
    1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 + * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 + * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 + * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 + * or <http://www.sourcemod.net/license.php>.
    1.31 + *
    1.32 + * Version: $Id$
    1.33 + */
    1.34 +
    1.35 +#ifndef _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
    1.36 +#define _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
    1.37 +
    1.38 +#include <IForwardSys.h>
    1.39 +#include <IPluginSys.h>
    1.40 +#include "sm_globals.h"
    1.41 +#include <sh_list.h>
    1.42 +#include <sh_stack.h>
    1.43 +#include "sourcemod.h"
    1.44 +
    1.45 +using namespace SourceHook;
    1.46 +
    1.47 +typedef List<IPluginFunction *>::iterator FuncIter;
    1.48 +
    1.49 +/* :TODO: a global name max define for sourcepawn, should mirror compiler's sNAMEMAX */
    1.50 +#define FORWARDS_NAME_MAX		64
    1.51 +
    1.52 +struct ByrefInfo
    1.53 +{
    1.54 +	unsigned int cells;
    1.55 +	cell_t *orig_addr;
    1.56 +	int flags;
    1.57 +	int sz_flags;
    1.58 +};
    1.59 +
    1.60 +struct FwdParamInfo
    1.61 +{
    1.62 +	cell_t val;
    1.63 +	ByrefInfo byref;
    1.64 +	ParamType pushedas;
    1.65 +};
    1.66 +
    1.67 +class CForward : public IChangeableForward
    1.68 +{
    1.69 +public: //ICallable
    1.70 +	virtual int PushCell(cell_t cell);
    1.71 +	virtual int PushCellByRef(cell_t *cell, int flags);
    1.72 +	virtual int PushFloat(float number);
    1.73 +	virtual int PushFloatByRef(float *number, int flags);
    1.74 +	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
    1.75 +	virtual int PushString(const char *string);
    1.76 +	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
    1.77 +	virtual void Cancel();
    1.78 +public: //IForward
    1.79 +	virtual const char *GetForwardName();
    1.80 +	virtual unsigned int GetFunctionCount();
    1.81 +	virtual ExecType GetExecType();
    1.82 +	virtual int Execute(cell_t *result, IForwardFilter *filter);
    1.83 +public: //IChangeableForward
    1.84 +	virtual bool RemoveFunction(IPluginFunction *func);
    1.85 +	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
    1.86 +	virtual bool AddFunction(IPluginFunction *func);
    1.87 +	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
    1.88 +	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
    1.89 +public:
    1.90 +	static CForward *CreateForward(const char *name, 
    1.91 +								   ExecType et, 
    1.92 +								   unsigned int num_params, 
    1.93 +								   const ParamType *types, 
    1.94 +								   va_list ap);
    1.95 +private:
    1.96 +	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
    1.97 +	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
    1.98 +	inline int SetError(int err)
    1.99 +	{
   1.100 +		m_errstate = err;
   1.101 +		return err;
   1.102 +	}
   1.103 +protected:
   1.104 +	/* :TODO: I want a caching list type here.
   1.105 +	 * Destroying these things and using new/delete for their members feels bad.
   1.106 +	 */
   1.107 +	mutable List<IPluginFunction *> m_functions;
   1.108 +	mutable List<IPluginFunction *> m_paused;
   1.109 +
   1.110 +	/* Type and name information */
   1.111 +	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
   1.112 +	ParamType m_types[SP_MAX_EXEC_PARAMS];
   1.113 +	char m_name[FORWARDS_NAME_MAX+1];
   1.114 +	unsigned int m_numparams;
   1.115 +	unsigned int m_varargs;
   1.116 +	ExecType m_ExecType;
   1.117 +
   1.118 +	/* State information */
   1.119 +	unsigned int m_curparam;
   1.120 +	int m_errstate;
   1.121 +};
   1.122 +
   1.123 +class CForwardManager : 
   1.124 +	public IForwardManager,
   1.125 +	public IPluginsListener,
   1.126 +	public SMGlobalClass
   1.127 +{
   1.128 +	friend class CForward;
   1.129 +public:
   1.130 +	~CForwardManager();
   1.131 +public: //IForwardManager
   1.132 +	IForward *CreateForward(const char *name, 
   1.133 +		ExecType et, 
   1.134 +		unsigned int num_params, 
   1.135 +		const ParamType *types, 
   1.136 +		...);
   1.137 +	IChangeableForward *CreateForwardEx(const char *name, 
   1.138 +		ExecType et, 
   1.139 +		int num_params, 
   1.140 +		const ParamType *types, 
   1.141 +		...);
   1.142 +	IForward *FindForward(const char *name, IChangeableForward **ifchng);
   1.143 +	void ReleaseForward(IForward *forward);
   1.144 +public: //IPluginsListener
   1.145 +	void OnPluginLoaded(IPlugin *plugin);
   1.146 +	void OnPluginUnloaded(IPlugin *plugin);
   1.147 +	void OnPluginPauseChange(IPlugin *plugin, bool paused);
   1.148 +public: //SMGlobalClass
   1.149 +	void OnSourceModAllInitialized();
   1.150 +protected:
   1.151 +	CForward *ForwardMake();
   1.152 +	void ForwardFree(CForward *fwd);
   1.153 +private:
   1.154 +	CStack<CForward *> m_FreeForwards;
   1.155 +	List<CForward *> m_managed;
   1.156 +	List<CForward *> m_unmanaged;
   1.157 +};
   1.158 +
   1.159 +extern CForwardManager g_Forwards;
   1.160 +
   1.161 +#endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_