1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/logic/ExtensionSys.cpp Fri Mar 29 11:37:29 2013 -0700
1.3 @@ -0,0 +1,1383 @@
1.4 +/**
1.5 + * vim: set ts=4 sw=4 tw=99 noet :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2010 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 <stdlib.h>
1.36 +#include "ExtensionSys.h"
1.37 +#include <ILibrarySys.h>
1.38 +#include <ISourceMod.h>
1.39 +#include "common_logic.h"
1.40 +#include "PluginSys.h"
1.41 +
1.42 +CExtensionManager g_Extensions;
1.43 +IdentityType_t g_ExtType;
1.44 +
1.45 +void CExtension::Initialize(const char *filename, const char *path)
1.46 +{
1.47 + m_pAPI = NULL;
1.48 + m_pIdentToken = NULL;
1.49 + unload_code = 0;
1.50 + m_bFullyLoaded = false;
1.51 + m_File.assign(filename);
1.52 + m_Path.assign(path);
1.53 + char real_name[PLATFORM_MAX_PATH];
1.54 + libsys->GetFileFromPath(real_name, sizeof(real_name), m_Path.c_str());
1.55 + m_RealFile.assign(real_name);
1.56 +}
1.57 +
1.58 +CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
1.59 +{
1.60 + Initialize(filename, path);
1.61 + m_pAPI = pAPI;
1.62 +}
1.63 +
1.64 +#if defined METAMOD_PLAPI_VERSION
1.65 +#if SOURCE_ENGINE == SE_LEFT4DEAD
1.66 +#define GAMEFIX "2.l4d"
1.67 +#elif SOURCE_ENGINE == SE_LEFT4DEAD2
1.68 +#define GAMEFIX "2.l4d2"
1.69 +#elif SOURCE_ENGINE == SE_ALIENSWARM
1.70 +#define GAMEFIX "2.swarm"
1.71 +#elif SOURCE_ENGINE == SE_ORANGEBOX
1.72 +#define GAMEFIX "2.ep2"
1.73 +#elif SOURCE_ENGINE == SE_BLOODYGOODTIME
1.74 +#define GAMEFIX "2.bgt"
1.75 +#elif SOURCE_ENGINE == SE_EYE
1.76 +#define GAMEFIX "2.eye"
1.77 +#elif SOURCE_ENGINE == SE_CSS
1.78 +#define GAMEFIX "2.css"
1.79 +#elif SOURCE_ENGINE == SE_ORANGEBOXVALVE
1.80 +#define GAMEFIX "2.ep2v"
1.81 +#elif SOURCE_ENGINE == SE_DARKMESSIAH
1.82 +#define GAMEFIX "2.darkm"
1.83 +#elif SOURCE_ENGINE == SE_PORTAL2
1.84 +#define GAMEFIX "2.portal2"
1.85 +#elif SOURCE_ENGINE == SE_CSGO
1.86 +#define GAMEFIX "2.csgo"
1.87 +#elif SOURCE_ENGINE == SE_DOTA
1.88 +#define GAMEFIX "2.dota"
1.89 +#else
1.90 +#define GAMEFIX "2.ep1"
1.91 +#endif //(SOURCE_ENGINE == SE_LEFT4DEAD) || (SOURCE_ENGINE == SE_LEFT4DEAD2)
1.92 +#else //METAMOD_PLAPI_VERSION
1.93 +#define GAMEFIX "1.ep1"
1.94 +#endif //METAMOD_PLAPI_VERSION
1.95 +
1.96 +CLocalExtension::CLocalExtension(const char *filename)
1.97 +{
1.98 + m_PlId = 0;
1.99 + m_pLib = NULL;
1.100 +
1.101 + char path[PLATFORM_MAX_PATH];
1.102 +
1.103 + /* Special case for new bintools binary */
1.104 + if (strcmp(filename, "bintools.ext") == 0)
1.105 + {
1.106 + goto normal;
1.107 + }
1.108 +
1.109 + /* Zeroth, see if there is an engine specific build in the new place. */
1.110 + g_pSM->BuildPath(Path_SM,
1.111 + path,
1.112 + PLATFORM_MAX_PATH,
1.113 + "extensions/%s." GAMEFIX "." PLATFORM_LIB_EXT,
1.114 + filename);
1.115 +
1.116 + if (libsys->IsPathFile(path))
1.117 + {
1.118 + goto found;
1.119 + }
1.120 +
1.121 + /* First see if there is an engine specific build! */
1.122 + g_pSM->BuildPath(Path_SM,
1.123 + path,
1.124 + PLATFORM_MAX_PATH,
1.125 + "extensions/auto." GAMEFIX "/%s." PLATFORM_LIB_EXT,
1.126 + filename);
1.127 +
1.128 + /* Try the "normal" version */
1.129 + if (!libsys->IsPathFile(path))
1.130 + {
1.131 + normal:
1.132 + g_pSM->BuildPath(Path_SM,
1.133 + path,
1.134 + PLATFORM_MAX_PATH,
1.135 + "extensions/%s." PLATFORM_LIB_EXT,
1.136 + filename);
1.137 + }
1.138 +
1.139 +found:
1.140 + Initialize(filename, path);
1.141 +}
1.142 +
1.143 +bool CRemoteExtension::Load(char *error, size_t maxlength)
1.144 +{
1.145 + if (!PerformAPICheck(error, maxlength))
1.146 + {
1.147 + m_pAPI = NULL;
1.148 + return false;
1.149 + }
1.150 +
1.151 + if (!CExtension::Load(error, maxlength))
1.152 + {
1.153 + m_pAPI = NULL;
1.154 + return false;
1.155 + }
1.156 +
1.157 + return true;
1.158 +}
1.159 +
1.160 +bool CLocalExtension::Load(char *error, size_t maxlength)
1.161 +{
1.162 + m_pLib = libsys->OpenLibrary(m_Path.c_str(), error, maxlength);
1.163 +
1.164 + if (m_pLib == NULL)
1.165 + {
1.166 + return false;
1.167 + }
1.168 +
1.169 + typedef IExtensionInterface *(*GETAPI)();
1.170 + GETAPI pfnGetAPI = NULL;
1.171 +
1.172 + if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
1.173 + {
1.174 + m_pLib->CloseLibrary();
1.175 + m_pLib = NULL;
1.176 + snprintf(error, maxlength, "Unable to find extension entry point");
1.177 + return false;
1.178 + }
1.179 +
1.180 + m_pAPI = pfnGetAPI();
1.181 +
1.182 + /* Check pointer and version */
1.183 + if (!PerformAPICheck(error, maxlength))
1.184 + {
1.185 + m_pLib->CloseLibrary();
1.186 + m_pLib = NULL;
1.187 + m_pAPI = NULL;
1.188 + return false;
1.189 + }
1.190 +
1.191 + /* Load as MM:S */
1.192 + if (m_pAPI->IsMetamodExtension())
1.193 + {
1.194 + bool ok;
1.195 + m_PlId = smcore.LoadMMSPlugin(m_Path.c_str(), &ok, error, maxlength);
1.196 +
1.197 + if (!m_PlId || !ok)
1.198 + {
1.199 + m_pLib->CloseLibrary();
1.200 + m_pLib = NULL;
1.201 + m_pAPI = NULL;
1.202 + return false;
1.203 + }
1.204 + }
1.205 +
1.206 + if (!CExtension::Load(error, maxlength))
1.207 + {
1.208 + if (m_pAPI->IsMetamodExtension())
1.209 + {
1.210 + if (m_PlId)
1.211 + {
1.212 + smcore.UnloadMMSPlugin(m_PlId);
1.213 + m_PlId = 0;
1.214 + }
1.215 + }
1.216 + m_pLib->CloseLibrary();
1.217 + m_pLib = NULL;
1.218 + m_pAPI = NULL;
1.219 + return false;
1.220 + }
1.221 +
1.222 + return true;
1.223 +}
1.224 +
1.225 +void CRemoteExtension::Unload()
1.226 +{
1.227 +}
1.228 +
1.229 +void CLocalExtension::Unload()
1.230 +{
1.231 + if (m_pAPI != NULL && m_PlId)
1.232 + {
1.233 + smcore.UnloadMMSPlugin(m_PlId);
1.234 + m_PlId = 0;
1.235 + }
1.236 +
1.237 + if (m_pLib != NULL)
1.238 + {
1.239 + m_pLib->CloseLibrary();
1.240 + m_pLib = NULL;
1.241 + }
1.242 +}
1.243 +
1.244 +bool CRemoteExtension::Reload(char *error, size_t maxlength)
1.245 +{
1.246 + snprintf(error, maxlength, "Remote extensions do not support reloading");
1.247 + return false;
1.248 +}
1.249 +
1.250 +bool CLocalExtension::Reload(char *error, size_t maxlength)
1.251 +{
1.252 + if (m_pLib == NULL) // FIXME: just load it instead?
1.253 + return false;
1.254 +
1.255 + m_pAPI->OnExtensionUnload();
1.256 + Unload();
1.257 +
1.258 + return Load(error, maxlength);
1.259 +}
1.260 +
1.261 +bool CRemoteExtension::IsExternal()
1.262 +{
1.263 + return true;
1.264 +}
1.265 +
1.266 +bool CLocalExtension::IsExternal()
1.267 +{
1.268 + return false;
1.269 +}
1.270 +
1.271 +CExtension::~CExtension()
1.272 +{
1.273 + DestroyIdentity();
1.274 +}
1.275 +
1.276 +bool CExtension::PerformAPICheck(char *error, size_t maxlength)
1.277 +{
1.278 + if (!m_pAPI)
1.279 + {
1.280 + snprintf(error, maxlength, "No IExtensionInterface instance provided");
1.281 + return false;
1.282 + }
1.283 +
1.284 + if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
1.285 + {
1.286 + snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
1.287 + return false;
1.288 + }
1.289 +
1.290 + return true;
1.291 +}
1.292 +
1.293 +bool CExtension::Load(char *error, size_t maxlength)
1.294 +{
1.295 + CreateIdentity();
1.296 + if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !smcore.IsMapLoading()))
1.297 + {
1.298 + DestroyIdentity();
1.299 + return false;
1.300 + }
1.301 + else
1.302 + {
1.303 + /* Check if we're past load time */
1.304 + if (!smcore.IsMapLoading())
1.305 + {
1.306 + m_pAPI->OnExtensionsAllLoaded();
1.307 + }
1.308 + }
1.309 +
1.310 + return true;
1.311 +}
1.312 +
1.313 +void CExtension::CreateIdentity()
1.314 +{
1.315 + if (m_pIdentToken != NULL)
1.316 + {
1.317 + return;
1.318 + }
1.319 +
1.320 + m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
1.321 +}
1.322 +
1.323 +void CExtension::DestroyIdentity()
1.324 +{
1.325 + if (m_pIdentToken == NULL)
1.326 + {
1.327 + return;
1.328 + }
1.329 +
1.330 + g_ShareSys.DestroyIdentity(m_pIdentToken);
1.331 + m_pIdentToken = NULL;
1.332 +}
1.333 +
1.334 +void CExtension::MarkAllLoaded()
1.335 +{
1.336 + assert(m_bFullyLoaded == false);
1.337 + if (!m_bFullyLoaded)
1.338 + {
1.339 + m_bFullyLoaded = true;
1.340 + m_pAPI->OnExtensionsAllLoaded();
1.341 + }
1.342 +}
1.343 +
1.344 +void CExtension::AddPlugin(CPlugin *pPlugin)
1.345 +{
1.346 + /* Unfortunately we have to do this :( */
1.347 + if (m_Dependents.find(pPlugin) != m_Dependents.end())
1.348 + {
1.349 + m_Dependents.push_back(pPlugin);
1.350 + }
1.351 +}
1.352 +
1.353 +void CExtension::SetError(const char *error)
1.354 +{
1.355 + m_Error.assign(error);
1.356 +}
1.357 +
1.358 +IExtensionInterface *CExtension::GetAPI()
1.359 +{
1.360 + return m_pAPI;
1.361 +}
1.362 +
1.363 +const char *CExtension::GetFilename()
1.364 +{
1.365 + return m_RealFile.c_str();
1.366 +}
1.367 +
1.368 +const char *CExtension::GetPath() const
1.369 +{
1.370 + return m_Path.c_str();
1.371 +}
1.372 +
1.373 +IdentityToken_t *CExtension::GetIdentity()
1.374 +{
1.375 + return m_pIdentToken;
1.376 +}
1.377 +
1.378 +bool CRemoteExtension::IsLoaded()
1.379 +{
1.380 + return (m_pAPI != NULL);
1.381 +}
1.382 +
1.383 +bool CLocalExtension::IsLoaded()
1.384 +{
1.385 + return (m_pLib != NULL);
1.386 +}
1.387 +
1.388 +void CExtension::AddDependency(const IfaceInfo *pInfo)
1.389 +{
1.390 + if (m_Deps.find(*pInfo) == m_Deps.end())
1.391 + {
1.392 + m_Deps.push_back(*pInfo);
1.393 + }
1.394 +}
1.395 +
1.396 +bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
1.397 +{
1.398 + return (i1.iface == i2.iface) && (i1.owner == i2.owner);
1.399 +}
1.400 +
1.401 +void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
1.402 +{
1.403 + IfaceInfo info;
1.404 + info.iface = iface;
1.405 + info.owner = pOther;
1.406 +
1.407 + List<IfaceInfo>::iterator iter;
1.408 + for (iter = m_ChildDeps.begin();
1.409 + iter != m_ChildDeps.end();
1.410 + iter++)
1.411 + {
1.412 + IfaceInfo &other = (*iter);
1.413 + if (other == info)
1.414 + {
1.415 + return;
1.416 + }
1.417 + }
1.418 +
1.419 + m_ChildDeps.push_back(info);
1.420 +}
1.421 +
1.422 +ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
1.423 +{
1.424 + List<IfaceInfo>::iterator iter = m_Deps.begin();
1.425 +
1.426 + if (iter == m_Deps.end())
1.427 + {
1.428 + return NULL;
1.429 + }
1.430 +
1.431 + if (pOwner)
1.432 + {
1.433 + *pOwner = (*iter).owner;
1.434 + }
1.435 + if (pInterface)
1.436 + {
1.437 + *pInterface = (*iter).iface;
1.438 + }
1.439 +
1.440 + List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
1.441 +
1.442 + return (ITERATOR *)pIter;
1.443 +}
1.444 +
1.445 +bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
1.446 +{
1.447 + List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
1.448 + List<IfaceInfo>::iterator _iter;
1.449 +
1.450 + if (_iter == m_Deps.end())
1.451 + {
1.452 + return false;
1.453 + }
1.454 +
1.455 + _iter++;
1.456 +
1.457 + if (pOwner)
1.458 + {
1.459 + *pOwner = (*_iter).owner;
1.460 + }
1.461 + if (pInterface)
1.462 + {
1.463 + *pInterface = (*_iter).iface;
1.464 + }
1.465 +
1.466 + *pIter = _iter;
1.467 +
1.468 + if (_iter == m_Deps.end())
1.469 + {
1.470 + return false;
1.471 + }
1.472 +
1.473 + return true;
1.474 +}
1.475 +
1.476 +void CExtension::FreeDependencyIterator(ITERATOR *iter)
1.477 +{
1.478 + List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
1.479 +
1.480 + delete pIter;
1.481 +}
1.482 +
1.483 +void CExtension::AddInterface(SMInterface *pInterface)
1.484 +{
1.485 + m_Interfaces.push_back(pInterface);
1.486 +}
1.487 +
1.488 +bool CExtension::IsRunning(char *error, size_t maxlength)
1.489 +{
1.490 + if (!IsLoaded())
1.491 + {
1.492 + if (error)
1.493 + {
1.494 + snprintf(error, maxlength, "%s", m_Error.c_str());
1.495 + }
1.496 + return false;
1.497 + }
1.498 +
1.499 + return m_pAPI->QueryRunning(error, maxlength);
1.500 +}
1.501 +
1.502 +void CExtension::AddLibrary(const char *library)
1.503 +{
1.504 + m_Libraries.push_back(library);
1.505 +}
1.506 +
1.507 +/*********************
1.508 + * EXTENSION MANAGER *
1.509 + *********************/
1.510 +
1.511 +void CExtensionManager::OnSourceModAllInitialized()
1.512 +{
1.513 + g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
1.514 + pluginsys->AddPluginsListener(this);
1.515 + g_ShareSys.AddInterface(NULL, this);
1.516 +}
1.517 +
1.518 +void CExtensionManager::OnSourceModShutdown()
1.519 +{
1.520 + pluginsys->RemovePluginsListener(this);
1.521 + g_ShareSys.DestroyIdentType(g_ExtType);
1.522 +}
1.523 +
1.524 +void CExtensionManager::Shutdown()
1.525 +{
1.526 + List<CExtension *>::iterator iter;
1.527 +
1.528 + while ((iter = m_Libs.begin()) != m_Libs.end())
1.529 + {
1.530 + UnloadExtension((*iter));
1.531 + }
1.532 +}
1.533 +
1.534 +void CExtensionManager::TryAutoload()
1.535 +{
1.536 + char path[PLATFORM_MAX_PATH];
1.537 +
1.538 + g_pSM->BuildPath(Path_SM, path, sizeof(path), "extensions");
1.539 +
1.540 + IDirectory *pDir = libsys->OpenDirectory(path);
1.541 + if (!pDir)
1.542 + {
1.543 + return;
1.544 + }
1.545 +
1.546 + const char *lfile;
1.547 + size_t len;
1.548 + while (pDir->MoreFiles())
1.549 + {
1.550 + if (pDir->IsEntryDirectory())
1.551 + {
1.552 + pDir->NextEntry();
1.553 + continue;
1.554 + }
1.555 +
1.556 + lfile = pDir->GetEntryName();
1.557 + len = strlen(lfile);
1.558 + if (len <= 9) /* size of ".autoload" */
1.559 + {
1.560 + pDir->NextEntry();
1.561 + continue;
1.562 + }
1.563 +
1.564 + if (strcmp(&lfile[len - 9], ".autoload") != 0)
1.565 + {
1.566 + pDir->NextEntry();
1.567 + continue;
1.568 + }
1.569 +
1.570 + char file[PLATFORM_MAX_PATH];
1.571 + len = smcore.Format(file, sizeof(file), "%s", lfile);
1.572 + strcpy(&file[len - 9], ".ext");
1.573 +
1.574 + LoadAutoExtension(file);
1.575 +
1.576 + pDir->NextEntry();
1.577 + }
1.578 +}
1.579 +
1.580 +IExtension *CExtensionManager::LoadAutoExtension(const char *path)
1.581 +{
1.582 + /* Remove platform extension if it's there. Compat hack. */
1.583 + const char *ext = libsys->GetFileExtension(path);
1.584 + if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
1.585 + {
1.586 + char path2[PLATFORM_MAX_PATH];
1.587 + smcore.Format(path2, sizeof(path2), "%s", path);
1.588 + path2[strlen(path) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
1.589 + return LoadAutoExtension(path);
1.590 + }
1.591 +
1.592 + IExtension *pAlready;
1.593 + if ((pAlready=FindExtensionByFile(path)) != NULL)
1.594 + {
1.595 + return pAlready;
1.596 + }
1.597 +
1.598 + char error[256];
1.599 + CExtension *p = new CLocalExtension(path);
1.600 +
1.601 + /* We put us in the list beforehand so extensions that check for each other
1.602 + * won't recursively load each other.
1.603 + */
1.604 + m_Libs.push_back(p);
1.605 +
1.606 + if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
1.607 + {
1.608 + smcore.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
1.609 + p->SetError(error);
1.610 + }
1.611 +
1.612 + return p;
1.613 +}
1.614 +
1.615 +IExtension *CExtensionManager::FindExtensionByFile(const char *file)
1.616 +{
1.617 + List<CExtension *>::iterator iter;
1.618 + CExtension *pExt;
1.619 +
1.620 + /* Chomp off the path */
1.621 + char lookup[PLATFORM_MAX_PATH];
1.622 + libsys->GetFileFromPath(lookup, sizeof(lookup), file);
1.623 +
1.624 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1.625 + {
1.626 + pExt = (*iter);
1.627 + if (pExt->IsSameFile(lookup))
1.628 + {
1.629 + return pExt;
1.630 + }
1.631 + }
1.632 +
1.633 + return NULL;
1.634 +}
1.635 +
1.636 +IExtension *CExtensionManager::FindExtensionByName(const char *ext)
1.637 +{
1.638 + List<CExtension *>::iterator iter;
1.639 + CExtension *pExt;
1.640 + IExtensionInterface *pAPI;
1.641 + const char *name;
1.642 +
1.643 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1.644 + {
1.645 + pExt = (*iter);
1.646 + if (!pExt->IsLoaded())
1.647 + {
1.648 + continue;
1.649 + }
1.650 + if ((pAPI = pExt->GetAPI()) == NULL)
1.651 + {
1.652 + continue;
1.653 + }
1.654 + name = pAPI->GetExtensionName();
1.655 + if (!name)
1.656 + {
1.657 + continue;
1.658 + }
1.659 + if (strcmp(name, ext) == 0)
1.660 + {
1.661 + return pExt;
1.662 + }
1.663 + }
1.664 +
1.665 + return NULL;
1.666 +}
1.667 +
1.668 +IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
1.669 +{
1.670 + /* Remove platform extension if it's there. Compat hack. */
1.671 + const char *ext = libsys->GetFileExtension(file);
1.672 + if (strcmp(ext, PLATFORM_LIB_EXT) == 0)
1.673 + {
1.674 + char path2[PLATFORM_MAX_PATH];
1.675 + smcore.Format(path2, sizeof(path2), "%s", file);
1.676 + path2[strlen(file) - strlen(PLATFORM_LIB_EXT) - 1] = '\0';
1.677 + return LoadExtension(path2, error, maxlength);
1.678 + }
1.679 +
1.680 + IExtension *pAlready;
1.681 + if ((pAlready=FindExtensionByFile(file)) != NULL)
1.682 + {
1.683 + return pAlready;
1.684 + }
1.685 +
1.686 + CExtension *pExt = new CLocalExtension(file);
1.687 +
1.688 + if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1.689 + {
1.690 + pExt->Unload();
1.691 + delete pExt;
1.692 + return NULL;
1.693 + }
1.694 +
1.695 + /* :TODO: do QueryRunning check if the map is loaded */
1.696 +
1.697 + m_Libs.push_back(pExt);
1.698 +
1.699 + return pExt;
1.700 +}
1.701 +
1.702 +void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
1.703 +{
1.704 + CExtension *pExt = (CExtension *)pRequester;
1.705 + CExtension *pOwner = (CExtension *)pInfo->owner;
1.706 +
1.707 + pExt->AddDependency(pInfo);
1.708 +
1.709 + IExtensionInterface *pAPI = pExt->GetAPI();
1.710 + if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
1.711 + {
1.712 + pOwner->AddChildDependent(pExt, pInfo->iface);
1.713 + }
1.714 +}
1.715 +
1.716 +void CExtensionManager::AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface)
1.717 +{
1.718 + CExtension *pExt = (CExtension *)ext;
1.719 + CExtension *pOwner = GetExtensionFromIdent(other);
1.720 +
1.721 + IfaceInfo info;
1.722 +
1.723 + info.iface = (SMInterface *)iface;
1.724 + info.owner = pOwner;
1.725 +
1.726 + pExt->AddDependency(&info);
1.727 + pOwner->AddChildDependent(pExt, (SMInterface *)iface);
1.728 +}
1.729 +
1.730 +void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
1.731 +{
1.732 + CExtension *pExt = (CExtension *)pOwner;
1.733 +
1.734 + pExt->AddInterface(pInterface);
1.735 +}
1.736 +
1.737 +void CExtensionManager::BindChildPlugin(IExtension *pParent, SMPlugin *pPlugin)
1.738 +{
1.739 + CExtension *pExt = (CExtension *)pParent;
1.740 +
1.741 + pExt->AddPlugin(static_cast<CPlugin *>(pPlugin));
1.742 +}
1.743 +
1.744 +void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
1.745 +{
1.746 + List<CExtension *>::iterator iter;
1.747 +
1.748 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1.749 + {
1.750 + (*iter)->DropRefsTo(static_cast<CPlugin *>(plugin));
1.751 + }
1.752 +}
1.753 +
1.754 +CExtension *CExtensionManager::FindByOrder(unsigned int num)
1.755 +{
1.756 + if (num < 1 || num > m_Libs.size())
1.757 + {
1.758 + return NULL;
1.759 + }
1.760 +
1.761 + List<CExtension *>::iterator iter = m_Libs.begin();
1.762 +
1.763 + while (iter != m_Libs.end())
1.764 + {
1.765 + if (--num == 0)
1.766 + {
1.767 + return (*iter);
1.768 + }
1.769 + iter++;
1.770 + }
1.771 +
1.772 + return NULL;
1.773 +}
1.774 +
1.775 +bool CExtensionManager::UnloadExtension(IExtension *_pExt)
1.776 +{
1.777 + if (!_pExt)
1.778 + {
1.779 + return false;
1.780 + }
1.781 +
1.782 + CExtension *pExt = (CExtension *)_pExt;
1.783 +
1.784 + if (m_Libs.find(pExt) == m_Libs.end())
1.785 + {
1.786 + return false;
1.787 + }
1.788 +
1.789 + /* Tell it to unload */
1.790 + if (pExt->IsLoaded())
1.791 + {
1.792 + IExtensionInterface *pAPI = pExt->GetAPI();
1.793 + pAPI->OnExtensionUnload();
1.794 + }
1.795 +
1.796 + /* First remove us from internal lists */
1.797 + g_ShareSys.RemoveInterfaces(_pExt);
1.798 + m_Libs.remove(pExt);
1.799 +
1.800 + List<CExtension *> UnloadQueue;
1.801 +
1.802 + /* Handle dependencies */
1.803 + if (pExt->IsLoaded())
1.804 + {
1.805 + /* Unload any dependent plugins */
1.806 + List<CPlugin *>::iterator p_iter = pExt->m_Dependents.begin();
1.807 + while (p_iter != pExt->m_Dependents.end())
1.808 + {
1.809 + /* We have to manually unlink ourselves here, since we're no longer being managed */
1.810 + scripts->UnloadPlugin((*p_iter));
1.811 + p_iter = pExt->m_Dependents.erase(p_iter);
1.812 + }
1.813 +
1.814 + List<String>::iterator s_iter;
1.815 + for (s_iter = pExt->m_Libraries.begin();
1.816 + s_iter != pExt->m_Libraries.end();
1.817 + s_iter++)
1.818 + {
1.819 + scripts->OnLibraryAction((*s_iter).c_str(), LibraryAction_Removed);
1.820 + }
1.821 +
1.822 + /* Notify and/or unload all dependencies */
1.823 + List<CExtension *>::iterator c_iter;
1.824 + CExtension *pDep;
1.825 + IExtensionInterface *pAPI;
1.826 + for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
1.827 + {
1.828 + pDep = (*c_iter);
1.829 + if ((pAPI=pDep->GetAPI()) == NULL)
1.830 + {
1.831 + continue;
1.832 + }
1.833 + if (pDep == pExt)
1.834 + {
1.835 + continue;
1.836 + }
1.837 + /* Now, get its dependency list */
1.838 + bool dropped = false;
1.839 + List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
1.840 + while (i_iter != pDep->m_Deps.end())
1.841 + {
1.842 + if ((*i_iter).owner == _pExt)
1.843 + {
1.844 + if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
1.845 + {
1.846 + if (!dropped)
1.847 + {
1.848 + dropped = true;
1.849 + UnloadQueue.push_back(pDep);
1.850 + }
1.851 + }
1.852 + pAPI->NotifyInterfaceDrop((*i_iter).iface);
1.853 + i_iter = pDep->m_Deps.erase(i_iter);
1.854 + }
1.855 + else
1.856 + {
1.857 + i_iter++;
1.858 + }
1.859 + }
1.860 + /* Flush out any back references to this plugin */
1.861 + i_iter = pDep->m_ChildDeps.begin();
1.862 + while (i_iter != pDep->m_ChildDeps.end())
1.863 + {
1.864 + if ((*i_iter).owner == pExt)
1.865 + {
1.866 + i_iter = pDep->m_ChildDeps.erase(i_iter);
1.867 + }
1.868 + else
1.869 + {
1.870 + i_iter++;
1.871 + }
1.872 + }
1.873 + }
1.874 +
1.875 + /* Unbind our natives from Core */
1.876 + pExt->DropEverything();
1.877 + }
1.878 +
1.879 + IdentityToken_t *pIdentity;
1.880 + if ((pIdentity = pExt->GetIdentity()) != NULL)
1.881 + {
1.882 + SMGlobalClass *glob = SMGlobalClass::head;
1.883 + while (glob)
1.884 + {
1.885 + glob->OnSourceModIdentityDropped(pIdentity);
1.886 + glob = glob->m_pGlobalClassNext;
1.887 + }
1.888 + }
1.889 +
1.890 + pExt->Unload();
1.891 + delete pExt;
1.892 +
1.893 + List<CExtension *>::iterator iter;
1.894 + for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
1.895 + {
1.896 + /* NOTE: This is safe because the unload function backs out of anything not present */
1.897 + UnloadExtension((*iter));
1.898 + }
1.899 +
1.900 + return true;
1.901 +}
1.902 +
1.903 +void CExtensionManager::MarkAllLoaded()
1.904 +{
1.905 + List<CExtension *>::iterator iter;
1.906 + CExtension *pExt;
1.907 +
1.908 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1.909 + {
1.910 + pExt = (*iter);
1.911 + if (!pExt->IsLoaded())
1.912 + {
1.913 + continue;
1.914 + }
1.915 + if (pExt->m_bFullyLoaded)
1.916 + {
1.917 + continue;
1.918 + }
1.919 + pExt->MarkAllLoaded();
1.920 + }
1.921 +}
1.922 +
1.923 +void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
1.924 +{
1.925 + /* This function doesn't really need to do anything now. We make sure the
1.926 + * other extension is loaded, but handling of dependencies is really done
1.927 + * by the interface fetcher.
1.928 + */
1.929 + if (required || autoload)
1.930 + {
1.931 + LoadAutoExtension(file);
1.932 + }
1.933 +}
1.934 +
1.935 +void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
1.936 +{
1.937 + int argcount = smcore.Argc(command);
1.938 + if (argcount >= 3)
1.939 + {
1.940 + const char *cmd = smcore.Arg(command, 2);
1.941 + if (strcmp(cmd, "list") == 0)
1.942 + {
1.943 + List<CExtension *>::iterator iter;
1.944 + CExtension *pExt;
1.945 + unsigned int num = 1;
1.946 + switch (m_Libs.size())
1.947 + {
1.948 + case 1:
1.949 + {
1.950 + rootmenu->ConsolePrint("[SM] Displaying 1 extension:");
1.951 + break;
1.952 + }
1.953 + case 0:
1.954 + {
1.955 + rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1.956 + break;
1.957 + }
1.958 + default:
1.959 + {
1.960 + rootmenu->ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
1.961 + break;
1.962 + }
1.963 + }
1.964 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
1.965 + {
1.966 + pExt = (*iter);
1.967 + if (pExt->IsLoaded())
1.968 + {
1.969 + char error[255];
1.970 + if (!pExt->IsRunning(error, sizeof(error)))
1.971 + {
1.972 + rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
1.973 + }
1.974 + else
1.975 + {
1.976 + IExtensionInterface *pAPI = pExt->GetAPI();
1.977 + const char *name = pAPI->GetExtensionName();
1.978 + const char *version = pAPI->GetExtensionVerString();
1.979 + const char *descr = pAPI->GetExtensionDescription();
1.980 + rootmenu->ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
1.981 + }
1.982 + } else {
1.983 + rootmenu->ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
1.984 + }
1.985 + }
1.986 + return;
1.987 + }
1.988 + else if (strcmp(cmd, "load") == 0)
1.989 + {
1.990 + if (argcount < 4)
1.991 + {
1.992 + rootmenu->ConsolePrint("[SM] Usage: sm exts load <file>");
1.993 + return;
1.994 + }
1.995 +
1.996 + const char *filename = smcore.Arg(command, 3);
1.997 + char path[PLATFORM_MAX_PATH];
1.998 + char error[256];
1.999 +
1.1000 + smcore.Format(path, sizeof(path), "%s%s%s", filename, !strstr(filename, ".ext") ? ".ext" : "",
1.1001 + !strstr(filename, "." PLATFORM_LIB_EXT) ? "." PLATFORM_LIB_EXT : "");
1.1002 +
1.1003 + if (FindExtensionByFile(path) != NULL)
1.1004 + {
1.1005 + rootmenu->ConsolePrint("[SM] Extension %s is already loaded.", path);
1.1006 + return;
1.1007 + }
1.1008 +
1.1009 + if (LoadExtension(path, error, sizeof(error)))
1.1010 + {
1.1011 + rootmenu->ConsolePrint("[SM] Loaded extension %s successfully.", path);
1.1012 + } else
1.1013 + {
1.1014 + rootmenu->ConsolePrint("[SM] Extension %s failed to load: %s", path, error);
1.1015 + }
1.1016 +
1.1017 + return;
1.1018 + }
1.1019 + else if (strcmp(cmd, "info") == 0)
1.1020 + {
1.1021 + if (argcount < 4)
1.1022 + {
1.1023 + rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1.1024 + return;
1.1025 + }
1.1026 +
1.1027 + const char *sId = smcore.Arg(command, 3);
1.1028 + unsigned int id = atoi(sId);
1.1029 + if (id <= 0)
1.1030 + {
1.1031 + rootmenu->ConsolePrint("[SM] Usage: sm exts info <#>");
1.1032 + return;
1.1033 + }
1.1034 +
1.1035 + if (m_Libs.size() == 0)
1.1036 + {
1.1037 + rootmenu->ConsolePrint("[SM] No extensions are loaded.");
1.1038 + return;
1.1039 + }
1.1040 +
1.1041 + if (id > m_Libs.size())
1.1042 + {
1.1043 + rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1.1044 + return;
1.1045 + }
1.1046 +
1.1047 + List<CExtension *>::iterator iter = m_Libs.begin();
1.1048 + CExtension *pExt = NULL;
1.1049 + while (iter != m_Libs.end())
1.1050 + {
1.1051 + if (--id == 0)
1.1052 + {
1.1053 + pExt = (*iter);
1.1054 + break;
1.1055 + }
1.1056 + iter++;
1.1057 + }
1.1058 + /* This should never happen */
1.1059 + if (!pExt)
1.1060 + {
1.1061 + rootmenu->ConsolePrint("[SM] No extension was found with id %d.", id);
1.1062 + return;
1.1063 + }
1.1064 +
1.1065 + if (!pExt->IsLoaded())
1.1066 + {
1.1067 + rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1.1068 + rootmenu->ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
1.1069 + }
1.1070 + else
1.1071 + {
1.1072 + char error[255];
1.1073 + if (!pExt->IsRunning(error, sizeof(error)))
1.1074 + {
1.1075 + rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1.1076 + rootmenu->ConsolePrint(" Loaded: Yes");
1.1077 + rootmenu->ConsolePrint(" Running: No (%s)", error);
1.1078 + }
1.1079 + else
1.1080 + {
1.1081 + IExtensionInterface *pAPI = pExt->GetAPI();
1.1082 + rootmenu->ConsolePrint(" File: %s", pExt->GetFilename());
1.1083 + rootmenu->ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
1.1084 + rootmenu->ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
1.1085 + rootmenu->ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
1.1086 + rootmenu->ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
1.1087 +
1.1088 + if (pExt->IsExternal())
1.1089 + {
1.1090 + rootmenu->ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
1.1091 + }
1.1092 + else if (pAPI->IsMetamodExtension())
1.1093 + {
1.1094 + rootmenu->ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
1.1095 + }
1.1096 + else
1.1097 + {
1.1098 + rootmenu->ConsolePrint(" Method: Loaded by SourceMod");
1.1099 + }
1.1100 + }
1.1101 + }
1.1102 + return;
1.1103 + }
1.1104 + else if (strcmp(cmd, "unload") == 0)
1.1105 + {
1.1106 + if (argcount < 4)
1.1107 + {
1.1108 + rootmenu->ConsolePrint("[SM] Usage: sm exts unload <#> [code]");
1.1109 + return;
1.1110 + }
1.1111 +
1.1112 + const char *arg = smcore.Arg(command, 3);
1.1113 + unsigned int num = atoi(arg);
1.1114 + CExtension *pExt = FindByOrder(num);
1.1115 +
1.1116 + if (!pExt)
1.1117 + {
1.1118 + rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1.1119 + return;
1.1120 + }
1.1121 +
1.1122 + if (argcount > 4 && pExt->unload_code)
1.1123 + {
1.1124 + const char *unload = smcore.Arg(command, 4);
1.1125 + if (pExt->unload_code == (unsigned)atoi(unload))
1.1126 + {
1.1127 + char filename[PLATFORM_MAX_PATH];
1.1128 + snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1.1129 + UnloadExtension(pExt);
1.1130 + rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1.1131 + }
1.1132 + else
1.1133 + {
1.1134 + rootmenu->ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
1.1135 + }
1.1136 + return;
1.1137 + }
1.1138 +
1.1139 + if (!pExt->IsLoaded()
1.1140 + || (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
1.1141 + {
1.1142 + char filename[PLATFORM_MAX_PATH];
1.1143 + snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1.1144 + UnloadExtension(pExt);
1.1145 + rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
1.1146 + return;
1.1147 + }
1.1148 + else
1.1149 + {
1.1150 + List<CPlugin *> plugins;
1.1151 + if (pExt->m_ChildDeps.size())
1.1152 + {
1.1153 + rootmenu->ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
1.1154 + List<CExtension *>::iterator iter;
1.1155 + CExtension *pOther;
1.1156 + /* Get list of all extensions */
1.1157 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1.1158 + {
1.1159 + List<IfaceInfo>::iterator i_iter;
1.1160 + pOther = (*iter);
1.1161 + if (!pOther->IsLoaded() || pOther == pExt)
1.1162 + {
1.1163 + continue;
1.1164 + }
1.1165 + /* Get their dependencies */
1.1166 + for (i_iter=pOther->m_Deps.begin();
1.1167 + i_iter!=pOther->m_Deps.end();
1.1168 + i_iter++)
1.1169 + {
1.1170 + /* Is this dependency to us? */
1.1171 + if ((*i_iter).owner != pExt)
1.1172 + {
1.1173 + continue;
1.1174 + }
1.1175 + /* Will our dependent care? */
1.1176 + if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
1.1177 + {
1.1178 + rootmenu->ConsolePrint(" -> %s", pExt->GetFilename());
1.1179 + /* Add to plugin unload list */
1.1180 + List<CPlugin *>::iterator p_iter;
1.1181 + for (p_iter=pOther->m_Dependents.begin();
1.1182 + p_iter!=pOther->m_Dependents.end();
1.1183 + p_iter++)
1.1184 + {
1.1185 + if (plugins.find((*p_iter)) == plugins.end())
1.1186 + {
1.1187 + plugins.push_back((*p_iter));
1.1188 + }
1.1189 + }
1.1190 + }
1.1191 + }
1.1192 + }
1.1193 + }
1.1194 + if (pExt->m_Dependents.size())
1.1195 + {
1.1196 + rootmenu->ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
1.1197 + List<CPlugin *>::iterator iter;
1.1198 + CPlugin *pPlugin;
1.1199 + for (iter = pExt->m_Dependents.begin(); iter != pExt->m_Dependents.end(); iter++)
1.1200 + {
1.1201 + pPlugin = (*iter);
1.1202 + if (plugins.find(pPlugin) == plugins.end())
1.1203 + {
1.1204 + plugins.push_back(pPlugin);
1.1205 + }
1.1206 + }
1.1207 + for (iter = plugins.begin(); iter != plugins.end(); iter++)
1.1208 + {
1.1209 + pPlugin = (*iter);
1.1210 + rootmenu->ConsolePrint(" -> %s", pPlugin->GetFilename());
1.1211 + }
1.1212 + }
1.1213 + srand(static_cast<int>(time(NULL)));
1.1214 + pExt->unload_code = (rand() % 877) + 123; //123 to 999
1.1215 + rootmenu->ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
1.1216 + rootmenu->ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
1.1217 +
1.1218 + return;
1.1219 + }
1.1220 + }
1.1221 + else if (strcmp(cmd, "reload") == 0)
1.1222 + {
1.1223 + if (argcount < 4)
1.1224 + {
1.1225 + rootmenu->ConsolePrint("[SM] Usage: sm exts reload <#>");
1.1226 + return;
1.1227 + }
1.1228 +
1.1229 + const char *arg = smcore.Arg(command, 3);
1.1230 + unsigned int num = atoi(arg);
1.1231 + CExtension *pExt = FindByOrder(num);
1.1232 +
1.1233 + if (!pExt)
1.1234 + {
1.1235 + rootmenu->ConsolePrint("[SM] Extension number %d was not found.", num);
1.1236 + return;
1.1237 + }
1.1238 +
1.1239 + if (pExt->IsLoaded())
1.1240 + {
1.1241 + char filename[PLATFORM_MAX_PATH];
1.1242 + char error[255];
1.1243 +
1.1244 + snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
1.1245 +
1.1246 + if (pExt->Reload(error, sizeof(error)))
1.1247 + {
1.1248 + rootmenu->ConsolePrint("[SM] Extension %s is now reloaded.", filename);
1.1249 + }
1.1250 + else
1.1251 + {
1.1252 + rootmenu->ConsolePrint("[SM] Extension %s failed to reload: %s", filename, error);
1.1253 + }
1.1254 +
1.1255 + return;
1.1256 + }
1.1257 + else
1.1258 + {
1.1259 + rootmenu->ConsolePrint("[SM] Extension %s is not loaded.", pExt->GetFilename());
1.1260 +
1.1261 + return;
1.1262 + }
1.1263 +
1.1264 + }
1.1265 + }
1.1266 +
1.1267 + rootmenu->ConsolePrint("SourceMod Extensions Menu:");
1.1268 + rootmenu->DrawGenericOption("info", "Extra extension information");
1.1269 + rootmenu->DrawGenericOption("list", "List extensions");
1.1270 + rootmenu->DrawGenericOption("load", "Load an extension");
1.1271 + rootmenu->DrawGenericOption("reload", "Reload an extension");
1.1272 + rootmenu->DrawGenericOption("unload", "Unload an extension");
1.1273 +}
1.1274 +
1.1275 +CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
1.1276 +{
1.1277 + if (ptr->type == g_ExtType)
1.1278 + {
1.1279 + return (CExtension *)(ptr->ptr);
1.1280 + }
1.1281 +
1.1282 + return NULL;
1.1283 +}
1.1284 +
1.1285 +CExtensionManager::CExtensionManager()
1.1286 +{
1.1287 +}
1.1288 +
1.1289 +CExtensionManager::~CExtensionManager()
1.1290 +{
1.1291 +}
1.1292 +
1.1293 +void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
1.1294 +{
1.1295 + CExtension *pExt = (CExtension *)pSource;
1.1296 + pExt->AddLibrary(library);
1.1297 + scripts->OnLibraryAction(library, LibraryAction_Added);
1.1298 +}
1.1299 +
1.1300 +bool CExtensionManager::LibraryExists(const char *library)
1.1301 +{
1.1302 + CExtension *pExt;
1.1303 +
1.1304 + for (List<CExtension *>::iterator iter = m_Libs.begin();
1.1305 + iter != m_Libs.end();
1.1306 + iter++)
1.1307 + {
1.1308 + pExt = (*iter);
1.1309 + for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
1.1310 + s_iter != pExt->m_Libraries.end();
1.1311 + s_iter++)
1.1312 + {
1.1313 + if ((*s_iter).compare(library) == 0)
1.1314 + {
1.1315 + return true;
1.1316 + }
1.1317 + }
1.1318 + }
1.1319 +
1.1320 + return false;
1.1321 +}
1.1322 +
1.1323 +IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
1.1324 + const char *filepath,
1.1325 + const char *filename,
1.1326 + char *error,
1.1327 + size_t maxlength)
1.1328 +{
1.1329 + IExtension *pAlready;
1.1330 + if ((pAlready=FindExtensionByFile(filename)) != NULL)
1.1331 + {
1.1332 + return pAlready;
1.1333 + }
1.1334 +
1.1335 + CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
1.1336 +
1.1337 + if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
1.1338 + {
1.1339 + pExt->Unload();
1.1340 + delete pExt;
1.1341 + return NULL;
1.1342 + }
1.1343 +
1.1344 + m_Libs.push_back(pExt);
1.1345 +
1.1346 + return pExt;
1.1347 +}
1.1348 +
1.1349 +void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
1.1350 +{
1.1351 + IExtensionInterface *pAPI;
1.1352 + List<CExtension *>::iterator iter;
1.1353 +
1.1354 + for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
1.1355 + {
1.1356 + if ((pAPI = (*iter)->GetAPI()) == NULL)
1.1357 + {
1.1358 + continue;
1.1359 + }
1.1360 + if (pAPI->GetExtensionVersion() > 3)
1.1361 + {
1.1362 + pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
1.1363 + }
1.1364 + }
1.1365 +}
1.1366 +
1.1367 +void CExtensionManager::ListExtensions(CVector<IExtension *> *list)
1.1368 +{
1.1369 + for (List<CExtension *>::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++)
1.1370 + {
1.1371 + list->push_back(*iter);
1.1372 + }
1.1373 +}
1.1374 +
1.1375 +bool CLocalExtension::IsSameFile(const char *file)
1.1376 +{
1.1377 + /* Only care about the shortened name. */
1.1378 + return strcmp(file, m_File.c_str()) == 0;
1.1379 +}
1.1380 +
1.1381 +bool CRemoteExtension::IsSameFile(const char *file)
1.1382 +{
1.1383 + /* :TODO: this could be better, but no one uses this API anyway. */
1.1384 + return strcmp(file, m_Path.c_str()) == 0;
1.1385 +}
1.1386 +