3 * =============================================================================
5 * Copyright (C) 2004-2007 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"
39 bool g_IsOriginalEngine = false;
44 int HashFunction<datamap_t *>(datamap_t * const &k)
46 return reinterpret_cast<int>(k);
50 int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
56 CHalfLife2::CHalfLife2()
58 m_pClasses = sm_trie_create();
61 CHalfLife2::~CHalfLife2()
63 sm_trie_destroy(m_pClasses);
65 List<DataTableInfo *>::iterator iter;
67 for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
70 sm_trie_destroy(pInfo->lookup);
76 THash<datamap_t *, DataMapTrie>::iterator h_iter;
77 for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
81 sm_trie_destroy(h_iter->val.trie);
82 h_iter->val.trie = NULL;
89 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
91 void CHalfLife2::OnSourceModStartup(bool late)
93 /* The Ship currently is the only known game to use an older version of the engine */
94 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
96 /* :TODO: Better engine versioning - perhaps something added to SourceMM? */
97 g_IsOriginalEngine = true;
98 } else if (!g_pSharedChangeInfo) {
99 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
103 void CHalfLife2::OnSourceModAllInitialized()
105 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
106 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
107 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
108 g_ShareSys.AddInterface(NULL, this);
111 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
113 return engine->GetChangeAccessor( (const edict_t *)this );
116 SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
119 int props = pTable->GetNumProps();
122 for (int i=0; i<props; i++)
124 prop = pTable->GetProp(i);
125 pname = prop->GetName();
126 if (pname && strcmp(name, pname) == 0)
130 if (prop->GetDataTable())
132 if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
142 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
146 for (int i=0; i<pMap->dataNumFields; i++)
148 if (pMap->dataDesc[i].fieldName == NULL)
152 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
154 return &(pMap->dataDesc[i]);
156 if (pMap->dataDesc[i].td)
158 typedescription_t *_td;
159 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
165 pMap = pMap->baseMap;
171 ServerClass *CHalfLife2::FindServerClass(const char *classname)
173 DataTableInfo *pInfo = _FindServerClass(classname);
182 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
184 DataTableInfo *pInfo = NULL;
186 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
188 ServerClass *sc = gamedll->GetAllServerClasses();
191 if (strcmp(classname, sc->GetName()) == 0)
193 pInfo = new DataTableInfo;
194 pInfo->lookup = sm_trie_create();
196 sm_trie_insert(m_pClasses, classname, pInfo);
197 m_Tables.push_back(pInfo);
211 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
213 DataTableInfo *pInfo = _FindServerClass(classname);
220 SendProp *pProp = NULL;
221 if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
223 if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
225 sm_trie_insert(pInfo->lookup, offset, pProp);
232 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
234 typedescription_t *td = NULL;
235 DataMapTrie &val = m_Maps[pMap];
239 val.trie = sm_trie_create();
241 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
243 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
245 sm_trie_insert(val.trie, offset, td);
252 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
254 if (!g_IsOriginalEngine)
258 pEdict->StateChanged(offset);
260 pEdict->StateChanged();
263 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
267 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
269 bf_write *pBitBuf = NULL;
270 cell_t players[] = {client};
272 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
277 pBitBuf->WriteByte(dest);
278 pBitBuf->WriteString(msg);
279 g_UserMsgs.EndMessage();
284 bool CHalfLife2::HintTextMsg(int client, const char *msg)
286 bf_write *pBitBuf = NULL;
287 cell_t players[] = {client};
289 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
294 pBitBuf->WriteByte(1);
295 pBitBuf->WriteString(msg);
296 g_UserMsgs.EndMessage();
301 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
303 bf_write *pBitBuf = NULL;
304 KeyValues *SubKey = NULL;
306 cell_t players[] = {client};
308 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
315 SubKey = data->GetFirstSubKey();
319 SubKey = SubKey->GetNextKey();
321 SubKey = data->GetFirstSubKey();
324 pBitBuf->WriteString(name);
325 pBitBuf->WriteByte((show) ? 1 : 0);
326 pBitBuf->WriteByte(count);
329 pBitBuf->WriteString(SubKey->GetName());
330 pBitBuf->WriteString(SubKey->GetString());
331 SubKey = SubKey->GetNextKey();
333 g_UserMsgs.EndMessage();
338 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
340 DelayedFakeCliCmd *pFake;
342 if (m_FreeCmds.empty())
344 pFake = new DelayedFakeCliCmd;
346 pFake = m_FreeCmds.front();
350 pFake->client = client;
351 pFake->userid = userid;
352 pFake->cmd.assign(cmd);
354 m_CmdQueue.push(pFake);
357 void CHalfLife2::ProcessFakeCliCmdQueue()
359 while (!m_CmdQueue.empty())
361 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
363 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
365 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
366 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());