core/ForwardSys.h
author David Anderson <dvander@alliedmods.net>
Fri Aug 30 10:16:28 2013 -0700 (2013-08-30)
changeset 4109 10eb68d668af
parent 4086 63763f2a5c6c
permissions -rw-r--r--
Use Refcounted to manage to NativeEntry (bug 5852 part 4, 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 FuncIteratorGuard
    73 {
    74 	bool triggered;
    75 	FuncIteratorGuard **pprev;
    76 	FuncIter *iter;
    77 	FuncIteratorGuard *next;
    78 public:
    79 	FuncIteratorGuard(FuncIteratorGuard **pprev, FuncIter *iter)
    80 		: triggered(false), pprev(pprev), iter(iter), next(*pprev)
    81 	{
    82 		*pprev = this;
    83 	}
    84 
    85 	~FuncIteratorGuard()
    86 	{
    87 		*pprev = next;
    88 	}
    89 
    90 	inline bool Triggered()
    91 	{
    92 		bool t = triggered;
    93 		triggered = false;
    94 		return t;
    95 	}
    96 
    97 	/**
    98 	 * This should not read from |this| before the NULL check, because FwdSys
    99 	 * can call (NULL)->FixIteratorChain().
   100 	 */
   101 	void FixIteratorChain(FuncIter &other)
   102 	{
   103 		FuncIteratorGuard *guard = this;
   104 		while (guard != NULL)
   105 		{
   106 			if (*guard->iter == other)
   107 			{
   108 				*(guard->iter) = ++(*(guard->iter));
   109 				guard->triggered = true;
   110 			}
   111 			guard = guard->next;
   112 		}
   113 	}
   114 };
   115 
   116 class CForward : public IChangeableForward
   117 {
   118 public: //ICallable
   119 	virtual int PushCell(cell_t cell);
   120 	virtual int PushCellByRef(cell_t *cell, int flags);
   121 	virtual int PushFloat(float number);
   122 	virtual int PushFloatByRef(float *number, int flags);
   123 	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
   124 	virtual int PushString(const char *string);
   125 	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
   126 	virtual void Cancel();
   127 public: //IForward
   128 	virtual const char *GetForwardName();
   129 	virtual unsigned int GetFunctionCount();
   130 	virtual ExecType GetExecType();
   131 	virtual int Execute(cell_t *result, IForwardFilter *filter);
   132 public: //IChangeableForward
   133 	virtual bool RemoveFunction(IPluginFunction *func);
   134 	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
   135 	virtual bool AddFunction(IPluginFunction *func);
   136 	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
   137 	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
   138 public:
   139 	static CForward *CreateForward(const char *name, 
   140 								   ExecType et, 
   141 								   unsigned int num_params, 
   142 								   const ParamType *types, 
   143 								   va_list ap);
   144 	bool IsFunctionRegistered(IPluginFunction *func);
   145 private:
   146 	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
   147 	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
   148 	inline int SetError(int err)
   149 	{
   150 		m_errstate = err;
   151 		return err;
   152 	}
   153 protected:
   154 	/* :TODO: I want a caching list type here.
   155 	 * Destroying these things and using new/delete for their members feels bad.
   156 	 */
   157 	mutable List<IPluginFunction *> m_functions;
   158 	mutable List<IPluginFunction *> m_paused;
   159 	FuncIteratorGuard *m_IterGuard;
   160 
   161 	/* Type and name information */
   162 	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
   163 	ParamType m_types[SP_MAX_EXEC_PARAMS];
   164 	char m_name[FORWARDS_NAME_MAX+1];
   165 	unsigned int m_numparams;
   166 	unsigned int m_varargs;
   167 	ExecType m_ExecType;
   168 
   169 	/* State information */
   170 	unsigned int m_curparam;
   171 	int m_errstate;
   172 };
   173 
   174 class CForwardManager : 
   175 	public IForwardManager,
   176 	public IPluginsListener,
   177 	public SMGlobalClass
   178 {
   179 	friend class CForward;
   180 public:
   181 	~CForwardManager();
   182 public: //IForwardManager
   183 	IForward *CreateForward(const char *name, 
   184 		ExecType et, 
   185 		unsigned int num_params, 
   186 		const ParamType *types, 
   187 		...);
   188 	IChangeableForward *CreateForwardEx(const char *name, 
   189 		ExecType et, 
   190 		int num_params, 
   191 		const ParamType *types, 
   192 		...);
   193 	IForward *FindForward(const char *name, IChangeableForward **ifchng);
   194 	void ReleaseForward(IForward *forward);
   195 public: //IPluginsListener
   196 	void OnPluginLoaded(IPlugin *plugin);
   197 	void OnPluginUnloaded(IPlugin *plugin);
   198 	void OnPluginPauseChange(IPlugin *plugin, bool paused);
   199 public: //SMGlobalClass
   200 	void OnSourceModAllInitialized();
   201 protected:
   202 	CForward *ForwardMake();
   203 	void ForwardFree(CForward *fwd);
   204 private:
   205 	CStack<CForward *> m_FreeForwards;
   206 	List<CForward *> m_managed;
   207 	List<CForward *> m_unmanaged;
   208 };
   209 
   210 extern CForwardManager g_Forwards;
   211 
   212 #endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_