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 "GameConfigs.h"
39 #include <compat_wrappers.h>
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;
93 bool is_original_engine = false;
95 void CHalfLife2::OnSourceModStartup(bool late)
97 /* The Ship currently is the only known game to use an older version of the engine */
98 #if defined METAMOD_PLAPI_VERSION
99 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
101 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
104 is_original_engine = true;
106 else if (g_pSharedChangeInfo == NULL)
108 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
112 void CHalfLife2::OnSourceModAllInitialized()
114 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
115 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
116 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
117 g_ShareSys.AddInterface(NULL, this);
120 #if !defined METAMOD_PLAPI_VERSION
121 bool CHalfLife2::IsOriginalEngine()
123 return is_original_engine;
127 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
129 return engine->GetChangeAccessor( (const edict_t *)this );
132 SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
135 int props = pTable->GetNumProps();
138 for (int i=0; i<props; i++)
140 prop = pTable->GetProp(i);
141 pname = prop->GetName();
142 if (pname && strcmp(name, pname) == 0)
146 if (prop->GetDataTable())
148 if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
158 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
162 for (int i=0; i<pMap->dataNumFields; i++)
164 if (pMap->dataDesc[i].fieldName == NULL)
168 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
170 return &(pMap->dataDesc[i]);
172 if (pMap->dataDesc[i].td)
174 typedescription_t *_td;
175 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
181 pMap = pMap->baseMap;
187 ServerClass *CHalfLife2::FindServerClass(const char *classname)
189 DataTableInfo *pInfo = _FindServerClass(classname);
198 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
200 DataTableInfo *pInfo = NULL;
202 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
204 ServerClass *sc = gamedll->GetAllServerClasses();
207 if (strcmp(classname, sc->GetName()) == 0)
209 pInfo = new DataTableInfo;
210 pInfo->lookup = sm_trie_create();
212 sm_trie_insert(m_pClasses, classname, pInfo);
213 m_Tables.push_back(pInfo);
227 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
229 DataTableInfo *pInfo = _FindServerClass(classname);
236 SendProp *pProp = NULL;
237 if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
239 if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
241 sm_trie_insert(pInfo->lookup, offset, pProp);
248 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
250 typedescription_t *td = NULL;
251 DataMapTrie &val = m_Maps[pMap];
255 val.trie = sm_trie_create();
257 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
259 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
261 sm_trie_insert(val.trie, offset, td);
268 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
270 if (g_pSharedChangeInfo != NULL)
274 pEdict->StateChanged(offset);
276 pEdict->StateChanged();
281 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
285 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
287 bf_write *pBitBuf = NULL;
288 cell_t players[] = {client};
290 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
295 pBitBuf->WriteByte(dest);
296 pBitBuf->WriteString(msg);
297 g_UserMsgs.EndMessage();
302 bool CHalfLife2::HintTextMsg(int client, const char *msg)
304 bf_write *pBitBuf = NULL;
305 cell_t players[] = {client};
307 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
312 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
313 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
315 pBitBuf->WriteByte(1);
317 pBitBuf->WriteString(msg);
318 g_UserMsgs.EndMessage();
323 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
325 bf_write *pBitBuf = NULL;
326 KeyValues *SubKey = NULL;
328 cell_t players[] = {client};
330 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
337 SubKey = data->GetFirstSubKey();
341 SubKey = SubKey->GetNextKey();
343 SubKey = data->GetFirstSubKey();
346 pBitBuf->WriteString(name);
347 pBitBuf->WriteByte((show) ? 1 : 0);
348 pBitBuf->WriteByte(count);
351 pBitBuf->WriteString(SubKey->GetName());
352 pBitBuf->WriteString(SubKey->GetString());
353 SubKey = SubKey->GetNextKey();
355 g_UserMsgs.EndMessage();
360 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
362 DelayedFakeCliCmd *pFake;
364 if (m_FreeCmds.empty())
366 pFake = new DelayedFakeCliCmd;
368 pFake = m_FreeCmds.front();
372 pFake->client = client;
373 pFake->userid = userid;
374 pFake->cmd.assign(cmd);
376 m_CmdQueue.push(pFake);
379 void CHalfLife2::ProcessFakeCliCmdQueue()
381 while (!m_CmdQueue.empty())
383 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
385 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
387 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
388 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
395 bool CHalfLife2::IsLANServer()
397 sv_lan = icvar->FindVar("sv_lan");
404 return (sv_lan->GetInt() != 0);
407 void CHalfLife2::PushCommandStack(const CCommand *cmd)
409 CachedCommandInfo info;
412 #if !defined ORANGEBOX_BUILD
413 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
416 m_CommandStack.push(info);
419 const CCommand *CHalfLife2::PeekCommandStack()
421 if (m_CommandStack.empty())
426 return m_CommandStack.front().args;
429 void CHalfLife2::PopCommandStack()
431 m_CommandStack.pop();
434 const char *CHalfLife2::CurrentCommandName()
436 #if defined ORANGEBOX_BUILD
437 return m_CommandStack.front().args->Arg(0);
439 return m_CommandStack.front().cmd;