ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include "HalfLife2.h" ds@1952: #include "sourcemod.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "UserMessages.h" ds@1952: #include "PlayerManager.h" ds@1952: #include "sm_stringutil.h" ds@1952: #include "GameConfigs.h" ds@1952: #include ds@1952: ds@1952: CHalfLife2 g_HL2; ds@1952: ConVar *sv_lan = NULL; ds@1952: ds@1952: namespace SourceHook ds@1952: { ds@1952: template<> ds@1952: int HashFunction(datamap_t * const &k) ds@1952: { ds@1952: return reinterpret_cast(k); ds@1952: } ds@1952: ds@1952: template<> ds@1952: int Compare(datamap_t * const &k1, datamap_t * const &k2) ds@1952: { ds@1952: return (k1-k2); ds@1952: } ds@1952: } ds@1952: ds@1952: CHalfLife2::CHalfLife2() ds@1952: { ds@1952: m_pClasses = sm_trie_create(); ds@1952: } ds@1952: ds@1952: CHalfLife2::~CHalfLife2() ds@1952: { ds@1952: sm_trie_destroy(m_pClasses); ds@1952: ds@1952: List::iterator iter; ds@1952: DataTableInfo *pInfo; ds@1952: for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++) ds@1952: { ds@1952: pInfo = (*iter); ds@1952: delete pInfo; ds@1952: } ds@1952: ds@1952: m_Tables.clear(); ds@1952: ds@1952: THash::iterator h_iter; ds@1952: for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++) ds@1952: { ds@1952: if (h_iter->val.trie) ds@1952: { ds@1952: sm_trie_destroy(h_iter->val.trie); ds@1952: h_iter->val.trie = NULL; ds@1952: } ds@1952: } ds@1952: ds@1952: m_Maps.clear(); ds@1952: } ds@1952: ds@1952: CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL; ds@1952: bool is_original_engine = false; ds@1952: ds@1952: void CHalfLife2::OnSourceModStartup(bool late) ds@1952: { ds@1952: /* The Ship currently is the only known game to use an older version of the engine */ ds@1952: #if defined METAMOD_PLAPI_VERSION ds@1952: if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL) ds@1952: #else ds@1952: if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0) ds@1952: #endif ds@1952: { ds@1952: is_original_engine = true; ds@1952: } ds@1952: else if (g_pSharedChangeInfo == NULL) ds@1952: { ds@1952: g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo(); ds@1952: } ds@1952: } ds@1952: ds@1952: void CHalfLife2::OnSourceModAllInitialized() ds@1952: { ds@1952: m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg"); ds@1952: m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText"); ds@1952: m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu"); ds@1952: g_ShareSys.AddInterface(NULL, this); ds@1952: } ds@1952: ds@1952: #if !defined METAMOD_PLAPI_VERSION ds@1952: bool CHalfLife2::IsOriginalEngine() ds@1952: { ds@1952: return is_original_engine; ds@1952: } ds@1952: #endif ds@1952: ds@1952: IChangeInfoAccessor *CBaseEdict::GetChangeAccessor() ds@1952: { ds@1952: return engine->GetChangeAccessor( (const edict_t *)this ); ds@1952: } ds@1952: ds@1952: bool UTIL_FindInSendTable(SendTable *pTable, ds@1952: const char *name, ds@1952: sm_sendprop_info_t *info, ds@1952: unsigned int offset) ds@1952: { ds@1952: const char *pname; ds@1952: int props = pTable->GetNumProps(); ds@1952: SendProp *prop; ds@1952: ds@1952: for (int i=0; iGetProp(i); ds@1952: pname = prop->GetName(); ds@1952: if (pname && strcmp(name, pname) == 0) ds@1952: { ds@1952: info->prop = prop; ds@1952: info->actual_offset = offset + info->prop->GetOffset(); ds@1952: return true; ds@1952: } ds@1952: if (prop->GetDataTable()) ds@1952: { ds@1952: if (UTIL_FindInSendTable(prop->GetDataTable(), ds@1952: name, ds@1952: info, ds@1952: offset + prop->GetOffset()) ds@1952: ) ds@1952: { ds@1952: return true; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: return false; ds@1952: } ds@1952: ds@1952: typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name) ds@1952: { ds@1952: while (pMap) ds@1952: { ds@1952: for (int i=0; idataNumFields; i++) ds@1952: { ds@1952: if (pMap->dataDesc[i].fieldName == NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (strcmp(name, pMap->dataDesc[i].fieldName) == 0) ds@1952: { ds@1952: return &(pMap->dataDesc[i]); ds@1952: } ds@1952: if (pMap->dataDesc[i].td) ds@1952: { ds@1952: typedescription_t *_td; ds@1952: if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL) ds@1952: { ds@1952: return _td; ds@1952: } ds@1952: } ds@1952: } ds@1952: pMap = pMap->baseMap; ds@1952: } ds@1952: ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: ServerClass *CHalfLife2::FindServerClass(const char *classname) ds@1952: { ds@1952: DataTableInfo *pInfo = _FindServerClass(classname); ds@1952: ds@1952: if (!pInfo) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: return pInfo->sc; ds@1952: } ds@1952: ds@1952: DataTableInfo *CHalfLife2::_FindServerClass(const char *classname) ds@1952: { ds@1952: DataTableInfo *pInfo = NULL; ds@1952: ds@1952: if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo)) ds@1952: { ds@1952: ServerClass *sc = gamedll->GetAllServerClasses(); ds@1952: while (sc) ds@1952: { ds@1952: if (strcmp(classname, sc->GetName()) == 0) ds@1952: { ds@1952: pInfo = new DataTableInfo; ds@1952: pInfo->sc = sc; ds@1952: sm_trie_insert(m_pClasses, classname, pInfo); ds@1952: m_Tables.push_back(pInfo); ds@1952: break; ds@1952: } ds@1952: sc = sc->m_pNext; ds@1952: } ds@1952: if (!pInfo) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: } ds@1952: ds@1952: return pInfo; ds@1952: } ds@1952: ds@1952: bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info) ds@1952: { ds@1952: DataTableInfo *pInfo; ds@1952: sm_sendprop_info_t *prop; ds@1952: ds@1952: if ((pInfo = _FindServerClass(classname)) == NULL) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: if ((prop = pInfo->lookup.retrieve(offset)) == NULL) ds@1952: { ds@1952: sm_sendprop_info_t temp_info; ds@1952: ds@1952: if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0)) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: pInfo->lookup.insert(offset, temp_info); ds@1952: *info = temp_info; ds@1952: } ds@1952: else ds@1952: { ds@1952: *info = *prop; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset) ds@1952: { ds@1952: sm_sendprop_info_t info; ds@1952: ds@1952: if (!FindSendPropInfo(classname, offset, &info)) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: return info.prop; ds@1952: } ds@1952: ds@1952: typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset) ds@1952: { ds@1952: typedescription_t *td = NULL; ds@1952: DataMapTrie &val = m_Maps[pMap]; ds@1952: ds@1952: if (!val.trie) ds@1952: { ds@1952: val.trie = sm_trie_create(); ds@1952: } ds@1952: if (!sm_trie_retrieve(val.trie, offset, (void **)&td)) ds@1952: { ds@1952: if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL) ds@1952: { ds@1952: sm_trie_insert(val.trie, offset, td); ds@1952: } ds@1952: } ds@1952: ds@1952: return td; ds@1952: } ds@1952: ds@1952: void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset) ds@1952: { ds@1952: if (g_pSharedChangeInfo != NULL) ds@1952: { ds@1952: if (offset) ds@1952: { ds@1952: pEdict->StateChanged(offset); ds@1952: } ds@1952: else ds@1952: { ds@1952: pEdict->StateChanged(); ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: pEdict->m_fStateFlags |= FL_EDICT_CHANGED; ds@1952: } ds@1952: } ds@1952: ds@1952: bool CHalfLife2::TextMsg(int client, int dest, const char *msg) ds@1952: { ds@1952: bf_write *pBitBuf = NULL; ds@1952: cell_t players[] = {client}; ds@1952: ds@1952: if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: pBitBuf->WriteByte(dest); ds@1952: pBitBuf->WriteString(msg); ds@1952: g_UserMsgs.EndMessage(); ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CHalfLife2::HintTextMsg(int client, const char *msg) ds@1952: { ds@1952: bf_write *pBitBuf = NULL; ds@1952: cell_t players[] = {client}; ds@1952: ds@1952: if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte"); ds@1952: if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0) ds@1952: { ds@1952: pBitBuf->WriteByte(1); ds@1952: } ds@1952: pBitBuf->WriteString(msg); ds@1952: g_UserMsgs.EndMessage(); ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show) ds@1952: { ds@1952: bf_write *pBitBuf = NULL; ds@1952: KeyValues *SubKey = NULL; ds@1952: int count = 0; ds@1952: cell_t players[] = {client}; ds@1952: ds@1952: if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (data) ds@1952: { ds@1952: SubKey = data->GetFirstSubKey(); ds@1952: while (SubKey) ds@1952: { ds@1952: count++; ds@1952: SubKey = SubKey->GetNextKey(); ds@1952: } ds@1952: SubKey = data->GetFirstSubKey(); ds@1952: } ds@1952: ds@1952: pBitBuf->WriteString(name); ds@1952: pBitBuf->WriteByte((show) ? 1 : 0); ds@1952: pBitBuf->WriteByte(count); ds@1952: while (SubKey) ds@1952: { ds@1952: pBitBuf->WriteString(SubKey->GetName()); ds@1952: pBitBuf->WriteString(SubKey->GetString()); ds@1952: SubKey = SubKey->GetNextKey(); ds@1952: } ds@1952: g_UserMsgs.EndMessage(); ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd) ds@1952: { ds@1952: DelayedFakeCliCmd *pFake; ds@1952: ds@1952: if (m_FreeCmds.empty()) ds@1952: { ds@1952: pFake = new DelayedFakeCliCmd; ds@1952: } else { ds@1952: pFake = m_FreeCmds.front(); ds@1952: m_FreeCmds.pop(); ds@1952: } ds@1952: ds@1952: pFake->client = client; ds@1952: pFake->userid = userid; ds@1952: pFake->cmd.assign(cmd); ds@1952: ds@1952: m_CmdQueue.push(pFake); ds@1952: } ds@1952: ds@1952: void CHalfLife2::ProcessFakeCliCmdQueue() ds@1952: { ds@1952: while (!m_CmdQueue.empty()) ds@1952: { ds@1952: DelayedFakeCliCmd *pFake = m_CmdQueue.first(); ds@1952: ds@1952: if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client) ds@1952: { ds@1952: CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client); ds@1952: serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str()); ds@1952: } ds@1952: ds@1952: m_CmdQueue.pop(); ds@1952: } ds@1952: } ds@1952: ds@1952: bool CHalfLife2::IsLANServer() ds@1952: { ds@1952: sv_lan = icvar->FindVar("sv_lan"); ds@1952: ds@1952: if (!sv_lan) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return (sv_lan->GetInt() != 0); ds@1952: } ds@1952: ds@1952: bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID) ds@1952: { ds@1952: #if defined METAMOD_PLAPI_VERSION ds@1952: if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL) ds@1952: #else ds@1952: if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0) ds@1952: #endif ds@1952: { ds@1952: Assert(filesystem); ds@1952: #ifdef _MSC_VER ds@1952: Assert(_heapchk() == _HEAPOK); ds@1952: #endif ds@1952: ds@1952: FileHandle_t f = filesystem->Open(resourceName, "rb", pathID); ds@1952: if (!f) ds@1952: return false; ds@1952: ds@1952: // load file into a null-terminated buffer ds@1952: int fileSize = filesystem->Size(f); ds@1952: char *buffer = (char *)MemAllocScratch(fileSize + 1); ds@1952: ds@1952: Assert(buffer); ds@1952: ds@1952: filesystem->Read(buffer, fileSize, f); // read into local buffer ds@1952: ds@1952: buffer[fileSize] = 0; // null terminate file as EOF ds@1952: ds@1952: filesystem->Close( f ); // close file after reading ds@1952: ds@1952: bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem ); ds@1952: ds@1952: MemFreeScratch(); ds@1952: ds@1952: return retOK; ds@1952: } ds@1952: else ds@1952: { ds@1952: return kv->LoadFromFile(filesystem, resourceName, pathID); ds@1952: } ds@1952: } ds@1952: ds@1952: void CHalfLife2::PushCommandStack(const CCommand *cmd) ds@1952: { ds@1952: CachedCommandInfo info; ds@1952: ds@1952: info.args = cmd; ds@1952: #if !defined ORANGEBOX_BUILD ds@1952: strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd)); ds@1952: #endif ds@1952: ds@1952: m_CommandStack.push(info); ds@1952: } ds@1952: ds@1952: const CCommand *CHalfLife2::PeekCommandStack() ds@1952: { ds@1952: if (m_CommandStack.empty()) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: return m_CommandStack.front().args; ds@1952: } ds@1952: ds@1952: void CHalfLife2::PopCommandStack() ds@1952: { ds@1952: m_CommandStack.pop(); ds@1952: } ds@1952: ds@1952: const char *CHalfLife2::CurrentCommandName() ds@1952: { ds@1952: #if defined ORANGEBOX_BUILD ds@1952: return m_CommandStack.front().args->Arg(0); ds@1952: #else ds@1952: return m_CommandStack.front().cmd; ds@1952: #endif ds@1952: } dvander@2162: dvander@2162: void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg) dvander@2162: { dvander@2162: DelayedKickInfo kick; dvander@2162: dvander@2162: kick.client = client; dvander@2162: kick.userid = userid; dvander@2162: UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg); dvander@2162: dvander@2162: m_DelayedKicks.push(kick); dvander@2162: } dvander@2162: dvander@2162: void CHalfLife2::ProcessDelayedKicks() dvander@2162: { dvander@2162: while (!m_DelayedKicks.empty()) dvander@2162: { dvander@2162: DelayedKickInfo info = m_DelayedKicks.first(); dvander@2162: m_DelayedKicks.pop(); dvander@2162: dvander@2162: CPlayer *player = g_Players.GetPlayerByIndex(info.client); dvander@2162: if (player == NULL || player->GetUserId() != info.userid) dvander@2162: { dvander@2162: continue; dvander@2162: } dvander@2162: dvander@2162: player->Kick(info.buffer); dvander@2162: } dvander@2162: }