2007-07-02 02:25:46 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* ===============================================================
|
|
|
|
* SourceMod SDK Tools Extension
|
|
|
|
* Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
|
|
|
|
* ===============================================================
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Version: $Id$
|
|
|
|
*/
|
|
|
|
|
2007-06-16 10:36:34 +02:00
|
|
|
#include "vcallbuilder.h"
|
|
|
|
#include "extension.h"
|
|
|
|
|
2007-06-17 21:39:11 +02:00
|
|
|
ValveCall::ValveCall()
|
|
|
|
{
|
|
|
|
call = NULL;
|
|
|
|
vparams = NULL;
|
|
|
|
retinfo = NULL;
|
|
|
|
thisinfo = NULL;
|
|
|
|
retbuf = NULL;
|
|
|
|
}
|
|
|
|
|
2007-06-16 10:36:34 +02:00
|
|
|
ValveCall::~ValveCall()
|
|
|
|
{
|
|
|
|
while (!stk.empty())
|
|
|
|
{
|
|
|
|
unsigned char *ptr = stk.front();
|
|
|
|
delete [] ptr;
|
|
|
|
stk.pop();
|
|
|
|
}
|
2007-06-17 21:49:07 +02:00
|
|
|
if (call)
|
|
|
|
{
|
|
|
|
call->Destroy();
|
|
|
|
}
|
2007-06-16 10:36:34 +02:00
|
|
|
delete [] retbuf;
|
|
|
|
delete [] vparams;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *ValveCall::stk_get()
|
|
|
|
{
|
|
|
|
unsigned char *ptr;
|
|
|
|
if (stk.empty())
|
|
|
|
{
|
|
|
|
ptr = new unsigned char[stackSize];
|
|
|
|
} else {
|
|
|
|
ptr = stk.front();
|
|
|
|
stk.pop();
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ValveCall::stk_put(unsigned char *ptr)
|
|
|
|
{
|
|
|
|
stk.push(ptr);
|
|
|
|
}
|
|
|
|
|
2007-06-16 17:40:22 +02:00
|
|
|
ValveCall *CreateValveCall(void *addr,
|
|
|
|
ValveCallType vcalltype,
|
|
|
|
const ValvePassInfo *retInfo,
|
|
|
|
const ValvePassInfo *params,
|
|
|
|
unsigned int numParams)
|
|
|
|
{
|
|
|
|
if (numParams > 32)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValveCall *vc = new ValveCall;
|
|
|
|
|
2007-07-08 11:46:27 +02:00
|
|
|
vc->type = vcalltype;
|
|
|
|
|
2007-06-16 17:40:22 +02:00
|
|
|
size_t size = 0;
|
|
|
|
vc->stackSize = 0;
|
|
|
|
|
2007-06-20 10:03:43 +02:00
|
|
|
/* Get return information - encode only */
|
2007-06-16 17:40:22 +02:00
|
|
|
PassInfo retBuf;
|
|
|
|
size_t retBufSize = 0;
|
2007-06-20 10:03:43 +02:00
|
|
|
bool retbuf_needs_extra;
|
2007-06-16 17:40:22 +02:00
|
|
|
if (retInfo)
|
|
|
|
{
|
2007-06-20 10:03:43 +02:00
|
|
|
if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
|
2007-06-16 17:40:22 +02:00
|
|
|
{
|
|
|
|
delete vc;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-20 10:03:43 +02:00
|
|
|
retBufSize = retBuf.size;
|
|
|
|
if (retbuf_needs_extra)
|
|
|
|
{
|
|
|
|
retBufSize += size;
|
|
|
|
}
|
2007-06-16 17:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get thisinfo if needed */
|
|
|
|
ValvePassInfo thisbuf;
|
|
|
|
ValvePassInfo *thisinfo = NULL;
|
|
|
|
CallConvention cv = CallConv_Cdecl;
|
|
|
|
if (vcalltype != ValveCall_Static)
|
|
|
|
{
|
|
|
|
thisinfo = &thisbuf;
|
|
|
|
thisinfo->type = PassType_Basic;
|
2007-07-08 11:46:27 +02:00
|
|
|
switch (vcalltype)
|
2007-06-16 17:40:22 +02:00
|
|
|
{
|
2007-07-08 11:46:27 +02:00
|
|
|
case ValveCall_Entity:
|
2007-06-16 17:40:22 +02:00
|
|
|
thisinfo->vtype = Valve_CBaseEntity;
|
2007-07-08 11:46:27 +02:00
|
|
|
thisinfo->flags = PASSFLAG_BYVAL;
|
2007-06-16 17:40:22 +02:00
|
|
|
thisinfo->decflags |= VDECODE_FLAG_ALLOWWORLD;
|
2007-07-08 11:46:27 +02:00
|
|
|
break;
|
|
|
|
case ValveCall_Player:
|
2007-06-16 17:40:22 +02:00
|
|
|
thisinfo->vtype = Valve_CBasePlayer;
|
2007-07-08 11:46:27 +02:00
|
|
|
thisinfo->flags = PASSFLAG_BYVAL;
|
|
|
|
thisinfo->decflags = 0;
|
|
|
|
break;
|
|
|
|
case ValveCall_GameRules:
|
|
|
|
thisinfo->vtype = Valve_POD;
|
|
|
|
thisinfo->flags = PASSFLAG_ASPOINTER;
|
2007-06-16 17:40:22 +02:00
|
|
|
thisinfo->decflags = 0;
|
|
|
|
}
|
|
|
|
thisinfo->encflags = 0;
|
|
|
|
thisinfo->offset = 0;
|
|
|
|
vc->stackSize += sizeof(void *);
|
|
|
|
cv = CallConv_ThisCall;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get parameter info */
|
|
|
|
PassInfo paramBuf[32];
|
2007-06-20 10:03:43 +02:00
|
|
|
size_t sizes[32];
|
|
|
|
size_t normSize = 0;
|
|
|
|
size_t extraSize = 0;
|
2007-06-16 17:40:22 +02:00
|
|
|
for (unsigned int i=0; i<numParams; i++)
|
|
|
|
{
|
2007-06-20 10:03:43 +02:00
|
|
|
bool needs_extra;
|
|
|
|
if ((size = ValveParamToBinParam(params[i].vtype,
|
|
|
|
params[i].type,
|
|
|
|
params[i].flags,
|
|
|
|
¶mBuf[i],
|
|
|
|
needs_extra)) == 0)
|
2007-06-16 17:40:22 +02:00
|
|
|
{
|
|
|
|
delete vc;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-20 10:03:43 +02:00
|
|
|
if (needs_extra)
|
|
|
|
{
|
|
|
|
sizes[i] = size;
|
|
|
|
} else {
|
|
|
|
sizes[i] = 0;
|
|
|
|
}
|
|
|
|
normSize += paramBuf[i].size;
|
|
|
|
extraSize += sizes[i];
|
2007-06-16 17:40:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we can try creating the call */
|
|
|
|
if ((vc->call = g_pBinTools->CreateCall(addr,
|
|
|
|
cv,
|
|
|
|
(retInfo ? &retBuf : NULL),
|
|
|
|
paramBuf,
|
|
|
|
numParams))
|
|
|
|
== NULL)
|
|
|
|
{
|
|
|
|
if (!vc->call)
|
|
|
|
{
|
|
|
|
delete vc;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate extra space for thisptr AND ret buffer, even if we don't use it */
|
|
|
|
vc->vparams = new ValvePassInfo[numParams + 2];
|
|
|
|
|
|
|
|
/* We've got the call and everything is encoded.
|
|
|
|
* It's time to save the valve specific information and helper variables.
|
|
|
|
*/
|
|
|
|
if (retInfo)
|
|
|
|
{
|
|
|
|
/* Allocate and copy */
|
|
|
|
vc->retinfo = &(vc->vparams[numParams]);
|
|
|
|
*vc->retinfo = *retInfo;
|
|
|
|
vc->retinfo->offset = 0;
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
|
2007-06-16 17:40:22 +02:00
|
|
|
/* Allocate stack space */
|
|
|
|
vc->retbuf = new unsigned char[retBufSize];
|
|
|
|
} else {
|
|
|
|
vc->retinfo = NULL;
|
|
|
|
vc->retbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thisinfo)
|
|
|
|
{
|
|
|
|
/* Allocate and copy */
|
|
|
|
vc->thisinfo = &(vc->vparams[numParams + 1]);
|
|
|
|
*vc->thisinfo = *thisinfo;
|
|
|
|
vc->thisinfo->offset = 0;
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->thisinfo->obj_offset = 0;
|
2007-06-16 17:40:22 +02:00
|
|
|
} else {
|
|
|
|
vc->thisinfo = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now, save info about each parameter. */
|
2007-06-20 10:03:43 +02:00
|
|
|
size_t last_extra_offset = 0;
|
2007-06-16 17:40:22 +02:00
|
|
|
for (unsigned int i=0; i<numParams; i++)
|
|
|
|
{
|
|
|
|
/* Copy */
|
|
|
|
vc->vparams[i] = params[i];
|
|
|
|
vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->vparams[i].obj_offset = last_extra_offset;
|
|
|
|
last_extra_offset += sizes[i];
|
2007-06-16 17:40:22 +02:00
|
|
|
}
|
|
|
|
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->stackSize = normSize + extraSize;
|
|
|
|
vc->stackEnd = normSize;
|
|
|
|
|
2007-06-16 17:40:22 +02:00
|
|
|
return vc;
|
|
|
|
}
|
|
|
|
|
2007-06-16 10:36:34 +02:00
|
|
|
ValveCall *CreateValveVCall(unsigned int vtableIdx,
|
|
|
|
ValveCallType vcalltype,
|
|
|
|
const ValvePassInfo *retInfo,
|
|
|
|
const ValvePassInfo *params,
|
|
|
|
unsigned int numParams)
|
|
|
|
{
|
|
|
|
if (numParams > 32)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ValveCall *vc = new ValveCall;
|
|
|
|
|
2007-07-08 11:46:27 +02:00
|
|
|
vc->type = vcalltype;
|
|
|
|
|
2007-06-16 10:36:34 +02:00
|
|
|
size_t size = 0;
|
|
|
|
vc->stackSize = 0;
|
|
|
|
|
2007-06-20 10:03:43 +02:00
|
|
|
/* Get return information - encode only */
|
2007-06-16 10:36:34 +02:00
|
|
|
PassInfo retBuf;
|
|
|
|
size_t retBufSize = 0;
|
2007-06-20 10:03:43 +02:00
|
|
|
bool retbuf_needs_extra;
|
2007-06-16 10:36:34 +02:00
|
|
|
if (retInfo)
|
|
|
|
{
|
2007-06-20 10:03:43 +02:00
|
|
|
if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
|
2007-06-16 10:36:34 +02:00
|
|
|
{
|
|
|
|
delete vc;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-20 10:03:43 +02:00
|
|
|
retBufSize = retBuf.size;
|
|
|
|
if (retbuf_needs_extra)
|
|
|
|
{
|
|
|
|
retBufSize += size;
|
|
|
|
}
|
2007-06-16 10:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get parameter info */
|
|
|
|
PassInfo paramBuf[32];
|
2007-06-20 10:03:43 +02:00
|
|
|
size_t sizes[32];
|
|
|
|
size_t normSize = 0;
|
|
|
|
size_t extraSize = 0;
|
2007-06-16 10:36:34 +02:00
|
|
|
for (unsigned int i=0; i<numParams; i++)
|
|
|
|
{
|
2007-06-20 10:03:43 +02:00
|
|
|
bool needs_extra;
|
|
|
|
if ((size = ValveParamToBinParam(params[i].vtype,
|
|
|
|
params[i].type,
|
|
|
|
params[i].flags,
|
|
|
|
¶mBuf[i],
|
|
|
|
needs_extra)) == 0)
|
2007-06-16 10:36:34 +02:00
|
|
|
{
|
|
|
|
delete vc;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-06-20 10:03:43 +02:00
|
|
|
if (needs_extra)
|
|
|
|
{
|
|
|
|
sizes[i] = size;
|
|
|
|
} else {
|
|
|
|
sizes[i] = 0;
|
|
|
|
}
|
|
|
|
normSize += paramBuf[i].size;
|
|
|
|
extraSize += sizes[i];
|
2007-06-16 10:36:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now we can try creating the call */
|
|
|
|
if ((vc->call = g_pBinTools->CreateVCall(vtableIdx,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
(retInfo ? &retBuf : NULL),
|
|
|
|
paramBuf,
|
|
|
|
numParams))
|
|
|
|
== NULL)
|
|
|
|
{
|
|
|
|
if (!vc->call)
|
|
|
|
{
|
|
|
|
delete vc;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate extra space for thisptr AND ret buffer, even if we don't use it */
|
|
|
|
vc->vparams = new ValvePassInfo[numParams + 2];
|
|
|
|
|
|
|
|
/* We've got the call and everything is encoded.
|
|
|
|
* It's time to save the valve specific information and helper variables.
|
|
|
|
*/
|
|
|
|
if (retInfo)
|
|
|
|
{
|
|
|
|
/* Allocate and copy */
|
|
|
|
vc->retinfo = &(vc->vparams[numParams]);
|
|
|
|
*vc->retinfo = *retInfo;
|
|
|
|
vc->retinfo->offset = 0;
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
|
2007-06-16 10:36:34 +02:00
|
|
|
/* Allocate stack space */
|
|
|
|
vc->retbuf = new unsigned char[retBufSize];
|
|
|
|
} else {
|
|
|
|
vc->retinfo = NULL;
|
|
|
|
vc->retbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the this info for the dynamic decoder */
|
|
|
|
vc->thisinfo = &(vc->vparams[numParams + 1]);
|
|
|
|
vc->thisinfo->type = PassType_Basic;
|
2007-07-08 11:46:27 +02:00
|
|
|
switch (vcalltype)
|
2007-06-16 10:36:34 +02:00
|
|
|
{
|
2007-07-08 11:46:27 +02:00
|
|
|
case ValveCall_Entity:
|
2007-06-16 10:36:34 +02:00
|
|
|
vc->thisinfo->vtype = Valve_CBaseEntity;
|
2007-07-08 11:46:27 +02:00
|
|
|
vc->thisinfo->flags = PASSFLAG_BYVAL;
|
2007-06-20 09:08:53 +02:00
|
|
|
vc->thisinfo->decflags = VDECODE_FLAG_ALLOWWORLD;
|
2007-07-08 11:46:27 +02:00
|
|
|
break;
|
|
|
|
case ValveCall_Player:
|
2007-06-16 10:36:34 +02:00
|
|
|
vc->thisinfo->vtype = Valve_CBasePlayer;
|
2007-07-08 11:46:27 +02:00
|
|
|
vc->thisinfo->flags = PASSFLAG_BYVAL;
|
|
|
|
vc->thisinfo->decflags = 0;
|
|
|
|
break;
|
|
|
|
case ValveCall_GameRules:
|
|
|
|
vc->thisinfo->vtype = Valve_POD;
|
|
|
|
vc->thisinfo->flags = PASSFLAG_ASPOINTER;
|
2007-06-16 10:36:34 +02:00
|
|
|
vc->thisinfo->decflags = 0;
|
2007-07-08 11:46:27 +02:00
|
|
|
break;
|
2007-06-16 10:36:34 +02:00
|
|
|
}
|
|
|
|
vc->thisinfo->encflags = 0;
|
|
|
|
vc->thisinfo->offset = 0;
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->thisinfo->obj_offset = 0;
|
|
|
|
normSize += sizeof(void *);
|
2007-06-16 10:36:34 +02:00
|
|
|
|
|
|
|
/* Now, save info about each parameter. */
|
2007-06-20 10:03:43 +02:00
|
|
|
size_t last_extra_offset = 0;
|
2007-06-16 10:36:34 +02:00
|
|
|
for (unsigned int i=0; i<numParams; i++)
|
|
|
|
{
|
|
|
|
/* Copy */
|
|
|
|
vc->vparams[i] = params[i];
|
|
|
|
vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->vparams[i].obj_offset = last_extra_offset;
|
|
|
|
last_extra_offset += sizes[i];
|
2007-06-16 10:36:34 +02:00
|
|
|
}
|
|
|
|
|
2007-06-20 10:03:43 +02:00
|
|
|
vc->stackSize = normSize + extraSize;
|
|
|
|
vc->stackEnd = normSize;
|
|
|
|
|
2007-06-16 10:36:34 +02:00
|
|
|
return vc;
|
|
|
|
}
|