1.1 --- a/core/HalfLife2.cpp Fri Jul 11 06:51:21 2008 +0000
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,541 +0,0 @@
1.4 -/**
1.5 - * vim: set ts=4 :
1.6 - * =============================================================================
1.7 - * SourceMod
1.8 - * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 - * =============================================================================
1.10 - *
1.11 - * This program is free software; you can redistribute it and/or modify it under
1.12 - * the terms of the GNU General Public License, version 3.0, as published by the
1.13 - * Free Software Foundation.
1.14 - *
1.15 - * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 - * details.
1.19 - *
1.20 - * You should have received a copy of the GNU General Public License along with
1.21 - * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 - *
1.23 - * As a special exception, AlliedModders LLC gives you permission to link the
1.24 - * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 - * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 - * by the Valve Corporation. You must obey the GNU General Public License in
1.27 - * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 - * this exception to all derivative works. AlliedModders LLC defines further
1.29 - * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 - * or <http://www.sourcemod.net/license.php>.
1.31 - *
1.32 - * Version: $Id$
1.33 - */
1.34 -
1.35 -#include "HalfLife2.h"
1.36 -#include "sourcemod.h"
1.37 -#include "sourcemm_api.h"
1.38 -#include "UserMessages.h"
1.39 -#include "PlayerManager.h"
1.40 -#include "sm_stringutil.h"
1.41 -#include "GameConfigs.h"
1.42 -#include <compat_wrappers.h>
1.43 -
1.44 -CHalfLife2 g_HL2;
1.45 -ConVar *sv_lan = NULL;
1.46 -
1.47 -namespace SourceHook
1.48 -{
1.49 - template<>
1.50 - int HashFunction<datamap_t *>(datamap_t * const &k)
1.51 - {
1.52 - return reinterpret_cast<int>(k);
1.53 - }
1.54 -
1.55 - template<>
1.56 - int Compare<datamap_t *>(datamap_t * const &k1, datamap_t * const &k2)
1.57 - {
1.58 - return (k1-k2);
1.59 - }
1.60 -}
1.61 -
1.62 -CHalfLife2::CHalfLife2()
1.63 -{
1.64 - m_pClasses = sm_trie_create();
1.65 -}
1.66 -
1.67 -CHalfLife2::~CHalfLife2()
1.68 -{
1.69 - sm_trie_destroy(m_pClasses);
1.70 -
1.71 - List<DataTableInfo *>::iterator iter;
1.72 - DataTableInfo *pInfo;
1.73 - for (iter=m_Tables.begin(); iter!=m_Tables.end(); iter++)
1.74 - {
1.75 - pInfo = (*iter);
1.76 - delete pInfo;
1.77 - }
1.78 -
1.79 - m_Tables.clear();
1.80 -
1.81 - THash<datamap_t *, DataMapTrie>::iterator h_iter;
1.82 - for (h_iter=m_Maps.begin(); h_iter!=m_Maps.end(); h_iter++)
1.83 - {
1.84 - if (h_iter->val.trie)
1.85 - {
1.86 - sm_trie_destroy(h_iter->val.trie);
1.87 - h_iter->val.trie = NULL;
1.88 - }
1.89 - }
1.90 -
1.91 - m_Maps.clear();
1.92 -}
1.93 -
1.94 -CSharedEdictChangeInfo *g_pSharedChangeInfo = NULL;
1.95 -bool is_original_engine = false;
1.96 -
1.97 -void CHalfLife2::OnSourceModStartup(bool late)
1.98 -{
1.99 - /* The Ship currently is the only known game to use an older version of the engine */
1.100 -#if defined METAMOD_PLAPI_VERSION
1.101 - if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
1.102 -#else
1.103 - if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
1.104 -#endif
1.105 - {
1.106 - is_original_engine = true;
1.107 - }
1.108 - else if (g_pSharedChangeInfo == NULL)
1.109 - {
1.110 - g_pSharedChangeInfo = engine->GetSharedEdictChangeInfo();
1.111 - }
1.112 -}
1.113 -
1.114 -void CHalfLife2::OnSourceModAllInitialized()
1.115 -{
1.116 - m_MsgTextMsg = g_UserMsgs.GetMessageIndex("TextMsg");
1.117 - m_HinTextMsg = g_UserMsgs.GetMessageIndex("HintText");
1.118 - m_VGUIMenu = g_UserMsgs.GetMessageIndex("VGUIMenu");
1.119 - g_ShareSys.AddInterface(NULL, this);
1.120 -}
1.121 -
1.122 -#if !defined METAMOD_PLAPI_VERSION
1.123 -bool CHalfLife2::IsOriginalEngine()
1.124 -{
1.125 - return is_original_engine;
1.126 -}
1.127 -#endif
1.128 -
1.129 -IChangeInfoAccessor *CBaseEdict::GetChangeAccessor()
1.130 -{
1.131 - return engine->GetChangeAccessor( (const edict_t *)this );
1.132 -}
1.133 -
1.134 -bool UTIL_FindInSendTable(SendTable *pTable,
1.135 - const char *name,
1.136 - sm_sendprop_info_t *info,
1.137 - unsigned int offset)
1.138 -{
1.139 - const char *pname;
1.140 - int props = pTable->GetNumProps();
1.141 - SendProp *prop;
1.142 -
1.143 - for (int i=0; i<props; i++)
1.144 - {
1.145 - prop = pTable->GetProp(i);
1.146 - pname = prop->GetName();
1.147 - if (pname && strcmp(name, pname) == 0)
1.148 - {
1.149 - info->prop = prop;
1.150 - info->actual_offset = offset + info->prop->GetOffset();
1.151 - return true;
1.152 - }
1.153 - if (prop->GetDataTable())
1.154 - {
1.155 - if (UTIL_FindInSendTable(prop->GetDataTable(),
1.156 - name,
1.157 - info,
1.158 - offset + prop->GetOffset())
1.159 - )
1.160 - {
1.161 - return true;
1.162 - }
1.163 - }
1.164 - }
1.165 -
1.166 - return false;
1.167 -}
1.168 -
1.169 -typedescription_t *UTIL_FindInDataMap(datamap_t *pMap, const char *name)
1.170 -{
1.171 - while (pMap)
1.172 - {
1.173 - for (int i=0; i<pMap->dataNumFields; i++)
1.174 - {
1.175 - if (pMap->dataDesc[i].fieldName == NULL)
1.176 - {
1.177 - continue;
1.178 - }
1.179 - if (strcmp(name, pMap->dataDesc[i].fieldName) == 0)
1.180 - {
1.181 - return &(pMap->dataDesc[i]);
1.182 - }
1.183 - if (pMap->dataDesc[i].td)
1.184 - {
1.185 - typedescription_t *_td;
1.186 - if ((_td=UTIL_FindInDataMap(pMap->dataDesc[i].td, name)) != NULL)
1.187 - {
1.188 - return _td;
1.189 - }
1.190 - }
1.191 - }
1.192 - pMap = pMap->baseMap;
1.193 - }
1.194 -
1.195 - return NULL;
1.196 -}
1.197 -
1.198 -ServerClass *CHalfLife2::FindServerClass(const char *classname)
1.199 -{
1.200 - DataTableInfo *pInfo = _FindServerClass(classname);
1.201 -
1.202 - if (!pInfo)
1.203 - {
1.204 - return NULL;
1.205 - }
1.206 -
1.207 - return pInfo->sc;
1.208 -}
1.209 -
1.210 -DataTableInfo *CHalfLife2::_FindServerClass(const char *classname)
1.211 -{
1.212 - DataTableInfo *pInfo = NULL;
1.213 -
1.214 - if (!sm_trie_retrieve(m_pClasses, classname, (void **)&pInfo))
1.215 - {
1.216 - ServerClass *sc = gamedll->GetAllServerClasses();
1.217 - while (sc)
1.218 - {
1.219 - if (strcmp(classname, sc->GetName()) == 0)
1.220 - {
1.221 - pInfo = new DataTableInfo;
1.222 - pInfo->sc = sc;
1.223 - sm_trie_insert(m_pClasses, classname, pInfo);
1.224 - m_Tables.push_back(pInfo);
1.225 - break;
1.226 - }
1.227 - sc = sc->m_pNext;
1.228 - }
1.229 - if (!pInfo)
1.230 - {
1.231 - return NULL;
1.232 - }
1.233 - }
1.234 -
1.235 - return pInfo;
1.236 -}
1.237 -
1.238 -bool CHalfLife2::FindSendPropInfo(const char *classname, const char *offset, sm_sendprop_info_t *info)
1.239 -{
1.240 - DataTableInfo *pInfo;
1.241 - sm_sendprop_info_t *prop;
1.242 -
1.243 - if ((pInfo = _FindServerClass(classname)) == NULL)
1.244 - {
1.245 - return false;
1.246 - }
1.247 -
1.248 - if ((prop = pInfo->lookup.retrieve(offset)) == NULL)
1.249 - {
1.250 - sm_sendprop_info_t temp_info;
1.251 -
1.252 - if (!UTIL_FindInSendTable(pInfo->sc->m_pTable, offset, &temp_info, 0))
1.253 - {
1.254 - return false;
1.255 - }
1.256 -
1.257 - pInfo->lookup.insert(offset, temp_info);
1.258 - *info = temp_info;
1.259 - }
1.260 - else
1.261 - {
1.262 - *info = *prop;
1.263 - }
1.264 -
1.265 - return true;
1.266 -}
1.267 -
1.268 -SendProp *CHalfLife2::FindInSendTable(const char *classname, const char *offset)
1.269 -{
1.270 - sm_sendprop_info_t info;
1.271 -
1.272 - if (!FindSendPropInfo(classname, offset, &info))
1.273 - {
1.274 - return NULL;
1.275 - }
1.276 -
1.277 - return info.prop;
1.278 -}
1.279 -
1.280 -typedescription_t *CHalfLife2::FindInDataMap(datamap_t *pMap, const char *offset)
1.281 -{
1.282 - typedescription_t *td = NULL;
1.283 - DataMapTrie &val = m_Maps[pMap];
1.284 -
1.285 - if (!val.trie)
1.286 - {
1.287 - val.trie = sm_trie_create();
1.288 - }
1.289 - if (!sm_trie_retrieve(val.trie, offset, (void **)&td))
1.290 - {
1.291 - if ((td = UTIL_FindInDataMap(pMap, offset)) != NULL)
1.292 - {
1.293 - sm_trie_insert(val.trie, offset, td);
1.294 - }
1.295 - }
1.296 -
1.297 - return td;
1.298 -}
1.299 -
1.300 -void CHalfLife2::SetEdictStateChanged(edict_t *pEdict, unsigned short offset)
1.301 -{
1.302 - if (g_pSharedChangeInfo != NULL)
1.303 - {
1.304 - if (offset)
1.305 - {
1.306 - pEdict->StateChanged(offset);
1.307 - }
1.308 - else
1.309 - {
1.310 - pEdict->StateChanged();
1.311 - }
1.312 - }
1.313 - else
1.314 - {
1.315 - pEdict->m_fStateFlags |= FL_EDICT_CHANGED;
1.316 - }
1.317 -}
1.318 -
1.319 -bool CHalfLife2::TextMsg(int client, int dest, const char *msg)
1.320 -{
1.321 - bf_write *pBitBuf = NULL;
1.322 - cell_t players[] = {client};
1.323 -
1.324 - if ((pBitBuf = g_UserMsgs.StartMessage(m_MsgTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
1.325 - {
1.326 - return false;
1.327 - }
1.328 -
1.329 - pBitBuf->WriteByte(dest);
1.330 - pBitBuf->WriteString(msg);
1.331 - g_UserMsgs.EndMessage();
1.332 -
1.333 - return true;
1.334 -}
1.335 -
1.336 -bool CHalfLife2::HintTextMsg(int client, const char *msg)
1.337 -{
1.338 - bf_write *pBitBuf = NULL;
1.339 - cell_t players[] = {client};
1.340 -
1.341 - if ((pBitBuf = g_UserMsgs.StartMessage(m_HinTextMsg, players, 1, USERMSG_RELIABLE)) == NULL)
1.342 - {
1.343 - return false;
1.344 - }
1.345 -
1.346 - const char *pre_byte = g_pGameConf->GetKeyValue("HintTextPreByte");
1.347 - if (pre_byte != NULL && strcmp(pre_byte, "yes") == 0)
1.348 - {
1.349 - pBitBuf->WriteByte(1);
1.350 - }
1.351 - pBitBuf->WriteString(msg);
1.352 - g_UserMsgs.EndMessage();
1.353 -
1.354 - return true;
1.355 -}
1.356 -
1.357 -bool CHalfLife2::ShowVGUIMenu(int client, const char *name, KeyValues *data, bool show)
1.358 -{
1.359 - bf_write *pBitBuf = NULL;
1.360 - KeyValues *SubKey = NULL;
1.361 - int count = 0;
1.362 - cell_t players[] = {client};
1.363 -
1.364 - if ((pBitBuf = g_UserMsgs.StartMessage(m_VGUIMenu, players, 1, USERMSG_RELIABLE)) == NULL)
1.365 - {
1.366 - return false;
1.367 - }
1.368 -
1.369 - if (data)
1.370 - {
1.371 - SubKey = data->GetFirstSubKey();
1.372 - while (SubKey)
1.373 - {
1.374 - count++;
1.375 - SubKey = SubKey->GetNextKey();
1.376 - }
1.377 - SubKey = data->GetFirstSubKey();
1.378 - }
1.379 -
1.380 - pBitBuf->WriteString(name);
1.381 - pBitBuf->WriteByte((show) ? 1 : 0);
1.382 - pBitBuf->WriteByte(count);
1.383 - while (SubKey)
1.384 - {
1.385 - pBitBuf->WriteString(SubKey->GetName());
1.386 - pBitBuf->WriteString(SubKey->GetString());
1.387 - SubKey = SubKey->GetNextKey();
1.388 - }
1.389 - g_UserMsgs.EndMessage();
1.390 -
1.391 - return true;
1.392 -}
1.393 -
1.394 -void CHalfLife2::AddToFakeCliCmdQueue(int client, int userid, const char *cmd)
1.395 -{
1.396 - DelayedFakeCliCmd *pFake;
1.397 -
1.398 - if (m_FreeCmds.empty())
1.399 - {
1.400 - pFake = new DelayedFakeCliCmd;
1.401 - } else {
1.402 - pFake = m_FreeCmds.front();
1.403 - m_FreeCmds.pop();
1.404 - }
1.405 -
1.406 - pFake->client = client;
1.407 - pFake->userid = userid;
1.408 - pFake->cmd.assign(cmd);
1.409 -
1.410 - m_CmdQueue.push(pFake);
1.411 -}
1.412 -
1.413 -void CHalfLife2::ProcessFakeCliCmdQueue()
1.414 -{
1.415 - while (!m_CmdQueue.empty())
1.416 - {
1.417 - DelayedFakeCliCmd *pFake = m_CmdQueue.first();
1.418 -
1.419 - if (g_Players.GetClientOfUserId(pFake->userid) == pFake->client)
1.420 - {
1.421 - CPlayer *pPlayer = g_Players.GetPlayerByIndex(pFake->client);
1.422 - serverpluginhelpers->ClientCommand(pPlayer->GetEdict(), pFake->cmd.c_str());
1.423 - }
1.424 -
1.425 - m_CmdQueue.pop();
1.426 - }
1.427 -}
1.428 -
1.429 -bool CHalfLife2::IsLANServer()
1.430 -{
1.431 - sv_lan = icvar->FindVar("sv_lan");
1.432 -
1.433 - if (!sv_lan)
1.434 - {
1.435 - return false;
1.436 - }
1.437 -
1.438 - return (sv_lan->GetInt() != 0);
1.439 -}
1.440 -
1.441 -bool CHalfLife2::KVLoadFromFile(KeyValues *kv, IBaseFileSystem *filesystem, const char *resourceName, const char *pathID)
1.442 -{
1.443 -#if defined METAMOD_PLAPI_VERSION
1.444 - if (g_SMAPI->GetSourceEngineBuild() == SOURCE_ENGINE_ORIGINAL)
1.445 -#else
1.446 - if (strcasecmp(g_SourceMod.GetGameFolderName(), "ship") == 0)
1.447 -#endif
1.448 - {
1.449 - Assert(filesystem);
1.450 -#ifdef _MSC_VER
1.451 - Assert(_heapchk() == _HEAPOK);
1.452 -#endif
1.453 -
1.454 - FileHandle_t f = filesystem->Open(resourceName, "rb", pathID);
1.455 - if (!f)
1.456 - return false;
1.457 -
1.458 - // load file into a null-terminated buffer
1.459 - int fileSize = filesystem->Size(f);
1.460 - char *buffer = (char *)MemAllocScratch(fileSize + 1);
1.461 -
1.462 - Assert(buffer);
1.463 -
1.464 - filesystem->Read(buffer, fileSize, f); // read into local buffer
1.465 -
1.466 - buffer[fileSize] = 0; // null terminate file as EOF
1.467 -
1.468 - filesystem->Close( f ); // close file after reading
1.469 -
1.470 - bool retOK = kv->LoadFromBuffer( resourceName, buffer, filesystem );
1.471 -
1.472 - MemFreeScratch();
1.473 -
1.474 - return retOK;
1.475 - }
1.476 - else
1.477 - {
1.478 - return kv->LoadFromFile(filesystem, resourceName, pathID);
1.479 - }
1.480 -}
1.481 -
1.482 -void CHalfLife2::PushCommandStack(const CCommand *cmd)
1.483 -{
1.484 - CachedCommandInfo info;
1.485 -
1.486 - info.args = cmd;
1.487 -#if !defined ORANGEBOX_BUILD
1.488 - strncopy(info.cmd, cmd->Arg(0), sizeof(info.cmd));
1.489 -#endif
1.490 -
1.491 - m_CommandStack.push(info);
1.492 -}
1.493 -
1.494 -const CCommand *CHalfLife2::PeekCommandStack()
1.495 -{
1.496 - if (m_CommandStack.empty())
1.497 - {
1.498 - return NULL;
1.499 - }
1.500 -
1.501 - return m_CommandStack.front().args;
1.502 -}
1.503 -
1.504 -void CHalfLife2::PopCommandStack()
1.505 -{
1.506 - m_CommandStack.pop();
1.507 -}
1.508 -
1.509 -const char *CHalfLife2::CurrentCommandName()
1.510 -{
1.511 -#if defined ORANGEBOX_BUILD
1.512 - return m_CommandStack.front().args->Arg(0);
1.513 -#else
1.514 - return m_CommandStack.front().cmd;
1.515 -#endif
1.516 -}
1.517 -
1.518 -void CHalfLife2::AddDelayedKick(int client, int userid, const char *msg)
1.519 -{
1.520 - DelayedKickInfo kick;
1.521 -
1.522 - kick.client = client;
1.523 - kick.userid = userid;
1.524 - UTIL_Format(kick.buffer, sizeof(kick.buffer), "%s", msg);
1.525 -
1.526 - m_DelayedKicks.push(kick);
1.527 -}
1.528 -
1.529 -void CHalfLife2::ProcessDelayedKicks()
1.530 -{
1.531 - while (!m_DelayedKicks.empty())
1.532 - {
1.533 - DelayedKickInfo info = m_DelayedKicks.first();
1.534 - m_DelayedKicks.pop();
1.535 -
1.536 - CPlayer *player = g_Players.GetPlayerByIndex(info.client);
1.537 - if (player == NULL || player->GetUserId() != info.userid)
1.538 - {
1.539 - continue;
1.540 - }
1.541 -
1.542 - player->Kick(info.buffer);
1.543 - }
1.544 -}