1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/sdktools/output.cpp Wed Apr 02 01:22:02 2008 +0000
1.3 @@ -0,0 +1,559 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod SDKTools Extension
1.8 + * Copyright (C) 2004-2007 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: tenatives.cpp 1566 2007-10-14 22:12:46Z faluco $
1.33 + */
1.34 +
1.35 +#include "extension.h"
1.36 +#include "output.h"
1.37 +
1.38 +ISourcePawnEngine *spengine = NULL;
1.39 +EntityOutputManager g_OutputManager;
1.40 +
1.41 +bool EntityOutputManager::CreateFireEventDetour()
1.42 +{
1.43 + g_pGameConf->GetMemSig("FireOutput", &info_address);
1.44 + g_pGameConf->GetOffset("FireOutputBackup", (int *)&(info_restore.bytes));
1.45 +
1.46 + if (!info_address)
1.47 + {
1.48 + g_pSM->LogError(myself, "Could not locate FireOutput - Disabling Entity Outputs");
1.49 + return false;
1.50 + }
1.51 +
1.52 + if (!info_restore.bytes)
1.53 + {
1.54 + g_pSM->LogError(myself, "Could not locate FireOutputBackup - Disabling Entity Outputs");
1.55 + return false;
1.56 + }
1.57 +
1.58 + /* First, save restore bits */
1.59 + for (size_t i=0; i<info_restore.bytes; i++)
1.60 + {
1.61 + info_restore.patch[i] = ((unsigned char *)info_address)[i];
1.62 + g_pSM->LogMessage(myself, "Backing up: %x", info_restore.patch[i]);
1.63 + }
1.64 +
1.65 + info_callback = spengine->ExecAlloc(100);
1.66 + JitWriter wr;
1.67 + JitWriter *jit = ≀
1.68 + wr.outbase = (jitcode_t)info_callback;
1.69 + wr.outptr = wr.outbase;
1.70 +
1.71 + g_pSM->LogMessage(myself, "info_callback : %x", info_callback);
1.72 + g_pSM->LogMessage(myself, "info_address : %x", info_address);
1.73 +
1.74 + /* Function we are detouring into is
1.75 + *
1.76 + * void FireEventDetour(CBaseEntityOutput(void *) *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay = 0 )
1.77 + */
1.78 +
1.79 + /* push fDelay [esp+20h]
1.80 + * push pCaller [esp+1Ch]
1.81 + * push pActivator [esp+18h]
1.82 + * push pOutput [ecx]
1.83 + */
1.84 +
1.85 +
1.86 +#if defined PLATFORM_WINDOWS
1.87 +
1.88 + IA32_Push_Rm_Disp8_ESP(jit, 32);
1.89 + IA32_Push_Rm_Disp8_ESP(jit, 32);
1.90 + IA32_Push_Rm_Disp8_ESP(jit, 32);
1.91 +
1.92 + /* variant_t doesnt do anything so i'll disable this bit */
1.93 + //IA32_Push_Rm_Disp8_ESP(jit, 32);
1.94 + //IA32_Push_Rm_Disp8_ESP(jit, 32);
1.95 + //IA32_Push_Rm_Disp8_ESP(jit, 32);
1.96 + //IA32_Push_Rm_Disp8_ESP(jit, 32);
1.97 + //IA32_Push_Rm_Disp8_ESP(jit, 32);
1.98 +
1.99 + IA32_Push_Reg(jit, REG_ECX);
1.100 +
1.101 +#elif defined PLATFORM_LINUX
1.102 + IA32_Push_Rm_Disp8_ESP(jit, 20);
1.103 + IA32_Push_Rm_Disp8_ESP(jit, 20);
1.104 + IA32_Push_Rm_Disp8_ESP(jit, 20);
1.105 + // We miss the variant_t pointer
1.106 + IA32_Push_Rm_Disp8_ESP(jit, 16);
1.107 +#endif
1.108 +
1.109 + jitoffs_t call = IA32_Call_Imm32(jit, 0);
1.110 + IA32_Write_Jump32_Abs(jit, call, (void *)TempDetour);
1.111 +
1.112 +
1.113 +#if defined PLATFORM_LINUX
1.114 + IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG); //add esp, 4
1.115 +#elif defined PLATFORM_WINDOWS
1.116 + IA32_Pop_Reg(jit, REG_ECX);
1.117 +#endif
1.118 +
1.119 + IA32_Add_Rm_Imm8(jit, REG_ESP, 12, MOD_REG); //add esp, 12 (0Ch)
1.120 +
1.121 +
1.122 + /* Patch old bytes in */
1.123 + for (size_t i=0; i<info_restore.bytes; i++)
1.124 + {
1.125 + jit->write_ubyte(info_restore.patch[i]);
1.126 + g_pSM->LogMessage(myself, "Writing: %x", info_restore.patch[i]);
1.127 + }
1.128 +
1.129 + /* Return to the original function */
1.130 + call = IA32_Jump_Imm32(jit, 0);
1.131 + IA32_Write_Jump32_Abs(jit, call, (unsigned char *)info_address + info_restore.bytes);
1.132 +
1.133 + return true;
1.134 +}
1.135 +
1.136 +void EntityOutputManager::InitFireEventDetour()
1.137 +{
1.138 + if (!is_detoured)
1.139 + {
1.140 + DoGatePatch((unsigned char *)info_address, &info_callback);
1.141 + is_detoured = true;
1.142 + }
1.143 +}
1.144 +
1.145 +void EntityOutputManager::DeleteFireEventDetour()
1.146 +{
1.147 + if (is_detoured)
1.148 + {
1.149 + ShutdownFireEventDetour();
1.150 + }
1.151 +
1.152 + if (info_callback)
1.153 + {
1.154 + /* Free the gate */
1.155 + spengine->ExecFree(info_callback);
1.156 + info_callback = NULL;
1.157 + }
1.158 +}
1.159 +
1.160 +void TempDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
1.161 +{
1.162 + g_OutputManager.FireEventDetour(pOutput, pActivator, pCaller, fDelay);
1.163 +}
1.164 +
1.165 +void EntityOutputManager::ShutdownFireEventDetour()
1.166 +{
1.167 + if (info_callback)
1.168 + {
1.169 + /* Remove the patch */
1.170 + ApplyPatch(info_address, 0, &info_restore, NULL);
1.171 + is_detoured = false;
1.172 + }
1.173 +}
1.174 +
1.175 +void EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
1.176 +{
1.177 + char sOutput[20];
1.178 + Q_snprintf(sOutput, sizeof(sOutput), "%x", pOutput);
1.179 +
1.180 + // attempt to directly lookup a hook using the pOutput pointer
1.181 + OutputNameStruct *pOutputName = NULL;
1.182 +
1.183 + edict_t *pEdict = gameents->BaseEntityToEdict(pCaller);
1.184 +
1.185 + /* DEBUG START */
1.186 +
1.187 + const char *classname = pEdict->GetClassName();
1.188 + const char *outputname = FindOutputName(pOutput, pCaller);
1.189 +
1.190 + g_pSM->LogMessage(myself, "Output \"%s\" fired from entity \"%s\"", outputname, classname);
1.191 +
1.192 + /* DEBUG END */
1.193 +
1.194 +
1.195 + bool fastLookup = false;
1.196 +
1.197 + // Fast lookup failed - check the slow way for hooks that havn't fired yet
1.198 + if ((fastLookup = EntityOutputs->Retrieve(sOutput, (void **)&pOutputName)) == false)
1.199 + {
1.200 + const char *classname = pEdict->GetClassName();
1.201 + const char *outputname = FindOutputName(pOutput, pCaller);
1.202 +
1.203 + pOutputName = FindOutputPointer(classname, outputname, false);
1.204 +
1.205 + if (!pOutputName)
1.206 + {
1.207 + return;
1.208 + }
1.209 + }
1.210 +
1.211 + if (!pOutputName->hooks.empty())
1.212 + {
1.213 + if (!fastLookup)
1.214 + {
1.215 + // hook exists on this classname and output - map it into our quick find trie
1.216 + EntityOutputs->Insert(sOutput, pOutputName);
1.217 + }
1.218 +
1.219 + SourceHook::List<omg_hooks *>::iterator _iter;
1.220 +
1.221 + omg_hooks *hook;
1.222 +
1.223 + _iter = pOutputName->hooks.begin();
1.224 +
1.225 + while (_iter != pOutputName->hooks.end())
1.226 + {
1.227 + hook = (omg_hooks *)*_iter;
1.228 +
1.229 + hook->in_use = true;
1.230 +
1.231 + int serial = pEdict->m_NetworkSerialNumber;
1.232 +
1.233 + if (serial != hook->entity_filter && hook->entity_index == engine->IndexOfEdict(pEdict))
1.234 + {
1.235 + // same entity index but different serial number. Entity has changed, kill the hook.
1.236 + _iter = pOutputName->hooks.erase(_iter);
1.237 + CleanUpHook(hook);
1.238 +
1.239 + continue;
1.240 + }
1.241 +
1.242 + if (hook->entity_filter == -1 || hook->entity_filter == serial) // Global classname hook
1.243 + {
1.244 + /*
1.245 + Handle_t handle;
1.246 +
1.247 + if (Value.FieldType() == FIELD_VOID)
1.248 + {
1.249 + handle = 0;
1.250 + }
1.251 + else
1.252 + {
1.253 + varhandle_t *varhandle = new varhandle_t;
1.254 + memmove ((void *)&varhandle->crab, (void *)&Value, sizeof(variant_t));
1.255 +
1.256 + handle = handlesys->CreateHandle(VariantHandle, (void *)varhandle, hook->pf->GetParentContext()->GetIdentity(), myself->GetIdentity(), NULL);
1.257 + }
1.258 + */
1.259 +
1.260 + //fire the forward to hook->pf
1.261 + hook->pf->PushString(pOutputName->Name);
1.262 + hook->pf->PushCell(engine->IndexOfEdict(pEdict));
1.263 + hook->pf->PushCell(engine->IndexOfEdict(gameents->BaseEntityToEdict(pActivator)));
1.264 + //hook->pf->PushCell(handle);
1.265 + hook->pf->PushFloat(fDelay);
1.266 + hook->pf->Execute(NULL);
1.267 +
1.268 + if ((hook->entity_filter != -1) && hook->only_once)
1.269 + {
1.270 + _iter = pOutputName->hooks.erase(_iter);
1.271 + CleanUpHook(hook);
1.272 +
1.273 + continue;
1.274 + }
1.275 +
1.276 + if (hook->delete_me)
1.277 + {
1.278 + _iter = pOutputName->hooks.erase(_iter);
1.279 + CleanUpHook(hook);
1.280 + continue;
1.281 + }
1.282 +
1.283 + hook->in_use = false;
1.284 + _iter++;
1.285 +
1.286 + continue;
1.287 + }
1.288 + }
1.289 + }
1.290 +}
1.291 +
1.292 +omg_hooks *EntityOutputManager::NewHook()
1.293 +{
1.294 + omg_hooks *hook;
1.295 +
1.296 + if (FreeHooks.empty())
1.297 + {
1.298 + hook = new omg_hooks;
1.299 + }
1.300 + else
1.301 + {
1.302 + hook = g_OutputManager.FreeHooks.front();
1.303 + g_OutputManager.FreeHooks.pop();
1.304 + }
1.305 +
1.306 + return hook;
1.307 +}
1.308 +
1.309 +void EntityOutputManager::OnHookAdded()
1.310 +{
1.311 + HookCount++;
1.312 +
1.313 + if (HookCount == 1)
1.314 + {
1.315 + // This is the first hook created
1.316 + InitFireEventDetour();
1.317 + }
1.318 +}
1.319 +
1.320 +void EntityOutputManager::OnHookRemoved()
1.321 +{
1.322 + HookCount--;
1.323 +
1.324 + if (HookCount == 0)
1.325 + {
1.326 + //we need to check if we are inside a detour. easy.
1.327 + //if we are how do we
1.328 + ShutdownFireEventDetour();
1.329 + }
1.330 +}
1.331 +
1.332 +void EntityOutputManager::CleanUpHook(omg_hooks *hook)
1.333 +{
1.334 + FreeHooks.push(hook);
1.335 +
1.336 + OnHookRemoved();
1.337 +
1.338 + IPlugin *pPlugin = plsys->FindPluginByContext(hook->pf->GetParentContext()->GetContext());
1.339 + SourceHook::List<omg_hooks *> *pList = NULL;
1.340 +
1.341 + if (!pPlugin->GetProperty("OutputHookList", (void **)&pList, false) || !pList)
1.342 + {
1.343 + return;
1.344 + }
1.345 +
1.346 + SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
1.347 +
1.348 + omg_hooks *pluginHook;
1.349 +
1.350 + while (p_iter != pList->end())
1.351 + {
1.352 + pluginHook = (omg_hooks *)*p_iter;
1.353 + if (pluginHook == hook)
1.354 + {
1.355 + p_iter = pList->erase(p_iter);
1.356 + }
1.357 + else
1.358 + {
1.359 + p_iter++;
1.360 + }
1.361 + }
1.362 +}
1.363 +
1.364 +void EntityOutputManager::OnPluginDestroyed(IPlugin *plugin)
1.365 +{
1.366 + SourceHook::List<omg_hooks *> *pList = NULL;
1.367 +
1.368 + if (plugin->GetProperty("OutputHookList", (void **)&pList, true))
1.369 + {
1.370 + SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
1.371 + omg_hooks *hook;
1.372 +
1.373 + while (p_iter != pList->end())
1.374 + {
1.375 + hook = (omg_hooks *)*p_iter;
1.376 +
1.377 + p_iter = pList->erase(p_iter); //remove from this plugins list
1.378 + hook->m_parent->hooks.remove(hook); // remove from the y's list
1.379 +
1.380 + FreeHooks.push(hook); //save the omg_hook
1.381 +
1.382 + OnHookRemoved();
1.383 + }
1.384 + }
1.385 +}
1.386 +
1.387 +OutputNameStruct *EntityOutputManager::FindOutputPointer(const char *classname, const char *outputname, bool create)
1.388 +{
1.389 + ClassNameStruct *pClassname;
1.390 +
1.391 + if (!ClassNames->Retrieve(classname, (void **)&pClassname))
1.392 + {
1.393 + if (create)
1.394 + {
1.395 + pClassname = new ClassNameStruct;
1.396 + ClassNames->Insert(classname, pClassname);
1.397 + }
1.398 + else
1.399 + {
1.400 + return NULL;
1.401 + }
1.402 + }
1.403 +
1.404 + OutputNameStruct *pOutputName;
1.405 +
1.406 + if (!pClassname->OutputList->Retrieve(outputname, (void **)&pOutputName))
1.407 + {
1.408 + if (create)
1.409 + {
1.410 + pOutputName = new OutputNameStruct;
1.411 + pClassname->OutputList->Insert(outputname, pOutputName);
1.412 + strncpy(pOutputName->Name, outputname, sizeof(pOutputName->Name));
1.413 + pOutputName->Name[49] = 0;
1.414 + }
1.415 + else
1.416 + {
1.417 + return NULL;
1.418 + }
1.419 + }
1.420 +
1.421 + return pOutputName;
1.422 +}
1.423 +
1.424 +// Iterate the datamap of pCaller and look for output pointers with the same address as pOutput
1.425 +const char *EntityOutputManager::FindOutputName(void *pOutput, CBaseEntity *pCaller)
1.426 +{
1.427 + datamap_t *pMap = gamehelpers->GetDataMap(pCaller);
1.428 +
1.429 + while (pMap)
1.430 + {
1.431 + for (int i=0; i<pMap->dataNumFields; i++)
1.432 + {
1.433 + if (pMap->dataDesc[i].flags & FTYPEDESC_OUTPUT)
1.434 + {
1.435 + if ((char *)pCaller + pMap->dataDesc[i].fieldOffset[0] == pOutput)
1.436 + {
1.437 + return pMap->dataDesc[i].externalName;
1.438 + }
1.439 + }
1.440 + }
1.441 + pMap = pMap->baseMap;
1.442 + }
1.443 +
1.444 + return NULL;
1.445 +}
1.446 +#if 0
1.447 +// Almost identical copy of this function from cbase.cpp - FIELD_EHANDLE changed to remove dependencies
1.448 +const char *variant_t::ToString( void ) const
1.449 +{
1.450 + COMPILE_TIME_ASSERT( sizeof(string_t) == sizeof(int) );
1.451 +
1.452 + static char szBuf[512];
1.453 +
1.454 + switch (fieldType)
1.455 + {
1.456 + case FIELD_STRING:
1.457 + {
1.458 + return(STRING(iszVal));
1.459 + }
1.460 +
1.461 + case FIELD_BOOLEAN:
1.462 + {
1.463 + if (bVal == 0)
1.464 + {
1.465 + Q_strncpy(szBuf, "false",sizeof(szBuf));
1.466 + }
1.467 + else
1.468 + {
1.469 + Q_strncpy(szBuf, "true",sizeof(szBuf));
1.470 + }
1.471 + return(szBuf);
1.472 + }
1.473 +
1.474 + case FIELD_INTEGER:
1.475 + {
1.476 + Q_snprintf( szBuf, sizeof( szBuf ), "%i", iVal );
1.477 + return(szBuf);
1.478 + }
1.479 +
1.480 + case FIELD_FLOAT:
1.481 + {
1.482 + Q_snprintf(szBuf,sizeof(szBuf), "%g", flVal);
1.483 + return(szBuf);
1.484 + }
1.485 +
1.486 + case FIELD_COLOR32:
1.487 + {
1.488 + Q_snprintf(szBuf,sizeof(szBuf), "%d %d %d %d", (int)rgbaVal.r, (int)rgbaVal.g, (int)rgbaVal.b, (int)rgbaVal.a);
1.489 + return(szBuf);
1.490 + }
1.491 +
1.492 + case FIELD_VECTOR:
1.493 + {
1.494 + Q_snprintf(szBuf,sizeof(szBuf), "[%g %g %g]", (double)vecVal[0], (double)vecVal[1], (double)vecVal[2]);
1.495 + return(szBuf);
1.496 + }
1.497 +
1.498 + case FIELD_VOID:
1.499 + {
1.500 + szBuf[0] = '\0';
1.501 + return(szBuf);
1.502 + }
1.503 +
1.504 + case FIELD_EHANDLE:
1.505 + {
1.506 + CBaseHandle temp = eVal;
1.507 +
1.508 + const char *pszName = g_OutputManager.BaseHandleToEdict(temp)->GetClassName();
1.509 +
1.510 + if (pszName == NULL)
1.511 + {
1.512 + Q_strncpy( szBuf, "<<null entity>>", 512 );
1.513 + return (szBuf);
1.514 + }
1.515 +
1.516 + Q_strncpy( szBuf, pszName, 512 );
1.517 + return (szBuf);
1.518 + }
1.519 +
1.520 + default:
1.521 + break;
1.522 + }
1.523 +
1.524 + return("No conversion to string");
1.525 +}
1.526 +#endif
1.527 +// Thanks SM core
1.528 +edict_t *EntityOutputManager::BaseHandleToEdict(CBaseHandle &hndl)
1.529 +{
1.530 + if (!hndl.IsValid())
1.531 + {
1.532 + return NULL;
1.533 + }
1.534 +
1.535 + int index = hndl.GetEntryIndex();
1.536 +
1.537 + edict_t *pStoredEdict;
1.538 +
1.539 + pStoredEdict = engine->PEntityOfEntIndex(index);
1.540 +
1.541 + if (pStoredEdict == NULL)
1.542 + {
1.543 + return NULL;
1.544 + }
1.545 +
1.546 + IServerEntity *pSE = pStoredEdict->GetIServerEntity();
1.547 +
1.548 + if (pSE == NULL)
1.549 + {
1.550 + return NULL;
1.551 + }
1.552 +
1.553 + if (pSE->GetRefEHandle() != hndl)
1.554 + {
1.555 + return NULL;
1.556 + }
1.557 +
1.558 + return pStoredEdict;
1.559 +}
1.560 +
1.561 +
1.562 +