Added more missing svn:keywords properties to some files (this should be the last of them before I never have to do this again)
Moved sm_memtable files in sqlite extension to sdk directory (to be consistent with topmenus)
Some extension source files had an incorrect extension name in their headers
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 bool EntityOutputManager::CreateFireEventDetour()
40 if (!g_pGameConf->GetMemSig("FireOutput", &info_address) || !info_address)
42 g_pSM->LogError(myself, "Could not locate FireOutput - Disabling Entity Outputs");
46 if (!g_pGameConf->GetOffset("FireOutputBackup", (int *)&(info_restore.bytes)))
48 g_pSM->LogError(myself, "Could not locate FireOutputBackup - Disabling Entity Outputs");
52 /* First, save restore bits */
53 for (size_t i=0; i<info_restore.bytes; i++)
55 info_restore.patch[i] = ((unsigned char *)info_address)[i];
58 info_callback = spengine->ExecAlloc(100);
61 wr.outbase = (jitcode_t)info_callback;
62 wr.outptr = wr.outbase;
64 /* Function we are detouring into is
66 * void FireEventDetour(CBaseEntityOutput(void *) *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay = 0 )
69 /* push fDelay [esp+20h]
70 * push pCaller [esp+1Ch]
71 * push pActivator [esp+18h]
76 #if defined PLATFORM_WINDOWS
78 IA32_Push_Rm_Disp8_ESP(jit, 32);
79 IA32_Push_Rm_Disp8_ESP(jit, 32);
80 IA32_Push_Rm_Disp8_ESP(jit, 32);
82 /* variant_t doesnt do anything so i'll disable this bit */
83 //IA32_Push_Rm_Disp8_ESP(jit, 32);
84 //IA32_Push_Rm_Disp8_ESP(jit, 32);
85 //IA32_Push_Rm_Disp8_ESP(jit, 32);
86 //IA32_Push_Rm_Disp8_ESP(jit, 32);
87 //IA32_Push_Rm_Disp8_ESP(jit, 32);
89 IA32_Push_Reg(jit, REG_ECX);
91 #elif defined PLATFORM_LINUX
92 IA32_Push_Rm_Disp8_ESP(jit, 20);
93 IA32_Push_Rm_Disp8_ESP(jit, 20);
94 IA32_Push_Rm_Disp8_ESP(jit, 20);
95 // We miss the variant_t pointer
96 IA32_Push_Rm_Disp8_ESP(jit, 16);
99 jitoffs_t call = IA32_Call_Imm32(jit, 0);
100 IA32_Write_Jump32_Abs(jit, call, (void *)TempDetour);
103 #if defined PLATFORM_LINUX
104 IA32_Add_Rm_Imm8(jit, REG_ESP, 4, MOD_REG); //add esp, 4
105 #elif defined PLATFORM_WINDOWS
106 IA32_Pop_Reg(jit, REG_ECX);
109 IA32_Add_Rm_Imm8(jit, REG_ESP, 12, MOD_REG); //add esp, 12 (0Ch)
112 /* Patch old bytes in */
113 for (size_t i=0; i<info_restore.bytes; i++)
115 jit->write_ubyte(info_restore.patch[i]);
118 /* Return to the original function */
119 call = IA32_Jump_Imm32(jit, 0);
120 IA32_Write_Jump32_Abs(jit, call, (unsigned char *)info_address + info_restore.bytes);
125 void EntityOutputManager::InitFireEventDetour()
129 DoGatePatch((unsigned char *)info_address, &info_callback);
134 void EntityOutputManager::DeleteFireEventDetour()
138 ShutdownFireEventDetour();
144 spengine->ExecFree(info_callback);
145 info_callback = NULL;
149 void TempDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
151 g_OutputManager.FireEventDetour(pOutput, pActivator, pCaller, fDelay);
154 void EntityOutputManager::ShutdownFireEventDetour()
158 /* Remove the patch */
159 ApplyPatch(info_address, 0, &info_restore, NULL);
164 void EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
167 Q_snprintf(sOutput, sizeof(sOutput), "%x", pOutput);
169 // attempt to directly lookup a hook using the pOutput pointer
170 OutputNameStruct *pOutputName = NULL;
172 edict_t *pEdict = gameents->BaseEntityToEdict(pCaller);
174 /* TODO: Add support for entities without an edict */
180 bool fastLookup = false;
182 // Fast lookup failed - check the slow way for hooks that havn't fired yet
183 if ((fastLookup = EntityOutputs->Retrieve(sOutput, (void **)&pOutputName)) == false)
185 const char *classname = pEdict->GetClassName();
186 const char *outputname = FindOutputName(pOutput, pCaller);
188 pOutputName = FindOutputPointer(classname, outputname, false);
196 if (!pOutputName->hooks.empty())
200 // hook exists on this classname and output - map it into our quick find trie
201 EntityOutputs->Insert(sOutput, pOutputName);
204 SourceHook::List<omg_hooks *>::iterator _iter;
208 _iter = pOutputName->hooks.begin();
210 while (_iter != pOutputName->hooks.end())
212 hook = (omg_hooks *)*_iter;
216 int serial = pEdict->m_NetworkSerialNumber;
218 if (serial != hook->entity_filter && hook->entity_index == engine->IndexOfEdict(pEdict))
220 // same entity index but different serial number. Entity has changed, kill the hook.
221 _iter = pOutputName->hooks.erase(_iter);
227 if (hook->entity_filter == -1 || hook->entity_filter == serial) // Global classname hook
232 if (Value.FieldType() == FIELD_VOID)
238 varhandle_t *varhandle = new varhandle_t;
239 memmove ((void *)&varhandle->crab, (void *)&Value, sizeof(variant_t));
241 handle = handlesys->CreateHandle(VariantHandle, (void *)varhandle, hook->pf->GetParentContext()->GetIdentity(), myself->GetIdentity(), NULL);
245 //fire the forward to hook->pf
246 hook->pf->PushString(pOutputName->Name);
247 hook->pf->PushCell(engine->IndexOfEdict(pEdict));
249 edict_t *pEdictActivator = gameents->BaseEntityToEdict(pActivator);
250 if (!pEdictActivator)
252 hook->pf->PushCell(-1);
256 hook->pf->PushCell(engine->IndexOfEdict(pEdictActivator));
258 //hook->pf->PushCell(handle);
259 hook->pf->PushFloat(fDelay);
260 hook->pf->Execute(NULL);
262 if ((hook->entity_filter != -1) && hook->only_once)
264 _iter = pOutputName->hooks.erase(_iter);
272 _iter = pOutputName->hooks.erase(_iter);
277 hook->in_use = false;
286 omg_hooks *EntityOutputManager::NewHook()
290 if (FreeHooks.empty())
292 hook = new omg_hooks;
296 hook = g_OutputManager.FreeHooks.front();
297 g_OutputManager.FreeHooks.pop();
303 void EntityOutputManager::OnHookAdded()
309 // This is the first hook created
310 InitFireEventDetour();
314 void EntityOutputManager::OnHookRemoved()
320 //we need to check if we are inside a detour. easy.
321 //if we are how do we
322 ShutdownFireEventDetour();
326 void EntityOutputManager::CleanUpHook(omg_hooks *hook)
328 FreeHooks.push(hook);
332 IPlugin *pPlugin = plsys->FindPluginByContext(hook->pf->GetParentContext()->GetContext());
333 SourceHook::List<omg_hooks *> *pList = NULL;
335 if (!pPlugin->GetProperty("OutputHookList", (void **)&pList, false) || !pList)
340 SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
342 omg_hooks *pluginHook;
344 while (p_iter != pList->end())
346 pluginHook = (omg_hooks *)*p_iter;
347 if (pluginHook == hook)
349 p_iter = pList->erase(p_iter);
358 void EntityOutputManager::OnPluginDestroyed(IPlugin *plugin)
360 SourceHook::List<omg_hooks *> *pList = NULL;
362 if (plugin->GetProperty("OutputHookList", (void **)&pList, true))
364 SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
367 while (p_iter != pList->end())
369 hook = (omg_hooks *)*p_iter;
371 p_iter = pList->erase(p_iter); //remove from this plugins list
372 hook->m_parent->hooks.remove(hook); // remove from the y's list
374 FreeHooks.push(hook); //save the omg_hook
381 OutputNameStruct *EntityOutputManager::FindOutputPointer(const char *classname, const char *outputname, bool create)
383 ClassNameStruct *pClassname;
385 if (!ClassNames->Retrieve(classname, (void **)&pClassname))
389 pClassname = new ClassNameStruct;
390 ClassNames->Insert(classname, pClassname);
398 OutputNameStruct *pOutputName;
400 if (!pClassname->OutputList->Retrieve(outputname, (void **)&pOutputName))
404 pOutputName = new OutputNameStruct;
405 pClassname->OutputList->Insert(outputname, pOutputName);
406 strncpy(pOutputName->Name, outputname, sizeof(pOutputName->Name));
407 pOutputName->Name[49] = 0;
418 // Iterate the datamap of pCaller and look for output pointers with the same address as pOutput
419 const char *EntityOutputManager::FindOutputName(void *pOutput, CBaseEntity *pCaller)
421 datamap_t *pMap = gamehelpers->GetDataMap(pCaller);
425 for (int i=0; i<pMap->dataNumFields; i++)
427 if (pMap->dataDesc[i].flags & FTYPEDESC_OUTPUT)
429 if ((char *)pCaller + pMap->dataDesc[i].fieldOffset[0] == pOutput)
431 return pMap->dataDesc[i].externalName;
435 pMap = pMap->baseMap;
442 // Almost identical copy of this function from cbase.cpp - FIELD_EHANDLE changed to remove dependencies
443 const char *variant_t::ToString( void ) const
445 COMPILE_TIME_ASSERT( sizeof(string_t) == sizeof(int) );
447 static char szBuf[512];
453 return(STRING(iszVal));
460 Q_strncpy(szBuf, "false",sizeof(szBuf));
464 Q_strncpy(szBuf, "true",sizeof(szBuf));
471 Q_snprintf( szBuf, sizeof( szBuf ), "%i", iVal );
477 Q_snprintf(szBuf,sizeof(szBuf), "%g", flVal);
483 Q_snprintf(szBuf,sizeof(szBuf), "%d %d %d %d", (int)rgbaVal.r, (int)rgbaVal.g, (int)rgbaVal.b, (int)rgbaVal.a);
489 Q_snprintf(szBuf,sizeof(szBuf), "[%g %g %g]", (double)vecVal[0], (double)vecVal[1], (double)vecVal[2]);
501 CBaseHandle temp = eVal;
503 const char *pszName = g_OutputManager.BaseHandleToEdict(temp)->GetClassName();
507 Q_strncpy( szBuf, "<<null entity>>", 512 );
511 Q_strncpy( szBuf, pszName, 512 );
519 return("No conversion to string");
524 edict_t *EntityOutputManager::BaseHandleToEdict(CBaseHandle &hndl)
531 int index = hndl.GetEntryIndex();
533 edict_t *pStoredEdict;
535 pStoredEdict = engine->PEntityOfEntIndex(index);
537 if (pStoredEdict == NULL)
542 IServerEntity *pSE = pStoredEdict->GetIServerEntity();
549 if (pSE->GetRefEHandle() != hndl)