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