amxmodx/amxmodx/CForward.h

251 lines
6.8 KiB
C
Raw Normal View History

2004-03-05 21:03:14 +00:00
/* AMX Mod X
*
* by the AMX Mod X Development Team
* originally developed by OLO
*
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* In addition, as a special exception, the author gives permission to
* link the code of this program with the Half-Life Game Engine ("HL
* Engine") and Modified Game Libraries ("MODs") developed by Valve,
* L.L.C ("Valve"). You must obey the GNU General Public License in all
* respects for all of the code used other than the HL Engine and MODs
* from Valve. If you modify this file, you may extend this exception
* to your version of the file, but you are not obligated to do so. If
* you do not wish to do so, delete this exception statement from your
* version.
*/
2004-01-31 20:56:22 +00:00
2004-05-05 18:42:16 +00:00
/*
CForward.h
forwards
1) normal forwards: called in all plugins
2) single plugin (sp) forwards: called in one plugin
The SP Forwards are handled differently because they are expected to be created / deleted
often, but the "normal" forwards are expected to be initialized at start up.
Note about forward ids:
for normal forwards: <index to vector> << 1
for sp forwards: (<index to vector> << 1) | 1
*/
2004-01-31 20:56:22 +00:00
#ifndef FORWARD_H
#define FORWARD_H
2006-02-01 12:09:19 +00:00
#include <stdarg.h>
#include "sh_stack.h"
const int FORWARD_MAX_PARAMS = 32;
2004-01-31 20:56:22 +00:00
#define FORWARD_ONLY_OLD 1
#define FORWARD_ONLY_NEW 2
#define FORWARD_ALL 3
2004-04-03 19:14:03 +00:00
enum ForwardExecType
{
ET_IGNORE = 0, // Ignore return vaue
ET_STOP, // Stop on PLUGIN_HANDLED
ET_STOP2, // Stop on PLUGIN_HANDLED, continue on other values, return biggest return value
ET_CONTINUE, // Continue; return biggest return value
2004-01-31 20:56:22 +00:00
};
2004-04-03 19:14:03 +00:00
enum ForwardParam
2004-01-31 20:56:22 +00:00
{
2004-04-03 19:14:03 +00:00
FP_DONE = -1, // specify this as the last argument
// only tells the function that there are no more arguments
FP_CELL, // normal cell
FP_FLOAT, // float; used as normal cell though
FP_STRING, // string
FP_STRINGEX, // string; will be updated to the last function's value
FP_ARRAY, // array; use the return value of prepareArray.
};
2004-01-31 20:56:22 +00:00
2004-05-05 18:42:16 +00:00
// for prepareArray
2004-04-03 19:14:03 +00:00
enum ForwardArrayElemType
{
Type_Cell = 0,
Type_Char
};
2004-01-31 20:56:22 +00:00
2004-04-03 19:14:03 +00:00
struct ForwardPreparedArray
{
void *ptr;
2005-09-16 23:48:51 +00:00
2004-04-03 19:14:03 +00:00
ForwardArrayElemType type;
2005-09-16 23:48:51 +00:00
2004-04-03 19:14:03 +00:00
unsigned int size;
bool copyBack;
2004-04-03 19:14:03 +00:00
};
2004-01-31 20:56:22 +00:00
2004-05-05 18:42:16 +00:00
// Normal forward
2004-04-03 19:14:03 +00:00
class CForward
{
const char *m_FuncName;
ForwardExecType m_ExecType;
int m_NumParams;
String m_Name;
2005-09-10 00:38:42 +00:00
2004-04-03 19:14:03 +00:00
struct AMXForward
{
CPluginMngr::CPlugin *pPlugin;
int func;
};
2005-09-10 00:38:42 +00:00
typedef CVector<AMXForward> AMXForwardList;
2005-09-16 23:48:51 +00:00
2004-04-03 19:14:03 +00:00
AMXForwardList m_Funcs;
ForwardParam m_ParamTypes[FORWARD_MAX_PARAMS];
2005-09-10 00:38:42 +00:00
2004-01-31 20:56:22 +00:00
public:
CForward(const char *name, ForwardExecType et, int numParams, const ForwardParam * paramTypes, int fwd_type=FORWARD_ALL);
2005-09-16 23:48:51 +00:00
CForward() {} // leaves everything unitialized'
2004-05-05 18:42:16 +00:00
cell execute(cell *params, ForwardPreparedArray *preparedArrays);
2005-09-10 00:38:42 +00:00
2004-04-03 19:14:03 +00:00
int getParamsNum() const
{
return m_NumParams;
}
2005-09-10 00:38:42 +00:00
2004-05-05 18:42:16 +00:00
int getFuncsNum() const
{
return m_Funcs.size();
}
const char *getFuncName() const
{
return m_Name.c_str();
}
2005-09-10 00:38:42 +00:00
ForwardParam getParamType(int paramId) const
{
if (paramId < 0 || paramId >= m_NumParams)
return FP_DONE;
2005-09-10 00:38:42 +00:00
return m_ParamTypes[paramId];
}
2004-05-05 18:42:16 +00:00
};
// Single plugin forward
class CSPForward
{
friend class CForwardMngr;
2004-05-05 18:42:16 +00:00
int m_NumParams;
2005-09-16 23:48:51 +00:00
2004-05-05 18:42:16 +00:00
ForwardParam m_ParamTypes[FORWARD_MAX_PARAMS];
AMX *m_Amx;
2005-09-16 23:48:51 +00:00
2004-05-05 18:42:16 +00:00
int m_Func;
bool m_HasFunc;
String m_Name;
bool m_InExec;
bool m_ToDelete;
2005-09-10 00:38:42 +00:00
public:
bool isFree;
2004-05-05 18:42:16 +00:00
public:
CSPForward() { m_HasFunc = false; }
void Set(const char *funcName, AMX *amx, int numParams, const ForwardParam * paramTypes);
void Set(int func, AMX *amx, int numParams, const ForwardParam * paramTypes);
cell execute(cell *params, ForwardPreparedArray *preparedArrays);
2005-09-10 00:38:42 +00:00
2004-05-05 18:42:16 +00:00
int getParamsNum() const
{
return m_NumParams;
}
2005-09-10 00:38:42 +00:00
2004-05-05 18:42:16 +00:00
int getFuncsNum() const
{
return (m_HasFunc) ? 1 : 0;
}
const char *getFuncName() const
{
return m_Name.c_str();
}
2005-09-10 00:38:42 +00:00
ForwardParam getParamType(int paramId) const
{
if (paramId < 0 || paramId >= m_NumParams)
return FP_DONE;
2005-09-10 00:38:42 +00:00
return m_ParamTypes[paramId];
}
2004-04-03 19:14:03 +00:00
};
2004-01-31 20:56:22 +00:00
2004-04-03 19:14:03 +00:00
class CForwardMngr
{
typedef CVector<CForward*> ForwardVec;
2004-05-05 18:42:16 +00:00
typedef CVector<CSPForward*> SPForwardVec;
typedef CStack<int> FreeSPVec; // Free SP Forwards
2004-05-05 18:42:16 +00:00
2004-04-03 19:14:03 +00:00
ForwardVec m_Forwards;
2004-05-05 18:42:16 +00:00
SPForwardVec m_SPForwards;
FreeSPVec m_FreeSPForwards; // so we don't have to free memory
2005-09-10 00:38:42 +00:00
ForwardPreparedArray m_TmpArrays[FORWARD_MAX_PARAMS]; // used by prepareArray
2004-04-03 19:14:03 +00:00
int m_TmpArraysNum;
public:
2004-01-31 20:56:22 +00:00
2004-04-03 19:14:03 +00:00
CForwardMngr()
{ m_TmpArraysNum = 0; }
2005-09-16 23:48:51 +00:00
~CForwardMngr() {}
2004-01-31 20:56:22 +00:00
2004-04-03 19:14:03 +00:00
// Interface
2004-05-05 18:42:16 +00:00
// Register normal forward
int registerForward(const char *funcName, ForwardExecType et, int numParams, const ForwardParam *paramTypes, int fwd_type=FORWARD_ALL);
2004-05-05 18:42:16 +00:00
// Register single plugin forward
int registerSPForward(const char *funcName, AMX *amx, int numParams, const ForwardParam * paramTypes);
int registerSPForward(int func, AMX *amx, int numParams, const ForwardParam * paramTypes);
2005-09-16 23:48:51 +00:00
2004-05-05 18:42:16 +00:00
// Unregister single plugin forward
void unregisterSPForward(int id);
int duplicateSPForward(int id);
int isSameSPForward(int id1, int id2);
2005-09-16 23:48:51 +00:00
2004-05-05 18:42:16 +00:00
// execute forward
cell executeForwards(int id, cell *params);
2005-09-10 00:38:42 +00:00
void clear(); // delete all forwards
2005-09-16 23:48:51 +00:00
2004-05-05 18:42:16 +00:00
bool isIdValid(int id) const; // check whether forward id is valid
bool isSPForward(int id) const; // check whether forward is single plugin
int getParamsNum(int id) const; // get num of params of a forward
int getFuncsNum(int id) const; // get num of found functions of a forward
const char *getFuncName(int id) const; // get the function name
2005-09-16 23:48:51 +00:00
ForwardParam getParamType(int id, int paramId) const;
2005-09-10 00:38:42 +00:00
cell prepareArray(void *ptr, unsigned int size, ForwardArrayElemType type, bool copyBack); // prepare array
2004-01-31 20:56:22 +00:00
};
2004-05-05 18:42:16 +00:00
// (un)register forward
2004-04-03 19:14:03 +00:00
int registerForward(const char *funcName, ForwardExecType et, ...);
int registerForwardC(const char *funcName, ForwardExecType et, cell *list, size_t num, int fwd_type=FORWARD_ALL);
2004-05-05 18:42:16 +00:00
int registerSPForwardByName(AMX *amx, const char *funcName, ...);
2006-02-01 12:09:19 +00:00
int registerSPForwardByNameC(AMX *amx, const char *funcName, cell *list, size_t num);
2004-05-05 18:42:16 +00:00
int registerSPForward(AMX *amx, int func, ...);
void unregisterSPForward(int id);
2004-04-03 19:14:03 +00:00
// execute forwards
2004-05-05 18:42:16 +00:00
cell executeForwards(int id, ...);
2004-04-03 19:14:03 +00:00
// prepare array
cell prepareCellArray(cell *ptr, unsigned int size, bool copyBack = false);
cell prepareCharArray(char *ptr, unsigned int size, bool copyBack = false);
2004-01-31 20:56:22 +00:00
2005-09-10 00:38:42 +00:00
#endif //FORWARD_H