1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/extensions/tf2/util.cpp Fri Apr 04 22:37:14 2008 +0000
1.3 @@ -0,0 +1,134 @@
1.4 +/**
1.5 + * vim: set ts=4 :
1.6 + * =============================================================================
1.7 + * SourceMod Team Fortress 2 Extension
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id: util.cpp 1558 2007-10-14 19:34:46Z faluco $
1.33 + */
1.34 +
1.35 +#include "extension.h"
1.36 +#include "util.h"
1.37 +
1.38 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1.39 +{
1.40 + va_list ap;
1.41 + va_start(ap, fmt);
1.42 + size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1.43 + va_end(ap);
1.44 +
1.45 + if (len >= maxlength)
1.46 + {
1.47 + buffer[maxlength - 1] = '\0';
1.48 + return (maxlength - 1);
1.49 + }
1.50 + else
1.51 + {
1.52 + return len;
1.53 + }
1.54 +}
1.55 +
1.56 +bool UTIL_FindDataTable(SendTable *pTable,
1.57 + const char *name,
1.58 + sm_sendprop_info_t *info,
1.59 + unsigned int offset)
1.60 +{
1.61 + const char *pname;
1.62 + int props = pTable->GetNumProps();
1.63 + SendProp *prop;
1.64 + SendTable *table;
1.65 +
1.66 + for (int i=0; i<props; i++)
1.67 + {
1.68 + prop = pTable->GetProp(i);
1.69 +
1.70 + if ((table = prop->GetDataTable()) != NULL)
1.71 + {
1.72 + pname = table->GetName();
1.73 + if (pname && strcmp(name, pname) == 0)
1.74 + {
1.75 + info->prop = prop;
1.76 + info->actual_offset = offset + info->prop->GetOffset();
1.77 + return true;
1.78 + }
1.79 +
1.80 + if (UTIL_FindDataTable(table,
1.81 + name,
1.82 + info,
1.83 + offset + prop->GetOffset())
1.84 + )
1.85 + {
1.86 + return true;
1.87 + }
1.88 + }
1.89 + }
1.90 +
1.91 + return false;
1.92 +}
1.93 +
1.94 +ServerClass *UTIL_FindServerClass(const char *classname)
1.95 +{
1.96 + ServerClass *sc = gamedll->GetAllServerClasses();
1.97 + while (sc)
1.98 + {
1.99 + if (strcmp(classname, sc->GetName()) == 0)
1.100 + {
1.101 + return sc;
1.102 + }
1.103 + sc = sc->m_pNext;
1.104 + }
1.105 +
1.106 + return NULL;
1.107 +}
1.108 +
1.109 +CBaseEntity *UTIL_GetCBaseEntity(int num, bool onlyPlayers)
1.110 +{
1.111 + edict_t *pEdict = engine->PEntityOfEntIndex(num);
1.112 + if (!pEdict || pEdict->IsFree())
1.113 + {
1.114 + return NULL;
1.115 + }
1.116 +
1.117 + if (num > 0 && num < playerhelpers->GetMaxClients())
1.118 + {
1.119 + IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(pEdict);
1.120 + if (!pPlayer || !pPlayer->IsConnected())
1.121 + {
1.122 + return NULL;
1.123 + }
1.124 + }
1.125 + else if (onlyPlayers)
1.126 + {
1.127 + return NULL;
1.128 + }
1.129 +
1.130 + IServerUnknown *pUnk;
1.131 + if ((pUnk=pEdict->GetUnknown()) == NULL)
1.132 + {
1.133 + return NULL;
1.134 + }
1.135 +
1.136 + return pUnk->GetBaseEntity();
1.137 +}