new message system and string class

This commit is contained in:
David Anderson 2005-07-22 09:26:37 +00:00
parent 99f65175ff
commit 191824d72a
4 changed files with 291 additions and 313 deletions

View File

@ -39,9 +39,7 @@ public:
String()
{
v = NULL;
mSize = 0;
cSize = 0;
Grow(2);
a_size = 0;
assign("");
}
@ -54,41 +52,38 @@ public:
String(const char *src)
{
v = NULL;
mSize = 0;
cSize = 0; assign(src);
a_size = 0;
assign(src);
}
String(String &src)
{
v = NULL;
mSize = 0;
cSize = 0;
a_size = 0;
assign(src.c_str());
}
const char *c_str() { return v?v:""; }
const char *c_str() const { return v?v:""; }
const char *c_str() { return v; }
const char *c_str() const { return v; }
void append(const char *t)
{
Grow(cSize + strlen(t) + 1);
Grow(strlen(v) + strlen(t) + 1);
strcat(v, t);
cSize = strlen(v);
}
void append(const char c)
{
Grow(cSize + 2);
v[cSize] = c;
v[++cSize] = 0;
size_t len = strlen(v);
Grow(len + 2);
v[len] = c;
v[len + 1] = '\0';
}
void append(String &d)
{
const char *t = d.c_str();
Grow(cSize + strlen(t));
strcat(v, t);
cSize = strlen(v);
append(d.c_str());
}
void assign(const String &src)
@ -100,79 +95,51 @@ public:
{
if (!d)
{
Grow(1);
cSize = 0;
strcpy(v, "");
return;
}
Grow(strlen(d));
if (v)
{
strcpy(v, d);
cSize = strlen(v);
clear();
} else {
cSize = 0;
Grow(strlen(d) + 1, false);
strcpy(v, d);
}
}
void clear()
{
if (v)
{
v[0] = 0;
cSize = 0;
}
v[0] = '\0';
}
int compare (const char *d)
{
if (v) {
if (d) {
return strcmp(v, d);
} else {
return strlen(v);
}
} else {
if (d) {
return strlen(d);
} else {
return 0;
}
}
}
//Added this for amxx inclusion
bool empty()
{
if (!v || !cSize)
if (v[0] == '\0')
return true;
return false;
}
int size()
size_t size()
{
if (!v)
return 0;
return cSize;
return strlen(v);
}
const char * _fread(FILE *fp)
{
Grow(512);
char * ret = fgets(v, 511, fp);
cSize = strlen(v);
return ret;
}
int find(const char c, int index = 0)
{
if (!v)
return npos;
if (index >= (int)cSize || index < 0)
size_t len = strlen(v);
if (index >= (int)len || index < 0)
return npos;
unsigned int i = 0;
for (i=index; i<cSize; i++)
for (i=index; i<(int)len; i++)
{
if (v[i] == c)
{
@ -197,12 +164,11 @@ public:
void trim()
{
if (!v)
return;
unsigned int i = 0;
unsigned int j = 0;
size_t len = strlen(v);
if (cSize == 1)
if (len == 1)
{
if (is_space(v[i]))
{
@ -215,9 +181,9 @@ public:
if (is_space(c0))
{
for (i=0; i<cSize; i++)
for (i=0; i<len; i++)
{
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==cSize-1)))
if (!is_space(v[i]) || (is_space(v[i]) && ((unsigned char)i==len-1)))
{
erase(0, i);
break;
@ -225,16 +191,16 @@ public:
}
}
cSize = strlen(v);
len = strlen(v);
if (cSize < 1)
if (len < 1)
{
return;
}
if (is_space(v[cSize-1]))
if (is_space(v[len-1]))
{
for (i=cSize-1; i>=0; i--)
for (i=len-1; i>=0; i--)
{
if (!is_space(v[i])
|| (is_space(v[i]) && i==0))
@ -246,7 +212,7 @@ public:
}
}
if (cSize == 1)
if (len == 1)
{
if (is_space(v[0]))
{
@ -256,21 +222,20 @@ public:
}
}
String & erase(unsigned int start, int num = npos)
void erase(unsigned int start, int num = npos)
{
if (!v)
return (*this);
unsigned int i = 0;
size_t len = size();
//check for bounds
if (num == npos || start+num > cSize-num+1)
num = cSize - start;
if (num == npos || start+num > len-num+1)
num = len - start;
//do the erasing
bool copyflag = false;
for (i=0; i<cSize; i++)
for (i=0; i<len; i++)
{
if (i>=start && i<start+num)
{
if (i+num < cSize)
if (i+num < len)
{
v[i] = v[i+num];
} else {
@ -278,7 +243,7 @@ public:
}
copyflag = true;
} else if (copyflag) {
if (i+num < cSize)
if (i+num < len)
{
v[i] = v[i+num];
} else {
@ -286,51 +251,45 @@ public:
}
}
}
cSize -= num;
v[cSize] = 0;
return (*this);
len -= num;
v[len] = 0;
}
String substr(unsigned int index, int num = npos)
{
String ns;
if (index >= cSize || !v)
size_t len = size();
if (index >= len || !v)
return ns;
if (num == npos)
{
num = cSize - index;
} else if (index+num >= cSize) {
num = cSize - index;
num = len - index;
} else if (index+num >= len) {
num = len - index;
}
unsigned int i = 0, j=0;
char *s = new char[cSize+1];
unsigned int nslen = num + 2;
ns.Grow(nslen);
for (i=index; i<index+num; i++)
{
s[j++] = v[i];
}
s[j] = 0;
ns.assign(s);
delete [] s;
ns.append(v[i]);
return ns;
}
void toLower()
{
if (!v)
return;
unsigned int i = 0;
for (i=0; i<cSize; i++)
size_t len = strlen(v);
for (i=0; i<len; i++)
{
if (v[i] >= 65 && v[i] <= 90)
v[i] |= 32;
v[i] &= ~(1<<5);
}
}
@ -349,7 +308,7 @@ public:
char operator [] (unsigned int index)
{
if (index > cSize)
if (index > size())
{
return -1;
} else {
@ -359,7 +318,7 @@ public:
int at(int a)
{
if (a < 0 || a >= (int)cSize)
if (a < 0 || a >= (int)size())
return -1;
return v[a];
@ -367,7 +326,7 @@ public:
bool at(int at, char c)
{
if (at < 0 || at >= (int)cSize)
if (at < 0 || at >= (int)size())
return false;
v[at] = c;
@ -376,27 +335,20 @@ public:
}
private:
void Grow(unsigned int d)
void Grow(unsigned int d, bool copy=true)
{
if (d<1)
if (d <= a_size)
return;
if (d > mSize)
{
mSize = d + 16; // allocate a buffer
char *t = new char[d+1];
if (v) {
strcpy(t, v);
t[cSize] = 0;
char *n = new char[d + 1];
if (copy)
strcpy(n, v);
delete [] v;
}
v = t;
mSize = d;
}
v = n;
a_size = d + 1;
}
char *v;
unsigned int mSize;
unsigned int cSize;
unsigned int a_size;
public:
static const int npos = -1;
};

View File

@ -26,7 +26,6 @@ void ClearHooks()
for (i=0; i<Thinks.size(); i++)
delete Thinks[i];
Msg.clear();
Touches.clear();
Impulses.clear();
Thinks.clear();
@ -226,7 +225,6 @@ void ServerActivate(edict_t *pEdictList, int edictCount, int clientMax)
{
for(int f = 1; f <= gpGlobals->maxClients;f++)
g_player_edicts[f]=pEdictList + f;
Msg.clear();
register int i = 0, j = 0;
for (i=0; i<256; i++) {
for (j=0; j<(int)msgHooks[i].size(); j++)

View File

@ -1,6 +1,6 @@
#include "engine.h"
CVector<argMsg> Msg;
Message Msg;
CVector<int> msgHooks[256];
int msgBlocks[256] = {0};
int msgDest;
@ -9,83 +9,162 @@ float *msgOrigin;
edict_t *msgpEntity;
bool inhook = false;
bool inblock = false;
unsigned int msgCount = 0;
argMsg::argMsg()
Message::Message()
{
Reset();
msgparam *p = new msgparam;
m_Params.push_back(p);
m_CurParam = 0;
}
void argMsg::Reset()
Message::~Message()
{
memset(&v, 0, sizeof(v));
cData.clear();
type = 0;
for (size_t i=0; i<m_Params.size(); i++)
delete m_Params[i];
m_Params.clear();
}
void argMsg::Send()
msgparam *Message::AdvPtr()
{
switch (type)
msgparam *pParam = NULL;
if (++m_CurParam >= m_Params.size())
{
case arg_byte:
WRITE_BYTE(v.iData);
break;
case arg_char:
WRITE_CHAR(v.iData);
break;
case arg_short:
WRITE_SHORT(v.iData);
break;
case arg_long:
WRITE_LONG(v.iData);
break;
case arg_angle:
WRITE_ANGLE(v.fData);
break;
case arg_coord:
WRITE_COORD(v.fData);
break;
case arg_string:
WRITE_STRING(cData.c_str());
break;
case arg_entity:
WRITE_ENTITY(v.iData);
break;
}
Reset();
pParam = new msgparam;
m_Params.push_back(pParam);
} else {
pParam = m_Params[m_CurParam];
}
int argMsg::Type()
{
switch (type)
{
case arg_byte:
return type_int;
break;
case arg_char:
return type_int;
break;
case arg_short:
return type_int;
break;
case arg_long:
return type_int;
break;
case arg_angle:
return type_float;
break;
case arg_coord:
return type_float;
break;
case arg_string:
return type_string;
break;
case arg_entity:
return type_int;
break;
return pParam;
}
void Message::AddParam(const char *data, msgtype type)
{
msgparam *pParam = AdvPtr();
pParam->szData.assign(data);
pParam->type = type;
}
void Message::AddParam(int data, msgtype type)
{
msgparam *pParam = AdvPtr();
pParam->v.iData = data;
pParam->type = type;
}
void Message::AddParam(float data, msgtype type)
{
msgparam *pParam = AdvPtr();
pParam->v.fData = data;
pParam->type = type;
}
msgtype Message::GetParamType(size_t index)
{
if (index < 1 || index > m_CurParam)
return static_cast<msgtype>(0);
return m_Params[index]->type;
}
float Message::GetParamFloat(size_t index)
{
if (index < 1 || index > m_CurParam)
return 0;
return m_Params[index]->v.fData;
}
const char *Message::GetParamString(size_t index)
{
if (index < 1 || index > m_CurParam)
return 0;
return m_Params[index]->szData.c_str();
}
int Message::GetParamInt(size_t index)
{
if (index < 1 || index > m_CurParam)
return 0;
return m_Params[index]->v.iData;
}
void Message::SetParam(size_t index, float data)
{
if (index < 1 || index > m_CurParam)
return;
m_Params[index]->v.fData = data;
}
void Message::SetParam(size_t index, int data)
{
if (index < 1 || index > m_CurParam)
return;
m_Params[index]->v.iData = data;
}
void Message::SetParam(size_t index, const char *data)
{
if (index < 1 || index > m_CurParam)
return;
m_Params[index]->szData.assign(data);
}
void Message::Reset()
{
m_CurParam = 0;
}
size_t Message::Params()
{
return m_CurParam;
}
void Message::Send()
{
msgparam *pParam = NULL;
for (size_t i=1; i<=m_CurParam; i++)
{
pParam = m_Params[i];
switch (pParam->type)
{
case arg_byte:
WRITE_BYTE(pParam->v.iData);
break;
case arg_char:
WRITE_CHAR(pParam->v.iData);
break;
case arg_short:
WRITE_SHORT(pParam->v.iData);
break;
case arg_long:
WRITE_LONG(pParam->v.iData);
break;
case arg_angle:
WRITE_ANGLE(pParam->v.fData);
break;
case arg_coord:
WRITE_COORD(pParam->v.fData);
break;
case arg_string:
WRITE_STRING(pParam->szData.c_str());
break;
case arg_entity:
WRITE_ENTITY(pParam->v.iData);
break;
}
}
}
void MessageBegin(int msg_dest, int msg_type, const float *pOrigin, edict_t *ed)
@ -96,7 +175,6 @@ void MessageBegin(int msg_dest, int msg_type, const float *pOrigin, edict_t *ed)
RETURN_META(MRES_SUPERCEDE);
} else if (msgHooks[msg_type].size()) {
inhook = true;
msgCount = 0;
msgDest = msg_dest;
msgType = msg_type;
msgOrigin = (float *)pOrigin;
@ -112,15 +190,7 @@ void WriteByte(int iValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.iData = iValue;
p.type = arg_byte;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.iData = iValue;
Msg[msgCount-1].type = arg_byte;
}
Msg.AddParam(iValue, arg_byte);
RETURN_META(MRES_SUPERCEDE);
}
@ -132,15 +202,7 @@ void WriteChar(int iValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.iData = iValue;
p.type = arg_char;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.iData = iValue;
Msg[msgCount-1].type = arg_char;
}
Msg.AddParam(iValue, arg_char);
RETURN_META(MRES_SUPERCEDE);
}
@ -152,15 +214,7 @@ void WriteShort(int iValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.iData = iValue;
p.type = arg_short;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.iData = iValue;
Msg[msgCount-1].type = arg_short;
}
Msg.AddParam(iValue, arg_short);
RETURN_META(MRES_SUPERCEDE);
}
@ -172,15 +226,7 @@ void WriteLong(int iValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.iData = iValue;
p.type = arg_long;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.iData = iValue;
Msg[msgCount-1].type = arg_long;
}
Msg.AddParam(iValue, arg_long);
RETURN_META(MRES_SUPERCEDE);
}
@ -192,15 +238,7 @@ void WriteAngle(float flValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.fData = flValue;
p.type = arg_angle;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.fData = flValue;
Msg[msgCount-1].type = arg_angle;
}
Msg.AddParam(flValue, arg_angle);
RETURN_META(MRES_SUPERCEDE);
}
@ -212,15 +250,7 @@ void WriteCoord(float flValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.fData = flValue;
p.type = arg_coord;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.fData = flValue;
Msg[msgCount-1].type = arg_coord;
}
Msg.AddParam(flValue, arg_coord);
RETURN_META(MRES_SUPERCEDE);
}
@ -232,15 +262,7 @@ void WriteString(const char *sz)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.cData.assign(sz);
p.type = arg_string;
Msg.push_back(p);
} else {
Msg[msgCount-1].cData.assign(sz);
Msg[msgCount-1].type = arg_string;
}
Msg.AddParam(sz, arg_string);
RETURN_META(MRES_SUPERCEDE);
}
@ -252,15 +274,7 @@ void WriteEntity(int iValue)
if (inblock) {
RETURN_META(MRES_SUPERCEDE);
} else if (inhook) {
if (++msgCount > Msg.size()) {
argMsg p;
p.v.iData = iValue;
p.type = arg_entity;
Msg.push_back(p);
} else {
Msg[msgCount-1].v.iData = iValue;
Msg[msgCount-1].type = arg_entity;
}
Msg.AddParam(iValue, arg_entity);
RETURN_META(MRES_SUPERCEDE);
}
@ -286,16 +300,13 @@ void MessageEnd(void)
inhook = false;
if (mres & 1)
{
msgCount = 0;
Msg.Reset();
RETURN_META(MRES_SUPERCEDE);
}
MESSAGE_BEGIN(msgDest, msgType, msgOrigin, msgpEntity);
for (i=0; i<msgCount; i++) {
Msg[i].Send();
Msg[i].Reset();
}
Msg.Send();
MESSAGE_END();
msgCount = 0;
Msg.Reset();
RETURN_META(MRES_SUPERCEDE);
}
@ -350,105 +361,109 @@ static cell AMX_NATIVE_CALL get_msg_block(AMX *amx, cell *params)
static cell AMX_NATIVE_CALL get_msg_args(AMX *amx, cell *params)
{
return msgCount;
return Msg.Params();
}
static cell AMX_NATIVE_CALL get_msg_argtype(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
return Msg[argn].type;
return Msg.GetParamType(argn);
}
static cell AMX_NATIVE_CALL get_msg_arg_int(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
int iVal = Msg[argn].v.iData;
return iVal;
return Msg.GetParamInt(argn);
}
static cell AMX_NATIVE_CALL set_msg_arg_int(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
Msg[argn].type = params[2];
Msg[argn].v.iData = params[3];
Msg.SetParam(argn, params[2]);
return 1;
}
static cell AMX_NATIVE_CALL get_msg_arg_float(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
return amx_ftoc(Msg[argn].v.fData);
return amx_ftoc(Msg.GetParamFloat(argn));
}
static cell AMX_NATIVE_CALL set_msg_arg_float(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
REAL fVal = amx_ctof(params[2]);
Msg[argn].v.fData = fVal;
Msg.SetParam(argn, fVal);
return 1;
}
static cell AMX_NATIVE_CALL get_msg_arg_string(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
const char *szVal = Msg[argn].cData.c_str();
const char *szVal = Msg.GetParamString(argn);
return MF_SetAmxString(amx, params[2], szVal?szVal:"", params[3]);
return MF_SetAmxString(amx, params[2], szVal, params[3]);
}
static cell AMX_NATIVE_CALL set_msg_arg_string(AMX *amx, cell *params)
{
unsigned int argn = params[1]-1;
size_t argn = static_cast<size_t>(params[1]);
int iLen;
if (!inhook || argn >= Msg.size()) {
MF_RaiseAmxError(amx, AMX_ERR_NATIVE);
if (!inhook || argn > Msg.Params())
{
MF_LogError(amx, AMX_ERR_NATIVE, "Invalid message argument %d", argn);
return 0;
}
char *szVal = MF_GetAmxString(amx, params[2], 0, &iLen);
Msg[argn].cData.assign(szVal);
Msg.SetParam(argn, szVal);
return 1;
}

View File

@ -9,7 +9,8 @@
#define BLOCK_ONCE 1
#define BLOCK_SET 2
enum {
enum msgtype
{
arg_byte = 1,
arg_char,
arg_short,
@ -20,31 +21,43 @@ enum {
arg_entity,
};
enum {
type_int = 1,
type_float,
type_string,
};
class argMsg
struct msgparam
{
public:
argMsg();
void Reset();
void Send();
int Type();
int type;
msgtype type;
union
{
REAL fData;
int iData;
} v;
String cData;
String szData;
};
class Message
{
public:
Message();
~Message();
void AddParam(float data, msgtype type);
void AddParam(int data, msgtype type);
void AddParam(const char *data, msgtype type);
void SetParam(size_t index, float data);
void SetParam(size_t index, int data);
void SetParam(size_t index, const char *data);
const char *GetParamString(size_t index);
float GetParamFloat(size_t index);
int GetParamInt(size_t index);
msgtype GetParamType(size_t index);
void Reset();
void Send();
size_t Params();
private:
msgparam *AdvPtr();
private:
CVector<msgparam *> m_Params;
size_t m_CurParam;
};
extern AMX_NATIVE_INFO msg_Natives[];
extern CVector<argMsg> Msg;
extern CVector<int> msgHooks[256];
extern int msgBlocks[256];