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