2 #include "vcallbuilder.h"
6 SDKLibrary_Server, /**< server.dll/server_i486.so */
7 SDKLibrary_Engine, /**< engine.dll/engine_*.so */
12 SDKPass_Pointer, /**< Pass as a pointer */
13 SDKPass_Plain, /**< Pass as plain data */
14 SDKPass_ByValue, /**< Pass an object by value */
15 SDKPass_ByRef, /**< Pass an object by reference */
19 void *s_call_addr = NULL;
20 ValveCallType s_vcalltype = ValveCall_Static;
21 bool s_has_return = false;
22 ValvePassInfo s_return;
23 unsigned int s_numparams = 0;
24 ValvePassInfo s_params[SP_MAX_EXEC_PARAMS];
26 inline void DecodePassMethod(ValveType vtype, SDKPassMethod method, PassType &type, unsigned int &flags)
28 if (method == SDKPass_Pointer)
30 type = PassType_Basic;
31 if (vtype == Valve_POD
32 || vtype == Valve_Float)
34 flags = PASSFLAG_BYREF;
36 flags = PASSFLAG_BYVAL;
38 } else if (method == SDKPass_Plain) {
39 type = PassType_Basic;
40 flags = PASSFLAG_BYVAL;
41 } else if (method == SDKPass_ByValue) {
42 if (vtype == Valve_Vector
43 || vtype == Valve_QAngle)
45 type = PassType_Object;
47 type = PassType_Basic;
49 flags = PASSFLAG_BYVAL;
50 } else if (method == SDKPass_ByRef) {
51 if (vtype == Valve_Vector
52 || vtype == Valve_QAngle)
54 type = PassType_Object;
56 type = PassType_Basic;
58 flags = PASSFLAG_BYREF;
62 static cell_t StartPrepSDKCall(IPluginContext *pContext, const cell_t *params)
68 s_vcalltype = (ValveCallType)params[1];
73 static cell_t PrepSDKCall_SetVirtual(IPluginContext *pContext, const cell_t *params)
75 s_vtbl_index = params[1];
80 static cell_t PrepSDKCall_SetSignature(IPluginContext *pContext, const cell_t *params)
82 void *addrInBase = NULL;
83 if (params[1] == SDKLibrary_Server)
85 addrInBase = g_SMAPI->serverFactory(false);
86 } else if (params[1] == SDKLibrary_Engine) {
87 addrInBase = g_SMAPI->engineFactory(false);
89 if (addrInBase == NULL)
95 pContext->LocalToString(params[2], &sig);
97 #if defined PLATFORM_LINUX
101 if (dladdr(addrInBase, &info) == 0)
105 void *handle = dlopen(info.dli_fname, RTLD_NOW);
110 s_call_addr = dlsym(handle, &sig[1]);
113 return (s_call_addr != NULL) ? 1 : 0;
117 s_call_addr = memutils->FindPattern(addrInBase, sig, params[3]);
119 return (s_call_addr != NULL) ? 1 : 0;
122 static cell_t PrepSDKCall_SetFromConf(IPluginContext *pContext, const cell_t *params)
126 if (params[1] == BAD_HANDLE)
131 if ((conf = gameconfs->ReadHandle(params[1], pContext->GetIdentity(), &err)) == NULL)
133 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
138 pContext->LocalToString(params[3], &key);
142 return conf->GetOffset(key, &s_vtbl_index) ? 1 : 0;
143 } else if (params[2] == 1) {
144 bool result = conf->GetMemSig(key, &s_call_addr) ? 1 : 0;
145 return (result && s_call_addr != NULL) ? 1 : 0;
151 static cell_t PrepSDKCall_SetReturnInfo(IPluginContext *pContext, const cell_t *params)
154 s_return.vtype = (ValveType)params[1];
155 DecodePassMethod(s_return.vtype, (SDKPassMethod)params[2], s_return.type, s_return.flags);
156 s_return.decflags = params[3];
157 s_return.encflags = params[4];
162 static cell_t PrepSDKCall_AddParameter(IPluginContext *pContext, const cell_t *params)
164 if (s_numparams >= SP_MAX_EXEC_PARAMS)
166 return pContext->ThrowNativeError("Parameter limit for SDK calls reached");
169 ValvePassInfo *info = &s_params[s_numparams++];
170 info->vtype = (ValveType)params[1];
171 DecodePassMethod(info->vtype, (SDKPassMethod)params[2], info->type, info->flags);
172 info->decflags = params[3];
173 info->encflags = params[4];
178 static cell_t EndPrepSDKCall(IPluginContext *pContext, const cell_t *params)
180 ValveCall *vc = NULL;
183 vc = CreateValveVCall(s_vtbl_index, s_vcalltype, s_has_return ? &s_return : NULL, s_params, s_numparams);
184 } else if (s_call_addr) {
185 vc = CreateValveCall(s_call_addr, s_vcalltype, s_has_return ? &s_return : NULL, s_params, s_numparams);
193 Handle_t hndl = handlesys->CreateHandle(g_CallHandle, vc, pContext->GetIdentity(), myself->GetIdentity(), NULL);
202 static cell_t SDKCall(IPluginContext *pContext, const cell_t *params)
206 HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
208 if ((err = handlesys->ReadHandle(params[1], g_CallHandle, &sec, (void **)&vc)) != HandleError_None)
210 return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
213 unsigned char *ptr = vc->stk_get();
215 unsigned int numparams = (unsigned)params[0];
216 unsigned int startparam = 2;
217 /* Do we need to write a thispointer? */
220 (vc->thisinfo->vtype == Valve_CBaseEntity
221 || vc->thisinfo->vtype == Valve_CBasePlayer))
223 if (startparam > numparams)
226 return pContext->ThrowNativeError("Expected 1 parameter for entity pointer; found none");
228 if (DecodeValveParam(pContext,
231 vc->thisinfo->decflags | VDECODE_FLAG_BYREF,
241 /* See if we need to skip any more parameters */
242 unsigned int retparam = startparam;
245 if (vc->retinfo->vtype == Valve_String)
248 } else if (vc->retinfo->vtype == Valve_Vector
249 || vc->retinfo->vtype == Valve_QAngle)
255 unsigned int callparams = vc->call->GetParamCount();
256 bool will_copyback = false;
257 for (unsigned int i=0; i<callparams; i++)
259 unsigned int p = startparam + i;
263 return pContext->ThrowNativeError("Expected %dth parameter, found none", p);
265 if (DecodeValveParam(pContext,
267 vc->vparams[i].vtype,
268 vc->vparams[i].decflags | VDECODE_FLAG_BYREF,
270 ptr + vc->vparams[i].offset) == Data_Fail)
275 if (vc->vparams[i].encflags & VENCODE_FLAG_COPYBACK)
277 will_copyback = true;
281 /* Make the actual call */
282 vc->call->Execute(ptr, vc->retbuf);
284 /* Do we need to copy anything back? */
287 for (unsigned int i=0; i<callparams; i++)
289 if (vc->vparams[i].encflags & VENCODE_FLAG_COPYBACK)
291 if (EncodeValveParam(pContext,
293 vc->vparams[i].vtype,
295 ptr + vc->vparams[i].offset) == Data_Fail)
304 /* Save stack once and for all */
307 /* Figure out how to decode the return information */
310 if (vc->retinfo->vtype == Valve_String)
314 return pContext->ThrowNativeError("Expected arguments (2,3) for string storage");
318 pContext->LocalToPhysAddr(params[retparam+1], &addr);
319 pContext->StringToLocalUTF8(params[retparam], *addr, *(char **)vc->retbuf, &written);
320 return (cell_t)written;
321 } else if (vc->retinfo->vtype == Valve_Vector
322 || vc->retinfo->vtype == Valve_QAngle)
326 return pContext->ThrowNativeError("Expected argument (2) for Float[3] storage");
328 if (EncodeValveParam(pContext, params[retparam], vc->retinfo->vtype, vc->retinfo->type, vc->retbuf)
333 } else if (vc->retinfo->vtype == Valve_CBaseEntity
334 || vc->retinfo->vtype == Valve_CBasePlayer)
336 CBaseEntity *pEntity = *(CBaseEntity **)(vc->retbuf);
341 edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
342 if (!pEdict || pEdict->IsFree())
346 return engine->IndexOfEdict(pEdict);
347 } else if (vc->retinfo->vtype == Valve_Edict) {
348 edict_t *pEdict = *(edict_t **)(vc->retbuf);
349 if (!pEdict || pEdict->IsFree())
353 return engine->IndexOfEdict(pEdict);
355 cell_t *addr = (cell_t *)vc->retbuf;
356 if (vc->retinfo->flags & PASSFLAG_BYREF)
358 addr = *(cell_t **)addr;
367 sp_nativeinfo_t g_CallNatives[] =
369 {"StartPrepSDKCall", StartPrepSDKCall},
370 {"PrepSDKCall_SetVirtual", PrepSDKCall_SetVirtual},
371 {"PrepSDKCall_SetSignature", PrepSDKCall_SetSignature},
372 {"PrepSDKCall_SetFromConf", PrepSDKCall_SetFromConf},
373 {"PrepSDKCall_SetReturnInfo", PrepSDKCall_SetReturnInfo},
374 {"PrepSDKCall_AddParameter", PrepSDKCall_AddParameter},
375 {"EndPrepSDKCall", EndPrepSDKCall},
376 {"SDKCall", SDKCall},