extensions/sdktools/output.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Apr 04 13:44:19 2011 -0400 (2011-04-04)
changeset 3221 ca0e525dcf86
parent 3141 c246da24b0ad
child 3414 3d224fc6dca6
permissions -rw-r--r--
Fixed some gamedata lookups requiring symbols on linux/mac (bug 4832, r=fyren).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod SDKTools Extension
     5  * Copyright (C) 2004-2010 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     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.
    11  * 
    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
    15  * details.
    16  *
    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/>.
    19  *
    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>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "extension.h"
    33 #include "output.h"
    34 
    35 ISourcePawnEngine *spengine = NULL;
    36 EntityOutputManager g_OutputManager;
    37 CDetour *fireOutputDetour = NULL;
    38 
    39 EntityOutputManager::EntityOutputManager()
    40 {
    41 	info_address = NULL;
    42 	info_callback = NULL;
    43 	HookCount = 0;
    44 	enabled = false;
    45 }
    46 
    47 EntityOutputManager::~EntityOutputManager()
    48 {
    49 	if (!enabled)
    50 	{
    51 		return;
    52 	}
    53 
    54 	EntityOutputs->Destroy();
    55 	ClassNames->Destroy();
    56 	fireOutputDetour->Destroy();
    57 }
    58 
    59 void EntityOutputManager::Init()
    60 {
    61 	enabled = CreateFireEventDetour();
    62 
    63 	if (!enabled)
    64 	{
    65 		return;
    66 	}
    67 
    68 	EntityOutputs = adtfactory->CreateBasicTrie();
    69 	ClassNames = adtfactory->CreateBasicTrie();
    70 }
    71 
    72 bool EntityOutputManager::IsEnabled()
    73 {
    74 	return enabled;
    75 }
    76 
    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)
    79 {
    80 	g_OutputManager.FireEventDetour((void *)this, pActivator, pCaller, fDelay);
    81 	DETOUR_MEMBER_CALL(FireOutput)(what, the, hell, msvc, variant_t, pActivator, pCaller, fDelay);
    82 }
    83 #else
    84 DETOUR_DECL_MEMBER4(FireOutput, void, void *, variant_t, CBaseEntity *, pActivator, CBaseEntity *, pCaller, float, fDelay)
    85 {
    86 	g_OutputManager.FireEventDetour((void *)this, pActivator, pCaller, fDelay);
    87 	DETOUR_MEMBER_CALL(FireOutput)(variant_t, pActivator, pCaller, fDelay);
    88 }
    89 #endif
    90 
    91 bool EntityOutputManager::CreateFireEventDetour()
    92 {
    93 	fireOutputDetour = DETOUR_CREATE_MEMBER(FireOutput, "FireOutput");
    94 
    95 	if (fireOutputDetour) return true;
    96 
    97 	return false;
    98 }
    99 
   100 void EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay)
   101 {
   102 	if (!pCaller)
   103 	{
   104 		return;
   105 	}
   106 
   107 	char sOutput[20];
   108 	Q_snprintf(sOutput, sizeof(sOutput), "%x", pOutput);
   109 
   110 	// attempt to directly lookup a hook using the pOutput pointer
   111 	OutputNameStruct *pOutputName = NULL;
   112 
   113 	bool fastLookup = false;
   114 	
   115 	// Fast lookup failed - check the slow way for hooks that haven't fired yet
   116 	if ((fastLookup = EntityOutputs->Retrieve(sOutput, (void **)&pOutputName)) == false)
   117 	{
   118 		const char *classname = GetEntityClassname(pCaller);
   119 		const char *outputname = FindOutputName(pOutput, pCaller);
   120 		
   121 		if (!outputname)
   122 		{
   123 			return;
   124 		}
   125 
   126 		pOutputName = FindOutputPointer(classname, outputname, false);
   127 
   128 		if (!pOutputName)
   129 		{
   130 			return;
   131 		}
   132 	}
   133 
   134 	if (!pOutputName->hooks.empty())
   135 	{
   136 		if (!fastLookup)
   137 		{
   138 			// hook exists on this classname and output - map it into our quick find trie
   139 			EntityOutputs->Insert(sOutput, pOutputName);
   140 		}
   141 
   142 		SourceHook::List<omg_hooks *>::iterator _iter;
   143 
   144 		omg_hooks *hook;
   145 
   146 		_iter = pOutputName->hooks.begin();
   147 
   148 		while (_iter != pOutputName->hooks.end())
   149 		{
   150 			hook = (omg_hooks *)*_iter;
   151 
   152 			hook->in_use = true;
   153 
   154 			cell_t ref = gamehelpers->EntityToReference(pCaller);
   155 			
   156 			if (hook->entity_ref != -1 
   157 					&& gamehelpers->ReferenceToIndex(hook->entity_ref) == gamehelpers->ReferenceToIndex(ref)
   158 					&& ref != hook->entity_ref)
   159 			{
   160 				// same entity index but different reference. Entity has changed, kill the hook.
   161 				_iter = pOutputName->hooks.erase(_iter);
   162 				CleanUpHook(hook);
   163 
   164 				continue;
   165 			}
   166 
   167 			if (hook->entity_ref == -1 || hook->entity_ref == ref) // Global classname hook
   168 			{
   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));
   173 
   174 				//hook->pf->PushCell(handle);
   175 				hook->pf->PushFloat(fDelay);
   176 				hook->pf->Execute(NULL);
   177 
   178 				if ((hook->entity_ref != -1) && hook->only_once)
   179 				{
   180 					_iter = pOutputName->hooks.erase(_iter);
   181 					CleanUpHook(hook);
   182 
   183 					continue;
   184 				}
   185 
   186 				if (hook->delete_me)
   187 				{
   188 					_iter = pOutputName->hooks.erase(_iter);
   189 					CleanUpHook(hook);
   190 					continue;
   191 				}
   192 			}
   193 
   194 			hook->in_use = false;
   195 			_iter++;
   196 		}
   197 	}
   198 }
   199 
   200 omg_hooks *EntityOutputManager::NewHook()
   201 {
   202 	omg_hooks *hook;
   203 
   204 	if (FreeHooks.empty())
   205 	{
   206 		hook = new omg_hooks;
   207 	}
   208 	else
   209 	{
   210 		hook = g_OutputManager.FreeHooks.front();
   211 		g_OutputManager.FreeHooks.pop();
   212 	}
   213 
   214 	return hook;
   215 }
   216 
   217 void EntityOutputManager::OnHookAdded()
   218 {
   219 	HookCount++;
   220 
   221 	if (HookCount == 1)
   222 	{
   223 		// This is the first hook created
   224 		fireOutputDetour->EnableDetour();
   225 	}
   226 }
   227 
   228 void EntityOutputManager::OnHookRemoved()
   229 {
   230 	HookCount--;
   231 
   232 	if (HookCount == 0)
   233 	{
   234 		fireOutputDetour->DisableDetour();
   235 	}
   236 }
   237 
   238 void EntityOutputManager::CleanUpHook(omg_hooks *hook)
   239 {
   240 	FreeHooks.push(hook);
   241 
   242 	OnHookRemoved();
   243 
   244 	IPlugin *pPlugin = plsys->FindPluginByContext(hook->pf->GetParentContext()->GetContext());
   245 	SourceHook::List<omg_hooks *> *pList = NULL;
   246 
   247 	if (!pPlugin->GetProperty("OutputHookList", (void **)&pList, false) || !pList)
   248 	{
   249 		return;
   250 	}
   251 
   252 	SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
   253 
   254 	omg_hooks *pluginHook;
   255 
   256 	while (p_iter != pList->end())
   257 	{
   258 		pluginHook = (omg_hooks *)*p_iter;
   259 		if (pluginHook == hook)
   260 		{
   261 			p_iter = pList->erase(p_iter);
   262 		}
   263 		else
   264 		{
   265 			p_iter++;
   266 		}
   267 	}
   268 }
   269 
   270 void EntityOutputManager::OnPluginDestroyed(IPlugin *plugin)
   271 {
   272 	SourceHook::List<omg_hooks *> *pList = NULL;
   273 
   274 	if (plugin->GetProperty("OutputHookList", (void **)&pList, true))
   275 	{
   276 		SourceHook::List<omg_hooks *>::iterator p_iter = pList->begin();
   277 		omg_hooks *hook;
   278 
   279 		while (p_iter != pList->end())
   280 		{
   281 			hook = (omg_hooks *)*p_iter;
   282 		
   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
   285 
   286 			FreeHooks.push(hook); //save the omg_hook
   287 
   288 			OnHookRemoved();
   289 		}		
   290 	}
   291 }
   292 
   293 OutputNameStruct *EntityOutputManager::FindOutputPointer(const char *classname, const char *outputname, bool create)
   294 {
   295 	ClassNameStruct *pClassname;
   296 	
   297 	if (!ClassNames->Retrieve(classname, (void **)&pClassname))
   298 	{
   299 		if (create)
   300 		{
   301 			pClassname = new ClassNameStruct;
   302 			ClassNames->Insert(classname, pClassname);
   303 		}
   304 		else
   305 		{
   306 			return NULL;
   307 		}
   308 	}
   309 
   310 	OutputNameStruct *pOutputName;
   311 
   312 	if (!pClassname->OutputList->Retrieve(outputname, (void **)&pOutputName))
   313 	{
   314 		if (create)
   315 		{
   316 			pOutputName = new OutputNameStruct;
   317 			pClassname->OutputList->Insert(outputname, pOutputName);
   318 			strncpy(pOutputName->Name, outputname, sizeof(pOutputName->Name));
   319 			pOutputName->Name[49] = 0;
   320 		}
   321 		else
   322 		{
   323 			return NULL;
   324 		}
   325 	}
   326 
   327 	return pOutputName;
   328 }
   329 
   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)
   332 {
   333 	datamap_t *pMap = gamehelpers->GetDataMap(pCaller);
   334 
   335 	while (pMap)
   336 	{
   337 		for (int i=0; i<pMap->dataNumFields; i++)
   338 		{
   339 			if (pMap->dataDesc[i].flags & FTYPEDESC_OUTPUT)
   340 			{
   341 				if ((char *)pCaller + GetTypeDescOffs(&pMap->dataDesc[i]) == pOutput)
   342 				{
   343 					return pMap->dataDesc[i].externalName;
   344 				}
   345 			}
   346 		}
   347 		pMap = pMap->baseMap;
   348 	}
   349 
   350 	return NULL;
   351 }
   352 
   353 const char *EntityOutputManager::GetEntityClassname(CBaseEntity *pEntity)
   354 {
   355 	static int offset = -1;
   356 	if (offset == -1)
   357 	{
   358 		datamap_t *pMap = gamehelpers->GetDataMap(pEntity);
   359 		typedescription_t *pDesc = gamehelpers->FindInDataMap(pMap, "m_iClassname");
   360 		offset = GetTypeDescOffs(pDesc);
   361 	}
   362 
   363 	return *(const char **)(((unsigned char *)pEntity) + offset);
   364 }