core/logic/ForwardSys.h
changeset 4139 de7d1cd00703
parent 4086 63763f2a5c6c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/logic/ForwardSys.h	Wed Oct 09 08:43:08 2013 -0400
     1.3 @@ -0,0 +1,189 @@
     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 "common_logic.h"
    1.41 +#include <sh_list.h>
    1.42 +#include <sh_stack.h>
    1.43 +#include "ISourceMod.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 +class FuncIteratorGuard
    1.53 +{
    1.54 +	bool triggered;
    1.55 +	FuncIteratorGuard **pprev;
    1.56 +	FuncIter *iter;
    1.57 +	FuncIteratorGuard *next;
    1.58 +public:
    1.59 +	FuncIteratorGuard(FuncIteratorGuard **pprev, FuncIter *iter)
    1.60 +		: triggered(false), pprev(pprev), iter(iter), next(*pprev)
    1.61 +	{
    1.62 +		*pprev = this;
    1.63 +	}
    1.64 +
    1.65 +	~FuncIteratorGuard()
    1.66 +	{
    1.67 +		*pprev = next;
    1.68 +	}
    1.69 +
    1.70 +	inline bool Triggered()
    1.71 +	{
    1.72 +		bool t = triggered;
    1.73 +		triggered = false;
    1.74 +		return t;
    1.75 +	}
    1.76 +
    1.77 +	/**
    1.78 +	 * This should not read from |this| before the NULL check, because FwdSys
    1.79 +	 * can call (NULL)->FixIteratorChain().
    1.80 +	 */
    1.81 +	void FixIteratorChain(FuncIter &other)
    1.82 +	{
    1.83 +		FuncIteratorGuard *guard = this;
    1.84 +		while (guard != NULL)
    1.85 +		{
    1.86 +			if (*guard->iter == other)
    1.87 +			{
    1.88 +				*(guard->iter) = ++(*(guard->iter));
    1.89 +				guard->triggered = true;
    1.90 +			}
    1.91 +			guard = guard->next;
    1.92 +		}
    1.93 +	}
    1.94 +};
    1.95 +
    1.96 +class CForward : public IChangeableForward
    1.97 +{
    1.98 +public: //ICallable
    1.99 +	virtual int PushCell(cell_t cell);
   1.100 +	virtual int PushCellByRef(cell_t *cell, int flags);
   1.101 +	virtual int PushFloat(float number);
   1.102 +	virtual int PushFloatByRef(float *number, int flags);
   1.103 +	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
   1.104 +	virtual int PushString(const char *string);
   1.105 +	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
   1.106 +	virtual void Cancel();
   1.107 +public: //IForward
   1.108 +	virtual const char *GetForwardName();
   1.109 +	virtual unsigned int GetFunctionCount();
   1.110 +	virtual ExecType GetExecType();
   1.111 +	virtual int Execute(cell_t *result, IForwardFilter *filter);
   1.112 +public: //IChangeableForward
   1.113 +	virtual bool RemoveFunction(IPluginFunction *func);
   1.114 +	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
   1.115 +	virtual bool AddFunction(IPluginFunction *func);
   1.116 +	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
   1.117 +	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
   1.118 +public:
   1.119 +	static CForward *CreateForward(const char *name, 
   1.120 +								   ExecType et, 
   1.121 +								   unsigned int num_params, 
   1.122 +								   const ParamType *types, 
   1.123 +								   va_list ap);
   1.124 +	bool IsFunctionRegistered(IPluginFunction *func);
   1.125 +private:
   1.126 +	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
   1.127 +	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
   1.128 +	inline int SetError(int err)
   1.129 +	{
   1.130 +		m_errstate = err;
   1.131 +		return err;
   1.132 +	}
   1.133 +protected:
   1.134 +	/* :TODO: I want a caching list type here.
   1.135 +	 * Destroying these things and using new/delete for their members feels bad.
   1.136 +	 */
   1.137 +	mutable List<IPluginFunction *> m_functions;
   1.138 +	mutable List<IPluginFunction *> m_paused;
   1.139 +	FuncIteratorGuard *m_IterGuard;
   1.140 +
   1.141 +	/* Type and name information */
   1.142 +	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
   1.143 +	ParamType m_types[SP_MAX_EXEC_PARAMS];
   1.144 +	char m_name[FORWARDS_NAME_MAX+1];
   1.145 +	unsigned int m_numparams;
   1.146 +	unsigned int m_varargs;
   1.147 +	ExecType m_ExecType;
   1.148 +
   1.149 +	/* State information */
   1.150 +	unsigned int m_curparam;
   1.151 +	int m_errstate;
   1.152 +};
   1.153 +
   1.154 +class CForwardManager : 
   1.155 +	public IForwardManager,
   1.156 +	public IPluginsListener,
   1.157 +	public SMGlobalClass
   1.158 +{
   1.159 +	friend class CForward;
   1.160 +public:
   1.161 +	~CForwardManager();
   1.162 +public: //IForwardManager
   1.163 +	IForward *CreateForward(const char *name, 
   1.164 +		ExecType et, 
   1.165 +		unsigned int num_params, 
   1.166 +		const ParamType *types, 
   1.167 +		...);
   1.168 +	IChangeableForward *CreateForwardEx(const char *name, 
   1.169 +		ExecType et, 
   1.170 +		int num_params, 
   1.171 +		const ParamType *types, 
   1.172 +		...);
   1.173 +	IForward *FindForward(const char *name, IChangeableForward **ifchng);
   1.174 +	void ReleaseForward(IForward *forward);
   1.175 +public: //IPluginsListener
   1.176 +	void OnPluginLoaded(IPlugin *plugin);
   1.177 +	void OnPluginUnloaded(IPlugin *plugin);
   1.178 +	void OnPluginPauseChange(IPlugin *plugin, bool paused);
   1.179 +public: //SMGlobalClass
   1.180 +	void OnSourceModAllInitialized();
   1.181 +protected:
   1.182 +	CForward *ForwardMake();
   1.183 +	void ForwardFree(CForward *fwd);
   1.184 +private:
   1.185 +	CStack<CForward *> m_FreeForwards;
   1.186 +	List<CForward *> m_managed;
   1.187 +	List<CForward *> m_unmanaged;
   1.188 +};
   1.189 +
   1.190 +extern CForwardManager g_Forwards;
   1.191 +
   1.192 +#endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_