Added support for Dark Messiah engine and game. (no bug, r=me).
The development of this feature would not be possible without the support of the following people from the game's community:
Dylan Riggs, Carl Pettengill, Ed Moreland, and Christian.
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 #if SOURCE_ENGINE != SE_DARKMESSIAH
92 CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
95 bool is_original_engine = false;
97 void CHalfLife2::OnSourceModStartup(bool late)
99 #if SOURCE_ENGINE != SE_DARKMESSIAH
101 /* The Ship currently is the only known game to use an older version of the engine */
102 #if defined METAMOD_PLAPI_VERSION
103 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
105 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
108 is_original_engine = true;
110 else if (g_pSharedChangeInfo == NULL)
112 g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
117 void CHalfLife2::OnSourceModAllInitialized()
119 m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
120 m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
121 m_SayTextMsg = g_UserMsgs.GetMessageIndex("SayText");
122 m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
123 g_ShareSys.AddInterface(NULL, this);
126 #if !defined METAMOD_PLAPI_VERSION
127 bool CHalfLife2::IsOriginalEngine()
129 return is_original_engine;
133 #if SOURCE_ENGINE != SE_DARKMESSIAH
134 IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
136 return engine->GetChangeAccessor( (const edict_t *)this );
140 bool UTIL_FindInSendTable(SendTable *pTable,
142 sm_sendprop_info_t *info,
146 int props = pTable->GetNumProps();
149 for (int i=0; i<props; i++)
151 prop = pTable->GetProp(i);
152 pname = prop->GetName();
153 if (pname && strcmp(name, pname) == 0)
156 info->actual_offset = offset + info->prop->GetOffset();
159 if (prop->GetDataTable())
161 if (UTIL_FindInSendTable(prop->GetDataTable(),
164 offset + prop->GetOffset())
175 typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
179 for (int i=0; i<pMap->dataNumFields; i++)
181 if (pMap->dataDesc[i].fieldName == NULL)
185 if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
187 return &(pMap->dataDesc[i]);
189 if (pMap->dataDesc[i].td)
191 typedescription_t *_td;
192 if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
198 pMap = pMap->baseMap;
204 ServerClass *CHalfLife2::FindServerClass(const char *classname)
206 DataTableInfo *pInfo = _FindServerClass(classname);
216 DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
218 DataTableInfo *pInfo = NULL;
220 if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
222 ServerClass *sc = gamedll->GetAllServerClasses();
225 if (strcmp(classname, sc->GetName()) == 0)
227 pInfo = new DataTableInfo;
229 sm_trie_insert(m_pClasses, classname, pInfo);
230 m_Tables.push_back(pInfo);
244 bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info)
246 DataTableInfo *pInfo;
247 sm_sendprop_info_t *prop;
249 if ((pInfo = _FindServerClass(classname)) == NULL)
254 if ((prop = pInfo->lookup.retrieve(offset)) == NULL)
256 sm_sendprop_info_t temp_info;
258 if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
263 pInfo->lookup.insert(offset, temp_info);
274 SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
276 sm_sendprop_info_t info;
278 if (!FindSendPropInfo(classname, offset, &info))
286 typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
288 typedescription_t *td = NULL;
289 DataMapTrie &val = m_Maps[pMap];
293 val.trie = sm_trie_create();
295 if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
297 if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
299 sm_trie_insert(val.trie, offset, td);
306 void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
308 #if SOURCE_ENGINE != SE_DARKMESSIAH
309 if (g_pSharedChangeInfo != NULL)
313 pEdict->StateChanged(offset);
317 pEdict->StateChanged();
323 pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
327 bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
329 bf_write *pBitBuf = NULL;
330 cell_t players[] = {client};
332 if (dest == HUD_PRINTTALK)
334 const char *chat_saytext = g_pGameConf->GetKeyValue("ChatSayText");
336 /* Use SayText user message instead */
337 if (chat_saytext != NULL && strcmp(chat_saytext, "yes") == 0)
340 UTIL_Format(buffer, sizeof(buffer), "%s\1\n", msg);
342 if ((pBitBuf = g_UserMsgs.StartMessage(m_SayTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
347 pBitBuf->WriteByte(0);
348 pBitBuf->WriteString(buffer);
349 pBitBuf->WriteByte(1);
351 g_UserMsgs.EndMessage();
357 if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
362 pBitBuf->WriteByte(dest);
363 pBitBuf->WriteString(msg);
365 g_UserMsgs.EndMessage();
370 bool CHalfLife2::HintTextMsg(int client, const char *msg)
372 bf_write *pBitBuf = NULL;
373 cell_t players[] = {client};
375 if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
380 const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
381 if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
383 pBitBuf->WriteByte(1);
385 pBitBuf->WriteString(msg);
386 g_UserMsgs.EndMessage();
391 bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
393 bf_write *pBitBuf = NULL;
394 KeyValues *SubKey = NULL;
396 cell_t players[] = {client};
398 if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
405 SubKey = data->GetFirstSubKey();
409 SubKey = SubKey->GetNextKey();
411 SubKey = data->GetFirstSubKey();
414 pBitBuf->WriteString(name);
415 pBitBuf->WriteByte((show) ? 1 : 0);
416 pBitBuf->WriteByte(count);
419 pBitBuf->WriteString(SubKey->GetName());
420 pBitBuf->WriteString(SubKey->GetString());
421 SubKey = SubKey->GetNextKey();
423 g_UserMsgs.EndMessage();
428 void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
430 DelayedFakeCliCmd *pFake;
432 if (m_FreeCmds.empty())
434 pFake = new DelayedFakeCliCmd;
436 pFake = m_FreeCmds.front();
440 pFake->client = client;
441 pFake->userid = userid;
442 pFake->cmd.assign(cmd);
444 m_CmdQueue.push(pFake);
447 void CHalfLife2::ProcessFakeCliCmdQueue()
449 while (!m_CmdQueue.empty())
451 DelayedFakeCliCmd *pFake = m_CmdQueue.first();
453 if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
455 CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
456 serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
463 bool CHalfLife2::IsLANServer()
465 sv_lan = icvar->FindVar("sv_lan");
472 return (sv_lan->GetInt() != 0);
475 bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
477 #if defined METAMOD_PLAPI_VERSION
478 if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
480 if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
485 Assert(_heapchk() == _HEAPOK);
488 FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
492 // load file into a null-terminated buffer
493 int fileSize = filesystem->Size(f);
494 char *buffer = (char *)MemAllocScratch(fileSize + 1);
498 filesystem->Read(buffer, fileSize, f); // read into local buffer
500 buffer[fileSize] = 0; // null terminate file as EOF
502 filesystem->Close( f ); // close file after reading
504 bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
512 return kv->LoadFromFile(filesystem, resourceName, pathID);
516 void CHalfLife2::PushCommandStack(const CCommand *cmd)
518 CachedCommandInfo info;
521 #if SOURCE_ENGINE <= SE_DARKMESSIAH
522 strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
525 m_CommandStack.push(info);
528 const CCommand *CHalfLife2::PeekCommandStack()
530 if (m_CommandStack.empty())
535 return m_CommandStack.front().args;
538 void CHalfLife2::PopCommandStack()
540 m_CommandStack.pop();
543 const char *CHalfLife2::CurrentCommandName()
545 #if SOURCE_ENGINE >= SE_ORANGEBOX
546 return m_CommandStack.front().args->Arg(0);
548 return m_CommandStack.front().cmd;
552 void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
554 DelayedKickInfo kick;
556 kick.client = client;
557 kick.userid = userid;
558 UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
560 m_DelayedKicks.push(kick);
563 void CHalfLife2::ProcessDelayedKicks()
565 while (!m_DelayedKicks.empty())
567 DelayedKickInfo info = m_DelayedKicks.first();
568 m_DelayedKicks.pop();
570 CPlayer *player = g_Players.GetPlayerByIndex(info.client);
571 if (player == NULL || player->GetUserId() != info.userid)
576 player->Kick(info.buffer);