-
Notifications
You must be signed in to change notification settings - Fork 1
/
typeconverter.cpp
275 lines (242 loc) · 6.21 KB
/
typeconverter.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
#include "typeconverter.h"
#include <IBinTools.h>
SourceMod::CallConvention Masterhook_TypeConverter::ToSourcemodCallConvention(eCallConv conv)
{
switch (conv) {
case CONV_THISCALL: {
return SourceMod::CallConv_ThisCall;
}
}
return SourceMod::CallConv_Cdecl;
}
/**
* For object pointers, the data looks like this instead:
* 4 bytes: POINTER TO LATER
* + bytes: Object internal data
*
* We use the virtual stack as extra fake stack space and create a temp object.
* If these objects had destructors, we'd need to fake destroy toom of course.
* Of course, BinTools only reads the first four bytes and passes the pointer.
*/
size_t Masterhook_TypeConverter::ToBinParam(MasterhookDataType type,
unsigned int flags,
PassInfo *info,
bool &needs_extra)
{
needs_extra = false;
switch (type)
{
case MhDataType_Vector:
{
size_t mySize = sizeof(Vector *);
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(Vector *);
mySize = sizeof(Vector);
needs_extra = true;
return mySize;
}
case MhDataType_QAngle: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(QAngle *);
needs_extra = true;
return sizeof(QAngle);
}
case MhDataType_CBaseEntity:
case MhDataType_Edict:
case MhDataType_Char_Ptr:
case MhDataType_Void: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(void *);
return sizeof(void *);
}
case MhDataType_Char: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(char);
return sizeof(char);
}
case MhDataType_Int8: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(byte);
return sizeof(byte);
}
case MhDataType_Int8_Ptr: {
info->type = PassType_Basic;
info->flags = flags;
needs_extra = true;
info->size = sizeof(byte *);
return sizeof(byte *) + sizeof(byte);
}
case MhDataType_Int16: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(short);
return sizeof(short);
}
case MhDataType_Int16_Ptr: {
info->type = PassType_Basic;
info->flags = flags;
needs_extra = true;
info->size = sizeof(short *);
return sizeof(short *) + sizeof(short);
}
case MhDataType_Int32: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(int);
return sizeof(int);
}
case MhDataType_Int32_Ptr: {
info->type = PassType_Basic;
info->flags = flags;
needs_extra = true;
info->size = sizeof(int *);
return sizeof(int *) + sizeof(int);
}
case MhDataType_Int64: {
info->type = PassType_Basic;
info->flags = flags;
info->size = sizeof(float);
return sizeof(float);
}
case MhDataType_Int64_Ptr: {
info->type = PassType_Basic;
info->flags = flags;
needs_extra = true;
info->size = sizeof(int64 *);
return sizeof(int64 *) + sizeof(int64);
}
case MhDataType_Float: {
info->type = PassType_Float;
info->size = sizeof(float);
return sizeof(float);
}
case MhDataType_Float_Ptr: {
needs_extra = true;
info->type = PassType_Basic;
info->size = sizeof(int64 *);
return sizeof(int64 *) + sizeof(int64);
}
case MhDataType_Bool:
{
info->type = PassType_Basic;
info->size = sizeof(bool);
return sizeof(bool);
}
}
return 0;
}
bool Masterhook_TypeConverter::Decode(IPluginContext *pContext,
cell_t param,
const PassInfo *data,
const MasterhookDataType dataType,
void *_buffer)
{
void *buffer = (unsigned char *)_buffer + data->size;
switch (dataType) {
case MhDataType_Vector: {
cell_t *addr;
int err;
err = pContext->LocalToPhysAddr(param, &addr);
unsigned char *mem = (unsigned char *)buffer;
/* Store the object in the next N bytes, and store
* a pointer to that object right beforehand.
*/
Vector **realPtr = (Vector **)buffer;
if (addr == pContext->GetNullRef(SP_NULL_VECTOR)) {
*realPtr = NULL;
return true;
}
else {
//mem = (unsigned char *)_buffer + pCall->stackEnd + data->obj_offset;
*realPtr = (Vector *)mem;
}
if (err != SP_ERROR_NONE) {
pContext->ThrowNativeErrorEx(err, "Could not read plugin data");
return false;
}
/* Use placement new to initialize the object cleanly
* This has no destructor so we don't need to do
* DestroyValveParam() or something :]
*/
Vector *v = new (mem) Vector(
sp_ctof(addr[0]),
sp_ctof(addr[1]),
sp_ctof(addr[2])
);
return true;
}
case MhDataType_QAngle: {
cell_t *addr;
int err;
err = pContext->LocalToPhysAddr(param, &addr);
unsigned char *mem = (unsigned char *)buffer;
/* Store the object in the next N bytes, and store
* a pointer to that object right beforehand.
*/
QAngle **realPtr = (QAngle **)buffer;
if (addr == pContext->GetNullRef(SP_NULL_VECTOR)) {
*realPtr = NULL;
return true;
}
else {
//mem = (unsigned char *)_buffer + pCall->stackEnd + data->obj_offset;
*realPtr = (QAngle *)mem;
}
if (err != SP_ERROR_NONE) {
pContext->ThrowNativeErrorEx(err, "Could not read plugin data");
return false;
}
/* Use placement new to initialize the object cleanly
* This has no destructor so we don't need to do
* DestroyValveParam() or something :]
*/
QAngle *v = new (mem) QAngle(
sp_ctof(addr[0]),
sp_ctof(addr[1]),
sp_ctof(addr[2])
);
return true;
}
case MhDataType_CBaseEntity: {
CBaseEntity *pEntity = NULL;
int index = gamehelpers->ReferenceToIndex(param);
if ((unsigned)index == INVALID_EHANDLE_INDEX && param != -1)
{
return false;
}
CBaseEntity **ebuf = (CBaseEntity **)buffer;
*ebuf = pEntity;
return true;
}
case MhDataType_Edict: {
edict_t *pEdict;
edict_t **ebuf = (edict_t **)buffer;
*ebuf = pEdict;
return true;
}
case MhDataType_Float:
case MhDataType_Int8:
case MhDataType_Int16:
case MhDataType_Int32:
case MhDataType_Int64: {
*(cell_t *)buffer = param;
return true;
}
case MhDataType_Bool: {
*(bool *)buffer = param ? true : false;
return true;
}
case MhDataType_Char_Ptr: {
char *addr;
pContext->LocalToString(param, &addr);
*(char **)buffer = addr;
return true;
}
}
return false;
}