3 * ===============================================================
4 * SourceMod (C)2004-2007 AlliedModders LLC. All rights reserved.
5 * ===============================================================
7 * This file is not open source and may not be copied without explicit
8 * written permission of AlliedModders LLC. This file may not be redistributed
9 * in whole or significant part.
10 * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
15 #include "HalfLife2.h"
16 #include "sourcemod.h"
17 #include "sourcemm_api.h"
18 #include "UserMessages.h"
21 bool g_IsOriginalEngine = false;
26 int HashFunction<datamap_t *>(datamap_t * const &k)
28 return reinterpret_cast<int>(k);
32 int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
38 CHalfLife2::CHalfLife2()
40 m_pClasses = sm_trie_create();
43 CHalfLife2::~CHalfLife2()
45 sm_trie_destroy(m_pClasses);
47 List<DataTableInfo *>::iterator iter;
49 for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
52 sm_trie_destroy(pInfo->lookup);
58 THash<datamap_t *, DataMapTrie>::iterator h_iter;
59 for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
63 sm_trie_destroy(h_iter->val.trie);
64 h_iter->val.trie = NULL;
71 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
73 void CHalfLife2::OnSourceModStartup(bool late)
75 /* The Ship currently is the only known game to use an older version of the engine */
76 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
78 /* :TODO: Better engine versioning - perhaps something added to SourceMM? */
79 g_IsOriginalEngine = true;
80 } else if (!g_pSharedChangeInfo) {
81 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
85 void CHalfLife2::OnSourceModAllInitialized()
87 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
88 g_ShareSys.AddInterface(NULL, this);
91 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
93 return engine->GetChangeAccessor( (const edict_t *)this );
96 SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
99 int props = pTable->GetNumProps();
102 for (int i=0; i<props; i++)
104 prop = pTable->GetProp(i);
105 pname = prop->GetName();
106 if (pname && strcmp(name, pname) == 0)
110 if (prop->GetDataTable())
112 if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
122 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
126 for (int i=0; i<pMap->dataNumFields; i++)
128 if (pMap->dataDesc[i].fieldName == NULL)
132 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
134 return &(pMap->dataDesc[i]);
136 if (pMap->dataDesc[i].td)
138 typedescription_t *_td;
139 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
145 pMap = pMap->baseMap;
151 ServerClass *CHalfLife2::FindServerClass(const char *classname)
153 DataTableInfo *pInfo = _FindServerClass(classname);
162 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
164 DataTableInfo *pInfo = NULL;
166 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
168 ServerClass *sc = gamedll->GetAllServerClasses();
171 if (strcmp(classname, sc->GetName()) == 0)
173 pInfo = new DataTableInfo;
174 pInfo->lookup = sm_trie_create();
176 sm_trie_insert(m_pClasses, classname, pInfo);
177 m_Tables.push_back(pInfo);
191 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
193 DataTableInfo *pInfo = _FindServerClass(classname);
200 SendProp *pProp = NULL;
201 if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
203 if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
205 sm_trie_insert(pInfo->lookup, offset, pProp);
212 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
214 typedescription_t *td = NULL;
215 DataMapTrie &val = m_Maps[pMap];
219 val.trie = sm_trie_create();
221 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
223 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
225 sm_trie_insert(val.trie, offset, td);
232 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
234 if (!g_IsOriginalEngine)
238 pEdict->StateChanged(offset);
240 pEdict->StateChanged();
243 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
247 void CHalfLife2::TextMsg(int client, int dest, const char *msg)
249 bf_write *pBitBuf = NULL;
250 cell_t players[] = {client};
252 pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE);
253 pBitBuf->WriteByte(dest);
254 pBitBuf->WriteString(msg);
255 g_UserMsgs.EndMessage();