core/logic/ForwardSys.h
author Nicholas Hastings <skamonkey@gmail.com>
Wed May 21 17:25:30 2014 -0400 (2014-05-21)
changeset 4377 7a44be5b7162
parent 4139 de7d1cd00703
permissions -rw-r--r--
Trigger build for hl2sdk-l4d changes.
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
ds@1947
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1330
     6
 * =============================================================================
dvander@370
     7
 *
ds@1330
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1330
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1330
    10
 * Free Software Foundation.
dvander@1231
    11
 * 
ds@1330
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1330
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1330
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1330
    15
 * details.
dvander@1231
    16
 *
ds@1330
    17
 * You should have received a copy of the GNU General Public License along with
ds@1330
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
dvander@1231
    19
 *
ds@1330
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1330
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1330
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1330
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1330
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1330
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1330
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1330
    27
 * or <http://www.sourcemod.net/license.php>.
dvander@370
    28
 *
dvander@370
    29
 * Version: $Id$
dvander@370
    30
 */
dvander@370
    31
dvander@180
    32
#ifndef _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
dvander@180
    33
#define _INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_
dvander@180
    34
dvander@180
    35
#include <IForwardSys.h>
dvander@180
    36
#include <IPluginSys.h>
skamonkey@4139
    37
#include "common_logic.h"
dvander@180
    38
#include <sh_list.h>
dvander@180
    39
#include <sh_stack.h>
skamonkey@4139
    40
#include "ISourceMod.h"
dvander@180
    41
dvander@180
    42
using namespace SourceHook;
dvander@180
    43
dvander@180
    44
typedef List<IPluginFunction *>::iterator FuncIter;
dvander@180
    45
ds@2181
    46
/* :TODO: a global name max define for sourcepawn, should mirror compiler's sNAMEMAX */
dvander@180
    47
#define FORWARDS_NAME_MAX		64
dvander@180
    48
dvander@2890
    49
class FuncIteratorGuard
dvander@2890
    50
{
dvander@2890
    51
	bool triggered;
dvander@2890
    52
	FuncIteratorGuard **pprev;
dvander@2890
    53
	FuncIter *iter;
dvander@2890
    54
	FuncIteratorGuard *next;
dvander@2890
    55
public:
dvander@2890
    56
	FuncIteratorGuard(FuncIteratorGuard **pprev, FuncIter *iter)
dvander@2890
    57
		: triggered(false), pprev(pprev), iter(iter), next(*pprev)
dvander@2890
    58
	{
dvander@2890
    59
		*pprev = this;
dvander@2890
    60
	}
dvander@2890
    61
dvander@2890
    62
	~FuncIteratorGuard()
dvander@2890
    63
	{
dvander@2890
    64
		*pprev = next;
dvander@2890
    65
	}
dvander@2890
    66
dvander@2890
    67
	inline bool Triggered()
dvander@2890
    68
	{
dvander@2890
    69
		bool t = triggered;
dvander@2890
    70
		triggered = false;
dvander@2890
    71
		return t;
dvander@2890
    72
	}
dvander@2890
    73
dvander@2890
    74
	/**
dvander@2890
    75
	 * This should not read from |this| before the NULL check, because FwdSys
dvander@2890
    76
	 * can call (NULL)->FixIteratorChain().
dvander@2890
    77
	 */
dvander@2890
    78
	void FixIteratorChain(FuncIter &other)
dvander@2890
    79
	{
dvander@2890
    80
		FuncIteratorGuard *guard = this;
dvander@2890
    81
		while (guard != NULL)
dvander@2890
    82
		{
dvander@2890
    83
			if (*guard->iter == other)
dvander@2890
    84
			{
dvander@2890
    85
				*(guard->iter) = ++(*(guard->iter));
dvander@2890
    86
				guard->triggered = true;
dvander@2890
    87
			}
dvander@2890
    88
			guard = guard->next;
dvander@2890
    89
		}
dvander@2890
    90
	}
dvander@2890
    91
};
dvander@2890
    92
dvander@183
    93
class CForward : public IChangeableForward
dvander@180
    94
{
dvander@180
    95
public: //ICallable
dvander@180
    96
	virtual int PushCell(cell_t cell);
ds@585
    97
	virtual int PushCellByRef(cell_t *cell, int flags);
dvander@180
    98
	virtual int PushFloat(float number);
ds@585
    99
	virtual int PushFloatByRef(float *number, int flags);
dvander@589
   100
	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
dvander@180
   101
	virtual int PushString(const char *string);
faluco@192
   102
	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
dvander@180
   103
	virtual void Cancel();
dvander@180
   104
public: //IForward
dvander@615
   105
	virtual const char *GetForwardName();
dvander@615
   106
	virtual unsigned int GetFunctionCount();
dvander@615
   107
	virtual ExecType GetExecType();
dvander@180
   108
	virtual int Execute(cell_t *result, IForwardFilter *filter);
dvander@180
   109
public: //IChangeableForward
dvander@180
   110
	virtual bool RemoveFunction(IPluginFunction *func);
dvander@180
   111
	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
dvander@180
   112
	virtual bool AddFunction(IPluginFunction *func);
dvander@327
   113
	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
ds@621
   114
	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
dvander@180
   115
public:
dvander@180
   116
	static CForward *CreateForward(const char *name, 
dvander@180
   117
								   ExecType et, 
dvander@180
   118
								   unsigned int num_params, 
ds@559
   119
								   const ParamType *types, 
dvander@180
   120
								   va_list ap);
januk@4086
   121
	bool IsFunctionRegistered(IPluginFunction *func);
dvander@180
   122
private:
dvander@180
   123
	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
faluco@192
   124
	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
dvander@180
   125
	inline int SetError(int err)
dvander@180
   126
	{
dvander@180
   127
		m_errstate = err;
dvander@180
   128
		return err;
dvander@180
   129
	}
dvander@180
   130
protected:
dvander@180
   131
	/* :TODO: I want a caching list type here.
dvander@180
   132
	 * Destroying these things and using new/delete for their members feels bad.
dvander@180
   133
	 */
dvander@464
   134
	mutable List<IPluginFunction *> m_functions;
dvander@527
   135
	mutable List<IPluginFunction *> m_paused;
dvander@2890
   136
	FuncIteratorGuard *m_IterGuard;
dvander@180
   137
dvander@180
   138
	/* Type and name information */
dvander@183
   139
	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
dvander@180
   140
	ParamType m_types[SP_MAX_EXEC_PARAMS];
dvander@180
   141
	char m_name[FORWARDS_NAME_MAX+1];
dvander@180
   142
	unsigned int m_numparams;
dvander@183
   143
	unsigned int m_varargs;
dvander@180
   144
	ExecType m_ExecType;
dvander@180
   145
dvander@180
   146
	/* State information */
dvander@180
   147
	unsigned int m_curparam;
dvander@180
   148
	int m_errstate;
dvander@180
   149
};
dvander@180
   150
dvander@211
   151
class CForwardManager : 
dvander@211
   152
	public IForwardManager,
dvander@211
   153
	public IPluginsListener,
dvander@211
   154
	public SMGlobalClass
dvander@186
   155
{
dvander@186
   156
	friend class CForward;
faluco@641
   157
public:
faluco@641
   158
	~CForwardManager();
dvander@211
   159
public: //IForwardManager
faluco@430
   160
	IForward *CreateForward(const char *name, 
dvander@186
   161
		ExecType et, 
dvander@186
   162
		unsigned int num_params, 
ds@559
   163
		const ParamType *types, 
dvander@186
   164
		...);
faluco@430
   165
	IChangeableForward *CreateForwardEx(const char *name, 
dvander@186
   166
		ExecType et, 
dvander@186
   167
		int num_params, 
ds@559
   168
		const ParamType *types, 
dvander@186
   169
		...);
faluco@430
   170
	IForward *FindForward(const char *name, IChangeableForward **ifchng);
faluco@430
   171
	void ReleaseForward(IForward *forward);
dvander@211
   172
public: //IPluginsListener
faluco@430
   173
	void OnPluginLoaded(IPlugin *plugin);
faluco@430
   174
	void OnPluginUnloaded(IPlugin *plugin);
faluco@430
   175
	void OnPluginPauseChange(IPlugin *plugin, bool paused);
dvander@211
   176
public: //SMGlobalClass
faluco@430
   177
	void OnSourceModAllInitialized();
dvander@186
   178
protected:
dvander@186
   179
	CForward *ForwardMake();
dvander@186
   180
	void ForwardFree(CForward *fwd);
dvander@186
   181
private:
dvander@186
   182
	CStack<CForward *> m_FreeForwards;
dvander@186
   183
	List<CForward *> m_managed;
dvander@186
   184
	List<CForward *> m_unmanaged;
dvander@186
   185
};
dvander@186
   186
dvander@186
   187
extern CForwardManager g_Forwards;
dvander@186
   188
dvander@180
   189
#endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_