core/HalfLife2.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Nov 30 13:09:30 2010 -0500 (2010-11-30)
changeset 3162 d8f90b56529f
parent 3099 1b7bf8509625
child 3221 ca0e525dcf86
permissions -rw-r--r--
Fixed BanClient always banning by IP address (bug 4626, r=fyren).
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod
     5  * Copyright (C) 2004-2008 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 <IGameConfigs.h>
    39 #include <compat_wrappers.h>
    40 #include <Logger.h>
    41 #include "logic_bridge.h"
    42 
    43 CHalfLife2 g_HL2;
    44 ConVar *sv_lan = NULL;
    45 
    46 static void *g_EntList = NULL;
    47 static int entInfoOffset = -1;
    48 
    49 namespace SourceHook
    50 {
    51 	template<>
    52 	int HashFunction<datamap_t *>(datamap_t * const &k)
    53 	{
    54 		return reinterpret_cast<int>(k);
    55 	}
    56 
    57 	template<>
    58 	int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
    59 	{
    60 		return (k1-k2);
    61 	}
    62 }
    63 
    64 CHalfLife2::CHalfLife2()
    65 {
    66 	m_pClasses = sm_trie_create();
    67 }
    68 
    69 CHalfLife2::~CHalfLife2()
    70 {
    71 	sm_trie_destroy(m_pClasses);
    72 
    73 	List<DataTableInfo *>::iterator iter;
    74 	DataTableInfo *pInfo;
    75 	for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
    76 	{
    77 		pInfo = (*iter);
    78 		delete pInfo;
    79 	}
    80 
    81 	m_Tables.clear();
    82 
    83 	THash<datamap_t *, DataMapTrie>::iterator h_iter;
    84 	for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
    85 	{
    86 		if (h_iter->val.trie)
    87 		{
    88 			sm_trie_destroy(h_iter->val.trie);
    89 			h_iter->val.trie = NULL;
    90 		}
    91 	}
    92 
    93 	m_Maps.clear();
    94 }
    95 
    96 #if SOURCE_ENGINE != SE_DARKMESSIAH
    97 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
    98 #endif
    99 
   100 #if !defined METAMOD_PLAPI_VERSION || PLAPI_VERSION < 11
   101 bool is_original_engine = false;
   102 #endif
   103 
   104 void CHalfLife2::OnSourceModStartup(bool late)
   105 {
   106 #if SOURCE_ENGINE != SE_DARKMESSIAH
   107 
   108 	/* The Ship currently is the only known game to use an older version of the engine */
   109 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
   110 	if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
   111 #else
   112 	if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
   113 #endif
   114 	{
   115 		is_original_engine = true;
   116 	}
   117 	else if (g_pSharedChangeInfo == NULL)
   118 	{
   119 		g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
   120 	}
   121 #endif
   122 }
   123 
   124 void CHalfLife2::OnSourceModAllInitialized()
   125 {
   126 	m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
   127 	m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
   128 	m_SayTextMsg = g_UserMsgs.GetMessageIndex("SayText");
   129 	m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
   130 	g_ShareSys.AddInterface(NULL, this);
   131 }
   132 
   133 void CHalfLife2::OnSourceModAllInitialized_Post()
   134 {
   135 	char *addr = NULL;
   136 #ifdef PLATFORM_WINDOWS
   137 	int offset;
   138 
   139 	/* gEntList and/or g_pEntityList */
   140 	if (!g_pGameConf->GetMemSig("LevelShutdown", (void **)&addr))
   141 	{
   142 		g_Logger.LogError("Logical Entities not supported by this mod (LevelShutdown) - Reverting to networkable entities only");
   143 		return;
   144 	}
   145 	if (!addr)
   146 	{
   147 		g_Logger.LogError("Failed lookup of LevelShutdown - Reverting to networkable entities only");
   148 		return;
   149 	}
   150 	if (!g_pGameConf->GetOffset("gEntList", &offset))
   151 	{
   152 		g_Logger.LogError("Logical Entities not supported by this mod (gEntList) - Reverting to networkable entities only");
   153 		return;
   154 	}
   155 	g_EntList = *reinterpret_cast<void **>(addr + offset);
   156 #elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
   157 	/* gEntList and/or g_pEntityList */
   158 	if (!g_pGameConf->GetMemSig("gEntList", (void **)&addr))
   159 	{
   160 		g_Logger.LogError("Logical Entities not supported by this mod (gEntList) - Reverting to networkable entities only");
   161 		return;
   162 	}
   163 	if (!addr)
   164 	{
   165 		g_Logger.LogError("Failed lookup of gEntList - Reverting to networkable entities only");
   166 		return;
   167 	}
   168 	g_EntList = reinterpret_cast<void *>(addr);
   169 #endif
   170 
   171 	if (!g_pGameConf->GetOffset("EntInfo", &entInfoOffset))
   172 	{
   173 		g_Logger.LogError("Logical Entities not supported by this mod (EntInfo) - Reverting to networkable entities only");
   174 		return;
   175 	}
   176 }
   177 
   178 #if !defined METAMOD_PLAPI_VERSION || PLAPI_VERSION < 11
   179 bool CHalfLife2::IsOriginalEngine()
   180 {
   181 	return is_original_engine;
   182 }
   183 #endif
   184 
   185 #if SOURCE_ENGINE != SE_DARKMESSIAH
   186 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
   187 {
   188 	return engine->GetChangeAccessor( (const edict_t *)this );
   189 }
   190 #endif
   191 
   192 bool UTIL_FindInSendTable(SendTable *pTable, 
   193 						  const char *name,
   194 						  sm_sendprop_info_t *info,
   195 						  unsigned int offset)
   196 {
   197 	const char *pname;
   198 	int props = pTable->GetNumProps();
   199 	SendProp *prop;
   200 
   201 	for (int i=0; i<props; i++)
   202 	{
   203 		prop = pTable->GetProp(i);
   204 		pname = prop->GetName();
   205 		if (pname && strcmp(name, pname) == 0)
   206 		{
   207 			info->prop = prop;
   208 			info->actual_offset = offset + info->prop->GetOffset();
   209 			return true;
   210 		}
   211 		if (prop->GetDataTable())
   212 		{
   213 			if (UTIL_FindInSendTable(prop->GetDataTable(), 
   214 				name,
   215 				info,
   216 				offset + prop->GetOffset())
   217 				)
   218 			{
   219 				return true;
   220 			}
   221 		}
   222 	}
   223 
   224 	return false;
   225 }
   226 
   227 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
   228 {
   229 	while (pMap)
   230 	{
   231 		for (int i=0; i<pMap->dataNumFields; i++)
   232 		{
   233 			if (pMap->dataDesc[i].fieldName == NULL)
   234 			{
   235 				continue;
   236 			}
   237 			if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
   238 			{
   239 				return &(pMap->dataDesc[i]);
   240 			}
   241 			if (pMap->dataDesc[i].td)
   242 			{
   243 				typedescription_t *_td;
   244 				if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
   245 				{
   246 					return _td;
   247 				}
   248 			}
   249 		}
   250 		pMap = pMap->baseMap;
   251 	}
   252 
   253 	return NULL; 
   254 }
   255 
   256 ServerClass *CHalfLife2::FindServerClass(const char *classname)
   257 {
   258 	DataTableInfo *pInfo = _FindServerClass(classname);
   259 
   260 	if (!pInfo)
   261 	{
   262 		return NULL;
   263 	}
   264 
   265 	return pInfo->sc;
   266 }
   267 
   268 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
   269 {
   270 	DataTableInfo *pInfo = NULL;
   271 
   272 	if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
   273 	{
   274 		ServerClass *sc = gamedll->GetAllServerClasses();
   275 		while (sc)
   276 		{
   277 			if (strcmp(classname, sc->GetName()) == 0)
   278 			{
   279 				pInfo = new DataTableInfo;
   280 				pInfo->sc = sc;
   281 				sm_trie_insert(m_pClasses, classname, pInfo);
   282 				m_Tables.push_back(pInfo);
   283 				break;
   284 			}
   285 			sc = sc->m_pNext;
   286 		}
   287 		if (!pInfo)
   288 		{
   289 			return NULL;
   290 		}
   291 	}
   292 
   293 	return pInfo;
   294 }
   295 
   296 bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info)
   297 {
   298 	DataTableInfo *pInfo;
   299 	sm_sendprop_info_t *prop;
   300 
   301 	if ((pInfo = _FindServerClass(classname)) == NULL)
   302 	{
   303 		return false;
   304 	}
   305 
   306 	if ((prop = pInfo->lookup.retrieve(offset)) == NULL)
   307 	{
   308 		sm_sendprop_info_t temp_info;
   309 
   310 		if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
   311 		{
   312 			return false;
   313 		}
   314 
   315 		pInfo->lookup.insert(offset, temp_info);
   316 		*info = temp_info;
   317 	}
   318 	else
   319 	{
   320 		*info = *prop;
   321 	}
   322 	
   323 	return true;
   324 }
   325 
   326 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
   327 {
   328 	sm_sendprop_info_t info;
   329 
   330 	if (!FindSendPropInfo(classname, offset, &info))
   331 	{
   332 		return NULL;
   333 	}
   334 
   335 	return info.prop;
   336 }
   337 
   338 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
   339 {
   340 	typedescription_t *td = NULL;
   341 	DataMapTrie &val = m_Maps[pMap];
   342 
   343 	if (!val.trie)
   344 	{
   345 		val.trie = sm_trie_create();
   346 	}
   347 	if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
   348 	{
   349 		if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
   350 		{
   351 			sm_trie_insert(val.trie, offset, td);
   352 		}
   353 	}
   354 
   355 	return td;
   356 }
   357 
   358 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
   359 {
   360 #if SOURCE_ENGINE != SE_DARKMESSIAH
   361 	if (g_pSharedChangeInfo != NULL)
   362 	{
   363 		if (offset)
   364 		{
   365 			pEdict->StateChanged(offset);
   366 		}
   367 		else
   368 		{
   369 			pEdict->StateChanged();
   370 		}
   371 	}
   372 	else
   373 #endif
   374 	{
   375 		pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
   376 	}
   377 }
   378 
   379 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
   380 {
   381 	bf_write *pBitBuf = NULL;
   382 	cell_t players[] = {client};
   383 
   384 	if (dest == HUD_PRINTTALK)
   385 	{
   386 		const char *chat_saytext = g_pGameConf->GetKeyValue("ChatSayText");
   387 
   388 		/* Use SayText user message instead */
   389 		if (chat_saytext != NULL && strcmp(chat_saytext, "yes") == 0)
   390 		{
   391 			char buffer[192];
   392 			UTIL_Format(buffer, sizeof(buffer), "%s\1\n", msg);
   393 
   394 			if ((pBitBuf = g_UserMsgs.StartMessage(m_SayTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   395 			{
   396 				return false;
   397 			}
   398 
   399 			pBitBuf->WriteByte(0);
   400 			pBitBuf->WriteString(buffer);
   401 			pBitBuf->WriteByte(1);
   402 
   403 			g_UserMsgs.EndMessage();
   404 
   405 			return true;
   406 		}
   407 	}
   408 
   409 	if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   410 	{
   411 		return false;
   412 	}
   413 
   414 	pBitBuf->WriteByte(dest);
   415 	pBitBuf->WriteString(msg);
   416 
   417 	g_UserMsgs.EndMessage();
   418 
   419 	return true;
   420 }
   421 
   422 bool CHalfLife2::HintTextMsg(int client, const char *msg)
   423 {
   424 	bf_write *pBitBuf = NULL;
   425 	cell_t players[] = {client};
   426 
   427 	if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   428 	{
   429 		return false;
   430 	}
   431 
   432 	const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
   433 	if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
   434 	{
   435 		pBitBuf->WriteByte(1);
   436 	}
   437 	pBitBuf->WriteString(msg);
   438 	g_UserMsgs.EndMessage();
   439 
   440 	return true;
   441 }
   442 
   443 bool CHalfLife2::HintTextMsg(cell_t *players, int count, const char *msg)
   444 {
   445 	bf_write *pBitBuf = NULL;
   446 
   447 	if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, count, USERMSG_RELIABLE)) == NULL)
   448 	{
   449 		return false;
   450 	}
   451 
   452 	const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
   453 	if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
   454 	{
   455 		pBitBuf->WriteByte(1);
   456 	}
   457 	pBitBuf->WriteString(msg);
   458 	g_UserMsgs.EndMessage();
   459 
   460 	return true;
   461 }
   462 
   463 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
   464 {
   465 	bf_write *pBitBuf = NULL;
   466 	KeyValues *SubKey = NULL;
   467 	int count = 0;
   468 	cell_t players[] = {client};
   469 
   470 	if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
   471 	{
   472 		return false;
   473 	}
   474 
   475 	if (data)
   476 	{
   477 		SubKey = data->GetFirstSubKey();
   478 		while (SubKey)
   479 		{
   480 			count++;
   481 			SubKey = SubKey->GetNextKey();
   482 		}
   483 		SubKey = data->GetFirstSubKey();
   484 	}
   485 
   486 	pBitBuf->WriteString(name);
   487 	pBitBuf->WriteByte((show) ? 1 : 0);
   488 	pBitBuf->WriteByte(count);
   489 	while (SubKey)
   490 	{
   491 		pBitBuf->WriteString(SubKey->GetName());
   492 		pBitBuf->WriteString(SubKey->GetString());
   493 		SubKey = SubKey->GetNextKey();
   494 	}
   495 	g_UserMsgs.EndMessage();
   496 
   497 	return true;
   498 }
   499 
   500 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
   501 {
   502 	DelayedFakeCliCmd *pFake;
   503 
   504 	if (m_FreeCmds.empty())
   505 	{
   506 		pFake = new DelayedFakeCliCmd;
   507 	} else {
   508 		pFake = m_FreeCmds.front();
   509 		m_FreeCmds.pop();
   510 	}
   511 
   512 	pFake->client = client;
   513 	pFake->userid = userid;
   514 	pFake->cmd.assign(cmd);
   515 
   516 	m_CmdQueue.push(pFake);
   517 }
   518 
   519 void CHalfLife2::ProcessFakeCliCmdQueue()
   520 {
   521 	while (!m_CmdQueue.empty())
   522 	{
   523 		DelayedFakeCliCmd *pFake = m_CmdQueue.first();
   524 
   525 		if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
   526 		{
   527 			CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
   528 			serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
   529 		}
   530 
   531 		m_CmdQueue.pop();
   532 	}
   533 }
   534 
   535 bool CHalfLife2::IsLANServer()
   536 {
   537 	sv_lan = icvar->FindVar("sv_lan");
   538 
   539 	if (!sv_lan)
   540 	{
   541 		return false;
   542 	}
   543 
   544 	return (sv_lan->GetInt() != 0);
   545 }
   546 
   547 bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
   548 {
   549 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
   550 	if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
   551 #else
   552 	if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
   553 #endif
   554 	{
   555 		Assert(filesystem);
   556 #ifdef _MSC_VER
   557 		Assert(_heapchk() == _HEAPOK);
   558 #endif
   559 
   560 		FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
   561 		if (!f)
   562 			return false;
   563 
   564 		// load file into a null-terminated buffer
   565 		int fileSize = filesystem->Size(f);
   566 		char *buffer = (char *)MemAllocScratch(fileSize + 1);
   567 
   568 		Assert(buffer);
   569 
   570 		filesystem->Read(buffer, fileSize, f); // read into local buffer
   571 
   572 		buffer[fileSize] = 0; // null terminate file as EOF
   573 
   574 		filesystem->Close( f );	// close file after reading
   575 
   576 		bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
   577 
   578 		MemFreeScratch();
   579 
   580 		return retOK;
   581 	}
   582 	else
   583 	{
   584 		return kv->LoadFromFile(filesystem, resourceName, pathID);
   585 	}
   586 }
   587 
   588 void CHalfLife2::PushCommandStack(const CCommand *cmd)
   589 {
   590 	CachedCommandInfo info;
   591 
   592 	info.args = cmd;
   593 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   594 	strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
   595 #endif
   596 
   597 	m_CommandStack.push(info);
   598 }
   599 
   600 const CCommand *CHalfLife2::PeekCommandStack()
   601 {
   602 	if (m_CommandStack.empty())
   603 	{
   604 		return NULL;
   605 	}
   606 
   607 	return m_CommandStack.front().args;
   608 }
   609 
   610 void CHalfLife2::PopCommandStack()
   611 {
   612 	m_CommandStack.pop();
   613 }
   614 
   615 const char *CHalfLife2::CurrentCommandName()
   616 {
   617 #if SOURCE_ENGINE >= SE_ORANGEBOX
   618 	return m_CommandStack.front().args->Arg(0);
   619 #else
   620 	return m_CommandStack.front().cmd;
   621 #endif
   622 }
   623 
   624 void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
   625 {
   626 	DelayedKickInfo kick;
   627 
   628 	kick.client = client;
   629 	kick.userid = userid;
   630 	UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
   631 
   632 	m_DelayedKicks.push(kick);
   633 }
   634 
   635 void CHalfLife2::ProcessDelayedKicks()
   636 {
   637 	while (!m_DelayedKicks.empty())
   638 	{
   639 		DelayedKickInfo info = m_DelayedKicks.first();
   640 		m_DelayedKicks.pop();
   641 
   642 		CPlayer *player = g_Players.GetPlayerByIndex(info.client);
   643 		if (player == NULL || player->GetUserId() != info.userid)
   644 		{
   645 			continue;
   646 		}
   647 
   648 		player->Kick(info.buffer);
   649 	}
   650 }
   651 
   652 edict_t *CHalfLife2::EdictOfIndex(int index)
   653 {
   654 	return ::PEntityOfEntIndex(index);
   655 }
   656 
   657 int CHalfLife2::IndexOfEdict(edict_t *pEnt)
   658 {
   659 	return ::IndexOfEdict(pEnt);
   660 }
   661 
   662 edict_t *CHalfLife2::GetHandleEntity(CBaseHandle &hndl)
   663 {
   664 	if (!hndl.IsValid())
   665 	{
   666 		return NULL;
   667 	}
   668 
   669 	int index = hndl.GetEntryIndex();
   670 
   671 	edict_t *pStoredEdict;
   672 	CBaseEntity *pStoredEntity;
   673 
   674 	if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
   675 	{
   676 		return NULL;
   677 	}
   678 
   679 	if (pStoredEdict == NULL || pStoredEntity == NULL)
   680 	{
   681 		return NULL;
   682 	}
   683 
   684 	IServerEntity *pSE = pStoredEdict->GetIServerEntity();
   685 
   686 	if (pSE == NULL)
   687 	{
   688 		return NULL;
   689 	}
   690 
   691 	if (pSE->GetRefEHandle() != hndl)
   692 	{
   693 		return NULL;
   694 	}
   695 
   696 	return pStoredEdict;
   697 }
   698 
   699 void CHalfLife2::SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt)
   700 {
   701 	IServerEntity *pEntOther = pEnt->GetIServerEntity();
   702 
   703 	if (pEntOther == NULL)
   704 	{
   705 		return;
   706 	}
   707 
   708 	hndl.Set(pEntOther);
   709 }
   710 
   711 const char *CHalfLife2::GetCurrentMap()
   712 {
   713 	return STRING(gpGlobals->mapname);
   714 }
   715 
   716 void CHalfLife2::ServerCommand(const char *buffer)
   717 {
   718 	engine->ServerCommand(buffer);
   719 }
   720 
   721 cell_t CHalfLife2::EntityToReference(CBaseEntity *pEntity)
   722 {
   723 	IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
   724 	CBaseHandle hndl = pUnknown->GetRefEHandle();
   725 	return (hndl.ToInt() | (1<<31));
   726 }
   727 
   728 CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef)
   729 {
   730 	CEntInfo *pInfo = NULL;
   731 
   732 	if (entRef & (1<<31))
   733 	{
   734 		/* Proper ent reference */
   735 		int hndlValue = entRef & ~(1<<31);
   736 		CBaseHandle hndl(hndlValue);
   737 
   738 		pInfo = LookupEntity(hndl.GetEntryIndex());
   739 		if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
   740 		{
   741 			return NULL;
   742 		}
   743 	}
   744 	else
   745 	{
   746 		/* Old style index only */
   747 		pInfo = LookupEntity(entRef);
   748 	}
   749 
   750 	if (!pInfo)
   751 	{
   752 		return NULL;
   753 	}
   754 
   755 	IServerUnknown *pUnk = static_cast<IServerUnknown *>(pInfo->m_pEntity);
   756 	if (pUnk)
   757 	{
   758 		return pUnk->GetBaseEntity();
   759 	}
   760 
   761 	return NULL;
   762 }
   763 
   764 /**
   765  * Retrieves the CEntInfo pointer from g_EntList for a given entity index
   766  */
   767 CEntInfo *CHalfLife2::LookupEntity(int entIndex)
   768 {
   769 	if (!g_EntList || entInfoOffset == -1)
   770 	{
   771 		/* Attempt to use engine interface instead */
   772 		static CEntInfo tempInfo;
   773 		tempInfo.m_pNext = NULL;
   774 		tempInfo.m_pPrev = NULL;
   775 
   776 		edict_t *pEdict = PEntityOfEntIndex(entIndex);
   777 
   778 		if (!pEdict)
   779 		{
   780 			return NULL;
   781 		}
   782 
   783 		IServerUnknown *pUnk = pEdict->GetUnknown();
   784 
   785 		if (!pUnk)
   786 		{
   787 			return NULL;
   788 		}
   789 
   790 		tempInfo.m_pEntity = pUnk;
   791 		tempInfo.m_SerialNumber = pUnk->GetRefEHandle().GetSerialNumber();
   792 
   793 		return &tempInfo;
   794 	}
   795 
   796 	CEntInfo *pArray = (CEntInfo *)(((unsigned char *)g_EntList) + entInfoOffset);
   797 	return &pArray[entIndex];
   798 }
   799 
   800 /**
   801 	SERIAL_MASK = 0x7fff (15 bits)
   802 
   803 	#define	MAX_EDICT_BITS				11
   804 	#define NUM_ENT_ENTRY_BITS		(MAX_EDICT_BITS + 1)
   805 	m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS); 
   806 	
   807 	Top 5 bits of a handle are unused.
   808 	Bit 31 - Our 'reference' flag indicator
   809 */
   810 
   811 cell_t CHalfLife2::IndexToReference(int entIndex)
   812 {
   813 	CBaseEntity *pEnt = ReferenceToEntity(entIndex);
   814 	if (!pEnt)
   815 	{
   816 		return INVALID_EHANDLE_INDEX;
   817 	}
   818 
   819 	return EntityToReference(pEnt);
   820 }
   821 
   822 int CHalfLife2::ReferenceToIndex(cell_t entRef)
   823 {
   824 	if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
   825 	{
   826 		return INVALID_EHANDLE_INDEX;
   827 	}
   828 
   829 	if (entRef & (1<<31))
   830 	{
   831 		/* Proper ent reference */
   832 		int hndlValue = entRef & ~(1<<31);
   833 		CBaseHandle hndl(hndlValue);
   834 
   835 		CEntInfo *pInfo = LookupEntity(hndl.GetEntryIndex());
   836 
   837 		if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
   838 		{
   839 			return INVALID_EHANDLE_INDEX;
   840 		}
   841 
   842 		return hndl.GetEntryIndex();
   843 	}
   844 
   845 	return entRef;
   846 }
   847 
   848 cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity)
   849 {
   850 	if (pEntity == NULL)
   851 	{
   852 		return INVALID_EHANDLE_INDEX;
   853 	}
   854 
   855 	IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
   856 	CBaseHandle hndl = pUnknown->GetRefEHandle();
   857 
   858 	if (hndl.GetEntryIndex() >= MAX_EDICTS)
   859 	{
   860 		return (hndl.ToInt() | (1<<31));
   861 	}
   862 	else
   863 	{
   864 		return hndl.GetEntryIndex();
   865 	}
   866 }
   867 
   868 cell_t CHalfLife2::ReferenceToBCompatRef(cell_t entRef)
   869 {
   870 	if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
   871 	{
   872 		return INVALID_EHANDLE_INDEX;
   873 	}
   874 
   875 	int hndlValue = entRef & ~(1<<31);
   876 	CBaseHandle hndl(hndlValue);
   877 
   878 	if (hndl.GetEntryIndex() < MAX_EDICTS)
   879 	{
   880 		return hndl.GetEntryIndex();
   881 	}
   882 
   883 	return entRef;
   884 }
   885 
   886 void *CHalfLife2::GetGlobalEntityList()
   887 {
   888 	return g_EntList;
   889 }
   890 
   891 int CHalfLife2::GetSendPropOffset(SendProp *prop)
   892 {
   893 	return prop->GetOffset();
   894 }
   895