extensions/sdkhooks/util.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Sat Aug 24 21:59:52 2013 -0400 (2013-08-24)
changeset 4054 0964336bb978
parent 3767 7223d0a452a8
child 4294 fa845b627daf
permissions -rw-r--r--
Fix SDKHooks hook ent validation missing first datatable name (bug 5881, r=asherkin).
skamonkey@3767
     1
/**
skamonkey@3767
     2
 * vim: set ts=4 :
skamonkey@3767
     3
 * =============================================================================
skamonkey@3767
     4
 * SourceMod Team Fortress 2 Extension
skamonkey@3767
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
skamonkey@3767
     6
 * =============================================================================
skamonkey@3767
     7
 *
skamonkey@3767
     8
 * This program is free software; you can redistribute it and/or modify it under
skamonkey@3767
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
skamonkey@3767
    10
 * Free Software Foundation.
skamonkey@3767
    11
 * 
skamonkey@3767
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
skamonkey@3767
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
skamonkey@3767
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
skamonkey@3767
    15
 * details.
skamonkey@3767
    16
 *
skamonkey@3767
    17
 * You should have received a copy of the GNU General Public License along with
skamonkey@3767
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
skamonkey@3767
    19
 *
skamonkey@3767
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
skamonkey@3767
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
skamonkey@3767
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
skamonkey@3767
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
skamonkey@3767
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
skamonkey@3767
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
skamonkey@3767
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
skamonkey@3767
    27
 * or <http://www.sourcemod.net/license.php>.
skamonkey@3767
    28
 *
skamonkey@3767
    29
 * Version: $Id$
skamonkey@3767
    30
 */
skamonkey@3767
    31
skamonkey@3767
    32
// ^ ripped from SM's TF2 Extension
skamonkey@3767
    33
skamonkey@3767
    34
#include "extension.h"
skamonkey@3767
    35
#include "util.h"
skamonkey@3767
    36
skamonkey@3767
    37
CBaseEntity *UTIL_GetCBaseEntity(int num, bool onlyPlayers)
skamonkey@3767
    38
{
skamonkey@3767
    39
	edict_t *pEdict = PEntityOfEntIndex(num);
skamonkey@3767
    40
	if (!pEdict || pEdict->IsFree())
skamonkey@3767
    41
	{
skamonkey@3767
    42
		return NULL;
skamonkey@3767
    43
	}
skamonkey@3767
    44
skamonkey@3767
    45
	if (num > 0 && num <= playerhelpers->GetMaxClients())
skamonkey@3767
    46
	{
skamonkey@3767
    47
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(pEdict);
skamonkey@3767
    48
		if (!pPlayer || !pPlayer->IsConnected())
skamonkey@3767
    49
		{
skamonkey@3767
    50
			return NULL;
skamonkey@3767
    51
		}
skamonkey@3767
    52
	}
skamonkey@3767
    53
	else if (onlyPlayers)
skamonkey@3767
    54
	{
skamonkey@3767
    55
		return NULL;
skamonkey@3767
    56
	}
skamonkey@3767
    57
skamonkey@3767
    58
	IServerUnknown *pUnk;
skamonkey@3767
    59
	if ((pUnk=pEdict->GetUnknown()) == NULL)
skamonkey@3767
    60
	{
skamonkey@3767
    61
		return NULL;
skamonkey@3767
    62
	}
skamonkey@3767
    63
skamonkey@3767
    64
	return pUnk->GetBaseEntity();
skamonkey@3767
    65
}
skamonkey@3767
    66
skamonkey@4054
    67
bool UTIL_ContainsDataTable(SendTable *pTable, const char *name)
skamonkey@3767
    68
{
skamonkey@4054
    69
	const char *pname = pTable->GetName();
skamonkey@3767
    70
	int props = pTable->GetNumProps();
skamonkey@3767
    71
	SendProp *prop;
skamonkey@3767
    72
	SendTable *table;
skamonkey@3767
    73
skamonkey@4054
    74
	if (pname && strcmp(name, pname) == 0)
skamonkey@4054
    75
		return true;
skamonkey@4054
    76
skamonkey@3767
    77
	for (int i=0; i<props; i++)
skamonkey@3767
    78
	{
skamonkey@3767
    79
		prop = pTable->GetProp(i);
skamonkey@3767
    80
skamonkey@3767
    81
		if ((table = prop->GetDataTable()) != NULL)
skamonkey@3767
    82
		{
skamonkey@3767
    83
			pname = table->GetName();
skamonkey@3767
    84
			if (pname && strcmp(name, pname) == 0)
skamonkey@3767
    85
			{
skamonkey@3767
    86
				return true;
skamonkey@3767
    87
			}
skamonkey@3767
    88
skamonkey@4054
    89
			if (UTIL_ContainsDataTable(table, name))
skamonkey@3767
    90
			{
skamonkey@3767
    91
				return true;
skamonkey@3767
    92
			}
skamonkey@3767
    93
		}
skamonkey@3767
    94
	}
skamonkey@3767
    95
skamonkey@3767
    96
	return false;
skamonkey@3767
    97
}