2008-04-02 03:22:02 +02:00
|
|
|
/**
|
|
|
|
* vim: set ts=4 :
|
|
|
|
* =============================================================================
|
|
|
|
* SourceMod SDKTools Extension
|
2010-07-22 05:09:03 +02:00
|
|
|
* Copyright (C) 2004-2010 AlliedModders LLC. All rights reserved.
|
2008-04-02 03:22:02 +02:00
|
|
|
* =============================================================================
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License, version 3.0, as published by the
|
|
|
|
* Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* As a special exception, AlliedModders LLC gives you permission to link the
|
|
|
|
* code of this program (as well as its derivative works) to "Half-Life 2," the
|
|
|
|
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
|
|
|
|
* by the Valve Corporation. You must obey the GNU General Public License in
|
|
|
|
* all respects for all other code used. Additionally, AlliedModders LLC grants
|
|
|
|
* this exception to all derivative works. AlliedModders LLC defines further
|
|
|
|
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
|
|
|
|
* or <http://www.sourcemod.net/license.php>.
|
|
|
|
*
|
2008-04-10 21:28:26 +02:00
|
|
|
* Version: $Id$
|
2008-04-02 03:22:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "extension.h"
|
|
|
|
#include "output.h"
|
|
|
|
|
|
|
|
ISourcePawnEngine *spengine = NULL;
|
|
|
|
EntityOutputManager g_OutputManager;
|
2010-07-22 05:09:03 +02:00
|
|
|
CDetour *fireOutputDetour = NULL;
|
2008-04-02 03:22:02 +02:00
|
|
|
|
2008-04-12 03:33:55 +02:00
|
|
|
EntityOutputManager::EntityOutputManager()
|
|
|
|
{
|
|
|
|
info_address = NULL;
|
|
|
|
info_callback = NULL;
|
|
|
|
HookCount = 0;
|
|
|
|
enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityOutputManager::~EntityOutputManager()
|
|
|
|
{
|
|
|
|
if (!enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityOutputs->Destroy();
|
|
|
|
ClassNames->Destroy();
|
2010-07-22 05:09:03 +02:00
|
|
|
fireOutputDetour->Destroy();
|
2008-04-12 03:33:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntityOutputManager::Init()
|
|
|
|
{
|
|
|
|
enabled = CreateFireEventDetour();
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EntityOutputs = adtfactory->CreateBasicTrie();
|
|
|
|
ClassNames = adtfactory->CreateBasicTrie();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EntityOutputManager::IsEnabled()
|
|
|
|
{
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
2010-10-20 14:10:13 +02:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
DETOUR_DECL_MEMBER8(FireOutput, void, int, what, int, the, int, hell, int, msvc, void *, variant_t, CBaseEntity *, pActivator, CBaseEntity *, pCaller, float, fDelay)
|
|
|
|
{
|
|
|
|
g_OutputManager.FireEventDetour((void *)this, pActivator, pCaller, fDelay);
|
|
|
|
DETOUR_MEMBER_CALL(FireOutput)(what, the, hell, msvc, variant_t, pActivator, pCaller, fDelay);
|
|
|
|
}
|
|
|
|
#else
|
2010-07-22 05:09:03 +02:00
|
|
|
DETOUR_DECL_MEMBER4(FireOutput, void, void *, variant_t, CBaseEntity *, pActivator, CBaseEntity *, pCaller, float, fDelay)
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
2010-07-22 05:09:03 +02:00
|
|
|
g_OutputManager.FireEventDetour((void *)this, pActivator, pCaller, fDelay);
|
|
|
|
DETOUR_MEMBER_CALL(FireOutput)(variant_t, pActivator, pCaller, fDelay);
|
2008-04-02 03:22:02 +02:00
|
|
|
}
|
2010-10-20 14:10:13 +02:00
|
|
|
#endif
|
2008-04-02 03:22:02 +02:00
|
|
|
|
2010-07-22 05:09:03 +02:00
|
|
|
bool EntityOutputManager::CreateFireEventDetour()
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
2010-07-22 05:09:03 +02:00
|
|
|
fireOutputDetour = DETOUR_CREATE_MEMBER(FireOutput, "FireOutput");
|
2008-04-02 03:22:02 +02:00
|
|
|
|
2010-07-22 05:09:03 +02:00
|
|
|
if (fireOutputDetour) return true;
|
2008-04-02 03:22:02 +02:00
|
|
|
|
2010-07-22 05:09:03 +02:00
|
|
|
return false;
|
2008-04-02 03:22:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
|
|
|
|
{
|
2010-03-20 16:07:45 +01:00
|
|
|
if (!pCaller)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-04-02 03:22:02 +02:00
|
|
|
char sOutput[20];
|
|
|
|
Q_snprintf(sOutput, sizeof(sOutput), "%x", pOutput);
|
|
|
|
|
|
|
|
// attempt to directly lookup a hook using the pOutput pointer
|
|
|
|
OutputNameStruct *pOutputName = NULL;
|
|
|
|
|
|
|
|
bool fastLookup = false;
|
|
|
|
|
2009-10-27 08:03:59 +01:00
|
|
|
// Fast lookup failed - check the slow way for hooks that haven't fired yet
|
2008-04-02 03:22:02 +02:00
|
|
|
if ((fastLookup = EntityOutputs->Retrieve(sOutput, (void **)&pOutputName)) == false)
|
|
|
|
{
|
2009-07-24 02:34:31 +02:00
|
|
|
const char *classname = GetEntityClassname(pCaller);
|
2008-04-02 03:22:02 +02:00
|
|
|
const char *outputname = FindOutputName(pOutput, pCaller);
|
2010-06-14 15:32:50 +02:00
|
|
|
|
|
|
|
if (!outputname)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2008-04-02 03:22:02 +02:00
|
|
|
|
|
|
|
pOutputName = FindOutputPointer(classname, outputname, false);
|
|
|
|
|
|
|
|
if (!pOutputName)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pOutputName->hooks.empty())
|
|
|
|
{
|
|
|
|
if (!fastLookup)
|
|
|
|
{
|
|
|
|
// hook exists on this classname and output - map it into our quick find trie
|
|
|
|
EntityOutputs->Insert(sOutput, pOutputName);
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceHook::List<omg_hooks *>::iterator _iter;
|
|
|
|
|
|
|
|
omg_hooks *hook;
|
|
|
|
|
|
|
|
_iter = pOutputName->hooks.begin();
|
|
|
|
|
|
|
|
while (_iter != pOutputName->hooks.end())
|
|
|
|
{
|
|
|
|
hook = (omg_hooks *)*_iter;
|
|
|
|
|
|
|
|
hook->in_use = true;
|
|
|
|
|
2009-07-24 02:34:31 +02:00
|
|
|
cell_t ref = gamehelpers->EntityToReference(pCaller);
|
2008-04-02 03:22:02 +02:00
|
|
|
|
2009-07-24 02:34:31 +02:00
|
|
|
if (hook->entity_ref != -1
|
|
|
|
&& gamehelpers->ReferenceToIndex(hook->entity_ref) == gamehelpers->ReferenceToIndex(ref)
|
|
|
|
&& ref != hook->entity_ref)
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
2009-07-24 02:34:31 +02:00
|
|
|
// same entity index but different reference. Entity has changed, kill the hook.
|
2008-04-02 03:22:02 +02:00
|
|
|
_iter = pOutputName->hooks.erase(_iter);
|
|
|
|
CleanUpHook(hook);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-24 02:34:31 +02:00
|
|
|
if (hook->entity_ref == -1 || hook->entity_ref == ref) // Global classname hook
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
|
|
|
//fire the forward to hook->pf
|
|
|
|
hook->pf->PushString(pOutputName->Name);
|
2009-07-24 02:34:31 +02:00
|
|
|
hook->pf->PushCell(gamehelpers->ReferenceToBCompatRef(ref));
|
|
|
|
hook->pf->PushCell(gamehelpers->EntityToBCompatRef(pActivator));
|
|
|
|
|
2008-04-02 03:22:02 +02:00
|
|
|
//hook->pf->PushCell(handle);
|
|
|
|
hook->pf->PushFloat(fDelay);
|
|
|
|
hook->pf->Execute(NULL);
|
|
|
|
|
2009-07-24 02:34:31 +02:00
|
|
|
if ((hook->entity_ref != -1) && hook->only_once)
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
|
|
|
_iter = pOutputName->hooks.erase(_iter);
|
|
|
|
CleanUpHook(hook);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hook->delete_me)
|
|
|
|
{
|
|
|
|
_iter = pOutputName->hooks.erase(_iter);
|
|
|
|
CleanUpHook(hook);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2008-05-20 04:51:02 +02:00
|
|
|
|
|
|
|
hook->in_use = false;
|
|
|
|
_iter++;
|
2008-04-02 03:22:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
omg_hooks *EntityOutputManager::NewHook()
|
|
|
|
{
|
|
|
|
omg_hooks *hook;
|
|
|
|
|
|
|
|
if (FreeHooks.empty())
|
|
|
|
{
|
|
|
|
hook = new omg_hooks;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hook = g_OutputManager.FreeHooks.front();
|
|
|
|
g_OutputManager.FreeHooks.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
return hook;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityOutputManager::OnHookAdded()
|
|
|
|
{
|
|
|
|
HookCount++;
|
|
|
|
|
|
|
|
if (HookCount == 1)
|
|
|
|
{
|
|
|
|
// This is the first hook created
|
2010-07-22 05:09:03 +02:00
|
|
|
fireOutputDetour->EnableDetour();
|
2008-04-02 03:22:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityOutputManager::OnHookRemoved()
|
|
|
|
{
|
|
|
|
HookCount--;
|
|
|
|
|
|
|
|
if (HookCount == 0)
|
|
|
|
{
|
2010-07-22 05:09:03 +02:00
|
|
|
fireOutputDetour->DisableDetour();
|
2008-04-02 03:22:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityOutputManager::CleanUpHook(omg_hooks *hook)
|
|
|
|
{
|
|
|
|
FreeHooks.push(hook);
|
|
|
|
|
|
|
|
OnHookRemoved();
|
|
|
|
|
|
|
|
IPlugin *pPlugin = plsys->FindPluginByContext(hook->pf->GetParentContext()->GetContext());
|
|
|
|
SourceHook::List<omg_hooks *> *pList = NULL;
|
|
|
|
|
|
|
|
if (!pPlugin->GetProperty("OutputHookList", (void **)&pList, false) || !pList)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
|
|
|
|
|
|
|
|
omg_hooks *pluginHook;
|
|
|
|
|
|
|
|
while (p_iter != pList->end())
|
|
|
|
{
|
|
|
|
pluginHook = (omg_hooks *)*p_iter;
|
|
|
|
if (pluginHook == hook)
|
|
|
|
{
|
|
|
|
p_iter = pList->erase(p_iter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p_iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EntityOutputManager::OnPluginDestroyed(IPlugin *plugin)
|
|
|
|
{
|
|
|
|
SourceHook::List<omg_hooks *> *pList = NULL;
|
|
|
|
|
|
|
|
if (plugin->GetProperty("OutputHookList", (void **)&pList, true))
|
|
|
|
{
|
|
|
|
SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
|
|
|
|
omg_hooks *hook;
|
|
|
|
|
|
|
|
while (p_iter != pList->end())
|
|
|
|
{
|
|
|
|
hook = (omg_hooks *)*p_iter;
|
|
|
|
|
|
|
|
p_iter = pList->erase(p_iter); //remove from this plugins list
|
|
|
|
hook->m_parent->hooks.remove(hook); // remove from the y's list
|
|
|
|
|
|
|
|
FreeHooks.push(hook); //save the omg_hook
|
|
|
|
|
|
|
|
OnHookRemoved();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputNameStruct *EntityOutputManager::FindOutputPointer(const char *classname, const char *outputname, bool create)
|
|
|
|
{
|
|
|
|
ClassNameStruct *pClassname;
|
|
|
|
|
|
|
|
if (!ClassNames->Retrieve(classname, (void **)&pClassname))
|
|
|
|
{
|
|
|
|
if (create)
|
|
|
|
{
|
|
|
|
pClassname = new ClassNameStruct;
|
|
|
|
ClassNames->Insert(classname, pClassname);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputNameStruct *pOutputName;
|
|
|
|
|
|
|
|
if (!pClassname->OutputList->Retrieve(outputname, (void **)&pOutputName))
|
|
|
|
{
|
|
|
|
if (create)
|
|
|
|
{
|
|
|
|
pOutputName = new OutputNameStruct;
|
|
|
|
pClassname->OutputList->Insert(outputname, pOutputName);
|
|
|
|
strncpy(pOutputName->Name, outputname, sizeof(pOutputName->Name));
|
|
|
|
pOutputName->Name[49] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pOutputName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate the datamap of pCaller and look for output pointers with the same address as pOutput
|
|
|
|
const char *EntityOutputManager::FindOutputName(void *pOutput, CBaseEntity *pCaller)
|
|
|
|
{
|
|
|
|
datamap_t *pMap = gamehelpers->GetDataMap(pCaller);
|
|
|
|
|
|
|
|
while (pMap)
|
|
|
|
{
|
|
|
|
for (int i=0; i<pMap->dataNumFields; i++)
|
|
|
|
{
|
|
|
|
if (pMap->dataDesc[i].flags & FTYPEDESC_OUTPUT)
|
|
|
|
{
|
2010-07-28 00:32:32 +02:00
|
|
|
if ((char *)pCaller + GetTypeDescOffs(&pMap->dataDesc[i]) == pOutput)
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
|
|
|
return pMap->dataDesc[i].externalName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pMap = pMap->baseMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-04-10 21:25:50 +02:00
|
|
|
|
2009-07-24 02:34:31 +02:00
|
|
|
const char *EntityOutputManager::GetEntityClassname(CBaseEntity *pEntity)
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
2009-07-24 02:34:31 +02:00
|
|
|
static int offset = -1;
|
|
|
|
if (offset == -1)
|
2008-04-02 03:22:02 +02:00
|
|
|
{
|
2009-07-24 02:34:31 +02:00
|
|
|
datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
|
|
|
|
typedescription_t *pDesc = gamehelpers->FindInDataMap(pMap, "m_iClassname");
|
2010-07-28 00:32:32 +02:00
|
|
|
offset = GetTypeDescOffs(pDesc);
|
2008-04-02 03:22:02 +02:00
|
|
|
}
|
|
|
|
|
2009-10-27 08:03:59 +01:00
|
|
|
return *(const char **)(((unsigned char *)pEntity) + offset);
|
2009-07-24 03:17:26 +02:00
|
|
|
}
|