Initial Import of beta structs extension.
3 * =============================================================================
5 * Copyright (C) 2004-2008 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++)
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 bool UTIL_FindInSendTable(SendTable *pTable,
133 sm_sendprop_info_t *info,
137 int props = pTable->GetNumProps();
140 for (int i=0; i<props; i++)
142 prop = pTable->GetProp(i);
143 pname = prop->GetName();
144 if (pname && strcmp(name, pname) == 0)
147 info->actual_offset = offset + info->prop->GetOffset();
150 if (prop->GetDataTable())
152 if (UTIL_FindInSendTable(prop->GetDataTable(),
155 offset + prop->GetOffset())
166 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
170 for (int i=0; i<pMap->dataNumFields; i++)
172 if (pMap->dataDesc[i].fieldName == NULL)
176 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
178 return &(pMap->dataDesc[i]);
180 if (pMap->dataDesc[i].td)
182 typedescription_t *_td;
183 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
189 pMap = pMap->baseMap;
195 ServerClass *CHalfLife2::FindServerClass(const char *classname)
197 DataTableInfo *pInfo = _FindServerClass(classname);
207 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
209 DataTableInfo *pInfo = NULL;
211 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
213 ServerClass *sc = gamedll->GetAllServerClasses();
216 if (strcmp(classname, sc->GetName()) == 0)
218 pInfo = new DataTableInfo;
220 sm_trie_insert(m_pClasses, classname, pInfo);
221 m_Tables.push_back(pInfo);
235 bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info)
237 DataTableInfo *pInfo;
238 sm_sendprop_info_t *prop;
240 if ((pInfo = _FindServerClass(classname)) == NULL)
245 if ((prop = pInfo->lookup.retrieve(offset)) == NULL)
247 sm_sendprop_info_t temp_info;
249 if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
254 pInfo->lookup.insert(offset, temp_info);
265 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
267 sm_sendprop_info_t info;
269 if (!FindSendPropInfo(classname, offset, &info))
277 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
279 typedescription_t *td = NULL;
280 DataMapTrie &val = m_Maps[pMap];
284 val.trie = sm_trie_create();
286 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
288 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
290 sm_trie_insert(val.trie, offset, td);
297 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
299 if (g_pSharedChangeInfo != NULL)
303 pEdict->StateChanged(offset);
307 pEdict->StateChanged();
312 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
316 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
318 bf_write *pBitBuf = NULL;
319 cell_t players[] = {client};
321 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
326 pBitBuf->WriteByte(dest);
327 pBitBuf->WriteString(msg);
328 g_UserMsgs.EndMessage();
333 bool CHalfLife2::HintTextMsg(int client, const char *msg)
335 bf_write *pBitBuf = NULL;
336 cell_t players[] = {client};
338 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
343 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
344 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
346 pBitBuf->WriteByte(1);
348 pBitBuf->WriteString(msg);
349 g_UserMsgs.EndMessage();
354 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
356 bf_write *pBitBuf = NULL;
357 KeyValues *SubKey = NULL;
359 cell_t players[] = {client};
361 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
368 SubKey = data->GetFirstSubKey();
372 SubKey = SubKey->GetNextKey();
374 SubKey = data->GetFirstSubKey();
377 pBitBuf->WriteString(name);
378 pBitBuf->WriteByte((show) ? 1 : 0);
379 pBitBuf->WriteByte(count);
382 pBitBuf->WriteString(SubKey->GetName());
383 pBitBuf->WriteString(SubKey->GetString());
384 SubKey = SubKey->GetNextKey();
386 g_UserMsgs.EndMessage();
391 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
393 DelayedFakeCliCmd *pFake;
395 if (m_FreeCmds.empty())
397 pFake = new DelayedFakeCliCmd;
399 pFake = m_FreeCmds.front();
403 pFake->client = client;
404 pFake->userid = userid;
405 pFake->cmd.assign(cmd);
407 m_CmdQueue.push(pFake);
410 void CHalfLife2::ProcessFakeCliCmdQueue()
412 while (!m_CmdQueue.empty())
414 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
416 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
418 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
419 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
426 bool CHalfLife2::IsLANServer()
428 sv_lan = icvar->FindVar("sv_lan");
435 return (sv_lan->GetInt() != 0);
438 bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
440 #if defined METAMOD_PLAPI_VERSION
441 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
443 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
448 Assert(_heapchk() == _HEAPOK);
451 FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
455 // load file into a null-terminated buffer
456 int fileSize = filesystem->Size(f);
457 char *buffer = (char *)MemAllocScratch(fileSize + 1);
461 filesystem->Read(buffer, fileSize, f); // read into local buffer
463 buffer[fileSize] = 0; // null terminate file as EOF
465 filesystem->Close( f ); // close file after reading
467 bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
475 return kv->LoadFromFile(filesystem, resourceName, pathID);
479 void CHalfLife2::PushCommandStack(const CCommand *cmd)
481 CachedCommandInfo info;
484 #if !defined ORANGEBOX_BUILD
485 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
488 m_CommandStack.push(info);
491 const CCommand *CHalfLife2::PeekCommandStack()
493 if (m_CommandStack.empty())
498 return m_CommandStack.front().args;
501 void CHalfLife2::PopCommandStack()
503 m_CommandStack.pop();
506 const char *CHalfLife2::CurrentCommandName()
508 #if defined ORANGEBOX_BUILD
509 return m_CommandStack.front().args->Arg(0);
511 return m_CommandStack.front().cmd;
515 void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
517 DelayedKickInfo kick;
519 kick.client = client;
520 kick.userid = userid;
521 UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
523 m_DelayedKicks.push(kick);
526 void CHalfLife2::ProcessDelayedKicks()
528 while (!m_DelayedKicks.empty())
530 DelayedKickInfo info = m_DelayedKicks.first();
531 m_DelayedKicks.pop();
533 CPlayer *player = g_Players.GetPlayerByIndex(info.client);
534 if (player == NULL || player->GetUserId() != info.userid)
539 player->Kick(info.buffer);