atcprintf() is now fully re-entrant, meaning translations can be used from inside translations
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 ConVar *sv_lan = NULL;
46 int HashFunction<datamap_t *>(datamap_t * const &k)
48 return reinterpret_cast<int>(k);
52 int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
58 CHalfLife2::CHalfLife2()
60 m_pClasses = sm_trie_create();
63 CHalfLife2::~CHalfLife2()
65 sm_trie_destroy(m_pClasses);
67 List<DataTableInfo *>::iterator iter;
69 for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
72 sm_trie_destroy(pInfo->lookup);
78 THash<datamap_t *, DataMapTrie>::iterator h_iter;
79 for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
83 sm_trie_destroy(h_iter->val.trie);
84 h_iter->val.trie = NULL;
91 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
92 bool is_original_engine = false;
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 defined METAMOD_PLAPI_VERSION
98 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
100 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
103 is_original_engine = true;
105 else if (g_pSharedChangeInfo == NULL)
107 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
111 void CHalfLife2::OnSourceModAllInitialized()
113 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
114 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
115 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
116 g_ShareSys.AddInterface(NULL, this);
119 #if !defined METAMOD_PLAPI_VERSION
120 bool CHalfLife2::IsOriginalEngine()
122 return is_original_engine;
126 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
128 return engine->GetChangeAccessor( (const edict_t *)this );
131 SendProp *UTIL_FindInSendTable(SendTable *pTable, const char *name)
134 int props = pTable->GetNumProps();
137 for (int i=0; i<props; i++)
139 prop = pTable->GetProp(i);
140 pname = prop->GetName();
141 if (pname && strcmp(name, pname) == 0)
145 if (prop->GetDataTable())
147 if ((prop=UTIL_FindInSendTable(prop->GetDataTable(), name)) != NULL)
157 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
161 for (int i=0; i<pMap->dataNumFields; i++)
163 if (pMap->dataDesc[i].fieldName == NULL)
167 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
169 return &(pMap->dataDesc[i]);
171 if (pMap->dataDesc[i].td)
173 typedescription_t *_td;
174 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
180 pMap = pMap->baseMap;
186 ServerClass *CHalfLife2::FindServerClass(const char *classname)
188 DataTableInfo *pInfo = _FindServerClass(classname);
197 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
199 DataTableInfo *pInfo = NULL;
201 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
203 ServerClass *sc = gamedll->GetAllServerClasses();
206 if (strcmp(classname, sc->GetName()) == 0)
208 pInfo = new DataTableInfo;
209 pInfo->lookup = sm_trie_create();
211 sm_trie_insert(m_pClasses, classname, pInfo);
212 m_Tables.push_back(pInfo);
226 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
228 DataTableInfo *pInfo = _FindServerClass(classname);
235 SendProp *pProp = NULL;
236 if (!sm_trie_retrieve(pInfo->lookup, offset, (void **)&pProp))
238 if ((pProp = UTIL_FindInSendTable(pInfo->sc->m_pTable, offset)) != NULL)
240 sm_trie_insert(pInfo->lookup, offset, pProp);
247 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
249 typedescription_t *td = NULL;
250 DataMapTrie &val = m_Maps[pMap];
254 val.trie = sm_trie_create();
256 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
258 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
260 sm_trie_insert(val.trie, offset, td);
267 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
269 if (g_pSharedChangeInfo != NULL)
273 pEdict->StateChanged(offset);
275 pEdict->StateChanged();
280 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
284 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
286 bf_write *pBitBuf = NULL;
287 cell_t players[] = {client};
289 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
294 pBitBuf->WriteByte(dest);
295 pBitBuf->WriteString(msg);
296 g_UserMsgs.EndMessage();
301 bool CHalfLife2::HintTextMsg(int client, const char *msg)
303 bf_write *pBitBuf = NULL;
304 cell_t players[] = {client};
306 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
311 pBitBuf->WriteByte(1);
312 pBitBuf->WriteString(msg);
313 g_UserMsgs.EndMessage();
318 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
320 bf_write *pBitBuf = NULL;
321 KeyValues *SubKey = NULL;
323 cell_t players[] = {client};
325 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
332 SubKey = data->GetFirstSubKey();
336 SubKey = SubKey->GetNextKey();
338 SubKey = data->GetFirstSubKey();
341 pBitBuf->WriteString(name);
342 pBitBuf->WriteByte((show) ? 1 : 0);
343 pBitBuf->WriteByte(count);
346 pBitBuf->WriteString(SubKey->GetName());
347 pBitBuf->WriteString(SubKey->GetString());
348 SubKey = SubKey->GetNextKey();
350 g_UserMsgs.EndMessage();
355 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
357 DelayedFakeCliCmd *pFake;
359 if (m_FreeCmds.empty())
361 pFake = new DelayedFakeCliCmd;
363 pFake = m_FreeCmds.front();
367 pFake->client = client;
368 pFake->userid = userid;
369 pFake->cmd.assign(cmd);
371 m_CmdQueue.push(pFake);
374 void CHalfLife2::ProcessFakeCliCmdQueue()
376 while (!m_CmdQueue.empty())
378 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
380 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
382 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
383 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
390 bool CHalfLife2::IsLANServer()
392 sv_lan = icvar->FindVar("sv_lan");
399 return (sv_lan->GetInt() != 0);
402 void CHalfLife2::PushCommandStack(const CCommand *cmd)
404 CachedCommandInfo info;
407 #if !defined ORANGEBOX_BUILD
408 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
411 m_CommandStack.push(info);
414 const CCommand *CHalfLife2::PeekCommandStack()
416 if (m_CommandStack.empty())
421 return m_CommandStack.front().args;
424 void CHalfLife2::PopCommandStack()
426 m_CommandStack.pop();
429 const char *CHalfLife2::CurrentCommandName()
431 #if defined ORANGEBOX_BUILD
432 return m_CommandStack.front().args->Arg(0);
434 return m_CommandStack.front().cmd;