/
vcallbuilder.cpp
378 lines (345 loc) · 8.99 KB
/
vcallbuilder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
* vim: set ts=4 :
* =============================================================================
* SourceMod SDKTools Extension
* Copyright (C) 2004-2008 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, version 3.0, as published by the
* Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
* As a special exception, AlliedModders LLC gives you permission to link the
* code of this program (as well as its derivative works) to "Half-Life 2," the
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, AlliedModders LLC grants
* this exception to all derivative works. AlliedModders LLC defines further
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
* or <http://www.sourcemod.net/license.php>.
*
* Version: $Id$
*/
#include <stdio.h>
#include "vcallbuilder.h"
#include "extension.h"
ValveCall::ValveCall()
{
call = NULL;
vparams = NULL;
retinfo = NULL;
thisinfo = NULL;
retbuf = NULL;
}
ValveCall::~ValveCall()
{
while (!stk.empty())
{
unsigned char *ptr = stk.front();
delete [] ptr;
stk.pop();
}
if (call)
{
call->Destroy();
}
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);
}
ValveCall *CreateValveCall(void *addr,
ValveCallType vcalltype,
const ValvePassInfo *retInfo,
const ValvePassInfo *params,
unsigned int numParams)
{
if (numParams > 32)
{
return NULL;
}
ValveCall *vc = new ValveCall;
vc->type = vcalltype;
size_t size = 0;
vc->stackSize = 0;
/* Get return information - encode only */
PassInfo retBuf;
ObjectField retFieldBuf[16];
size_t retBufSize = 0;
bool retbuf_needs_extra;
if (retInfo)
{
retBuf.fields = retFieldBuf;
if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
{
delete vc;
return NULL;
}
retBufSize = retBuf.size;
}
/* Get parameter info */
PassInfo paramBuf[32];
ObjectField fieldBuf[32][16];
size_t sizes[32];
size_t normSize = 0;
size_t extraSize = 0;
for (unsigned int i=0; i<numParams; i++)
{
bool needs_extra;
paramBuf[i].fields = fieldBuf[i];
if ((size = ValveParamToBinParam(params[i].vtype,
params[i].type,
params[i].flags,
¶mBuf[i],
needs_extra)) == 0)
{
delete vc;
return NULL;
}
if (needs_extra)
{
sizes[i] = size;
} else {
sizes[i] = 0;
}
normSize += paramBuf[i].size;
extraSize += sizes[i];
}
/* Get thisinfo if needed */
ValvePassInfo thisbuf;
ValvePassInfo *thisinfo = NULL;
CallConvention cv = CallConv_Cdecl;
if (vcalltype != ValveCall_Static)
{
thisinfo = &thisbuf;
thisinfo->type = PassType_Basic;
switch (vcalltype)
{
case ValveCall_Entity:
thisinfo->vtype = Valve_CBaseEntity;
thisinfo->flags = PASSFLAG_BYVAL;
thisinfo->decflags |= VDECODE_FLAG_ALLOWWORLD;
break;
case ValveCall_Player:
thisinfo->vtype = Valve_CBasePlayer;
thisinfo->flags = PASSFLAG_BYVAL;
thisinfo->decflags = 0;
break;
default:
thisinfo->vtype = Valve_POD;
thisinfo->flags = PASSFLAG_ASPOINTER;
thisinfo->decflags = 0;
break;
}
thisinfo->encflags = 0;
thisinfo->offset = 0;
normSize += sizeof(void *);
cv = CallConv_ThisCall;
}
/* 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;
vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
/* 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;
vc->thisinfo->obj_offset = 0;
} else {
vc->thisinfo = NULL;
}
/* Now, save info about each parameter. */
size_t last_extra_offset = 0;
for (unsigned int i=0; i<numParams; i++)
{
/* Copy */
vc->vparams[i] = params[i];
vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
vc->vparams[i].obj_offset = last_extra_offset;
last_extra_offset += sizes[i];
}
vc->stackSize = normSize + extraSize;
vc->stackEnd = normSize;
return vc;
}
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;
vc->type = vcalltype;
size_t size = 0;
vc->stackSize = 0;
/* Get return information - encode only */
PassInfo retBuf;
ObjectField retFieldBuf[16];
size_t retBufSize = 0;
bool retbuf_needs_extra;
if (retInfo)
{
retBuf.fields = retFieldBuf;
if ((size = ValveParamToBinParam(retInfo->vtype, retInfo->type, retInfo->flags, &retBuf, retbuf_needs_extra)) == 0)
{
delete vc;
return NULL;
}
retBufSize = retBuf.size;
}
/* Get parameter info */
PassInfo paramBuf[32];
ObjectField fieldBuf[32][16];
size_t sizes[32];
size_t normSize = 0;
size_t extraSize = 0;
for (unsigned int i=0; i<numParams; i++)
{
bool needs_extra;
paramBuf[i].fields = fieldBuf[i];
if ((size = ValveParamToBinParam(params[i].vtype,
params[i].type,
params[i].flags,
¶mBuf[i],
needs_extra)) == 0)
{
delete vc;
return NULL;
}
if (needs_extra)
{
sizes[i] = size;
} else {
sizes[i] = 0;
}
normSize += paramBuf[i].size;
extraSize += sizes[i];
}
/* 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;
vc->retinfo->obj_offset = retbuf_needs_extra ? sizeof(void *) : 0;
/* 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;
switch (vcalltype)
{
case ValveCall_Entity:
vc->thisinfo->vtype = Valve_CBaseEntity;
vc->thisinfo->flags = PASSFLAG_BYVAL;
vc->thisinfo->decflags = VDECODE_FLAG_ALLOWWORLD;
break;
case ValveCall_Player:
vc->thisinfo->vtype = Valve_CBasePlayer;
vc->thisinfo->flags = PASSFLAG_BYVAL;
vc->thisinfo->decflags = 0;
break;
default:
vc->thisinfo->vtype = Valve_POD;
vc->thisinfo->flags = PASSFLAG_ASPOINTER;
vc->thisinfo->decflags = 0;
break;
}
vc->thisinfo->encflags = 0;
vc->thisinfo->offset = 0;
vc->thisinfo->obj_offset = 0;
normSize += sizeof(void *);
/* Now, save info about each parameter. */
size_t last_extra_offset = 0;
for (unsigned int i=0; i<numParams; i++)
{
/* Copy */
vc->vparams[i] = params[i];
vc->vparams[i].offset = vc->call->GetParamInfo(i)->offset;
vc->vparams[i].obj_offset = last_extra_offset;
last_extra_offset += sizes[i];
}
vc->stackSize = normSize + extraSize;
vc->stackEnd = normSize;
return vc;
}