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"
37 #include "sm_stringutil.h"
38 #include <compat_wrappers.h>
41 bool g_IsOriginalEngine = false;
42 ConVar *sv_lan = NULL;
47 int HashFunction<datamap_t *>(datamap_t * const &k)
49 return reinterpret_cast<int>(k);
53 int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
59 CHalfLife2::CHalfLife2()
61 m_pClasses = sm_trie_create();
64 CHalfLife2::~CHalfLife2()
66 sm_trie_destroy(m_pClasses);
68 List<DataTableInfo *>::iterator iter;
70 for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
73 sm_trie_destroy(pInfo->lookup);
79 THash<datamap_t *, DataMapTrie>::iterator h_iter;
80 for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
84 sm_trie_destroy(h_iter->val.trie);
85 h_iter->val.trie = NULL;
92 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
94 void CHalfLife2::OnSourceModStartup(bool late)
96 /* The Ship currently is the only known game to use an older version of the engine */
97 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
99 /* :TODO: Better engine versioning - perhaps something added to SourceMM? */
100 g_IsOriginalEngine = true;
101 } else if (!g_pSharedChangeInfo) {
102 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
106 void CHalfLife2::OnSourceModAllInitialized()
108 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
109 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
110 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
111 g_ShareSys.AddInterface(NULL, this);
114 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
116 return engine->GetChangeAccessor( (const edict_t *)this );
119 SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
122 int props = pTable->GetNumProps();
125 for (int i=0; i<props; i++)
127 prop = pTable->GetProp(i);
128 pname = prop->GetName();
129 if (pname && strcmp(name, pname) == 0)
133 if (prop->GetDataTable())
135 if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
145 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
149 for (int i=0; i<pMap->dataNumFields; i++)
151 if (pMap->dataDesc[i].fieldName == NULL)
155 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
157 return &(pMap->dataDesc[i]);
159 if (pMap->dataDesc[i].td)
161 typedescription_t *_td;
162 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
168 pMap = pMap->baseMap;
174 ServerClass *CHalfLife2::FindServerClass(const char *classname)
176 DataTableInfo *pInfo = _FindServerClass(classname);
185 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
187 DataTableInfo *pInfo = NULL;
189 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
191 ServerClass *sc = gamedll->GetAllServerClasses();
194 if (strcmp(classname, sc->GetName()) == 0)
196 pInfo = new DataTableInfo;
197 pInfo->lookup = sm_trie_create();
199 sm_trie_insert(m_pClasses, classname, pInfo);
200 m_Tables.push_back(pInfo);
214 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
216 DataTableInfo *pInfo = _FindServerClass(classname);
223 SendProp *pProp = NULL;
224 if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
226 if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
228 sm_trie_insert(pInfo->lookup, offset, pProp);
235 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
237 typedescription_t *td = NULL;
238 DataMapTrie &val = m_Maps[pMap];
242 val.trie = sm_trie_create();
244 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
246 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
248 sm_trie_insert(val.trie, offset, td);
255 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
257 if (!g_IsOriginalEngine)
261 pEdict->StateChanged(offset);
263 pEdict->StateChanged();
266 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
270 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
272 bf_write *pBitBuf = NULL;
273 cell_t players[] = {client};
275 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
280 pBitBuf->WriteByte(dest);
281 pBitBuf->WriteString(msg);
282 g_UserMsgs.EndMessage();
287 bool CHalfLife2::HintTextMsg(int client, const char *msg)
289 bf_write *pBitBuf = NULL;
290 cell_t players[] = {client};
292 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
297 pBitBuf->WriteByte(1);
298 pBitBuf->WriteString(msg);
299 g_UserMsgs.EndMessage();
304 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
306 bf_write *pBitBuf = NULL;
307 KeyValues *SubKey = NULL;
309 cell_t players[] = {client};
311 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
318 SubKey = data->GetFirstSubKey();
322 SubKey = SubKey->GetNextKey();
324 SubKey = data->GetFirstSubKey();
327 pBitBuf->WriteString(name);
328 pBitBuf->WriteByte((show) ? 1 : 0);
329 pBitBuf->WriteByte(count);
332 pBitBuf->WriteString(SubKey->GetName());
333 pBitBuf->WriteString(SubKey->GetString());
334 SubKey = SubKey->GetNextKey();
336 g_UserMsgs.EndMessage();
341 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
343 DelayedFakeCliCmd *pFake;
345 if (m_FreeCmds.empty())
347 pFake = new DelayedFakeCliCmd;
349 pFake = m_FreeCmds.front();
353 pFake->client = client;
354 pFake->userid = userid;
355 pFake->cmd.assign(cmd);
357 m_CmdQueue.push(pFake);
360 void CHalfLife2::ProcessFakeCliCmdQueue()
362 while (!m_CmdQueue.empty())
364 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
366 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
368 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
369 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
376 bool CHalfLife2::IsLANServer()
378 sv_lan = icvar->FindVar("sv_lan");
385 return (sv_lan->GetInt() != 0);
388 void CHalfLife2::PushCommandStack(const CCommand *cmd)
390 CachedCommandInfo info;
393 #if !defined ORANGEBOX_BUILD
394 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
397 m_CommandStack.push(info);
400 const CCommand *CHalfLife2::PeekCommandStack()
402 if (m_CommandStack.empty())
407 return m_CommandStack.front().args;
410 void CHalfLife2::PopCommandStack()
412 m_CommandStack.pop();
415 const char *CHalfLife2::CurrentCommandName()
417 #if defined ORANGEBOX_BUILD
418 return m_CommandStack.front().args->Arg(0);
420 return m_CommandStack.front().cmd;