core/logic/ForwardSys.h
author Scott Ehlert <ds@alliedmods.net>
Sun Feb 09 22:50:20 2014 -0600 (2014-02-09)
changeset 4241 46fdb8cd9883
parent 4139 de7d1cd00703
permissions -rw-r--r--
Use == instead of is operator for checking string equality in AMBuild scripts (bug 6043, r=dvander).
     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 "common_logic.h"
    38 #include <sh_list.h>
    39 #include <sh_stack.h>
    40 #include "ISourceMod.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 class FuncIteratorGuard
    50 {
    51 	bool triggered;
    52 	FuncIteratorGuard **pprev;
    53 	FuncIter *iter;
    54 	FuncIteratorGuard *next;
    55 public:
    56 	FuncIteratorGuard(FuncIteratorGuard **pprev, FuncIter *iter)
    57 		: triggered(false), pprev(pprev), iter(iter), next(*pprev)
    58 	{
    59 		*pprev = this;
    60 	}
    61 
    62 	~FuncIteratorGuard()
    63 	{
    64 		*pprev = next;
    65 	}
    66 
    67 	inline bool Triggered()
    68 	{
    69 		bool t = triggered;
    70 		triggered = false;
    71 		return t;
    72 	}
    73 
    74 	/**
    75 	 * This should not read from |this| before the NULL check, because FwdSys
    76 	 * can call (NULL)->FixIteratorChain().
    77 	 */
    78 	void FixIteratorChain(FuncIter &other)
    79 	{
    80 		FuncIteratorGuard *guard = this;
    81 		while (guard != NULL)
    82 		{
    83 			if (*guard->iter == other)
    84 			{
    85 				*(guard->iter) = ++(*(guard->iter));
    86 				guard->triggered = true;
    87 			}
    88 			guard = guard->next;
    89 		}
    90 	}
    91 };
    92 
    93 class CForward : public IChangeableForward
    94 {
    95 public: //ICallable
    96 	virtual int PushCell(cell_t cell);
    97 	virtual int PushCellByRef(cell_t *cell, int flags);
    98 	virtual int PushFloat(float number);
    99 	virtual int PushFloatByRef(float *number, int flags);
   100 	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
   101 	virtual int PushString(const char *string);
   102 	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
   103 	virtual void Cancel();
   104 public: //IForward
   105 	virtual const char *GetForwardName();
   106 	virtual unsigned int GetFunctionCount();
   107 	virtual ExecType GetExecType();
   108 	virtual int Execute(cell_t *result, IForwardFilter *filter);
   109 public: //IChangeableForward
   110 	virtual bool RemoveFunction(IPluginFunction *func);
   111 	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
   112 	virtual bool AddFunction(IPluginFunction *func);
   113 	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
   114 	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
   115 public:
   116 	static CForward *CreateForward(const char *name, 
   117 								   ExecType et, 
   118 								   unsigned int num_params, 
   119 								   const ParamType *types, 
   120 								   va_list ap);
   121 	bool IsFunctionRegistered(IPluginFunction *func);
   122 private:
   123 	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
   124 	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
   125 	inline int SetError(int err)
   126 	{
   127 		m_errstate = err;
   128 		return err;
   129 	}
   130 protected:
   131 	/* :TODO: I want a caching list type here.
   132 	 * Destroying these things and using new/delete for their members feels bad.
   133 	 */
   134 	mutable List<IPluginFunction *> m_functions;
   135 	mutable List<IPluginFunction *> m_paused;
   136 	FuncIteratorGuard *m_IterGuard;
   137 
   138 	/* Type and name information */
   139 	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
   140 	ParamType m_types[SP_MAX_EXEC_PARAMS];
   141 	char m_name[FORWARDS_NAME_MAX+1];
   142 	unsigned int m_numparams;
   143 	unsigned int m_varargs;
   144 	ExecType m_ExecType;
   145 
   146 	/* State information */
   147 	unsigned int m_curparam;
   148 	int m_errstate;
   149 };
   150 
   151 class CForwardManager : 
   152 	public IForwardManager,
   153 	public IPluginsListener,
   154 	public SMGlobalClass
   155 {
   156 	friend class CForward;
   157 public:
   158 	~CForwardManager();
   159 public: //IForwardManager
   160 	IForward *CreateForward(const char *name, 
   161 		ExecType et, 
   162 		unsigned int num_params, 
   163 		const ParamType *types, 
   164 		...);
   165 	IChangeableForward *CreateForwardEx(const char *name, 
   166 		ExecType et, 
   167 		int num_params, 
   168 		const ParamType *types, 
   169 		...);
   170 	IForward *FindForward(const char *name, IChangeableForward **ifchng);
   171 	void ReleaseForward(IForward *forward);
   172 public: //IPluginsListener
   173 	void OnPluginLoaded(IPlugin *plugin);
   174 	void OnPluginUnloaded(IPlugin *plugin);
   175 	void OnPluginPauseChange(IPlugin *plugin, bool paused);
   176 public: //SMGlobalClass
   177 	void OnSourceModAllInitialized();
   178 protected:
   179 	CForward *ForwardMake();
   180 	void ForwardFree(CForward *fwd);
   181 private:
   182 	CStack<CForward *> m_FreeForwards;
   183 	List<CForward *> m_managed;
   184 	List<CForward *> m_unmanaged;
   185 };
   186 
   187 extern CForwardManager g_Forwards;
   188 
   189 #endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_