2 * vim: set ts=4 sw=4 tw=99 noet :
3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2009 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>.
30 #include "extension.h"
31 #include "vcallbuilder.h"
36 SDKLibrary_Server, /**< server.dll/server_i486.so */
37 SDKLibrary_Engine, /**< engine.dll/engine_*.so */
42 SDKPass_Pointer, /**< Pass as a pointer */
43 SDKPass_Plain, /**< Pass as plain data */
44 SDKPass_ByValue, /**< Pass an object by value */
45 SDKPass_ByRef, /**< Pass an object by reference */
48 int s_vtbl_index = -1;
49 void *s_call_addr = NULL;
50 ValveCallType s_vcalltype = ValveCall_Static;
51 bool s_has_return = false;
52 ValvePassInfo s_return;
53 unsigned int s_numparams = 0;
54 ValvePassInfo s_params[SP_MAX_EXEC_PARAMS];
56 inline void DecodePassMethod(ValveType vtype, SDKPassMethod method, PassType &type, unsigned int &flags)
58 if (method == SDKPass_Pointer || method == SDKPass_ByRef)
60 type = PassType_Basic;
61 if (vtype == Valve_POD
62 || vtype == Valve_Float
63 || vtype == Valve_Bool)
65 flags = PASSFLAG_BYVAL | PASSFLAG_ASPOINTER;
67 flags = PASSFLAG_BYVAL;
69 } else if (method == SDKPass_Plain) {
70 type = PassType_Basic;
71 flags = PASSFLAG_BYVAL;
72 } else if (method == SDKPass_ByValue) {
73 if (vtype == Valve_Vector
74 || vtype == Valve_QAngle)
76 type = PassType_Object;
78 type = PassType_Basic;
80 flags = PASSFLAG_BYVAL;
84 static cell_t StartPrepSDKCall(IPluginContext *pContext, const cell_t *params)
90 s_vcalltype = (ValveCallType)params[1];
95 static cell_t PrepSDKCall_SetVirtual(IPluginContext *pContext, const cell_t *params)
97 s_vtbl_index = params[1];
102 static cell_t PrepSDKCall_SetSignature(IPluginContext *pContext, const cell_t *params)
104 void *addrInBase = NULL;
105 if (params[1] == SDKLibrary_Server)
107 addrInBase = (void *)g_SMAPI->GetServerFactory(false);
108 } else if (params[1] == SDKLibrary_Engine) {
109 addrInBase = (void *)g_SMAPI->GetEngineFactory(false);
111 if (addrInBase == NULL)
117 pContext->LocalToString(params[2], &sig);
119 #if defined PLATFORM_POSIX
123 if (dladdr(addrInBase, &info) == 0)
127 void *handle = dlopen(info.dli_fname, RTLD_NOW);
132 #if (SOURCE_ENGINE == SE_CSS) || (SOURCE_ENGINE == SE_HL2DM) || (SOURCE_ENGINE == SE_DODS) || (SOURCE_ENGINE == SE_TF2) || (SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2) || (SOURCE_ENGINE == SE_NUCLEARDAWN) || (SOURCE_ENGINE == SE_CSGO)
133 s_call_addr = memutils->ResolveSymbol(handle, &sig[1]);
135 s_call_addr = dlsym(handle, &sig[1]);
139 return (s_call_addr != NULL) ? 1 : 0;
143 s_call_addr = memutils->FindPattern(addrInBase, sig, params[3]);
145 return (s_call_addr != NULL) ? 1 : 0;
148 static cell_t PrepSDKCall_SetFromConf(IPluginContext *pContext, const cell_t *params)
152 if (params[1] == BAD_HANDLE)
157 if ((conf = gameconfs->ReadHandle(params[1], pContext->GetIdentity(), &err)) == NULL)
159 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
164 pContext->LocalToString(params[3], &key);
168 return conf->GetOffset(key, &s_vtbl_index) ? 1 : 0;
169 } else if (params[2] == 1) {
170 bool result = conf->GetMemSig(key, &s_call_addr) ? 1 : 0;
171 return (result && s_call_addr != NULL) ? 1 : 0;
177 static cell_t PrepSDKCall_SetReturnInfo(IPluginContext *pContext, const cell_t *params)
180 s_return.vtype = (ValveType)params[1];
181 DecodePassMethod(s_return.vtype, (SDKPassMethod)params[2], s_return.type, s_return.flags);
182 s_return.decflags = params[3];
183 s_return.encflags = params[4];
188 static cell_t PrepSDKCall_AddParameter(IPluginContext *pContext, const cell_t *params)
190 if (s_numparams >= SP_MAX_EXEC_PARAMS)
192 return pContext->ThrowNativeError("Parameter limit for SDK calls reached");
195 ValvePassInfo *info = &s_params[s_numparams++];
196 info->vtype = (ValveType)params[1];
197 SDKPassMethod method = (SDKPassMethod)params[2];
198 DecodePassMethod(info->vtype, method, info->type, info->flags);
199 info->decflags = params[3] | VDECODE_FLAG_BYREF;
200 info->encflags = params[4];
202 /* Since SDKPass_ByRef acts like SDKPass_Pointer we can't allow NULL, just in case */
203 if (method == SDKPass_ByRef)
205 info->decflags &= ~VDECODE_FLAG_ALLOWNULL;
211 static cell_t EndPrepSDKCall(IPluginContext *pContext, const cell_t *params)
213 ValveCall *vc = NULL;
214 if (s_vtbl_index > -1)
216 vc = CreateValveVCall(s_vtbl_index, s_vcalltype, s_has_return ? &s_return : NULL, s_params, s_numparams);
217 } else if (s_call_addr) {
218 vc = CreateValveCall(s_call_addr, s_vcalltype, s_has_return ? &s_return : NULL, s_params, s_numparams);
228 vc->thisinfo->decflags |= VDECODE_FLAG_BYREF;
231 Handle_t hndl = handlesys->CreateHandle(g_CallHandle, vc, pContext->GetIdentity(), myself->GetIdentity(), NULL);
240 static cell_t SDKCall(IPluginContext *pContext, const cell_t *params)
244 HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
246 if ((err = handlesys->ReadHandle(params[1], g_CallHandle, &sec, (void **)&vc)) != HandleError_None)
248 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
251 unsigned char *ptr = vc->stk_get();
253 const unsigned int numparams = (unsigned)params[0];
254 unsigned int startparam = 2;
255 /* Do we need to write a thispointer? */
261 case ValveCall_Entity:
262 case ValveCall_Player:
264 if (startparam > numparams)
267 return pContext->ThrowNativeError("Expected 1 parameter for entity pointer; found none");
270 if (DecodeValveParam(pContext,
282 case ValveCall_GameRules:
284 if (g_pGameRules == NULL)
287 return pContext->ThrowNativeError("GameRules unsupported or not available; file a bug report");
290 void *gamerules = *g_pGameRules;
292 if (gamerules == NULL)
295 return pContext->ThrowNativeError("GameRules not available before map is loaded");
297 *(void **)ptr = gamerules;
300 case ValveCall_EntityList:
302 if (g_EntList == NULL)
305 return pContext->ThrowNativeError("EntityList unsupported or not available; file a bug report");
308 *(void **)ptr = g_EntList;
313 //params[startparam] is an address to a pointer to THIS
314 //params following this are params to the method we will invoke later
315 if (startparam > numparams)
318 return pContext->ThrowNativeError("Expected a ThisPtr address, it wasn't found");
321 //note: varargs pawn args are passed by-ref
323 pContext->LocalToPhysAddr(params[startparam], &cell);
324 void *thisptr = reinterpret_cast<void*>(*cell);
326 *(void **)ptr = thisptr;
333 /* See if we need to skip any more parameters */
334 unsigned int retparam = startparam;
337 if (vc->retinfo->vtype == Valve_String)
340 } else if (vc->retinfo->vtype == Valve_Vector
341 || vc->retinfo->vtype == Valve_QAngle)
347 unsigned int callparams = vc->call->GetParamCount();
348 bool will_copyback = false;
349 for (unsigned int i=0; i<callparams; i++)
351 unsigned int p = startparam + i;
355 return pContext->ThrowNativeError("Expected %dth parameter, found none", p);
357 if (DecodeValveParam(pContext,
366 if (vc->vparams[i].encflags & VENCODE_FLAG_COPYBACK)
368 will_copyback = true;
372 /* Make the actual call */
373 vc->call->Execute(ptr, vc->retbuf);
375 /* Do we need to copy anything back? */
378 for (unsigned int i=0; i<callparams; i++)
380 if (vc->vparams[i].encflags & VENCODE_FLAG_COPYBACK)
382 if (EncodeValveParam(pContext,
383 params[startparam + i],
395 /* Save stack once and for all */
398 /* Figure out how to decode the return information */
401 if (vc->retinfo->vtype == Valve_String)
405 return pContext->ThrowNativeError("Expected arguments (2,3) for string storage");
409 pContext->LocalToPhysAddr(params[retparam+1], &addr);
410 pContext->StringToLocalUTF8(params[retparam], *addr, *(char **)vc->retbuf, &written);
411 return (cell_t)written;
412 } else if (vc->retinfo->vtype == Valve_Vector
413 || vc->retinfo->vtype == Valve_QAngle)
417 return pContext->ThrowNativeError("Expected argument (2) for Float[3] storage");
419 if (EncodeValveParam(pContext, params[retparam], vc, vc->retinfo, vc->retbuf)
424 } else if (vc->retinfo->vtype == Valve_CBaseEntity
425 || vc->retinfo->vtype == Valve_CBasePlayer)
427 CBaseEntity *pEntity = *(CBaseEntity **)(vc->retbuf);
432 edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
433 if (!pEdict || pEdict->IsFree())
437 return IndexOfEdict(pEdict);
438 } else if (vc->retinfo->vtype == Valve_Edict) {
439 edict_t *pEdict = *(edict_t **)(vc->retbuf);
440 if (!pEdict || pEdict->IsFree())
444 return IndexOfEdict(pEdict);
445 } else if (vc->retinfo->vtype == Valve_Bool) {
446 bool *addr = (bool *)vc->retbuf;
447 if (vc->retinfo->flags & PASSFLAG_ASPOINTER)
449 addr = *(bool **)addr;
451 return *addr ? 1 : 0;
453 cell_t *addr = (cell_t *)vc->retbuf;
454 if (vc->retinfo->flags & PASSFLAG_ASPOINTER)
456 addr = *(cell_t **)addr;
465 sp_nativeinfo_t g_CallNatives[] =
467 {"StartPrepSDKCall", StartPrepSDKCall},
468 {"PrepSDKCall_SetVirtual", PrepSDKCall_SetVirtual},
469 {"PrepSDKCall_SetSignature", PrepSDKCall_SetSignature},
470 {"PrepSDKCall_SetFromConf", PrepSDKCall_SetFromConf},
471 {"PrepSDKCall_SetReturnInfo", PrepSDKCall_SetReturnInfo},
472 {"PrepSDKCall_AddParameter", PrepSDKCall_AddParameter},
473 {"EndPrepSDKCall", EndPrepSDKCall},
474 {"SDKCall", SDKCall},