core/systems/PluginSys.cpp
author Borja Ferrer <faluco@alliedmods.net>
Thu Jul 12 02:57:43 2007 +0000 (2007-07-12)
changeset 1089 275107f9ca95
parent 1018 e466e7319a68
child 1112 152ceef17866
permissions -rw-r--r--
added experimental optional natives (part 1)
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  * This file is not open source and may not be copied without explicit
     8  * written permission of AlliedModders LLC.  This file may not be redistributed 
     9  * in whole or significant part.
    10  * For information, see LICENSE.txt or http://www.sourcemod.net/license.php
    11  *
    12  * Version: $Id$
    13  */
    14 
    15 #include <stdio.h>
    16 #include "PluginSys.h"
    17 #include "ShareSys.h"
    18 #include "LibrarySys.h"
    19 #include "HandleSys.h"
    20 #include "ForwardSys.h"
    21 #include "sourcemm_api.h"
    22 #include "sourcemod.h"
    23 #include "TextParsers.h"
    24 #include "Logger.h"
    25 #include "ExtensionSys.h"
    26 #include "sm_srvcmds.h"
    27 #include "sm_stringutil.h"
    28 #include "ConCmdManager.h"
    29 #include "PlayerManager.h"
    30 #include "CoreConfig.h"
    31 
    32 CPluginManager g_PluginSys;
    33 HandleType_t g_PluginType = 0;
    34 IdentityType_t g_PluginIdent = 0;
    35 
    36 CPlugin::CPlugin(const char *file)
    37 {
    38 	static int MySerial = 0;
    39 
    40 	m_type = PluginType_Private;
    41 	m_status = Plugin_Uncompiled;
    42 	m_serial = ++MySerial;
    43 	m_plugin = NULL;
    44 	m_errormsg[256] = '\0';
    45 	snprintf(m_filename, sizeof(m_filename), "%s", file);
    46 	m_handle = 0;
    47 	m_ident = NULL;
    48 	m_pProps = sm_trie_create();
    49 	m_FakeNativesMissing = false;
    50 }
    51 
    52 CPlugin::~CPlugin()
    53 {
    54 	if (m_handle)
    55 	{
    56 		HandleSecurity sec;
    57 		sec.pOwner = g_PluginSys.GetIdentity();
    58 		sec.pIdentity = sec.pOwner;
    59 
    60 		g_HandleSys.FreeHandle(m_handle, &sec);
    61 		g_ShareSys.DestroyIdentity(m_ident);
    62 	}
    63 
    64 	if (m_ctx.base)
    65 	{
    66 		delete m_ctx.base;
    67 		m_ctx.base = NULL;
    68 	}
    69 	if (m_ctx.ctx)
    70 	{
    71 		m_ctx.vm->FreeContext(m_ctx.ctx);
    72 		m_ctx.ctx = NULL;
    73 	}
    74 	if (m_ctx.co)
    75 	{
    76 		m_ctx.vm->AbortCompilation(m_ctx.co);
    77 		m_ctx.co = NULL;
    78 	}
    79 
    80 	if (m_plugin)
    81 	{
    82 		g_pSourcePawn->FreeFromMemory(m_plugin);
    83 		m_plugin = NULL;
    84 	}
    85 	if (m_pProps)
    86 	{
    87 		sm_trie_destroy(m_pProps);
    88 	}
    89 	for (size_t i=0; i<m_configs.size(); i++)
    90 	{
    91 		delete m_configs[i];
    92 	}
    93 	m_configs.clear();
    94 }
    95 
    96 void CPlugin::InitIdentity()
    97 {
    98 	if (!m_handle)
    99 	{
   100 		m_ident = g_ShareSys.CreateIdentity(g_PluginIdent, this);
   101 		m_handle = g_HandleSys.CreateHandle(g_PluginType, this, g_PluginSys.GetIdentity(), g_PluginSys.GetIdentity(), NULL);
   102 		m_ctx.base->SetIdentity(m_ident);
   103 	}
   104 }
   105 
   106 Handle_t CPlugin::GetMyHandle()
   107 {
   108 	return m_handle;
   109 }
   110 
   111 CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength)
   112 {
   113 	char fullpath[PLATFORM_MAX_PATH];
   114 	g_SourceMod.BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file);
   115 	FILE *fp = fopen(fullpath, "rb");
   116 
   117 	CPlugin *pPlugin = new CPlugin(file);
   118 
   119 	if (!fp)
   120 	{
   121 		if (error)
   122 		{
   123 			snprintf(error, maxlength, "Unable to open file");
   124 		}
   125 		pPlugin->m_status = Plugin_BadLoad;
   126 		return pPlugin;
   127 	}
   128 
   129 	int err;
   130 	sp_plugin_t *pl = g_pSourcePawn->LoadFromFilePointer(fp, &err);
   131 	if (pl == NULL)
   132 	{
   133 		fclose(fp);
   134 		if (error)
   135 		{
   136 			snprintf(error, maxlength, "Error %d while parsing plugin", err);
   137 		}
   138 		pPlugin->m_status = Plugin_BadLoad;
   139 		return pPlugin;
   140 	}
   141 
   142 	fclose(fp);
   143 
   144 	pPlugin->m_plugin = pl;
   145 
   146 	return pPlugin;
   147 }
   148 
   149 bool CPlugin::GetProperty(const char *prop, void **ptr, bool remove/* =false */)
   150 {
   151 	bool exists = sm_trie_retrieve(m_pProps, prop, ptr);
   152 
   153 	if (exists && remove)
   154 	{
   155 		sm_trie_delete(m_pProps, prop);
   156 	}
   157 
   158 	return exists;
   159 }
   160 
   161 bool CPlugin::SetProperty(const char *prop, void *ptr)
   162 {
   163 	return sm_trie_insert(m_pProps, prop, ptr);
   164 }
   165 
   166 ICompilation *CPlugin::StartMyCompile(IVirtualMachine *vm)
   167 {
   168 	if (!m_plugin)
   169 	{
   170 		return NULL;
   171 	}
   172 
   173 	/* :NOTICE: We will eventually need to change these natives
   174 	 * for swapping in new contexts
   175 	 */
   176 	if (m_ctx.co || m_ctx.ctx)
   177 	{
   178 		return NULL;
   179 	}
   180 
   181 	m_status = Plugin_Uncompiled;
   182 
   183 	m_ctx.vm = vm;
   184 	m_ctx.co = vm->StartCompilation(m_plugin);
   185 
   186 	return m_ctx.co;
   187 }
   188 
   189 void CPlugin::CancelMyCompile()
   190 {
   191 	if (!m_ctx.co)
   192 	{
   193 		return;
   194 	}
   195 
   196 	m_ctx.vm->AbortCompilation(m_ctx.co);
   197 	m_ctx.co = NULL;
   198 	m_ctx.vm = NULL;
   199 }
   200 
   201 bool CPlugin::FinishMyCompile(char *error, size_t maxlength)
   202 {
   203 	if (!m_ctx.co)
   204 	{
   205 		return false;
   206 	}
   207 
   208 	int err;
   209 	m_ctx.ctx = m_ctx.vm->CompileToContext(m_ctx.co, &err);
   210 	if (!m_ctx.ctx)
   211 	{
   212 		memset(&m_ctx, 0, sizeof(m_ctx));
   213 		if (error)
   214 		{
   215 			snprintf(error, maxlength, "JIT failed to compile (error %d)", err);
   216 		}
   217 		return false;
   218 	}
   219 
   220 	m_ctx.base = new BaseContext(m_ctx.ctx);
   221 	m_ctx.ctx->user[SM_CONTEXTVAR_MYSELF] = (void *)this;
   222 
   223 	m_status = Plugin_Created;
   224 	m_ctx.co = NULL;
   225 
   226 	UpdateInfo();
   227 
   228 	return true;
   229 }
   230 
   231 void CPlugin::SetErrorState(PluginStatus status, const char *error_fmt, ...)
   232 {
   233 	PluginStatus old_status = m_status;
   234 	m_status = status;
   235 
   236 	if (old_status == Plugin_Running)
   237 	{
   238 		/* Tell everyone we're now paused */
   239 		g_PluginSys._SetPauseState(this, true);
   240 	}
   241 
   242 	va_list ap;
   243 	va_start(ap, error_fmt);
   244 	vsnprintf(m_errormsg, sizeof(m_errormsg), error_fmt, ap);
   245 	va_end(ap);
   246 
   247 	if (m_ctx.ctx)
   248 	{
   249 		m_ctx.ctx->flags |= SPFLAG_PLUGIN_PAUSED;
   250 	}
   251 }
   252 
   253 void CPlugin::UpdateInfo()
   254 {
   255 	/* Now grab the info */
   256 	uint32_t idx;
   257 	IPluginContext *base = GetBaseContext();
   258 	int err = base->FindPubvarByName("myinfo", &idx);
   259 
   260 	memset(&m_info, 0, sizeof(m_info));
   261 
   262 	if (err == SP_ERROR_NONE)
   263 	{
   264 		struct sm_plugininfo_c_t
   265 		{
   266 			cell_t name;
   267 			cell_t description;
   268 			cell_t author;
   269 			cell_t version;
   270 			cell_t url;
   271 		};
   272 		sm_plugininfo_c_t *cinfo;
   273 		cell_t local_addr;
   274 
   275 		base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&cinfo);
   276 		base->LocalToString(cinfo->name, (char **)&m_info.name);
   277 		base->LocalToString(cinfo->description, (char **)&m_info.description);
   278 		base->LocalToString(cinfo->author, (char **)&m_info.author);
   279 		base->LocalToString(cinfo->url, (char **)&m_info.url);
   280 		base->LocalToString(cinfo->version, (char **)&m_info.version);
   281 	}
   282 
   283 	m_info.author = m_info.author ? m_info.author : "";
   284 	m_info.description = m_info.description ? m_info.description : "";
   285 	m_info.name = m_info.name ? m_info.name : "";
   286 	m_info.url = m_info.url ? m_info.url : "";
   287 	m_info.version = m_info.version ? m_info.version : "";
   288 }
   289 
   290 void CPlugin::Call_OnPluginStart()
   291 {
   292 	if (m_status != Plugin_Loaded)
   293 	{
   294 		return;
   295 	}
   296 
   297 	m_status = Plugin_Running;
   298 
   299 	cell_t result;
   300 	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginStart");
   301 	if (!pFunction)
   302 	{
   303 		return;
   304 	}
   305 
   306 	int err;
   307 	if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE)
   308 	{
   309 		SetErrorState(Plugin_Error, "Error detected in plugin startup (see error logs)");
   310 	} else {
   311 		if (g_OnMapStarted)
   312 		{
   313 			if ((pFunction = m_ctx.base->GetFunctionByName("OnMapStart")) != NULL)
   314 			{
   315 				pFunction->Execute(NULL);
   316 			}
   317 		}
   318 		if (SM_AreConfigsExecuted())
   319 		{
   320 			SM_ExecuteForPlugin(GetBaseContext());
   321 		}
   322 	}
   323 }
   324 
   325 void CPlugin::Call_OnPluginEnd()
   326 {
   327 	if (m_status > Plugin_Paused)
   328 	{
   329 		return;
   330 	}
   331 
   332 	cell_t result;
   333 	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginEnd");
   334 	if (!pFunction)
   335 	{
   336 		return;
   337 	}
   338 
   339 	pFunction->Execute(&result);
   340 }
   341 
   342 bool CPlugin::Call_AskPluginLoad(char *error, size_t maxlength)
   343 {
   344 	if (m_status != Plugin_Created)
   345 	{
   346 		return false;
   347 	}
   348 
   349 	m_status = Plugin_Loaded;
   350 
   351 	int err;
   352 	cell_t result;
   353 	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("AskPluginLoad");
   354 
   355 	if (!pFunction)
   356 	{
   357 		return true;
   358 	}
   359 
   360 	m_IsInAskPluginLoad = true;
   361 	pFunction->PushCell(m_handle);
   362 	pFunction->PushCell(g_PluginSys.IsLateLoadTime() ? 1 : 0);
   363 	pFunction->PushStringEx(error, maxlength, 0, SM_PARAM_COPYBACK);
   364 	pFunction->PushCell(maxlength);
   365 	if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE)
   366 	{
   367 		m_IsInAskPluginLoad = false;
   368 		return false;
   369 	}
   370 
   371 	m_IsInAskPluginLoad = false;
   372 
   373 	if (!result || m_status != Plugin_Loaded)
   374 	{
   375 		return false;
   376 	}
   377 
   378 	return true;
   379 }
   380 
   381 const sp_plugin_t *CPlugin::GetPluginStructure()
   382 {
   383 	return m_plugin;
   384 }
   385 
   386 IPluginContext *CPlugin::GetBaseContext()
   387 {
   388 	return m_ctx.base;
   389 }
   390 
   391 sp_context_t *CPlugin::GetContext()
   392 {
   393 	return m_ctx.ctx;
   394 }
   395 
   396 const char *CPlugin::GetFilename()
   397 {
   398 	return m_filename;
   399 }
   400 
   401 PluginType CPlugin::GetType()
   402 {
   403 	return m_type;
   404 }
   405 
   406 const sm_plugininfo_t *CPlugin::GetPublicInfo()
   407 {
   408 	return &m_info;
   409 }
   410 
   411 unsigned int CPlugin::GetSerial()
   412 {
   413 	return m_serial;
   414 }
   415 
   416 PluginStatus CPlugin::GetStatus()
   417 {
   418 	return m_status;
   419 }
   420 
   421 bool CPlugin::IsDebugging()
   422 {
   423 	if (!m_ctx.ctx)
   424 	{
   425 		return false;
   426 	}
   427 
   428 	return ((m_ctx.ctx->flags & SP_FLAG_DEBUG) == SP_FLAG_DEBUG);
   429 }
   430 
   431 bool CPlugin::SetPauseState(bool paused)
   432 {
   433 	if (paused && GetStatus() != Plugin_Paused)
   434 	{
   435 		return false;
   436 	} else if (!paused && GetStatus() != Plugin_Running) {
   437 		return false;
   438 	}
   439 
   440 	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginPauseChange");
   441 	if (pFunction)
   442 	{
   443 		cell_t result;
   444 		pFunction->PushCell(paused ? 1 : 0);
   445 		pFunction->Execute(&result);
   446 	}
   447 
   448 	if (paused)
   449 	{
   450 		m_status = Plugin_Paused;
   451 		m_ctx.ctx->flags |= SPFLAG_PLUGIN_PAUSED;
   452 	} else {
   453 		m_status = Plugin_Running;
   454 		m_ctx.ctx->flags &= ~SPFLAG_PLUGIN_PAUSED;
   455 	}
   456 
   457 	g_PluginSys._SetPauseState(this, paused);
   458 
   459 	return true;
   460 }
   461 
   462 IdentityToken_t *CPlugin::GetIdentity()
   463 {
   464 	return m_ident;
   465 }
   466 
   467 bool CPlugin::ToggleDebugMode(bool debug, char *error, size_t maxlength)
   468 {
   469 	int err;
   470 
   471 	if (!IsRunnable())
   472 	{
   473 		if (error)
   474 		{
   475 			snprintf(error, maxlength, "Plugin is not runnable.");
   476 		}
   477 		return false;
   478 	}
   479 
   480 	if (debug && IsDebugging())
   481 	{
   482 		if (error)
   483 		{
   484 			snprintf(error, maxlength, "Plugin is already in debug mode.");
   485 		}
   486 		return false;
   487 	} else if (!debug && !IsDebugging()) {
   488 		if (error)
   489 		{
   490 			snprintf(error, maxlength, "Plugins is already in production mode.");
   491 		}
   492 		return false;
   493 	}
   494 
   495 	ICompilation *co = g_pVM->StartCompilation(m_ctx.ctx->plugin);
   496 	if (!g_pVM->SetCompilationOption(co, "debug", (debug) ? "1" : "0"))
   497 	{
   498 		if (error)
   499 		{
   500 			snprintf(error, maxlength, "Failed to change plugin mode (JIT failure).");
   501 		}
   502 		return false;
   503 	}
   504 
   505 	sp_context_t *new_ctx = g_pVM->CompileToContext(co, &err);
   506 
   507 	if (new_ctx)
   508 	{
   509 		memcpy(new_ctx->memory, m_ctx.ctx->memory, m_ctx.ctx->mem_size);
   510 		new_ctx->hp = m_ctx.ctx->hp;
   511 		new_ctx->sp = m_ctx.ctx->sp;
   512 		new_ctx->frm = m_ctx.ctx->frm;
   513 		new_ctx->dbreak = m_ctx.ctx->dbreak;
   514 		new_ctx->context = m_ctx.ctx->context;
   515 		memcpy(new_ctx->user, m_ctx.ctx->user, sizeof(m_ctx.ctx->user));
   516 
   517 		uint32_t nativeCount = m_plugin->info.natives_num;
   518 		for (uint32_t i=0; i<nativeCount; i++)
   519 		{
   520 			new_ctx->natives[i].pfn = m_ctx.ctx->natives[i].pfn;
   521 			new_ctx->natives[i].status = m_ctx.ctx->natives[i].status;
   522 		}
   523 
   524 		g_pVM->FreeContext(m_ctx.ctx);
   525 		m_ctx.ctx = new_ctx;
   526 		m_ctx.base->SetContext(new_ctx);
   527 
   528 		UpdateInfo();
   529 	} else {
   530 		if (error)
   531 		{
   532 			snprintf(error, maxlength, "Failed to recompile plugin (JIT error %d).", err);
   533 		}
   534 		return false;
   535 	}
   536 
   537 	return true;
   538 }
   539 
   540 bool CPlugin::IsRunnable()
   541 {
   542 	return (m_status <= Plugin_Paused) ? true : false;
   543 }
   544 
   545 time_t CPlugin::GetFileTimeStamp()
   546 {
   547 	char path[PLATFORM_MAX_PATH];
   548 	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "plugins/%s", m_filename);
   549 #ifdef PLATFORM_WINDOWS
   550 	struct _stat s;
   551 	if (_stat(path, &s) != 0)
   552 #elif defined PLATFORM_POSIX
   553 	struct stat s;
   554 	if (stat(path, &s) != 0)
   555 #endif
   556 	{
   557 		return 0;
   558 	} else {
   559 		return s.st_mtime;
   560 	}
   561 }
   562 
   563 time_t CPlugin::GetTimeStamp()
   564 {
   565 	return m_LastAccess;
   566 }
   567 
   568 void CPlugin::SetTimeStamp(time_t t)
   569 {
   570 	m_LastAccess = t;
   571 }
   572 
   573 void CPlugin::AddLangFile(unsigned int index)
   574 {
   575 	m_PhraseFiles.push_back(index);
   576 }
   577 
   578 size_t CPlugin::GetLangFileCount()
   579 {
   580 	return m_PhraseFiles.size();
   581 }
   582 
   583 unsigned int CPlugin::GetLangFileByIndex(unsigned int index)
   584 {
   585 	return m_PhraseFiles.at(index);
   586 }
   587 
   588 void CPlugin::DependencyDropped(CPlugin *pOwner)
   589 {
   590 	if (!m_ctx.ctx)
   591 	{
   592 		return;
   593 	}
   594 
   595 	List<FakeNative *>::iterator iter;
   596 	FakeNative *pNative;
   597 	sp_native_t *native;
   598 	uint32_t idx;
   599 	unsigned int unbound = 0;
   600 
   601 	for (iter = pOwner->m_fakeNatives.begin();
   602 		 iter != pOwner->m_fakeNatives.end();
   603 		 iter++)
   604 	{
   605 		pNative = (*iter);
   606 		/* Find this native! */
   607 		if (m_ctx.base->FindNativeByName(pNative->name.c_str(), &idx) != SP_ERROR_NONE)
   608 		{
   609 			continue;
   610 		}
   611 		/* Unbind it */
   612 		m_ctx.base->GetNativeByIndex(idx, &native);
   613 		native->pfn = NULL;
   614 		native->status = SP_NATIVE_UNBOUND;
   615 		unbound++;
   616 	}
   617 
   618 	/* :IDEA: in the future, add native trapping? */
   619 	if (unbound)
   620 	{
   621 		m_FakeNativesMissing = true;
   622 		SetErrorState(Plugin_Error, "Depends on plugin: %s", pOwner->GetFilename());
   623 	}
   624 
   625 	m_dependsOn.remove(pOwner);
   626 }
   627 
   628 unsigned int CPlugin::GetConfigCount()
   629 {
   630 	return (unsigned int)m_configs.size();
   631 }
   632 
   633 AutoConfig *CPlugin::GetConfig(unsigned int i)
   634 {
   635 	if (i >= GetConfigCount())
   636 	{
   637 		return NULL;
   638 	}
   639 
   640 	return m_configs[i];
   641 }
   642 
   643 void CPlugin::AddConfig(bool autoCreate, const char *cfg, const char *folder)
   644 {
   645 	AutoConfig *c = new AutoConfig;
   646 
   647 	c->autocfg = cfg;
   648 	c->folder = folder;
   649 	c->create = autoCreate;
   650 
   651 	m_configs.push_back(c);
   652 }
   653 
   654 /*******************
   655  * PLUGIN ITERATOR *
   656  *******************/
   657 
   658 CPluginManager::CPluginIterator::CPluginIterator(List<CPlugin *> *_mylist)
   659 {
   660 	mylist = _mylist;
   661 	Reset();
   662 }
   663 
   664 IPlugin *CPluginManager::CPluginIterator::GetPlugin()
   665 {
   666 	return (*current);
   667 }
   668 
   669 bool CPluginManager::CPluginIterator::MorePlugins()
   670 {
   671 	return (current != mylist->end());
   672 }
   673 
   674 void CPluginManager::CPluginIterator::NextPlugin()
   675 {
   676 	current++;
   677 }
   678 
   679 void CPluginManager::CPluginIterator::Release()
   680 {
   681 	g_PluginSys.ReleaseIterator(this);
   682 }
   683 
   684 CPluginManager::CPluginIterator::~CPluginIterator()
   685 {
   686 }
   687 
   688 void CPluginManager::CPluginIterator::Reset()
   689 {
   690 	current = mylist->begin();
   691 }
   692 
   693 /******************
   694  * PLUGIN MANAGER *
   695  ******************/
   696 
   697 CPluginManager::CPluginManager()
   698 {
   699 	m_LoadLookup = sm_trie_create();
   700 	m_AllPluginsLoaded = false;
   701 	m_MyIdent = NULL;
   702 	m_pNativeLookup = sm_trie_create();
   703 }
   704 
   705 CPluginManager::~CPluginManager()
   706 {
   707 	/* :NOTICE: 
   708 	 * Ignore the fact that there might be plugins in the cache.
   709 	 * This usually means that Core is not being unloaded properly, and everything
   710 	 * will crash anyway.  YAY
   711 	 */
   712 	sm_trie_destroy(m_LoadLookup);
   713 	sm_trie_destroy(m_pNativeLookup);
   714 
   715 	CStack<CPluginManager::CPluginIterator *>::iterator iter;
   716 	for (iter=m_iters.begin(); iter!=m_iters.end(); iter++)
   717 	{
   718 		delete (*iter);
   719 	}
   720 	m_iters.popall();
   721 }
   722 
   723 void CPluginManager::Shutdown()
   724 {
   725 	List<CPlugin *>::iterator iter;
   726 
   727 	while ((iter = m_plugins.begin()) != m_plugins.end())
   728 	{
   729 		UnloadPlugin(*iter);
   730 	}
   731 }
   732 
   733 void CPluginManager::LoadAll_FirstPass(const char *config, const char *basedir)
   734 {
   735 	/* First read in the database of plugin settings */
   736 	SMCParseError err;
   737 	unsigned int line, col;
   738 	m_AllPluginsLoaded = false;
   739 	if ((err=g_TextParser.ParseFile_SMC(config, &m_PluginInfo, &line, &col)) != SMCParse_Okay)
   740 	{
   741 		g_Logger.LogError("[SM] Encountered fatal error parsing file \"%s\"", config);
   742 		const char *err_msg = g_TextParser.GetSMCErrorString(err);
   743 		if (err_msg)
   744 		{
   745 			g_Logger.LogError("[SM] Parse error encountered: \"%s\"", err_msg);
   746 		}
   747 	}
   748 
   749 	LoadPluginsFromDir(basedir, NULL);
   750 }
   751 
   752 void CPluginManager::LoadPluginsFromDir(const char *basedir, const char *localpath)
   753 {
   754 	char base_path[PLATFORM_MAX_PATH];
   755 
   756 	/* Form the current path to start reading from */
   757 	if (localpath == NULL)
   758 	{
   759 		g_LibSys.PathFormat(base_path, sizeof(base_path), "%s", basedir);
   760 	} else {
   761 		g_LibSys.PathFormat(base_path, sizeof(base_path), "%s/%s", basedir, localpath);
   762 	}
   763 
   764 	IDirectory *dir = g_LibSys.OpenDirectory(base_path);
   765 
   766 	if (!dir)
   767 	{
   768 		char error[256];
   769 		g_LibSys.GetPlatformError(error, sizeof(error));
   770 		g_Logger.LogError("[SM] Failure reading from plugins path: %s", localpath);
   771 		g_Logger.LogError("[SM] Platform returned error: %s", error);
   772 		return;
   773 	}
   774 
   775 	while (dir->MoreFiles())
   776 	{
   777 		if (dir->IsEntryDirectory() 
   778 			&& (strcmp(dir->GetEntryName(), ".") != 0)
   779 			&& (strcmp(dir->GetEntryName(), "..") != 0)
   780 			&& (strcmp(dir->GetEntryName(), "disabled") != 0)
   781 			&& (strcmp(dir->GetEntryName(), "optional") != 0))
   782 		{
   783 			char new_local[PLATFORM_MAX_PATH];
   784 			if (localpath == NULL)
   785 			{
   786 				/* If no path yet, don't add a former slash */
   787 				snprintf(new_local, sizeof(new_local), "%s", dir->GetEntryName());
   788 			} else {
   789 				g_LibSys.PathFormat(new_local, sizeof(new_local), "%s/%s", localpath, dir->GetEntryName());
   790 			}
   791 			LoadPluginsFromDir(basedir, new_local);
   792 		} else if (dir->IsEntryFile()) {
   793 			const char *name = dir->GetEntryName();
   794 			size_t len = strlen(name);
   795 			if (len >= 4
   796 				&& strcmp(&name[len-4], ".smx") == 0)
   797 			{
   798 				/* If the filename matches, load the plugin */
   799 				char plugin[PLATFORM_MAX_PATH];
   800 				if (localpath == NULL)
   801 				{
   802 					snprintf(plugin, sizeof(plugin), "%s", name);
   803 				} else {
   804 					g_LibSys.PathFormat(plugin, sizeof(plugin), "%s/%s", localpath, name);
   805 				}
   806 				LoadAutoPlugin(plugin);
   807 			}
   808 		}
   809 		dir->NextEntry();
   810 	}
   811 	g_LibSys.CloseDirectory(dir);
   812 }
   813 
   814 //well i have discovered that gabe newell is very fat, so i wrote this comment now
   815 LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength)
   816 {
   817 	/**
   818 	 * Does this plugin already exist?
   819 	 */
   820 	CPlugin *pPlugin;
   821 	if (sm_trie_retrieve(m_LoadLookup, path, (void **)&pPlugin))
   822 	{
   823 		/* Check to see if we should try reloading it */
   824 		if (pPlugin->GetStatus() == Plugin_BadLoad
   825 			|| pPlugin->GetStatus() == Plugin_Error
   826 			|| pPlugin->GetStatus() == Plugin_Failed)
   827 		{
   828 			UnloadPlugin(pPlugin);
   829 		} else {
   830 			if (_plugin)
   831 			{
   832 				*_plugin = pPlugin;
   833 			}
   834 			return LoadRes_AlreadyLoaded;
   835 		}
   836 	}
   837 
   838 	pPlugin = CPlugin::CreatePlugin(path, error, maxlength);
   839 
   840 	assert(pPlugin != NULL);
   841 
   842 	pPlugin->m_type = PluginType_MapUpdated;
   843 
   844 	ICompilation *co = NULL;
   845 
   846 	if (pPlugin->m_status == Plugin_Uncompiled)
   847 	{
   848 		co = pPlugin->StartMyCompile(g_pVM);
   849 	}
   850 
   851 	PluginSettings *pset;
   852 	unsigned int setcount = m_PluginInfo.GetSettingsNum();
   853 	for (unsigned int i=0; i<setcount; i++)
   854 	{
   855 		if ((pset=m_PluginInfo.GetSettingsIfMatch(i, path)) == NULL)
   856 		{
   857 			continue;
   858 		}
   859 		pPlugin->m_type = pset->type_val;
   860 		if (co)
   861 		{
   862 			for (unsigned int j=0; j<pset->opts_num; j++)
   863 			{
   864 				const char *key, *val;
   865 				m_PluginInfo.GetOptionsForPlugin(pset, j, &key, &val);
   866 				if (!key || !val)
   867 				{
   868 					continue;
   869 				}
   870 				if (!g_pVM->SetCompilationOption(co, key, val))
   871 				{
   872 					if (error)
   873 					{
   874 						snprintf(error, maxlength, "Unable to set JIT option (key \"%s\") (value \"%s\")", key, val);
   875 					}
   876 					pPlugin->CancelMyCompile();
   877 					co = NULL;
   878 					break;
   879 				}
   880 			}
   881 		}
   882 	}
   883 
   884 	/* Do the actual compiling */
   885 	if (co)
   886 	{
   887 		pPlugin->FinishMyCompile(error, maxlength);
   888 		co = NULL;
   889 	}
   890 
   891 	/* Get the status */
   892 	if (pPlugin->GetStatus() == Plugin_Created)
   893 	{
   894 		AddCoreNativesToPlugin(pPlugin);
   895 		pPlugin->InitIdentity();
   896 		if (pPlugin->Call_AskPluginLoad(error, maxlength))
   897 		{
   898 			/* Autoload any modules */
   899 			LoadOrRequireExtensions(pPlugin, 1, error, maxlength);
   900 		}
   901 	}
   902 
   903 	/* Save the time stamp */
   904 	time_t t = pPlugin->GetFileTimeStamp();
   905 	pPlugin->SetTimeStamp(t);
   906 
   907 	if (_plugin)
   908 	{
   909 		*_plugin = pPlugin;
   910 	}
   911 
   912 	return (pPlugin->GetStatus() == Plugin_Loaded) ? LoadRes_Successful : LoadRes_Failure;
   913 }
   914 
   915 IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType type, char error[], size_t maxlength, bool *wasloaded)
   916 {
   917 	CPlugin *pl;
   918 	LoadRes res;
   919 
   920 	*wasloaded = false;
   921 	if ((res=_LoadPlugin(&pl, path, debug, type, error, maxlength)) == LoadRes_Failure)
   922 	{
   923 		delete pl;
   924 		return NULL;
   925 	}
   926 
   927 	if (res == LoadRes_AlreadyLoaded)
   928 	{
   929 		*wasloaded = true;
   930 		return pl;
   931 	}
   932 
   933 	AddPlugin(pl);
   934 
   935 	/* Run second pass if we need to */
   936 	if (IsLateLoadTime() && pl->GetStatus() == Plugin_Loaded)
   937 	{
   938 		if (!RunSecondPass(pl, error, maxlength))
   939 		{
   940 			UnloadPlugin(pl);
   941 			return NULL;
   942 		}
   943 	}
   944 
   945 	return pl;
   946 }
   947 
   948 void CPluginManager::LoadAutoPlugin(const char *plugin)
   949 {
   950 	CPlugin *pl;
   951 	LoadRes res;
   952 	char error[255] = "Unknown error";
   953 
   954 	if ((res=_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure)
   955 	{
   956 		g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error);
   957 		pl->SetErrorState(Plugin_Failed, "%s", error);
   958 	}
   959 
   960 	if (res == LoadRes_Successful)
   961 	{
   962 		AddPlugin(pl);
   963 	}
   964 }
   965 
   966 void CPluginManager::AddPlugin(CPlugin *pPlugin)
   967 {
   968 	List<IPluginsListener *>::iterator iter;
   969 	IPluginsListener *pListener;
   970 
   971 	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
   972 	{
   973 		pListener = (*iter);
   974 		pListener->OnPluginCreated(pPlugin);
   975 	}
   976 
   977 	m_plugins.push_back(pPlugin);
   978 	sm_trie_insert(m_LoadLookup, pPlugin->m_filename, pPlugin);
   979 }
   980 
   981 void CPluginManager::LoadAll_SecondPass()
   982 {
   983 	List<CPlugin *>::iterator iter;
   984 	CPlugin *pPlugin;
   985 
   986 	char error[256];
   987 	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++)
   988 	{
   989 		pPlugin = (*iter);
   990 		if (pPlugin->GetStatus() == Plugin_Loaded)
   991 		{
   992 			error[0] = '\0';
   993 			if (!RunSecondPass(pPlugin, error, sizeof(error)))
   994 			{
   995 				g_Logger.LogError("[SM] Unable to load plugin \"%s\": %s", pPlugin->GetFilename(), error);
   996 				pPlugin->SetErrorState(Plugin_Failed, "%s", error);
   997 			}
   998 		}
   999 	}
  1000 
  1001 	m_AllPluginsLoaded = true;
  1002 }
  1003 
  1004 bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength)
  1005 {
  1006 	/* Find any extensions this plugin needs */
  1007 	struct _ext
  1008 	{
  1009 		cell_t name;
  1010 		cell_t file;
  1011 		cell_t autoload;
  1012 		cell_t required;
  1013 	} *ext;
  1014 
  1015 	IPluginContext *pBase = pPlugin->GetBaseContext();
  1016 	uint32_t num = pBase->GetPubVarsNum();
  1017 	sp_pubvar_t *pubvar;
  1018 	IExtension *pExt;
  1019 	char path[PLATFORM_MAX_PATH];
  1020 	char *file, *name;
  1021 	for (uint32_t i=0; i<num; i++)
  1022 	{
  1023 		if (pBase->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
  1024 		{
  1025 			continue;
  1026 		}
  1027 		if (strncmp(pubvar->name, "__ext_", 6) == 0)
  1028 		{
  1029 			ext = (_ext *)pubvar->offs;
  1030 			if (!ext->required && !ext->autoload)
  1031 			{
  1032 				continue;
  1033 			}
  1034 			if (pBase->LocalToString(ext->file, &file) != SP_ERROR_NONE)
  1035 			{
  1036 				continue;
  1037 			}
  1038 			if (pBase->LocalToString(ext->name, &name) != SP_ERROR_NONE)
  1039 			{
  1040 				continue;
  1041 			}
  1042 			if (pass == 1)
  1043 			{
  1044 				/* Attempt to auto-load if necessary */
  1045 				if (ext->autoload)
  1046 				{
  1047 					g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
  1048 					g_Extensions.LoadAutoExtension(path);
  1049 				}
  1050 			} else if (pass == 2) {
  1051 				/* Is this required? */
  1052 				if (ext->required)
  1053 				{
  1054 					g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
  1055 					if ((pExt = g_Extensions.FindExtensionByFile(path)) == NULL)
  1056 					{
  1057 						pExt = g_Extensions.FindExtensionByName(name);
  1058 					}
  1059 					/* :TODO: should we bind to unloaded extensions?
  1060 					 * Currently the extension manager will ignore this.
  1061 					 */
  1062 					if (!pExt || !pExt->IsRunning(NULL, 0))
  1063 					{
  1064 						if (error)
  1065 						{
  1066 							snprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file);
  1067 						}
  1068 						return false;
  1069 					} else {
  1070 						g_Extensions.BindChildPlugin(pExt, pPlugin);
  1071 					}
  1072 				}
  1073 			}
  1074 		}
  1075 	}
  1076 
  1077 	return true;
  1078 }
  1079 
  1080 bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxlength)
  1081 {
  1082 	/* Second pass for extension requirements */
  1083 	if (!LoadOrRequireExtensions(pPlugin, 2, error, maxlength))
  1084 	{
  1085 		return false;
  1086 	}
  1087 
  1088 	/* Bind all extra natives */
  1089 	g_Extensions.BindAllNativesToPlugin(pPlugin);
  1090 	AddFakeNativesToPlugin(pPlugin);
  1091 
  1092 	/* Find any unbound natives
  1093 	 * Right now, these are not allowed
  1094 	 */
  1095 	IPluginContext *pContext = pPlugin->GetBaseContext();
  1096 	uint32_t num = pContext->GetNativesNum();
  1097 	sp_native_t *native;
  1098 	for (unsigned int i=0; i<num; i++)
  1099 	{
  1100 		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
  1101 		{
  1102 			break;
  1103 		}
  1104 		if (native->status == SP_NATIVE_UNBOUND
  1105 			&& native->name[0] != '@'
  1106 			&& !(native->flags & SP_NTVFLAG_OPTIONAL))
  1107 		{
  1108 			if (error)
  1109 			{
  1110 				snprintf(error, maxlength, "Native \"%s\" was not found.", native->name);
  1111 			}
  1112 			return false;
  1113 		}
  1114 	}
  1115 
  1116 	/* Finish by telling all listeners */
  1117 	List<IPluginsListener *>::iterator iter;
  1118 	IPluginsListener *pListener;
  1119 	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
  1120 	{
  1121 		pListener = (*iter);
  1122 		pListener->OnPluginLoaded(pPlugin);
  1123 	}
  1124 	
  1125 	/* Tell this plugin to finish initializing itself */
  1126 	pPlugin->Call_OnPluginStart();
  1127 
  1128 	/* Now, if we have fake natives, go through all plugins that might need rebinding */
  1129 	if (pPlugin->GetStatus() >= Plugin_Paused && pPlugin->m_fakeNatives.size())
  1130 	{
  1131 		List<CPlugin *>::iterator pl_iter;
  1132 		CPlugin *pOther;
  1133 		for (pl_iter = m_plugins.begin();
  1134 			 pl_iter != m_plugins.end();
  1135 			 pl_iter++)
  1136 		{
  1137 			pOther = (*pl_iter);
  1138 			if (pOther->GetStatus() == Plugin_Error
  1139 				&& pOther->m_FakeNativesMissing)
  1140 			{
  1141 				TryRefreshNatives(pOther);
  1142 			}
  1143 		}
  1144 	}
  1145 
  1146 	return true;
  1147 }
  1148 
  1149 void CPluginManager::AddCoreNativesToPlugin(CPlugin *pPlugin)
  1150 {
  1151 	List<sp_nativeinfo_t *>::iterator iter;
  1152 
  1153 	for (iter=m_natives.begin(); iter!=m_natives.end(); iter++)
  1154 	{
  1155 		sp_nativeinfo_t *natives = (*iter);
  1156 		IPluginContext *ctx = pPlugin->GetBaseContext();
  1157 		unsigned int i=0;
  1158 		/* Attempt to bind every native! */
  1159 		while (natives[i].func != NULL)
  1160 		{
  1161 			ctx->BindNative(&natives[i++]);
  1162 		}
  1163 	}
  1164 }
  1165 
  1166 void CPluginManager::TryRefreshNatives(CPlugin *pPlugin)
  1167 {
  1168 	assert(pPlugin->GetBaseContext() != NULL);
  1169 
  1170 	AddFakeNativesToPlugin(pPlugin);
  1171 
  1172 	/* Find any unbound natives
  1173 	 * Right now, these are not allowed
  1174 	 */
  1175 	IPluginContext *pContext = pPlugin->GetBaseContext();
  1176 	uint32_t num = pContext->GetNativesNum();
  1177 	sp_native_t *native;
  1178 	for (unsigned int i=0; i<num; i++)
  1179 	{
  1180 		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
  1181 		{
  1182 			break;
  1183 		}
  1184 		if (native->status == SP_NATIVE_UNBOUND && !(native->flags & SP_NTVFLAG_OPTIONAL))
  1185 		{
  1186 			pPlugin->SetErrorState(Plugin_Error, "Native not found: %s", native->name);
  1187 			return;
  1188 		}
  1189 	}
  1190 
  1191 	/* If we got here, all natives are okay again! */
  1192 	pPlugin->m_status = Plugin_Running;
  1193 	if ((pPlugin->m_ctx.ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
  1194 	{
  1195 		pPlugin->m_ctx.ctx->flags &= ~SPFLAG_PLUGIN_PAUSED;
  1196 		_SetPauseState(pPlugin, false);
  1197 	}
  1198 }
  1199 
  1200 void CPluginManager::AddFakeNativesToPlugin(CPlugin *pPlugin)
  1201 {
  1202 	IPluginContext *pContext = pPlugin->GetBaseContext();
  1203 	sp_nativeinfo_t native;
  1204 
  1205 	List<FakeNative *>::iterator iter;
  1206 	FakeNative *pNative;
  1207 	sp_context_t *ctx;
  1208 	for (iter = m_Natives.begin(); iter != m_Natives.end(); iter++)
  1209 	{
  1210 		pNative = (*iter);
  1211 		ctx = pNative->ctx->GetContext();
  1212 		if ((ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
  1213 		{
  1214 			/* Ignore natives in paused plugins */
  1215 			continue;
  1216 		}
  1217 		native.name = pNative->name.c_str();
  1218 		native.func = pNative->func;
  1219 		if (pContext->BindNative(&native) == SP_ERROR_NONE)
  1220 		{
  1221 			uint32_t idx;
  1222 			pContext->FindNativeByName(native.name, &idx);
  1223 			if (!(pPlugin->GetContext()->natives[idx].flags & SP_NTVFLAG_OPTIONAL))
  1224 			{
  1225 				continue;
  1226 			}
  1227 			/* Add us as a dependency, but we're careful not to do this circularly! */
  1228 			if (pNative->ctx != pContext)
  1229 			{
  1230 				CPlugin *pOther = GetPluginByCtx(ctx);
  1231 				if (pOther->m_dependents.find(pPlugin) == pOther->m_dependents.end())
  1232 				{
  1233 					pOther->m_dependents.push_back(pPlugin);
  1234 				}
  1235 				if (pPlugin->m_dependsOn.find(pOther) == pPlugin->m_dependsOn.end())
  1236 				{
  1237 					pPlugin->m_dependsOn.push_back(pOther);
  1238 				}
  1239 			}
  1240 		}
  1241 	}
  1242 }
  1243 
  1244 bool CPluginManager::UnloadPlugin(IPlugin *plugin)
  1245 {
  1246 	CPlugin *pPlugin = (CPlugin *)plugin;
  1247 
  1248 	/* This prevents removal during insertion or anything else weird */
  1249 	if (m_plugins.find(pPlugin) == m_plugins.end())
  1250 	{
  1251 		return false;
  1252 	}
  1253 
  1254 	/* Remove us from the lookup table and linked list */
  1255 	m_plugins.remove(pPlugin);
  1256 	sm_trie_delete(m_LoadLookup, pPlugin->m_filename);
  1257 
  1258 	/* Go through all dependent plugins and tell them this plugin is now gone */
  1259 	List<CPlugin *>::iterator pl_iter;
  1260 	CPlugin *pOther;
  1261 	for (pl_iter = pPlugin->m_dependents.begin();
  1262 		 pl_iter != pPlugin->m_dependents.end();
  1263 		 pl_iter++)
  1264 	{
  1265 		pOther = (*pl_iter);
  1266 		pOther->DependencyDropped(pPlugin);
  1267 	}
  1268 
  1269 	/* Tell everyone we depend on that we no longer exist */
  1270 	for (pl_iter = pPlugin->m_dependsOn.begin();
  1271 		 pl_iter != pPlugin->m_dependsOn.end();
  1272 		 pl_iter++)
  1273 	{
  1274 		pOther = (*pl_iter);
  1275 		pOther->m_dependents.remove(pPlugin);
  1276 	}
  1277 
  1278 	/* Remove all of our native functions */
  1279 	List<FakeNative *>::iterator fn_iter;
  1280 	FakeNative *pNative;
  1281 	for (fn_iter = pPlugin->m_fakeNatives.begin();
  1282 		 fn_iter != pPlugin->m_fakeNatives.end();
  1283 		 fn_iter++)
  1284 	{
  1285 		pNative = (*fn_iter);
  1286 		m_Natives.remove(pNative);
  1287 		sm_trie_delete(m_pNativeLookup, pNative->name.c_str());
  1288 		g_pVM->DestroyFakeNative(pNative->func);
  1289 		delete pNative;
  1290 	}
  1291 
  1292 	List<IPluginsListener *>::iterator iter;
  1293 	IPluginsListener *pListener;
  1294 
  1295 	if (pPlugin->GetStatus() <= Plugin_Error)
  1296 	{
  1297 		/* Notify listeners of unloading */
  1298 		for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
  1299 		{
  1300 			pListener = (*iter);
  1301 			pListener->OnPluginUnloaded(pPlugin);
  1302 		}
  1303 		/* Notify plugin */
  1304 		pPlugin->Call_OnPluginEnd();
  1305 	}
  1306 
  1307 	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
  1308 	{
  1309 		/* Notify listeners of destruction */
  1310 		pListener = (*iter);
  1311 		pListener->OnPluginDestroyed(pPlugin);
  1312 	}
  1313 	
  1314 	/* Tell the plugin to delete itself */
  1315 	delete pPlugin;
  1316 
  1317 	return true;
  1318 }
  1319 
  1320 IPlugin *CPluginManager::FindPluginByContext(const sp_context_t *ctx)
  1321 {
  1322 	IPlugin *pl = (IPlugin *)ctx->user[SM_CONTEXTVAR_MYSELF];
  1323 	return pl;
  1324 }
  1325 
  1326 unsigned int CPluginManager::GetPluginCount()
  1327 {
  1328 	return m_plugins.size();
  1329 }
  1330 
  1331 void CPluginManager::AddPluginsListener(IPluginsListener *listener)
  1332 {
  1333 	m_listeners.push_back(listener);
  1334 }
  1335 
  1336 void CPluginManager::RemovePluginsListener(IPluginsListener *listener)
  1337 {
  1338 	m_listeners.remove(listener);
  1339 }
  1340 
  1341 IPluginIterator *CPluginManager::GetPluginIterator()
  1342 {
  1343 	if (m_iters.empty())
  1344 	{
  1345 		return new CPluginIterator(&m_plugins);
  1346 	} else {
  1347 		CPluginIterator *iter = m_iters.front();
  1348 		m_iters.pop();
  1349 		iter->Reset();
  1350 		return iter;
  1351 	}
  1352 }
  1353 
  1354 void CPluginManager::ReleaseIterator(CPluginIterator *iter)
  1355 {
  1356 	m_iters.push(iter);
  1357 }
  1358 
  1359 bool CPluginManager::TestAliasMatch(const char *alias, const char *localpath)
  1360 {
  1361 	/* As an optimization, we do not call strlen, but compute the length in the first pass */
  1362 	size_t alias_len = 0;
  1363 	size_t local_len = 0;
  1364 
  1365 	const char *ptr = alias;
  1366 	unsigned int alias_explicit_paths = 0;
  1367 	unsigned int alias_path_end = 0;
  1368 	while (*ptr != '\0')
  1369 	{
  1370 		if (*ptr == '\\' || *ptr == '/')
  1371 		{
  1372 			alias_explicit_paths++;
  1373 			alias_path_end = alias_len;
  1374 		}
  1375 		alias_len++;
  1376 		ptr++;
  1377 	}
  1378 
  1379 	if (alias_explicit_paths && alias_path_end == alias_len - 1)
  1380 	{
  1381 		/* Trailing slash is totally invalid here */
  1382 		return false;
  1383 	}
  1384 
  1385 	ptr = localpath;
  1386 	unsigned int local_explicit_paths = 0;
  1387 	unsigned int local_path_end = 0;
  1388 	while (*ptr != '\0')
  1389 	{
  1390 		if (*ptr == '\\' || *ptr == '/')
  1391 		{
  1392 			local_explicit_paths++;
  1393 			local_path_end = local_len;
  1394 		}
  1395 		local_len++;
  1396 		ptr++;
  1397 	}
  1398 
  1399 	/* If the alias has more explicit paths than the real path,
  1400 	 * no match will be possible.
  1401 	 */
  1402 	if (alias_explicit_paths > local_explicit_paths)
  1403 	{
  1404 		return false;
  1405 	}
  1406 
  1407 	if (alias_explicit_paths)
  1408 	{
  1409 		/* We need to find if the paths match now.  For example, these should all match:
  1410 		 * csdm     csdm
  1411 		 * csdm     optional/csdm
  1412 		 * csdm/ban optional/crab/csdm/ban
  1413 		*/
  1414 		const char *aliasptr = alias;
  1415 		const char *localptr = localpath;
  1416 		bool match = true;
  1417 		do
  1418 		{
  1419 			if (*aliasptr != *localptr)
  1420 			{
  1421 				/* We have to knock one path off */
  1422 				local_explicit_paths--;
  1423 				if (alias_explicit_paths > local_explicit_paths)
  1424 				{
  1425 					/* Skip out if we're gonna have an impossible match */
  1426 					return false;
  1427 				}
  1428 				/* Eat up localptr tokens until we get a result */
  1429 				while (((localptr - localpath) < (int)local_path_end)
  1430 					&& *localptr != '/'
  1431 					&& *localptr != '\\')
  1432 				{
  1433 					localptr++;
  1434 				}
  1435 				/* Check if we hit the end of our searchable area.
  1436 				 * This probably isn't possible because of the path 
  1437 				 * count check, but it's a good idea anyway.
  1438 				 */
  1439 				if ((localptr - localpath) >= (int)local_path_end)
  1440 				{
  1441 					return false;
  1442 				} else {
  1443 					/* Consume the slash token */
  1444 					localptr++;
  1445 				}
  1446 				/* Reset the alias pointer so we can continue consuming */
  1447 				aliasptr = alias;
  1448 				match = false;
  1449 				continue;
  1450 			}
  1451 			/* Note:
  1452 			 * This is safe because if localptr terminates early, aliasptr will too
  1453 			 */
  1454 			do
  1455 			{
  1456 				/* We should never reach the end of the string because of this check. */
  1457 				bool aliasend = (aliasptr - alias) > (int)alias_path_end;
  1458 				bool localend = (localptr - localpath) > (int)local_path_end;
  1459 				if (aliasend || localend)
  1460 				{
  1461 					if (aliasend && localend)
  1462 					{
  1463 						/* we matched, and we can break out now */
  1464 						match = true;
  1465 						break;
  1466 					}
  1467 					/* Otherwise, we've hit the end somehow and rest won't match up.  Break out. */
  1468 					match = false;
  1469 					break;
  1470 				}
  1471 
  1472 				/* If we got here, it's safe to compare the next two tokens */
  1473 				if (*localptr != *aliasptr)
  1474 				{
  1475 					match = false;
  1476 					break;
  1477 				}
  1478 				localptr++;
  1479 				aliasptr++;
  1480 			} while (true);
  1481 		} while (!match);
  1482 	}
  1483 
  1484 	/* If we got here, it's time to compare filenames */
  1485 	const char *aliasptr = alias;
  1486 	const char *localptr = localpath;
  1487 
  1488 	if (alias_explicit_paths)
  1489 	{
  1490 		aliasptr = &alias[alias_path_end + 1];
  1491 	}
  1492 
  1493 	if (local_explicit_paths)
  1494 	{
  1495 		localptr = &localpath[local_path_end + 1];
  1496 	}
  1497 
  1498 	while (true)
  1499 	{
  1500 		if (*aliasptr == '*')
  1501 		{
  1502 			/* First, see if this is the last character */
  1503 			if ((unsigned)(aliasptr - alias) == alias_len - 1)
  1504 			{
  1505 				/* If so, there's no need to match anything else */
  1506 				return true;
  1507 			}
  1508 			/* Otherwise, we need to search for an appropriate matching sequence in local.
  1509 			 * Note that we only need to search up to the next asterisk.
  1510 			 */
  1511 			aliasptr++;
  1512 			bool match = true;
  1513 			const char *local_orig = localptr;
  1514 			do
  1515 			{
  1516 				match = true;
  1517 				while (*aliasptr != '\0' && *aliasptr != '*')
  1518 				{
  1519 					/* Since aliasptr is never '\0', localptr hitting the end will fail */
  1520 					if (*aliasptr != *localptr)
  1521 					{
  1522 						match = false;
  1523 						break;
  1524 					}
  1525 					aliasptr++;
  1526 					localptr++;
  1527 				}
  1528 				if (!match)
  1529 				{
  1530 					/* If we didn't get a match, we need to advance the search stream.
  1531 					 * This will let us skip tokens while still searching for another match.
  1532 					 */
  1533 					localptr = ++local_orig;
  1534 					/* Make sure we don't go out of bounds */
  1535 					if (*localptr == '\0')
  1536 					{
  1537 						break;
  1538 					}
  1539 				}
  1540 			} while (!match);
  1541 
  1542 			if (!match)
  1543 			{
  1544 				return false;
  1545 			} else {
  1546 				/* If we got a match, move on to the next token */
  1547 				continue;
  1548 			}
  1549 		} else if (*aliasptr == '\0') {
  1550 			if (*localptr == '\0'
  1551 				||
  1552 				strcmp(localptr, ".smx") == 0)
  1553 			{
  1554 				return true;
  1555 			} else {
  1556 				return false;
  1557 			}
  1558 		} else if (*aliasptr != *localptr) {
  1559 			return false;
  1560 		}
  1561 		aliasptr++;
  1562 		localptr++;
  1563 	}
  1564 
  1565 	return true;
  1566 }
  1567 
  1568 bool CPluginManager::IsLateLoadTime() const
  1569 {
  1570 	return (m_AllPluginsLoaded || !g_SourceMod.IsMapLoading());
  1571 }
  1572 
  1573 void CPluginManager::OnSourceModAllInitialized()
  1574 {
  1575 	m_MyIdent = g_ShareSys.CreateCoreIdentity();
  1576 
  1577 	HandleAccess sec;
  1578 	g_HandleSys.InitAccessDefaults(NULL, &sec);
  1579 
  1580 	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
  1581 	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
  1582 
  1583 	g_PluginType = g_HandleSys.CreateType("Plugin", this, 0, NULL, &sec, m_MyIdent, NULL);
  1584 	g_PluginIdent = g_ShareSys.CreateIdentType("PLUGIN");
  1585 
  1586 	g_RootMenu.AddRootConsoleCommand("plugins", "Manage Plugins", this);
  1587 
  1588 	g_ShareSys.AddInterface(NULL, this);
  1589 }
  1590 
  1591 void CPluginManager::OnSourceModShutdown()
  1592 {
  1593 	g_RootMenu.RemoveRootConsoleCommand("plugins", this);
  1594 
  1595 	List<CPlugin *>::iterator iter;
  1596 	while ( (iter = m_plugins.begin()) != m_plugins.end() )
  1597 	{
  1598 		UnloadPlugin((*iter));
  1599 	}
  1600 
  1601 	g_HandleSys.RemoveType(g_PluginType, m_MyIdent);
  1602 	g_ShareSys.DestroyIdentType(g_PluginIdent);
  1603 	g_ShareSys.DestroyIdentity(m_MyIdent);
  1604 }
  1605 
  1606 void CPluginManager::OnHandleDestroy(HandleType_t type, void *object)
  1607 {
  1608 	/* We don't care about the internal object, actually */
  1609 }
  1610 
  1611 void CPluginManager::RegisterNativesFromCore(sp_nativeinfo_t *natives)
  1612 {
  1613 	m_natives.push_back(natives);
  1614 }
  1615 
  1616 IPlugin *CPluginManager::PluginFromHandle(Handle_t handle, HandleError *err)
  1617 {
  1618 	IPlugin *pPlugin;
  1619 	HandleError _err;
  1620 
  1621 	HandleSecurity sec;
  1622 
  1623 	sec.pOwner = NULL;
  1624 	sec.pIdentity = m_MyIdent;
  1625 
  1626 	if ((_err=g_HandleSys.ReadHandle(handle, g_PluginType, &sec, (void **)&pPlugin)) != HandleError_None)
  1627 	{
  1628 		pPlugin = NULL;
  1629 	}
  1630 
  1631 	if (err)
  1632 	{
  1633 		*err = _err;
  1634 	}
  1635 
  1636 	return pPlugin;
  1637 }
  1638 
  1639 CPlugin *CPluginManager::GetPluginByOrder(int num)
  1640 {
  1641 	if (num < 1 || num > (int)GetPluginCount())
  1642 	{
  1643 		return NULL;
  1644 	}
  1645 
  1646 	CPlugin *pl;
  1647 	int id = 1;
  1648 
  1649 	IPluginIterator *iter = GetPluginIterator();
  1650 	for (; iter->MorePlugins() && id<num; iter->NextPlugin(), id++) {}
  1651 
  1652 	pl = (CPlugin *)(iter->GetPlugin());
  1653 	iter->Release();
  1654 
  1655 	return pl;
  1656 }
  1657 
  1658 const char *CPluginManager::GetStatusText(PluginStatus st)
  1659 {
  1660 	switch (st)
  1661 	{
  1662 	case Plugin_Running:
  1663 		return "Running";
  1664 	case Plugin_Paused:
  1665 		return "Paused";
  1666 	case Plugin_Error:
  1667 		return "Error";
  1668 	case Plugin_Uncompiled:
  1669 		return "Uncompiled";
  1670 	case Plugin_BadLoad:
  1671 		return "Bad Load";
  1672 	case Plugin_Failed:
  1673 		return "Failed";
  1674 	default:
  1675 		assert(false);
  1676 		return "-";
  1677 	}
  1678 }
  1679 
  1680 void CPluginManager::OnRootConsoleCommand(const char *command, unsigned int argcount)
  1681 {
  1682 	if (argcount >= 3)
  1683 	{
  1684 		const char *cmd = g_RootMenu.GetArgument(2);
  1685 		if (strcmp(cmd, "list") == 0)
  1686 		{
  1687 			char buffer[256];
  1688 			unsigned int id = 1;
  1689 			int plnum = GetPluginCount();
  1690 
  1691 			if (!plnum)
  1692 			{
  1693 				g_RootMenu.ConsolePrint("[SM] No plugins loaded");
  1694 				return;
  1695 			} else {
  1696 				g_RootMenu.ConsolePrint("[SM] Listing %d plugin%s:", GetPluginCount(), (plnum > 1) ? "s" : "");
  1697 			}
  1698 
  1699 			IPluginIterator *iter = GetPluginIterator();
  1700 			for (; iter->MorePlugins(); iter->NextPlugin(), id++)
  1701 			{
  1702 				IPlugin *pl = iter->GetPlugin();
  1703 				assert(pl->GetStatus() != Plugin_Created);
  1704 				int len = 0;
  1705 				const sm_plugininfo_t *info = pl->GetPublicInfo();
  1706 				if (pl->GetStatus() != Plugin_Running)
  1707 				{
  1708 					len += UTIL_Format(buffer, sizeof(buffer), "  %02d <%s>", id, GetStatusText(pl->GetStatus()));
  1709 				} else {
  1710 					len += UTIL_Format(buffer, sizeof(buffer), "  %02d", id);
  1711 				}
  1712 				len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " \"%s\"", (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
  1713 				if (IS_STR_FILLED(info->version))
  1714 				{
  1715 					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", info->version);
  1716 				}
  1717 				if (IS_STR_FILLED(info->author))
  1718 				{
  1719 					UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", info->author);
  1720 				}
  1721 				g_RootMenu.ConsolePrint("%s", buffer);
  1722 			}
  1723 
  1724 			iter->Release();
  1725 			return;
  1726 		} else if (strcmp(cmd, "load") == 0) {
  1727 			if (argcount < 4)
  1728 			{
  1729 				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins load <file>");
  1730 				return;
  1731 			}
  1732 
  1733 			char error[128];
  1734 			bool wasloaded;
  1735 			const char *filename = g_RootMenu.GetArgument(3);
  1736 
  1737 			char pluginfile[256];
  1738 			const char *ext = g_LibSys.GetFileExtension(filename) ? "" : ".smx";
  1739 			UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", filename, ext);
  1740 
  1741 			IPlugin *pl = LoadPlugin(pluginfile, false, PluginType_MapUpdated, error, sizeof(error), &wasloaded);
  1742 
  1743 			if (wasloaded)
  1744 			{
  1745 				g_RootMenu.ConsolePrint("[SM] Plugin %s is already loaded.", pluginfile);
  1746 				return;
  1747 			}
  1748 
  1749 			if (pl)
  1750 			{
  1751 				g_RootMenu.ConsolePrint("[SM] Loaded plugin %s successfully.", pluginfile);
  1752 			} else {
  1753 				g_RootMenu.ConsolePrint("[SM] Plugin %s failed to load: %s.", pluginfile, error);
  1754 			}
  1755 
  1756 			return;
  1757 		} else if (strcmp(cmd, "unload") == 0) {
  1758 			if (argcount < 4)
  1759 			{
  1760 				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins unload <#>");
  1761 				return;
  1762 			}
  1763 
  1764 			int id = 1;
  1765 			int num = atoi(g_RootMenu.GetArgument(3));
  1766 			if (num < 1 || num > (int)GetPluginCount())
  1767 			{
  1768 				g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", num);
  1769 				return;
  1770 			}
  1771 
  1772 			IPluginIterator *iter = GetPluginIterator();
  1773 			for (; iter->MorePlugins() && id<num; iter->NextPlugin(), id++)
  1774 				;
  1775 			IPlugin *pl = iter->GetPlugin();
  1776 
  1777 			char name[PLATFORM_MAX_PATH];
  1778 			const sm_plugininfo_t *info = pl->GetPublicInfo();
  1779 			strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
  1780 
  1781 			if (UnloadPlugin(pl))
  1782 			{
  1783 				g_RootMenu.ConsolePrint("[SM] Plugin %s unloaded successfully.", name);
  1784 			} else {
  1785 				g_RootMenu.ConsolePrint("[SM] Failed to unload plugin %s.", name);
  1786 			}
  1787 
  1788 			iter->Release();
  1789 			return;
  1790 		} else if (strcmp(cmd, "info") == 0) {
  1791 			if (argcount < 4)
  1792 			{
  1793 				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins info <#>");
  1794 				return;
  1795 			}
  1796 
  1797 			int num = atoi(g_RootMenu.GetArgument(3));
  1798 			if (num < 1 || num > (int)GetPluginCount())
  1799 			{
  1800 				g_RootMenu.ConsolePrint("[SM] Plugin index not found.");
  1801 				return;
  1802 			}
  1803 
  1804 			CPlugin *pl = GetPluginByOrder(num);
  1805 			const sm_plugininfo_t *info = pl->GetPublicInfo();
  1806 
  1807 			g_RootMenu.ConsolePrint("  Filename: %s", pl->GetFilename());
  1808 			if (pl->GetStatus() <= Plugin_Error)
  1809 			{
  1810 				if (IS_STR_FILLED(info->name))
  1811 				{
  1812 					if (IS_STR_FILLED(info->description))
  1813 					{
  1814 						g_RootMenu.ConsolePrint("  Title: %s (%s)", info->name, info->description);
  1815 					} else {
  1816 						g_RootMenu.ConsolePrint("  Title: %s", info->name);
  1817 					}
  1818 				}
  1819 				if (IS_STR_FILLED(info->author))
  1820 				{
  1821 					g_RootMenu.ConsolePrint("  Author: %s", info->author);
  1822 				}
  1823 				if (IS_STR_FILLED(info->version))
  1824 				{
  1825 					g_RootMenu.ConsolePrint("  Version: %s", info->version);
  1826 				}
  1827 				if (IS_STR_FILLED(info->url))
  1828 				{
  1829 					g_RootMenu.ConsolePrint("  URL: %s", info->url);
  1830 				}
  1831 				if (pl->GetStatus() == Plugin_Error)
  1832 				{
  1833 					g_RootMenu.ConsolePrint("  Error: %s", pl->m_errormsg);
  1834 				} else {
  1835 					g_RootMenu.ConsolePrint("  Debugging: %s", pl->IsDebugging() ? "Yes" : "No");
  1836 					g_RootMenu.ConsolePrint("  Running: %s", pl->GetStatus() == Plugin_Running ? "Yes" : "No");
  1837 
  1838 					const char *typestr = "";
  1839 					switch (pl->GetType())
  1840 					{
  1841 					case PluginType_MapUpdated:
  1842 						typestr = "Map Change if Updated";
  1843 						break;
  1844 					case PluginType_MapOnly:
  1845 						typestr = "Map Change";
  1846 						break;
  1847 					case PluginType_Private:
  1848 					case PluginType_Global:
  1849 						typestr = "Never";
  1850 						break;
  1851 					}
  1852 
  1853 					g_RootMenu.ConsolePrint("  Reloads: %s", typestr);
  1854 				}
  1855 			} else {
  1856 				g_RootMenu.ConsolePrint("  Load error: %s", pl->m_errormsg);
  1857 				g_RootMenu.ConsolePrint("  File info: (title \"%s\") (version \"%s\")", 
  1858 										info->name ? info->name : "<none>",
  1859 										info->version ? info->version : "<none>");
  1860 				if (IS_STR_FILLED(info->url))
  1861 				{
  1862 					g_RootMenu.ConsolePrint("  File URL: %s", info->url);
  1863 				}
  1864 			}
  1865 
  1866 			return;
  1867 		} else if (strcmp(cmd, "debug") == 0) {
  1868 			if (argcount < 5)
  1869 			{
  1870 				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins debug <#> [on|off]");
  1871 				return;
  1872 			}
  1873 
  1874 			int num = atoi(g_RootMenu.GetArgument(3));
  1875 			if (num < 1 || num > (int)GetPluginCount())
  1876 			{
  1877 				g_RootMenu.ConsolePrint("[SM] Plugin index not found.");
  1878 				return;
  1879 			}
  1880 
  1881 			int res;
  1882 			const char *mode = g_RootMenu.GetArgument(4);
  1883 			if ((res=strcmp("on", mode)) && strcmp("off", mode))
  1884 			{
  1885 				g_RootMenu.ConsolePrint("[SM] The only possible options are \"on\" and \"off.\"");
  1886 				return;
  1887 			}
  1888 
  1889 			bool debug;
  1890 			if (!res)
  1891 			{
  1892 				debug = true;
  1893 			} else {
  1894 				debug = false;
  1895 			}
  1896 
  1897 			CPlugin *pl = GetPluginByOrder(num);
  1898 			if (debug && pl->IsDebugging())
  1899 			{
  1900 				g_RootMenu.ConsolePrint("[SM] This plugin is already in debug mode.");
  1901 				return;
  1902 			} else if (!debug && !pl->IsDebugging()) {
  1903 				g_RootMenu.ConsolePrint("[SM] Debug mode is already disabled in this plugin.");
  1904 				return;
  1905 			}
  1906 
  1907 			char error[256];
  1908 			if (pl->ToggleDebugMode(debug, error, sizeof(error)))
  1909 			{
  1910 				g_RootMenu.ConsolePrint("[SM] Successfully toggled debug mode on plugin %s.", pl->GetFilename());
  1911 				return;
  1912 			} else {
  1913 				g_RootMenu.ConsolePrint("[SM] Could not toggle debug mode on plugin %s.", pl->GetFilename());
  1914 				g_RootMenu.ConsolePrint("[SM] Plugin returned error: %s", error);
  1915 				return;
  1916 			}
  1917 		}
  1918 	}
  1919 
  1920 	/* Draw the main menu */
  1921 	g_RootMenu.ConsolePrint("SourceMod Plugins Menu:");
  1922 	g_RootMenu.DrawGenericOption("list", "Show loaded plugins");
  1923 	g_RootMenu.DrawGenericOption("load", "Load a plugin");
  1924 	g_RootMenu.DrawGenericOption("unload", "Unload a plugin");
  1925 	g_RootMenu.DrawGenericOption("info", "Information about a plugin");
  1926 	g_RootMenu.DrawGenericOption("debug", "Toggle debug mode on a plugin");
  1927 }
  1928 
  1929 void CPluginManager::ReloadOrUnloadPlugins()
  1930 {
  1931 	List<CPlugin *>::iterator iter;
  1932 	List<CPlugin *> tmp_list = m_plugins;
  1933 	CPlugin *pl;
  1934 	time_t t;
  1935 
  1936 	for (iter=tmp_list.begin(); iter!=tmp_list.end(); iter++)
  1937 	{
  1938 		pl = (*iter);
  1939 		if (pl->GetType() ==  PluginType_MapOnly)
  1940 		{
  1941 			UnloadPlugin((IPlugin *)pl);
  1942 		} else if (pl->GetType() == PluginType_MapUpdated) {
  1943 			t = pl->GetFileTimeStamp();
  1944 			if (!t || t > pl->GetTimeStamp())
  1945 			{
  1946 				pl->SetTimeStamp(t);
  1947 				UnloadPlugin((IPlugin *)pl);
  1948 			}
  1949 		}
  1950 	}
  1951 }
  1952 
  1953 void CPluginManager::_SetPauseState(CPlugin *pl, bool paused)
  1954 {
  1955 	List<IPluginsListener *>::iterator iter;
  1956 	IPluginsListener *pListener;
  1957 	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
  1958 	{
  1959 		pListener = (*iter);
  1960 		pListener->OnPluginPauseChange(pl, paused);
  1961 	}	
  1962 }
  1963 
  1964 bool CPluginManager::AddFakeNative(IPluginFunction *pFunction, const char *name, SPVM_FAKENATIVE_FUNC func)
  1965 {
  1966 	if (sm_trie_retrieve(m_pNativeLookup, name, NULL))
  1967 	{
  1968 		return false;
  1969 	}
  1970 
  1971 	FakeNative *pNative = new FakeNative;
  1972 
  1973 	pNative->func = g_pVM->CreateFakeNative(func, pNative);
  1974 	if (!pNative->func)
  1975 	{
  1976 		delete pNative;
  1977 		return false;
  1978 	}
  1979 
  1980 	pNative->call = pFunction;
  1981 	pNative->name.assign(name);
  1982 	pNative->ctx = pFunction->GetParentContext();
  1983 
  1984 	m_Natives.push_back(pNative);
  1985 	sm_trie_insert(m_pNativeLookup, name,pNative);
  1986 
  1987 	CPlugin *pPlugin = GetPluginByCtx(pNative->ctx->GetContext());
  1988 	pPlugin->m_fakeNatives.push_back(pNative);
  1989 
  1990 	return true;
  1991 }
  1992 
  1993 void CPluginManager::AddFunctionsToForward(const char *name, IChangeableForward *pForward)
  1994 {
  1995 	List<CPlugin *>::iterator iter;
  1996 	CPlugin *pPlugin;
  1997 	IPluginFunction *pFunction;
  1998 
  1999 	for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++)
  2000 	{
  2001 		pPlugin = (*iter);
  2002 
  2003 		if (pPlugin->GetStatus() <= Plugin_Paused)
  2004 		{
  2005 			pFunction = pPlugin->GetBaseContext()->GetFunctionByName(name);
  2006 
  2007 			if (pFunction)
  2008 			{
  2009 				pForward->AddFunction(pFunction);
  2010 			}
  2011 		}
  2012 	}
  2013 }
  2014 
  2015 CPlugin *CPluginManager::GetPluginFromIdentity(IdentityToken_t *pToken)
  2016 {
  2017 	if (pToken->type != g_PluginIdent)
  2018 	{
  2019 		return NULL;
  2020 	}
  2021 
  2022 	return (CPlugin *)(pToken->ptr);
  2023 }
  2024 
  2025 void CPluginManager::ExecAndGenPluginConfs()
  2026 {
  2027 	List<CPlugin *>::iterator iter;
  2028 
  2029 	//for (iter = 
  2030 }