extensions/tf2/util.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Thu Aug 29 09:27:35 2013 -0400 (2013-08-29)
changeset 4092 f184c016f489
parent 2001 428da4bc4235
permissions -rw-r--r--
Clean up TF2 ext CalcCritical code (bug 5894, r=voided).
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
skamonkey@4092
    91
bool UTIL_ContainsDataTable(SendTable *pTable, const char *name)
skamonkey@4092
    92
{
skamonkey@4092
    93
	const char *pname = pTable->GetName();
skamonkey@4092
    94
	int props = pTable->GetNumProps();
skamonkey@4092
    95
	SendProp *prop;
skamonkey@4092
    96
	SendTable *table;
skamonkey@4092
    97
skamonkey@4092
    98
	if (pname && strcmp(name, pname) == 0)
skamonkey@4092
    99
		return true;
skamonkey@4092
   100
skamonkey@4092
   101
	for (int i = 0; i < props; i++)
skamonkey@4092
   102
	{
skamonkey@4092
   103
		prop = pTable->GetProp(i);
skamonkey@4092
   104
skamonkey@4092
   105
		if ((table = prop->GetDataTable()) != NULL)
skamonkey@4092
   106
		{
skamonkey@4092
   107
			pname = table->GetName();
skamonkey@4092
   108
			if (pname && strcmp(name, pname) == 0)
skamonkey@4092
   109
			{
skamonkey@4092
   110
				return true;
skamonkey@4092
   111
			}
skamonkey@4092
   112
skamonkey@4092
   113
			if (UTIL_ContainsDataTable(table, name))
skamonkey@4092
   114
			{
skamonkey@4092
   115
				return true;
skamonkey@4092
   116
			}
skamonkey@4092
   117
		}
skamonkey@4092
   118
	}
skamonkey@4092
   119
skamonkey@4092
   120
	return false;
skamonkey@4092
   121
}
skamonkey@4092
   122
pred@1963
   123
ServerClass *UTIL_FindServerClass(const char *classname)
pred@1963
   124
{
pred@1963
   125
	ServerClass *sc = gamedll->GetAllServerClasses();
pred@1963
   126
	while (sc)
pred@1963
   127
	{
pred@1963
   128
		if (strcmp(classname, sc->GetName()) == 0)
pred@1963
   129
		{
pred@1963
   130
			return sc;
pred@1963
   131
		}
pred@1963
   132
		sc = sc->m_pNext;
pred@1963
   133
	}
pred@1963
   134
pred@1963
   135
	return NULL;
pred@1963
   136
}
pred@1963
   137
pred@1963
   138
CBaseEntity *UTIL_GetCBaseEntity(int num, bool onlyPlayers)
pred@1963
   139
{
pred@1963
   140
	edict_t *pEdict = engine->PEntityOfEntIndex(num);
pred@1963
   141
	if (!pEdict || pEdict->IsFree())
pred@1963
   142
	{
pred@1963
   143
		return NULL;
pred@1963
   144
	}
pred@1963
   145
pred@2001
   146
	if (num > 0 && num <= playerhelpers->GetMaxClients())
pred@1963
   147
	{
pred@1963
   148
		IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(pEdict);
pred@1963
   149
		if (!pPlayer || !pPlayer->IsConnected())
pred@1963
   150
		{
pred@1963
   151
			return NULL;
pred@1963
   152
		}
pred@1963
   153
	}
pred@1963
   154
	else if (onlyPlayers)
pred@1963
   155
	{
pred@1963
   156
		return NULL;
pred@1963
   157
	}
pred@1963
   158
pred@1963
   159
	IServerUnknown *pUnk;
pred@1963
   160
	if ((pUnk=pEdict->GetUnknown()) == NULL)
pred@1963
   161
	{
pred@1963
   162
		return NULL;
pred@1963
   163
	}
pred@1963
   164
pred@1963
   165
	return pUnk->GetBaseEntity();
pred@1963
   166
}