core/HalfLife2.cpp
author Nicholas Hastings <skamonkey@gmail.com>
Tue Jun 22 19:26:07 2010 -0400 (2010-06-22)
changeset 3043 c4c06ddb3fc0
parent 3027 0f90dd9754ca
child 3099 1b7bf8509625
permissions -rw-r--r--
Updated TF2 'Burn' signature (bug 4463, 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 SOURCE_ENGINE != SE_LEFT4DEAD2
   364 		if (offset)
   365 		{
   366 			pEdict->StateChanged(offset);
   367 		}
   368 		else
   369 #endif
   370 		{
   371 			pEdict->StateChanged();
   372 		}
   373 	}
   374 	else
   375 #endif
   376 	{
   377 		pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
   378 	}
   379 }
   380 
   381 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
   382 {
   383 	bf_write *pBitBuf = NULL;
   384 	cell_t players[] = {client};
   385 
   386 	if (dest == HUD_PRINTTALK)
   387 	{
   388 		const char *chat_saytext = g_pGameConf->GetKeyValue("ChatSayText");
   389 
   390 		/* Use SayText user message instead */
   391 		if (chat_saytext != NULL && strcmp(chat_saytext, "yes") == 0)
   392 		{
   393 			char buffer[192];
   394 			UTIL_Format(buffer, sizeof(buffer), "%s\1\n", msg);
   395 
   396 			if ((pBitBuf = g_UserMsgs.StartMessage(m_SayTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   397 			{
   398 				return false;
   399 			}
   400 
   401 			pBitBuf->WriteByte(0);
   402 			pBitBuf->WriteString(buffer);
   403 			pBitBuf->WriteByte(1);
   404 
   405 			g_UserMsgs.EndMessage();
   406 
   407 			return true;
   408 		}
   409 	}
   410 
   411 	if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   412 	{
   413 		return false;
   414 	}
   415 
   416 	pBitBuf->WriteByte(dest);
   417 	pBitBuf->WriteString(msg);
   418 
   419 	g_UserMsgs.EndMessage();
   420 
   421 	return true;
   422 }
   423 
   424 bool CHalfLife2::HintTextMsg(int client, const char *msg)
   425 {
   426 	bf_write *pBitBuf = NULL;
   427 	cell_t players[] = {client};
   428 
   429 	if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
   430 	{
   431 		return false;
   432 	}
   433 
   434 	const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
   435 	if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
   436 	{
   437 		pBitBuf->WriteByte(1);
   438 	}
   439 	pBitBuf->WriteString(msg);
   440 	g_UserMsgs.EndMessage();
   441 
   442 	return true;
   443 }
   444 
   445 bool CHalfLife2::HintTextMsg(cell_t *players, int count, const char *msg)
   446 {
   447 	bf_write *pBitBuf = NULL;
   448 
   449 	if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, count, USERMSG_RELIABLE)) == NULL)
   450 	{
   451 		return false;
   452 	}
   453 
   454 	const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
   455 	if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
   456 	{
   457 		pBitBuf->WriteByte(1);
   458 	}
   459 	pBitBuf->WriteString(msg);
   460 	g_UserMsgs.EndMessage();
   461 
   462 	return true;
   463 }
   464 
   465 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
   466 {
   467 	bf_write *pBitBuf = NULL;
   468 	KeyValues *SubKey = NULL;
   469 	int count = 0;
   470 	cell_t players[] = {client};
   471 
   472 	if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
   473 	{
   474 		return false;
   475 	}
   476 
   477 	if (data)
   478 	{
   479 		SubKey = data->GetFirstSubKey();
   480 		while (SubKey)
   481 		{
   482 			count++;
   483 			SubKey = SubKey->GetNextKey();
   484 		}
   485 		SubKey = data->GetFirstSubKey();
   486 	}
   487 
   488 	pBitBuf->WriteString(name);
   489 	pBitBuf->WriteByte((show) ? 1 : 0);
   490 	pBitBuf->WriteByte(count);
   491 	while (SubKey)
   492 	{
   493 		pBitBuf->WriteString(SubKey->GetName());
   494 		pBitBuf->WriteString(SubKey->GetString());
   495 		SubKey = SubKey->GetNextKey();
   496 	}
   497 	g_UserMsgs.EndMessage();
   498 
   499 	return true;
   500 }
   501 
   502 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
   503 {
   504 	DelayedFakeCliCmd *pFake;
   505 
   506 	if (m_FreeCmds.empty())
   507 	{
   508 		pFake = new DelayedFakeCliCmd;
   509 	} else {
   510 		pFake = m_FreeCmds.front();
   511 		m_FreeCmds.pop();
   512 	}
   513 
   514 	pFake->client = client;
   515 	pFake->userid = userid;
   516 	pFake->cmd.assign(cmd);
   517 
   518 	m_CmdQueue.push(pFake);
   519 }
   520 
   521 void CHalfLife2::ProcessFakeCliCmdQueue()
   522 {
   523 	while (!m_CmdQueue.empty())
   524 	{
   525 		DelayedFakeCliCmd *pFake = m_CmdQueue.first();
   526 
   527 		if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
   528 		{
   529 			CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
   530 			serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
   531 		}
   532 
   533 		m_CmdQueue.pop();
   534 	}
   535 }
   536 
   537 bool CHalfLife2::IsLANServer()
   538 {
   539 	sv_lan = icvar->FindVar("sv_lan");
   540 
   541 	if (!sv_lan)
   542 	{
   543 		return false;
   544 	}
   545 
   546 	return (sv_lan->GetInt() != 0);
   547 }
   548 
   549 bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
   550 {
   551 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
   552 	if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
   553 #else
   554 	if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
   555 #endif
   556 	{
   557 		Assert(filesystem);
   558 #ifdef _MSC_VER
   559 		Assert(_heapchk() == _HEAPOK);
   560 #endif
   561 
   562 		FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
   563 		if (!f)
   564 			return false;
   565 
   566 		// load file into a null-terminated buffer
   567 		int fileSize = filesystem->Size(f);
   568 		char *buffer = (char *)MemAllocScratch(fileSize + 1);
   569 
   570 		Assert(buffer);
   571 
   572 		filesystem->Read(buffer, fileSize, f); // read into local buffer
   573 
   574 		buffer[fileSize] = 0; // null terminate file as EOF
   575 
   576 		filesystem->Close( f );	// close file after reading
   577 
   578 		bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
   579 
   580 		MemFreeScratch();
   581 
   582 		return retOK;
   583 	}
   584 	else
   585 	{
   586 		return kv->LoadFromFile(filesystem, resourceName, pathID);
   587 	}
   588 }
   589 
   590 void CHalfLife2::PushCommandStack(const CCommand *cmd)
   591 {
   592 	CachedCommandInfo info;
   593 
   594 	info.args = cmd;
   595 #if SOURCE_ENGINE <= SE_DARKMESSIAH
   596 	strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
   597 #endif
   598 
   599 	m_CommandStack.push(info);
   600 }
   601 
   602 const CCommand *CHalfLife2::PeekCommandStack()
   603 {
   604 	if (m_CommandStack.empty())
   605 	{
   606 		return NULL;
   607 	}
   608 
   609 	return m_CommandStack.front().args;
   610 }
   611 
   612 void CHalfLife2::PopCommandStack()
   613 {
   614 	m_CommandStack.pop();
   615 }
   616 
   617 const char *CHalfLife2::CurrentCommandName()
   618 {
   619 #if SOURCE_ENGINE >= SE_ORANGEBOX
   620 	return m_CommandStack.front().args->Arg(0);
   621 #else
   622 	return m_CommandStack.front().cmd;
   623 #endif
   624 }
   625 
   626 void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
   627 {
   628 	DelayedKickInfo kick;
   629 
   630 	kick.client = client;
   631 	kick.userid = userid;
   632 	UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
   633 
   634 	m_DelayedKicks.push(kick);
   635 }
   636 
   637 void CHalfLife2::ProcessDelayedKicks()
   638 {
   639 	while (!m_DelayedKicks.empty())
   640 	{
   641 		DelayedKickInfo info = m_DelayedKicks.first();
   642 		m_DelayedKicks.pop();
   643 
   644 		CPlayer *player = g_Players.GetPlayerByIndex(info.client);
   645 		if (player == NULL || player->GetUserId() != info.userid)
   646 		{
   647 			continue;
   648 		}
   649 
   650 		player->Kick(info.buffer);
   651 	}
   652 }
   653 
   654 edict_t *CHalfLife2::EdictOfIndex(int index)
   655 {
   656 	return ::PEntityOfEntIndex(index);
   657 }
   658 
   659 int CHalfLife2::IndexOfEdict(edict_t *pEnt)
   660 {
   661 	return ::IndexOfEdict(pEnt);
   662 }
   663 
   664 edict_t *CHalfLife2::GetHandleEntity(CBaseHandle &hndl)
   665 {
   666 	if (!hndl.IsValid())
   667 	{
   668 		return NULL;
   669 	}
   670 
   671 	int index = hndl.GetEntryIndex();
   672 
   673 	edict_t *pStoredEdict;
   674 	CBaseEntity *pStoredEntity;
   675 
   676 	if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
   677 	{
   678 		return NULL;
   679 	}
   680 
   681 	if (pStoredEdict == NULL || pStoredEntity == NULL)
   682 	{
   683 		return NULL;
   684 	}
   685 
   686 	IServerEntity *pSE = pStoredEdict->GetIServerEntity();
   687 
   688 	if (pSE == NULL)
   689 	{
   690 		return NULL;
   691 	}
   692 
   693 	if (pSE->GetRefEHandle() != hndl)
   694 	{
   695 		return NULL;
   696 	}
   697 
   698 	return pStoredEdict;
   699 }
   700 
   701 void CHalfLife2::SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt)
   702 {
   703 	IServerEntity *pEntOther = pEnt->GetIServerEntity();
   704 
   705 	if (pEntOther == NULL)
   706 	{
   707 		return;
   708 	}
   709 
   710 	hndl.Set(pEntOther);
   711 }
   712 
   713 const char *CHalfLife2::GetCurrentMap()
   714 {
   715 	return STRING(gpGlobals->mapname);
   716 }
   717 
   718 void CHalfLife2::ServerCommand(const char *buffer)
   719 {
   720 	engine->ServerCommand(buffer);
   721 }
   722 
   723 cell_t CHalfLife2::EntityToReference(CBaseEntity *pEntity)
   724 {
   725 	IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
   726 	CBaseHandle hndl = pUnknown->GetRefEHandle();
   727 	return (hndl.ToInt() | (1<<31));
   728 }
   729 
   730 CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef)
   731 {
   732 	CEntInfo *pInfo = NULL;
   733 
   734 	if (entRef & (1<<31))
   735 	{
   736 		/* Proper ent reference */
   737 		int hndlValue = entRef & ~(1<<31);
   738 		CBaseHandle hndl(hndlValue);
   739 
   740 		pInfo = LookupEntity(hndl.GetEntryIndex());
   741 		if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
   742 		{
   743 			return NULL;
   744 		}
   745 	}
   746 	else
   747 	{
   748 		/* Old style index only */
   749 		pInfo = LookupEntity(entRef);
   750 	}
   751 
   752 	if (!pInfo)
   753 	{
   754 		return NULL;
   755 	}
   756 
   757 	IServerUnknown *pUnk = static_cast<IServerUnknown *>(pInfo->m_pEntity);
   758 	if (pUnk)
   759 	{
   760 		return pUnk->GetBaseEntity();
   761 	}
   762 
   763 	return NULL;
   764 }
   765 
   766 /**
   767  * Retrieves the CEntInfo pointer from g_EntList for a given entity index
   768  */
   769 CEntInfo *CHalfLife2::LookupEntity(int entIndex)
   770 {
   771 	if (!g_EntList || entInfoOffset == -1)
   772 	{
   773 		/* Attempt to use engine interface instead */
   774 		static CEntInfo tempInfo;
   775 		tempInfo.m_pNext = NULL;
   776 		tempInfo.m_pPrev = NULL;
   777 
   778 		edict_t *pEdict = PEntityOfEntIndex(entIndex);
   779 
   780 		if (!pEdict)
   781 		{
   782 			return NULL;
   783 		}
   784 
   785 		IServerUnknown *pUnk = pEdict->GetUnknown();
   786 
   787 		if (!pUnk)
   788 		{
   789 			return NULL;
   790 		}
   791 
   792 		tempInfo.m_pEntity = pUnk;
   793 		tempInfo.m_SerialNumber = pUnk->GetRefEHandle().GetSerialNumber();
   794 
   795 		return &tempInfo;
   796 	}
   797 
   798 	CEntInfo *pArray = (CEntInfo *)(((unsigned char *)g_EntList) + entInfoOffset);
   799 	return &pArray[entIndex];
   800 }
   801 
   802 /**
   803 	SERIAL_MASK = 0x7fff (15 bits)
   804 
   805 	#define	MAX_EDICT_BITS				11
   806 	#define NUM_ENT_ENTRY_BITS		(MAX_EDICT_BITS + 1)
   807 	m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS); 
   808 	
   809 	Top 5 bits of a handle are unused.
   810 	Bit 31 - Our 'reference' flag indicator
   811 */
   812 
   813 cell_t CHalfLife2::IndexToReference(int entIndex)
   814 {
   815 	CBaseEntity *pEnt = ReferenceToEntity(entIndex);
   816 	if (!pEnt)
   817 	{
   818 		return INVALID_EHANDLE_INDEX;
   819 	}
   820 
   821 	return EntityToReference(pEnt);
   822 }
   823 
   824 int CHalfLife2::ReferenceToIndex(cell_t entRef)
   825 {
   826 	if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
   827 	{
   828 		return INVALID_EHANDLE_INDEX;
   829 	}
   830 
   831 	if (entRef & (1<<31))
   832 	{
   833 		/* Proper ent reference */
   834 		int hndlValue = entRef & ~(1<<31);
   835 		CBaseHandle hndl(hndlValue);
   836 
   837 		CEntInfo *pInfo = LookupEntity(hndl.GetEntryIndex());
   838 
   839 		if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
   840 		{
   841 			return INVALID_EHANDLE_INDEX;
   842 		}
   843 
   844 		return hndl.GetEntryIndex();
   845 	}
   846 
   847 	return entRef;
   848 }
   849 
   850 cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity)
   851 {
   852 	if (pEntity == NULL)
   853 	{
   854 		return INVALID_EHANDLE_INDEX;
   855 	}
   856 
   857 	IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
   858 	CBaseHandle hndl = pUnknown->GetRefEHandle();
   859 
   860 	if (hndl.GetEntryIndex() >= MAX_EDICTS)
   861 	{
   862 		return (hndl.ToInt() | (1<<31));
   863 	}
   864 	else
   865 	{
   866 		return hndl.GetEntryIndex();
   867 	}
   868 }
   869 
   870 cell_t CHalfLife2::ReferenceToBCompatRef(cell_t entRef)
   871 {
   872 	if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
   873 	{
   874 		return INVALID_EHANDLE_INDEX;
   875 	}
   876 
   877 	int hndlValue = entRef & ~(1<<31);
   878 	CBaseHandle hndl(hndlValue);
   879 
   880 	if (hndl.GetEntryIndex() < MAX_EDICTS)
   881 	{
   882 		return hndl.GetEntryIndex();
   883 	}
   884 
   885 	return entRef;
   886 }
   887 
   888 void *CHalfLife2::GetGlobalEntityList()
   889 {
   890 	return g_EntList;
   891 }
   892 
   893 int CHalfLife2::GetSendPropOffset(SendProp *prop)
   894 {
   895 	return prop->GetOffset();
   896 }
   897