core/HalfLife2.cpp
branchsourcemod-1.1.0
changeset 2283 03a67e41afbc
parent 1951 c7b4237fff50
child 2287 017a725c9d17
     1.1 --- a/core/HalfLife2.cpp	Fri Mar 28 06:21:34 2008 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,441 +0,0 @@
     1.4 -/**
     1.5 - * vim: set ts=4 :
     1.6 - * =============================================================================
     1.7 - * SourceMod
     1.8 - * Copyright (C) 2004-2007 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$
    1.33 - */
    1.34 -
    1.35 -#include "HalfLife2.h"
    1.36 -#include "sourcemod.h"
    1.37 -#include "sourcemm_api.h"
    1.38 -#include "UserMessages.h"
    1.39 -#include "PlayerManager.h"
    1.40 -#include "sm_stringutil.h"
    1.41 -#include "GameConfigs.h"
    1.42 -#include <compat_wrappers.h>
    1.43 -
    1.44 -CHalfLife2 g_HL2;
    1.45 -ConVar *sv_lan = NULL;
    1.46 -
    1.47 -namespace SourceHook
    1.48 -{
    1.49 -	template<>
    1.50 -	int HashFunction<datamap_t *>(datamap_t * const &k)
    1.51 -	{
    1.52 -		return reinterpret_cast<int>(k);
    1.53 -	}
    1.54 -
    1.55 -	template<>
    1.56 -	int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
    1.57 -	{
    1.58 -		return (k1-k2);
    1.59 -	}
    1.60 -}
    1.61 -
    1.62 -CHalfLife2::CHalfLife2()
    1.63 -{
    1.64 -	m_pClasses = sm_trie_create();
    1.65 -}
    1.66 -
    1.67 -CHalfLife2::~CHalfLife2()
    1.68 -{
    1.69 -	sm_trie_destroy(m_pClasses);
    1.70 -
    1.71 -	List<DataTableInfo *>::iterator iter;
    1.72 -	DataTableInfo *pInfo;
    1.73 -	for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
    1.74 -	{
    1.75 -		pInfo = (*iter);
    1.76 -		sm_trie_destroy(pInfo->lookup);
    1.77 -		delete pInfo;
    1.78 -	}
    1.79 -
    1.80 -	m_Tables.clear();
    1.81 -
    1.82 -	THash<datamap_t *, DataMapTrie>::iterator h_iter;
    1.83 -	for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
    1.84 -	{
    1.85 -		if (h_iter->val.trie)
    1.86 -		{
    1.87 -			sm_trie_destroy(h_iter->val.trie);
    1.88 -			h_iter->val.trie = NULL;
    1.89 -		}
    1.90 -	}
    1.91 -
    1.92 -	m_Maps.clear();
    1.93 -}
    1.94 -
    1.95 -CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
    1.96 -bool is_original_engine = false;
    1.97 -
    1.98 -void CHalfLife2::OnSourceModStartup(bool late)
    1.99 -{
   1.100 -	/* The Ship currently is the only known game to use an older version of the engine */
   1.101 -#if defined METAMOD_PLAPI_VERSION
   1.102 -	if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
   1.103 -#else
   1.104 -	if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
   1.105 -#endif
   1.106 -	{
   1.107 -		is_original_engine = true;
   1.108 -	}
   1.109 -	else if (g_pSharedChangeInfo == NULL)
   1.110 -	{
   1.111 -		g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
   1.112 -	}
   1.113 -}
   1.114 -
   1.115 -void CHalfLife2::OnSourceModAllInitialized()
   1.116 -{
   1.117 -	m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
   1.118 -	m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
   1.119 -	m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
   1.120 -	g_ShareSys.AddInterface(NULL, this);
   1.121 -}
   1.122 -
   1.123 -#if !defined METAMOD_PLAPI_VERSION
   1.124 -bool CHalfLife2::IsOriginalEngine()
   1.125 -{
   1.126 -	return is_original_engine;
   1.127 -}
   1.128 -#endif
   1.129 -
   1.130 -IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
   1.131 -{
   1.132 -	return engine->GetChangeAccessor( (const edict_t *)this );
   1.133 -}
   1.134 -
   1.135 -SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
   1.136 -{
   1.137 -	const char *pname;
   1.138 -	int props = pTable->GetNumProps();
   1.139 -	SendProp *prop;
   1.140 -
   1.141 -	for (int i=0; i<props; i++)
   1.142 -	{
   1.143 -		prop = pTable->GetProp(i);
   1.144 -		pname = prop->GetName();
   1.145 -		if (pname && strcmp(name, pname) == 0)
   1.146 -		{
   1.147 -			return prop;
   1.148 -		}
   1.149 -		if (prop->GetDataTable())
   1.150 -		{
   1.151 -			if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
   1.152 -			{
   1.153 -				return prop;
   1.154 -			}
   1.155 -		}
   1.156 -	}
   1.157 -
   1.158 -	return NULL;
   1.159 -}
   1.160 -
   1.161 -typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
   1.162 -{
   1.163 -	while (pMap)
   1.164 -	{
   1.165 -		for (int i=0; i<pMap->dataNumFields; i++)
   1.166 -		{
   1.167 -			if (pMap->dataDesc[i].fieldName == NULL)
   1.168 -			{
   1.169 -				continue;
   1.170 -			}
   1.171 -			if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
   1.172 -			{
   1.173 -				return &(pMap->dataDesc[i]);
   1.174 -			}
   1.175 -			if (pMap->dataDesc[i].td)
   1.176 -			{
   1.177 -				typedescription_t *_td;
   1.178 -				if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
   1.179 -				{
   1.180 -					return _td;
   1.181 -				}
   1.182 -			}
   1.183 -		}
   1.184 -		pMap = pMap->baseMap;
   1.185 -	}
   1.186 -
   1.187 -	return NULL; 
   1.188 -}
   1.189 -
   1.190 -ServerClass *CHalfLife2::FindServerClass(const char *classname)
   1.191 -{
   1.192 -	DataTableInfo *pInfo = _FindServerClass(classname);
   1.193 -	if (!pInfo)
   1.194 -	{
   1.195 -		return NULL;
   1.196 -	}
   1.197 -
   1.198 -	return pInfo->sc;
   1.199 -}
   1.200 -
   1.201 -DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
   1.202 -{
   1.203 -	DataTableInfo *pInfo = NULL;
   1.204 -
   1.205 -	if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
   1.206 -	{
   1.207 -		ServerClass *sc = gamedll->GetAllServerClasses();
   1.208 -		while (sc)
   1.209 -		{
   1.210 -			if (strcmp(classname, sc->GetName()) == 0)
   1.211 -			{
   1.212 -				pInfo = new DataTableInfo;
   1.213 -				pInfo->lookup = sm_trie_create();
   1.214 -				pInfo->sc = sc;
   1.215 -				sm_trie_insert(m_pClasses, classname, pInfo);
   1.216 -				m_Tables.push_back(pInfo);
   1.217 -				break;
   1.218 -			}
   1.219 -			sc = sc->m_pNext;
   1.220 -		}
   1.221 -		if (!pInfo)
   1.222 -		{
   1.223 -			return NULL;
   1.224 -		}
   1.225 -	}
   1.226 -
   1.227 -	return pInfo;
   1.228 -}
   1.229 -
   1.230 -SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
   1.231 -{
   1.232 -	DataTableInfo *pInfo = _FindServerClass(classname);
   1.233 -
   1.234 -	if (!pInfo)
   1.235 -	{
   1.236 -		return NULL;
   1.237 -	}
   1.238 -
   1.239 -	SendProp *pProp = NULL;
   1.240 -	if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
   1.241 -	{
   1.242 -		if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
   1.243 -		{
   1.244 -			sm_trie_insert(pInfo->lookup, offset, pProp);
   1.245 -		}
   1.246 -	}
   1.247 -
   1.248 -	return pProp;
   1.249 -}
   1.250 -
   1.251 -typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
   1.252 -{
   1.253 -	typedescription_t *td = NULL;
   1.254 -	DataMapTrie &val = m_Maps[pMap];
   1.255 -
   1.256 -	if (!val.trie)
   1.257 -	{
   1.258 -		val.trie = sm_trie_create();
   1.259 -	}
   1.260 -	if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
   1.261 -	{
   1.262 -		if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
   1.263 -		{
   1.264 -			sm_trie_insert(val.trie, offset, td);
   1.265 -		}
   1.266 -	}
   1.267 -
   1.268 -	return td;
   1.269 -}
   1.270 -
   1.271 -void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
   1.272 -{
   1.273 -	if (g_pSharedChangeInfo != NULL)
   1.274 -	{
   1.275 -		if (offset)
   1.276 -		{
   1.277 -			pEdict->StateChanged(offset);
   1.278 -		} else {
   1.279 -			pEdict->StateChanged();
   1.280 -		}
   1.281 -	}
   1.282 -	else
   1.283 -	{
   1.284 -		pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
   1.285 -	}
   1.286 -}
   1.287 -
   1.288 -bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
   1.289 -{
   1.290 -	bf_write *pBitBuf = NULL;
   1.291 -	cell_t players[] = {client};
   1.292 -
   1.293 -	if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   1.294 -	{
   1.295 -		return false;
   1.296 -	}
   1.297 -
   1.298 -	pBitBuf->WriteByte(dest);
   1.299 -	pBitBuf->WriteString(msg);
   1.300 -	g_UserMsgs.EndMessage();
   1.301 -
   1.302 -	return true;
   1.303 -}
   1.304 -
   1.305 -bool CHalfLife2::HintTextMsg(int client, const char *msg)
   1.306 -{
   1.307 -	bf_write *pBitBuf = NULL;
   1.308 -	cell_t players[] = {client};
   1.309 -
   1.310 -	if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   1.311 -	{
   1.312 -		return false;
   1.313 -	}
   1.314 -
   1.315 -	const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
   1.316 -	if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
   1.317 -	{
   1.318 -		pBitBuf->WriteByte(1);
   1.319 -	}
   1.320 -	pBitBuf->WriteString(msg);
   1.321 -	g_UserMsgs.EndMessage();
   1.322 -
   1.323 -	return true;
   1.324 -}
   1.325 -
   1.326 -bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
   1.327 -{
   1.328 -	bf_write *pBitBuf = NULL;
   1.329 -	KeyValues *SubKey = NULL;
   1.330 -	int count = 0;
   1.331 -	cell_t players[] = {client};
   1.332 -
   1.333 -	if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
   1.334 -	{
   1.335 -		return false;
   1.336 -	}
   1.337 -
   1.338 -	if (data)
   1.339 -	{
   1.340 -		SubKey = data->GetFirstSubKey();
   1.341 -		while (SubKey)
   1.342 -		{
   1.343 -			count++;
   1.344 -			SubKey = SubKey->GetNextKey();
   1.345 -		}
   1.346 -		SubKey = data->GetFirstSubKey();
   1.347 -	}
   1.348 -
   1.349 -	pBitBuf->WriteString(name);
   1.350 -	pBitBuf->WriteByte((show) ? 1 : 0);
   1.351 -	pBitBuf->WriteByte(count);
   1.352 -	while (SubKey)
   1.353 -	{
   1.354 -		pBitBuf->WriteString(SubKey->GetName());
   1.355 -		pBitBuf->WriteString(SubKey->GetString());
   1.356 -		SubKey = SubKey->GetNextKey();
   1.357 -	}
   1.358 -	g_UserMsgs.EndMessage();
   1.359 -
   1.360 -	return true;
   1.361 -}
   1.362 -
   1.363 -void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
   1.364 -{
   1.365 -	DelayedFakeCliCmd *pFake;
   1.366 -
   1.367 -	if (m_FreeCmds.empty())
   1.368 -	{
   1.369 -		pFake = new DelayedFakeCliCmd;
   1.370 -	} else {
   1.371 -		pFake = m_FreeCmds.front();
   1.372 -		m_FreeCmds.pop();
   1.373 -	}
   1.374 -
   1.375 -	pFake->client = client;
   1.376 -	pFake->userid = userid;
   1.377 -	pFake->cmd.assign(cmd);
   1.378 -
   1.379 -	m_CmdQueue.push(pFake);
   1.380 -}
   1.381 -
   1.382 -void CHalfLife2::ProcessFakeCliCmdQueue()
   1.383 -{
   1.384 -	while (!m_CmdQueue.empty())
   1.385 -	{
   1.386 -		DelayedFakeCliCmd *pFake = m_CmdQueue.first();
   1.387 -
   1.388 -		if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
   1.389 -		{
   1.390 -			CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
   1.391 -			serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
   1.392 -		}
   1.393 -
   1.394 -		m_CmdQueue.pop();
   1.395 -	}
   1.396 -}
   1.397 -
   1.398 -bool CHalfLife2::IsLANServer()
   1.399 -{
   1.400 -	sv_lan = icvar->FindVar("sv_lan");
   1.401 -
   1.402 -	if (!sv_lan)
   1.403 -	{
   1.404 -		return false;
   1.405 -	}
   1.406 -
   1.407 -	return (sv_lan->GetInt() != 0);
   1.408 -}
   1.409 -
   1.410 -void CHalfLife2::PushCommandStack(const CCommand *cmd)
   1.411 -{
   1.412 -	CachedCommandInfo info;
   1.413 -
   1.414 -	info.args = cmd;
   1.415 -#if !defined ORANGEBOX_BUILD
   1.416 -	strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
   1.417 -#endif
   1.418 -
   1.419 -	m_CommandStack.push(info);
   1.420 -}
   1.421 -
   1.422 -const CCommand *CHalfLife2::PeekCommandStack()
   1.423 -{
   1.424 -	if (m_CommandStack.empty())
   1.425 -	{
   1.426 -		return NULL;
   1.427 -	}
   1.428 -
   1.429 -	return m_CommandStack.front().args;
   1.430 -}
   1.431 -
   1.432 -void CHalfLife2::PopCommandStack()
   1.433 -{
   1.434 -	m_CommandStack.pop();
   1.435 -}
   1.436 -
   1.437 -const char *CHalfLife2::CurrentCommandName()
   1.438 -{
   1.439 -#if defined ORANGEBOX_BUILD
   1.440 -	return m_CommandStack.front().args->Arg(0);
   1.441 -#else
   1.442 -	return m_CommandStack.front().cmd;
   1.443 -#endif
   1.444 -}