3 * =============================================================================
5 * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
6 * =============================================================================
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.
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
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/>.
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>.
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>
41 #include "LibrarySys.h"
42 #include "logic_bridge.h"
43 #include <tier0/mem.h>
45 #if SOURCE_ENGINE == SE_CSGO
46 #include <cstrike15_usermessages.pb.h>
50 typedef ICommandLine *(*FakeGetCommandLine)();
53 #define TIER0_NAME "tier0.dll"
54 #define VSTDLIB_NAME "vstdlib.dll"
55 #elif defined __APPLE__
56 #define TIER0_NAME "libtier0.dylib"
57 #define VSTDLIB_NAME "libvstdlib.dylib"
58 #elif defined __linux__
59 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
60 #define TIER0_NAME "libtier0_srv.so"
61 #define VSTDLIB_NAME "libvstdlib_srv.so"
62 #elif SOURCE_ENGINE >= SE_CSS
63 #define TIER0_NAME "libtier0.so"
64 #define VSTDLIB_NAME "libvstdlib.so"
66 #define TIER0_NAME "tier0_i486.so"
67 #define VSTDLIB_NAME "vstdlib_i486.so"
72 ConVar *sv_lan = NULL;
74 static void *g_EntList = NULL;
75 static int entInfoOffset = -1;
80 int HashFunction<datamap_t *>(datamap_t * const &k)
82 return reinterpret_cast<int>(k);
86 int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
92 CHalfLife2::CHalfLife2()
94 m_pClasses = sm_trie_create();
97 CHalfLife2::~CHalfLife2()
99 sm_trie_destroy(m_pClasses);
101 List<DataTableInfo *>::iterator iter;
102 DataTableInfo *pInfo;
103 for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
111 THash<datamap_t *, DataMapTrie>::iterator h_iter;
112 for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
114 if (h_iter->val.trie)
116 sm_trie_destroy(h_iter->val.trie);
117 h_iter->val.trie = NULL;
124 #if SOURCE_ENGINE != SE_DARKMESSIAH
125 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
128 #if !defined METAMOD_PLAPI_VERSION || PLAPI_VERSION < 11
129 bool is_original_engine = false;
132 void CHalfLife2::OnSourceModStartup(bool late)
134 #if SOURCE_ENGINE != SE_DARKMESSIAH
136 /* The Ship currently is the only known game to use an older version of the engine */
137 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
138 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
140 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
143 is_original_engine = true;
145 else if (g_pSharedChangeInfo == NULL)
147 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
152 void CHalfLife2::OnSourceModAllInitialized()
154 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
155 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
156 m_SayTextMsg = g_UserMsgs.GetMessageIndex("SayText");
157 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
158 g_ShareSys.AddInterface(NULL, this);
161 void CHalfLife2::OnSourceModAllInitialized_Post()
163 InitLogicalEntData();
167 void CHalfLife2::InitLogicalEntData()
172 * gEntList and/or g_pEntityList
174 * First try to lookup pointer directly for platforms with symbols.
175 * If symbols aren't present (Windows or stripped Linux/Mac),
176 * attempt find via LevelShutdown + offset
178 if (g_pGameConf->GetMemSig("gEntList", (void **)&addr))
180 #if !defined PLATFORM_WINDOWS
183 // Key exists so notify if lookup fails, but try other method.
184 g_Logger.LogError("Failed lookup of gEntList directly - Reverting to lookup via LevelShutdown");
189 g_EntList = reinterpret_cast<void *>(addr);
190 #if !defined PLATFORM_WINDOWS
198 if (!g_pGameConf->GetMemSig("LevelShutdown", (void **)&addr))
200 g_Logger.LogError("Logical Entities not supported by this mod (LevelShutdown) - Reverting to networkable entities only");
206 g_Logger.LogError("Failed lookup of LevelShutdown - Reverting to networkable entities only");
211 if (!g_pGameConf->GetOffset("gEntList", &offset))
213 g_Logger.LogError("Logical Entities not supported by this mod (gEntList) - Reverting to networkable entities only");
217 g_EntList = *reinterpret_cast<void **>(addr + offset);
222 g_Logger.LogError("Failed lookup of gEntList - Reverting to networkable entities only");
226 if (!g_pGameConf->GetOffset("EntInfo", &entInfoOffset))
228 g_Logger.LogError("Logical Entities not supported by this mod (EntInfo) - Reverting to networkable entities only");
233 void CHalfLife2::InitCommandLine()
235 char path[PLATFORM_MAX_PATH];
238 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "../bin/" TIER0_NAME);
240 if (!g_LibSys.IsPathFile(path))
242 g_Logger.LogError("Could not find path for: " TIER0_NAME);
246 ILibrary *lib = g_LibSys.OpenLibrary(path, error, sizeof(error));
247 m_pGetCommandLine = lib->GetSymbolAddress("CommandLine_Tier0");
249 /* '_Tier0' dropped on Alien Swarm version */
250 if (m_pGetCommandLine == NULL)
252 m_pGetCommandLine = lib->GetSymbolAddress("CommandLine");
255 if (m_pGetCommandLine == NULL)
257 /* We probably have a Ship engine. */
259 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "../bin/" VSTDLIB_NAME);
260 if (!g_LibSys.IsPathFile(path))
262 g_Logger.LogError("Could not find path for: " VSTDLIB_NAME);
266 if ((lib = g_LibSys.OpenLibrary(path, error, sizeof(error))) == NULL)
268 g_Logger.LogError("Could not load %s: %s", path, error);
272 m_pGetCommandLine = lib->GetSymbolAddress("CommandLine");
274 if (m_pGetCommandLine == NULL)
276 g_Logger.LogError("Could not locate any command line functionality");
283 ICommandLine *CHalfLife2::GetValveCommandLine()
285 if (!m_pGetCommandLine)
288 return ((FakeGetCommandLine)((FakeGetCommandLine *)m_pGetCommandLine))();
291 #if !defined METAMOD_PLAPI_VERSION || PLAPI_VERSION < 11
292 bool CHalfLife2::IsOriginalEngine()
294 return is_original_engine;
298 #if SOURCE_ENGINE != SE_DARKMESSIAH
299 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
301 return engine->GetChangeAccessor( (const edict_t *)this );
305 bool UTIL_FindInSendTable(SendTable *pTable,
307 sm_sendprop_info_t *info,
311 int props = pTable->GetNumProps();
314 for (int i=0; i<props; i++)
316 prop = pTable->GetProp(i);
317 pname = prop->GetName();
318 if (pname && strcmp(name, pname) == 0)
321 info->actual_offset = offset + info->prop->GetOffset();
324 if (prop->GetDataTable())
326 if (UTIL_FindInSendTable(prop->GetDataTable(),
329 offset + prop->GetOffset())
340 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
344 for (int i=0; i<pMap->dataNumFields; i++)
346 if (pMap->dataDesc[i].fieldName == NULL)
350 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
352 return &(pMap->dataDesc[i]);
354 if (pMap->dataDesc[i].td)
356 typedescription_t *_td;
357 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
363 pMap = pMap->baseMap;
369 ServerClass *CHalfLife2::FindServerClass(const char *classname)
371 DataTableInfo *pInfo = _FindServerClass(classname);
381 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
383 DataTableInfo *pInfo = NULL;
385 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
387 ServerClass *sc = gamedll->GetAllServerClasses();
390 if (strcmp(classname, sc->GetName()) == 0)
392 pInfo = new DataTableInfo;
394 sm_trie_insert(m_pClasses, classname, pInfo);
395 m_Tables.push_back(pInfo);
409 bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info)
411 DataTableInfo *pInfo;
412 sm_sendprop_info_t *prop;
414 if ((pInfo = _FindServerClass(classname)) == NULL)
419 if ((prop = pInfo->lookup.retrieve(offset)) == NULL)
421 sm_sendprop_info_t temp_info;
423 if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
428 pInfo->lookup.insert(offset, temp_info);
439 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
441 sm_sendprop_info_t info;
443 if (!FindSendPropInfo(classname, offset, &info))
451 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
453 typedescription_t *td = NULL;
454 DataMapTrie &val = m_Maps[pMap];
458 val.trie = sm_trie_create();
460 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
462 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
464 sm_trie_insert(val.trie, offset, td);
471 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
473 #if SOURCE_ENGINE != SE_DARKMESSIAH
474 if (g_pSharedChangeInfo != NULL)
478 pEdict->StateChanged(offset);
482 pEdict->StateChanged();
488 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
492 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
494 #if SOURCE_ENGINE != SE_CSGO
495 bf_write *pBitBuf = NULL;
497 cell_t players[] = {client};
499 if (dest == HUD_PRINTTALK)
501 const char *chat_saytext = g_pGameConf->GetKeyValue("ChatSayText");
503 /* Use SayText user message instead */
504 if (chat_saytext != NULL && strcmp(chat_saytext, "yes") == 0)
507 UTIL_Format(buffer, sizeof(buffer), "%s\1\n", msg);
509 #if SOURCE_ENGINE == SE_CSGO
510 CCSUsrMsg_SayText *pMsg;
511 if ((pMsg = (CCSUsrMsg_SayText *)g_UserMsgs.StartProtobufMessage(m_SayTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
516 pMsg->set_ent_idx(0);
517 pMsg->set_text(buffer);
518 pMsg->set_chat(false);
520 if ((pBitBuf = g_UserMsgs.StartBitBufMessage(m_SayTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
525 pBitBuf->WriteByte(0);
526 pBitBuf->WriteString(buffer);
527 pBitBuf->WriteByte(1);
530 g_UserMsgs.EndMessage();
536 #if SOURCE_ENGINE == SE_CSGO
537 CCSUsrMsg_TextMsg *pMsg;
538 if ((pMsg = (CCSUsrMsg_TextMsg *)g_UserMsgs.StartProtobufMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
543 // Client tries to read all 5 'params' and will crash if less
544 pMsg->set_msg_dst(dest);
545 pMsg->add_params(msg);
546 pMsg->add_params("");
547 pMsg->add_params("");
548 pMsg->add_params("");
549 pMsg->add_params("");
551 if ((pBitBuf = g_UserMsgs.StartBitBufMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
556 pBitBuf->WriteByte(dest);
557 pBitBuf->WriteString(msg);
560 g_UserMsgs.EndMessage();
565 bool CHalfLife2::HintTextMsg(int client, const char *msg)
567 cell_t players[] = {client};
569 #if SOURCE_ENGINE == SE_CSGO
570 CCSUsrMsg_HintText *pMsg;
571 if ((pMsg = (CCSUsrMsg_HintText *)g_UserMsgs.StartProtobufMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
578 bf_write *pBitBuf = NULL;
580 if ((pBitBuf = g_UserMsgs.StartBitBufMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
585 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
586 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
588 pBitBuf->WriteByte(1);
590 pBitBuf->WriteString(msg);
592 g_UserMsgs.EndMessage();
597 bool CHalfLife2::HintTextMsg(cell_t *players, int count, const char *msg)
599 #if SOURCE_ENGINE == SE_CSGO
600 CCSUsrMsg_HintText *pMsg;
601 if ((pMsg = (CCSUsrMsg_HintText *)g_UserMsgs.StartProtobufMessage(m_HinTextMsg, players, count, USERMSG_RELIABLE)) == NULL)
608 bf_write *pBitBuf = NULL;
610 if ((pBitBuf = g_UserMsgs.StartBitBufMessage(m_HinTextMsg, players, count, USERMSG_RELIABLE)) == NULL)
615 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
616 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
618 pBitBuf->WriteByte(1);
620 pBitBuf->WriteString(msg);
623 g_UserMsgs.EndMessage();
628 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
630 KeyValues *SubKey = NULL;
632 cell_t players[] = {client};
634 #if SOURCE_ENGINE == SE_CSGO
635 CCSUsrMsg_VGUIMenu *pMsg;
636 if ((pMsg = (CCSUsrMsg_VGUIMenu *)g_UserMsgs.StartProtobufMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
641 bf_write *pBitBuf = NULL;
642 if ((pBitBuf = g_UserMsgs.StartBitBufMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
650 SubKey = data->GetFirstSubKey();
654 SubKey = SubKey->GetNextKey();
656 SubKey = data->GetFirstSubKey();
659 #if SOURCE_ENGINE == SE_CSGO
660 pMsg->set_name(name);
661 pMsg->set_show(show);
665 CCSUsrMsg_VGUIMenu_Subkey *key = pMsg->add_subkeys();
666 key->set_name(SubKey->GetName());
667 key->set_str(SubKey->GetString());
668 SubKey = SubKey->GetNextKey();
671 pBitBuf->WriteString(name);
672 pBitBuf->WriteByte((show) ? 1 : 0);
673 pBitBuf->WriteByte(count);
676 pBitBuf->WriteString(SubKey->GetName());
677 pBitBuf->WriteString(SubKey->GetString());
678 SubKey = SubKey->GetNextKey();
682 g_UserMsgs.EndMessage();
687 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
689 DelayedFakeCliCmd *pFake;
691 if (m_FreeCmds.empty())
693 pFake = new DelayedFakeCliCmd;
695 pFake = m_FreeCmds.front();
699 pFake->client = client;
700 pFake->userid = userid;
701 pFake->cmd.assign(cmd);
703 m_CmdQueue.push(pFake);
706 void CHalfLife2::ProcessFakeCliCmdQueue()
708 while (!m_CmdQueue.empty())
710 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
712 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
714 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
715 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
722 bool CHalfLife2::IsLANServer()
724 sv_lan = icvar->FindVar("sv_lan");
731 return (sv_lan->GetInt() != 0);
734 bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
736 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
737 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
739 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
744 Assert(_heapchk() == _HEAPOK);
747 FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
751 // load file into a null-terminated buffer
752 int fileSize = filesystem->Size(f);
753 char *buffer = (char *)MemAllocScratch(fileSize + 1);
757 filesystem->Read(buffer, fileSize, f); // read into local buffer
759 buffer[fileSize] = 0; // null terminate file as EOF
761 filesystem->Close( f ); // close file after reading
763 bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
771 return kv->LoadFromFile(filesystem, resourceName, pathID);
775 void CHalfLife2::PushCommandStack(const CCommand *cmd)
777 CachedCommandInfo info;
780 #if SOURCE_ENGINE <= SE_DARKMESSIAH
781 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
784 m_CommandStack.push(info);
787 const CCommand *CHalfLife2::PeekCommandStack()
789 if (m_CommandStack.empty())
794 return m_CommandStack.front().args;
797 void CHalfLife2::PopCommandStack()
799 m_CommandStack.pop();
802 const char *CHalfLife2::CurrentCommandName()
804 #if SOURCE_ENGINE >= SE_ORANGEBOX
805 return m_CommandStack.front().args->Arg(0);
807 return m_CommandStack.front().cmd;
811 void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
813 DelayedKickInfo kick;
815 kick.client = client;
816 kick.userid = userid;
817 UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
819 m_DelayedKicks.push(kick);
822 void CHalfLife2::ProcessDelayedKicks()
824 while (!m_DelayedKicks.empty())
826 DelayedKickInfo info = m_DelayedKicks.first();
827 m_DelayedKicks.pop();
829 CPlayer *player = g_Players.GetPlayerByIndex(info.client);
830 if (player == NULL || player->GetUserId() != info.userid)
835 player->Kick(info.buffer);
839 edict_t *CHalfLife2::EdictOfIndex(int index)
841 return ::PEntityOfEntIndex(index);
844 int CHalfLife2::IndexOfEdict(edict_t *pEnt)
846 return ::IndexOfEdict(pEnt);
849 edict_t *CHalfLife2::GetHandleEntity(CBaseHandle &hndl)
856 int index = hndl.GetEntryIndex();
858 edict_t *pStoredEdict;
859 CBaseEntity *pStoredEntity;
861 if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
866 if (pStoredEdict == NULL || pStoredEntity == NULL)
871 IServerEntity *pSE = pStoredEdict->GetIServerEntity();
878 if (pSE->GetRefEHandle() != hndl)
886 void CHalfLife2::SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt)
888 IServerEntity *pEntOther = pEnt->GetIServerEntity();
890 if (pEntOther == NULL)
898 const char *CHalfLife2::GetCurrentMap()
900 return STRING(gpGlobals->mapname);
903 void CHalfLife2::ServerCommand(const char *buffer)
905 engine->ServerCommand(buffer);
908 cell_t CHalfLife2::EntityToReference(CBaseEntity *pEntity)
910 IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
911 CBaseHandle hndl = pUnknown->GetRefEHandle();
912 return (hndl.ToInt() | (1<<31));
915 CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef)
917 if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
922 CEntInfo *pInfo = NULL;
924 if (entRef & (1<<31))
926 /* Proper ent reference */
927 int hndlValue = entRef & ~(1<<31);
928 CBaseHandle hndl(hndlValue);
930 pInfo = LookupEntity(hndl.GetEntryIndex());
931 if (!pInfo || pInfo->m_SerialNumber != hndl.GetSerialNumber())
938 /* Old style index only */
939 pInfo = LookupEntity(entRef);
947 IServerUnknown *pUnk = static_cast<IServerUnknown *>(pInfo->m_pEntity);
950 return pUnk->GetBaseEntity();
957 * Retrieves the CEntInfo pointer from g_EntList for a given entity index
959 CEntInfo *CHalfLife2::LookupEntity(int entIndex)
961 // Make sure that our index is within the bounds of the global ent array
962 if (entIndex < 0 || entIndex >= NUM_ENT_ENTRIES)
967 if (!g_EntList || entInfoOffset == -1)
969 /* Attempt to use engine interface instead */
970 static CEntInfo tempInfo;
971 tempInfo.m_pNext = NULL;
972 tempInfo.m_pPrev = NULL;
974 edict_t *pEdict = PEntityOfEntIndex(entIndex);
981 IServerUnknown *pUnk = pEdict->GetUnknown();
988 tempInfo.m_pEntity = pUnk;
989 tempInfo.m_SerialNumber = pUnk->GetRefEHandle().GetSerialNumber();
994 CEntInfo *pArray = (CEntInfo *)(((unsigned char *)g_EntList) + entInfoOffset);
995 return &pArray[entIndex];
999 SERIAL_MASK = 0x7fff (15 bits)
1001 #define MAX_EDICT_BITS 11
1002 #define NUM_ENT_ENTRY_BITS (MAX_EDICT_BITS + 1)
1003 m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS);
1005 Top 5 bits of a handle are unused.
1006 Bit 31 - Our 'reference' flag indicator
1009 cell_t CHalfLife2::IndexToReference(int entIndex)
1011 CBaseEntity *pEnt = ReferenceToEntity(entIndex);
1014 return INVALID_EHANDLE_INDEX;
1017 return EntityToReference(pEnt);
1020 int CHalfLife2::ReferenceToIndex(cell_t entRef)
1022 if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
1024 return INVALID_EHANDLE_INDEX;
1027 if (entRef & (1<<31))
1029 /* Proper ent reference */
1030 int hndlValue = entRef & ~(1<<31);
1031 CBaseHandle hndl(hndlValue);
1033 CEntInfo *pInfo = LookupEntity(hndl.GetEntryIndex());
1035 if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
1037 return INVALID_EHANDLE_INDEX;
1040 return hndl.GetEntryIndex();
1046 cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity)
1048 if (pEntity == NULL)
1050 return INVALID_EHANDLE_INDEX;
1053 IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
1054 CBaseHandle hndl = pUnknown->GetRefEHandle();
1056 if (hndl == INVALID_EHANDLE_INDEX)
1058 return INVALID_EHANDLE_INDEX;
1061 if (hndl.GetEntryIndex() >= MAX_EDICTS)
1063 return (hndl.ToInt() | (1<<31));
1067 return hndl.GetEntryIndex();
1071 cell_t CHalfLife2::ReferenceToBCompatRef(cell_t entRef)
1073 if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
1075 return INVALID_EHANDLE_INDEX;
1078 int hndlValue = entRef & ~(1<<31);
1079 CBaseHandle hndl(hndlValue);
1081 if (hndl.GetEntryIndex() < MAX_EDICTS)
1083 return hndl.GetEntryIndex();
1089 void *CHalfLife2::GetGlobalEntityList()
1094 int CHalfLife2::GetSendPropOffset(SendProp *prop)
1096 return prop->GetOffset();
1099 const char *CHalfLife2::GetEntityClassname(edict_t * pEdict)
1101 if (pEdict == NULL || pEdict->IsFree())
1106 IServerUnknown *pUnk = pEdict->GetUnknown();
1112 CBaseEntity * pEntity = pUnk->GetBaseEntity();
1114 if (pEntity == NULL)
1119 return GetEntityClassname(pEntity);
1122 const char *CHalfLife2::GetEntityClassname(CBaseEntity *pEntity)
1124 static int offset = -1;
1127 datamap_t *pMap = GetDataMap(pEntity);
1128 typedescription_t *pDesc = FindInDataMap(pMap, "m_iClassname");
1129 offset = GetTypeDescOffs(pDesc);
1132 return *(const char **)(((unsigned char *)pEntity) + offset);