core/systems/ForwardSys.h
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 15 06:19:30 2007 +0000 (2007-08-15)
changeset 1330 ab94316bb52b
parent 1231 17badaf66ce4
child 1947 e73a1e51833b
permissions -rw-r--r--
Updated license headers on virtually all files with extremely minor changes in the name of some sort of strange consistency.
All plugin and include file headers also have been changed to say about GPL3 instead of GPL2.

(This day shall henceforth be known as the Eighty Column Massacre of '07)
dvander@370
     1
/**
ds@653
     2
 * vim: set ts=4 :
ds@1330
     3
 * =============================================================================
dvander@1231
     4
 * SourceMod
dvander@1231
     5
 * Copyright (C) 2004-2007 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>
dvander@180
    37
#include "sm_globals.h"
dvander@180
    38
#include <sh_list.h>
dvander@180
    39
#include <sh_stack.h>
dvander@211
    40
#include "sourcemod.h"
dvander@180
    41
dvander@180
    42
using namespace SourceHook;
dvander@180
    43
dvander@180
    44
typedef List<IPluginFunction *>::iterator FuncIter;
dvander@180
    45
dvander@180
    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@183
    49
struct ByrefInfo
dvander@180
    50
{
dvander@183
    51
	unsigned int cells;
dvander@183
    52
	cell_t *orig_addr;
dvander@183
    53
	int flags;
faluco@192
    54
	int sz_flags;
dvander@180
    55
};
dvander@180
    56
dvander@183
    57
struct FwdParamInfo
dvander@183
    58
{
dvander@183
    59
	cell_t val;
dvander@183
    60
	ByrefInfo byref;
dvander@183
    61
	ParamType pushedas;
dvander@183
    62
};
dvander@183
    63
dvander@183
    64
class CForward : public IChangeableForward
dvander@180
    65
{
dvander@180
    66
public: //ICallable
dvander@180
    67
	virtual int PushCell(cell_t cell);
ds@585
    68
	virtual int PushCellByRef(cell_t *cell, int flags);
dvander@180
    69
	virtual int PushFloat(float number);
ds@585
    70
	virtual int PushFloatByRef(float *number, int flags);
dvander@589
    71
	virtual int PushArray(cell_t *inarray, unsigned int cells, int flags);
dvander@180
    72
	virtual int PushString(const char *string);
faluco@192
    73
	virtual int PushStringEx(char *buffer, size_t length, int sz_flags, int cp_flags);
dvander@180
    74
	virtual void Cancel();
dvander@180
    75
public: //IForward
dvander@615
    76
	virtual const char *GetForwardName();
dvander@615
    77
	virtual unsigned int GetFunctionCount();
dvander@615
    78
	virtual ExecType GetExecType();
dvander@180
    79
	virtual int Execute(cell_t *result, IForwardFilter *filter);
dvander@180
    80
public: //IChangeableForward
dvander@180
    81
	virtual bool RemoveFunction(IPluginFunction *func);
dvander@180
    82
	virtual unsigned int RemoveFunctionsOfPlugin(IPlugin *plugin);
dvander@180
    83
	virtual bool AddFunction(IPluginFunction *func);
dvander@327
    84
	virtual bool AddFunction(IPluginContext *ctx, funcid_t index);
ds@621
    85
	virtual bool RemoveFunction(IPluginContext *ctx, funcid_t index);
dvander@180
    86
public:
dvander@180
    87
	static CForward *CreateForward(const char *name, 
dvander@180
    88
								   ExecType et, 
dvander@180
    89
								   unsigned int num_params, 
ds@559
    90
								   const ParamType *types, 
dvander@180
    91
								   va_list ap);
dvander@180
    92
private:
dvander@180
    93
	void _Int_PushArray(cell_t *inarray, unsigned int cells, int flags);
faluco@192
    94
	void _Int_PushString(cell_t *inarray, unsigned int cells, int sz_flags, int cp_flags);
dvander@180
    95
	inline int SetError(int err)
dvander@180
    96
	{
dvander@180
    97
		m_errstate = err;
dvander@180
    98
		return err;
dvander@180
    99
	}
dvander@180
   100
protected:
dvander@180
   101
	/* :TODO: I want a caching list type here.
dvander@180
   102
	 * Destroying these things and using new/delete for their members feels bad.
dvander@180
   103
	 */
dvander@464
   104
	mutable List<IPluginFunction *> m_functions;
dvander@527
   105
	mutable List<IPluginFunction *> m_paused;
dvander@180
   106
dvander@180
   107
	/* Type and name information */
dvander@183
   108
	FwdParamInfo m_params[SP_MAX_EXEC_PARAMS];
dvander@180
   109
	ParamType m_types[SP_MAX_EXEC_PARAMS];
dvander@180
   110
	char m_name[FORWARDS_NAME_MAX+1];
dvander@180
   111
	unsigned int m_numparams;
dvander@183
   112
	unsigned int m_varargs;
dvander@180
   113
	ExecType m_ExecType;
dvander@180
   114
dvander@180
   115
	/* State information */
dvander@180
   116
	unsigned int m_curparam;
dvander@180
   117
	int m_errstate;
dvander@180
   118
};
dvander@180
   119
dvander@211
   120
class CForwardManager : 
dvander@211
   121
	public IForwardManager,
dvander@211
   122
	public IPluginsListener,
dvander@211
   123
	public SMGlobalClass
dvander@186
   124
{
dvander@186
   125
	friend class CForward;
faluco@641
   126
public:
faluco@641
   127
	~CForwardManager();
dvander@211
   128
public: //IForwardManager
faluco@430
   129
	IForward *CreateForward(const char *name, 
dvander@186
   130
		ExecType et, 
dvander@186
   131
		unsigned int num_params, 
ds@559
   132
		const ParamType *types, 
dvander@186
   133
		...);
faluco@430
   134
	IChangeableForward *CreateForwardEx(const char *name, 
dvander@186
   135
		ExecType et, 
dvander@186
   136
		int num_params, 
ds@559
   137
		const ParamType *types, 
dvander@186
   138
		...);
faluco@430
   139
	IForward *FindForward(const char *name, IChangeableForward **ifchng);
faluco@430
   140
	void ReleaseForward(IForward *forward);
dvander@211
   141
public: //IPluginsListener
faluco@430
   142
	void OnPluginLoaded(IPlugin *plugin);
faluco@430
   143
	void OnPluginUnloaded(IPlugin *plugin);
faluco@430
   144
	void OnPluginPauseChange(IPlugin *plugin, bool paused);
dvander@211
   145
public: //SMGlobalClass
faluco@430
   146
	void OnSourceModAllInitialized();
dvander@186
   147
protected:
dvander@186
   148
	CForward *ForwardMake();
dvander@186
   149
	void ForwardFree(CForward *fwd);
dvander@186
   150
private:
dvander@186
   151
	CStack<CForward *> m_FreeForwards;
dvander@186
   152
	List<CForward *> m_managed;
dvander@186
   153
	List<CForward *> m_unmanaged;
dvander@186
   154
};
dvander@186
   155
dvander@186
   156
extern CForwardManager g_Forwards;
dvander@186
   157
dvander@180
   158
#endif //_INCLUDE_SOURCEMOD_FORWARDSYSTEM_H_