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 "PluginSys.h" ds@1952: #include "ShareSys.h" ds@1952: #include "LibrarySys.h" ds@1952: #include "HandleSys.h" ds@1952: #include "ForwardSys.h" ds@1952: #include "sourcemm_api.h" ds@1952: #include "sourcemod.h" ds@1952: #include "TextParsers.h" ds@1952: #include "Logger.h" ds@1952: #include "ExtensionSys.h" ds@1952: #include "sm_srvcmds.h" ds@1952: #include "sm_stringutil.h" ds@1952: #include "ConCmdManager.h" ds@1952: #include "PlayerManager.h" ds@1952: #include "CoreConfig.h" ds@1952: #include "Translator.h" ds@1952: ds@1952: CPluginManager g_PluginSys; ds@1952: HandleType_t g_PluginType = 0; ds@1952: IdentityType_t g_PluginIdent = 0; ds@1952: ds@1952: CPlugin::CPlugin(const char *file) ds@1952: { ds@1952: static int MySerial = 0; ds@1952: ds@1952: m_type = PluginType_Private; ds@1952: m_status = Plugin_Uncompiled; ds@1952: m_serial = ++MySerial; dvander@2193: m_pRuntime = NULL; ds@1952: m_errormsg[256] = '\0'; ds@1952: snprintf(m_filename, sizeof(m_filename), "%s", file); ds@1952: m_handle = 0; ds@1952: m_ident = NULL; ds@1952: m_pProps = sm_trie_create(); ds@1952: m_FakeNativesMissing = false; ds@1952: m_LibraryMissing = false; ds@1952: m_bGotAllLoaded = false; dvander@2042: m_pPhrases = g_Translator.CreatePhraseCollection(); ds@1952: } ds@1952: ds@1952: CPlugin::~CPlugin() ds@1952: { ds@1952: if (m_handle) ds@1952: { ds@1952: HandleSecurity sec; ds@1952: sec.pOwner = g_PluginSys.GetIdentity(); ds@1952: sec.pIdentity = sec.pOwner; ds@1952: ds@1952: g_HandleSys.FreeHandle(m_handle, &sec); ds@1952: g_ShareSys.DestroyIdentity(m_ident); ds@1952: } ds@1952: dvander@2193: if (m_pRuntime != NULL) ds@1952: { dvander@2193: delete m_pRuntime; dvander@2193: m_pRuntime = NULL; ds@1952: } ds@1952: ds@1952: if (m_pProps) ds@1952: { ds@1952: sm_trie_destroy(m_pProps); ds@1952: } ds@1952: for (size_t i=0; iDestroy(); dvander@2042: m_pPhrases = NULL; dvander@2042: } ds@1952: } ds@1952: ds@1952: void CPlugin::InitIdentity() ds@1952: { ds@1952: if (!m_handle) ds@1952: { ds@1952: m_ident = g_ShareSys.CreateIdentity(g_PluginIdent, this); ds@1952: m_handle = g_HandleSys.CreateHandle(g_PluginType, this, g_PluginSys.GetIdentity(), g_PluginSys.GetIdentity(), NULL); dvander@2193: m_pRuntime->GetDefaultContext()->SetKey(1, m_ident); dvander@2193: m_pRuntime->GetDefaultContext()->SetKey(2, (IPlugin *)this); ds@1952: } ds@1952: } ds@1952: ds@1952: unsigned int CPlugin::CalcMemUsage() ds@1952: { ds@1952: unsigned int base_size = ds@1952: sizeof(CPlugin) ds@1952: + sizeof(IdentityToken_t) ds@1952: + (m_configs.size() * (sizeof(AutoConfig *) + sizeof(AutoConfig))) ds@1952: + sm_trie_mem_usage(m_pProps); ds@1952: ds@1952: for (unsigned int i = 0; i < m_configs.size(); i++) ds@1952: { ds@1952: base_size += m_configs[i]->autocfg.size(); ds@1952: base_size += m_configs[i]->folder.size(); ds@1952: } ds@1952: ds@1952: for (List::iterator i = m_Libraries.begin(); ds@1952: i != m_Libraries.end(); ds@1952: i++) ds@1952: { ds@1952: base_size += (*i).size(); ds@1952: } ds@1952: ds@1952: for (List::iterator i = m_RequiredLibs.begin(); ds@1952: i != m_RequiredLibs.end(); ds@1952: i++) ds@1952: { ds@1952: base_size += (*i).size(); ds@1952: } ds@1952: ds@1952: return base_size; ds@1952: } ds@1952: ds@1952: Handle_t CPlugin::GetMyHandle() ds@1952: { ds@1952: return m_handle; ds@1952: } ds@1952: ds@1952: CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength) ds@1952: { ds@1952: char fullpath[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file); ds@1952: FILE *fp = fopen(fullpath, "rb"); ds@1952: ds@1952: CPlugin *pPlugin = new CPlugin(file); ds@1952: ds@1952: if (!fp) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Unable to open file"); ds@1952: } ds@1952: pPlugin->m_status = Plugin_BadLoad; ds@1952: return pPlugin; ds@1952: } ds@1952: ds@1952: return pPlugin; ds@1952: } ds@1952: ds@1952: bool CPlugin::GetProperty(const char *prop, void **ptr, bool remove/* =false */) ds@1952: { ds@1952: bool exists = sm_trie_retrieve(m_pProps, prop, ptr); ds@1952: ds@1952: if (exists && remove) ds@1952: { ds@1952: sm_trie_delete(m_pProps, prop); ds@1952: } ds@1952: ds@1952: return exists; ds@1952: } ds@1952: ds@1952: bool CPlugin::SetProperty(const char *prop, void *ptr) ds@1952: { ds@1952: return sm_trie_insert(m_pProps, prop, ptr); ds@1952: } ds@1952: dvander@2193: IPluginRuntime *CPlugin::GetRuntime() ds@1952: { dvander@2193: return m_pRuntime; ds@1952: } ds@1952: ds@1952: void CPlugin::SetErrorState(PluginStatus status, const char *error_fmt, ...) ds@1952: { ds@1952: PluginStatus old_status = m_status; ds@1952: m_status = status; ds@1952: ds@1952: if (old_status == Plugin_Running) ds@1952: { ds@1952: /* Tell everyone we're now paused */ ds@1952: g_PluginSys._SetPauseState(this, true); ds@1952: } ds@1952: ds@1952: va_list ap; ds@1952: va_start(ap, error_fmt); ds@1952: vsnprintf(m_errormsg, sizeof(m_errormsg), error_fmt, ap); ds@1952: va_end(ap); ds@1952: dvander@2193: if (m_pRuntime != NULL) ds@1952: { dvander@2193: m_pRuntime->SetPauseState(true); ds@1952: } ds@1952: } ds@1952: ds@1952: void CPlugin::UpdateInfo() ds@1952: { ds@1952: /* Now grab the info */ ds@1952: uint32_t idx; ds@1952: IPluginContext *base = GetBaseContext(); ds@1952: int err = base->FindPubvarByName("myinfo", &idx); ds@1952: ds@1952: memset(&m_info, 0, sizeof(m_info)); ds@1952: ds@1952: if (err == SP_ERROR_NONE) ds@1952: { ds@1952: struct sm_plugininfo_c_t ds@1952: { ds@1952: cell_t name; ds@1952: cell_t description; ds@1952: cell_t author; ds@1952: cell_t version; ds@1952: cell_t url; ds@1952: }; ds@1952: sm_plugininfo_c_t *cinfo; ds@1952: cell_t local_addr; ds@1952: ds@1952: base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&cinfo); ds@1952: base->LocalToString(cinfo->name, (char **)&m_info.name); ds@1952: base->LocalToString(cinfo->description, (char **)&m_info.description); ds@1952: base->LocalToString(cinfo->author, (char **)&m_info.author); ds@1952: base->LocalToString(cinfo->url, (char **)&m_info.url); ds@1952: base->LocalToString(cinfo->version, (char **)&m_info.version); ds@1952: } ds@1952: ds@1952: m_info.author = m_info.author ? m_info.author : ""; ds@1952: m_info.description = m_info.description ? m_info.description : ""; ds@1952: m_info.name = m_info.name ? m_info.name : ""; ds@1952: m_info.url = m_info.url ? m_info.url : ""; ds@1952: m_info.version = m_info.version ? m_info.version : ""; dvander@2105: dvander@2105: if ((err = base->FindPubvarByName("__version", &idx)) == SP_ERROR_NONE) dvander@2105: { dvander@2105: struct __version_info dvander@2105: { dvander@2105: cell_t version; dvander@2105: cell_t filevers; dvander@2105: cell_t date; dvander@2105: cell_t time; dvander@2105: }; dvander@2105: __version_info *info; dvander@2105: cell_t local_addr; dvander@2105: const char *pDate, *pTime; dvander@2105: dvander@2105: pDate = ""; dvander@2105: pTime = ""; dvander@2105: dvander@2105: base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&info); dvander@2105: m_FileVersion = info->version; dvander@2105: if (m_FileVersion >= 3) dvander@2105: { dvander@2105: base->LocalToString(info->date, (char **)&pDate); dvander@2105: base->LocalToString(info->time, (char **)&pTime); dvander@2105: UTIL_Format(m_DateTime, sizeof(m_DateTime), "%s %s", pDate, pTime); dvander@2105: } dvander@2105: } dvander@2105: else dvander@2105: { dvander@2105: m_FileVersion = 0; dvander@2105: } ds@1952: } ds@1952: ds@1952: void CPlugin::Call_OnPluginStart() ds@1952: { ds@1952: if (m_status != Plugin_Loaded) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: m_status = Plugin_Running; ds@1952: ds@1952: cell_t result; dvander@2193: IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginStart"); ds@1952: if (!pFunction) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: int err; ds@1952: if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE) ds@1952: { ds@1952: SetErrorState(Plugin_Error, "Error detected in plugin startup (see error logs)"); ds@1952: } ds@1952: } ds@1952: ds@1952: void CPlugin::Call_OnPluginEnd() ds@1952: { ds@1952: if (m_status > Plugin_Paused) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: cell_t result; dvander@2193: IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginEnd"); ds@1952: if (!pFunction) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: pFunction->Execute(&result); ds@1952: } ds@1952: ds@1952: void CPlugin::Call_OnAllPluginsLoaded() ds@1952: { ds@1952: if (m_status > Plugin_Paused) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: if (m_bGotAllLoaded) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: m_bGotAllLoaded = true; ds@1952: ds@1952: cell_t result; dvander@2193: IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnAllPluginsLoaded"); ds@1952: if (pFunction != NULL) ds@1952: { ds@1952: pFunction->Execute(&result); ds@1952: } ds@1952: ds@1952: if (g_OnMapStarted) ds@1952: { dvander@2193: if ((pFunction = m_pRuntime->GetFunctionByName("OnMapStart")) != NULL) ds@1952: { ds@1952: pFunction->Execute(NULL); ds@1952: } ds@1952: } ds@1952: ds@1952: if (SM_AreConfigsExecuted()) ds@1952: { ds@1952: SM_ExecuteForPlugin(GetBaseContext()); ds@1952: } ds@1952: } ds@1952: ds@1952: bool CPlugin::Call_AskPluginLoad(char *error, size_t maxlength) ds@1952: { ds@1952: if (m_status != Plugin_Created) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: m_status = Plugin_Loaded; ds@1952: ds@1952: int err; ds@1952: cell_t result; dvander@2193: IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("AskPluginLoad"); ds@1952: ds@1952: if (!pFunction) ds@1952: { ds@1952: return true; ds@1952: } ds@1952: ds@1952: pFunction->PushCell(m_handle); ds@1952: pFunction->PushCell(g_PluginSys.IsLateLoadTime() ? 1 : 0); ds@1952: pFunction->PushStringEx(error, maxlength, 0, SM_PARAM_COPYBACK); ds@1952: pFunction->PushCell(maxlength); ds@1952: if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (!result || m_status != Plugin_Loaded) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: dvander@2193: void *CPlugin::GetPluginStructure() ds@1952: { dvander@2193: return NULL; ds@1952: } ds@1952: ds@1952: IPluginContext *CPlugin::GetBaseContext() ds@1952: { dvander@2193: if (m_pRuntime == NULL) dvander@2193: { dvander@2193: return NULL; dvander@2193: } dvander@2193: dvander@2193: return m_pRuntime->GetDefaultContext(); ds@1952: } ds@1952: ds@1952: sp_context_t *CPlugin::GetContext() ds@1952: { dvander@2193: return NULL; ds@1952: } ds@1952: ds@1952: const char *CPlugin::GetFilename() ds@1952: { ds@1952: return m_filename; ds@1952: } ds@1952: ds@1952: PluginType CPlugin::GetType() ds@1952: { ds@1952: return m_type; ds@1952: } ds@1952: ds@1952: const sm_plugininfo_t *CPlugin::GetPublicInfo() ds@1952: { ds@1952: return &m_info; ds@1952: } ds@1952: ds@1952: unsigned int CPlugin::GetSerial() ds@1952: { ds@1952: return m_serial; ds@1952: } ds@1952: ds@1952: PluginStatus CPlugin::GetStatus() ds@1952: { ds@1952: return m_status; ds@1952: } ds@1952: ds@1952: bool CPlugin::IsDebugging() ds@1952: { dvander@2193: if (m_pRuntime == NULL) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: dvander@2193: return m_pRuntime->IsDebugging(); ds@1952: } ds@1952: ds@1952: void CPlugin::LibraryActions(bool dropping) ds@1952: { ds@1952: List::iterator iter; ds@1952: for (iter = m_Libraries.begin(); ds@1952: iter != m_Libraries.end(); ds@1952: iter++) ds@1952: { ds@1952: g_PluginSys.OnLibraryAction((*iter).c_str(), true, dropping); ds@1952: } ds@1952: } ds@1952: ds@1952: bool CPlugin::SetPauseState(bool paused) ds@1952: { ds@1952: if (paused && GetStatus() != Plugin_Running) ds@1952: { ds@1952: return false; ds@1952: } else if (!paused && GetStatus() != Plugin_Paused) { ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (paused) ds@1952: { ds@1952: LibraryActions(true); ds@1952: } ds@1952: dvander@2193: IPluginFunction *pFunction = m_pRuntime->GetFunctionByName("OnPluginPauseChange"); ds@1952: if (pFunction) ds@1952: { ds@1952: cell_t result; ds@1952: pFunction->PushCell(paused ? 1 : 0); ds@1952: pFunction->Execute(&result); ds@1952: } ds@1952: ds@1952: if (paused) ds@1952: { ds@1952: m_status = Plugin_Paused; dvander@2193: m_pRuntime->SetPauseState(true); ds@1952: } else { ds@1952: m_status = Plugin_Running; dvander@2193: m_pRuntime->SetPauseState(false); ds@1952: } ds@1952: ds@1952: g_PluginSys._SetPauseState(this, paused); ds@1952: ds@1952: if (!paused) ds@1952: { ds@1952: LibraryActions(false); ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: IdentityToken_t *CPlugin::GetIdentity() ds@1952: { ds@1952: return m_ident; ds@1952: } ds@1952: ds@1952: bool CPlugin::ToggleDebugMode(bool debug, char *error, size_t maxlength) ds@1952: { ds@1952: int err; ds@1952: ds@1952: if (!IsRunnable()) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Plugin is not runnable."); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (debug && IsDebugging()) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Plugin is already in debug mode."); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: else if (!debug && !IsDebugging()) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Plugins is already in production mode."); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: dvander@2193: ICompilation *co = g_pSourcePawn2->StartCompilation(); dvander@2193: dvander@2193: if (!co->SetOption("debug", (debug) ? "1" : "0")) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Failed to change plugin mode (JIT failure)."); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: dvander@2193: if ((err = m_pRuntime->ApplyCompilationOptions(co)) != SP_ERROR_NONE) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Failed to recompile plugin (JIT error %d).", err); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: dvander@2193: UpdateInfo(); dvander@2193: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CPlugin::IsRunnable() ds@1952: { ds@1952: return (m_status <= Plugin_Paused) ? true : false; ds@1952: } ds@1952: ds@1952: time_t CPlugin::GetFileTimeStamp() ds@1952: { ds@1952: char path[PLATFORM_MAX_PATH]; ds@1952: g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "plugins/%s", m_filename); ds@1952: #ifdef PLATFORM_WINDOWS ds@1952: struct _stat s; ds@1952: if (_stat(path, &s) != 0) ds@1952: #elif defined PLATFORM_POSIX ds@1952: struct stat s; ds@1952: if (stat(path, &s) != 0) ds@1952: #endif ds@1952: { ds@1952: return 0; ds@1952: } ds@1952: else ds@1952: { ds@1952: return s.st_mtime; ds@1952: } ds@1952: } ds@1952: ds@1952: time_t CPlugin::GetTimeStamp() ds@1952: { ds@1952: return m_LastAccess; ds@1952: } ds@1952: ds@1952: void CPlugin::SetTimeStamp(time_t t) ds@1952: { ds@1952: m_LastAccess = t; ds@1952: } ds@1952: dvander@2042: IPhraseCollection *CPlugin::GetPhrases() ds@1952: { dvander@2042: return m_pPhrases; ds@1952: } ds@1952: ds@1952: void CPlugin::DependencyDropped(CPlugin *pOwner) ds@1952: { dvander@2193: if (!m_pRuntime) ds@1952: { ds@1952: return; ds@1952: } ds@1952: ds@1952: List::iterator reqlib_iter; ds@1952: List::iterator lib_iter; ds@1952: for (lib_iter=pOwner->m_Libraries.begin(); lib_iter!=pOwner->m_Libraries.end(); lib_iter++) ds@1952: { ds@1952: for (reqlib_iter=m_RequiredLibs.begin(); reqlib_iter!=m_RequiredLibs.end(); reqlib_iter++) ds@1952: { ds@1952: if ((*reqlib_iter) == (*lib_iter)) ds@1952: { ds@1952: m_LibraryMissing = true; ds@1952: } ds@1952: } ds@1952: } ds@1952: dvander@2086: List::iterator iter; dvander@2086: NativeEntry *pNative; ds@1952: sp_native_t *native; ds@1952: uint32_t idx; ds@1952: unsigned int unbound = 0; ds@1952: dvander@2086: for (iter = pOwner->m_Natives.begin(); dvander@2086: iter != pOwner->m_Natives.end(); ds@1952: iter++) ds@1952: { ds@1952: pNative = (*iter); ds@1952: /* Find this native! */ dvander@2193: if (m_pRuntime->FindNativeByName(pNative->name, &idx) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: /* Unbind it */ dvander@2193: m_pRuntime->GetNativeByIndex(idx, &native); ds@1952: native->pfn = NULL; ds@1952: native->status = SP_NATIVE_UNBOUND; ds@1952: unbound++; ds@1952: } ds@1952: ds@1952: if (unbound) ds@1952: { ds@1952: m_FakeNativesMissing = true; ds@1952: } ds@1952: ds@1952: /* :IDEA: in the future, add native trapping? */ ds@1952: if (m_FakeNativesMissing || m_LibraryMissing) ds@1952: { ds@1952: SetErrorState(Plugin_Error, "Depends on plugin: %s", pOwner->GetFilename()); ds@1952: } ds@1952: } ds@1952: ds@1952: unsigned int CPlugin::GetConfigCount() ds@1952: { ds@1952: return (unsigned int)m_configs.size(); ds@1952: } ds@1952: ds@1952: AutoConfig *CPlugin::GetConfig(unsigned int i) ds@1952: { ds@1952: if (i >= GetConfigCount()) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: return m_configs[i]; ds@1952: } ds@1952: ds@1952: void CPlugin::AddConfig(bool autoCreate, const char *cfg, const char *folder) ds@1952: { ds@1952: /* Do a check for duplicates to prevent double-execution */ ds@1952: for (size_t i = 0; i < m_configs.size(); i++) ds@1952: { ds@1952: if (m_configs[i]->autocfg.compare(cfg) == 0 ds@1952: && m_configs[i]->folder.compare(folder) == 0 ds@1952: && m_configs[i]->create == autoCreate) ds@1952: { ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: AutoConfig *c = new AutoConfig; ds@1952: ds@1952: c->autocfg = cfg; ds@1952: c->folder = folder; ds@1952: c->create = autoCreate; ds@1952: ds@1952: m_configs.push_back(c); ds@1952: } ds@1952: dvander@2086: void CPlugin::DropEverything() dvander@2086: { dvander@2086: CPlugin *pOther; dvander@2086: List::iterator iter; dvander@2086: List::iterator wk_iter; dvander@2086: dvander@2086: /* Tell everyone that depends on us that we're about to drop */ dvander@2086: for (iter = m_Dependents.begin(); dvander@2086: iter != m_Dependents.end(); dvander@2086: iter++) dvander@2086: { dvander@2086: pOther = (*iter); dvander@2086: pOther->DependencyDropped(this); dvander@2086: } dvander@2086: dvander@2086: /* Note: we don't care about things we depend on. dvander@2086: * The reason is that extensions have their own cleanup dvander@2086: * code for plugins. Although the "right" design would be dvander@2086: * to centralize that here, i'm omitting it for now. Thus, dvander@2086: * the code below to walk the plugins list will suffice. dvander@2086: */ dvander@2086: dvander@2086: /* Other plugins could be holding weak references that were dvander@2086: * added by us. We need to clean all of those up now. dvander@2086: */ dvander@2086: for (iter = g_PluginSys.m_plugins.begin(); dvander@2086: iter != g_PluginSys.m_plugins.end(); dvander@2086: iter++) dvander@2086: { dvander@2086: (*iter)->DropRefsTo(this); dvander@2086: } dvander@2086: dvander@2086: /* Proceed with the rest of the necessities. */ dvander@2086: CNativeOwner::DropEverything(); dvander@2086: } dvander@2086: dvander@2086: bool CPlugin::AddFakeNative(IPluginFunction *pFunc, const char *name, SPVM_FAKENATIVE_FUNC func) dvander@2086: { dvander@2086: NativeEntry *pEntry; dvander@2086: dvander@2086: if ((pEntry = g_ShareSys.AddFakeNative(pFunc, name, func)) == NULL) dvander@2086: { dvander@2086: return false; dvander@2086: } dvander@2086: dvander@2086: m_Natives.push_back(pEntry); dvander@2086: dvander@2086: return true; dvander@2086: } dvander@2086: ds@1952: /******************* ds@1952: * PLUGIN ITERATOR * ds@1952: *******************/ ds@1952: ds@1952: CPluginManager::CPluginIterator::CPluginIterator(List *_mylist) ds@1952: { ds@1952: mylist = _mylist; ds@1952: Reset(); ds@1952: } ds@1952: ds@1952: IPlugin *CPluginManager::CPluginIterator::GetPlugin() ds@1952: { ds@1952: return (*current); ds@1952: } ds@1952: ds@1952: bool CPluginManager::CPluginIterator::MorePlugins() ds@1952: { ds@1952: return (current != mylist->end()); ds@1952: } ds@1952: ds@1952: void CPluginManager::CPluginIterator::NextPlugin() ds@1952: { ds@1952: current++; ds@1952: } ds@1952: ds@1952: void CPluginManager::CPluginIterator::Release() ds@1952: { ds@1952: g_PluginSys.ReleaseIterator(this); ds@1952: } ds@1952: ds@1952: CPluginManager::CPluginIterator::~CPluginIterator() ds@1952: { ds@1952: } ds@1952: ds@1952: void CPluginManager::CPluginIterator::Reset() ds@1952: { ds@1952: current = mylist->begin(); ds@1952: } ds@1952: ds@1952: /****************** ds@1952: * PLUGIN MANAGER * ds@1952: ******************/ ds@1952: ds@1952: CPluginManager::CPluginManager() ds@1952: { ds@1952: m_LoadLookup = sm_trie_create(); ds@1952: m_AllPluginsLoaded = false; ds@1952: m_MyIdent = NULL; ds@1952: m_LoadingLocked = false; ds@1952: } ds@1952: ds@1952: CPluginManager::~CPluginManager() ds@1952: { ds@1952: /* :NOTICE: ds@1952: * Ignore the fact that there might be plugins in the cache. ds@1952: * This usually means that Core is not being unloaded properly, and everything ds@1952: * will crash anyway. YAY ds@1952: */ ds@1952: sm_trie_destroy(m_LoadLookup); ds@1952: ds@1952: CStack::iterator iter; ds@1952: for (iter=m_iters.begin(); iter!=m_iters.end(); iter++) ds@1952: { ds@1952: delete (*iter); ds@1952: } ds@1952: m_iters.popall(); ds@1952: } ds@1952: ds@1952: void CPluginManager::Shutdown() ds@1952: { ds@1952: List::iterator iter; ds@1952: ds@1952: while ((iter = m_plugins.begin()) != m_plugins.end()) ds@1952: { ds@1952: UnloadPlugin(*iter); ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::LoadAll_FirstPass(const char *config, const char *basedir) ds@1952: { ds@1952: /* First read in the database of plugin settings */ ds@1952: SMCError err; ds@1952: SMCStates states; ds@1952: m_AllPluginsLoaded = false; ds@1952: if ((err=textparsers->ParseFile_SMC(config, &m_PluginInfo, &states)) != SMCError_Okay) ds@1952: { ds@1952: g_Logger.LogError("[SM] Encountered fatal error parsing file \"%s\"", config); ds@1952: const char *err_msg = textparsers->GetSMCErrorString(err); ds@1952: if (err_msg) ds@1952: { ds@1952: g_Logger.LogError("[SM] Parse error encountered: \"%s\"", err_msg); ds@1952: } ds@1952: } ds@1952: ds@1952: LoadPluginsFromDir(basedir, NULL); ds@1952: } ds@1952: ds@1952: void CPluginManager::LoadPluginsFromDir(const char *basedir, const char *localpath) ds@1952: { ds@1952: char base_path[PLATFORM_MAX_PATH]; ds@1952: ds@1952: /* Form the current path to start reading from */ ds@1952: if (localpath == NULL) ds@1952: { ds@1952: g_LibSys.PathFormat(base_path, sizeof(base_path), "%s", basedir); ds@1952: } else { ds@1952: g_LibSys.PathFormat(base_path, sizeof(base_path), "%s/%s", basedir, localpath); ds@1952: } ds@1952: ds@1952: IDirectory *dir = g_LibSys.OpenDirectory(base_path); ds@1952: ds@1952: if (!dir) ds@1952: { ds@1952: char error[256]; ds@1952: g_LibSys.GetPlatformError(error, sizeof(error)); ds@1952: g_Logger.LogError("[SM] Failure reading from plugins path: %s", localpath); ds@1952: g_Logger.LogError("[SM] Platform returned error: %s", error); ds@1952: return; ds@1952: } ds@1952: ds@1952: while (dir->MoreFiles()) ds@1952: { ds@1952: if (dir->IsEntryDirectory() ds@1952: && (strcmp(dir->GetEntryName(), ".") != 0) ds@1952: && (strcmp(dir->GetEntryName(), "..") != 0) ds@1952: && (strcmp(dir->GetEntryName(), "disabled") != 0) ds@1952: && (strcmp(dir->GetEntryName(), "optional") != 0)) ds@1952: { ds@1952: char new_local[PLATFORM_MAX_PATH]; ds@1952: if (localpath == NULL) ds@1952: { ds@1952: /* If no path yet, don't add a former slash */ ds@1952: snprintf(new_local, sizeof(new_local), "%s", dir->GetEntryName()); ds@1952: } else { ds@1952: g_LibSys.PathFormat(new_local, sizeof(new_local), "%s/%s", localpath, dir->GetEntryName()); ds@1952: } ds@1952: LoadPluginsFromDir(basedir, new_local); ds@1952: } else if (dir->IsEntryFile()) { ds@1952: const char *name = dir->GetEntryName(); ds@1952: size_t len = strlen(name); ds@1952: if (len >= 4 ds@1952: && strcmp(&name[len-4], ".smx") == 0) ds@1952: { ds@1952: /* If the filename matches, load the plugin */ ds@1952: char plugin[PLATFORM_MAX_PATH]; ds@1952: if (localpath == NULL) ds@1952: { ds@1952: snprintf(plugin, sizeof(plugin), "%s", name); ds@1952: } else { ds@1952: g_LibSys.PathFormat(plugin, sizeof(plugin), "%s/%s", localpath, name); ds@1952: } ds@1952: LoadAutoPlugin(plugin); ds@1952: } ds@1952: } ds@1952: dir->NextEntry(); ds@1952: } ds@1952: g_LibSys.CloseDirectory(dir); ds@1952: } ds@1952: ds@1952: LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength) ds@1952: { ds@1952: if (m_LoadingLocked) ds@1952: { ds@1952: return LoadRes_NeverLoad; ds@1952: } ds@1952: dvander@2193: int err; ds@1952: bool no_load = false; ds@1952: PluginSettings *pset; ds@1952: unsigned int setcount = m_PluginInfo.GetSettingsNum(); ds@1952: for (unsigned int i=0; iblockload_val) ds@1952: { ds@1952: no_load = true; ds@1952: break; ds@1952: } ds@1952: } ds@1952: ds@1952: /** ds@1952: * Does this plugin already exist? ds@1952: */ ds@1952: CPlugin *pPlugin; ds@1952: if (sm_trie_retrieve(m_LoadLookup, path, (void **)&pPlugin)) ds@1952: { ds@1952: /* Check to see if we should try reloading it */ ds@1952: if (pPlugin->GetStatus() == Plugin_BadLoad ds@1952: || pPlugin->GetStatus() == Plugin_Error ds@1952: || pPlugin->GetStatus() == Plugin_Failed ds@1952: || no_load) ds@1952: { ds@1952: UnloadPlugin(pPlugin); ds@1952: } ds@1952: else ds@1952: { ds@1952: if (_plugin) ds@1952: { ds@1952: *_plugin = pPlugin; ds@1952: } ds@1952: return LoadRes_AlreadyLoaded; ds@1952: } ds@1952: } ds@1952: ds@1952: if (no_load) ds@1952: { ds@1952: return LoadRes_NeverLoad; ds@1952: } ds@1952: ds@1952: pPlugin = CPlugin::CreatePlugin(path, error, maxlength); ds@1952: ds@1952: assert(pPlugin != NULL); ds@1952: ds@1952: pPlugin->m_type = PluginType_MapUpdated; ds@1952: ds@1952: ICompilation *co = NULL; ds@1952: ds@1952: if (pPlugin->m_status == Plugin_Uncompiled) ds@1952: { dvander@2193: co = g_pSourcePawn2->StartCompilation(); ds@1952: } ds@1952: ds@1952: for (unsigned int i=0; im_type = pset->type_val; ds@1952: if (co) ds@1952: { ds@1952: for (unsigned int j=0; jopts_num; j++) ds@1952: { ds@1952: const char *key, *val; ds@1952: m_PluginInfo.GetOptionsForPlugin(pset, j, &key, &val); ds@1952: if (!key || !val) ds@1952: { ds@1952: continue; ds@1952: } dvander@2193: if ((err = co->SetOption(key, val)) == SP_ERROR_NONE) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Unable to set JIT option (key \"%s\") (value \"%s\")", key, val); ds@1952: } dvander@2193: co->Abort(); ds@1952: co = NULL; ds@1952: break; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Do the actual compiling */ dvander@2193: if (co != NULL) ds@1952: { dvander@2193: char fullpath[PLATFORM_MAX_PATH]; dvander@2193: g_SourceMod.BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", pPlugin->m_filename); dvander@2193: dvander@2193: pPlugin->m_pRuntime = g_pSourcePawn2->LoadPlugin(co, fullpath, &err); dvander@2193: if (pPlugin->m_pRuntime == NULL) dvander@2193: { dvander@2193: snprintf(error, dvander@2193: maxlength, dvander@2193: "Unable to load plugin (error %d: %s)", dvander@2193: err, dvander@2193: g_pSourcePawn2->GetErrorString(err)); dvander@2193: } dvander@2193: else dvander@2193: { dvander@2193: pPlugin->UpdateInfo(); dvander@2193: pPlugin->m_status = Plugin_Created; dvander@2193: } ds@1952: } ds@1952: ds@1952: /* Get the status */ ds@1952: if (pPlugin->GetStatus() == Plugin_Created) ds@1952: { dvander@2086: /* First native pass - add anything from Core */ dvander@2086: g_ShareSys.BindNativesToPlugin(pPlugin, true); ds@1952: pPlugin->InitIdentity(); ds@1952: if (pPlugin->Call_AskPluginLoad(error, maxlength)) ds@1952: { ds@1952: /* Autoload any modules */ ds@1952: LoadOrRequireExtensions(pPlugin, 1, error, maxlength); ds@1952: } ds@1952: else ds@1952: { ds@1952: pPlugin->SetErrorState(Plugin_Failed, "%s", error); ds@1952: } ds@1952: } ds@1952: ds@1952: /* Save the time stamp */ ds@1952: time_t t = pPlugin->GetFileTimeStamp(); ds@1952: pPlugin->SetTimeStamp(t); ds@1952: ds@1952: if (_plugin) ds@1952: { ds@1952: *_plugin = pPlugin; ds@1952: } ds@1952: ds@1952: return (pPlugin->GetStatus() == Plugin_Loaded) ? LoadRes_Successful : LoadRes_Failure; ds@1952: } ds@1952: ds@1952: IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType type, char error[], size_t maxlength, bool *wasloaded) ds@1952: { ds@1952: CPlugin *pl; ds@1952: LoadRes res; ds@1952: ds@1952: *wasloaded = false; ds@1952: if ((res=_LoadPlugin(&pl, path, debug, type, error, maxlength)) == LoadRes_Failure) ds@1952: { ds@1952: delete pl; ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: if (res == LoadRes_AlreadyLoaded) ds@1952: { ds@1952: *wasloaded = true; ds@1952: return pl; ds@1952: } ds@1952: ds@1952: if (res == LoadRes_NeverLoad) ds@1952: { ds@1952: if (m_LoadingLocked) ds@1952: { ds@1952: UTIL_Format(error, maxlength, "There is a global plugin loading lock in effect"); ds@1952: } ds@1952: else ds@1952: { ds@1952: UTIL_Format(error, maxlength, "This plugin is blocked from loading (see plugin_settings.cfg)"); ds@1952: } ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: AddPlugin(pl); ds@1952: ds@1952: /* Run second pass if we need to */ ds@1952: if (IsLateLoadTime() && pl->GetStatus() == Plugin_Loaded) ds@1952: { ds@1952: if (!RunSecondPass(pl, error, maxlength)) ds@1952: { ds@1952: UnloadPlugin(pl); ds@1952: return NULL; ds@1952: } ds@1952: pl->Call_OnAllPluginsLoaded(); ds@1952: } ds@1952: ds@1952: return pl; ds@1952: } ds@1952: ds@1952: void CPluginManager::LoadAutoPlugin(const char *plugin) ds@1952: { ds@1952: CPlugin *pl = NULL; ds@1952: LoadRes res; ds@1952: char error[255] = "Unknown error"; ds@1952: ds@1952: if ((res=_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure) ds@1952: { ds@1952: g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error); ds@1952: pl->SetErrorState(Plugin_Failed, "%s", error); ds@1952: } ds@1952: ds@1952: if (res == LoadRes_Successful) ds@1952: { ds@1952: AddPlugin(pl); ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::AddPlugin(CPlugin *pPlugin) ds@1952: { ds@1952: List::iterator iter; ds@1952: IPluginsListener *pListener; ds@1952: ds@1952: for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++) ds@1952: { ds@1952: pListener = (*iter); ds@1952: pListener->OnPluginCreated(pPlugin); ds@1952: } ds@1952: ds@1952: m_plugins.push_back(pPlugin); ds@1952: sm_trie_insert(m_LoadLookup, pPlugin->m_filename, pPlugin); ds@1952: } ds@1952: ds@1952: void CPluginManager::LoadAll_SecondPass() ds@1952: { ds@1952: List::iterator iter; ds@1952: CPlugin *pPlugin; ds@1952: ds@1952: char error[256]; ds@1952: for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++) ds@1952: { ds@1952: pPlugin = (*iter); ds@1952: if (pPlugin->GetStatus() == Plugin_Loaded) ds@1952: { ds@1952: error[0] = '\0'; ds@1952: if (!RunSecondPass(pPlugin, error, sizeof(error))) ds@1952: { ds@1952: g_Logger.LogError("[SM] Unable to load plugin \"%s\": %s", pPlugin->GetFilename(), error); ds@1952: pPlugin->SetErrorState(Plugin_Failed, "%s", error); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: m_AllPluginsLoaded = true; ds@1952: } ds@1952: ds@1952: bool CPluginManager::FindOrRequirePluginDeps(CPlugin *pPlugin, char *error, size_t maxlength) ds@1952: { ds@1952: struct _pl ds@1952: { ds@1952: cell_t name; ds@1952: cell_t file; ds@1952: cell_t required; ds@1952: } *pl; ds@1952: ds@1952: IPluginContext *pBase = pPlugin->GetBaseContext(); ds@1952: uint32_t num = pBase->GetPubVarsNum(); ds@1952: sp_pubvar_t *pubvar; ds@1952: char *name, *file; ds@1952: char pathfile[PLATFORM_MAX_PATH]; ds@1952: ds@1952: for (uint32_t i=0; iGetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (strncmp(pubvar->name, "__pl_", 5) == 0) ds@1952: { ds@1952: pl = (_pl *)pubvar->offs; ds@1952: if (pBase->LocalToString(pl->file, &file) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (pBase->LocalToString(pl->name, &name) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: g_LibSys.GetFileFromPath(pathfile, sizeof(pathfile), pPlugin->GetFilename()); ds@1952: if (strcmp(pathfile, file) == 0) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (pl->required == false) ds@1952: { ds@1952: IPluginFunction *pFunc; ds@1952: char buffer[64]; ds@1952: UTIL_Format(buffer, sizeof(buffer), "__pl_%s_SetNTVOptional", &pubvar->name[5]); ds@1952: if ((pFunc=pBase->GetFunctionByName(buffer))) ds@1952: { ds@1952: cell_t res; ds@1952: pFunc->Execute(&res); dvander@2193: if (pPlugin->GetBaseContext()->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Fatal error during initializing plugin load"); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: /* Check that we aren't registering the same library twice */ ds@1952: if (pPlugin->m_RequiredLibs.find(name) == pPlugin->m_RequiredLibs.end()) ds@1952: { ds@1952: pPlugin->m_RequiredLibs.push_back(name); ds@1952: } ds@1952: else ds@1952: { ds@1952: continue; ds@1952: } ds@1952: List::iterator iter; ds@1952: CPlugin *pl; ds@1952: bool found = false; ds@1952: for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++) ds@1952: { ds@1952: pl = (*iter); ds@1952: if (pl->m_Libraries.find(name) != pl->m_Libraries.end()) ds@1952: { ds@1952: found = true; ds@1952: break; ds@1952: } ds@1952: } ds@1952: if (!found) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Could not find required plugin \"%s\"", name); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength) ds@1952: { ds@1952: /* Find any extensions this plugin needs */ ds@1952: struct _ext ds@1952: { ds@1952: cell_t name; ds@1952: cell_t file; ds@1952: cell_t autoload; ds@1952: cell_t required; ds@1952: } *ext; ds@1952: ds@1952: IPluginContext *pBase = pPlugin->GetBaseContext(); ds@1952: uint32_t num = pBase->GetPubVarsNum(); ds@1952: sp_pubvar_t *pubvar; ds@1952: IExtension *pExt; ds@1952: char path[PLATFORM_MAX_PATH]; ds@1952: char *file, *name; ds@1952: for (uint32_t i=0; iGetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (strncmp(pubvar->name, "__ext_", 6) == 0) ds@1952: { ds@1952: ext = (_ext *)pubvar->offs; ds@1952: if (pBase->LocalToString(ext->file, &file) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (pBase->LocalToString(ext->name, &name) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (pass == 1) ds@1952: { ds@1952: /* Attempt to auto-load if necessary */ ds@1952: if (ext->autoload) ds@1952: { ds@1952: g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file); ds@1952: g_Extensions.LoadAutoExtension(path); ds@1952: } ds@1952: } ds@1952: else if (pass == 2) ds@1952: { ds@1952: /* Is this required? */ ds@1952: if (ext->required) ds@1952: { ds@1952: g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file); ds@1952: if ((pExt = g_Extensions.FindExtensionByFile(path)) == NULL) ds@1952: { ds@1952: pExt = g_Extensions.FindExtensionByName(name); ds@1952: } ds@1952: /* :TODO: should we bind to unloaded extensions? ds@1952: * Currently the extension manager will ignore this. ds@1952: */ ds@1952: if (!pExt || !pExt->IsRunning(NULL, 0)) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: else ds@1952: { ds@1952: g_Extensions.BindChildPlugin(pExt, pPlugin); ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: IPluginFunction *pFunc; ds@1952: char buffer[64]; ds@1952: UTIL_Format(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar->name[6]); ds@1952: ds@1952: if ((pFunc = pBase->GetFunctionByName(buffer)) != NULL) ds@1952: { ds@1952: cell_t res; ds@1952: pFunc->Execute(&res); dvander@2193: if (pPlugin->GetBaseContext()->GetLastNativeError() != SP_ERROR_NONE) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: UTIL_Format(error, maxlength, "Fatal error during plugin initialization (ext req)"); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxlength) ds@1952: { ds@1952: /* Second pass for extension requirements */ ds@1952: if (!LoadOrRequireExtensions(pPlugin, 2, error, maxlength)) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (!FindOrRequirePluginDeps(pPlugin, error, maxlength)) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: dvander@2086: /* Run another binding pass */ dvander@2086: g_ShareSys.BindNativesToPlugin(pPlugin, false); ds@1952: dvander@2086: /* Find any unbound natives. Right now, these are not allowed. */ ds@1952: IPluginContext *pContext = pPlugin->GetBaseContext(); ds@1952: uint32_t num = pContext->GetNativesNum(); ds@1952: sp_native_t *native; ds@1952: for (unsigned int i=0; iGetNativeByIndex(i, &native) != SP_ERROR_NONE) ds@1952: { ds@1952: break; ds@1952: } ds@1952: if (native->status == SP_NATIVE_UNBOUND ds@1952: && native->name[0] != '@' ds@1952: && !(native->flags & SP_NTVFLAG_OPTIONAL)) ds@1952: { ds@1952: if (error) ds@1952: { ds@1952: snprintf(error, maxlength, "Native \"%s\" was not found.", native->name); ds@1952: } ds@1952: return false; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Finish by telling all listeners */ ds@1952: List::iterator iter; ds@1952: IPluginsListener *pListener; ds@1952: for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++) ds@1952: { ds@1952: pListener = (*iter); ds@1952: pListener->OnPluginLoaded(pPlugin); ds@1952: } ds@1952: ds@1952: /* Tell this plugin to finish initializing itself */ ds@1952: pPlugin->Call_OnPluginStart(); ds@1952: ds@1952: /* Now, if we have fake natives, go through all plugins that might need rebinding */ dvander@2086: if (pPlugin->GetStatus() <= Plugin_Paused && pPlugin->m_Natives.size()) ds@1952: { ds@1952: List::iterator pl_iter; ds@1952: CPlugin *pOther; ds@1952: for (pl_iter = m_plugins.begin(); ds@1952: pl_iter != m_plugins.end(); ds@1952: pl_iter++) ds@1952: { ds@1952: pOther = (*pl_iter); ds@1952: if ((pOther->GetStatus() == Plugin_Error ds@1952: && (pOther->m_FakeNativesMissing || pOther->m_LibraryMissing)) ds@1952: || pOther->m_FakeNativesMissing) ds@1952: { ds@1952: TryRefreshDependencies(pOther); ds@1952: } dvander@2086: else if ((pOther->GetStatus() == Plugin_Running dvander@2086: || pOther->GetStatus() == Plugin_Paused) dvander@2086: && pOther != pPlugin) dvander@2086: { dvander@2086: List::iterator nv_iter; dvander@2086: for (nv_iter = pPlugin->m_Natives.begin(); dvander@2086: nv_iter != pPlugin->m_Natives.end(); dvander@2086: nv_iter++) dvander@2086: { dvander@2086: g_ShareSys.BindNativeToPlugin(pOther, (*nv_iter)); dvander@2086: } dvander@2086: } ds@1952: } ds@1952: } ds@1952: ds@1952: /* Go through our libraries and tell other plugins they're added */ ds@1952: List::iterator s_iter; ds@1952: for (s_iter = pPlugin->m_Libraries.begin(); ds@1952: s_iter != pPlugin->m_Libraries.end(); ds@1952: s_iter++) ds@1952: { ds@1952: OnLibraryAction((*s_iter).c_str(), true, false); ds@1952: } ds@1952: dvander@2042: /* :TODO: optimize? does this even matter? */ dvander@2042: pPlugin->GetPhrases()->AddPhraseFile("core.phrases"); ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: void CPluginManager::TryRefreshDependencies(CPlugin *pPlugin) ds@1952: { ds@1952: assert(pPlugin->GetBaseContext() != NULL); ds@1952: dvander@2086: g_ShareSys.BindNativesToPlugin(pPlugin, false); ds@1952: ds@1952: List::iterator lib_iter; ds@1952: List::iterator req_iter; ds@1952: List::iterator pl_iter; ds@1952: CPlugin *pl; ds@1952: for (req_iter=pPlugin->m_RequiredLibs.begin(); req_iter!=pPlugin->m_RequiredLibs.end(); req_iter++) ds@1952: { ds@1952: bool found = false; ds@1952: for (pl_iter=m_plugins.begin(); pl_iter!=m_plugins.end(); pl_iter++) ds@1952: { ds@1952: pl = (*pl_iter); ds@1952: for (lib_iter=pl->m_Libraries.begin(); lib_iter!=pl->m_Libraries.end(); lib_iter++) ds@1952: { ds@1952: if ((*req_iter) == (*lib_iter)) ds@1952: { ds@1952: found = true; ds@1952: } ds@1952: } ds@1952: } ds@1952: if (!found) ds@1952: { ds@1952: pPlugin->SetErrorState(Plugin_Error, "Library not found: %s", (*req_iter).c_str()); ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Find any unbound natives ds@1952: * Right now, these are not allowed ds@1952: */ ds@1952: IPluginContext *pContext = pPlugin->GetBaseContext(); ds@1952: uint32_t num = pContext->GetNativesNum(); ds@1952: sp_native_t *native; ds@1952: for (unsigned int i=0; iGetNativeByIndex(i, &native) != SP_ERROR_NONE) ds@1952: { ds@1952: break; ds@1952: } ds@1952: if (native->status == SP_NATIVE_UNBOUND && !(native->flags & SP_NTVFLAG_OPTIONAL)) ds@1952: { ds@1952: pPlugin->SetErrorState(Plugin_Error, "Native not found: %s", native->name); ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: if (pPlugin->GetStatus() == Plugin_Error) ds@1952: { ds@1952: /* If we got here, all natives are okay again! */ ds@1952: pPlugin->m_status = Plugin_Running; dvander@2193: if (pPlugin->m_pRuntime->IsPaused()) ds@1952: { dvander@2193: pPlugin->m_pRuntime->SetPauseState(false); ds@1952: _SetPauseState(pPlugin, false); ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: bool CPluginManager::UnloadPlugin(IPlugin *plugin) ds@1952: { ds@1952: CPlugin *pPlugin = (CPlugin *)plugin; ds@1952: ds@1952: /* This prevents removal during insertion or anything else weird */ ds@1952: if (m_plugins.find(pPlugin) == m_plugins.end()) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: IPluginContext *pContext = plugin->GetBaseContext(); ds@1952: if (pContext->IsInExec()) ds@1952: { ds@1952: char buffer[255]; ds@1952: UTIL_Format(buffer, sizeof(buffer), "sm plugins unload %s\n", plugin->GetFilename()); ds@1952: engine->ServerCommand(buffer); ds@1952: return false; ds@1952: } ds@1952: ds@1952: /* Remove us from the lookup table and linked list */ ds@1952: m_plugins.remove(pPlugin); ds@1952: sm_trie_delete(m_LoadLookup, pPlugin->m_filename); ds@1952: ds@1952: /* Go through our libraries and tell other plugins they're gone */ ds@1952: List::iterator s_iter; ds@1952: for (s_iter = pPlugin->m_Libraries.begin(); ds@1952: s_iter != pPlugin->m_Libraries.end(); ds@1952: s_iter++) ds@1952: { ds@1952: OnLibraryAction((*s_iter).c_str(), true, true); ds@1952: } ds@1952: ds@1952: List::iterator iter; ds@1952: IPluginsListener *pListener; ds@1952: ds@1952: if (pPlugin->GetStatus() <= Plugin_Error) ds@1952: { ds@1952: /* Notify listeners of unloading */ ds@1952: for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++) ds@1952: { ds@1952: pListener = (*iter); ds@1952: pListener->OnPluginUnloaded(pPlugin); ds@1952: } ds@1952: /* Notify plugin */ ds@1952: pPlugin->Call_OnPluginEnd(); ds@1952: } ds@1952: dvander@2086: pPlugin->DropEverything(); ds@1952: ds@1952: for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++) ds@1952: { ds@1952: /* Notify listeners of destruction */ ds@1952: pListener = (*iter); ds@1952: pListener->OnPluginDestroyed(pPlugin); ds@1952: } ds@1952: ds@1952: /* Tell the plugin to delete itself */ ds@1952: delete pPlugin; ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: IPlugin *CPluginManager::FindPluginByContext(const sp_context_t *ctx) ds@1952: { dvander@2193: IPlugin *pPlugin; dvander@2193: IPluginContext *pContext; dvander@2193: dvander@2193: pContext = reinterpret_cast(const_cast(ctx)); dvander@2193: dvander@2193: if (pContext->GetKey(2, (void **)&pPlugin)) dvander@2193: { dvander@2193: return pPlugin; dvander@2193: } dvander@2193: dvander@2193: return NULL; dvander@2193: } dvander@2193: dvander@2193: CPlugin *CPluginManager::GetPluginByCtx(const sp_context_t *ctx) dvander@2193: { dvander@2193: return (CPlugin *)FindPluginByContext(ctx); ds@1952: } ds@1952: ds@1952: unsigned int CPluginManager::GetPluginCount() ds@1952: { ds@1952: return m_plugins.size(); ds@1952: } ds@1952: ds@1952: void CPluginManager::AddPluginsListener(IPluginsListener *listener) ds@1952: { ds@1952: m_listeners.push_back(listener); ds@1952: } ds@1952: ds@1952: void CPluginManager::RemovePluginsListener(IPluginsListener *listener) ds@1952: { ds@1952: m_listeners.remove(listener); ds@1952: } ds@1952: ds@1952: IPluginIterator *CPluginManager::GetPluginIterator() ds@1952: { ds@1952: if (m_iters.empty()) ds@1952: { ds@1952: return new CPluginIterator(&m_plugins); ds@1952: } else { ds@1952: CPluginIterator *iter = m_iters.front(); ds@1952: m_iters.pop(); ds@1952: iter->Reset(); ds@1952: return iter; ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::ReleaseIterator(CPluginIterator *iter) ds@1952: { ds@1952: m_iters.push(iter); ds@1952: } ds@1952: ds@1952: bool CPluginManager::TestAliasMatch(const char *alias, const char *localpath) ds@1952: { ds@1952: /* As an optimization, we do not call strlen, but compute the length in the first pass */ ds@1952: size_t alias_len = 0; ds@1952: size_t local_len = 0; ds@1952: ds@1952: const char *ptr = alias; ds@1952: unsigned int alias_explicit_paths = 0; ds@1952: unsigned int alias_path_end = 0; ds@1952: while (*ptr != '\0') ds@1952: { ds@1952: if (*ptr == '\\' || *ptr == '/') ds@1952: { ds@1952: alias_explicit_paths++; ds@1952: alias_path_end = alias_len; ds@1952: } ds@1952: alias_len++; ds@1952: ptr++; ds@1952: } ds@1952: ds@1952: if (alias_explicit_paths && alias_path_end == alias_len - 1) ds@1952: { ds@1952: /* Trailing slash is totally invalid here */ ds@1952: return false; ds@1952: } ds@1952: ds@1952: ptr = localpath; ds@1952: unsigned int local_explicit_paths = 0; ds@1952: unsigned int local_path_end = 0; ds@1952: while (*ptr != '\0') ds@1952: { ds@1952: if (*ptr == '\\' || *ptr == '/') ds@1952: { ds@1952: local_explicit_paths++; ds@1952: local_path_end = local_len; ds@1952: } ds@1952: local_len++; ds@1952: ptr++; ds@1952: } ds@1952: ds@1952: /* If the alias has more explicit paths than the real path, ds@1952: * no match will be possible. ds@1952: */ ds@1952: if (alias_explicit_paths > local_explicit_paths) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: if (alias_explicit_paths) ds@1952: { ds@1952: /* We need to find if the paths match now. For example, these should all match: ds@1952: * csdm csdm ds@1952: * csdm optional/csdm ds@1952: * csdm/ban optional/crab/csdm/ban ds@1952: */ ds@1952: const char *aliasptr = alias; ds@1952: const char *localptr = localpath; ds@1952: bool match = true; ds@1952: do ds@1952: { ds@1952: if (*aliasptr != *localptr) ds@1952: { ds@1952: /* We have to knock one path off */ ds@1952: local_explicit_paths--; ds@1952: if (alias_explicit_paths > local_explicit_paths) ds@1952: { ds@1952: /* Skip out if we're gonna have an impossible match */ ds@1952: return false; ds@1952: } ds@1952: /* Eat up localptr tokens until we get a result */ ds@1952: while (((localptr - localpath) < (int)local_path_end) ds@1952: && *localptr != '/' ds@1952: && *localptr != '\\') ds@1952: { ds@1952: localptr++; ds@1952: } ds@1952: /* Check if we hit the end of our searchable area. ds@1952: * This probably isn't possible because of the path ds@1952: * count check, but it's a good idea anyway. ds@1952: */ ds@1952: if ((localptr - localpath) >= (int)local_path_end) ds@1952: { ds@1952: return false; ds@1952: } else { ds@1952: /* Consume the slash token */ ds@1952: localptr++; ds@1952: } ds@1952: /* Reset the alias pointer so we can continue consuming */ ds@1952: aliasptr = alias; ds@1952: match = false; ds@1952: continue; ds@1952: } ds@1952: /* Note: ds@1952: * This is safe because if localptr terminates early, aliasptr will too ds@1952: */ ds@1952: do ds@1952: { ds@1952: /* We should never reach the end of the string because of this check. */ ds@1952: bool aliasend = (aliasptr - alias) > (int)alias_path_end; ds@1952: bool localend = (localptr - localpath) > (int)local_path_end; ds@1952: if (aliasend || localend) ds@1952: { ds@1952: if (aliasend && localend) ds@1952: { ds@1952: /* we matched, and we can break out now */ ds@1952: match = true; ds@1952: break; ds@1952: } ds@1952: /* Otherwise, we've hit the end somehow and rest won't match up. Break out. */ ds@1952: match = false; ds@1952: break; ds@1952: } ds@1952: ds@1952: /* If we got here, it's safe to compare the next two tokens */ ds@1952: if (*localptr != *aliasptr) ds@1952: { ds@1952: match = false; ds@1952: break; ds@1952: } ds@1952: localptr++; ds@1952: aliasptr++; ds@1952: } while (true); ds@1952: } while (!match); ds@1952: } ds@1952: ds@1952: /* If we got here, it's time to compare filenames */ ds@1952: const char *aliasptr = alias; ds@1952: const char *localptr = localpath; ds@1952: ds@1952: if (alias_explicit_paths) ds@1952: { ds@1952: aliasptr = &alias[alias_path_end + 1]; ds@1952: } ds@1952: ds@1952: if (local_explicit_paths) ds@1952: { ds@1952: localptr = &localpath[local_path_end + 1]; ds@1952: } ds@1952: ds@1952: while (true) ds@1952: { ds@1952: if (*aliasptr == '*') ds@1952: { ds@1952: /* First, see if this is the last character */ ds@1952: if ((unsigned)(aliasptr - alias) == alias_len - 1) ds@1952: { ds@1952: /* If so, there's no need to match anything else */ ds@1952: return true; ds@1952: } ds@1952: /* Otherwise, we need to search for an appropriate matching sequence in local. ds@1952: * Note that we only need to search up to the next asterisk. ds@1952: */ ds@1952: aliasptr++; ds@1952: bool match = true; ds@1952: const char *local_orig = localptr; ds@1952: do ds@1952: { ds@1952: match = true; ds@1952: while (*aliasptr != '\0' && *aliasptr != '*') ds@1952: { ds@1952: /* Since aliasptr is never '\0', localptr hitting the end will fail */ ds@1952: if (*aliasptr != *localptr) ds@1952: { ds@1952: match = false; ds@1952: break; ds@1952: } ds@1952: aliasptr++; ds@1952: localptr++; ds@1952: } ds@1952: if (!match) ds@1952: { ds@1952: /* If we didn't get a match, we need to advance the search stream. ds@1952: * This will let us skip tokens while still searching for another match. ds@1952: */ ds@1952: localptr = ++local_orig; ds@1952: /* Make sure we don't go out of bounds */ ds@1952: if (*localptr == '\0') ds@1952: { ds@1952: break; ds@1952: } ds@1952: } ds@1952: } while (!match); ds@1952: ds@1952: if (!match) ds@1952: { ds@1952: return false; ds@1952: } else { ds@1952: /* If we got a match, move on to the next token */ ds@1952: continue; ds@1952: } ds@1952: } else if (*aliasptr == '\0') { ds@1952: if (*localptr == '\0' ds@1952: || ds@1952: strcmp(localptr, ".smx") == 0) ds@1952: { ds@1952: return true; ds@1952: } else { ds@1952: return false; ds@1952: } ds@1952: } else if (*aliasptr != *localptr) { ds@1952: return false; ds@1952: } ds@1952: aliasptr++; ds@1952: localptr++; ds@1952: } ds@1952: ds@1952: return true; ds@1952: } ds@1952: ds@1952: bool CPluginManager::IsLateLoadTime() const ds@1952: { ds@1952: return (m_AllPluginsLoaded || !g_SourceMod.IsMapLoading()); ds@1952: } ds@1952: ds@1952: void CPluginManager::OnSourceModAllInitialized() ds@1952: { ds@1952: m_MyIdent = g_ShareSys.CreateCoreIdentity(); ds@1952: ds@1952: HandleAccess sec; ds@1952: g_HandleSys.InitAccessDefaults(NULL, &sec); ds@1952: ds@1952: sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY; ds@1952: sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY; ds@1952: ds@1952: g_PluginType = g_HandleSys.CreateType("Plugin", this, 0, NULL, &sec, m_MyIdent, NULL); ds@1952: g_PluginIdent = g_ShareSys.CreateIdentType("PLUGIN"); ds@1952: ds@1952: g_RootMenu.AddRootConsoleCommand("plugins", "Manage Plugins", this); ds@1952: ds@1952: g_ShareSys.AddInterface(NULL, this); ds@1952: } ds@1952: ds@1952: void CPluginManager::OnSourceModShutdown() ds@1952: { ds@1952: g_RootMenu.RemoveRootConsoleCommand("plugins", this); ds@1952: ds@1952: UnloadAll(); ds@1952: ds@1952: g_HandleSys.RemoveType(g_PluginType, m_MyIdent); ds@1952: g_ShareSys.DestroyIdentType(g_PluginIdent); ds@1952: g_ShareSys.DestroyIdentity(m_MyIdent); ds@1952: } ds@1952: ds@1952: void CPluginManager::OnHandleDestroy(HandleType_t type, void *object) ds@1952: { ds@1952: /* We don't care about the internal object, actually */ ds@1952: } ds@1952: ds@1952: bool CPluginManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize) ds@1952: { ds@1952: *pSize = ((CPlugin *)object)->CalcMemUsage(); ds@1952: return true; ds@1952: } ds@1952: ds@1952: IPlugin *CPluginManager::PluginFromHandle(Handle_t handle, HandleError *err) ds@1952: { ds@1952: IPlugin *pPlugin; ds@1952: HandleError _err; ds@1952: ds@1952: HandleSecurity sec; ds@1952: ds@1952: sec.pOwner = NULL; ds@1952: sec.pIdentity = m_MyIdent; ds@1952: ds@1952: if ((_err=g_HandleSys.ReadHandle(handle, g_PluginType, &sec, (void **)&pPlugin)) != HandleError_None) ds@1952: { ds@1952: pPlugin = NULL; ds@1952: } ds@1952: ds@1952: if (err) ds@1952: { ds@1952: *err = _err; ds@1952: } ds@1952: ds@1952: return pPlugin; ds@1952: } ds@1952: ds@1952: CPlugin *CPluginManager::GetPluginByOrder(int num) ds@1952: { ds@1952: if (num < 1 || num > (int)GetPluginCount()) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: CPlugin *pl; ds@1952: int id = 1; ds@1952: ds@2113: SourceHook::List::iterator iter; ds@2113: for (iter = m_plugins.begin(); iter != m_plugins.end() && id < num; iter++, id++) {} ds@1952: ds@2113: pl = *iter; ds@1952: ds@1952: return pl; ds@1952: } ds@1952: ds@1952: const char *CPluginManager::GetStatusText(PluginStatus st) ds@1952: { ds@1952: switch (st) ds@1952: { ds@1952: case Plugin_Running: ds@1952: return "Running"; ds@1952: case Plugin_Paused: ds@1952: return "Paused"; ds@1952: case Plugin_Error: ds@1952: return "Error"; ds@1952: case Plugin_Uncompiled: ds@1952: return "Uncompiled"; ds@1952: case Plugin_BadLoad: ds@1952: return "Bad Load"; ds@1952: case Plugin_Failed: ds@1952: return "Failed"; ds@1952: default: ds@1952: assert(false); ds@1952: return "-"; ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::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: char buffer[256]; ds@1952: unsigned int id = 1; ds@1952: int plnum = GetPluginCount(); ds@1952: ds@1952: if (!plnum) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] No plugins loaded"); ds@1952: return; ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Listing %d plugin%s:", GetPluginCount(), (plnum > 1) ? "s" : ""); ds@1952: } ds@1952: dvander@2105: CPlugin *pl; dvander@2105: SourceHook::List::iterator iter; dvander@2105: SourceHook::List m_FailList; ds@1952: ds@2113: for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++, id++) ds@1952: { dvander@2105: pl = (*iter); ds@1952: assert(pl->GetStatus() != Plugin_Created); ds@1952: int len = 0; ds@1952: const sm_plugininfo_t *info = pl->GetPublicInfo(); ds@1952: if (pl->GetStatus() != Plugin_Running) ds@1952: { ds@1952: len += UTIL_Format(buffer, sizeof(buffer), " %02d <%s>", id, GetStatusText(pl->GetStatus())); ds@1952: ds@1952: if (pl->GetStatus() <= Plugin_Error) ds@1952: { ds@1952: /* Plugin has failed to load. */ ds@1952: m_FailList.push_back(pl); ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: len += UTIL_Format(buffer, sizeof(buffer), " %02d", id); ds@1952: } ds@1952: len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " \"%s\"", (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename()); ds@1952: if (IS_STR_FILLED(info->version)) ds@1952: { ds@1952: len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", info->version); ds@1952: } ds@1952: if (IS_STR_FILLED(info->author)) ds@1952: { ds@1952: UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", info->author); ds@1952: } ds@1952: g_RootMenu.ConsolePrint("%s", buffer); ds@1952: } ds@1952: ds@1952: if (!m_FailList.empty()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("Load Errors:"); ds@1952: dvander@2105: SourceHook::List::iterator _iter; ds@1952: ds@1952: CPlugin *pl; ds@1952: ds@1952: for (_iter=m_FailList.begin(); _iter!=m_FailList.end(); _iter++) ds@1952: { ds@1952: pl = (CPlugin *)*_iter; ds@1952: g_RootMenu.ConsolePrint("%s: %s",(IS_STR_FILLED(pl->GetPublicInfo()->name)) ? pl->GetPublicInfo()->name : pl->GetFilename(), pl->m_errormsg); ds@1952: } ds@1952: } ds@1952: ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "load") == 0) ds@1952: { ds@1952: if (argcount < 4) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Usage: sm plugins load "); ds@1952: return; ds@1952: } ds@1952: ds@1952: char error[128]; ds@1952: bool wasloaded; ds@1952: const char *filename = command.Arg(3); ds@1952: ds@1952: char pluginfile[256]; ds@1952: const char *ext = g_LibSys.GetFileExtension(filename) ? "" : ".smx"; ds@1952: UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", filename, ext); ds@1952: ds@1952: IPlugin *pl = LoadPlugin(pluginfile, false, PluginType_MapUpdated, error, sizeof(error), &wasloaded); ds@1952: ds@1952: if (wasloaded) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s is already loaded.", pluginfile); ds@1952: return; ds@1952: } ds@1952: ds@1952: if (pl) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Loaded plugin %s successfully.", pluginfile); ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s failed to load: %s.", pluginfile, error); 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 plugins unload <#|file>"); ds@1952: return; ds@1952: } ds@1952: ds@1952: CPlugin *pl; ds@1952: char *end; ds@1952: const char *arg = command.Arg(3); ds@1952: int id = strtol(arg, &end, 10); ds@1952: ds@1952: if (*end == '\0') ds@1952: { ds@1952: pl = GetPluginByOrder(id); ds@1952: if (!pl) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id); ds@1952: return; ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: char pluginfile[256]; ds@1952: const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx"; ds@1952: UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext); ds@1952: ds@1952: if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile); ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: char name[PLATFORM_MAX_PATH]; ds@1952: const sm_plugininfo_t *info = pl->GetPublicInfo(); ds@1952: strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename()); ds@1952: ds@1952: if (UnloadPlugin(pl)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s unloaded successfully.", name); ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Failed to unload plugin %s.", name); ds@1952: } ds@1952: ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "unload_all") == 0) ds@1952: { ds@1952: g_PluginSys.UnloadAll(); ds@1952: g_RootMenu.ConsolePrint("[SM] All plugins have been unloaded."); ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "load_lock") == 0) ds@1952: { ds@1952: if (m_LoadingLocked) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] There is already a loading lock in effect."); ds@1952: } ds@1952: else ds@1952: { ds@1952: m_LoadingLocked = true; ds@1952: g_RootMenu.ConsolePrint("[SM] Loading is now locked; no plugins will be loaded or re-loaded."); ds@1952: } ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "load_unlock") == 0) ds@1952: { ds@1952: if (m_LoadingLocked) ds@1952: { ds@1952: m_LoadingLocked = false; ds@1952: g_RootMenu.ConsolePrint("[SM] The loading lock is no longer in effect."); ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] There was no loading lock in effect."); 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 plugins info <#>"); ds@1952: return; ds@1952: } ds@1952: ds@1952: CPlugin *pl; ds@1952: char *end; ds@1952: const char *arg = command.Arg(3); ds@1952: int id = strtol(arg, &end, 10); ds@1952: ds@1952: if (*end == '\0') ds@1952: { ds@1952: pl = GetPluginByOrder(id); ds@1952: if (!pl) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id); ds@1952: return; ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: char pluginfile[256]; ds@1952: const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx"; ds@1952: UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext); ds@1952: ds@1952: if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile); ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: const sm_plugininfo_t *info = pl->GetPublicInfo(); ds@1952: ds@1952: g_RootMenu.ConsolePrint(" Filename: %s", pl->GetFilename()); ds@1952: if (pl->GetStatus() <= Plugin_Error) ds@1952: { ds@1952: if (IS_STR_FILLED(info->name)) ds@1952: { ds@1952: if (IS_STR_FILLED(info->description)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Title: %s (%s)", info->name, info->description); ds@1952: } else { ds@1952: g_RootMenu.ConsolePrint(" Title: %s", info->name); ds@1952: } ds@1952: } ds@1952: if (IS_STR_FILLED(info->author)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Author: %s", info->author); ds@1952: } ds@1952: if (IS_STR_FILLED(info->version)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Version: %s", info->version); ds@1952: } ds@1952: if (IS_STR_FILLED(info->url)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" URL: %s", info->url); ds@1952: } ds@1952: if (pl->GetStatus() == Plugin_Error) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Error: %s", pl->m_errormsg); ds@1952: } ds@1952: else ds@1952: { dvander@2105: if (pl->GetStatus() == Plugin_Running) dvander@2105: { dvander@2105: if (pl->IsDebugging()) dvander@2105: { dvander@2105: g_RootMenu.ConsolePrint(" Status: running, debugging"); dvander@2105: } dvander@2105: else dvander@2105: { dvander@2105: g_RootMenu.ConsolePrint(" Status: running"); dvander@2105: } dvander@2105: } dvander@2105: else dvander@2105: { dvander@2105: g_RootMenu.ConsolePrint(" Status: not running"); dvander@2105: } ds@1952: ds@1952: const char *typestr = ""; ds@1952: switch (pl->GetType()) ds@1952: { ds@1952: case PluginType_MapUpdated: ds@1952: typestr = "Map Change if Updated"; ds@1952: break; ds@1952: case PluginType_MapOnly: ds@1952: typestr = "Map Change"; ds@1952: break; ds@1952: case PluginType_Private: ds@1952: case PluginType_Global: ds@1952: typestr = "Never"; ds@1952: break; ds@1952: } ds@1952: ds@1952: g_RootMenu.ConsolePrint(" Reloads: %s", typestr); ds@1952: } dvander@2105: if (pl->m_FileVersion >= 3) dvander@2105: { dvander@2105: g_RootMenu.ConsolePrint(" Timestamp: %s", pl->m_DateTime); dvander@2105: } ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" Load error: %s", pl->m_errormsg); ds@1952: g_RootMenu.ConsolePrint(" File info: (title \"%s\") (version \"%s\")", ds@1952: info->name ? info->name : "", ds@1952: info->version ? info->version : ""); ds@1952: if (IS_STR_FILLED(info->url)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint(" File URL: %s", info->url); ds@1952: } ds@1952: } ds@1952: ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "debug") == 0) ds@1952: { ds@1952: if (argcount < 5) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Usage: sm plugins debug <#> [on|off]"); ds@1952: return; ds@1952: } ds@1952: ds@1952: CPlugin *pl; ds@1952: char *end; ds@1952: const char *arg = command.Arg(3); ds@1952: int id = strtol(arg, &end, 10); ds@1952: ds@1952: if (*end == '\0') ds@1952: { ds@1952: pl = GetPluginByOrder(id); ds@1952: if (!pl) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id); ds@1952: return; ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: char pluginfile[256]; ds@1952: const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx"; ds@1952: UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext); ds@1952: ds@1952: if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile); ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: int res; ds@1952: const char *mode = command.Arg(4); ds@1952: if ((res=strcmp("on", mode)) && strcmp("off", mode)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] The only possible options are \"on\" and \"off.\""); ds@1952: return; ds@1952: } ds@1952: ds@1952: bool debug; ds@1952: if (!res) ds@1952: { ds@1952: debug = true; ds@1952: } ds@1952: else ds@1952: { ds@1952: debug = false; ds@1952: } ds@1952: ds@1952: if (debug && pl->IsDebugging()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] This plugin is already in debug mode."); ds@1952: return; ds@1952: } ds@1952: else if (!debug && !pl->IsDebugging()) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Debug mode is already disabled in this plugin."); ds@1952: return; ds@1952: } ds@1952: ds@1952: char error[256]; ds@1952: if (pl->ToggleDebugMode(debug, error, sizeof(error))) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Successfully toggled debug mode on plugin %s.", pl->GetFilename()); ds@1952: return; ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Could not toggle debug mode on plugin %s.", pl->GetFilename()); ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin returned error: %s", error); ds@1952: return; ds@1952: } ds@1952: } ds@1952: else if (strcmp(cmd, "refresh") == 0) ds@1952: { ds@1952: g_SourceMod.DoGlobalPluginLoads(); ds@1952: g_RootMenu.ConsolePrint("[SM] The plugin list has been refreshed and reloaded."); ds@1952: return; ds@1952: } ds@1952: else if (strcmp(cmd, "reload") == 0) ds@1952: { ds@1952: if (argcount < 4) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Usage: sm plugins reload <#|file>"); ds@1952: return; ds@1952: } ds@1952: ds@1952: CPlugin *pl; ds@1952: char *end; ds@1952: const char *arg = command.Arg(3); ds@1952: int id = strtol(arg, &end, 10); ds@1952: ds@1952: if (*end == '\0') ds@1952: { ds@1952: pl = GetPluginByOrder(id); ds@1952: if (!pl) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id); ds@1952: return; ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: char pluginfile[256]; ds@1952: const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx"; ds@1952: UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext); ds@1952: ds@1952: if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile); ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: char name[PLATFORM_MAX_PATH]; ds@1952: const sm_plugininfo_t *info = pl->GetPublicInfo(); ds@1952: strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename()); ds@1952: ds@1952: if (ReloadPlugin(pl)) ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Plugin %s reloaded successfully.", name); ds@1952: } ds@1952: else ds@1952: { ds@1952: g_RootMenu.ConsolePrint("[SM] Failed to reload plugin %s.", name); ds@1952: } ds@1952: ds@1952: return; ds@1952: } ds@1952: } ds@1952: ds@1952: /* Draw the main menu */ ds@1952: g_RootMenu.ConsolePrint("SourceMod Plugins Menu:"); ds@1952: g_RootMenu.DrawGenericOption("debug", "Toggle debug mode on a plugin"); ds@1952: g_RootMenu.DrawGenericOption("info", "Information about a plugin"); ds@1952: g_RootMenu.DrawGenericOption("list", "Show loaded plugins"); ds@1952: g_RootMenu.DrawGenericOption("load", "Load a plugin"); ds@1952: g_RootMenu.DrawGenericOption("load_lock", "Prevents any more plugins from being loaded"); ds@1952: g_RootMenu.DrawGenericOption("load_unlock", "Re-enables plugin loading"); ds@1952: g_RootMenu.DrawGenericOption("refresh", "Reloads/refreshes all plugins in the plugins folder"); ds@1952: g_RootMenu.DrawGenericOption("reload", "Reloads a plugin"); ds@1952: g_RootMenu.DrawGenericOption("unload", "Unload a plugin"); ds@1952: g_RootMenu.DrawGenericOption("unload_all", "Unloads all plugins"); ds@1952: } ds@1952: ds@1952: bool CPluginManager::ReloadPlugin(CPlugin *pl) ds@1952: { ds@1952: List::iterator iter; ds@1952: char filename[PLATFORM_MAX_PATH]; ds@1952: bool debug, wasloaded; ds@1952: PluginType ptype; ds@1952: IPlugin *newpl; ds@1952: int id = 1; ds@1952: ds@1952: strcpy(filename, pl->m_filename); ds@1952: debug = pl->IsDebugging(); ds@1952: ptype = pl->GetType(); ds@1952: ds@1952: for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++, id++) ds@1952: { ds@1952: if ((*iter) == pl) ds@1952: { ds@1952: break; ds@1952: } ds@1952: } ds@1952: ds@1952: if (!UnloadPlugin(pl)) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: if (!(newpl=LoadPlugin(filename, debug, ptype, NULL, 0, &wasloaded))) ds@1952: { ds@1952: return false; ds@1952: } ds@1952: ds@1952: for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++) ds@1952: { ds@1952: if ((*iter) == (CPlugin *)newpl) ds@1952: { ds@1952: m_plugins.erase(iter); ds@1952: break; ds@1952: } ds@1952: } ds@1952: ds@1952: int i; ds@1952: for (i=1, iter=m_plugins.begin(); iter!=m_plugins.end() && i::iterator iter; ds@1952: List tmp_list = m_plugins; ds@1952: CPlugin *pl; ds@1952: time_t t; ds@1952: ds@1952: for (iter=tmp_list.begin(); iter!=tmp_list.end(); iter++) ds@1952: { ds@1952: pl = (*iter); ds@1952: if (pl->GetType() == PluginType_MapOnly) ds@1952: { ds@1952: UnloadPlugin((IPlugin *)pl); ds@1952: } ds@1952: else if (pl->GetType() == PluginType_MapUpdated) ds@1952: { ds@1952: t = pl->GetFileTimeStamp(); ds@1952: if (!t || t > pl->GetTimeStamp()) ds@1952: { ds@1952: pl->SetTimeStamp(t); ds@1952: UnloadPlugin((IPlugin *)pl); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::_SetPauseState(CPlugin *pl, bool paused) ds@1952: { ds@1952: List::iterator iter; ds@1952: IPluginsListener *pListener; ds@1952: for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++) ds@1952: { ds@1952: pListener = (*iter); ds@1952: pListener->OnPluginPauseChange(pl, paused); ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::AddFunctionsToForward(const char *name, IChangeableForward *pForward) ds@1952: { ds@1952: List::iterator iter; ds@1952: CPlugin *pPlugin; ds@1952: IPluginFunction *pFunction; ds@1952: ds@1952: for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++) ds@1952: { ds@1952: pPlugin = (*iter); ds@1952: ds@1952: if (pPlugin->GetStatus() <= Plugin_Paused) ds@1952: { ds@1952: pFunction = pPlugin->GetBaseContext()->GetFunctionByName(name); ds@1952: ds@1952: if (pFunction) ds@1952: { ds@1952: pForward->AddFunction(pFunction); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: CPlugin *CPluginManager::GetPluginFromIdentity(IdentityToken_t *pToken) ds@1952: { ds@1952: if (pToken->type != g_PluginIdent) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: ds@1952: return (CPlugin *)(pToken->ptr); ds@1952: } ds@1952: ds@1952: void CPluginManager::OnLibraryAction(const char *lib, bool is_a_plugin, bool drop) ds@1952: { ds@1952: List::iterator iter; ds@1952: ds@1952: struct _pl ds@1952: { ds@1952: cell_t name; ds@1952: cell_t file; ds@1952: cell_t required; ds@1952: } *plc; ds@1952: ds@1952: struct _ext ds@1952: { ds@1952: cell_t name; ds@1952: cell_t file; ds@1952: cell_t autoload; ds@1952: cell_t required; ds@1952: } *ext; ds@1952: ds@1952: const char *name = drop ? "OnLibraryRemoved" : "OnLibraryAdded"; ds@1952: ds@1952: for (iter=m_plugins.begin(); ds@1952: iter!=m_plugins.end(); ds@1952: iter++) ds@1952: { ds@1952: CPlugin *pl = (*iter); ds@1952: if (pl->GetStatus() != Plugin_Running) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: IPluginContext *pContext = pl->GetBaseContext(); ds@1952: IPluginFunction *pf = pContext->GetFunctionByName(name); ds@1952: if (!pf) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: uint32_t num_vars = pContext->GetPubVarsNum(); ds@1952: for (uint32_t i=0; iGetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: if (is_a_plugin && strncmp(pubvar->name, "__pl_", 5) == 0) ds@1952: { ds@1952: plc = (_pl *)pubvar->offs; ds@1952: if (plc->required) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: char *str; ds@1952: pContext->LocalToString(plc->name, &str); ds@1952: if (strcmp(str, lib) != 0) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: pf->PushString(lib); ds@1952: pf->Execute(NULL); ds@1952: } ds@1952: else if (!is_a_plugin && strncmp(pubvar->name, "__ext_", 6) == 0) ds@1952: { ds@1952: ext = (_ext *)pubvar->offs; ds@1952: if (ext->required) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: char *str; ds@1952: pContext->LocalToString(ext->name, &str); ds@1952: if (strcmp(str, lib) != 0) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: pf->PushString(lib); ds@1952: pf->Execute(NULL); ds@1952: } ds@1952: } ds@1952: } ds@1952: } ds@1952: ds@1952: bool CPluginManager::LibraryExists(const char *lib) ds@1952: { ds@1952: List::iterator iter; ds@1952: ds@1952: for (iter=m_plugins.begin(); ds@1952: iter!=m_plugins.end(); ds@1952: iter++) ds@1952: { ds@1952: CPlugin *pl = (*iter); ds@1952: if (pl->GetStatus() != Plugin_Running) ds@1952: { ds@1952: continue; ds@1952: } ds@1952: List::iterator s_iter; ds@1952: for (s_iter = pl->m_Libraries.begin(); ds@1952: s_iter != pl->m_Libraries.end(); ds@1952: s_iter++) ds@1952: { ds@1952: if ((*s_iter).compare(lib) == 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: void CPluginManager::AllPluginsLoaded() ds@1952: { ds@1952: List::iterator iter; ds@1952: CPlugin *pl; ds@1952: ds@1952: for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++) ds@1952: { ds@1952: pl = (*iter); ds@1952: pl->Call_OnAllPluginsLoaded(); ds@1952: } ds@1952: } ds@1952: ds@1952: void CPluginManager::UnloadAll() ds@1952: { ds@1952: List::iterator iter; ds@1952: while ( (iter = m_plugins.begin()) != m_plugins.end() ) ds@1952: { ds@1952: UnloadPlugin((*iter)); ds@1952: } ds@1952: } ds@1952: ds@1952: int CPluginManager::GetOrderOfPlugin(IPlugin *pl) ds@1952: { ds@1952: int id = 1; ds@1952: List::iterator iter; ds@1952: ds@1952: for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++, id++) ds@1952: { ds@1952: if ((*iter) == pl) ds@1952: { ds@1952: return id; ds@1952: } ds@1952: } ds@1952: ds@1952: return -1; ds@1952: } ds@1952: ds@1952: CPlugin *CPluginManager::FindPluginByConsoleArg(const char *arg) ds@1952: { ds@1952: int id; ds@1952: char *end; ds@1952: CPlugin *pl; ds@1952: ds@1952: id = strtol(arg, &end, 10); ds@1952: ds@1952: if (*end == '\0') ds@1952: { ds@1952: pl = GetPluginByOrder(id); ds@1952: if (!pl) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: } ds@1952: else ds@1952: { ds@1952: char pluginfile[256]; ds@1952: const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx"; ds@1952: UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext); ds@1952: ds@1952: if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl)) ds@1952: { ds@1952: return NULL; ds@1952: } ds@1952: } ds@1952: ds@1952: return pl; ds@1952: }