extensions/sdkhooks/util.cpp
author David Anderson <dvander@alliedmods.net>
Thu Aug 08 22:02:42 2013 -0700 (2013-08-08)
changeset 3980 dc7dfcd855dc
parent 3767 7223d0a452a8
child 4054 0964336bb978
permissions -rw-r--r--
More Windows build fix.
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@3767
    67
bool UTIL_FindDataTable(SendTable *pTable, 
skamonkey@3767
    68
						  const char *name,
skamonkey@3767
    69
						  sm_sendprop_info_t *info,
skamonkey@3767
    70
						  unsigned int offset)
skamonkey@3767
    71
{
skamonkey@3767
    72
	const char *pname;
skamonkey@3767
    73
	int props = pTable->GetNumProps();
skamonkey@3767
    74
	SendProp *prop;
skamonkey@3767
    75
	SendTable *table;
skamonkey@3767
    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
				info->prop = prop;
skamonkey@3767
    87
				info->actual_offset = offset + info->prop->GetOffset();
skamonkey@3767
    88
				return true;
skamonkey@3767
    89
			}
skamonkey@3767
    90
skamonkey@3767
    91
			if (UTIL_FindDataTable(table, 
skamonkey@3767
    92
				name,
skamonkey@3767
    93
				info,
skamonkey@3767
    94
				offset + prop->GetOffset())
skamonkey@3767
    95
				)
skamonkey@3767
    96
			{
skamonkey@3767
    97
				return true;
skamonkey@3767
    98
			}
skamonkey@3767
    99
		}
skamonkey@3767
   100
	}
skamonkey@3767
   101
skamonkey@3767
   102
	return false;
skamonkey@3767
   103
}