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