extensions/sdktools/vcallbuilder.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/extensions/sdktools/vcallbuilder.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,369 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod SDKTools Extension
     1.8 - * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     1.9 - * =============================================================================
    1.10 - *
    1.11 - * This program is free software; you can redistribute it and/or modify it under
    1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
    1.13 - * Free Software Foundation.
    1.14 - * 
    1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
    1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 - * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    1.18 - * details.
    1.19 - *
    1.20 - * You should have received a copy of the GNU General Public License along with
    1.21 - * this program.  If not, see <http://www.gnu.org/licenses/>.
    1.22 - *
    1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
    1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
    1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    1.26 - * by the Valve Corporation.  You must obey the GNU General Public License in
    1.27 - * all respects for all other code used.  Additionally, AlliedModders LLC grants
    1.28 - * this exception to all derivative works.  AlliedModders LLC defines further
    1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    1.30 - * or <http://www.sourcemod.net/license.php>.
    1.31 - *
    1.32 - * Version: $Id$
    1.33 - */
    1.34 -
    1.35 -#include "vcallbuilder.h"
    1.36 -#include "extension.h"
    1.37 -
    1.38 -ValveCall::ValveCall()
    1.39 -{
    1.40 -	call = NULL;
    1.41 -	vparams = NULL;
    1.42 -	retinfo = NULL;
    1.43 -	thisinfo = NULL;
    1.44 -	retbuf = NULL;
    1.45 -}
    1.46 -
    1.47 -ValveCall::~ValveCall()
    1.48 -{
    1.49 -	while (!stk.empty())
    1.50 -	{
    1.51 -		unsigned char *ptr = stk.front();
    1.52 -		delete [] ptr;
    1.53 -		stk.pop();
    1.54 -	}
    1.55 -	if (call)
    1.56 -	{
    1.57 -		call->Destroy();
    1.58 -	}
    1.59 -	delete [] retbuf;
    1.60 -	delete [] vparams;
    1.61 -}
    1.62 -
    1.63 -unsigned char *ValveCall::stk_get()
    1.64 -{
    1.65 -	unsigned char *ptr;
    1.66 -	if (stk.empty())
    1.67 -	{
    1.68 -		ptr = new unsigned char[stackSize];
    1.69 -	} else {
    1.70 -		ptr = stk.front();
    1.71 -		stk.pop();
    1.72 -	}
    1.73 -	return ptr;
    1.74 -}
    1.75 -
    1.76 -void ValveCall::stk_put(unsigned char *ptr)
    1.77 -{
    1.78 -	stk.push(ptr);
    1.79 -}
    1.80 -
    1.81 -ValveCall *CreateValveCall(void *addr,
    1.82 -						   ValveCallType vcalltype,
    1.83 -						   const ValvePassInfo *retInfo,
    1.84 -						   const ValvePassInfo *params,
    1.85 -						   unsigned int numParams)
    1.86 -{
    1.87 -	if (numParams > 32)
    1.88 -	{
    1.89 -		return NULL;
    1.90 -	}
    1.91 -
    1.92 -	ValveCall *vc = new ValveCall;
    1.93 -
    1.94 -	vc->type = vcalltype;
    1.95 -
    1.96 -	size_t size = 0;
    1.97 -	vc->stackSize = 0;
    1.98 -
    1.99 -	/* Get return information - encode only */
   1.100 -	PassInfo retBuf;
   1.101 -	size_t retBufSize = 0;
   1.102 -	bool retbuf_needs_extra;
   1.103 -	if (retInfo)
   1.104 -	{
   1.105 -		if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
   1.106 -		{
   1.107 -			delete vc;
   1.108 -			return NULL;
   1.109 -		}
   1.110 -		retBufSize = retBuf.size;
   1.111 -	}
   1.112 -
   1.113 -	/* Get parameter info */
   1.114 -	PassInfo paramBuf[32];
   1.115 -	size_t sizes[32];
   1.116 -	size_t normSize = 0;
   1.117 -	size_t extraSize = 0;
   1.118 -	for (unsigned int i=0; i<numParams; i++)
   1.119 -	{
   1.120 -		bool needs_extra;
   1.121 -		if ((size = ValveParamToBinParam(params[i].vtype, 
   1.122 -			params[i].type,
   1.123 -			params[i].flags,
   1.124 -			&paramBuf[i],
   1.125 -			needs_extra)) == 0)
   1.126 -		{
   1.127 -			delete vc;
   1.128 -			return NULL;
   1.129 -		}
   1.130 -		if (needs_extra)
   1.131 -		{
   1.132 -			sizes[i] = size;
   1.133 -		} else {
   1.134 -			sizes[i] = 0;
   1.135 -		}
   1.136 -		normSize += paramBuf[i].size;
   1.137 -		extraSize += sizes[i];
   1.138 -	}
   1.139 -
   1.140 -
   1.141 -	/* Get thisinfo if needed */
   1.142 -	ValvePassInfo thisbuf;
   1.143 -	ValvePassInfo *thisinfo = NULL;
   1.144 -	CallConvention cv = CallConv_Cdecl;
   1.145 -	if (vcalltype != ValveCall_Static)
   1.146 -	{
   1.147 -		thisinfo = &thisbuf;
   1.148 -		thisinfo->type = PassType_Basic;
   1.149 -		switch (vcalltype)
   1.150 -		{
   1.151 -		case ValveCall_Entity:
   1.152 -			thisinfo->vtype = Valve_CBaseEntity;
   1.153 -			thisinfo->flags = PASSFLAG_BYVAL;
   1.154 -			thisinfo->decflags |= VDECODE_FLAG_ALLOWWORLD;
   1.155 -			break;
   1.156 -		case ValveCall_Player:
   1.157 -			thisinfo->vtype = Valve_CBasePlayer;
   1.158 -			thisinfo->flags = PASSFLAG_BYVAL;
   1.159 -			thisinfo->decflags = 0;
   1.160 -			break;
   1.161 -		default:
   1.162 -			thisinfo->vtype = Valve_POD;
   1.163 -			thisinfo->flags = PASSFLAG_ASPOINTER;
   1.164 -			thisinfo->decflags = 0;
   1.165 -			break;
   1.166 -		}
   1.167 -		thisinfo->encflags = 0;
   1.168 -		thisinfo->offset = 0;
   1.169 -		normSize += sizeof(void *);
   1.170 -		cv = CallConv_ThisCall;
   1.171 -	}
   1.172 -
   1.173 -	/* Now we can try creating the call */
   1.174 -	if ((vc->call = g_pBinTools->CreateCall(addr,
   1.175 -		cv,
   1.176 -		(retInfo ? &retBuf : NULL),
   1.177 -		paramBuf,
   1.178 -		numParams))
   1.179 -		== NULL)
   1.180 -	{
   1.181 -		if (!vc->call)
   1.182 -		{
   1.183 -			delete vc;
   1.184 -			return NULL;
   1.185 -		}
   1.186 -	}
   1.187 -
   1.188 -	/* Allocate extra space for thisptr AND ret buffer, even if we don't use it */
   1.189 -	vc->vparams = new ValvePassInfo[numParams + 2];
   1.190 -
   1.191 -	/* We've got the call and everything is encoded.
   1.192 -	 * It's time to save the valve specific information and helper variables.
   1.193 -	 */
   1.194 -	if (retInfo)
   1.195 -	{
   1.196 -		/* Allocate and copy */
   1.197 -		vc->retinfo = &(vc->vparams[numParams]);
   1.198 -		*vc->retinfo = *retInfo;
   1.199 -		vc->retinfo->offset = 0;
   1.200 -		vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
   1.201 -		/* Allocate stack space */
   1.202 -		vc->retbuf = new unsigned char[retBufSize];
   1.203 -	} else {
   1.204 -		vc->retinfo = NULL;
   1.205 -		vc->retbuf = NULL;
   1.206 -	}
   1.207 -
   1.208 -	if (thisinfo)
   1.209 -	{
   1.210 -		/* Allocate and copy */
   1.211 -		vc->thisinfo = &(vc->vparams[numParams + 1]);
   1.212 -		*vc->thisinfo = *thisinfo;
   1.213 -		vc->thisinfo->offset = 0;
   1.214 -		vc->thisinfo->obj_offset = 0;
   1.215 -	} else {
   1.216 -		vc->thisinfo = NULL;
   1.217 -	}
   1.218 -
   1.219 -	/* Now, save info about each parameter. */
   1.220 -	size_t last_extra_offset = 0;
   1.221 -	for (unsigned int i=0; i<numParams; i++)
   1.222 -	{
   1.223 -		/* Copy */
   1.224 -		vc->vparams[i] = params[i];
   1.225 -		vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
   1.226 -		vc->vparams[i].obj_offset = last_extra_offset;
   1.227 -		last_extra_offset += sizes[i];
   1.228 -	}
   1.229 -
   1.230 -	vc->stackSize = normSize + extraSize;
   1.231 -	vc->stackEnd = normSize;
   1.232 -
   1.233 -	return vc;
   1.234 -}
   1.235 -
   1.236 -ValveCall *CreateValveVCall(unsigned int vtableIdx,
   1.237 -							ValveCallType vcalltype,
   1.238 -							const ValvePassInfo *retInfo,
   1.239 -							const ValvePassInfo *params,
   1.240 -							unsigned int numParams)
   1.241 -{
   1.242 -	if (numParams > 32)
   1.243 -	{
   1.244 -		return NULL;
   1.245 -	}
   1.246 -
   1.247 -	ValveCall *vc = new ValveCall;
   1.248 -
   1.249 -	vc->type = vcalltype;
   1.250 -
   1.251 -	size_t size = 0;
   1.252 -	vc->stackSize = 0;
   1.253 -
   1.254 -	/* Get return information - encode only */
   1.255 -	PassInfo retBuf;
   1.256 -	size_t retBufSize = 0;
   1.257 -	bool retbuf_needs_extra;
   1.258 -	if (retInfo)
   1.259 -	{
   1.260 -		if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
   1.261 -		{
   1.262 -			delete vc;
   1.263 -			return NULL;
   1.264 -		}
   1.265 -		retBufSize = retBuf.size;
   1.266 -	}
   1.267 -
   1.268 -	/* Get parameter info */
   1.269 -	PassInfo paramBuf[32];
   1.270 -	size_t sizes[32];
   1.271 -	size_t normSize = 0;
   1.272 -	size_t extraSize = 0;
   1.273 -	for (unsigned int i=0; i<numParams; i++)
   1.274 -	{
   1.275 -		bool needs_extra;
   1.276 -		if ((size = ValveParamToBinParam(params[i].vtype, 
   1.277 -										params[i].type,
   1.278 -										params[i].flags,
   1.279 -										&paramBuf[i],
   1.280 -										needs_extra)) == 0)
   1.281 -		{
   1.282 -			delete vc;
   1.283 -			return NULL;
   1.284 -		}
   1.285 -		if (needs_extra)
   1.286 -		{
   1.287 -			sizes[i] = size;
   1.288 -		} else {
   1.289 -			sizes[i] = 0;
   1.290 -		}
   1.291 -		normSize += paramBuf[i].size;
   1.292 -		extraSize += sizes[i];
   1.293 -	}
   1.294 -
   1.295 -	/* Now we can try creating the call */
   1.296 -	if ((vc->call = g_pBinTools->CreateVCall(vtableIdx, 
   1.297 -		0, 
   1.298 -		0, 
   1.299 -		(retInfo ? &retBuf : NULL),
   1.300 -		paramBuf,
   1.301 -		numParams))
   1.302 -		== NULL)
   1.303 -	{
   1.304 -		if (!vc->call)
   1.305 -		{
   1.306 -			delete vc;
   1.307 -			return NULL;
   1.308 -		}
   1.309 -	}
   1.310 -
   1.311 -	/* Allocate extra space for thisptr AND ret buffer, even if we don't use it */
   1.312 -	vc->vparams = new ValvePassInfo[numParams + 2];
   1.313 -
   1.314 -	/* We've got the call and everything is encoded.
   1.315 -	 * It's time to save the valve specific information and helper variables.
   1.316 -	 */
   1.317 -	if (retInfo)
   1.318 -	{
   1.319 -		/* Allocate and copy */
   1.320 -		vc->retinfo = &(vc->vparams[numParams]);
   1.321 -		*vc->retinfo = *retInfo;
   1.322 -		vc->retinfo->offset = 0;
   1.323 -		vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
   1.324 -		/* Allocate stack space */
   1.325 -		vc->retbuf = new unsigned char[retBufSize];
   1.326 -	} else {
   1.327 -		vc->retinfo = NULL;
   1.328 -		vc->retbuf = NULL;
   1.329 -	}
   1.330 -
   1.331 -	/* Save the this info for the dynamic decoder */
   1.332 -	vc->thisinfo = &(vc->vparams[numParams + 1]);
   1.333 -	vc->thisinfo->type = PassType_Basic;
   1.334 -	switch (vcalltype)
   1.335 -	{
   1.336 -	case ValveCall_Entity:
   1.337 -		vc->thisinfo->vtype = Valve_CBaseEntity;
   1.338 -		vc->thisinfo->flags = PASSFLAG_BYVAL;
   1.339 -		vc->thisinfo->decflags = VDECODE_FLAG_ALLOWWORLD;
   1.340 -		break;
   1.341 -	case ValveCall_Player:
   1.342 -		vc->thisinfo->vtype = Valve_CBasePlayer;
   1.343 -		vc->thisinfo->flags = PASSFLAG_BYVAL;
   1.344 -		vc->thisinfo->decflags = 0;
   1.345 -		break;
   1.346 -	default:
   1.347 -		vc->thisinfo->vtype = Valve_POD;
   1.348 -		vc->thisinfo->flags = PASSFLAG_ASPOINTER;
   1.349 -		vc->thisinfo->decflags = 0;
   1.350 -		break;
   1.351 -	}
   1.352 -	vc->thisinfo->encflags = 0;
   1.353 -	vc->thisinfo->offset = 0;
   1.354 -	vc->thisinfo->obj_offset = 0;
   1.355 -	normSize += sizeof(void *);
   1.356 -
   1.357 -	/* Now, save info about each parameter. */
   1.358 -	size_t last_extra_offset = 0;
   1.359 -	for (unsigned int i=0; i<numParams; i++)
   1.360 -	{
   1.361 -		/* Copy */
   1.362 -		vc->vparams[i] = params[i];
   1.363 -		vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
   1.364 -		vc->vparams[i].obj_offset = last_extra_offset;
   1.365 -		last_extra_offset += sizes[i];
   1.366 -	}
   1.367 -
   1.368 -	vc->stackSize = normSize + extraSize;
   1.369 -	vc->stackEnd = normSize;
   1.370 -
   1.371 -	return vc;
   1.372 -}