-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
inputnatives.cpp
274 lines (227 loc) · 7.89 KB
/
inputnatives.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
/**
* 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 "extension.h"
#include <datamap.h>
#define SIZEOF_VARIANT_T 20
ICallWrapper *g_pAcceptInput = NULL;
unsigned char g_Variant_t[SIZEOF_VARIANT_T] = {0};
#define ENTINDEX_TO_CBASEENTITY(ref, buffer) \
buffer = gamehelpers->ReferenceToEntity(ref); \
if (!buffer) \
{ \
return pContext->ThrowNativeError("Entity %d (%d) is not a CBaseEntity", gamehelpers->ReferenceToIndex(ref), ref); \
}
/* Hack to init the variant_t object for the first time */
class VariantFirstTimeInit
{
public:
VariantFirstTimeInit()
{
*(unsigned int *)(&g_Variant_t[12]) = INVALID_EHANDLE_INDEX;
}
} g_VariantFirstTimeInit;
inline void _init_variant_t()
{
unsigned char *vptr = g_Variant_t;
*(int *)vptr = 0;
vptr += sizeof(int)*3;
*(unsigned long *)vptr = INVALID_EHANDLE_INDEX;
vptr += sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_VOID;
}
static cell_t AcceptEntityInput(IPluginContext *pContext, const cell_t *params)
{
if (!g_pAcceptInput)
{
int offset;
if (!g_pGameConf->GetOffset("AcceptInput", &offset))
{
return pContext->ThrowNativeError("\"AcceptEntityInput\" not supported by this mod");
}
PassInfo pass[6];
pass[0].type = PassType_Basic;
pass[0].flags = PASSFLAG_BYVAL;
pass[0].size = sizeof(const char *);
pass[1].type = pass[2].type = PassType_Basic;
pass[1].flags = pass[2].flags = PASSFLAG_BYVAL;
pass[1].size = pass[2].size = sizeof(CBaseEntity *);
pass[3].type = PassType_Object;
pass[3].flags = PASSFLAG_BYVAL|PASSFLAG_OCTOR|PASSFLAG_ODTOR|PASSFLAG_OASSIGNOP;
pass[3].size = SIZEOF_VARIANT_T;
pass[4].type = PassType_Basic;
pass[4].flags = PASSFLAG_BYVAL;
pass[4].size = sizeof(int);
pass[5].type = PassType_Basic;
pass[5].flags = PASSFLAG_BYVAL;
pass[5].size = sizeof(bool);
if (!(g_pAcceptInput=g_pBinTools->CreateVCall(offset, 0, 0, &pass[5], pass, 5)))
{
pContext->ThrowNativeError("\"AcceptEntityInput\" wrapper failed to initialized");
}
}
CBaseEntity *pActivator, *pCaller, *pDest;
char *inputname;
unsigned char vstk[sizeof(void *) + sizeof(const char *) + sizeof(CBaseEntity *)*2 + SIZEOF_VARIANT_T + sizeof(int)];
unsigned char *vptr = vstk;
ENTINDEX_TO_CBASEENTITY(params[1], pDest);
pContext->LocalToString(params[2], &inputname);
if (params[3] == -1)
{
pActivator = NULL;
} else {
ENTINDEX_TO_CBASEENTITY(params[3], pActivator);
}
if (params[4] == -1)
{
pCaller = NULL;
} else {
ENTINDEX_TO_CBASEENTITY(params[4], pCaller);
}
*(void **)vptr = pDest;
vptr += sizeof(void *);
*(const char **)vptr = inputname;
vptr += sizeof(const char *);
*(CBaseEntity **)vptr = pActivator;
vptr += sizeof(CBaseEntity *);
*(CBaseEntity **)vptr = pCaller;
vptr += sizeof(CBaseEntity *);
memcpy(vptr, g_Variant_t, SIZEOF_VARIANT_T);
vptr += SIZEOF_VARIANT_T;
*(int *)vptr = params[5];
bool ret;
g_pAcceptInput->Execute(vstk, &ret);
_init_variant_t();
return (ret) ? 1 : 0;
}
static cell_t SetVariantBool(IPluginContext *pContext, const cell_t *params)
{
unsigned char *vptr = g_Variant_t;
*(bool *)vptr = (params[1]) ? true : false;
vptr += sizeof(int)*3 + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_BOOLEAN;
return 1;
}
static cell_t SetVariantString(IPluginContext *pContext, const cell_t *params)
{
char *str;
unsigned char *vptr = g_Variant_t;
pContext->LocalToString(params[1], &str);
*(string_t *)vptr = MAKE_STRING(str);
vptr += sizeof(int)*3 + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_STRING;
return 1;
}
static cell_t SetVariantInt(IPluginContext *pContext, const cell_t *params)
{
unsigned char *vptr = g_Variant_t;
*(int *)vptr = params[1];
vptr += sizeof(int)*3 + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_INTEGER;
return 1;
}
static cell_t SetVariantFloat(IPluginContext *pContext, const cell_t *params)
{
unsigned char *vptr = g_Variant_t;
*(float *)vptr = sp_ctof(params[1]);
vptr += sizeof(int)*3 + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_FLOAT;
return 1;
}
static cell_t SetVariantVector3D(IPluginContext *pContext, const cell_t *params)
{
cell_t *val;
unsigned char *vptr = g_Variant_t;
pContext->LocalToPhysAddr(params[1], &val);
*(float *)vptr = sp_ctof(val[0]);
vptr += sizeof(float);
*(float *)vptr = sp_ctof(val[1]);
vptr += sizeof(float);
*(float *)vptr = sp_ctof(val[2]);
vptr += sizeof(float) + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_VECTOR;
return 1;
}
static cell_t SetVariantPosVector3D(IPluginContext *pContext, const cell_t *params)
{
cell_t *val;
unsigned char *vptr = g_Variant_t;
pContext->LocalToPhysAddr(params[1], &val);
*(float *)vptr = sp_ctof(val[0]);
vptr += sizeof(float);
*(float *)vptr = sp_ctof(val[1]);
vptr += sizeof(float);
*(float *)vptr = sp_ctof(val[2]);
vptr += sizeof(float) + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_POSITION_VECTOR;
return 1;
}
static cell_t SetVariantColor(IPluginContext *pContext, const cell_t *params)
{
cell_t *val;
unsigned char *vptr = g_Variant_t;
pContext->LocalToPhysAddr(params[1], &val);
*(unsigned char *)vptr = val[0];
vptr += sizeof(unsigned char);
*(unsigned char *)vptr = val[1];
vptr += sizeof(unsigned char);
*(unsigned char *)vptr = val[2];
vptr += sizeof(unsigned char);
*(unsigned char *)vptr = val[3];
vptr += sizeof(unsigned char) + sizeof(int)*2 + sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_COLOR32;
return 1;
}
static cell_t SetVariantEntity(IPluginContext *pContext, const cell_t *params)
{
CBaseEntity *pEntity;
unsigned char *vptr = g_Variant_t;
CBaseHandle bHandle;
ENTINDEX_TO_CBASEENTITY(params[1], pEntity);
bHandle = reinterpret_cast<IHandleEntity *>(pEntity)->GetRefEHandle();
vptr += sizeof(int)*3;
*(unsigned long *)vptr = (unsigned long)(bHandle.ToInt());
vptr += sizeof(unsigned long);
*(fieldtype_t *)vptr = FIELD_EHANDLE;
return 1;
}
sp_nativeinfo_t g_EntInputNatives[] =
{
{"AcceptEntityInput", AcceptEntityInput},
{"SetVariantBool", SetVariantBool},
{"SetVariantString", SetVariantString},
{"SetVariantInt", SetVariantInt},
{"SetVariantFloat", SetVariantFloat},
{"SetVariantVector3D", SetVariantVector3D},
{"SetVariantPosVector3D", SetVariantPosVector3D},
{"SetVariantColor", SetVariantColor},
{"SetVariantEntity", SetVariantEntity},
{NULL, NULL},
};