Fixed build for real.
3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
33 #include "vcallbuilder.h"
34 #include "extension.h"
36 ValveCall::ValveCall()
45 ValveCall::~ValveCall()
49 unsigned char *ptr = stk.front();
61 unsigned char *ValveCall::stk_get()
66 ptr = new unsigned char[stackSize];
74 void ValveCall::stk_put(unsigned char *ptr)
79 ValveCall *CreateValveCall(void *addr,
80 ValveCallType vcalltype,
81 const ValvePassInfo *retInfo,
82 const ValvePassInfo *params,
83 unsigned int numParams)
90 ValveCall *vc = new ValveCall;
97 /* Get return information - encode only */
99 size_t retBufSize = 0;
100 bool retbuf_needs_extra;
103 if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
108 retBufSize = retBuf.size;
111 /* Get parameter info */
112 PassInfo paramBuf[32];
115 size_t extraSize = 0;
116 for (unsigned int i=0; i<numParams; i++)
119 if ((size = ValveParamToBinParam(params[i].vtype,
134 normSize += paramBuf[i].size;
135 extraSize += sizes[i];
139 /* Get thisinfo if needed */
140 ValvePassInfo thisbuf;
141 ValvePassInfo *thisinfo = NULL;
142 CallConvention cv = CallConv_Cdecl;
143 if (vcalltype != ValveCall_Static)
146 thisinfo->type = PassType_Basic;
149 case ValveCall_Entity:
150 thisinfo->vtype = Valve_CBaseEntity;
151 thisinfo->flags = PASSFLAG_BYVAL;
152 thisinfo->decflags |= VDECODE_FLAG_ALLOWWORLD;
154 case ValveCall_Player:
155 thisinfo->vtype = Valve_CBasePlayer;
156 thisinfo->flags = PASSFLAG_BYVAL;
157 thisinfo->decflags = 0;
160 thisinfo->vtype = Valve_POD;
161 thisinfo->flags = PASSFLAG_ASPOINTER;
162 thisinfo->decflags = 0;
165 thisinfo->encflags = 0;
166 thisinfo->offset = 0;
167 normSize += sizeof(void *);
168 cv = CallConv_ThisCall;
171 /* Now we can try creating the call */
172 if ((vc->call = g_pBinTools->CreateCall(addr,
174 (retInfo ? &retBuf : NULL),
186 /* Allocate extra space for thisptr AND ret buffer, even if we don't use it */
187 vc->vparams = new ValvePassInfo[numParams + 2];
189 /* We've got the call and everything is encoded.
190 * It's time to save the valve specific information and helper variables.
194 /* Allocate and copy */
195 vc->retinfo = &(vc->vparams[numParams]);
196 *vc->retinfo = *retInfo;
197 vc->retinfo->offset = 0;
198 vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
199 /* Allocate stack space */
200 vc->retbuf = new unsigned char[retBufSize];
208 /* Allocate and copy */
209 vc->thisinfo = &(vc->vparams[numParams + 1]);
210 *vc->thisinfo = *thisinfo;
211 vc->thisinfo->offset = 0;
212 vc->thisinfo->obj_offset = 0;
217 /* Now, save info about each parameter. */
218 size_t last_extra_offset = 0;
219 for (unsigned int i=0; i<numParams; i++)
222 vc->vparams[i] = params[i];
223 vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
224 vc->vparams[i].obj_offset = last_extra_offset;
225 last_extra_offset += sizes[i];
228 vc->stackSize = normSize + extraSize;
229 vc->stackEnd = normSize;
234 ValveCall *CreateValveVCall(unsigned int vtableIdx,
235 ValveCallType vcalltype,
236 const ValvePassInfo *retInfo,
237 const ValvePassInfo *params,
238 unsigned int numParams)
245 ValveCall *vc = new ValveCall;
247 vc->type = vcalltype;
252 /* Get return information - encode only */
254 size_t retBufSize = 0;
255 bool retbuf_needs_extra;
258 if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
263 retBufSize = retBuf.size;
266 /* Get parameter info */
267 PassInfo paramBuf[32];
270 size_t extraSize = 0;
271 for (unsigned int i=0; i<numParams; i++)
274 if ((size = ValveParamToBinParam(params[i].vtype,
289 normSize += paramBuf[i].size;
290 extraSize += sizes[i];
293 /* Now we can try creating the call */
294 if ((vc->call = g_pBinTools->CreateVCall(vtableIdx,
297 (retInfo ? &retBuf : NULL),
309 /* Allocate extra space for thisptr AND ret buffer, even if we don't use it */
310 vc->vparams = new ValvePassInfo[numParams + 2];
312 /* We've got the call and everything is encoded.
313 * It's time to save the valve specific information and helper variables.
317 /* Allocate and copy */
318 vc->retinfo = &(vc->vparams[numParams]);
319 *vc->retinfo = *retInfo;
320 vc->retinfo->offset = 0;
321 vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
322 /* Allocate stack space */
323 vc->retbuf = new unsigned char[retBufSize];
329 /* Save the this info for the dynamic decoder */
330 vc->thisinfo = &(vc->vparams[numParams + 1]);
331 vc->thisinfo->type = PassType_Basic;
334 case ValveCall_Entity:
335 vc->thisinfo->vtype = Valve_CBaseEntity;
336 vc->thisinfo->flags = PASSFLAG_BYVAL;
337 vc->thisinfo->decflags = VDECODE_FLAG_ALLOWWORLD;
339 case ValveCall_Player:
340 vc->thisinfo->vtype = Valve_CBasePlayer;
341 vc->thisinfo->flags = PASSFLAG_BYVAL;
342 vc->thisinfo->decflags = 0;
345 vc->thisinfo->vtype = Valve_POD;
346 vc->thisinfo->flags = PASSFLAG_ASPOINTER;
347 vc->thisinfo->decflags = 0;
350 vc->thisinfo->encflags = 0;
351 vc->thisinfo->offset = 0;
352 vc->thisinfo->obj_offset = 0;
353 normSize += sizeof(void *);
355 /* Now, save info about each parameter. */
356 size_t last_extra_offset = 0;
357 for (unsigned int i=0; i<numParams; i++)
360 vc->vparams[i] = params[i];
361 vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
362 vc->vparams[i].obj_offset = last_extra_offset;
363 last_extra_offset += sizes[i];
366 vc->stackSize = normSize + extraSize;
367 vc->stackEnd = normSize;