core/HalfLife2.cpp
author David Anderson <dvander@alliedmods.net>
Sun Oct 14 18:53:29 2007 +0000 (2007-10-14)
changeset 1547 78abc407c8a3
parent 1544 feff70182207
child 1581 2562512b4a44
permissions -rw-r--r--
various build fixes and vcproj changes
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2007 AlliedModders LLC.  All rights reserved.
     6  * =============================================================================
     7  *
     8  * This program is free software; you can redistribute it and/or modify it under
     9  * the terms of the GNU General Public License, version 3.0, as published by the
    10  * Free Software Foundation.
    11  * 
    12  * This program is distributed in the hope that it will be useful, but WITHOUT
    13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    15  * details.
    16  *
    17  * You should have received a copy of the GNU General Public License along with
    18  * this program.  If not, see <http://www.gnu.org/licenses/>.
    19  *
    20  * As a special exception, AlliedModders LLC gives you permission to link the
    21  * code of this program (as well as its derivative works) to "Half-Life 2," the
    22  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    23  * by the Valve Corporation.  You must obey the GNU General Public License in
    24  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    25  * this exception to all derivative works.  AlliedModders LLC defines further
    26  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    27  * or <http://www.sourcemod.net/license.php>.
    28  *
    29  * Version: $Id$
    30  */
    31 
    32 #include "HalfLife2.h"
    33 #include "sourcemod.h"
    34 #include "sourcemm_api.h"
    35 #include "UserMessages.h"
    36 #include "PlayerManager.h"
    37 #include "sm_stringutil.h"
    38 #include <compat_wrappers.h>
    39 
    40 CHalfLife2 g_HL2;
    41 bool g_IsOriginalEngine = false;
    42 ConVar *sv_lan = NULL;
    43 
    44 namespace SourceHook
    45 {
    46 	template<>
    47 	int HashFunction<datamap_t *>(datamap_t * const &k)
    48 	{
    49 		return reinterpret_cast<int>(k);
    50 	}
    51 
    52 	template<>
    53 	int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
    54 	{
    55 		return (k1-k2);
    56 	}
    57 }
    58 
    59 CHalfLife2::CHalfLife2()
    60 {
    61 	m_pClasses = sm_trie_create();
    62 }
    63 
    64 CHalfLife2::~CHalfLife2()
    65 {
    66 	sm_trie_destroy(m_pClasses);
    67 
    68 	List<DataTableInfo *>::iterator iter;
    69 	DataTableInfo *pInfo;
    70 	for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
    71 	{
    72 		pInfo = (*iter);
    73 		sm_trie_destroy(pInfo->lookup);
    74 		delete pInfo;
    75 	}
    76 
    77 	m_Tables.clear();
    78 
    79 	THash<datamap_t *, DataMapTrie>::iterator h_iter;
    80 	for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
    81 	{
    82 		if (h_iter->val.trie)
    83 		{
    84 			sm_trie_destroy(h_iter->val.trie);
    85 			h_iter->val.trie = NULL;
    86 		}
    87 	}
    88 
    89 	m_Maps.clear();
    90 }
    91 
    92 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
    93 
    94 void CHalfLife2::OnSourceModStartup(bool late)
    95 {
    96 	/* The Ship currently is the only known game to use an older version of the engine */
    97 	if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
    98 	{
    99 		/* :TODO: Better engine versioning - perhaps something added to SourceMM? */
   100 		g_IsOriginalEngine = true;
   101 	} else if (!g_pSharedChangeInfo) {
   102 		g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
   103 	}
   104 }
   105 
   106 void CHalfLife2::OnSourceModAllInitialized()
   107 {
   108 	m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
   109 	m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
   110 	m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
   111 	g_ShareSys.AddInterface(NULL, this);
   112 }
   113 
   114 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
   115 {
   116 	return engine->GetChangeAccessor( (const edict_t *)this );
   117 }
   118 
   119 SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
   120 {
   121 	const char *pname;
   122 	int props = pTable->GetNumProps();
   123 	SendProp *prop;
   124 
   125 	for (int i=0; i<props; i++)
   126 	{
   127 		prop = pTable->GetProp(i);
   128 		pname = prop->GetName();
   129 		if (pname && strcmp(name, pname) == 0)
   130 		{
   131 			return prop;
   132 		}
   133 		if (prop->GetDataTable())
   134 		{
   135 			if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
   136 			{
   137 				return prop;
   138 			}
   139 		}
   140 	}
   141 
   142 	return NULL;
   143 }
   144 
   145 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
   146 {
   147 	while (pMap)
   148 	{
   149 		for (int i=0; i<pMap->dataNumFields; i++)
   150 		{
   151 			if (pMap->dataDesc[i].fieldName == NULL)
   152 			{
   153 				continue;
   154 			}
   155 			if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
   156 			{
   157 				return &(pMap->dataDesc[i]);
   158 			}
   159 			if (pMap->dataDesc[i].td)
   160 			{
   161 				typedescription_t *_td;
   162 				if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
   163 				{
   164 					return _td;
   165 				}
   166 			}
   167 		}
   168 		pMap = pMap->baseMap;
   169 	}
   170 
   171 	return NULL; 
   172 }
   173 
   174 ServerClass *CHalfLife2::FindServerClass(const char *classname)
   175 {
   176 	DataTableInfo *pInfo = _FindServerClass(classname);
   177 	if (!pInfo)
   178 	{
   179 		return NULL;
   180 	}
   181 
   182 	return pInfo->sc;
   183 }
   184 
   185 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
   186 {
   187 	DataTableInfo *pInfo = NULL;
   188 
   189 	if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
   190 	{
   191 		ServerClass *sc = gamedll->GetAllServerClasses();
   192 		while (sc)
   193 		{
   194 			if (strcmp(classname, sc->GetName()) == 0)
   195 			{
   196 				pInfo = new DataTableInfo;
   197 				pInfo->lookup = sm_trie_create();
   198 				pInfo->sc = sc;
   199 				sm_trie_insert(m_pClasses, classname, pInfo);
   200 				m_Tables.push_back(pInfo);
   201 				break;
   202 			}
   203 			sc = sc->m_pNext;
   204 		}
   205 		if (!pInfo)
   206 		{
   207 			return NULL;
   208 		}
   209 	}
   210 
   211 	return pInfo;
   212 }
   213 
   214 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
   215 {
   216 	DataTableInfo *pInfo = _FindServerClass(classname);
   217 
   218 	if (!pInfo)
   219 	{
   220 		return NULL;
   221 	}
   222 
   223 	SendProp *pProp = NULL;
   224 	if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
   225 	{
   226 		if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
   227 		{
   228 			sm_trie_insert(pInfo->lookup, offset, pProp);
   229 		}
   230 	}
   231 
   232 	return pProp;
   233 }
   234 
   235 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
   236 {
   237 	typedescription_t *td = NULL;
   238 	DataMapTrie &val = m_Maps[pMap];
   239 
   240 	if (!val.trie)
   241 	{
   242 		val.trie = sm_trie_create();
   243 	}
   244 	if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
   245 	{
   246 		if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
   247 		{
   248 			sm_trie_insert(val.trie, offset, td);
   249 		}
   250 	}
   251 
   252 	return td;
   253 }
   254 
   255 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
   256 {
   257 	if (!g_IsOriginalEngine)
   258 	{
   259 		if (offset)
   260 		{
   261 			pEdict->StateChanged(offset);
   262 		} else {
   263 			pEdict->StateChanged();
   264 		}
   265 	} else {
   266 		pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
   267 	}
   268 }
   269 
   270 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
   271 {
   272 	bf_write *pBitBuf = NULL;
   273 	cell_t players[] = {client};
   274 
   275 	if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   276 	{
   277 		return false;
   278 	}
   279 
   280 	pBitBuf->WriteByte(dest);
   281 	pBitBuf->WriteString(msg);
   282 	g_UserMsgs.EndMessage();
   283 
   284 	return true;
   285 }
   286 
   287 bool CHalfLife2::HintTextMsg(int client, const char *msg)
   288 {
   289 	bf_write *pBitBuf = NULL;
   290 	cell_t players[] = {client};
   291 
   292 	if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   293 	{
   294 		return false;
   295 	}
   296 
   297 	pBitBuf->WriteByte(1);
   298 	pBitBuf->WriteString(msg);
   299 	g_UserMsgs.EndMessage();
   300 
   301 	return true;
   302 }
   303 
   304 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
   305 {
   306 	bf_write *pBitBuf = NULL;
   307 	KeyValues *SubKey = NULL;
   308 	int count = 0;
   309 	cell_t players[] = {client};
   310 
   311 	if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
   312 	{
   313 		return false;
   314 	}
   315 
   316 	if (data)
   317 	{
   318 		SubKey = data->GetFirstSubKey();
   319 		while (SubKey)
   320 		{
   321 			count++;
   322 			SubKey = SubKey->GetNextKey();
   323 		}
   324 		SubKey = data->GetFirstSubKey();
   325 	}
   326 
   327 	pBitBuf->WriteString(name);
   328 	pBitBuf->WriteByte((show) ? 1 : 0);
   329 	pBitBuf->WriteByte(count);
   330 	while (SubKey)
   331 	{
   332 		pBitBuf->WriteString(SubKey->GetName());
   333 		pBitBuf->WriteString(SubKey->GetString());
   334 		SubKey = SubKey->GetNextKey();
   335 	}
   336 	g_UserMsgs.EndMessage();
   337 
   338 	return true;
   339 }
   340 
   341 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
   342 {
   343 	DelayedFakeCliCmd *pFake;
   344 
   345 	if (m_FreeCmds.empty())
   346 	{
   347 		pFake = new DelayedFakeCliCmd;
   348 	} else {
   349 		pFake = m_FreeCmds.front();
   350 		m_FreeCmds.pop();
   351 	}
   352 
   353 	pFake->client = client;
   354 	pFake->userid = userid;
   355 	pFake->cmd.assign(cmd);
   356 
   357 	m_CmdQueue.push(pFake);
   358 }
   359 
   360 void CHalfLife2::ProcessFakeCliCmdQueue()
   361 {
   362 	while (!m_CmdQueue.empty())
   363 	{
   364 		DelayedFakeCliCmd *pFake = m_CmdQueue.first();
   365 
   366 		if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
   367 		{
   368 			CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
   369 			serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
   370 		}
   371 
   372 		m_CmdQueue.pop();
   373 	}
   374 }
   375 
   376 bool CHalfLife2::IsLANServer()
   377 {
   378 	sv_lan = icvar->FindVar("sv_lan");
   379 
   380 	if (!sv_lan)
   381 	{
   382 		return false;
   383 	}
   384 
   385 	return (sv_lan->GetInt() != 0);
   386 }
   387 
   388 void CHalfLife2::PushCommandStack(const CCommand *cmd)
   389 {
   390 	CachedCommandInfo info;
   391 
   392 	info.args = cmd;
   393 #if !defined ORANGEBOX_BUILD
   394 	strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
   395 #endif
   396 
   397 	m_CommandStack.push(info);
   398 }
   399 
   400 const CCommand *CHalfLife2::PeekCommandStack()
   401 {
   402 	if (m_CommandStack.empty())
   403 	{
   404 		return NULL;
   405 	}
   406 
   407 	return m_CommandStack.front().args;
   408 }
   409 
   410 void CHalfLife2::PopCommandStack()
   411 {
   412 	m_CommandStack.pop();
   413 }
   414 
   415 const char *CHalfLife2::CurrentCommandName()
   416 {
   417 #if defined ORANGEBOX_BUILD
   418 	return m_CommandStack.front().args->Arg(0);
   419 #else
   420 	return m_CommandStack.front().cmd;
   421 #endif
   422 }