ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #include ds@1952: #include "ExtensionSys.h" ds@1952: #include "LibrarySys.h" ds@1952: #include "ShareSys.h" ds@1952: #include "Logger.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "sm_srvcmds.h" ds@1952: #include "sm_stringutil.h" ds@1952: ds@1952: CExtensionManager g_Extensions; ds@1952: IdentityType_t g_ExtType; ds@1952: ds@1952: void CExtension::Initialize(const char *filename, const char *path) ds@1952: { ds@1952: m_pAPI = NULL; ds@1952: m_pIdentToken = NULL; ds@1952: unload_code = 0; ds@1952: m_bFullyLoaded = false; ds@1952: m_File.assign(filename); ds@1952: m_Path.assign(path); ds@1952: } ds@1952: ds@1952: CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path) ds@1952: { ds@1952: Initialize(filename, path); ds@1952: m_pAPI = pAPI; ds@1952: } ds@1952: ds@1952: CLocalExtension::CLocalExtension(const char *filename) ds@1952: { ds@1952: m_PlId = 0; ds@1952: m_pLib = NULL; ds@1952: ds@1952: char path[PLATFORM_MAX_PATH]; ds@1952: ds@1952: /* First see if there is an engine specific build! */ ds@1952: g_SourceMod.BuildPath(Path_SM, ds@1952: path, ds@1952: PLATFORM_MAX_PATH, ds@1952: #if defined METAMOD_PLAPI_VERSION ds@1952: #if defined ORANGEBOX_BUILD ds@1952: "extensions/auto.2.ep2/%s", ds@1952: #else ds@1952: "extensions/auto.2.ep1/%s", ds@1952: #endif //ORANGEBOX_BUILD ds@1952: #else //METAMOD_PLAPI_VERSION ds@1952: "extensions/auto.1.ep1/%s", ds@1952: #endif //METAMOD_PLAPI_VERSION ds@1952: filename); ds@1952: ds@1952: /* Try the "normal" version */ ds@1952: if (!g_LibSys.IsPathFile(path)) ds@1952: { ds@1952: g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename); ds@1952: } ds@1952: ds@1952: Initialize(filename, path); ds@1952: } ds@1952: ds@1952: bool CRemoteExtension::Load(char *error, size_t maxlength) ds@1952: { ds@1952: if (!PerformAPICheck(error, maxlength)) ds@1952: { ds@1952: m_pAPI = NULL; ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (!CExtension::Load(error, maxlength)) ds@1952: { ds@1952: m_pAPI = NULL; ds@1952: return false; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CLocalExtension::Load(char *error, size_t maxlength) ds@1952: { ds@1952: m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength); ds@1952: ds@1952: if (m_pLib == NULL) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: typedef IExtensionInterface *(*GETAPI)(); ds@1952: GETAPI pfnGetAPI = NULL; ds@1952: ds@1952: if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL) ds@1952: { ds@1952: m_pLib->CloseLibrary(); ds@1952: m_pLib = NULL; ds@1952: snprintf(error, maxlength, "Unable to find extension entry point"); ds@1952: return false; ds@1952: } ds@1952: ds@1952: m_pAPI = pfnGetAPI(); ds@1952: ds@1952: /* Check pointer and version */ ds@1952: if (!PerformAPICheck(error, maxlength)) ds@1952: { ds@1952: m_pLib->CloseLibrary(); ds@1952: m_pLib = NULL; ds@1952: m_pAPI = NULL; ds@1952: return false; ds@1952: } ds@1952: ds@1952: /* Load as MM:S */ ds@1952: if (m_pAPI->IsMetamodExtension()) ds@1952: { ds@1952: bool already; ds@1952: m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength); ds@1952: } ds@1952: ds@1952: if (!CExtension::Load(error, maxlength)) ds@1952: { ds@1952: if (m_pAPI->IsMetamodExtension()) ds@1952: { ds@1952: if (m_PlId) ds@1952: { ds@1952: char dummy[255]; ds@1952: g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy)); ds@1952: m_PlId = 0; ds@1952: } ds@1952: } ds@1952: m_pLib->CloseLibrary(); ds@1952: m_pLib = NULL; ds@1952: m_pAPI = NULL; ds@1952: return false; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: void CRemoteExtension::Unload() ds@1952: { ds@1952: } ds@1952: ds@1952: void CLocalExtension::Unload() ds@1952: { ds@1952: if (m_pAPI != NULL && m_PlId) ds@1952: { ds@1952: char temp_buffer[255]; ds@1952: g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer)); ds@1952: m_PlId = 0; ds@1952: } ds@1952: ds@1952: if (m_pLib != NULL) ds@1952: { ds@1952: m_pLib->CloseLibrary(); ds@1952: m_pLib = NULL; ds@1952: } ds@1952: } ds@1952: ds@1952: bool CRemoteExtension::IsExternal() ds@1952: { ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CLocalExtension::IsExternal() ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: CExtension::~CExtension() ds@1952: { ds@1952: DestroyIdentity(); ds@1952: } ds@1952: ds@1952: bool CExtension::PerformAPICheck(char *error, size_t maxlength) ds@1952: { ds@1952: if (!m_pAPI) ds@1952: { ds@1952: snprintf(error, maxlength, "No IExtensionInterface instance provided"); ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION) ds@1952: { ds@1952: snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION); ds@1952: return false; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CExtension::Load(char *error, size_t maxlength) ds@1952: { ds@1952: CreateIdentity(); ds@1952: if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading())) ds@1952: { ds@1952: DestroyIdentity(); ds@1952: return false; ds@1952: } ds@1952: else ds@1952: { ds@1952: /* Check if we're past load time */ ds@1952: if (!g_SourceMod.IsMapLoading()) ds@1952: { ds@1952: m_pAPI->OnExtensionsAllLoaded(); ds@1952: } ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: void CExtension::CreateIdentity() ds@1952: { ds@1952: if (m_pIdentToken != NULL) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this); ds@1952: } ds@1952: ds@1952: void CExtension::DestroyIdentity() ds@1952: { ds@1952: if (m_pIdentToken == NULL) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: g_ShareSys.DestroyIdentity(m_pIdentToken); ds@1952: m_pIdentToken = NULL; ds@1952: } ds@1952: ds@1952: void CExtension::MarkAllLoaded() ds@1952: { ds@1952: assert(m_bFullyLoaded == false); ds@1952: if (!m_bFullyLoaded) ds@1952: { ds@1952: m_bFullyLoaded = true; ds@1952: m_pAPI->OnExtensionsAllLoaded(); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtension::AddPlugin(IPlugin *pPlugin) ds@1952: { ds@1952: /* Unfortunately we have to do this :( */ ds@1952: if (m_Plugins.find(pPlugin) != m_Plugins.end()) ds@1952: { ds@1952: m_Plugins.push_back(pPlugin); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtension::RemovePlugin(IPlugin *pPlugin) ds@1952: { ds@1952: m_Plugins.remove(pPlugin); ds@1952: ds@1952: List::iterator iter = m_WeakNatives.begin(); ds@1952: while (iter != m_WeakNatives.end()) ds@1952: { ds@1952: if ((*iter).pl == pPlugin) ds@1952: { ds@1952: iter = m_WeakNatives.erase(iter); ds@1952: } ds@1952: else ds@1952: { ds@1952: iter++; ds@1952: } ds@1952: } ds@1952: ds@1952: iter = m_ReplacedNatives.begin(); ds@1952: while (iter != m_ReplacedNatives.end()) ds@1952: { ds@1952: if ((*iter).pl == pPlugin) ds@1952: { ds@1952: iter = m_ReplacedNatives.erase(iter); ds@1952: } ds@1952: else ds@1952: { ds@1952: iter++; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtension::SetError(const char *error) ds@1952: { ds@1952: m_Error.assign(error); ds@1952: } ds@1952: ds@1952: IExtensionInterface *CExtension::GetAPI() ds@1952: { ds@1952: return m_pAPI; ds@1952: } ds@1952: ds@1952: const char *CExtension::GetFilename() ds@1952: { ds@1952: return m_File.c_str(); ds@1952: } ds@1952: ds@1952: IdentityToken_t *CExtension::GetIdentity() ds@1952: { ds@1952: return m_pIdentToken; ds@1952: } ds@1952: ds@1952: bool CRemoteExtension::IsLoaded() ds@1952: { ds@1952: return (m_pAPI != NULL); ds@1952: } ds@1952: ds@1952: bool CLocalExtension::IsLoaded() ds@1952: { ds@1952: return (m_pLib != NULL); ds@1952: } ds@1952: ds@1952: void CExtension::AddDependency(IfaceInfo *pInfo) ds@1952: { ds@1952: if (m_Deps.find(*pInfo) == m_Deps.end()) ds@1952: { ds@1952: m_Deps.push_back(*pInfo); ds@1952: } ds@1952: } ds@1952: ds@1952: bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2) ds@1952: { ds@1952: return (i1.iface == i2.iface) && (i1.owner == i2.owner); ds@1952: } ds@1952: ds@1952: void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface) ds@1952: { ds@1952: IfaceInfo info; ds@1952: info.iface = iface; ds@1952: info.owner = pOther; ds@1952: ds@1952: List::iterator iter; ds@1952: for (iter = m_ChildDeps.begin(); ds@1952: iter != m_ChildDeps.end(); ds@1952: iter++) ds@1952: { ds@1952: IfaceInfo &other = (*iter); ds@1952: if (other == info) ds@1952: { ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: m_ChildDeps.push_back(info); ds@1952: } ds@1952: ds@1952: ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface) ds@1952: { ds@1952: List::iterator iter = m_Deps.begin(); ds@1952: ds@1952: if (iter == m_Deps.end()) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: if (pOwner) ds@1952: { ds@1952: *pOwner = (*iter).owner; ds@1952: } ds@1952: if (pInterface) ds@1952: { ds@1952: *pInterface = (*iter).iface; ds@1952: } ds@1952: ds@1952: List::iterator *pIter = new List::iterator(iter); ds@1952: ds@1952: return (ITERATOR *)pIter; ds@1952: } ds@1952: ds@1952: bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface) ds@1952: { ds@1952: List::iterator *pIter = (List::iterator *)iter; ds@1952: List::iterator _iter; ds@1952: ds@1952: if (_iter == m_Deps.end()) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: _iter++; ds@1952: ds@1952: if (pOwner) ds@1952: { ds@1952: *pOwner = (*_iter).owner; ds@1952: } ds@1952: if (pInterface) ds@1952: { ds@1952: *pInterface = (*_iter).iface; ds@1952: } ds@1952: ds@1952: *pIter = _iter; ds@1952: ds@1952: if (_iter == m_Deps.end()) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: void CExtension::FreeDependencyIterator(ITERATOR *iter) ds@1952: { ds@1952: List::iterator *pIter = (List::iterator *)iter; ds@1952: ds@1952: delete pIter; ds@1952: } ds@1952: ds@1952: void CExtension::AddInterface(SMInterface *pInterface) ds@1952: { ds@1952: m_Interfaces.push_back(pInterface); ds@1952: } ds@1952: ds@1952: bool CExtension::IsRunning(char *error, size_t maxlength) ds@1952: { ds@1952: if (!IsLoaded()) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "%s", m_Error.c_str()); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: ds@1952: return m_pAPI->QueryRunning(error, maxlength); ds@1952: } ds@1952: ds@1952: void CExtension::AddLibrary(const char *library) ds@1952: { ds@1952: m_Libraries.push_back(library); ds@1952: } ds@1952: ds@1952: /********************* ds@1952: * EXTENSION MANAGER * ds@1952: *********************/ ds@1952: ds@1952: void CExtensionManager::OnSourceModAllInitialized() ds@1952: { ds@1952: g_ExtType = g_ShareSys.CreateIdentType("EXTENSION"); ds@1952: g_PluginSys.AddPluginsListener(this); ds@1952: g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this); ds@1952: g_ShareSys.AddInterface(NULL, this); ds@1952: } ds@1952: ds@1952: void CExtensionManager::OnSourceModShutdown() ds@1952: { ds@1952: g_RootMenu.RemoveRootConsoleCommand("exts", this); ds@1952: g_ShareSys.DestroyIdentType(g_ExtType); ds@1952: } ds@1952: ds@1952: void CExtensionManager::Shutdown() ds@1952: { ds@1952: List::iterator iter; ds@1952: ds@1952: while ((iter = m_Libs.begin()) != m_Libs.end()) ds@1952: { ds@1952: UnloadExtension((*iter)); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtensionManager::TryAutoload() ds@1952: { ds@1952: char path[PLATFORM_MAX_PATH]; ds@1952: ds@1952: g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions"); ds@1952: ds@1952: IDirectory *pDir = g_LibSys.OpenDirectory(path); ds@1952: if (!pDir) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: const char *lfile; ds@1952: size_t len; ds@1952: while (pDir->MoreFiles()) ds@1952: { ds@1952: if (pDir->IsEntryDirectory()) ds@1952: { ds@1952: pDir->NextEntry(); ds@1952: continue; ds@1952: } ds@1952: ds@1952: lfile = pDir->GetEntryName(); ds@1952: len = strlen(lfile); ds@1952: if (len <= 9) /* size of ".autoload" */ ds@1952: { ds@1952: pDir->NextEntry(); ds@1952: continue; ds@1952: } ds@1952: ds@1952: if (strcmp(&lfile[len - 9], ".autoload") != 0) ds@1952: { ds@1952: pDir->NextEntry(); ds@1952: continue; ds@1952: } ds@1952: ds@1952: char file[PLATFORM_MAX_PATH]; ds@1952: len = UTIL_Format(file, sizeof(file), "%s", lfile); ds@1952: strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT); ds@1952: ds@1952: LoadAutoExtension(file); ds@1952: ds@1952: pDir->NextEntry(); ds@1952: } ds@1952: } ds@1952: ds@1952: IExtension *CExtensionManager::LoadAutoExtension(const char *path) ds@1952: { ds@1952: if (!strstr(path, "." PLATFORM_LIB_EXT)) ds@1952: { ds@1952: char newpath[PLATFORM_MAX_PATH]; ds@1952: g_LibSys.PathFormat(newpath, sizeof(newpath), "%s.%s", path, PLATFORM_LIB_EXT); ds@1952: return LoadAutoExtension(newpath); ds@1952: } ds@1952: ds@1952: IExtension *pAlready; ds@1952: if ((pAlready=FindExtensionByFile(path)) != NULL) ds@1952: { ds@1952: return pAlready; ds@1952: } ds@1952: ds@1952: char error[256]; ds@1952: CExtension *p = new CLocalExtension(path); ds@1952: ds@1952: /* We put us in the list beforehand so extensions that check for each other ds@1952: * won't recursively load each other. ds@1952: */ ds@1952: m_Libs.push_back(p); ds@1952: ds@1952: if (!p->Load(error, sizeof(error)) || !p->IsLoaded()) ds@1952: { ds@1952: g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error); ds@1952: p->SetError(error); ds@1952: } ds@1952: ds@1952: return p; ds@1952: } ds@1952: ds@1952: IExtension *CExtensionManager::FindExtensionByFile(const char *file) ds@1952: { ds@1952: List::iterator iter; ds@1952: CExtension *pExt; ds@1952: ds@1952: /* Chomp off the path */ ds@1952: char lookup[PLATFORM_MAX_PATH]; ds@1952: g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file); ds@1952: ds@1952: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++) ds@1952: { ds@1952: pExt = (*iter); ds@1952: char short_file[PLATFORM_MAX_PATH]; ds@1952: g_LibSys.GetFileFromPath(short_file, sizeof(short_file), pExt->GetFilename()); ds@1952: if (strcmp(lookup, short_file) == 0) ds@1952: { ds@1952: return pExt; ds@1952: } ds@1952: } ds@1952: ds@1952: /* If we got no results, test if there was a platform extension. ds@1952: * If not, add one. ds@1952: */ ds@1952: if (!strstr(file, "." PLATFORM_LIB_EXT)) ds@1952: { ds@1952: char path[PLATFORM_MAX_PATH]; ds@1952: UTIL_Format(path, sizeof(path), "%s.%s", file, PLATFORM_LIB_EXT); ds@1952: return FindExtensionByFile(path); ds@1952: } ds@1952: ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: IExtension *CExtensionManager::FindExtensionByName(const char *ext) ds@1952: { ds@1952: List::iterator iter; ds@1952: CExtension *pExt; ds@1952: IExtensionInterface *pAPI; ds@1952: const char *name; ds@1952: ds@1952: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++) ds@1952: { ds@1952: pExt = (*iter); ds@1952: if (!pExt->IsLoaded()) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if ((pAPI = pExt->GetAPI()) == NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: name = pAPI->GetExtensionName(); ds@1952: if (!name) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (strcmp(name, ext) == 0) ds@1952: { ds@1952: return pExt; ds@1952: } ds@1952: } ds@1952: ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength) ds@1952: { ds@1952: IExtension *pAlready; ds@1952: if ((pAlready=FindExtensionByFile(file)) != NULL) ds@1952: { ds@1952: return pAlready; ds@1952: } ds@1952: ds@1952: CExtension *pExt = new CLocalExtension(file); ds@1952: ds@1952: if (!pExt->Load(error, maxlength) || !pExt->IsLoaded()) ds@1952: { ds@1952: pExt->Unload(); ds@1952: delete pExt; ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: /* :TODO: do QueryRunning check if the map is loaded */ ds@1952: ds@1952: m_Libs.push_back(pExt); ds@1952: ds@1952: return pExt; ds@1952: } ds@1952: ds@1952: void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo) ds@1952: { ds@1952: CExtension *pExt = (CExtension *)pRequester; ds@1952: CExtension *pOwner = (CExtension *)pInfo->owner; ds@1952: ds@1952: pExt->AddDependency(pInfo); ds@1952: ds@1952: IExtensionInterface *pAPI = pExt->GetAPI(); ds@1952: if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface)) ds@1952: { ds@1952: pOwner->AddChildDependent(pExt, pInfo->iface); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface) ds@1952: { ds@1952: CExtension *pExt = (CExtension *)pOwner; ds@1952: ds@1952: pExt->AddInterface(pInterface); ds@1952: } ds@1952: ds@1952: void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin) ds@1952: { ds@1952: CExtension *pExt = (CExtension *)pParent; ds@1952: ds@1952: pExt->AddPlugin(pPlugin); ds@1952: } ds@1952: ds@1952: void CExtensionManager::OnPluginDestroyed(IPlugin *plugin) ds@1952: { ds@1952: List::iterator iter; ds@1952: ds@1952: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++) ds@1952: { ds@1952: (*iter)->RemovePlugin(plugin); ds@1952: } ds@1952: } ds@1952: ds@1952: CExtension *CExtensionManager::FindByOrder(unsigned int num) ds@1952: { ds@1952: if (num < 1 || num > m_Libs.size()) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: List::iterator iter = m_Libs.begin(); ds@1952: ds@1952: while (iter != m_Libs.end()) ds@1952: { ds@1952: if (--num == 0) ds@1952: { ds@1952: return (*iter); ds@1952: } ds@1952: iter++; ds@1952: } ds@1952: ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin) ds@1952: { ds@1952: IPluginContext *pContext = pPlugin->GetBaseContext(); ds@1952: ds@1952: List exts; ds@1952: ds@1952: uint32_t natives = pContext->GetNativesNum(); ds@1952: sp_native_t *native; ds@1952: sm_extnative_t *x_native; ds@1952: sm_repnative_t *r_native; ds@1952: for (uint32_t i=0; iGetNativeByIndex(i, &native) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: ds@1952: /* Make sure the native is not already bound */ ds@1952: if (native->status == SP_NATIVE_BOUND) ds@1952: { ds@1952: /* If it is bound, see if there is a replacement. */ ds@1952: if ((r_native = m_RepNatives.retrieve(native->name)) == NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: ds@1952: /* Rewrite the address. Whee! */ ds@1952: native->pfn = r_native->info.func; ds@1952: ds@1952: /* Make sure this will unload safely */ ds@1952: WeakNative wn((CPlugin *)pPlugin, i); ds@1952: r_native->owner->m_ReplacedNatives.push_back(wn); ds@1952: ds@1952: continue; ds@1952: } ds@1952: ds@1952: /* See if we've got this native in our cache */ ds@1952: if ((x_native = m_ExtNatives.retrieve(native->name)) == NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: /* Try and bind it */ ds@1952: if (pContext->BindNativeToIndex(i, x_native->info->func) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: /* See if it's optional */ ds@1952: if (native->flags & SP_NTVFLAG_OPTIONAL) ds@1952: { ds@1952: WeakNative wkn = WeakNative((CPlugin *)pPlugin, i); ds@1952: x_native->owner->m_WeakNatives.push_back(wkn); ds@1952: } ds@1952: else if (exts.find(x_native->owner) == exts.end()) ds@1952: { ds@1952: exts.push_back(x_native->owner); ds@1952: } ds@1952: } ds@1952: ds@1952: List::iterator iter; ds@1952: for (iter = exts.begin(); iter != exts.end(); iter++) ds@1952: { ds@1952: CExtension *pExt = (*iter); ds@1952: if (pExt->m_Plugins.find(pPlugin) != pExt->m_Plugins.end()) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: pExt->m_Plugins.push_back(pPlugin); ds@1952: } ds@1952: } ds@1952: ds@1952: bool CExtensionManager::UnloadExtension(IExtension *_pExt) ds@1952: { ds@1952: if (!_pExt) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: CExtension *pExt = (CExtension *)_pExt; ds@1952: ds@1952: if (m_Libs.find(pExt) == m_Libs.end()) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: /* First remove us from internal lists */ ds@1952: g_ShareSys.RemoveInterfaces(_pExt); ds@1952: m_Libs.remove(pExt); ds@1952: ds@1952: List UnloadQueue; ds@1952: ds@1952: /* Handle dependencies */ ds@1952: if (pExt->IsLoaded()) ds@1952: { ds@1952: /* Unload any dependent plugins */ ds@1952: List::iterator p_iter = pExt->m_Plugins.begin(); ds@1952: while (p_iter != pExt->m_Plugins.end()) ds@1952: { ds@1952: /* We have to manually unlink ourselves here, since we're no longer being managed */ ds@1952: g_PluginSys.UnloadPlugin((*p_iter)); ds@1952: p_iter = pExt->m_Plugins.erase(p_iter); ds@1952: } ds@1952: ds@1952: List::iterator s_iter; ds@1952: for (s_iter = pExt->m_Libraries.begin(); ds@1952: s_iter != pExt->m_Libraries.end(); ds@1952: s_iter++) ds@1952: { ds@1952: g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true); ds@1952: } ds@1952: ds@1952: /* Unbind weak natives */ ds@1952: List::iterator wkn_iter; ds@1952: for (wkn_iter=pExt->m_WeakNatives.begin(); wkn_iter!=pExt->m_WeakNatives.end(); wkn_iter++) ds@1952: { ds@1952: WeakNative & wkn = (*wkn_iter); ds@1952: sp_context_t *ctx = wkn.pl->GetContext(); ds@1952: ctx->natives[wkn.idx].status = SP_NATIVE_UNBOUND; ds@1952: } ds@1952: ds@1952: /* Unbind replacement natives, link them back to their originals */ ds@1952: for (wkn_iter = pExt->m_ReplacedNatives.begin(); ds@1952: wkn_iter != pExt->m_ReplacedNatives.end(); ds@1952: wkn_iter++) ds@1952: { ds@1952: WeakNative & wkn = (*wkn_iter); ds@1952: sp_context_t *ctx = wkn.pl->GetContext(); ds@1952: sm_repnative_t *r_native = m_RepNatives.retrieve(ctx->natives[wkn.idx].name); ds@1952: if (r_native == NULL || ctx->natives[wkn.idx].pfn != r_native->info.func) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: ctx->natives[wkn.idx].pfn = r_native->original; ds@1952: } ds@1952: ds@1952: /* Notify and/or unload all dependencies */ ds@1952: List::iterator c_iter; ds@1952: CExtension *pDep; ds@1952: IExtensionInterface *pAPI; ds@1952: for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++) ds@1952: { ds@1952: pDep = (*c_iter); ds@1952: if ((pAPI=pDep->GetAPI()) == NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (pDep == pExt) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: /* Now, get its dependency list */ ds@1952: bool dropped = false; ds@1952: List::iterator i_iter = pDep->m_Deps.begin(); ds@1952: while (i_iter != pDep->m_Deps.end()) ds@1952: { ds@1952: if ((*i_iter).owner == _pExt) ds@1952: { ds@1952: if (!pAPI->QueryInterfaceDrop((*i_iter).iface)) ds@1952: { ds@1952: if (!dropped) ds@1952: { ds@1952: dropped = true; ds@1952: UnloadQueue.push_back(pDep); ds@1952: } ds@1952: } ds@1952: pAPI->NotifyInterfaceDrop((*i_iter).iface); ds@1952: i_iter = pDep->m_Deps.erase(i_iter); ds@1952: } ds@1952: else ds@1952: { ds@1952: i_iter++; ds@1952: } ds@1952: } ds@1952: /* Flush out any back references to this plugin */ ds@1952: i_iter = pDep->m_ChildDeps.begin(); ds@1952: while (i_iter != pDep->m_ChildDeps.end()) ds@1952: { ds@1952: if ((*i_iter).owner == pExt) ds@1952: { ds@1952: i_iter = pDep->m_ChildDeps.erase(i_iter); ds@1952: } ds@1952: else ds@1952: { ds@1952: i_iter++; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Unbind our natives from Core */ ds@1952: List::iterator native_iter; ds@1952: for (native_iter = pExt->m_Natives.begin(); ds@1952: native_iter != pExt->m_Natives.end(); ds@1952: native_iter++) ds@1952: { ds@1952: const sp_nativeinfo_t *natives = (*native_iter); ds@1952: sm_extnative_t *x_native; ds@1952: for (unsigned int n = 0; ds@1952: natives[n].name != NULL && natives[n].func != NULL; ds@1952: n++) ds@1952: { ds@1952: x_native = m_ExtNatives.retrieve(natives[n].name); ds@1952: if (x_native && x_native->owner == pExt) ds@1952: { ds@1952: m_ExtNatives.remove(natives[n].name); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Unbind our replacement natives */ ds@1952: List::iterator rep_iter = m_RepNativeList.begin(); ds@1952: while (rep_iter != m_RepNativeList.end()) ds@1952: { ds@1952: sm_repnative_t & r_native = (*rep_iter); ds@1952: if (r_native.owner == pExt) ds@1952: { ds@1952: m_RepNatives.remove(r_native.info.name); ds@1952: rep_iter = m_RepNativeList.erase(rep_iter); ds@1952: } ds@1952: else ds@1952: { ds@1952: rep_iter++; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Tell it to unload */ ds@1952: pAPI = pExt->GetAPI(); ds@1952: pAPI->OnExtensionUnload(); ds@1952: } ds@1952: ds@1952: IdentityToken_t *pIdentity; ds@1952: if ((pIdentity = pExt->GetIdentity()) != NULL) ds@1952: { ds@1952: SMGlobalClass *glob = SMGlobalClass::head; ds@1952: while (glob) ds@1952: { ds@1952: glob->OnSourceModIdentityDropped(pIdentity); ds@1952: glob = glob->m_pGlobalClassNext; ds@1952: } ds@1952: } ds@1952: ds@1952: pExt->Unload(); ds@1952: delete pExt; ds@1952: ds@1952: List::iterator iter; ds@1952: for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++) ds@1952: { ds@1952: /* NOTE: This is safe because the unload function backs out of anything not present */ ds@1952: UnloadExtension((*iter)); ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives) ds@1952: { ds@1952: CExtension *pExt = (CExtension *)pOwner; ds@1952: ds@1952: pExt->m_Natives.push_back(natives); ds@1952: ds@1952: for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++) ds@1952: { ds@1952: if (m_ExtNatives.retrieve(natives[i].name) != NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: sm_extnative_t x_native; ds@1952: x_native.info = &natives[i]; ds@1952: x_native.owner = pExt; ds@1952: m_ExtNatives.insert(natives[i].name, x_native); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtensionManager::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives) ds@1952: { ds@1952: SPVM_NATIVE_FUNC orig; ds@1952: ds@1952: for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++) ds@1952: { ds@1952: if ((orig = g_PluginSys.FindCoreNative(natives[i].name)) == NULL) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: sm_repnative_t rep; ds@1952: rep.info = natives[i]; ds@1952: rep.owner = (CExtension *)myself; ds@1952: rep.original = orig; ds@1952: m_RepNativeList.push_back(rep); ds@1952: m_RepNatives.insert(natives[i].name, rep); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtensionManager::MarkAllLoaded() ds@1952: { ds@1952: List::iterator iter; ds@1952: CExtension *pExt; ds@1952: ds@1952: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++) ds@1952: { ds@1952: pExt = (*iter); ds@1952: if (!pExt->IsLoaded()) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (pExt->m_bFullyLoaded) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: pExt->MarkAllLoaded(); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload) ds@1952: { ds@1952: /* This function doesn't really need to do anything now. We make sure the ds@1952: * other extension is loaded, but handling of dependencies is really done ds@1952: * by the interface fetcher. ds@1952: */ ds@1952: if (required || autoload) ds@1952: { ds@1952: LoadAutoExtension(file); ds@1952: } ds@1952: } ds@1952: ds@1952: void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command) ds@1952: { ds@1952: int argcount = command.ArgC(); ds@1952: if (argcount >= 3) ds@1952: { ds@1952: const char *cmd = command.Arg(2); ds@1952: if (strcmp(cmd, "list") == 0) ds@1952: { ds@1952: List::iterator iter; ds@1952: CExtension *pExt; ds@1952: unsigned int num = 1; ds@1952: switch (m_Libs.size()) ds@1952: { ds@1952: case 1: ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:"); ds@1952: break; ds@1952: } ds@1952: case 0: ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] No extensions are loaded."); ds@1952: break; ds@1952: } ds@1952: default: ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size()); ds@1952: break; ds@1952: } ds@1952: } ds@1952: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++) ds@1952: { ds@1952: pExt = (*iter); ds@1952: if (pExt->IsLoaded()) ds@1952: { ds@1952: char error[255]; ds@1952: if (!pExt->IsRunning(error, sizeof(error))) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[%02d] file \"%s\": %s", num, pExt->GetFilename(), error); ds@1952: } ds@1952: else ds@1952: { ds@1952: IExtensionInterface *pAPI = pExt->GetAPI(); ds@1952: const char *name = pAPI->GetExtensionName(); ds@1952: const char *version = pAPI->GetExtensionVerString(); ds@1952: const char *descr = pAPI->GetExtensionDescription(); ds@1952: g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr); ds@1952: } ds@1952: } else { ds@1952: g_RootMenu.ConsolePrint("[%02d] file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str()); ds@1952: } ds@1952: } ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "info") == 0) ds@1952: { ds@1952: if (argcount < 4) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>"); ds@1952: return; ds@1952: } ds@1952: ds@1952: const char *sId = command.Arg(3); ds@1952: unsigned int id = atoi(sId); ds@1952: if (id <= 0) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>"); ds@1952: return; ds@1952: } ds@1952: ds@1952: if (m_Libs.size() == 0) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] No extensions are loaded."); ds@1952: return; ds@1952: } ds@1952: ds@1952: if (id > m_Libs.size()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id); ds@1952: return; ds@1952: } ds@1952: ds@1952: List::iterator iter = m_Libs.begin(); ds@1952: CExtension *pExt = NULL; ds@1952: while (iter != m_Libs.end()) ds@1952: { ds@1952: if (--id == 0) ds@1952: { ds@1952: pExt = (*iter); ds@1952: break; ds@1952: } ds@1952: iter++; ds@1952: } ds@1952: /* This should never happen */ ds@1952: if (!pExt) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id); ds@1952: return; ds@1952: } ds@1952: ds@1952: if (!pExt->IsLoaded()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename()); ds@1952: g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str()); ds@1952: } ds@1952: else ds@1952: { ds@1952: char error[255]; ds@1952: if (!pExt->IsRunning(error, sizeof(error))) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename()); ds@1952: g_RootMenu.ConsolePrint(" Loaded: Yes"); ds@1952: g_RootMenu.ConsolePrint(" Running: No (%s)", error); ds@1952: } ds@1952: else ds@1952: { ds@1952: IExtensionInterface *pAPI = pExt->GetAPI(); ds@1952: g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename()); ds@1952: g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString()); ds@1952: g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription()); ds@1952: g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL()); ds@1952: g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString()); ds@1952: ds@1952: if (pExt->IsExternal()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod"); ds@1952: } ds@1952: else if (pAPI->IsMetamodExtension()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source"); ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod"); ds@1952: } ds@1952: } ds@1952: } ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "unload") == 0) ds@1952: { ds@1952: if (argcount < 4) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]"); ds@1952: return; ds@1952: } ds@1952: ds@1952: const char *arg = command.Arg(3); ds@1952: unsigned int num = atoi(arg); ds@1952: CExtension *pExt = FindByOrder(num); ds@1952: ds@1952: if (!pExt) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num); ds@1952: return; ds@1952: } ds@1952: ds@1952: if (argcount > 4 && pExt->unload_code) ds@1952: { ds@1952: const char *unload = command.Arg(4); ds@1952: if (pExt->unload_code == (unsigned)atoi(unload)) ds@1952: { ds@1952: char filename[PLATFORM_MAX_PATH]; ds@1952: snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename()); ds@1952: UnloadExtension(pExt); ds@1952: g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename); ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code); ds@1952: } ds@1952: return; ds@1952: } ds@1952: ds@1952: if (!pExt->IsLoaded() ds@1952: || (!pExt->m_ChildDeps.size() && !pExt->m_Plugins.size())) ds@1952: { ds@1952: char filename[PLATFORM_MAX_PATH]; ds@1952: snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename()); ds@1952: UnloadExtension(pExt); ds@1952: g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename); ds@1952: return; ds@1952: } ds@1952: else ds@1952: { ds@1952: List plugins; ds@1952: if (pExt->m_ChildDeps.size()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename()); ds@1952: List::iterator iter; ds@1952: CExtension *pOther; ds@1952: /* Get list of all extensions */ ds@1952: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++) ds@1952: { ds@1952: List::iterator i_iter; ds@1952: pOther = (*iter); ds@1952: if (!pOther->IsLoaded() || pOther == pExt) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: /* Get their dependencies */ ds@1952: for (i_iter=pOther->m_Deps.begin(); ds@1952: i_iter!=pOther->m_Deps.end(); ds@1952: i_iter++) ds@1952: { ds@1952: /* Is this dependency to us? */ ds@1952: if ((*i_iter).owner != pExt) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: /* Will our dependent care? */ ds@1952: if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename()); ds@1952: /* Add to plugin unload list */ ds@1952: List::iterator p_iter; ds@1952: for (p_iter=pOther->m_Plugins.begin(); ds@1952: p_iter!=pOther->m_Plugins.end(); ds@1952: p_iter++) ds@1952: { ds@1952: if (plugins.find((*p_iter)) == plugins.end()) ds@1952: { ds@1952: plugins.push_back((*p_iter)); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: if (pExt->m_Plugins.size()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename()); ds@1952: List::iterator iter; ds@1952: IPlugin *pPlugin; ds@1952: for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++) ds@1952: { ds@1952: pPlugin = (*iter); ds@1952: if (plugins.find(pPlugin) == plugins.end()) ds@1952: { ds@1952: plugins.push_back(pPlugin); ds@1952: } ds@1952: } ds@1952: for (iter = plugins.begin(); iter != plugins.end(); iter++) ds@1952: { ds@1952: pPlugin = (*iter); ds@1952: g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename()); ds@1952: } ds@1952: } ds@1952: srand(static_cast(time(NULL))); ds@1952: pExt->unload_code = (rand() % 877) + 123; //123 to 999 ds@1952: g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename()); ds@1952: g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code); ds@1952: ds@1952: return; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: g_RootMenu.ConsolePrint("SourceMod Extensions Menu:"); ds@1952: g_RootMenu.DrawGenericOption("info", "Extra extension information"); ds@1952: g_RootMenu.DrawGenericOption("list", "List extensions"); ds@1952: g_RootMenu.DrawGenericOption("unload", "Unload an extension"); ds@1952: } ds@1952: ds@1952: CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr) ds@1952: { ds@1952: if (ptr->type == g_ExtType) ds@1952: { ds@1952: return (CExtension *)(ptr->ptr); ds@1952: } ds@1952: ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: CExtensionManager::CExtensionManager() ds@1952: { ds@1952: } ds@1952: ds@1952: CExtensionManager::~CExtensionManager() ds@1952: { ds@1952: } ds@1952: ds@1952: void CExtensionManager::AddLibrary(IExtension *pSource, const char *library) ds@1952: { ds@1952: CExtension *pExt = (CExtension *)pSource; ds@1952: pExt->AddLibrary(library); ds@1952: g_PluginSys.OnLibraryAction(library, false, false); ds@1952: } ds@1952: ds@1952: bool CExtensionManager::LibraryExists(const char *library) ds@1952: { ds@1952: CExtension *pExt; ds@1952: ds@1952: for (List::iterator iter = m_Libs.begin(); ds@1952: iter != m_Libs.end(); ds@1952: iter++) ds@1952: { ds@1952: pExt = (*iter); ds@1952: for (List::iterator s_iter = pExt->m_Libraries.begin(); ds@1952: s_iter != pExt->m_Libraries.end(); ds@1952: s_iter++) ds@1952: { ds@1952: if ((*s_iter).compare(library) == 0) ds@1952: { ds@1952: return true; ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: return false; ds@1952: } ds@1952: ds@1952: IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface, ds@1952: const char *filepath, ds@1952: const char *filename, ds@1952: char *error, ds@1952: size_t maxlength) ds@1952: { ds@1952: IExtension *pAlready; ds@1952: if ((pAlready=FindExtensionByFile(filename)) != NULL) ds@1952: { ds@1952: return pAlready; ds@1952: } ds@1952: ds@1952: CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath); ds@1952: ds@1952: if (!pExt->Load(error, maxlength) || !pExt->IsLoaded()) ds@1952: { ds@1952: pExt->Unload(); ds@1952: delete pExt; ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: m_Libs.push_back(pExt); ds@1952: ds@1952: return pExt; ds@1952: } ds@1952: faluco@1993: void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax) faluco@1993: { dvander@1998: IExtensionInterface *pAPI; faluco@1993: List::iterator iter; faluco@1993: faluco@1993: for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++) faluco@1993: { dvander@1998: if ((pAPI = (*iter)->GetAPI()) == NULL) faluco@1993: { dvander@1998: continue; dvander@1998: } dvander@1998: if (pAPI->GetExtensionVersion() > 3) dvander@1998: { dvander@1998: pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax); faluco@1993: } faluco@1993: } faluco@1993: }