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