3 * =============================================================================
4 * SourceMod SDKTools Extension
5 * Copyright (C) 2004-2010 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;
37 CDetour *fireOutputDetour = NULL;
39 EntityOutputManager::EntityOutputManager()
47 EntityOutputManager::~EntityOutputManager()
54 EntityOutputs->Destroy();
55 ClassNames->Destroy();
56 fireOutputDetour->Destroy();
59 void EntityOutputManager::Init()
61 enabled = CreateFireEventDetour();
68 EntityOutputs = adtfactory->CreateBasicTrie();
69 ClassNames = adtfactory->CreateBasicTrie();
72 bool EntityOutputManager::IsEnabled()
77 #ifdef PLATFORM_WINDOWS
78 DETOUR_DECL_MEMBER8(FireOutput, void, int, what, int, the, int, hell, int, msvc, void *, variant_t, CBaseEntity *, pActivator, CBaseEntity *, pCaller, float, fDelay)
80 g_OutputManager.FireEventDetour((void *)this, pActivator, pCaller, fDelay);
81 DETOUR_MEMBER_CALL(FireOutput)(what, the, hell, msvc, variant_t, pActivator, pCaller, fDelay);
84 DETOUR_DECL_MEMBER4(FireOutput, void, void *, variant_t, CBaseEntity *, pActivator, CBaseEntity *, pCaller, float, fDelay)
86 g_OutputManager.FireEventDetour((void *)this, pActivator, pCaller, fDelay);
87 DETOUR_MEMBER_CALL(FireOutput)(variant_t, pActivator, pCaller, fDelay);
91 bool EntityOutputManager::CreateFireEventDetour()
93 fireOutputDetour = DETOUR_CREATE_MEMBER(FireOutput, "FireOutput");
95 if (fireOutputDetour) return true;
100 void EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
108 Q_snprintf(sOutput, sizeof(sOutput), "%x", pOutput);
110 // attempt to directly lookup a hook using the pOutput pointer
111 OutputNameStruct *pOutputName = NULL;
113 bool fastLookup = false;
115 // Fast lookup failed - check the slow way for hooks that haven't fired yet
116 if ((fastLookup = EntityOutputs->Retrieve(sOutput, (void **)&pOutputName)) == false)
118 const char *classname = GetEntityClassname(pCaller);
119 const char *outputname = FindOutputName(pOutput, pCaller);
126 pOutputName = FindOutputPointer(classname, outputname, false);
134 if (!pOutputName->hooks.empty())
138 // hook exists on this classname and output - map it into our quick find trie
139 EntityOutputs->Insert(sOutput, pOutputName);
142 SourceHook::List<omg_hooks *>::iterator _iter;
146 _iter = pOutputName->hooks.begin();
148 while (_iter != pOutputName->hooks.end())
150 hook = (omg_hooks *)*_iter;
154 cell_t ref = gamehelpers->EntityToReference(pCaller);
156 if (hook->entity_ref != -1
157 && gamehelpers->ReferenceToIndex(hook->entity_ref) == gamehelpers->ReferenceToIndex(ref)
158 && ref != hook->entity_ref)
160 // same entity index but different reference. Entity has changed, kill the hook.
161 _iter = pOutputName->hooks.erase(_iter);
167 if (hook->entity_ref == -1 || hook->entity_ref == ref) // Global classname hook
169 //fire the forward to hook->pf
170 hook->pf->PushString(pOutputName->Name);
171 hook->pf->PushCell(gamehelpers->ReferenceToBCompatRef(ref));
172 hook->pf->PushCell(gamehelpers->EntityToBCompatRef(pActivator));
174 //hook->pf->PushCell(handle);
175 hook->pf->PushFloat(fDelay);
176 hook->pf->Execute(NULL);
178 if ((hook->entity_ref != -1) && hook->only_once)
180 _iter = pOutputName->hooks.erase(_iter);
188 _iter = pOutputName->hooks.erase(_iter);
194 hook->in_use = false;
200 omg_hooks *EntityOutputManager::NewHook()
204 if (FreeHooks.empty())
206 hook = new omg_hooks;
210 hook = g_OutputManager.FreeHooks.front();
211 g_OutputManager.FreeHooks.pop();
217 void EntityOutputManager::OnHookAdded()
223 // This is the first hook created
224 fireOutputDetour->EnableDetour();
228 void EntityOutputManager::OnHookRemoved()
234 fireOutputDetour->DisableDetour();
238 void EntityOutputManager::CleanUpHook(omg_hooks *hook)
240 FreeHooks.push(hook);
244 IPlugin *pPlugin = plsys->FindPluginByContext(hook->pf->GetParentContext()->GetContext());
245 SourceHook::List<omg_hooks *> *pList = NULL;
247 if (!pPlugin->GetProperty("OutputHookList", (void **)&pList, false) || !pList)
252 SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
254 omg_hooks *pluginHook;
256 while (p_iter != pList->end())
258 pluginHook = (omg_hooks *)*p_iter;
259 if (pluginHook == hook)
261 p_iter = pList->erase(p_iter);
270 void EntityOutputManager::OnPluginDestroyed(IPlugin *plugin)
272 SourceHook::List<omg_hooks *> *pList = NULL;
274 if (plugin->GetProperty("OutputHookList", (void **)&pList, true))
276 SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
279 while (p_iter != pList->end())
281 hook = (omg_hooks *)*p_iter;
283 p_iter = pList->erase(p_iter); //remove from this plugins list
284 hook->m_parent->hooks.remove(hook); // remove from the y's list
286 FreeHooks.push(hook); //save the omg_hook
293 OutputNameStruct *EntityOutputManager::FindOutputPointer(const char *classname, const char *outputname, bool create)
295 ClassNameStruct *pClassname;
297 if (!ClassNames->Retrieve(classname, (void **)&pClassname))
301 pClassname = new ClassNameStruct;
302 ClassNames->Insert(classname, pClassname);
310 OutputNameStruct *pOutputName;
312 if (!pClassname->OutputList->Retrieve(outputname, (void **)&pOutputName))
316 pOutputName = new OutputNameStruct;
317 pClassname->OutputList->Insert(outputname, pOutputName);
318 strncpy(pOutputName->Name, outputname, sizeof(pOutputName->Name));
319 pOutputName->Name[49] = 0;
330 // Iterate the datamap of pCaller and look for output pointers with the same address as pOutput
331 const char *EntityOutputManager::FindOutputName(void *pOutput, CBaseEntity *pCaller)
333 datamap_t *pMap = gamehelpers->GetDataMap(pCaller);
337 for (int i=0; i<pMap->dataNumFields; i++)
339 if (pMap->dataDesc[i].flags & FTYPEDESC_OUTPUT)
341 if ((char *)pCaller + GetTypeDescOffs(&pMap->dataDesc[i]) == pOutput)
343 return pMap->dataDesc[i].externalName;
347 pMap = pMap->baseMap;
353 const char *EntityOutputManager::GetEntityClassname(CBaseEntity *pEntity)
355 static int offset = -1;
358 datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
359 typedescription_t *pDesc = gamehelpers->FindInDataMap(pMap, "m_iClassname");
360 offset = GetTypeDescOffs(pDesc);
363 return *(const char **)(((unsigned char *)pEntity) + offset);