3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2008 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>.
32 #include "extension.h"
35 ISourcePawnEngine *spengine = NULL;
36 EntityOutputManager g_OutputManager;
38 EntityOutputManager::EntityOutputManager()
47 EntityOutputManager::~EntityOutputManager()
54 EntityOutputs->Destroy();
55 ClassNames->Destroy();
56 ShutdownFireEventDetour();
59 void EntityOutputManager::Init()
61 enabled = CreateFireEventDetour();
68 EntityOutputs = adtfactory->CreateBasicTrie();
69 ClassNames = adtfactory->CreateBasicTrie();
72 bool EntityOutputManager::IsEnabled()
77 bool EntityOutputManager::CreateFireEventDetour()
79 if (!g_pGameConf->GetMemSig("FireOutput", &info_address) || !info_address)
81 g_pSM->LogError(myself, "Could not locate FireOutput - Disabling Entity Outputs");
85 if (!g_pGameConf->GetOffset("FireOutputBackup", (int *)&(info_restore.bytes)))
87 g_pSM->LogError(myself, "Could not locate FireOutputBackup - Disabling Entity Outputs");
91 /* First, save restore bits */
92 for (size_t i=0; i<info_restore.bytes; i++)
94 info_restore.patch[i] = ((unsigned char *)info_address)[i];
97 info_callback = spengine->ExecAlloc(100);
100 wr.outbase = (jitcode_t)info_callback;
101 wr.outptr = wr.outbase;
103 /* Function we are detouring into is
105 * void FireEventDetour(CBaseEntityOutput(void *) *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay = 0 )
108 /* push fDelay [esp+20h]
109 * push pCaller [esp+1Ch]
110 * push pActivator [esp+18h]
115 #if defined PLATFORM_WINDOWS
117 IA32_Push_Rm_Disp8_ESP(jit, 32);
118 IA32_Push_Rm_Disp8_ESP(jit, 32);
119 IA32_Push_Rm_Disp8_ESP(jit, 32);
121 /* variant_t doesnt do anything so i'll disable this bit */
122 //IA32_Push_Rm_Disp8_ESP(jit, 32);
123 //IA32_Push_Rm_Disp8_ESP(jit, 32);
124 //IA32_Push_Rm_Disp8_ESP(jit, 32);
125 //IA32_Push_Rm_Disp8_ESP(jit, 32);
126 //IA32_Push_Rm_Disp8_ESP(jit, 32);
128 IA32_Push_Reg(jit, REG_ECX);
130 #elif defined PLATFORM_LINUX
131 IA32_Push_Rm_Disp8_ESP(jit, 20);
132 IA32_Push_Rm_Disp8_ESP(jit, 20);
133 IA32_Push_Rm_Disp8_ESP(jit, 20);
134 // We miss the variant_t pointer
135 IA32_Push_Rm_Disp8_ESP(jit, 16);
138 jitoffs_t call = IA32_Call_Imm32(jit, 0);
139 IA32_Write_Jump32_Abs(jit, call, (void *)TempDetour);
142 #if defined PLATFORM_LINUX
143 IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG); //add esp, 4
144 #elif defined PLATFORM_WINDOWS
145 IA32_Pop_Reg(jit, REG_ECX);
148 IA32_Add_Rm_Imm8(jit, REG_ESP, 12, MOD_REG); //add esp, 12 (0Ch)
151 /* Patch old bytes in */
152 for (size_t i=0; i<info_restore.bytes; i++)
154 jit->write_ubyte(info_restore.patch[i]);
157 /* Return to the original function */
158 call = IA32_Jump_Imm32(jit, 0);
159 IA32_Write_Jump32_Abs(jit, call, (unsigned char *)info_address + info_restore.bytes);
164 void EntityOutputManager::InitFireEventDetour()
168 DoGatePatch((unsigned char *)info_address, &info_callback);
173 void EntityOutputManager::DeleteFireEventDetour()
177 ShutdownFireEventDetour();
183 spengine->ExecFree(info_callback);
184 info_callback = NULL;
188 void TempDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
190 g_OutputManager.FireEventDetour(pOutput, pActivator, pCaller, fDelay);
193 void EntityOutputManager::ShutdownFireEventDetour()
197 /* Remove the patch */
198 ApplyPatch(info_address, 0, &info_restore, NULL);
203 void EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
206 Q_snprintf(sOutput, sizeof(sOutput), "%x", pOutput);
208 // attempt to directly lookup a hook using the pOutput pointer
209 OutputNameStruct *pOutputName = NULL;
211 edict_t *pEdict = gameents->BaseEntityToEdict(pCaller);
213 /* TODO: Add support for entities without an edict */
219 bool fastLookup = false;
221 // Fast lookup failed - check the slow way for hooks that havn't fired yet
222 if ((fastLookup = EntityOutputs->Retrieve(sOutput, (void **)&pOutputName)) == false)
224 const char *classname = pEdict->GetClassName();
225 const char *outputname = FindOutputName(pOutput, pCaller);
227 pOutputName = FindOutputPointer(classname, outputname, false);
235 if (!pOutputName->hooks.empty())
239 // hook exists on this classname and output - map it into our quick find trie
240 EntityOutputs->Insert(sOutput, pOutputName);
243 SourceHook::List<omg_hooks *>::iterator _iter;
247 _iter = pOutputName->hooks.begin();
249 while (_iter != pOutputName->hooks.end())
251 hook = (omg_hooks *)*_iter;
255 int serial = pEdict->m_NetworkSerialNumber;
257 if (serial != hook->entity_filter && hook->entity_index == engine->IndexOfEdict(pEdict))
259 // same entity index but different serial number. Entity has changed, kill the hook.
260 _iter = pOutputName->hooks.erase(_iter);
266 if (hook->entity_filter == -1 || hook->entity_filter == serial) // Global classname hook
271 if (Value.FieldType() == FIELD_VOID)
277 varhandle_t *varhandle = new varhandle_t;
278 memmove ((void *)&varhandle->crab, (void *)&Value, sizeof(variant_t));
280 handle = handlesys->CreateHandle(VariantHandle, (void *)varhandle, hook->pf->GetParentContext()->GetIdentity(), myself->GetIdentity(), NULL);
284 //fire the forward to hook->pf
285 hook->pf->PushString(pOutputName->Name);
286 hook->pf->PushCell(engine->IndexOfEdict(pEdict));
288 edict_t *pEdictActivator = gameents->BaseEntityToEdict(pActivator);
289 if (!pEdictActivator)
291 hook->pf->PushCell(-1);
295 hook->pf->PushCell(engine->IndexOfEdict(pEdictActivator));
297 //hook->pf->PushCell(handle);
298 hook->pf->PushFloat(fDelay);
299 hook->pf->Execute(NULL);
301 if ((hook->entity_filter != -1) && hook->only_once)
303 _iter = pOutputName->hooks.erase(_iter);
311 _iter = pOutputName->hooks.erase(_iter);
316 hook->in_use = false;
325 omg_hooks *EntityOutputManager::NewHook()
329 if (FreeHooks.empty())
331 hook = new omg_hooks;
335 hook = g_OutputManager.FreeHooks.front();
336 g_OutputManager.FreeHooks.pop();
342 void EntityOutputManager::OnHookAdded()
348 // This is the first hook created
349 InitFireEventDetour();
353 void EntityOutputManager::OnHookRemoved()
359 //we need to check if we are inside a detour. easy.
360 //if we are how do we
361 ShutdownFireEventDetour();
365 void EntityOutputManager::CleanUpHook(omg_hooks *hook)
367 FreeHooks.push(hook);
371 IPlugin *pPlugin = plsys->FindPluginByContext(hook->pf->GetParentContext()->GetContext());
372 SourceHook::List<omg_hooks *> *pList = NULL;
374 if (!pPlugin->GetProperty("OutputHookList", (void **)&pList, false) || !pList)
379 SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
381 omg_hooks *pluginHook;
383 while (p_iter != pList->end())
385 pluginHook = (omg_hooks *)*p_iter;
386 if (pluginHook == hook)
388 p_iter = pList->erase(p_iter);
397 void EntityOutputManager::OnPluginDestroyed(IPlugin *plugin)
399 SourceHook::List<omg_hooks *> *pList = NULL;
401 if (plugin->GetProperty("OutputHookList", (void **)&pList, true))
403 SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
406 while (p_iter != pList->end())
408 hook = (omg_hooks *)*p_iter;
410 p_iter = pList->erase(p_iter); //remove from this plugins list
411 hook->m_parent->hooks.remove(hook); // remove from the y's list
413 FreeHooks.push(hook); //save the omg_hook
420 OutputNameStruct *EntityOutputManager::FindOutputPointer(const char *classname, const char *outputname, bool create)
422 ClassNameStruct *pClassname;
424 if (!ClassNames->Retrieve(classname, (void **)&pClassname))
428 pClassname = new ClassNameStruct;
429 ClassNames->Insert(classname, pClassname);
437 OutputNameStruct *pOutputName;
439 if (!pClassname->OutputList->Retrieve(outputname, (void **)&pOutputName))
443 pOutputName = new OutputNameStruct;
444 pClassname->OutputList->Insert(outputname, pOutputName);
445 strncpy(pOutputName->Name, outputname, sizeof(pOutputName->Name));
446 pOutputName->Name[49] = 0;
457 // Iterate the datamap of pCaller and look for output pointers with the same address as pOutput
458 const char *EntityOutputManager::FindOutputName(void *pOutput, CBaseEntity *pCaller)
460 datamap_t *pMap = gamehelpers->GetDataMap(pCaller);
464 for (int i=0; i<pMap->dataNumFields; i++)
466 if (pMap->dataDesc[i].flags & FTYPEDESC_OUTPUT)
468 if ((char *)pCaller + pMap->dataDesc[i].fieldOffset[0] == pOutput)
470 return pMap->dataDesc[i].externalName;
474 pMap = pMap->baseMap;
481 // Almost identical copy of this function from cbase.cpp - FIELD_EHANDLE changed to remove dependencies
482 const char *variant_t::ToString( void ) const
484 COMPILE_TIME_ASSERT( sizeof(string_t) == sizeof(int) );
486 static char szBuf[512];
492 return(STRING(iszVal));
499 Q_strncpy(szBuf, "false",sizeof(szBuf));
503 Q_strncpy(szBuf, "true",sizeof(szBuf));
510 Q_snprintf( szBuf, sizeof( szBuf ), "%i", iVal );
516 Q_snprintf(szBuf,sizeof(szBuf), "%g", flVal);
522 Q_snprintf(szBuf,sizeof(szBuf), "%d %d %d %d", (int)rgbaVal.r, (int)rgbaVal.g, (int)rgbaVal.b, (int)rgbaVal.a);
528 Q_snprintf(szBuf,sizeof(szBuf), "[%g %g %g]", (double)vecVal[0], (double)vecVal[1], (double)vecVal[2]);
540 CBaseHandle temp = eVal;
542 const char *pszName = g_OutputManager.BaseHandleToEdict(temp)->GetClassName();
546 Q_strncpy( szBuf, "<<null entity>>", 512 );
550 Q_strncpy( szBuf, pszName, 512 );
558 return("No conversion to string");
563 edict_t *EntityOutputManager::BaseHandleToEdict(CBaseHandle &hndl)
570 int index = hndl.GetEntryIndex();
572 edict_t *pStoredEdict;
574 pStoredEdict = engine->PEntityOfEntIndex(index);
576 if (pStoredEdict == NULL)
581 IServerEntity *pSE = pStoredEdict->GetIServerEntity();
588 if (pSE->GetRefEHandle() != hndl)