1.1 --- a/extensions/sdktools/vcaller.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,455 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod SDKTools Extension
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "extension.h"
1.36 -#include "vcallbuilder.h"
1.37 -#include "vglobals.h"
1.38 -
1.39 -enum SDKLibrary
1.40 -{
1.41 - SDKLibrary_Server, /**< server.dll/server_i486.so */
1.42 - SDKLibrary_Engine, /**< engine.dll/engine_*.so */
1.43 -};
1.44 -
1.45 -enum SDKPassMethod
1.46 -{
1.47 - SDKPass_Pointer, /**< Pass as a pointer */
1.48 - SDKPass_Plain, /**< Pass as plain data */
1.49 - SDKPass_ByValue, /**< Pass an object by value */
1.50 - SDKPass_ByRef, /**< Pass an object by reference */
1.51 -};
1.52 -
1.53 -int s_vtbl_index = -1;
1.54 -void *s_call_addr = NULL;
1.55 -ValveCallType s_vcalltype = ValveCall_Static;
1.56 -bool s_has_return = false;
1.57 -ValvePassInfo s_return;
1.58 -unsigned int s_numparams = 0;
1.59 -ValvePassInfo s_params[SP_MAX_EXEC_PARAMS];
1.60 -
1.61 -inline void DecodePassMethod(ValveType vtype, SDKPassMethod method, PassType &type, unsigned int &flags)
1.62 -{
1.63 - if (method == SDKPass_Pointer || method == SDKPass_ByRef)
1.64 - {
1.65 - type = PassType_Basic;
1.66 - if (vtype == Valve_POD
1.67 - || vtype == Valve_Float
1.68 - || vtype == Valve_Bool)
1.69 - {
1.70 - flags = PASSFLAG_BYVAL | PASSFLAG_ASPOINTER;
1.71 - } else {
1.72 - flags = PASSFLAG_BYVAL;
1.73 - }
1.74 - } else if (method == SDKPass_Plain) {
1.75 - type = PassType_Basic;
1.76 - flags = PASSFLAG_BYVAL;
1.77 - } else if (method == SDKPass_ByValue) {
1.78 - if (vtype == Valve_Vector
1.79 - || vtype == Valve_QAngle)
1.80 - {
1.81 - type = PassType_Object;
1.82 - } else {
1.83 - type = PassType_Basic;
1.84 - }
1.85 - flags = PASSFLAG_BYVAL;
1.86 - }
1.87 -}
1.88 -
1.89 -static cell_t StartPrepSDKCall(IPluginContext *pContext, const cell_t *params)
1.90 -{
1.91 - s_numparams = 0;
1.92 - s_vtbl_index = -1;
1.93 - s_call_addr = NULL;
1.94 - s_has_return = false;
1.95 - s_vcalltype = (ValveCallType)params[1];
1.96 -
1.97 - return 1;
1.98 -}
1.99 -
1.100 -static cell_t PrepSDKCall_SetVirtual(IPluginContext *pContext, const cell_t *params)
1.101 -{
1.102 - s_vtbl_index = params[1];
1.103 -
1.104 - return 1;
1.105 -}
1.106 -
1.107 -static cell_t PrepSDKCall_SetSignature(IPluginContext *pContext, const cell_t *params)
1.108 -{
1.109 - void *addrInBase = NULL;
1.110 - if (params[1] == SDKLibrary_Server)
1.111 - {
1.112 - addrInBase = (void *)g_SMAPI->GetServerFactory(false);
1.113 - } else if (params[1] == SDKLibrary_Engine) {
1.114 - addrInBase = (void *)g_SMAPI->GetEngineFactory(false);
1.115 - }
1.116 - if (addrInBase == NULL)
1.117 - {
1.118 - return 0;
1.119 - }
1.120 -
1.121 - char *sig;
1.122 - pContext->LocalToString(params[2], &sig);
1.123 -
1.124 -#if defined PLATFORM_LINUX
1.125 - if (sig[0] == '@')
1.126 - {
1.127 - Dl_info info;
1.128 - if (dladdr(addrInBase, &info) == 0)
1.129 - {
1.130 - return 0;
1.131 - }
1.132 - void *handle = dlopen(info.dli_fname, RTLD_NOW);
1.133 - if (!handle)
1.134 - {
1.135 - return 0;
1.136 - }
1.137 - s_call_addr = dlsym(handle, &sig[1]);
1.138 - dlclose(handle);
1.139 -
1.140 - return (s_call_addr != NULL) ? 1 : 0;
1.141 - }
1.142 -#endif
1.143 -
1.144 - s_call_addr = memutils->FindPattern(addrInBase, sig, params[3]);
1.145 -
1.146 - return (s_call_addr != NULL) ? 1 : 0;
1.147 -}
1.148 -
1.149 -static cell_t PrepSDKCall_SetFromConf(IPluginContext *pContext, const cell_t *params)
1.150 -{
1.151 - IGameConfig *conf;
1.152 -
1.153 - if (params[1] == BAD_HANDLE)
1.154 - {
1.155 - conf = g_pGameConf;
1.156 - } else {
1.157 - HandleError err;
1.158 - if ((conf = gameconfs->ReadHandle(params[1], pContext->GetIdentity(), &err)) == NULL)
1.159 - {
1.160 - return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1.161 - }
1.162 - }
1.163 -
1.164 - char *key;
1.165 - pContext->LocalToString(params[3], &key);
1.166 -
1.167 - if (params[2] == 0)
1.168 - {
1.169 - return conf->GetOffset(key, &s_vtbl_index) ? 1 : 0;
1.170 - } else if (params[2] == 1) {
1.171 - bool result = conf->GetMemSig(key, &s_call_addr) ? 1 : 0;
1.172 - return (result && s_call_addr != NULL) ? 1 : 0;
1.173 - }
1.174 -
1.175 - return 0;
1.176 -}
1.177 -
1.178 -static cell_t PrepSDKCall_SetReturnInfo(IPluginContext *pContext, const cell_t *params)
1.179 -{
1.180 - s_has_return = true;
1.181 - s_return.vtype = (ValveType)params[1];
1.182 - DecodePassMethod(s_return.vtype, (SDKPassMethod)params[2], s_return.type, s_return.flags);
1.183 - s_return.decflags = params[3];
1.184 - s_return.encflags = params[4];
1.185 -
1.186 - return 1;
1.187 -}
1.188 -
1.189 -static cell_t PrepSDKCall_AddParameter(IPluginContext *pContext, const cell_t *params)
1.190 -{
1.191 - if (s_numparams >= SP_MAX_EXEC_PARAMS)
1.192 - {
1.193 - return pContext->ThrowNativeError("Parameter limit for SDK calls reached");
1.194 - }
1.195 -
1.196 - ValvePassInfo *info = &s_params[s_numparams++];
1.197 - info->vtype = (ValveType)params[1];
1.198 - SDKPassMethod method = (SDKPassMethod)params[2];
1.199 - DecodePassMethod(info->vtype, method, info->type, info->flags);
1.200 - info->decflags = params[3] | VDECODE_FLAG_BYREF;
1.201 - info->encflags = params[4];
1.202 -
1.203 - /* Since SDKPass_ByRef acts like SDKPass_Pointer we can't allow NULL, just in case */
1.204 - if (method == SDKPass_ByRef)
1.205 - {
1.206 - info->decflags &= ~VDECODE_FLAG_ALLOWNULL;
1.207 - }
1.208 -
1.209 - return 1;
1.210 -}
1.211 -
1.212 -static cell_t EndPrepSDKCall(IPluginContext *pContext, const cell_t *params)
1.213 -{
1.214 - ValveCall *vc = NULL;
1.215 - if (s_vtbl_index > -1)
1.216 - {
1.217 - vc = CreateValveVCall(s_vtbl_index, s_vcalltype, s_has_return ? &s_return : NULL, s_params, s_numparams);
1.218 - } else if (s_call_addr) {
1.219 - vc = CreateValveCall(s_call_addr, s_vcalltype, s_has_return ? &s_return : NULL, s_params, s_numparams);
1.220 - }
1.221 -
1.222 - if (!vc)
1.223 - {
1.224 - return BAD_HANDLE;
1.225 - }
1.226 -
1.227 - if (vc->thisinfo)
1.228 - {
1.229 - vc->thisinfo->decflags |= VDECODE_FLAG_BYREF;
1.230 - }
1.231 -
1.232 - Handle_t hndl = handlesys->CreateHandle(g_CallHandle, vc, pContext->GetIdentity(), myself->GetIdentity(), NULL);
1.233 - if (!hndl)
1.234 - {
1.235 - delete vc;
1.236 - }
1.237 -
1.238 - return hndl;
1.239 -}
1.240 -
1.241 -static cell_t SDKCall(IPluginContext *pContext, const cell_t *params)
1.242 -{
1.243 - ValveCall *vc;
1.244 - HandleError err;
1.245 - HandleSecurity sec(pContext->GetIdentity(), myself->GetIdentity());
1.246 -
1.247 - if ((err = handlesys->ReadHandle(params[1], g_CallHandle, &sec, (void **)&vc)) != HandleError_None)
1.248 - {
1.249 - return pContext->ThrowNativeError("Invalid Handle %x (error %d)", params[1], err);
1.250 - }
1.251 -
1.252 - unsigned char *ptr = vc->stk_get();
1.253 -
1.254 - unsigned int numparams = (unsigned)params[0];
1.255 - unsigned int startparam = 2;
1.256 - /* Do we need to write a thispointer? */
1.257 -
1.258 - if (vc->thisinfo)
1.259 - {
1.260 - switch (vc->type)
1.261 - {
1.262 - case ValveCall_Entity:
1.263 - case ValveCall_Player:
1.264 - {
1.265 - if (startparam > numparams)
1.266 - {
1.267 - vc->stk_put(ptr);
1.268 - return pContext->ThrowNativeError("Expected 1 parameter for entity pointer; found none");
1.269 - }
1.270 -
1.271 - if (DecodeValveParam(pContext,
1.272 - params[startparam],
1.273 - vc,
1.274 - vc->thisinfo,
1.275 - ptr) == Data_Fail)
1.276 - {
1.277 - vc->stk_put(ptr);
1.278 - return 0;
1.279 - }
1.280 - startparam++;
1.281 - }
1.282 - break;
1.283 - case ValveCall_GameRules:
1.284 - {
1.285 - if (g_pGameRules == NULL)
1.286 - {
1.287 - vc->stk_put(ptr);
1.288 - return pContext->ThrowNativeError("GameRules unsupported or not available; file a bug report");
1.289 - }
1.290 -
1.291 - void *gamerules = *g_pGameRules;
1.292 -
1.293 - if (gamerules == NULL)
1.294 - {
1.295 - vc->stk_put(ptr);
1.296 - return pContext->ThrowNativeError("GameRules not available before map is loaded");
1.297 - }
1.298 - *(void **)ptr = gamerules;
1.299 - }
1.300 - break;
1.301 - case ValveCall_EntityList:
1.302 - {
1.303 - if (g_EntList == NULL)
1.304 - {
1.305 - vc->stk_put(ptr);
1.306 - return pContext->ThrowNativeError("EntityList unsupported or not available; file a bug report");
1.307 - }
1.308 -
1.309 - *(void **)ptr = g_EntList;
1.310 - }
1.311 - break;
1.312 - }
1.313 - }
1.314 -
1.315 - /* See if we need to skip any more parameters */
1.316 - unsigned int retparam = startparam;
1.317 - if (vc->retinfo)
1.318 - {
1.319 - if (vc->retinfo->vtype == Valve_String)
1.320 - {
1.321 - startparam += 2;
1.322 - } else if (vc->retinfo->vtype == Valve_Vector
1.323 - || vc->retinfo->vtype == Valve_QAngle)
1.324 - {
1.325 - startparam += 1;
1.326 - }
1.327 - }
1.328 -
1.329 - unsigned int callparams = vc->call->GetParamCount();
1.330 - bool will_copyback = false;
1.331 - for (unsigned int i=0; i<callparams; i++)
1.332 - {
1.333 - unsigned int p = startparam + i;
1.334 - if (p > numparams)
1.335 - {
1.336 - vc->stk_put(ptr);
1.337 - return pContext->ThrowNativeError("Expected %dth parameter, found none", p);
1.338 - }
1.339 - if (DecodeValveParam(pContext,
1.340 - params[p],
1.341 - vc,
1.342 - &(vc->vparams[i]),
1.343 - ptr) == Data_Fail)
1.344 - {
1.345 - vc->stk_put(ptr);
1.346 - return 0;
1.347 - }
1.348 - if (vc->vparams[i].encflags & VENCODE_FLAG_COPYBACK)
1.349 - {
1.350 - will_copyback = true;
1.351 - }
1.352 - }
1.353 -
1.354 - /* Make the actual call */
1.355 - vc->call->Execute(ptr, vc->retbuf);
1.356 -
1.357 - /* Do we need to copy anything back? */
1.358 - if (will_copyback)
1.359 - {
1.360 - for (unsigned int i=0; i<callparams; i++)
1.361 - {
1.362 - if (vc->vparams[i].encflags & VENCODE_FLAG_COPYBACK)
1.363 - {
1.364 - if (EncodeValveParam(pContext,
1.365 - params[startparam + i],
1.366 - vc,
1.367 - &vc->vparams[i],
1.368 - ptr) == Data_Fail)
1.369 - {
1.370 - vc->stk_put(ptr);
1.371 - return 0;
1.372 - }
1.373 - }
1.374 - }
1.375 - }
1.376 -
1.377 - /* Save stack once and for all */
1.378 - vc->stk_put(ptr);
1.379 -
1.380 - /* Figure out how to decode the return information */
1.381 - if (vc->retinfo)
1.382 - {
1.383 - if (vc->retinfo->vtype == Valve_String)
1.384 - {
1.385 - if (numparams < 3)
1.386 - {
1.387 - return pContext->ThrowNativeError("Expected arguments (2,3) for string storage");
1.388 - }
1.389 - cell_t *addr;
1.390 - size_t written;
1.391 - pContext->LocalToPhysAddr(params[retparam+1], &addr);
1.392 - pContext->StringToLocalUTF8(params[retparam], *addr, *(char **)vc->retbuf, &written);
1.393 - return (cell_t)written;
1.394 - } else if (vc->retinfo->vtype == Valve_Vector
1.395 - || vc->retinfo->vtype == Valve_QAngle)
1.396 - {
1.397 - if (numparams < 2)
1.398 - {
1.399 - return pContext->ThrowNativeError("Expected argument (2) for Float[3] storage");
1.400 - }
1.401 - if (EncodeValveParam(pContext, params[retparam], vc, vc->retinfo, vc->retbuf)
1.402 - == Data_Fail)
1.403 - {
1.404 - return 0;
1.405 - }
1.406 - } else if (vc->retinfo->vtype == Valve_CBaseEntity
1.407 - || vc->retinfo->vtype == Valve_CBasePlayer)
1.408 - {
1.409 - CBaseEntity *pEntity = *(CBaseEntity **)(vc->retbuf);
1.410 - if (!pEntity)
1.411 - {
1.412 - return -1;
1.413 - }
1.414 - edict_t *pEdict = gameents->BaseEntityToEdict(pEntity);
1.415 - if (!pEdict || pEdict->IsFree())
1.416 - {
1.417 - return -1;
1.418 - }
1.419 - return engine->IndexOfEdict(pEdict);
1.420 - } else if (vc->retinfo->vtype == Valve_Edict) {
1.421 - edict_t *pEdict = *(edict_t **)(vc->retbuf);
1.422 - if (!pEdict || pEdict->IsFree())
1.423 - {
1.424 - return -1;
1.425 - }
1.426 - return engine->IndexOfEdict(pEdict);
1.427 - } else if (vc->retinfo->vtype == Valve_Bool) {
1.428 - bool *addr = (bool *)vc->retbuf;
1.429 - if (vc->retinfo->flags & PASSFLAG_ASPOINTER)
1.430 - {
1.431 - addr = *(bool **)addr;
1.432 - }
1.433 - return *addr ? 1 : 0;
1.434 - } else {
1.435 - cell_t *addr = (cell_t *)vc->retbuf;
1.436 - if (vc->retinfo->flags & PASSFLAG_ASPOINTER)
1.437 - {
1.438 - addr = *(cell_t **)addr;
1.439 - }
1.440 - return *addr;
1.441 - }
1.442 - }
1.443 -
1.444 - return 0;
1.445 -}
1.446 -
1.447 -sp_nativeinfo_t g_CallNatives[] =
1.448 -{
1.449 - {"StartPrepSDKCall", StartPrepSDKCall},
1.450 - {"PrepSDKCall_SetVirtual", PrepSDKCall_SetVirtual},
1.451 - {"PrepSDKCall_SetSignature", PrepSDKCall_SetSignature},
1.452 - {"PrepSDKCall_SetFromConf", PrepSDKCall_SetFromConf},
1.453 - {"PrepSDKCall_SetReturnInfo", PrepSDKCall_SetReturnInfo},
1.454 - {"PrepSDKCall_AddParameter", PrepSDKCall_AddParameter},
1.455 - {"EndPrepSDKCall", EndPrepSDKCall},
1.456 - {"SDKCall", SDKCall},
1.457 - {NULL, NULL},
1.458 -};