extensions/tf2/util.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Mon Jul 29 10:03:18 2013 -0400 (2013-07-29)
changeset 3960 d50b36dc6821
parent 2001 428da4bc4235
child 4092 f184c016f489
permissions -rw-r--r--
Updated for latest hl2sdk-dota changes.
pred@1963
     1
/**
pred@1963
     2
 * vim: set ts=4 :
pred@1963
     3
 * =============================================================================
pred@1963
     4
 * SourceMod Team Fortress 2 Extension
pred@1963
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
pred@1963
     6
 * =============================================================================
pred@1963
     7
 *
pred@1963
     8
 * This program is free software; you can redistribute it and/or modify it under
pred@1963
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
pred@1963
    10
 * Free Software Foundation.
pred@1963
    11
 * 
pred@1963
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
pred@1963
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
pred@1963
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
pred@1963
    15
 * details.
pred@1963
    16
 *
pred@1963
    17
 * You should have received a copy of the GNU General Public License along with
pred@1963
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
pred@1963
    19
 *
pred@1963
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
pred@1963
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
pred@1963
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
pred@1963
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
pred@1963
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
pred@1963
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
pred@1963
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
pred@1963
    27
 * or <http://www.sourcemod.net/license.php>.
pred@1963
    28
 *
ds@1987
    29
 * Version: $Id$
pred@1963
    30
 */
pred@1963
    31
pred@1963
    32
#include "extension.h"
pred@1963
    33
#include "util.h"
pred@1963
    34
pred@1963
    35
size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
pred@1963
    36
{
pred@1963
    37
	va_list ap;
pred@1963
    38
	va_start(ap, fmt);
pred@1963
    39
	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
pred@1963
    40
	va_end(ap);
pred@1963
    41
pred@1963
    42
	if (len >= maxlength)
pred@1963
    43
	{
pred@1963
    44
		buffer[maxlength - 1] = '\0';
pred@1963
    45
		return (maxlength - 1);
pred@1963
    46
	}
pred@1963
    47
	else
pred@1963
    48
	{
pred@1963
    49
		return len;
pred@1963
    50
	}
pred@1963
    51
}
pred@1963
    52
pred@1963
    53
bool UTIL_FindDataTable(SendTable *pTable, 
pred@1963
    54
						  const char *name,
pred@1963
    55
						  sm_sendprop_info_t *info,
pred@1963
    56
						  unsigned int offset)
pred@1963
    57
{
pred@1963
    58
	const char *pname;
pred@1963
    59
	int props = pTable->GetNumProps();
pred@1963
    60
	SendProp *prop;
pred@1963
    61
	SendTable *table;
pred@1963
    62
pred@1963
    63
	for (int i=0; i<props; i++)
pred@1963
    64
	{
pred@1963
    65
		prop = pTable->GetProp(i);
pred@1963
    66
pred@1963
    67
		if ((table = prop->GetDataTable()) != NULL)
pred@1963
    68
		{
pred@1963
    69
			pname = table->GetName();
pred@1963
    70
			if (pname && strcmp(name, pname) == 0)
pred@1963
    71
			{
pred@1963
    72
				info->prop = prop;
pred@1963
    73
				info->actual_offset = offset + info->prop->GetOffset();
pred@1963
    74
				return true;
pred@1963
    75
			}
pred@1963
    76
pred@1963
    77
			if (UTIL_FindDataTable(table, 
pred@1963
    78
				name,
pred@1963
    79
				info,
pred@1963
    80
				offset + prop->GetOffset())
pred@1963
    81
				)
pred@1963
    82
			{
pred@1963
    83
				return true;
pred@1963
    84
			}
pred@1963
    85
		}
pred@1963
    86
	}
pred@1963
    87
pred@1963
    88
	return false;
pred@1963
    89
}
pred@1963
    90
pred@1963
    91
ServerClass *UTIL_FindServerClass(const char *classname)
pred@1963
    92
{
pred@1963
    93
	ServerClass *sc = gamedll->GetAllServerClasses();
pred@1963
    94
	while (sc)
pred@1963
    95
	{
pred@1963
    96
		if (strcmp(classname, sc->GetName()) == 0)
pred@1963
    97
		{
pred@1963
    98
			return sc;
pred@1963
    99
		}
pred@1963
   100
		sc = sc->m_pNext;
pred@1963
   101
	}
pred@1963
   102
pred@1963
   103
	return NULL;
pred@1963
   104
}
pred@1963
   105
pred@1963
   106
CBaseEntity *UTIL_GetCBaseEntity(int num, bool onlyPlayers)
pred@1963
   107
{
pred@1963
   108
	edict_t *pEdict = engine->PEntityOfEntIndex(num);
pred@1963
   109
	if (!pEdict || pEdict->IsFree())
pred@1963
   110
	{
pred@1963
   111
		return NULL;
pred@1963
   112
	}
pred@1963
   113
pred@2001
   114
	if (num > 0 && num <= playerhelpers->GetMaxClients())
pred@1963
   115
	{
pred@1963
   116
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(pEdict);
pred@1963
   117
		if (!pPlayer || !pPlayer->IsConnected())
pred@1963
   118
		{
pred@1963
   119
			return NULL;
pred@1963
   120
		}
pred@1963
   121
	}
pred@1963
   122
	else if (onlyPlayers)
pred@1963
   123
	{
pred@1963
   124
		return NULL;
pred@1963
   125
	}
pred@1963
   126
pred@1963
   127
	IServerUnknown *pUnk;
pred@1963
   128
	if ((pUnk=pEdict->GetUnknown()) == NULL)
pred@1963
   129
	{
pred@1963
   130
		return NULL;
pred@1963
   131
	}
pred@1963
   132
pred@1963
   133
	return pUnk->GetBaseEntity();
pred@1963
   134
}