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>
46 typedef ICommandLine *(*FakeGetCommandLine)();
49 #define TIER0_NAME "tier0.dll"
50 #define VSTDLIB_NAME "vstdlib.dll"
51 #elif defined __APPLE__
52 #define TIER0_NAME "libtier0.dylib"
53 #define VSTDLIB_NAME "libvstdlib.dylib"
54 #elif defined __linux__
55 #if SOURCE_ENGINE == SE_ORANGEBOXVALVE
56 #define TIER0_NAME "libtier0_srv.so"
57 #define VSTDLIB_NAME "libvstdlib_srv.so"
58 #elif SOURCE_ENGINE >= SE_CSS
59 #define TIER0_NAME "libtier0.so"
60 #define VSTDLIB_NAME "libvstdlib.so"
62 #define TIER0_NAME "tier0_i486.so"
63 #define VSTDLIB_NAME "vstdlib_i486.so"
68 ConVar *sv_lan = NULL;
70 static void *g_EntList = NULL;
71 static int entInfoOffset = -1;
76 int HashFunction<datamap_t *>(datamap_t * const &k)
78 return reinterpret_cast<int>(k);
82 int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
88 CHalfLife2::CHalfLife2()
90 m_pClasses = sm_trie_create();
93 CHalfLife2::~CHalfLife2()
95 sm_trie_destroy(m_pClasses);
97 List<DataTableInfo *>::iterator iter;
99 for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
107 THash<datamap_t *, DataMapTrie>::iterator h_iter;
108 for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
110 if (h_iter->val.trie)
112 sm_trie_destroy(h_iter->val.trie);
113 h_iter->val.trie = NULL;
120 #if SOURCE_ENGINE != SE_DARKMESSIAH
121 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
124 #if !defined METAMOD_PLAPI_VERSION || PLAPI_VERSION < 11
125 bool is_original_engine = false;
128 void CHalfLife2::OnSourceModStartup(bool late)
130 #if SOURCE_ENGINE != SE_DARKMESSIAH
132 /* The Ship currently is the only known game to use an older version of the engine */
133 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
134 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
136 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
139 is_original_engine = true;
141 else if (g_pSharedChangeInfo == NULL)
143 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
148 void CHalfLife2::OnSourceModAllInitialized()
150 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
151 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
152 m_SayTextMsg = g_UserMsgs.GetMessageIndex("SayText");
153 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
154 g_ShareSys.AddInterface(NULL, this);
157 void CHalfLife2::OnSourceModAllInitialized_Post()
159 InitLogicalEntData();
163 void CHalfLife2::InitLogicalEntData()
168 * gEntList and/or g_pEntityList
170 * First try to lookup pointer directly for platforms with symbols.
171 * If symbols aren't present (Windows or stripped Linux/Mac),
172 * attempt find via LevelShutdown + offset
174 if (g_pGameConf->GetMemSig("gEntList", (void **)&addr))
176 #if !defined PLATFORM_WINDOWS
179 // Key exists so notify if lookup fails, but try other method.
180 g_Logger.LogError("Failed lookup of gEntList directly - Reverting to lookup via LevelShutdown");
185 g_EntList = reinterpret_cast<void *>(addr);
186 #if !defined PLATFORM_WINDOWS
194 if (!g_pGameConf->GetMemSig("LevelShutdown", (void **)&addr))
196 g_Logger.LogError("Logical Entities not supported by this mod (LevelShutdown) - Reverting to networkable entities only");
202 g_Logger.LogError("Failed lookup of LevelShutdown - Reverting to networkable entities only");
207 if (!g_pGameConf->GetOffset("gEntList", &offset))
209 g_Logger.LogError("Logical Entities not supported by this mod (gEntList) - Reverting to networkable entities only");
213 g_EntList = *reinterpret_cast<void **>(addr + offset);
218 g_Logger.LogError("Failed lookup of gEntList - Reverting to networkable entities only");
222 if (!g_pGameConf->GetOffset("EntInfo", &entInfoOffset))
224 g_Logger.LogError("Logical Entities not supported by this mod (EntInfo) - Reverting to networkable entities only");
229 void CHalfLife2::InitCommandLine()
231 char path[PLATFORM_MAX_PATH];
234 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "../bin/" TIER0_NAME);
236 if (!g_LibSys.IsPathFile(path))
238 g_Logger.LogError("Could not find path for: " TIER0_NAME);
242 ILibrary *lib = g_LibSys.OpenLibrary(path, error, sizeof(error));
243 m_pGetCommandLine = lib->GetSymbolAddress("CommandLine_Tier0");
245 /* '_Tier0' dropped on Alien Swarm version */
246 if (m_pGetCommandLine == NULL)
248 m_pGetCommandLine = lib->GetSymbolAddress("CommandLine");
251 if (m_pGetCommandLine == NULL)
253 /* We probably have a Ship engine. */
255 g_SourceMod.BuildPath(Path_Game, path, sizeof(path), "../bin/" VSTDLIB_NAME);
256 if (!g_LibSys.IsPathFile(path))
258 g_Logger.LogError("Could not find path for: " VSTDLIB_NAME);
262 if ((lib = g_LibSys.OpenLibrary(path, error, sizeof(error))) == NULL)
264 g_Logger.LogError("Could not load %s: %s", path, error);
268 m_pGetCommandLine = lib->GetSymbolAddress("CommandLine");
270 if (m_pGetCommandLine == NULL)
272 g_Logger.LogError("Could not locate any command line functionality");
279 ICommandLine *CHalfLife2::GetValveCommandLine()
281 if (!m_pGetCommandLine)
284 return ((FakeGetCommandLine)((FakeGetCommandLine *)m_pGetCommandLine))();
287 #if !defined METAMOD_PLAPI_VERSION || PLAPI_VERSION < 11
288 bool CHalfLife2::IsOriginalEngine()
290 return is_original_engine;
294 #if SOURCE_ENGINE != SE_DARKMESSIAH
295 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
297 return engine->GetChangeAccessor( (const edict_t *)this );
301 bool UTIL_FindInSendTable(SendTable *pTable,
303 sm_sendprop_info_t *info,
307 int props = pTable->GetNumProps();
310 for (int i=0; i<props; i++)
312 prop = pTable->GetProp(i);
313 pname = prop->GetName();
314 if (pname && strcmp(name, pname) == 0)
317 info->actual_offset = offset + info->prop->GetOffset();
320 if (prop->GetDataTable())
322 if (UTIL_FindInSendTable(prop->GetDataTable(),
325 offset + prop->GetOffset())
336 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
340 for (int i=0; i<pMap->dataNumFields; i++)
342 if (pMap->dataDesc[i].fieldName == NULL)
346 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
348 return &(pMap->dataDesc[i]);
350 if (pMap->dataDesc[i].td)
352 typedescription_t *_td;
353 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
359 pMap = pMap->baseMap;
365 ServerClass *CHalfLife2::FindServerClass(const char *classname)
367 DataTableInfo *pInfo = _FindServerClass(classname);
377 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
379 DataTableInfo *pInfo = NULL;
381 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
383 ServerClass *sc = gamedll->GetAllServerClasses();
386 if (strcmp(classname, sc->GetName()) == 0)
388 pInfo = new DataTableInfo;
390 sm_trie_insert(m_pClasses, classname, pInfo);
391 m_Tables.push_back(pInfo);
405 bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info)
407 DataTableInfo *pInfo;
408 sm_sendprop_info_t *prop;
410 if ((pInfo = _FindServerClass(classname)) == NULL)
415 if ((prop = pInfo->lookup.retrieve(offset)) == NULL)
417 sm_sendprop_info_t temp_info;
419 if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
424 pInfo->lookup.insert(offset, temp_info);
435 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
437 sm_sendprop_info_t info;
439 if (!FindSendPropInfo(classname, offset, &info))
447 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
449 typedescription_t *td = NULL;
450 DataMapTrie &val = m_Maps[pMap];
454 val.trie = sm_trie_create();
456 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
458 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
460 sm_trie_insert(val.trie, offset, td);
467 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
469 #if SOURCE_ENGINE != SE_DARKMESSIAH
470 if (g_pSharedChangeInfo != NULL)
474 pEdict->StateChanged(offset);
478 pEdict->StateChanged();
484 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
488 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
490 bf_write *pBitBuf = NULL;
491 cell_t players[] = {client};
493 if (dest == HUD_PRINTTALK)
495 const char *chat_saytext = g_pGameConf->GetKeyValue("ChatSayText");
497 /* Use SayText user message instead */
498 if (chat_saytext != NULL && strcmp(chat_saytext, "yes") == 0)
501 UTIL_Format(buffer, sizeof(buffer), "%s\1\n", msg);
503 if ((pBitBuf = g_UserMsgs.StartMessage(m_SayTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
508 pBitBuf->WriteByte(0);
509 pBitBuf->WriteString(buffer);
510 pBitBuf->WriteByte(1);
512 g_UserMsgs.EndMessage();
518 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
523 pBitBuf->WriteByte(dest);
524 pBitBuf->WriteString(msg);
526 g_UserMsgs.EndMessage();
531 bool CHalfLife2::HintTextMsg(int client, const char *msg)
533 bf_write *pBitBuf = NULL;
534 cell_t players[] = {client};
536 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
541 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
542 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
544 pBitBuf->WriteByte(1);
546 pBitBuf->WriteString(msg);
547 g_UserMsgs.EndMessage();
552 bool CHalfLife2::HintTextMsg(cell_t *players, int count, const char *msg)
554 bf_write *pBitBuf = NULL;
556 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, count, USERMSG_RELIABLE)) == NULL)
561 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
562 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
564 pBitBuf->WriteByte(1);
566 pBitBuf->WriteString(msg);
567 g_UserMsgs.EndMessage();
572 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
574 bf_write *pBitBuf = NULL;
575 KeyValues *SubKey = NULL;
577 cell_t players[] = {client};
579 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
586 SubKey = data->GetFirstSubKey();
590 SubKey = SubKey->GetNextKey();
592 SubKey = data->GetFirstSubKey();
595 pBitBuf->WriteString(name);
596 pBitBuf->WriteByte((show) ? 1 : 0);
597 pBitBuf->WriteByte(count);
600 pBitBuf->WriteString(SubKey->GetName());
601 pBitBuf->WriteString(SubKey->GetString());
602 SubKey = SubKey->GetNextKey();
604 g_UserMsgs.EndMessage();
609 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
611 DelayedFakeCliCmd *pFake;
613 if (m_FreeCmds.empty())
615 pFake = new DelayedFakeCliCmd;
617 pFake = m_FreeCmds.front();
621 pFake->client = client;
622 pFake->userid = userid;
623 pFake->cmd.assign(cmd);
625 m_CmdQueue.push(pFake);
628 void CHalfLife2::ProcessFakeCliCmdQueue()
630 while (!m_CmdQueue.empty())
632 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
634 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
636 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
637 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
644 bool CHalfLife2::IsLANServer()
646 sv_lan = icvar->FindVar("sv_lan");
653 return (sv_lan->GetInt() != 0);
656 bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
658 #if defined METAMOD_PLAPI_VERSION || PLAPI_VERSION >= 11
659 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
661 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
666 Assert(_heapchk() == _HEAPOK);
669 FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
673 // load file into a null-terminated buffer
674 int fileSize = filesystem->Size(f);
675 char *buffer = (char *)MemAllocScratch(fileSize + 1);
679 filesystem->Read(buffer, fileSize, f); // read into local buffer
681 buffer[fileSize] = 0; // null terminate file as EOF
683 filesystem->Close( f ); // close file after reading
685 bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
693 return kv->LoadFromFile(filesystem, resourceName, pathID);
697 void CHalfLife2::PushCommandStack(const CCommand *cmd)
699 CachedCommandInfo info;
702 #if SOURCE_ENGINE <= SE_DARKMESSIAH
703 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
706 m_CommandStack.push(info);
709 const CCommand *CHalfLife2::PeekCommandStack()
711 if (m_CommandStack.empty())
716 return m_CommandStack.front().args;
719 void CHalfLife2::PopCommandStack()
721 m_CommandStack.pop();
724 const char *CHalfLife2::CurrentCommandName()
726 #if SOURCE_ENGINE >= SE_ORANGEBOX
727 return m_CommandStack.front().args->Arg(0);
729 return m_CommandStack.front().cmd;
733 void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
735 DelayedKickInfo kick;
737 kick.client = client;
738 kick.userid = userid;
739 UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
741 m_DelayedKicks.push(kick);
744 void CHalfLife2::ProcessDelayedKicks()
746 while (!m_DelayedKicks.empty())
748 DelayedKickInfo info = m_DelayedKicks.first();
749 m_DelayedKicks.pop();
751 CPlayer *player = g_Players.GetPlayerByIndex(info.client);
752 if (player == NULL || player->GetUserId() != info.userid)
757 player->Kick(info.buffer);
761 edict_t *CHalfLife2::EdictOfIndex(int index)
763 return ::PEntityOfEntIndex(index);
766 int CHalfLife2::IndexOfEdict(edict_t *pEnt)
768 return ::IndexOfEdict(pEnt);
771 edict_t *CHalfLife2::GetHandleEntity(CBaseHandle &hndl)
778 int index = hndl.GetEntryIndex();
780 edict_t *pStoredEdict;
781 CBaseEntity *pStoredEntity;
783 if (!IndexToAThings(index, &pStoredEntity, &pStoredEdict))
788 if (pStoredEdict == NULL || pStoredEntity == NULL)
793 IServerEntity *pSE = pStoredEdict->GetIServerEntity();
800 if (pSE->GetRefEHandle() != hndl)
808 void CHalfLife2::SetHandleEntity(CBaseHandle &hndl, edict_t *pEnt)
810 IServerEntity *pEntOther = pEnt->GetIServerEntity();
812 if (pEntOther == NULL)
820 const char *CHalfLife2::GetCurrentMap()
822 return STRING(gpGlobals->mapname);
825 void CHalfLife2::ServerCommand(const char *buffer)
827 engine->ServerCommand(buffer);
830 cell_t CHalfLife2::EntityToReference(CBaseEntity *pEntity)
832 IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
833 CBaseHandle hndl = pUnknown->GetRefEHandle();
834 return (hndl.ToInt() | (1<<31));
837 CBaseEntity *CHalfLife2::ReferenceToEntity(cell_t entRef)
839 if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
844 CEntInfo *pInfo = NULL;
846 if (entRef & (1<<31))
848 /* Proper ent reference */
849 int hndlValue = entRef & ~(1<<31);
850 CBaseHandle hndl(hndlValue);
852 pInfo = LookupEntity(hndl.GetEntryIndex());
853 if (!pInfo || pInfo->m_SerialNumber != hndl.GetSerialNumber())
860 /* Old style index only */
861 pInfo = LookupEntity(entRef);
869 IServerUnknown *pUnk = static_cast<IServerUnknown *>(pInfo->m_pEntity);
872 return pUnk->GetBaseEntity();
879 * Retrieves the CEntInfo pointer from g_EntList for a given entity index
881 CEntInfo *CHalfLife2::LookupEntity(int entIndex)
883 // Make sure that our index is within the bounds of the global ent array
884 if (entIndex < 0 || entIndex >= NUM_ENT_ENTRIES)
889 if (!g_EntList || entInfoOffset == -1)
891 /* Attempt to use engine interface instead */
892 static CEntInfo tempInfo;
893 tempInfo.m_pNext = NULL;
894 tempInfo.m_pPrev = NULL;
896 edict_t *pEdict = PEntityOfEntIndex(entIndex);
903 IServerUnknown *pUnk = pEdict->GetUnknown();
910 tempInfo.m_pEntity = pUnk;
911 tempInfo.m_SerialNumber = pUnk->GetRefEHandle().GetSerialNumber();
916 CEntInfo *pArray = (CEntInfo *)(((unsigned char *)g_EntList) + entInfoOffset);
917 return &pArray[entIndex];
921 SERIAL_MASK = 0x7fff (15 bits)
923 #define MAX_EDICT_BITS 11
924 #define NUM_ENT_ENTRY_BITS (MAX_EDICT_BITS + 1)
925 m_Index = iEntry | (iSerialNumber << NUM_ENT_ENTRY_BITS);
927 Top 5 bits of a handle are unused.
928 Bit 31 - Our 'reference' flag indicator
931 cell_t CHalfLife2::IndexToReference(int entIndex)
933 CBaseEntity *pEnt = ReferenceToEntity(entIndex);
936 return INVALID_EHANDLE_INDEX;
939 return EntityToReference(pEnt);
942 int CHalfLife2::ReferenceToIndex(cell_t entRef)
944 if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
946 return INVALID_EHANDLE_INDEX;
949 if (entRef & (1<<31))
951 /* Proper ent reference */
952 int hndlValue = entRef & ~(1<<31);
953 CBaseHandle hndl(hndlValue);
955 CEntInfo *pInfo = LookupEntity(hndl.GetEntryIndex());
957 if (pInfo->m_SerialNumber != hndl.GetSerialNumber())
959 return INVALID_EHANDLE_INDEX;
962 return hndl.GetEntryIndex();
968 cell_t CHalfLife2::EntityToBCompatRef(CBaseEntity *pEntity)
972 return INVALID_EHANDLE_INDEX;
975 IServerUnknown *pUnknown = (IServerUnknown *)pEntity;
976 CBaseHandle hndl = pUnknown->GetRefEHandle();
978 if (hndl == INVALID_EHANDLE_INDEX)
980 return INVALID_EHANDLE_INDEX;
983 if (hndl.GetEntryIndex() >= MAX_EDICTS)
985 return (hndl.ToInt() | (1<<31));
989 return hndl.GetEntryIndex();
993 cell_t CHalfLife2::ReferenceToBCompatRef(cell_t entRef)
995 if ((unsigned)entRef == INVALID_EHANDLE_INDEX)
997 return INVALID_EHANDLE_INDEX;
1000 int hndlValue = entRef & ~(1<<31);
1001 CBaseHandle hndl(hndlValue);
1003 if (hndl.GetEntryIndex() < MAX_EDICTS)
1005 return hndl.GetEntryIndex();
1011 void *CHalfLife2::GetGlobalEntityList()
1016 int CHalfLife2::GetSendPropOffset(SendProp *prop)
1018 return prop->GetOffset();
1021 const char *CHalfLife2::GetEntityClassname(edict_t * pEdict)
1023 if (pEdict == NULL || pEdict->IsFree())
1028 IServerUnknown *pUnk = pEdict->GetUnknown();
1034 CBaseEntity * pEntity = pUnk->GetBaseEntity();
1036 if (pEntity == NULL)
1041 return GetEntityClassname(pEntity);
1044 const char *CHalfLife2::GetEntityClassname(CBaseEntity *pEntity)
1046 static int offset = -1;
1049 datamap_t *pMap = GetDataMap(pEntity);
1050 typedescription_t *pDesc = FindInDataMap(pMap, "m_iClassname");
1051 offset = GetTypeDescOffs(pDesc);
1054 return *(const char **)(((unsigned char *)pEntity) + offset);