core/systems/PluginSys.cpp
author David Anderson <dvander@alliedmods.net>
Sat May 10 09:23:55 2008 +0000 (2008-05-10)
changeset 2042 4a2faafca477
parent 1952 b7aa2b626807
child 2086 debb2ab2c782
permissions -rw-r--r--
added amb1666 - rehaul over the ml api so extensions can easily use translations
ds@1952
     1
/**
ds@1952
     2
 * vim: set ts=4 :
ds@1952
     3
 * =============================================================================
ds@1952
     4
 * SourceMod
ds@1952
     5
 * Copyright (C) 2004-2008 AlliedModders LLC.  All rights reserved.
ds@1952
     6
 * =============================================================================
ds@1952
     7
 *
ds@1952
     8
 * This program is free software; you can redistribute it and/or modify it under
ds@1952
     9
 * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952
    10
 * Free Software Foundation.
ds@1952
    11
 * 
ds@1952
    12
 * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952
    13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952
    14
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
ds@1952
    15
 * details.
ds@1952
    16
 *
ds@1952
    17
 * You should have received a copy of the GNU General Public License along with
ds@1952
    18
 * this program.  If not, see <http://www.gnu.org/licenses/>.
ds@1952
    19
 *
ds@1952
    20
 * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952
    21
 * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952
    22
 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952
    23
 * by the Valve Corporation.  You must obey the GNU General Public License in
ds@1952
    24
 * all respects for all other code used.  Additionally, AlliedModders LLC grants
ds@1952
    25
 * this exception to all derivative works.  AlliedModders LLC defines further
ds@1952
    26
 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952
    27
 * or <http://www.sourcemod.net/license.php>.
ds@1952
    28
 *
ds@1952
    29
 * Version: $Id$
ds@1952
    30
 */
ds@1952
    31
ds@1952
    32
#include <stdio.h>
ds@1952
    33
#include "PluginSys.h"
ds@1952
    34
#include "ShareSys.h"
ds@1952
    35
#include "LibrarySys.h"
ds@1952
    36
#include "HandleSys.h"
ds@1952
    37
#include "ForwardSys.h"
ds@1952
    38
#include "sourcemm_api.h"
ds@1952
    39
#include "sourcemod.h"
ds@1952
    40
#include "TextParsers.h"
ds@1952
    41
#include "Logger.h"
ds@1952
    42
#include "ExtensionSys.h"
ds@1952
    43
#include "sm_srvcmds.h"
ds@1952
    44
#include "sm_stringutil.h"
ds@1952
    45
#include "ConCmdManager.h"
ds@1952
    46
#include "PlayerManager.h"
ds@1952
    47
#include "CoreConfig.h"
ds@1952
    48
#include "Translator.h"
ds@1952
    49
ds@1952
    50
CPluginManager g_PluginSys;
ds@1952
    51
HandleType_t g_PluginType = 0;
ds@1952
    52
IdentityType_t g_PluginIdent = 0;
ds@1952
    53
ds@1952
    54
CPlugin::CPlugin(const char *file)
ds@1952
    55
{
ds@1952
    56
	static int MySerial = 0;
ds@1952
    57
ds@1952
    58
	m_type = PluginType_Private;
ds@1952
    59
	m_status = Plugin_Uncompiled;
ds@1952
    60
	m_serial = ++MySerial;
ds@1952
    61
	m_plugin = NULL;
ds@1952
    62
	m_errormsg[256] = '\0';
ds@1952
    63
	snprintf(m_filename, sizeof(m_filename), "%s", file);
ds@1952
    64
	m_handle = 0;
ds@1952
    65
	m_ident = NULL;
ds@1952
    66
	m_pProps = sm_trie_create();
ds@1952
    67
	m_FakeNativesMissing = false;
ds@1952
    68
	m_LibraryMissing = false;
ds@1952
    69
	m_bGotAllLoaded = false;
dvander@2042
    70
	m_pPhrases = g_Translator.CreatePhraseCollection();
ds@1952
    71
}
ds@1952
    72
ds@1952
    73
CPlugin::~CPlugin()
ds@1952
    74
{
ds@1952
    75
	if (m_handle)
ds@1952
    76
	{
ds@1952
    77
		HandleSecurity sec;
ds@1952
    78
		sec.pOwner = g_PluginSys.GetIdentity();
ds@1952
    79
		sec.pIdentity = sec.pOwner;
ds@1952
    80
ds@1952
    81
		g_HandleSys.FreeHandle(m_handle, &sec);
ds@1952
    82
		g_ShareSys.DestroyIdentity(m_ident);
ds@1952
    83
	}
ds@1952
    84
ds@1952
    85
	if (m_ctx.base)
ds@1952
    86
	{
ds@1952
    87
		delete m_ctx.base;
ds@1952
    88
		m_ctx.base = NULL;
ds@1952
    89
	}
ds@1952
    90
	if (m_ctx.ctx)
ds@1952
    91
	{
ds@1952
    92
		m_ctx.vm->FreeContext(m_ctx.ctx);
ds@1952
    93
		m_ctx.ctx = NULL;
ds@1952
    94
	}
ds@1952
    95
	if (m_ctx.co)
ds@1952
    96
	{
ds@1952
    97
		m_ctx.vm->AbortCompilation(m_ctx.co);
ds@1952
    98
		m_ctx.co = NULL;
ds@1952
    99
	}
ds@1952
   100
ds@1952
   101
	if (m_plugin)
ds@1952
   102
	{
ds@1952
   103
		g_pSourcePawn->FreeFromMemory(m_plugin);
ds@1952
   104
		m_plugin = NULL;
ds@1952
   105
	}
ds@1952
   106
	if (m_pProps)
ds@1952
   107
	{
ds@1952
   108
		sm_trie_destroy(m_pProps);
ds@1952
   109
	}
ds@1952
   110
	for (size_t i=0; i<m_configs.size(); i++)
ds@1952
   111
	{
ds@1952
   112
		delete m_configs[i];
ds@1952
   113
	}
ds@1952
   114
	m_configs.clear();
dvander@2042
   115
	if (m_pPhrases != NULL)
dvander@2042
   116
	{
dvander@2042
   117
		m_pPhrases->Destroy();
dvander@2042
   118
		m_pPhrases = NULL;
dvander@2042
   119
	}
ds@1952
   120
}
ds@1952
   121
ds@1952
   122
void CPlugin::InitIdentity()
ds@1952
   123
{
ds@1952
   124
	if (!m_handle)
ds@1952
   125
	{
ds@1952
   126
		m_ident = g_ShareSys.CreateIdentity(g_PluginIdent, this);
ds@1952
   127
		m_handle = g_HandleSys.CreateHandle(g_PluginType, this, g_PluginSys.GetIdentity(), g_PluginSys.GetIdentity(), NULL);
ds@1952
   128
		m_ctx.base->SetIdentity(m_ident);
ds@1952
   129
	}
ds@1952
   130
}
ds@1952
   131
ds@1952
   132
unsigned int CPlugin::CalcMemUsage()
ds@1952
   133
{
ds@1952
   134
	unsigned int base_size = 
ds@1952
   135
		sizeof(CPlugin) 
ds@1952
   136
		+ sizeof(IdentityToken_t)
ds@1952
   137
		+ (m_dependents.size() * sizeof(CPlugin *))
ds@1952
   138
		+ (m_dependsOn.size() * sizeof(CPlugin *))
ds@1952
   139
		+ (m_fakeNatives.size() * (sizeof(FakeNative *) + sizeof(FakeNative)))
ds@1952
   140
		+ (m_WeakNatives.size() * sizeof(WeakNative))
ds@1952
   141
		+ (m_configs.size() * (sizeof(AutoConfig *) + sizeof(AutoConfig)))
ds@1952
   142
		+ sm_trie_mem_usage(m_pProps);
ds@1952
   143
ds@1952
   144
	for (unsigned int i = 0; i < m_configs.size(); i++)
ds@1952
   145
	{
ds@1952
   146
		base_size += m_configs[i]->autocfg.size();
ds@1952
   147
		base_size += m_configs[i]->folder.size();
ds@1952
   148
	}
ds@1952
   149
ds@1952
   150
	for (List<String>::iterator i = m_Libraries.begin();
ds@1952
   151
		 i != m_Libraries.end();
ds@1952
   152
		 i++)
ds@1952
   153
	{
ds@1952
   154
		base_size += (*i).size();
ds@1952
   155
	}
ds@1952
   156
ds@1952
   157
	for (List<String>::iterator i = m_RequiredLibs.begin();
ds@1952
   158
		 i != m_RequiredLibs.end();
ds@1952
   159
		 i++)
ds@1952
   160
	{
ds@1952
   161
		base_size += (*i).size();
ds@1952
   162
	}
ds@1952
   163
ds@1952
   164
	for (List<FakeNative *>::iterator i = m_fakeNatives.begin();
ds@1952
   165
		 i != m_fakeNatives.end();
ds@1952
   166
		 i++)
ds@1952
   167
	{
ds@1952
   168
		base_size += (*i)->name.size();
ds@1952
   169
	}
ds@1952
   170
ds@1952
   171
	if (m_plugin != NULL)
ds@1952
   172
	{
ds@1952
   173
		base_size += sizeof(sp_plugin_t);
ds@1952
   174
		base_size += m_plugin->data_size;
ds@1952
   175
		base_size += m_plugin->pcode_size;
ds@1952
   176
		base_size += (m_plugin->info.natives_num * sizeof(sp_file_natives_t));
ds@1952
   177
		base_size += (m_plugin->info.publics_num * sizeof(sp_file_publics_t));
ds@1952
   178
		base_size += (m_plugin->info.pubvars_num * sizeof(sp_file_pubvars_t));
ds@1952
   179
		base_size += (m_plugin->debug.files_num * sizeof(sp_fdbg_file_t));
ds@1952
   180
		base_size += (m_plugin->debug.lines_num * sizeof(sp_fdbg_line_t));
ds@1952
   181
		base_size += (m_plugin->debug.syms_num * sizeof(sp_fdbg_symbol_t));
ds@1952
   182
		/* We can't get strtab size, oh well. */
ds@1952
   183
	}
ds@1952
   184
ds@1952
   185
	if (m_ctx.base != NULL)
ds@1952
   186
	{
ds@1952
   187
		base_size += sizeof(BaseContext);
ds@1952
   188
		base_size += m_ctx.base->GetPublicsNum() * sizeof(CFunction);
ds@1952
   189
	}
ds@1952
   190
	if (m_ctx.ctx != NULL)
ds@1952
   191
	{
ds@1952
   192
		base_size += m_ctx.ctx->mem_size;
ds@1952
   193
		base_size += (m_plugin->debug.files_num * sizeof(sp_debug_file_t));
ds@1952
   194
		base_size += (m_plugin->debug.lines_num * sizeof(sp_debug_line_t));
ds@1952
   195
		base_size += (m_plugin->debug.syms_num * sizeof(sp_debug_symbol_t));
ds@1952
   196
		base_size += (m_plugin->info.pubvars_num * sizeof(sp_pubvar_t));
ds@1952
   197
		base_size += (m_plugin->info.publics_num * sizeof(sp_public_t));
ds@1952
   198
		base_size += (m_plugin->info.natives_num * sizeof(sp_native_t));
ds@1952
   199
		/* We also don't know the JIT code size, oh well. */
ds@1952
   200
	}
ds@1952
   201
ds@1952
   202
	return base_size;
ds@1952
   203
}
ds@1952
   204
ds@1952
   205
Handle_t CPlugin::GetMyHandle()
ds@1952
   206
{
ds@1952
   207
	return m_handle;
ds@1952
   208
}
ds@1952
   209
ds@1952
   210
CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength)
ds@1952
   211
{
ds@1952
   212
	char fullpath[PLATFORM_MAX_PATH];
ds@1952
   213
	g_SourceMod.BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file);
ds@1952
   214
	FILE *fp = fopen(fullpath, "rb");
ds@1952
   215
ds@1952
   216
	CPlugin *pPlugin = new CPlugin(file);
ds@1952
   217
ds@1952
   218
	if (!fp)
ds@1952
   219
	{
ds@1952
   220
		if (error)
ds@1952
   221
		{
ds@1952
   222
			snprintf(error, maxlength, "Unable to open file");
ds@1952
   223
		}
ds@1952
   224
		pPlugin->m_status = Plugin_BadLoad;
ds@1952
   225
		return pPlugin;
ds@1952
   226
	}
ds@1952
   227
ds@1952
   228
	int err;
ds@1952
   229
	sp_plugin_t *pl = g_pSourcePawn->LoadFromFilePointer(fp, &err);
ds@1952
   230
	if (pl == NULL)
ds@1952
   231
	{
ds@1952
   232
		fclose(fp);
ds@1952
   233
		if (error)
ds@1952
   234
		{
ds@1952
   235
			snprintf(error, maxlength, "Error %d while parsing plugin", err);
ds@1952
   236
		}
ds@1952
   237
		pPlugin->m_status = Plugin_BadLoad;
ds@1952
   238
		return pPlugin;
ds@1952
   239
	}
ds@1952
   240
ds@1952
   241
	fclose(fp);
ds@1952
   242
ds@1952
   243
	pPlugin->m_plugin = pl;
ds@1952
   244
ds@1952
   245
	return pPlugin;
ds@1952
   246
}
ds@1952
   247
ds@1952
   248
bool CPlugin::GetProperty(const char *prop, void **ptr, bool remove/* =false */)
ds@1952
   249
{
ds@1952
   250
	bool exists = sm_trie_retrieve(m_pProps, prop, ptr);
ds@1952
   251
ds@1952
   252
	if (exists && remove)
ds@1952
   253
	{
ds@1952
   254
		sm_trie_delete(m_pProps, prop);
ds@1952
   255
	}
ds@1952
   256
ds@1952
   257
	return exists;
ds@1952
   258
}
ds@1952
   259
ds@1952
   260
bool CPlugin::SetProperty(const char *prop, void *ptr)
ds@1952
   261
{
ds@1952
   262
	return sm_trie_insert(m_pProps, prop, ptr);
ds@1952
   263
}
ds@1952
   264
ds@1952
   265
ICompilation *CPlugin::StartMyCompile(IVirtualMachine *vm)
ds@1952
   266
{
ds@1952
   267
	if (!m_plugin)
ds@1952
   268
	{
ds@1952
   269
		return NULL;
ds@1952
   270
	}
ds@1952
   271
ds@1952
   272
	/* :NOTICE: We will eventually need to change these natives
ds@1952
   273
	 * for swapping in new contexts
ds@1952
   274
	 */
ds@1952
   275
	if (m_ctx.co || m_ctx.ctx)
ds@1952
   276
	{
ds@1952
   277
		return NULL;
ds@1952
   278
	}
ds@1952
   279
ds@1952
   280
	m_status = Plugin_Uncompiled;
ds@1952
   281
ds@1952
   282
	m_ctx.vm = vm;
ds@1952
   283
	m_ctx.co = vm->StartCompilation(m_plugin);
ds@1952
   284
ds@1952
   285
	return m_ctx.co;
ds@1952
   286
}
ds@1952
   287
ds@1952
   288
void CPlugin::CancelMyCompile()
ds@1952
   289
{
ds@1952
   290
	if (!m_ctx.co)
ds@1952
   291
	{
ds@1952
   292
		return;
ds@1952
   293
	}
ds@1952
   294
ds@1952
   295
	m_ctx.vm->AbortCompilation(m_ctx.co);
ds@1952
   296
	m_ctx.co = NULL;
ds@1952
   297
	m_ctx.vm = NULL;
ds@1952
   298
}
ds@1952
   299
ds@1952
   300
bool CPlugin::FinishMyCompile(char *error, size_t maxlength)
ds@1952
   301
{
ds@1952
   302
	if (!m_ctx.co)
ds@1952
   303
	{
ds@1952
   304
		return false;
ds@1952
   305
	}
ds@1952
   306
ds@1952
   307
	int err;
ds@1952
   308
	m_ctx.ctx = m_ctx.vm->CompileToContext(m_ctx.co, &err);
ds@1952
   309
	if (!m_ctx.ctx)
ds@1952
   310
	{
ds@1952
   311
		memset(&m_ctx, 0, sizeof(m_ctx));
ds@1952
   312
		if (error)
ds@1952
   313
		{
ds@1952
   314
			snprintf(error, maxlength, "JIT failed to compile (error %d)", err);
ds@1952
   315
		}
ds@1952
   316
		return false;
ds@1952
   317
	}
ds@1952
   318
ds@1952
   319
	m_ctx.base = new BaseContext(m_ctx.ctx);
ds@1952
   320
	m_ctx.ctx->user[SM_CONTEXTVAR_MYSELF] = (void *)this;
ds@1952
   321
ds@1952
   322
	m_status = Plugin_Created;
ds@1952
   323
	m_ctx.co = NULL;
ds@1952
   324
ds@1952
   325
	UpdateInfo();
ds@1952
   326
ds@1952
   327
	return true;
ds@1952
   328
}
ds@1952
   329
ds@1952
   330
void CPlugin::SetErrorState(PluginStatus status, const char *error_fmt, ...)
ds@1952
   331
{
ds@1952
   332
	PluginStatus old_status = m_status;
ds@1952
   333
	m_status = status;
ds@1952
   334
ds@1952
   335
	if (old_status == Plugin_Running)
ds@1952
   336
	{
ds@1952
   337
		/* Tell everyone we're now paused */
ds@1952
   338
		g_PluginSys._SetPauseState(this, true);
ds@1952
   339
	}
ds@1952
   340
ds@1952
   341
	va_list ap;
ds@1952
   342
	va_start(ap, error_fmt);
ds@1952
   343
	vsnprintf(m_errormsg, sizeof(m_errormsg), error_fmt, ap);
ds@1952
   344
	va_end(ap);
ds@1952
   345
ds@1952
   346
	if (m_ctx.ctx)
ds@1952
   347
	{
ds@1952
   348
		m_ctx.ctx->flags |= SPFLAG_PLUGIN_PAUSED;
ds@1952
   349
	}
ds@1952
   350
}
ds@1952
   351
ds@1952
   352
void CPlugin::UpdateInfo()
ds@1952
   353
{
ds@1952
   354
	/* Now grab the info */
ds@1952
   355
	uint32_t idx;
ds@1952
   356
	IPluginContext *base = GetBaseContext();
ds@1952
   357
	int err = base->FindPubvarByName("myinfo", &idx);
ds@1952
   358
ds@1952
   359
	memset(&m_info, 0, sizeof(m_info));
ds@1952
   360
ds@1952
   361
	if (err == SP_ERROR_NONE)
ds@1952
   362
	{
ds@1952
   363
		struct sm_plugininfo_c_t
ds@1952
   364
		{
ds@1952
   365
			cell_t name;
ds@1952
   366
			cell_t description;
ds@1952
   367
			cell_t author;
ds@1952
   368
			cell_t version;
ds@1952
   369
			cell_t url;
ds@1952
   370
		};
ds@1952
   371
		sm_plugininfo_c_t *cinfo;
ds@1952
   372
		cell_t local_addr;
ds@1952
   373
ds@1952
   374
		base->GetPubvarAddrs(idx, &local_addr, (cell_t **)&cinfo);
ds@1952
   375
		base->LocalToString(cinfo->name, (char **)&m_info.name);
ds@1952
   376
		base->LocalToString(cinfo->description, (char **)&m_info.description);
ds@1952
   377
		base->LocalToString(cinfo->author, (char **)&m_info.author);
ds@1952
   378
		base->LocalToString(cinfo->url, (char **)&m_info.url);
ds@1952
   379
		base->LocalToString(cinfo->version, (char **)&m_info.version);
ds@1952
   380
	}
ds@1952
   381
ds@1952
   382
	m_info.author = m_info.author ? m_info.author : "";
ds@1952
   383
	m_info.description = m_info.description ? m_info.description : "";
ds@1952
   384
	m_info.name = m_info.name ? m_info.name : "";
ds@1952
   385
	m_info.url = m_info.url ? m_info.url : "";
ds@1952
   386
	m_info.version = m_info.version ? m_info.version : "";
ds@1952
   387
}
ds@1952
   388
ds@1952
   389
void CPlugin::Call_OnPluginStart()
ds@1952
   390
{
ds@1952
   391
	if (m_status != Plugin_Loaded)
ds@1952
   392
	{
ds@1952
   393
		return;
ds@1952
   394
	}
ds@1952
   395
ds@1952
   396
	m_status = Plugin_Running;
ds@1952
   397
ds@1952
   398
	cell_t result;
ds@1952
   399
	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginStart");
ds@1952
   400
	if (!pFunction)
ds@1952
   401
	{
ds@1952
   402
		return;
ds@1952
   403
	}
ds@1952
   404
ds@1952
   405
	int err;
ds@1952
   406
	if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE)
ds@1952
   407
	{
ds@1952
   408
		SetErrorState(Plugin_Error, "Error detected in plugin startup (see error logs)");
ds@1952
   409
	}
ds@1952
   410
}
ds@1952
   411
ds@1952
   412
void CPlugin::Call_OnPluginEnd()
ds@1952
   413
{
ds@1952
   414
	if (m_status > Plugin_Paused)
ds@1952
   415
	{
ds@1952
   416
		return;
ds@1952
   417
	}
ds@1952
   418
ds@1952
   419
	cell_t result;
ds@1952
   420
	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginEnd");
ds@1952
   421
	if (!pFunction)
ds@1952
   422
	{
ds@1952
   423
		return;
ds@1952
   424
	}
ds@1952
   425
ds@1952
   426
	pFunction->Execute(&result);
ds@1952
   427
}
ds@1952
   428
ds@1952
   429
void CPlugin::Call_OnAllPluginsLoaded()
ds@1952
   430
{
ds@1952
   431
	if (m_status > Plugin_Paused)
ds@1952
   432
	{
ds@1952
   433
		return;
ds@1952
   434
	}
ds@1952
   435
ds@1952
   436
	if (m_bGotAllLoaded)
ds@1952
   437
	{
ds@1952
   438
		return;
ds@1952
   439
	}
ds@1952
   440
ds@1952
   441
	m_bGotAllLoaded = true;
ds@1952
   442
ds@1952
   443
	cell_t result;
ds@1952
   444
	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnAllPluginsLoaded");
ds@1952
   445
	if (pFunction != NULL)
ds@1952
   446
	{
ds@1952
   447
		pFunction->Execute(&result);
ds@1952
   448
	}
ds@1952
   449
ds@1952
   450
	if (g_OnMapStarted)
ds@1952
   451
	{
ds@1952
   452
		if ((pFunction = m_ctx.base->GetFunctionByName("OnMapStart")) != NULL)
ds@1952
   453
		{
ds@1952
   454
			pFunction->Execute(NULL);
ds@1952
   455
		}
ds@1952
   456
	}
ds@1952
   457
ds@1952
   458
	if (SM_AreConfigsExecuted())
ds@1952
   459
	{
ds@1952
   460
		SM_ExecuteForPlugin(GetBaseContext());
ds@1952
   461
	}
ds@1952
   462
}
ds@1952
   463
ds@1952
   464
bool CPlugin::Call_AskPluginLoad(char *error, size_t maxlength)
ds@1952
   465
{
ds@1952
   466
	if (m_status != Plugin_Created)
ds@1952
   467
	{
ds@1952
   468
		return false;
ds@1952
   469
	}
ds@1952
   470
ds@1952
   471
	m_status = Plugin_Loaded;
ds@1952
   472
ds@1952
   473
	int err;
ds@1952
   474
	cell_t result;
ds@1952
   475
	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("AskPluginLoad");
ds@1952
   476
ds@1952
   477
	if (!pFunction)
ds@1952
   478
	{
ds@1952
   479
		return true;
ds@1952
   480
	}
ds@1952
   481
ds@1952
   482
	pFunction->PushCell(m_handle);
ds@1952
   483
	pFunction->PushCell(g_PluginSys.IsLateLoadTime() ? 1 : 0);
ds@1952
   484
	pFunction->PushStringEx(error, maxlength, 0, SM_PARAM_COPYBACK);
ds@1952
   485
	pFunction->PushCell(maxlength);
ds@1952
   486
	if ((err=pFunction->Execute(&result)) != SP_ERROR_NONE)
ds@1952
   487
	{
ds@1952
   488
		return false;
ds@1952
   489
	}
ds@1952
   490
ds@1952
   491
	if (!result || m_status != Plugin_Loaded)
ds@1952
   492
	{
ds@1952
   493
		return false;
ds@1952
   494
	}
ds@1952
   495
ds@1952
   496
	return true;
ds@1952
   497
}
ds@1952
   498
ds@1952
   499
const sp_plugin_t *CPlugin::GetPluginStructure()
ds@1952
   500
{
ds@1952
   501
	return m_plugin;
ds@1952
   502
}
ds@1952
   503
ds@1952
   504
IPluginContext *CPlugin::GetBaseContext()
ds@1952
   505
{
ds@1952
   506
	return m_ctx.base;
ds@1952
   507
}
ds@1952
   508
ds@1952
   509
sp_context_t *CPlugin::GetContext()
ds@1952
   510
{
ds@1952
   511
	return m_ctx.ctx;
ds@1952
   512
}
ds@1952
   513
ds@1952
   514
const char *CPlugin::GetFilename()
ds@1952
   515
{
ds@1952
   516
	return m_filename;
ds@1952
   517
}
ds@1952
   518
ds@1952
   519
PluginType CPlugin::GetType()
ds@1952
   520
{
ds@1952
   521
	return m_type;
ds@1952
   522
}
ds@1952
   523
ds@1952
   524
const sm_plugininfo_t *CPlugin::GetPublicInfo()
ds@1952
   525
{
ds@1952
   526
	return &m_info;
ds@1952
   527
}
ds@1952
   528
ds@1952
   529
unsigned int CPlugin::GetSerial()
ds@1952
   530
{
ds@1952
   531
	return m_serial;
ds@1952
   532
}
ds@1952
   533
ds@1952
   534
PluginStatus CPlugin::GetStatus()
ds@1952
   535
{
ds@1952
   536
	return m_status;
ds@1952
   537
}
ds@1952
   538
ds@1952
   539
bool CPlugin::IsDebugging()
ds@1952
   540
{
ds@1952
   541
	if (!m_ctx.ctx)
ds@1952
   542
	{
ds@1952
   543
		return false;
ds@1952
   544
	}
ds@1952
   545
ds@1952
   546
	return ((m_ctx.ctx->flags & SP_FLAG_DEBUG) == SP_FLAG_DEBUG);
ds@1952
   547
}
ds@1952
   548
ds@1952
   549
void CPlugin::LibraryActions(bool dropping)
ds@1952
   550
{
ds@1952
   551
	List<String>::iterator iter;
ds@1952
   552
	for (iter = m_Libraries.begin();
ds@1952
   553
		iter != m_Libraries.end();
ds@1952
   554
		iter++)
ds@1952
   555
	{
ds@1952
   556
		g_PluginSys.OnLibraryAction((*iter).c_str(), true, dropping);
ds@1952
   557
	}
ds@1952
   558
}
ds@1952
   559
ds@1952
   560
bool CPlugin::SetPauseState(bool paused)
ds@1952
   561
{
ds@1952
   562
	if (paused && GetStatus() != Plugin_Running)
ds@1952
   563
	{
ds@1952
   564
		return false;
ds@1952
   565
	} else if (!paused && GetStatus() != Plugin_Paused) {
ds@1952
   566
		return false;
ds@1952
   567
	}
ds@1952
   568
ds@1952
   569
	if (paused)
ds@1952
   570
	{
ds@1952
   571
		LibraryActions(true);
ds@1952
   572
	}
ds@1952
   573
ds@1952
   574
	IPluginFunction *pFunction = m_ctx.base->GetFunctionByName("OnPluginPauseChange");
ds@1952
   575
	if (pFunction)
ds@1952
   576
	{
ds@1952
   577
		cell_t result;
ds@1952
   578
		pFunction->PushCell(paused ? 1 : 0);
ds@1952
   579
		pFunction->Execute(&result);
ds@1952
   580
	}
ds@1952
   581
ds@1952
   582
	if (paused)
ds@1952
   583
	{
ds@1952
   584
		m_status = Plugin_Paused;
ds@1952
   585
		m_ctx.ctx->flags |= SPFLAG_PLUGIN_PAUSED;
ds@1952
   586
	} else {
ds@1952
   587
		m_status = Plugin_Running;
ds@1952
   588
		m_ctx.ctx->flags &= ~SPFLAG_PLUGIN_PAUSED;
ds@1952
   589
	}
ds@1952
   590
ds@1952
   591
	g_PluginSys._SetPauseState(this, paused);
ds@1952
   592
ds@1952
   593
	if (!paused)
ds@1952
   594
	{
ds@1952
   595
		LibraryActions(false);
ds@1952
   596
	}
ds@1952
   597
ds@1952
   598
	return true;
ds@1952
   599
}
ds@1952
   600
ds@1952
   601
IdentityToken_t *CPlugin::GetIdentity()
ds@1952
   602
{
ds@1952
   603
	return m_ident;
ds@1952
   604
}
ds@1952
   605
ds@1952
   606
bool CPlugin::ToggleDebugMode(bool debug, char *error, size_t maxlength)
ds@1952
   607
{
ds@1952
   608
	int err;
ds@1952
   609
ds@1952
   610
	if (!IsRunnable())
ds@1952
   611
	{
ds@1952
   612
		if (error)
ds@1952
   613
		{
ds@1952
   614
			snprintf(error, maxlength, "Plugin is not runnable.");
ds@1952
   615
		}
ds@1952
   616
		return false;
ds@1952
   617
	}
ds@1952
   618
ds@1952
   619
	if (debug && IsDebugging())
ds@1952
   620
	{
ds@1952
   621
		if (error)
ds@1952
   622
		{
ds@1952
   623
			snprintf(error, maxlength, "Plugin is already in debug mode.");
ds@1952
   624
		}
ds@1952
   625
		return false;
ds@1952
   626
	}
ds@1952
   627
	else if (!debug && !IsDebugging())
ds@1952
   628
	{
ds@1952
   629
		if (error)
ds@1952
   630
		{
ds@1952
   631
			snprintf(error, maxlength, "Plugins is already in production mode.");
ds@1952
   632
		}
ds@1952
   633
		return false;
ds@1952
   634
	}
ds@1952
   635
ds@1952
   636
	ICompilation *co = g_pVM->StartCompilation(m_ctx.ctx->plugin);
ds@1952
   637
	if (!g_pVM->SetCompilationOption(co, "debug", (debug) ? "1" : "0"))
ds@1952
   638
	{
ds@1952
   639
		if (error)
ds@1952
   640
		{
ds@1952
   641
			snprintf(error, maxlength, "Failed to change plugin mode (JIT failure).");
ds@1952
   642
		}
ds@1952
   643
		return false;
ds@1952
   644
	}
ds@1952
   645
ds@1952
   646
	sp_context_t *new_ctx = g_pVM->CompileToContext(co, &err);
ds@1952
   647
ds@1952
   648
	if (new_ctx)
ds@1952
   649
	{
ds@1952
   650
		memcpy(new_ctx->memory, m_ctx.ctx->memory, m_ctx.ctx->mem_size);
ds@1952
   651
		new_ctx->hp = m_ctx.ctx->hp;
ds@1952
   652
		new_ctx->sp = m_ctx.ctx->sp;
ds@1952
   653
		new_ctx->frm = m_ctx.ctx->frm;
ds@1952
   654
		new_ctx->dbreak = m_ctx.ctx->dbreak;
ds@1952
   655
		new_ctx->context = m_ctx.ctx->context;
ds@1952
   656
		memcpy(new_ctx->user, m_ctx.ctx->user, sizeof(m_ctx.ctx->user));
ds@1952
   657
ds@1952
   658
		uint32_t nativeCount = m_plugin->info.natives_num;
ds@1952
   659
		for (uint32_t i=0; i<nativeCount; i++)
ds@1952
   660
		{
ds@1952
   661
			new_ctx->natives[i].pfn = m_ctx.ctx->natives[i].pfn;
ds@1952
   662
			new_ctx->natives[i].status = m_ctx.ctx->natives[i].status;
ds@1952
   663
		}
ds@1952
   664
ds@1952
   665
		g_pVM->FreeContext(m_ctx.ctx);
ds@1952
   666
		m_ctx.ctx = new_ctx;
ds@1952
   667
		m_ctx.base->SetContext(new_ctx);
ds@1952
   668
ds@1952
   669
		UpdateInfo();
ds@1952
   670
	}
ds@1952
   671
	else
ds@1952
   672
	{
ds@1952
   673
		if (error)
ds@1952
   674
		{
ds@1952
   675
			snprintf(error, maxlength, "Failed to recompile plugin (JIT error %d).", err);
ds@1952
   676
		}
ds@1952
   677
		return false;
ds@1952
   678
	}
ds@1952
   679
ds@1952
   680
	return true;
ds@1952
   681
}
ds@1952
   682
ds@1952
   683
bool CPlugin::IsRunnable()
ds@1952
   684
{
ds@1952
   685
	return (m_status <= Plugin_Paused) ? true : false;
ds@1952
   686
}
ds@1952
   687
ds@1952
   688
time_t CPlugin::GetFileTimeStamp()
ds@1952
   689
{
ds@1952
   690
	char path[PLATFORM_MAX_PATH];
ds@1952
   691
	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "plugins/%s", m_filename);
ds@1952
   692
#ifdef PLATFORM_WINDOWS
ds@1952
   693
	struct _stat s;
ds@1952
   694
	if (_stat(path, &s) != 0)
ds@1952
   695
#elif defined PLATFORM_POSIX
ds@1952
   696
	struct stat s;
ds@1952
   697
	if (stat(path, &s) != 0)
ds@1952
   698
#endif
ds@1952
   699
	{
ds@1952
   700
		return 0;
ds@1952
   701
	}
ds@1952
   702
	else
ds@1952
   703
	{
ds@1952
   704
		return s.st_mtime;
ds@1952
   705
	}
ds@1952
   706
}
ds@1952
   707
ds@1952
   708
time_t CPlugin::GetTimeStamp()
ds@1952
   709
{
ds@1952
   710
	return m_LastAccess;
ds@1952
   711
}
ds@1952
   712
ds@1952
   713
void CPlugin::SetTimeStamp(time_t t)
ds@1952
   714
{
ds@1952
   715
	m_LastAccess = t;
ds@1952
   716
}
ds@1952
   717
dvander@2042
   718
IPhraseCollection *CPlugin::GetPhrases()
ds@1952
   719
{
dvander@2042
   720
	return m_pPhrases;
ds@1952
   721
}
ds@1952
   722
ds@1952
   723
void CPlugin::DependencyDropped(CPlugin *pOwner)
ds@1952
   724
{
ds@1952
   725
	if (!m_ctx.ctx)
ds@1952
   726
	{
ds@1952
   727
		return;
ds@1952
   728
	}
ds@1952
   729
ds@1952
   730
	List<String>::iterator reqlib_iter;
ds@1952
   731
	List<String>::iterator lib_iter;
ds@1952
   732
	for (lib_iter=pOwner->m_Libraries.begin(); lib_iter!=pOwner->m_Libraries.end(); lib_iter++)
ds@1952
   733
	{
ds@1952
   734
		for (reqlib_iter=m_RequiredLibs.begin(); reqlib_iter!=m_RequiredLibs.end(); reqlib_iter++)
ds@1952
   735
		{
ds@1952
   736
			if ((*reqlib_iter) == (*lib_iter))
ds@1952
   737
			{
ds@1952
   738
				m_LibraryMissing = true;
ds@1952
   739
			}
ds@1952
   740
		}	
ds@1952
   741
	}
ds@1952
   742
ds@1952
   743
	List<FakeNative *>::iterator iter;
ds@1952
   744
	FakeNative *pNative;
ds@1952
   745
	sp_native_t *native;
ds@1952
   746
	uint32_t idx;
ds@1952
   747
	unsigned int unbound = 0;
ds@1952
   748
ds@1952
   749
	for (iter = pOwner->m_fakeNatives.begin();
ds@1952
   750
		 iter != pOwner->m_fakeNatives.end();
ds@1952
   751
		 iter++)
ds@1952
   752
	{
ds@1952
   753
		pNative = (*iter);
ds@1952
   754
		/* Find this native! */
ds@1952
   755
		if (m_ctx.base->FindNativeByName(pNative->name.c_str(), &idx) != SP_ERROR_NONE)
ds@1952
   756
		{
ds@1952
   757
			continue;
ds@1952
   758
		}
ds@1952
   759
		/* Unbind it */
ds@1952
   760
		m_ctx.base->GetNativeByIndex(idx, &native);
ds@1952
   761
		native->pfn = NULL;
ds@1952
   762
		native->status = SP_NATIVE_UNBOUND;
ds@1952
   763
		unbound++;
ds@1952
   764
	}
ds@1952
   765
ds@1952
   766
	if (unbound)
ds@1952
   767
	{
ds@1952
   768
		m_FakeNativesMissing = true;
ds@1952
   769
	}
ds@1952
   770
ds@1952
   771
	/* :IDEA: in the future, add native trapping? */
ds@1952
   772
	if (m_FakeNativesMissing || m_LibraryMissing)
ds@1952
   773
	{
ds@1952
   774
		SetErrorState(Plugin_Error, "Depends on plugin: %s", pOwner->GetFilename());
ds@1952
   775
	}
ds@1952
   776
ds@1952
   777
	m_dependsOn.remove(pOwner);
ds@1952
   778
}
ds@1952
   779
ds@1952
   780
unsigned int CPlugin::GetConfigCount()
ds@1952
   781
{
ds@1952
   782
	return (unsigned int)m_configs.size();
ds@1952
   783
}
ds@1952
   784
ds@1952
   785
AutoConfig *CPlugin::GetConfig(unsigned int i)
ds@1952
   786
{
ds@1952
   787
	if (i >= GetConfigCount())
ds@1952
   788
	{
ds@1952
   789
		return NULL;
ds@1952
   790
	}
ds@1952
   791
ds@1952
   792
	return m_configs[i];
ds@1952
   793
}
ds@1952
   794
ds@1952
   795
void CPlugin::AddConfig(bool autoCreate, const char *cfg, const char *folder)
ds@1952
   796
{
ds@1952
   797
	/* Do a check for duplicates to prevent double-execution */
ds@1952
   798
	for (size_t i = 0; i < m_configs.size(); i++)
ds@1952
   799
	{
ds@1952
   800
		if (m_configs[i]->autocfg.compare(cfg) == 0
ds@1952
   801
			&& m_configs[i]->folder.compare(folder) == 0
ds@1952
   802
			&& m_configs[i]->create == autoCreate)
ds@1952
   803
		{
ds@1952
   804
			return;
ds@1952
   805
		}
ds@1952
   806
	}
ds@1952
   807
ds@1952
   808
	AutoConfig *c = new AutoConfig;
ds@1952
   809
ds@1952
   810
	c->autocfg = cfg;
ds@1952
   811
	c->folder = folder;
ds@1952
   812
	c->create = autoCreate;
ds@1952
   813
ds@1952
   814
	m_configs.push_back(c);
ds@1952
   815
}
ds@1952
   816
ds@1952
   817
/*******************
ds@1952
   818
 * PLUGIN ITERATOR *
ds@1952
   819
 *******************/
ds@1952
   820
ds@1952
   821
CPluginManager::CPluginIterator::CPluginIterator(List<CPlugin *> *_mylist)
ds@1952
   822
{
ds@1952
   823
	mylist = _mylist;
ds@1952
   824
	Reset();
ds@1952
   825
}
ds@1952
   826
ds@1952
   827
IPlugin *CPluginManager::CPluginIterator::GetPlugin()
ds@1952
   828
{
ds@1952
   829
	return (*current);
ds@1952
   830
}
ds@1952
   831
ds@1952
   832
bool CPluginManager::CPluginIterator::MorePlugins()
ds@1952
   833
{
ds@1952
   834
	return (current != mylist->end());
ds@1952
   835
}
ds@1952
   836
ds@1952
   837
void CPluginManager::CPluginIterator::NextPlugin()
ds@1952
   838
{
ds@1952
   839
	current++;
ds@1952
   840
}
ds@1952
   841
ds@1952
   842
void CPluginManager::CPluginIterator::Release()
ds@1952
   843
{
ds@1952
   844
	g_PluginSys.ReleaseIterator(this);
ds@1952
   845
}
ds@1952
   846
ds@1952
   847
CPluginManager::CPluginIterator::~CPluginIterator()
ds@1952
   848
{
ds@1952
   849
}
ds@1952
   850
ds@1952
   851
void CPluginManager::CPluginIterator::Reset()
ds@1952
   852
{
ds@1952
   853
	current = mylist->begin();
ds@1952
   854
}
ds@1952
   855
ds@1952
   856
/******************
ds@1952
   857
 * PLUGIN MANAGER *
ds@1952
   858
 ******************/
ds@1952
   859
ds@1952
   860
CPluginManager::CPluginManager()
ds@1952
   861
{
ds@1952
   862
	m_LoadLookup = sm_trie_create();
ds@1952
   863
	m_AllPluginsLoaded = false;
ds@1952
   864
	m_MyIdent = NULL;
ds@1952
   865
	m_pNativeLookup = sm_trie_create();
ds@1952
   866
	m_pCoreNatives = sm_trie_create();
ds@1952
   867
	m_LoadingLocked = false;
ds@1952
   868
}
ds@1952
   869
ds@1952
   870
CPluginManager::~CPluginManager()
ds@1952
   871
{
ds@1952
   872
	/* :NOTICE: 
ds@1952
   873
	 * Ignore the fact that there might be plugins in the cache.
ds@1952
   874
	 * This usually means that Core is not being unloaded properly, and everything
ds@1952
   875
	 * will crash anyway.  YAY
ds@1952
   876
	 */
ds@1952
   877
	sm_trie_destroy(m_LoadLookup);
ds@1952
   878
	sm_trie_destroy(m_pNativeLookup);
ds@1952
   879
	sm_trie_destroy(m_pCoreNatives);
ds@1952
   880
ds@1952
   881
	CStack<CPluginManager::CPluginIterator *>::iterator iter;
ds@1952
   882
	for (iter=m_iters.begin(); iter!=m_iters.end(); iter++)
ds@1952
   883
	{
ds@1952
   884
		delete (*iter);
ds@1952
   885
	}
ds@1952
   886
	m_iters.popall();
ds@1952
   887
}
ds@1952
   888
ds@1952
   889
void CPluginManager::Shutdown()
ds@1952
   890
{
ds@1952
   891
	List<CPlugin *>::iterator iter;
ds@1952
   892
ds@1952
   893
	while ((iter = m_plugins.begin()) != m_plugins.end())
ds@1952
   894
	{
ds@1952
   895
		UnloadPlugin(*iter);
ds@1952
   896
	}
ds@1952
   897
}
ds@1952
   898
ds@1952
   899
void CPluginManager::LoadAll_FirstPass(const char *config, const char *basedir)
ds@1952
   900
{
ds@1952
   901
	/* First read in the database of plugin settings */
ds@1952
   902
	SMCError err;
ds@1952
   903
	SMCStates states;
ds@1952
   904
	m_AllPluginsLoaded = false;
ds@1952
   905
	if ((err=textparsers->ParseFile_SMC(config, &m_PluginInfo, &states)) != SMCError_Okay)
ds@1952
   906
	{
ds@1952
   907
		g_Logger.LogError("[SM] Encountered fatal error parsing file \"%s\"", config);
ds@1952
   908
		const char *err_msg = textparsers->GetSMCErrorString(err);
ds@1952
   909
		if (err_msg)
ds@1952
   910
		{
ds@1952
   911
			g_Logger.LogError("[SM] Parse error encountered: \"%s\"", err_msg);
ds@1952
   912
		}
ds@1952
   913
	}
ds@1952
   914
ds@1952
   915
	LoadPluginsFromDir(basedir, NULL);
ds@1952
   916
}
ds@1952
   917
ds@1952
   918
void CPluginManager::LoadPluginsFromDir(const char *basedir, const char *localpath)
ds@1952
   919
{
ds@1952
   920
	char base_path[PLATFORM_MAX_PATH];
ds@1952
   921
ds@1952
   922
	/* Form the current path to start reading from */
ds@1952
   923
	if (localpath == NULL)
ds@1952
   924
	{
ds@1952
   925
		g_LibSys.PathFormat(base_path, sizeof(base_path), "%s", basedir);
ds@1952
   926
	} else {
ds@1952
   927
		g_LibSys.PathFormat(base_path, sizeof(base_path), "%s/%s", basedir, localpath);
ds@1952
   928
	}
ds@1952
   929
ds@1952
   930
	IDirectory *dir = g_LibSys.OpenDirectory(base_path);
ds@1952
   931
ds@1952
   932
	if (!dir)
ds@1952
   933
	{
ds@1952
   934
		char error[256];
ds@1952
   935
		g_LibSys.GetPlatformError(error, sizeof(error));
ds@1952
   936
		g_Logger.LogError("[SM] Failure reading from plugins path: %s", localpath);
ds@1952
   937
		g_Logger.LogError("[SM] Platform returned error: %s", error);
ds@1952
   938
		return;
ds@1952
   939
	}
ds@1952
   940
ds@1952
   941
	while (dir->MoreFiles())
ds@1952
   942
	{
ds@1952
   943
		if (dir->IsEntryDirectory() 
ds@1952
   944
			&& (strcmp(dir->GetEntryName(), ".") != 0)
ds@1952
   945
			&& (strcmp(dir->GetEntryName(), "..") != 0)
ds@1952
   946
			&& (strcmp(dir->GetEntryName(), "disabled") != 0)
ds@1952
   947
			&& (strcmp(dir->GetEntryName(), "optional") != 0))
ds@1952
   948
		{
ds@1952
   949
			char new_local[PLATFORM_MAX_PATH];
ds@1952
   950
			if (localpath == NULL)
ds@1952
   951
			{
ds@1952
   952
				/* If no path yet, don't add a former slash */
ds@1952
   953
				snprintf(new_local, sizeof(new_local), "%s", dir->GetEntryName());
ds@1952
   954
			} else {
ds@1952
   955
				g_LibSys.PathFormat(new_local, sizeof(new_local), "%s/%s", localpath, dir->GetEntryName());
ds@1952
   956
			}
ds@1952
   957
			LoadPluginsFromDir(basedir, new_local);
ds@1952
   958
		} else if (dir->IsEntryFile()) {
ds@1952
   959
			const char *name = dir->GetEntryName();
ds@1952
   960
			size_t len = strlen(name);
ds@1952
   961
			if (len >= 4
ds@1952
   962
				&& strcmp(&name[len-4], ".smx") == 0)
ds@1952
   963
			{
ds@1952
   964
				/* If the filename matches, load the plugin */
ds@1952
   965
				char plugin[PLATFORM_MAX_PATH];
ds@1952
   966
				if (localpath == NULL)
ds@1952
   967
				{
ds@1952
   968
					snprintf(plugin, sizeof(plugin), "%s", name);
ds@1952
   969
				} else {
ds@1952
   970
					g_LibSys.PathFormat(plugin, sizeof(plugin), "%s/%s", localpath, name);
ds@1952
   971
				}
ds@1952
   972
				LoadAutoPlugin(plugin);
ds@1952
   973
			}
ds@1952
   974
		}
ds@1952
   975
		dir->NextEntry();
ds@1952
   976
	}
ds@1952
   977
	g_LibSys.CloseDirectory(dir);
ds@1952
   978
}
ds@1952
   979
ds@1952
   980
LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength)
ds@1952
   981
{
ds@1952
   982
	if (m_LoadingLocked)
ds@1952
   983
	{
ds@1952
   984
		return LoadRes_NeverLoad;
ds@1952
   985
	}
ds@1952
   986
ds@1952
   987
	bool no_load = false;
ds@1952
   988
	PluginSettings *pset;
ds@1952
   989
	unsigned int setcount = m_PluginInfo.GetSettingsNum();
ds@1952
   990
	for (unsigned int i=0; i<setcount; i++)
ds@1952
   991
	{
ds@1952
   992
		if ((pset = m_PluginInfo.GetSettingsIfMatch(i, path)) == NULL)
ds@1952
   993
		{
ds@1952
   994
			continue;
ds@1952
   995
		}
ds@1952
   996
		if (pset->blockload_val)
ds@1952
   997
		{
ds@1952
   998
			no_load = true;
ds@1952
   999
			break;
ds@1952
  1000
		}
ds@1952
  1001
	}
ds@1952
  1002
ds@1952
  1003
	/**
ds@1952
  1004
	 * Does this plugin already exist?
ds@1952
  1005
	 */
ds@1952
  1006
	CPlugin *pPlugin;
ds@1952
  1007
	if (sm_trie_retrieve(m_LoadLookup, path, (void **)&pPlugin))
ds@1952
  1008
	{
ds@1952
  1009
		/* Check to see if we should try reloading it */
ds@1952
  1010
		if (pPlugin->GetStatus() == Plugin_BadLoad
ds@1952
  1011
			|| pPlugin->GetStatus() == Plugin_Error
ds@1952
  1012
			|| pPlugin->GetStatus() == Plugin_Failed
ds@1952
  1013
			|| no_load)
ds@1952
  1014
		{
ds@1952
  1015
			UnloadPlugin(pPlugin);
ds@1952
  1016
		}
ds@1952
  1017
		else
ds@1952
  1018
		{
ds@1952
  1019
			if (_plugin)
ds@1952
  1020
			{
ds@1952
  1021
				*_plugin = pPlugin;
ds@1952
  1022
			}
ds@1952
  1023
			return LoadRes_AlreadyLoaded;
ds@1952
  1024
		}
ds@1952
  1025
	}
ds@1952
  1026
ds@1952
  1027
	if (no_load)
ds@1952
  1028
	{
ds@1952
  1029
		return LoadRes_NeverLoad;
ds@1952
  1030
	}
ds@1952
  1031
ds@1952
  1032
	pPlugin = CPlugin::CreatePlugin(path, error, maxlength);
ds@1952
  1033
ds@1952
  1034
	assert(pPlugin != NULL);
ds@1952
  1035
ds@1952
  1036
	pPlugin->m_type = PluginType_MapUpdated;
ds@1952
  1037
ds@1952
  1038
	ICompilation *co = NULL;
ds@1952
  1039
ds@1952
  1040
	if (pPlugin->m_status == Plugin_Uncompiled)
ds@1952
  1041
	{
ds@1952
  1042
		co = pPlugin->StartMyCompile(g_pVM);
ds@1952
  1043
	}
ds@1952
  1044
ds@1952
  1045
	for (unsigned int i=0; i<setcount; i++)
ds@1952
  1046
	{
ds@1952
  1047
		if ((pset=m_PluginInfo.GetSettingsIfMatch(i, path)) == NULL)
ds@1952
  1048
		{
ds@1952
  1049
			continue;
ds@1952
  1050
		}
ds@1952
  1051
		pPlugin->m_type = pset->type_val;
ds@1952
  1052
		if (co)
ds@1952
  1053
		{
ds@1952
  1054
			for (unsigned int j=0; j<pset->opts_num; j++)
ds@1952
  1055
			{
ds@1952
  1056
				const char *key, *val;
ds@1952
  1057
				m_PluginInfo.GetOptionsForPlugin(pset, j, &key, &val);
ds@1952
  1058
				if (!key || !val)
ds@1952
  1059
				{
ds@1952
  1060
					continue;
ds@1952
  1061
				}
ds@1952
  1062
				if (!g_pVM->SetCompilationOption(co, key, val))
ds@1952
  1063
				{
ds@1952
  1064
					if (error)
ds@1952
  1065
					{
ds@1952
  1066
						snprintf(error, maxlength, "Unable to set JIT option (key \"%s\") (value \"%s\")", key, val);
ds@1952
  1067
					}
ds@1952
  1068
					pPlugin->CancelMyCompile();
ds@1952
  1069
					co = NULL;
ds@1952
  1070
					break;
ds@1952
  1071
				}
ds@1952
  1072
			}
ds@1952
  1073
		}
ds@1952
  1074
	}
ds@1952
  1075
ds@1952
  1076
	/* Do the actual compiling */
ds@1952
  1077
	if (co)
ds@1952
  1078
	{
ds@1952
  1079
		pPlugin->FinishMyCompile(error, maxlength);
ds@1952
  1080
		co = NULL;
ds@1952
  1081
	}
ds@1952
  1082
ds@1952
  1083
	/* Get the status */
ds@1952
  1084
	if (pPlugin->GetStatus() == Plugin_Created)
ds@1952
  1085
	{
ds@1952
  1086
		AddCoreNativesToPlugin(pPlugin);
ds@1952
  1087
		pPlugin->InitIdentity();
ds@1952
  1088
		if (pPlugin->Call_AskPluginLoad(error, maxlength))
ds@1952
  1089
		{
ds@1952
  1090
			/* Autoload any modules */
ds@1952
  1091
			LoadOrRequireExtensions(pPlugin, 1, error, maxlength);
ds@1952
  1092
		}
ds@1952
  1093
		else
ds@1952
  1094
		{
ds@1952
  1095
			pPlugin->SetErrorState(Plugin_Failed, "%s", error);
ds@1952
  1096
		}
ds@1952
  1097
	}
ds@1952
  1098
ds@1952
  1099
	/* Save the time stamp */
ds@1952
  1100
	time_t t = pPlugin->GetFileTimeStamp();
ds@1952
  1101
	pPlugin->SetTimeStamp(t);
ds@1952
  1102
ds@1952
  1103
	if (_plugin)
ds@1952
  1104
	{
ds@1952
  1105
		*_plugin = pPlugin;
ds@1952
  1106
	}
ds@1952
  1107
ds@1952
  1108
	return (pPlugin->GetStatus() == Plugin_Loaded) ? LoadRes_Successful : LoadRes_Failure;
ds@1952
  1109
}
ds@1952
  1110
ds@1952
  1111
IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType type, char error[], size_t maxlength, bool *wasloaded)
ds@1952
  1112
{
ds@1952
  1113
	CPlugin *pl;
ds@1952
  1114
	LoadRes res;
ds@1952
  1115
ds@1952
  1116
	*wasloaded = false;
ds@1952
  1117
	if ((res=_LoadPlugin(&pl, path, debug, type, error, maxlength)) == LoadRes_Failure)
ds@1952
  1118
	{
ds@1952
  1119
		delete pl;
ds@1952
  1120
		return NULL;
ds@1952
  1121
	}
ds@1952
  1122
ds@1952
  1123
	if (res == LoadRes_AlreadyLoaded)
ds@1952
  1124
	{
ds@1952
  1125
		*wasloaded = true;
ds@1952
  1126
		return pl;
ds@1952
  1127
	}
ds@1952
  1128
ds@1952
  1129
	if (res == LoadRes_NeverLoad)
ds@1952
  1130
	{
ds@1952
  1131
		if (m_LoadingLocked)
ds@1952
  1132
		{
ds@1952
  1133
			UTIL_Format(error, maxlength, "There is a global plugin loading lock in effect");
ds@1952
  1134
		}
ds@1952
  1135
		else
ds@1952
  1136
		{
ds@1952
  1137
			UTIL_Format(error, maxlength, "This plugin is blocked from loading (see plugin_settings.cfg)");
ds@1952
  1138
		}
ds@1952
  1139
		return NULL;
ds@1952
  1140
	}
ds@1952
  1141
ds@1952
  1142
	AddPlugin(pl);
ds@1952
  1143
ds@1952
  1144
	/* Run second pass if we need to */
ds@1952
  1145
	if (IsLateLoadTime() && pl->GetStatus() == Plugin_Loaded)
ds@1952
  1146
	{
ds@1952
  1147
		if (!RunSecondPass(pl, error, maxlength))
ds@1952
  1148
		{
ds@1952
  1149
			UnloadPlugin(pl);
ds@1952
  1150
			return NULL;
ds@1952
  1151
		}
ds@1952
  1152
		pl->Call_OnAllPluginsLoaded();
ds@1952
  1153
	}
ds@1952
  1154
ds@1952
  1155
	return pl;
ds@1952
  1156
}
ds@1952
  1157
ds@1952
  1158
void CPluginManager::LoadAutoPlugin(const char *plugin)
ds@1952
  1159
{
ds@1952
  1160
	CPlugin *pl = NULL;
ds@1952
  1161
	LoadRes res;
ds@1952
  1162
	char error[255] = "Unknown error";
ds@1952
  1163
ds@1952
  1164
	if ((res=_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure)
ds@1952
  1165
	{
ds@1952
  1166
		g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error);
ds@1952
  1167
		pl->SetErrorState(Plugin_Failed, "%s", error);
ds@1952
  1168
	}
ds@1952
  1169
ds@1952
  1170
	if (res == LoadRes_Successful)
ds@1952
  1171
	{
ds@1952
  1172
		AddPlugin(pl);
ds@1952
  1173
	}
ds@1952
  1174
}
ds@1952
  1175
ds@1952
  1176
void CPluginManager::AddPlugin(CPlugin *pPlugin)
ds@1952
  1177
{
ds@1952
  1178
	List<IPluginsListener *>::iterator iter;
ds@1952
  1179
	IPluginsListener *pListener;
ds@1952
  1180
ds@1952
  1181
	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
ds@1952
  1182
	{
ds@1952
  1183
		pListener = (*iter);
ds@1952
  1184
		pListener->OnPluginCreated(pPlugin);
ds@1952
  1185
	}
ds@1952
  1186
ds@1952
  1187
	m_plugins.push_back(pPlugin);
ds@1952
  1188
	sm_trie_insert(m_LoadLookup, pPlugin->m_filename, pPlugin);
ds@1952
  1189
}
ds@1952
  1190
ds@1952
  1191
void CPluginManager::LoadAll_SecondPass()
ds@1952
  1192
{
ds@1952
  1193
	List<CPlugin *>::iterator iter;
ds@1952
  1194
	CPlugin *pPlugin;
ds@1952
  1195
ds@1952
  1196
	char error[256];
ds@1952
  1197
	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++)
ds@1952
  1198
	{
ds@1952
  1199
		pPlugin = (*iter);
ds@1952
  1200
		if (pPlugin->GetStatus() == Plugin_Loaded)
ds@1952
  1201
		{
ds@1952
  1202
			error[0] = '\0';
ds@1952
  1203
			if (!RunSecondPass(pPlugin, error, sizeof(error)))
ds@1952
  1204
			{
ds@1952
  1205
				g_Logger.LogError("[SM] Unable to load plugin \"%s\": %s", pPlugin->GetFilename(), error);
ds@1952
  1206
				pPlugin->SetErrorState(Plugin_Failed, "%s", error);
ds@1952
  1207
			}
ds@1952
  1208
		}
ds@1952
  1209
	}
ds@1952
  1210
ds@1952
  1211
	m_AllPluginsLoaded = true;
ds@1952
  1212
}
ds@1952
  1213
ds@1952
  1214
bool CPluginManager::FindOrRequirePluginDeps(CPlugin *pPlugin, char *error, size_t maxlength)
ds@1952
  1215
{
ds@1952
  1216
	struct _pl
ds@1952
  1217
	{
ds@1952
  1218
		cell_t name;
ds@1952
  1219
		cell_t file;
ds@1952
  1220
		cell_t required;
ds@1952
  1221
	} *pl;
ds@1952
  1222
ds@1952
  1223
	IPluginContext *pBase = pPlugin->GetBaseContext();
ds@1952
  1224
	uint32_t num = pBase->GetPubVarsNum();
ds@1952
  1225
	sp_pubvar_t *pubvar;
ds@1952
  1226
	char *name, *file;
ds@1952
  1227
	char pathfile[PLATFORM_MAX_PATH];
ds@1952
  1228
ds@1952
  1229
	for (uint32_t i=0; i<num; i++)
ds@1952
  1230
	{
ds@1952
  1231
		if (pBase->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
ds@1952
  1232
		{
ds@1952
  1233
			continue;
ds@1952
  1234
		}
ds@1952
  1235
		if (strncmp(pubvar->name, "__pl_", 5) == 0)
ds@1952
  1236
		{
ds@1952
  1237
			pl = (_pl *)pubvar->offs;
ds@1952
  1238
			if (pBase->LocalToString(pl->file, &file) != SP_ERROR_NONE)
ds@1952
  1239
			{
ds@1952
  1240
				continue;
ds@1952
  1241
			}
ds@1952
  1242
			if (pBase->LocalToString(pl->name, &name) != SP_ERROR_NONE)
ds@1952
  1243
			{
ds@1952
  1244
				continue;
ds@1952
  1245
			}
ds@1952
  1246
			g_LibSys.GetFileFromPath(pathfile, sizeof(pathfile), pPlugin->GetFilename());
ds@1952
  1247
			if (strcmp(pathfile, file) == 0)
ds@1952
  1248
			{
ds@1952
  1249
				continue;
ds@1952
  1250
			}
ds@1952
  1251
			if (pl->required == false)
ds@1952
  1252
			{
ds@1952
  1253
				IPluginFunction *pFunc;
ds@1952
  1254
				char buffer[64];
ds@1952
  1255
				UTIL_Format(buffer, sizeof(buffer), "__pl_%s_SetNTVOptional", &pubvar->name[5]);
ds@1952
  1256
				if ((pFunc=pBase->GetFunctionByName(buffer)))
ds@1952
  1257
				{
ds@1952
  1258
					cell_t res;
ds@1952
  1259
					pFunc->Execute(&res);
ds@1952
  1260
					if (pPlugin->GetContext()->n_err != SP_ERROR_NONE)
ds@1952
  1261
					{
ds@1952
  1262
						if (error)
ds@1952
  1263
						{
ds@1952
  1264
							snprintf(error, maxlength, "Fatal error during initializing plugin load");
ds@1952
  1265
						}
ds@1952
  1266
						return false;
ds@1952
  1267
					}
ds@1952
  1268
				}
ds@1952
  1269
			}
ds@1952
  1270
			else
ds@1952
  1271
			{
ds@1952
  1272
				/* Check that we aren't registering the same library twice */
ds@1952
  1273
				if (pPlugin->m_RequiredLibs.find(name) == pPlugin->m_RequiredLibs.end())
ds@1952
  1274
				{
ds@1952
  1275
					pPlugin->m_RequiredLibs.push_back(name);
ds@1952
  1276
				}
ds@1952
  1277
				else
ds@1952
  1278
				{
ds@1952
  1279
					continue;
ds@1952
  1280
				}
ds@1952
  1281
				List<CPlugin *>::iterator iter;
ds@1952
  1282
				CPlugin *pl;
ds@1952
  1283
				bool found = false;
ds@1952
  1284
				for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++)
ds@1952
  1285
				{
ds@1952
  1286
					pl = (*iter);
ds@1952
  1287
					if (pl->m_Libraries.find(name) != pl->m_Libraries.end())
ds@1952
  1288
					{
ds@1952
  1289
						found = true;
ds@1952
  1290
						break;
ds@1952
  1291
					}
ds@1952
  1292
				}
ds@1952
  1293
				if (!found)
ds@1952
  1294
				{
ds@1952
  1295
					if (error)
ds@1952
  1296
					{
ds@1952
  1297
						snprintf(error, maxlength, "Could not find required plugin \"%s\"", name);
ds@1952
  1298
					}
ds@1952
  1299
					return false;
ds@1952
  1300
				}
ds@1952
  1301
			}
ds@1952
  1302
		}
ds@1952
  1303
	}
ds@1952
  1304
ds@1952
  1305
	return true;
ds@1952
  1306
}
ds@1952
  1307
ds@1952
  1308
bool CPluginManager::LoadOrRequireExtensions(CPlugin *pPlugin, unsigned int pass, char *error, size_t maxlength)
ds@1952
  1309
{
ds@1952
  1310
	/* Find any extensions this plugin needs */
ds@1952
  1311
	struct _ext
ds@1952
  1312
	{
ds@1952
  1313
		cell_t name;
ds@1952
  1314
		cell_t file;
ds@1952
  1315
		cell_t autoload;
ds@1952
  1316
		cell_t required;
ds@1952
  1317
	} *ext;
ds@1952
  1318
ds@1952
  1319
	IPluginContext *pBase = pPlugin->GetBaseContext();
ds@1952
  1320
	uint32_t num = pBase->GetPubVarsNum();
ds@1952
  1321
	sp_pubvar_t *pubvar;
ds@1952
  1322
	IExtension *pExt;
ds@1952
  1323
	char path[PLATFORM_MAX_PATH];
ds@1952
  1324
	char *file, *name;
ds@1952
  1325
	for (uint32_t i=0; i<num; i++)
ds@1952
  1326
	{
ds@1952
  1327
		if (pBase->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
ds@1952
  1328
		{
ds@1952
  1329
			continue;
ds@1952
  1330
		}
ds@1952
  1331
		if (strncmp(pubvar->name, "__ext_", 6) == 0)
ds@1952
  1332
		{
ds@1952
  1333
			ext = (_ext *)pubvar->offs;
ds@1952
  1334
			if (pBase->LocalToString(ext->file, &file) != SP_ERROR_NONE)
ds@1952
  1335
			{
ds@1952
  1336
				continue;
ds@1952
  1337
			}
ds@1952
  1338
			if (pBase->LocalToString(ext->name, &name) != SP_ERROR_NONE)
ds@1952
  1339
			{
ds@1952
  1340
				continue;
ds@1952
  1341
			}
ds@1952
  1342
			if (pass == 1)
ds@1952
  1343
			{
ds@1952
  1344
				/* Attempt to auto-load if necessary */
ds@1952
  1345
				if (ext->autoload)
ds@1952
  1346
				{
ds@1952
  1347
					g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
ds@1952
  1348
					g_Extensions.LoadAutoExtension(path);
ds@1952
  1349
				}
ds@1952
  1350
			}
ds@1952
  1351
			else if (pass == 2)
ds@1952
  1352
			{
ds@1952
  1353
				/* Is this required? */
ds@1952
  1354
				if (ext->required)
ds@1952
  1355
				{
ds@1952
  1356
					g_LibSys.PathFormat(path, PLATFORM_MAX_PATH, "%s", file);
ds@1952
  1357
					if ((pExt = g_Extensions.FindExtensionByFile(path)) == NULL)
ds@1952
  1358
					{
ds@1952
  1359
						pExt = g_Extensions.FindExtensionByName(name);
ds@1952
  1360
					}
ds@1952
  1361
					/* :TODO: should we bind to unloaded extensions?
ds@1952
  1362
					 * Currently the extension manager will ignore this.
ds@1952
  1363
					 */
ds@1952
  1364
					if (!pExt || !pExt->IsRunning(NULL, 0))
ds@1952
  1365
					{
ds@1952
  1366
						if (error)
ds@1952
  1367
						{
ds@1952
  1368
							snprintf(error, maxlength, "Required extension \"%s\" file(\"%s\") not running", name, file);
ds@1952
  1369
						}
ds@1952
  1370
						return false;
ds@1952
  1371
					}
ds@1952
  1372
					else
ds@1952
  1373
					{
ds@1952
  1374
						g_Extensions.BindChildPlugin(pExt, pPlugin);
ds@1952
  1375
					}
ds@1952
  1376
				}
ds@1952
  1377
				else
ds@1952
  1378
				{
ds@1952
  1379
					IPluginFunction *pFunc;
ds@1952
  1380
					char buffer[64];
ds@1952
  1381
					UTIL_Format(buffer, sizeof(buffer), "__ext_%s_SetNTVOptional", &pubvar->name[6]);
ds@1952
  1382
ds@1952
  1383
					if ((pFunc = pBase->GetFunctionByName(buffer)) != NULL)
ds@1952
  1384
					{
ds@1952
  1385
						cell_t res;
ds@1952
  1386
						pFunc->Execute(&res);
ds@1952
  1387
						if (pPlugin->GetContext()->n_err != SP_ERROR_NONE)
ds@1952
  1388
						{
ds@1952
  1389
							if (error)
ds@1952
  1390
							{
ds@1952
  1391
								UTIL_Format(error, maxlength, "Fatal error during plugin initialization (ext req)");
ds@1952
  1392
							}
ds@1952
  1393
							return false;
ds@1952
  1394
						}
ds@1952
  1395
					}
ds@1952
  1396
				}
ds@1952
  1397
			}
ds@1952
  1398
		}
ds@1952
  1399
	}
ds@1952
  1400
ds@1952
  1401
	return true;
ds@1952
  1402
}
ds@1952
  1403
ds@1952
  1404
bool CPluginManager::RunSecondPass(CPlugin *pPlugin, char *error, size_t maxlength)
ds@1952
  1405
{
ds@1952
  1406
	/* Second pass for extension requirements */
ds@1952
  1407
	if (!LoadOrRequireExtensions(pPlugin, 2, error, maxlength))
ds@1952
  1408
	{
ds@1952
  1409
		return false;
ds@1952
  1410
	}
ds@1952
  1411
ds@1952
  1412
	/* Bind all extra natives */
ds@1952
  1413
	g_Extensions.BindAllNativesToPlugin(pPlugin);
ds@1952
  1414
ds@1952
  1415
	if (!FindOrRequirePluginDeps(pPlugin, error, maxlength))
ds@1952
  1416
	{
ds@1952
  1417
		return false;
ds@1952
  1418
	}
ds@1952
  1419
ds@1952
  1420
	AddFakeNativesToPlugin(pPlugin);
ds@1952
  1421
ds@1952
  1422
	/* Find any unbound natives
ds@1952
  1423
	 * Right now, these are not allowed
ds@1952
  1424
	 */
ds@1952
  1425
	IPluginContext *pContext = pPlugin->GetBaseContext();
ds@1952
  1426
	uint32_t num = pContext->GetNativesNum();
ds@1952
  1427
	sp_native_t *native;
ds@1952
  1428
	for (unsigned int i=0; i<num; i++)
ds@1952
  1429
	{
ds@1952
  1430
		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
ds@1952
  1431
		{
ds@1952
  1432
			break;
ds@1952
  1433
		}
ds@1952
  1434
		if (native->status == SP_NATIVE_UNBOUND
ds@1952
  1435
			&& native->name[0] != '@'
ds@1952
  1436
			&& !(native->flags & SP_NTVFLAG_OPTIONAL))
ds@1952
  1437
		{
ds@1952
  1438
			if (error)
ds@1952
  1439
			{
ds@1952
  1440
				snprintf(error, maxlength, "Native \"%s\" was not found.", native->name);
ds@1952
  1441
			}
ds@1952
  1442
			return false;
ds@1952
  1443
		}
ds@1952
  1444
	}
ds@1952
  1445
ds@1952
  1446
	/* Finish by telling all listeners */
ds@1952
  1447
	List<IPluginsListener *>::iterator iter;
ds@1952
  1448
	IPluginsListener *pListener;
ds@1952
  1449
	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
ds@1952
  1450
	{
ds@1952
  1451
		pListener = (*iter);
ds@1952
  1452
		pListener->OnPluginLoaded(pPlugin);
ds@1952
  1453
	}
ds@1952
  1454
	
ds@1952
  1455
	/* Tell this plugin to finish initializing itself */
ds@1952
  1456
	pPlugin->Call_OnPluginStart();
ds@1952
  1457
ds@1952
  1458
	/* Now, if we have fake natives, go through all plugins that might need rebinding */
ds@1952
  1459
	if (pPlugin->GetStatus() <= Plugin_Paused && pPlugin->m_fakeNatives.size())
ds@1952
  1460
	{
ds@1952
  1461
		List<CPlugin *>::iterator pl_iter;
ds@1952
  1462
		CPlugin *pOther;
ds@1952
  1463
		for (pl_iter = m_plugins.begin();
ds@1952
  1464
			 pl_iter != m_plugins.end();
ds@1952
  1465
			 pl_iter++)
ds@1952
  1466
		{
ds@1952
  1467
			pOther = (*pl_iter);
ds@1952
  1468
			if ((pOther->GetStatus() == Plugin_Error
ds@1952
  1469
				&& (pOther->m_FakeNativesMissing || pOther->m_LibraryMissing))
ds@1952
  1470
				|| pOther->m_FakeNativesMissing)
ds@1952
  1471
			{
ds@1952
  1472
				TryRefreshDependencies(pOther);
ds@1952
  1473
			}
ds@1952
  1474
		}
ds@1952
  1475
	}
ds@1952
  1476
ds@1952
  1477
	/* Go through our libraries and tell other plugins they're added */
ds@1952
  1478
	List<String>::iterator s_iter;
ds@1952
  1479
	for (s_iter = pPlugin->m_Libraries.begin();
ds@1952
  1480
		s_iter != pPlugin->m_Libraries.end();
ds@1952
  1481
		s_iter++)
ds@1952
  1482
	{
ds@1952
  1483
		OnLibraryAction((*s_iter).c_str(), true, false);
ds@1952
  1484
	}
ds@1952
  1485
dvander@2042
  1486
	/* :TODO: optimize? does this even matter? */
dvander@2042
  1487
	pPlugin->GetPhrases()->AddPhraseFile("core.phrases");
ds@1952
  1488
ds@1952
  1489
	return true;
ds@1952
  1490
}
ds@1952
  1491
ds@1952
  1492
void CPluginManager::AddCoreNativesToPlugin(CPlugin *pPlugin)
ds@1952
  1493
{
ds@1952
  1494
	IPluginContext *pContext = pPlugin->GetBaseContext();
ds@1952
  1495
	
ds@1952
  1496
ds@1952
  1497
	uint32_t natives = pContext->GetNativesNum();
ds@1952
  1498
	sp_native_t *native;
ds@1952
  1499
	SPVM_NATIVE_FUNC pfn;
ds@1952
  1500
	for (uint32_t i=0; i<natives; i++)
ds@1952
  1501
	{
ds@1952
  1502
		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
ds@1952
  1503
		{
ds@1952
  1504
			continue;
ds@1952
  1505
		}
ds@1952
  1506
		if (native->status == SP_NATIVE_BOUND)
ds@1952
  1507
		{
ds@1952
  1508
			continue;
ds@1952
  1509
		}
ds@1952
  1510
		if (!sm_trie_retrieve(m_pCoreNatives, native->name, (void **)&pfn))
ds@1952
  1511
		{
ds@1952
  1512
			continue;
ds@1952
  1513
		}
ds@1952
  1514
		pContext->BindNativeToIndex(i, pfn);
ds@1952
  1515
	}
ds@1952
  1516
}
ds@1952
  1517
ds@1952
  1518
SPVM_NATIVE_FUNC CPluginManager::FindCoreNative(const char *name)
ds@1952
  1519
{
ds@1952
  1520
	SPVM_NATIVE_FUNC pfn;
ds@1952
  1521
ds@1952
  1522
	if (!sm_trie_retrieve(m_pCoreNatives, name, (void **)&pfn))
ds@1952
  1523
	{
ds@1952
  1524
		return NULL;
ds@1952
  1525
	}
ds@1952
  1526
ds@1952
  1527
	return pfn;
ds@1952
  1528
}
ds@1952
  1529
ds@1952
  1530
void CPluginManager::TryRefreshDependencies(CPlugin *pPlugin)
ds@1952
  1531
{
ds@1952
  1532
	assert(pPlugin->GetBaseContext() != NULL);
ds@1952
  1533
ds@1952
  1534
	AddFakeNativesToPlugin(pPlugin);
ds@1952
  1535
ds@1952
  1536
	List<String>::iterator lib_iter;
ds@1952
  1537
	List<String>::iterator req_iter;
ds@1952
  1538
	List<CPlugin *>::iterator pl_iter;
ds@1952
  1539
	CPlugin *pl;
ds@1952
  1540
	for (req_iter=pPlugin->m_RequiredLibs.begin(); req_iter!=pPlugin->m_RequiredLibs.end(); req_iter++)
ds@1952
  1541
	{
ds@1952
  1542
		bool found = false;
ds@1952
  1543
		for (pl_iter=m_plugins.begin(); pl_iter!=m_plugins.end(); pl_iter++)
ds@1952
  1544
		{
ds@1952
  1545
			pl = (*pl_iter);
ds@1952
  1546
			for (lib_iter=pl->m_Libraries.begin(); lib_iter!=pl->m_Libraries.end(); lib_iter++)
ds@1952
  1547
			{
ds@1952
  1548
				if ((*req_iter) == (*lib_iter))
ds@1952
  1549
				{
ds@1952
  1550
					found = true;
ds@1952
  1551
				}
ds@1952
  1552
			}
ds@1952
  1553
		}
ds@1952
  1554
		if (!found)
ds@1952
  1555
		{
ds@1952
  1556
			pPlugin->SetErrorState(Plugin_Error, "Library not found: %s", (*req_iter).c_str());
ds@1952
  1557
			return;
ds@1952
  1558
		}
ds@1952
  1559
	}
ds@1952
  1560
ds@1952
  1561
	/* Find any unbound natives
ds@1952
  1562
	 * Right now, these are not allowed
ds@1952
  1563
	 */
ds@1952
  1564
	IPluginContext *pContext = pPlugin->GetBaseContext();
ds@1952
  1565
	uint32_t num = pContext->GetNativesNum();
ds@1952
  1566
	sp_native_t *native;
ds@1952
  1567
	for (unsigned int i=0; i<num; i++)
ds@1952
  1568
	{
ds@1952
  1569
		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
ds@1952
  1570
		{
ds@1952
  1571
			break;
ds@1952
  1572
		}
ds@1952
  1573
		if (native->status == SP_NATIVE_UNBOUND && !(native->flags & SP_NTVFLAG_OPTIONAL))
ds@1952
  1574
		{
ds@1952
  1575
			pPlugin->SetErrorState(Plugin_Error, "Native not found: %s", native->name);
ds@1952
  1576
			return;
ds@1952
  1577
		}
ds@1952
  1578
	}
ds@1952
  1579
ds@1952
  1580
	if (pPlugin->GetStatus() == Plugin_Error)
ds@1952
  1581
	{
ds@1952
  1582
		/* If we got here, all natives are okay again! */
ds@1952
  1583
		pPlugin->m_status = Plugin_Running;
ds@1952
  1584
		if ((pPlugin->m_ctx.ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
ds@1952
  1585
		{
ds@1952
  1586
			pPlugin->m_ctx.ctx->flags &= ~SPFLAG_PLUGIN_PAUSED;
ds@1952
  1587
			_SetPauseState(pPlugin, false);
ds@1952
  1588
		}
ds@1952
  1589
	}
ds@1952
  1590
}
ds@1952
  1591
ds@1952
  1592
void CPluginManager::AddFakeNativesToPlugin(CPlugin *pPlugin)
ds@1952
  1593
{
ds@1952
  1594
	IPluginContext *pContext = pPlugin->GetBaseContext();
ds@1952
  1595
	sp_nativeinfo_t native;
ds@1952
  1596
ds@1952
  1597
	List<FakeNative *>::iterator iter;
ds@1952
  1598
	FakeNative *pNative;
ds@1952
  1599
	sp_context_t *ctx;
ds@1952
  1600
	for (iter = m_Natives.begin(); iter != m_Natives.end(); iter++)
ds@1952
  1601
	{
ds@1952
  1602
		pNative = (*iter);
ds@1952
  1603
		ctx = pNative->ctx->GetContext();
ds@1952
  1604
		if ((ctx->flags & SPFLAG_PLUGIN_PAUSED) == SPFLAG_PLUGIN_PAUSED)
ds@1952
  1605
		{
ds@1952
  1606
			/* Ignore natives in paused plugins */
ds@1952
  1607
			continue;
ds@1952
  1608
		}
ds@1952
  1609
		native.name = pNative->name.c_str();
ds@1952
  1610
		native.func = pNative->func;
ds@1952
  1611
		if (pContext->BindNative(&native) == SP_ERROR_NONE)
ds@1952
  1612
		{
ds@1952
  1613
			uint32_t idx;
ds@1952
  1614
			pContext->FindNativeByName(native.name, &idx);
ds@1952
  1615
			if (pPlugin->GetContext()->natives[idx].flags & SP_NTVFLAG_OPTIONAL)
ds@1952
  1616
			{
ds@1952
  1617
				WeakNative wkn(pPlugin, idx);
ds@1952
  1618
				GetPluginByCtx(ctx)->m_WeakNatives.push_back(wkn);
ds@1952
  1619
				continue;
ds@1952
  1620
			}
ds@1952
  1621
			/* Add us as a dependency, but we're careful not to do this circularly! */
ds@1952
  1622
			if (pNative->ctx != pContext)
ds@1952
  1623
			{
ds@1952
  1624
				CPlugin *pOther = GetPluginByCtx(ctx);
ds@1952
  1625
				if (pOther->m_dependents.find(pPlugin) == pOther->m_dependents.end())
ds@1952
  1626
				{
ds@1952
  1627
					pOther->m_dependents.push_back(pPlugin);
ds@1952
  1628
				}
ds@1952
  1629
				if (pPlugin->m_dependsOn.find(pOther) == pPlugin->m_dependsOn.end())
ds@1952
  1630
				{
ds@1952
  1631
					pPlugin->m_dependsOn.push_back(pOther);
ds@1952
  1632
				}
ds@1952
  1633
			}
ds@1952
  1634
		}
ds@1952
  1635
	}
ds@1952
  1636
}
ds@1952
  1637
ds@1952
  1638
bool CPluginManager::UnloadPlugin(IPlugin *plugin)
ds@1952
  1639
{
ds@1952
  1640
	CPlugin *pPlugin = (CPlugin *)plugin;
ds@1952
  1641
ds@1952
  1642
	/* This prevents removal during insertion or anything else weird */
ds@1952
  1643
	if (m_plugins.find(pPlugin) == m_plugins.end())
ds@1952
  1644
	{
ds@1952
  1645
		return false;
ds@1952
  1646
	}
ds@1952
  1647
ds@1952
  1648
	IPluginContext *pContext = plugin->GetBaseContext();
ds@1952
  1649
	if (pContext->IsInExec())
ds@1952
  1650
	{
ds@1952
  1651
		char buffer[255];
ds@1952
  1652
		UTIL_Format(buffer, sizeof(buffer), "sm plugins unload %s\n", plugin->GetFilename());
ds@1952
  1653
		engine->ServerCommand(buffer);
ds@1952
  1654
		return false;
ds@1952
  1655
	}
ds@1952
  1656
ds@1952
  1657
	/* Remove us from the lookup table and linked list */
ds@1952
  1658
	m_plugins.remove(pPlugin);
ds@1952
  1659
	sm_trie_delete(m_LoadLookup, pPlugin->m_filename);
ds@1952
  1660
ds@1952
  1661
	/* Go through our libraries and tell other plugins they're gone */
ds@1952
  1662
	List<String>::iterator s_iter;
ds@1952
  1663
	for (s_iter = pPlugin->m_Libraries.begin();
ds@1952
  1664
		 s_iter != pPlugin->m_Libraries.end();
ds@1952
  1665
		 s_iter++)
ds@1952
  1666
	{
ds@1952
  1667
		OnLibraryAction((*s_iter).c_str(), true, true);
ds@1952
  1668
	}
ds@1952
  1669
ds@1952
  1670
	/* Go through all dependent plugins and tell them this plugin is now gone */
ds@1952
  1671
	List<CPlugin *>::iterator pl_iter;
ds@1952
  1672
	CPlugin *pOther;
ds@1952
  1673
	for (pl_iter = pPlugin->m_dependents.begin();
ds@1952
  1674
		 pl_iter != pPlugin->m_dependents.end();
ds@1952
  1675
		 pl_iter++)
ds@1952
  1676
	{
ds@1952
  1677
		pOther = (*pl_iter);
ds@1952
  1678
		pOther->DependencyDropped(pPlugin);
ds@1952
  1679
	}
ds@1952
  1680
ds@1952
  1681
	/* Tell everyone we depend on that we no longer exist */
ds@1952
  1682
	for (pl_iter = pPlugin->m_dependsOn.begin();
ds@1952
  1683
		 pl_iter != pPlugin->m_dependsOn.end();
ds@1952
  1684
		 pl_iter++)
ds@1952
  1685
	{
ds@1952
  1686
		pOther = (*pl_iter);
ds@1952
  1687
		pOther->m_dependents.remove(pPlugin);
ds@1952
  1688
	}
ds@1952
  1689
ds@1952
  1690
	/* Remove weak references to us */
ds@1952
  1691
	for (pl_iter = m_plugins.begin();
ds@1952
  1692
		 pl_iter != m_plugins.end();
ds@1952
  1693
		 pl_iter++)
ds@1952
  1694
	{
ds@1952
  1695
		pOther = (*pl_iter);
ds@1952
  1696
		List<WeakNative>::iterator wk_iter = pOther->m_WeakNatives.begin();
ds@1952
  1697
		while (wk_iter != pOther->m_WeakNatives.end())
ds@1952
  1698
		{
ds@1952
  1699
			if ((*wk_iter).pl == pPlugin)
ds@1952
  1700
			{
ds@1952
  1701
				wk_iter = pOther->m_WeakNatives.erase(wk_iter);
ds@1952
  1702
			} else {
ds@1952
  1703
				wk_iter++;
ds@1952
  1704
			}
ds@1952
  1705
		}
ds@1952
  1706
	}
ds@1952
  1707
ds@1952
  1708
	List<IPluginsListener *>::iterator iter;
ds@1952
  1709
	IPluginsListener *pListener;
ds@1952
  1710
ds@1952
  1711
	if (pPlugin->GetStatus() <= Plugin_Error)
ds@1952
  1712
	{
ds@1952
  1713
		/* Notify listeners of unloading */
ds@1952
  1714
		for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
ds@1952
  1715
		{
ds@1952
  1716
			pListener = (*iter);
ds@1952
  1717
			pListener->OnPluginUnloaded(pPlugin);
ds@1952
  1718
		}
ds@1952
  1719
		/* Notify plugin */
ds@1952
  1720
		pPlugin->Call_OnPluginEnd();
ds@1952
  1721
	}
ds@1952
  1722
ds@1952
  1723
	/* Unbound weak natives */
ds@1952
  1724
	List<WeakNative>::iterator wk_iter;
ds@1952
  1725
	for (wk_iter=pPlugin->m_WeakNatives.begin(); wk_iter!=pPlugin->m_WeakNatives.end(); wk_iter++)
ds@1952
  1726
	{
ds@1952
  1727
		WeakNative & wkn = (*wk_iter);
ds@1952
  1728
		sp_context_t *ctx = wkn.pl->GetContext();
ds@1952
  1729
		ctx->natives[wkn.idx].status = SP_NATIVE_UNBOUND;
ds@1952
  1730
		wkn.pl->m_FakeNativesMissing = true;
ds@1952
  1731
	}
ds@1952
  1732
ds@1952
  1733
	/* Remove all of our native functions */
ds@1952
  1734
	List<FakeNative *>::iterator fn_iter;
ds@1952
  1735
	FakeNative *pNative;
ds@1952
  1736
	for (fn_iter = pPlugin->m_fakeNatives.begin();
ds@1952
  1737
		fn_iter != pPlugin->m_fakeNatives.end();
ds@1952
  1738
		fn_iter++)
ds@1952
  1739
	{
ds@1952
  1740
		pNative = (*fn_iter);
ds@1952
  1741
		m_Natives.remove(pNative);
ds@1952
  1742
		sm_trie_delete(m_pNativeLookup, pNative->name.c_str());
ds@1952
  1743
		g_pVM->DestroyFakeNative(pNative->func);
ds@1952
  1744
		delete pNative;
ds@1952
  1745
	}
ds@1952
  1746
ds@1952
  1747
	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
ds@1952
  1748
	{
ds@1952
  1749
		/* Notify listeners of destruction */
ds@1952
  1750
		pListener = (*iter);
ds@1952
  1751
		pListener->OnPluginDestroyed(pPlugin);
ds@1952
  1752
	}
ds@1952
  1753
	
ds@1952
  1754
	/* Tell the plugin to delete itself */
ds@1952
  1755
	delete pPlugin;
ds@1952
  1756
ds@1952
  1757
	return true;
ds@1952
  1758
}
ds@1952
  1759
ds@1952
  1760
IPlugin *CPluginManager::FindPluginByContext(const sp_context_t *ctx)
ds@1952
  1761
{
ds@1952
  1762
	IPlugin *pl = (IPlugin *)ctx->user[SM_CONTEXTVAR_MYSELF];
ds@1952
  1763
	return pl;
ds@1952
  1764
}
ds@1952
  1765
ds@1952
  1766
unsigned int CPluginManager::GetPluginCount()
ds@1952
  1767
{
ds@1952
  1768
	return m_plugins.size();
ds@1952
  1769
}
ds@1952
  1770
ds@1952
  1771
void CPluginManager::AddPluginsListener(IPluginsListener *listener)
ds@1952
  1772
{
ds@1952
  1773
	m_listeners.push_back(listener);
ds@1952
  1774
}
ds@1952
  1775
ds@1952
  1776
void CPluginManager::RemovePluginsListener(IPluginsListener *listener)
ds@1952
  1777
{
ds@1952
  1778
	m_listeners.remove(listener);
ds@1952
  1779
}
ds@1952
  1780
ds@1952
  1781
IPluginIterator *CPluginManager::GetPluginIterator()
ds@1952
  1782
{
ds@1952
  1783
	if (m_iters.empty())
ds@1952
  1784
	{
ds@1952
  1785
		return new CPluginIterator(&m_plugins);
ds@1952
  1786
	} else {
ds@1952
  1787
		CPluginIterator *iter = m_iters.front();
ds@1952
  1788
		m_iters.pop();
ds@1952
  1789
		iter->Reset();
ds@1952
  1790
		return iter;
ds@1952
  1791
	}
ds@1952
  1792
}
ds@1952
  1793
ds@1952
  1794
void CPluginManager::ReleaseIterator(CPluginIterator *iter)
ds@1952
  1795
{
ds@1952
  1796
	m_iters.push(iter);
ds@1952
  1797
}
ds@1952
  1798
ds@1952
  1799
bool CPluginManager::TestAliasMatch(const char *alias, const char *localpath)
ds@1952
  1800
{
ds@1952
  1801
	/* As an optimization, we do not call strlen, but compute the length in the first pass */
ds@1952
  1802
	size_t alias_len = 0;
ds@1952
  1803
	size_t local_len = 0;
ds@1952
  1804
ds@1952
  1805
	const char *ptr = alias;
ds@1952
  1806
	unsigned int alias_explicit_paths = 0;
ds@1952
  1807
	unsigned int alias_path_end = 0;
ds@1952
  1808
	while (*ptr != '\0')
ds@1952
  1809
	{
ds@1952
  1810
		if (*ptr == '\\' || *ptr == '/')
ds@1952
  1811
		{
ds@1952
  1812
			alias_explicit_paths++;
ds@1952
  1813
			alias_path_end = alias_len;
ds@1952
  1814
		}
ds@1952
  1815
		alias_len++;
ds@1952
  1816
		ptr++;
ds@1952
  1817
	}
ds@1952
  1818
ds@1952
  1819
	if (alias_explicit_paths && alias_path_end == alias_len - 1)
ds@1952
  1820
	{
ds@1952
  1821
		/* Trailing slash is totally invalid here */
ds@1952
  1822
		return false;
ds@1952
  1823
	}
ds@1952
  1824
ds@1952
  1825
	ptr = localpath;
ds@1952
  1826
	unsigned int local_explicit_paths = 0;
ds@1952
  1827
	unsigned int local_path_end = 0;
ds@1952
  1828
	while (*ptr != '\0')
ds@1952
  1829
	{
ds@1952
  1830
		if (*ptr == '\\' || *ptr == '/')
ds@1952
  1831
		{
ds@1952
  1832
			local_explicit_paths++;
ds@1952
  1833
			local_path_end = local_len;
ds@1952
  1834
		}
ds@1952
  1835
		local_len++;
ds@1952
  1836
		ptr++;
ds@1952
  1837
	}
ds@1952
  1838
ds@1952
  1839
	/* If the alias has more explicit paths than the real path,
ds@1952
  1840
	 * no match will be possible.
ds@1952
  1841
	 */
ds@1952
  1842
	if (alias_explicit_paths > local_explicit_paths)
ds@1952
  1843
	{
ds@1952
  1844
		return false;
ds@1952
  1845
	}
ds@1952
  1846
ds@1952
  1847
	if (alias_explicit_paths)
ds@1952
  1848
	{
ds@1952
  1849
		/* We need to find if the paths match now.  For example, these should all match:
ds@1952
  1850
		 * csdm     csdm
ds@1952
  1851
		 * csdm     optional/csdm
ds@1952
  1852
		 * csdm/ban optional/crab/csdm/ban
ds@1952
  1853
		*/
ds@1952
  1854
		const char *aliasptr = alias;
ds@1952
  1855
		const char *localptr = localpath;
ds@1952
  1856
		bool match = true;
ds@1952
  1857
		do
ds@1952
  1858
		{
ds@1952
  1859
			if (*aliasptr != *localptr)
ds@1952
  1860
			{
ds@1952
  1861
				/* We have to knock one path off */
ds@1952
  1862
				local_explicit_paths--;
ds@1952
  1863
				if (alias_explicit_paths > local_explicit_paths)
ds@1952
  1864
				{
ds@1952
  1865
					/* Skip out if we're gonna have an impossible match */
ds@1952
  1866
					return false;
ds@1952
  1867
				}
ds@1952
  1868
				/* Eat up localptr tokens until we get a result */
ds@1952
  1869
				while (((localptr - localpath) < (int)local_path_end)
ds@1952
  1870
					&& *localptr != '/'
ds@1952
  1871
					&& *localptr != '\\')
ds@1952
  1872
				{
ds@1952
  1873
					localptr++;
ds@1952
  1874
				}
ds@1952
  1875
				/* Check if we hit the end of our searchable area.
ds@1952
  1876
				 * This probably isn't possible because of the path 
ds@1952
  1877
				 * count check, but it's a good idea anyway.
ds@1952
  1878
				 */
ds@1952
  1879
				if ((localptr - localpath) >= (int)local_path_end)
ds@1952
  1880
				{
ds@1952
  1881
					return false;
ds@1952
  1882
				} else {
ds@1952
  1883
					/* Consume the slash token */
ds@1952
  1884
					localptr++;
ds@1952
  1885
				}
ds@1952
  1886
				/* Reset the alias pointer so we can continue consuming */
ds@1952
  1887
				aliasptr = alias;
ds@1952
  1888
				match = false;
ds@1952
  1889
				continue;
ds@1952
  1890
			}
ds@1952
  1891
			/* Note:
ds@1952
  1892
			 * This is safe because if localptr terminates early, aliasptr will too
ds@1952
  1893
			 */
ds@1952
  1894
			do
ds@1952
  1895
			{
ds@1952
  1896
				/* We should never reach the end of the string because of this check. */
ds@1952
  1897
				bool aliasend = (aliasptr - alias) > (int)alias_path_end;
ds@1952
  1898
				bool localend = (localptr - localpath) > (int)local_path_end;
ds@1952
  1899
				if (aliasend || localend)
ds@1952
  1900
				{
ds@1952
  1901
					if (aliasend && localend)
ds@1952
  1902
					{
ds@1952
  1903
						/* we matched, and we can break out now */
ds@1952
  1904
						match = true;
ds@1952
  1905
						break;
ds@1952
  1906
					}
ds@1952
  1907
					/* Otherwise, we've hit the end somehow and rest won't match up.  Break out. */
ds@1952
  1908
					match = false;
ds@1952
  1909
					break;
ds@1952
  1910
				}
ds@1952
  1911
ds@1952
  1912
				/* If we got here, it's safe to compare the next two tokens */
ds@1952
  1913
				if (*localptr != *aliasptr)
ds@1952
  1914
				{
ds@1952
  1915
					match = false;
ds@1952
  1916
					break;
ds@1952
  1917
				}
ds@1952
  1918
				localptr++;
ds@1952
  1919
				aliasptr++;
ds@1952
  1920
			} while (true);
ds@1952
  1921
		} while (!match);
ds@1952
  1922
	}
ds@1952
  1923
ds@1952
  1924
	/* If we got here, it's time to compare filenames */
ds@1952
  1925
	const char *aliasptr = alias;
ds@1952
  1926
	const char *localptr = localpath;
ds@1952
  1927
ds@1952
  1928
	if (alias_explicit_paths)
ds@1952
  1929
	{
ds@1952
  1930
		aliasptr = &alias[alias_path_end + 1];
ds@1952
  1931
	}
ds@1952
  1932
ds@1952
  1933
	if (local_explicit_paths)
ds@1952
  1934
	{
ds@1952
  1935
		localptr = &localpath[local_path_end + 1];
ds@1952
  1936
	}
ds@1952
  1937
ds@1952
  1938
	while (true)
ds@1952
  1939
	{
ds@1952
  1940
		if (*aliasptr == '*')
ds@1952
  1941
		{
ds@1952
  1942
			/* First, see if this is the last character */
ds@1952
  1943
			if ((unsigned)(aliasptr - alias) == alias_len - 1)
ds@1952
  1944
			{
ds@1952
  1945
				/* If so, there's no need to match anything else */
ds@1952
  1946
				return true;
ds@1952
  1947
			}
ds@1952
  1948
			/* Otherwise, we need to search for an appropriate matching sequence in local.
ds@1952
  1949
			 * Note that we only need to search up to the next asterisk.
ds@1952
  1950
			 */
ds@1952
  1951
			aliasptr++;
ds@1952
  1952
			bool match = true;
ds@1952
  1953
			const char *local_orig = localptr;
ds@1952
  1954
			do
ds@1952
  1955
			{
ds@1952
  1956
				match = true;
ds@1952
  1957
				while (*aliasptr != '\0' && *aliasptr != '*')
ds@1952
  1958
				{
ds@1952
  1959
					/* Since aliasptr is never '\0', localptr hitting the end will fail */
ds@1952
  1960
					if (*aliasptr != *localptr)
ds@1952
  1961
					{
ds@1952
  1962
						match = false;
ds@1952
  1963
						break;
ds@1952
  1964
					}
ds@1952
  1965
					aliasptr++;
ds@1952
  1966
					localptr++;
ds@1952
  1967
				}
ds@1952
  1968
				if (!match)
ds@1952
  1969
				{
ds@1952
  1970
					/* If we didn't get a match, we need to advance the search stream.
ds@1952
  1971
					 * This will let us skip tokens while still searching for another match.
ds@1952
  1972
					 */
ds@1952
  1973
					localptr = ++local_orig;
ds@1952
  1974
					/* Make sure we don't go out of bounds */
ds@1952
  1975
					if (*localptr == '\0')
ds@1952
  1976
					{
ds@1952
  1977
						break;
ds@1952
  1978
					}
ds@1952
  1979
				}
ds@1952
  1980
			} while (!match);
ds@1952
  1981
ds@1952
  1982
			if (!match)
ds@1952
  1983
			{
ds@1952
  1984
				return false;
ds@1952
  1985
			} else {
ds@1952
  1986
				/* If we got a match, move on to the next token */
ds@1952
  1987
				continue;
ds@1952
  1988
			}
ds@1952
  1989
		} else if (*aliasptr == '\0') {
ds@1952
  1990
			if (*localptr == '\0'
ds@1952
  1991
				||
ds@1952
  1992
				strcmp(localptr, ".smx") == 0)
ds@1952
  1993
			{
ds@1952
  1994
				return true;
ds@1952
  1995
			} else {
ds@1952
  1996
				return false;
ds@1952
  1997
			}
ds@1952
  1998
		} else if (*aliasptr != *localptr) {
ds@1952
  1999
			return false;
ds@1952
  2000
		}
ds@1952
  2001
		aliasptr++;
ds@1952
  2002
		localptr++;
ds@1952
  2003
	}
ds@1952
  2004
ds@1952
  2005
	return true;
ds@1952
  2006
}
ds@1952
  2007
ds@1952
  2008
bool CPluginManager::IsLateLoadTime() const
ds@1952
  2009
{
ds@1952
  2010
	return (m_AllPluginsLoaded || !g_SourceMod.IsMapLoading());
ds@1952
  2011
}
ds@1952
  2012
ds@1952
  2013
void CPluginManager::OnSourceModAllInitialized()
ds@1952
  2014
{
ds@1952
  2015
	m_MyIdent = g_ShareSys.CreateCoreIdentity();
ds@1952
  2016
ds@1952
  2017
	HandleAccess sec;
ds@1952
  2018
	g_HandleSys.InitAccessDefaults(NULL, &sec);
ds@1952
  2019
ds@1952
  2020
	sec.access[HandleAccess_Delete] = HANDLE_RESTRICT_IDENTITY;
ds@1952
  2021
	sec.access[HandleAccess_Clone] = HANDLE_RESTRICT_IDENTITY;
ds@1952
  2022
ds@1952
  2023
	g_PluginType = g_HandleSys.CreateType("Plugin", this, 0, NULL, &sec, m_MyIdent, NULL);
ds@1952
  2024
	g_PluginIdent = g_ShareSys.CreateIdentType("PLUGIN");
ds@1952
  2025
ds@1952
  2026
	g_RootMenu.AddRootConsoleCommand("plugins", "Manage Plugins", this);
ds@1952
  2027
ds@1952
  2028
	g_ShareSys.AddInterface(NULL, this);
ds@1952
  2029
}
ds@1952
  2030
ds@1952
  2031
void CPluginManager::OnSourceModShutdown()
ds@1952
  2032
{
ds@1952
  2033
	g_RootMenu.RemoveRootConsoleCommand("plugins", this);
ds@1952
  2034
ds@1952
  2035
	UnloadAll();
ds@1952
  2036
ds@1952
  2037
	g_HandleSys.RemoveType(g_PluginType, m_MyIdent);
ds@1952
  2038
	g_ShareSys.DestroyIdentType(g_PluginIdent);
ds@1952
  2039
	g_ShareSys.DestroyIdentity(m_MyIdent);
ds@1952
  2040
}
ds@1952
  2041
ds@1952
  2042
void CPluginManager::OnHandleDestroy(HandleType_t type, void *object)
ds@1952
  2043
{
ds@1952
  2044
	/* We don't care about the internal object, actually */
ds@1952
  2045
}
ds@1952
  2046
ds@1952
  2047
bool CPluginManager::GetHandleApproxSize(HandleType_t type, void *object, unsigned int *pSize)
ds@1952
  2048
{
ds@1952
  2049
	*pSize = ((CPlugin *)object)->CalcMemUsage();
ds@1952
  2050
	return true;
ds@1952
  2051
}
ds@1952
  2052
ds@1952
  2053
void CPluginManager::RegisterNativesFromCore(sp_nativeinfo_t *natives)
ds@1952
  2054
{
ds@1952
  2055
	for (unsigned int i = 0; natives[i].func != NULL; i++)
ds@1952
  2056
	{
ds@1952
  2057
		sm_trie_insert(m_pCoreNatives, natives[i].name, (void *)natives[i].func);
ds@1952
  2058
	}
ds@1952
  2059
}
ds@1952
  2060
ds@1952
  2061
IPlugin *CPluginManager::PluginFromHandle(Handle_t handle, HandleError *err)
ds@1952
  2062
{
ds@1952
  2063
	IPlugin *pPlugin;
ds@1952
  2064
	HandleError _err;
ds@1952
  2065
ds@1952
  2066
	HandleSecurity sec;
ds@1952
  2067
ds@1952
  2068
	sec.pOwner = NULL;
ds@1952
  2069
	sec.pIdentity = m_MyIdent;
ds@1952
  2070
ds@1952
  2071
	if ((_err=g_HandleSys.ReadHandle(handle, g_PluginType, &sec, (void **)&pPlugin)) != HandleError_None)
ds@1952
  2072
	{
ds@1952
  2073
		pPlugin = NULL;
ds@1952
  2074
	}
ds@1952
  2075
ds@1952
  2076
	if (err)
ds@1952
  2077
	{
ds@1952
  2078
		*err = _err;
ds@1952
  2079
	}
ds@1952
  2080
ds@1952
  2081
	return pPlugin;
ds@1952
  2082
}
ds@1952
  2083
ds@1952
  2084
CPlugin *CPluginManager::GetPluginByOrder(int num)
ds@1952
  2085
{
ds@1952
  2086
	if (num < 1 || num > (int)GetPluginCount())
ds@1952
  2087
	{
ds@1952
  2088
		return NULL;
ds@1952
  2089
	}
ds@1952
  2090
ds@1952
  2091
	CPlugin *pl;
ds@1952
  2092
	int id = 1;
ds@1952
  2093
ds@1952
  2094
	IPluginIterator *iter = GetPluginIterator();
ds@1952
  2095
	for (; iter->MorePlugins() && id<num; iter->NextPlugin(), id++) {}
ds@1952
  2096
ds@1952
  2097
	pl = (CPlugin *)(iter->GetPlugin());
ds@1952
  2098
	iter->Release();
ds@1952
  2099
ds@1952
  2100
	return pl;
ds@1952
  2101
}
ds@1952
  2102
ds@1952
  2103
const char *CPluginManager::GetStatusText(PluginStatus st)
ds@1952
  2104
{
ds@1952
  2105
	switch (st)
ds@1952
  2106
	{
ds@1952
  2107
	case Plugin_Running:
ds@1952
  2108
		return "Running";
ds@1952
  2109
	case Plugin_Paused:
ds@1952
  2110
		return "Paused";
ds@1952
  2111
	case Plugin_Error:
ds@1952
  2112
		return "Error";
ds@1952
  2113
	case Plugin_Uncompiled:
ds@1952
  2114
		return "Uncompiled";
ds@1952
  2115
	case Plugin_BadLoad:
ds@1952
  2116
		return "Bad Load";
ds@1952
  2117
	case Plugin_Failed:
ds@1952
  2118
		return "Failed";
ds@1952
  2119
	default:
ds@1952
  2120
		assert(false);
ds@1952
  2121
		return "-";
ds@1952
  2122
	}
ds@1952
  2123
}
ds@1952
  2124
ds@1952
  2125
void CPluginManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@1952
  2126
{
ds@1952
  2127
	int argcount = command.ArgC();
ds@1952
  2128
	if (argcount >= 3)
ds@1952
  2129
	{
ds@1952
  2130
		const char *cmd = command.Arg(2);
ds@1952
  2131
		if (strcmp(cmd, "list") == 0)
ds@1952
  2132
		{
ds@1952
  2133
			char buffer[256];
ds@1952
  2134
			unsigned int id = 1;
ds@1952
  2135
			int plnum = GetPluginCount();
ds@1952
  2136
ds@1952
  2137
			if (!plnum)
ds@1952
  2138
			{
ds@1952
  2139
				g_RootMenu.ConsolePrint("[SM] No plugins loaded");
ds@1952
  2140
				return;
ds@1952
  2141
			}
ds@1952
  2142
			else
ds@1952
  2143
			{
ds@1952
  2144
				g_RootMenu.ConsolePrint("[SM] Listing %d plugin%s:", GetPluginCount(), (plnum > 1) ? "s" : "");
ds@1952
  2145
			}
ds@1952
  2146
ds@1952
  2147
			SourceHook::List<IPlugin *> m_FailList;
ds@1952
  2148
ds@1952
  2149
			IPluginIterator *iter = GetPluginIterator();
ds@1952
  2150
			for (; iter->MorePlugins(); iter->NextPlugin(), id++)
ds@1952
  2151
			{
ds@1952
  2152
				IPlugin *pl = iter->GetPlugin();
ds@1952
  2153
				assert(pl->GetStatus() != Plugin_Created);
ds@1952
  2154
				int len = 0;
ds@1952
  2155
				const sm_plugininfo_t *info = pl->GetPublicInfo();
ds@1952
  2156
				if (pl->GetStatus() != Plugin_Running)
ds@1952
  2157
				{
ds@1952
  2158
					len += UTIL_Format(buffer, sizeof(buffer), "  %02d <%s>", id, GetStatusText(pl->GetStatus()));
ds@1952
  2159
ds@1952
  2160
					if (pl->GetStatus() <= Plugin_Error)
ds@1952
  2161
					{
ds@1952
  2162
						/* Plugin has failed to load. */
ds@1952
  2163
						m_FailList.push_back(pl);
ds@1952
  2164
					}
ds@1952
  2165
				}
ds@1952
  2166
				else
ds@1952
  2167
				{
ds@1952
  2168
					len += UTIL_Format(buffer, sizeof(buffer), "  %02d", id);
ds@1952
  2169
				}
ds@1952
  2170
				len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " \"%s\"", (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
ds@1952
  2171
				if (IS_STR_FILLED(info->version))
ds@1952
  2172
				{
ds@1952
  2173
					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", info->version);
ds@1952
  2174
				}
ds@1952
  2175
				if (IS_STR_FILLED(info->author))
ds@1952
  2176
				{
ds@1952
  2177
					UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", info->author);
ds@1952
  2178
				}
ds@1952
  2179
				g_RootMenu.ConsolePrint("%s", buffer);
ds@1952
  2180
			}
ds@1952
  2181
ds@1952
  2182
			iter->Release();
ds@1952
  2183
ds@1952
  2184
			if (!m_FailList.empty())
ds@1952
  2185
			{
ds@1952
  2186
				g_RootMenu.ConsolePrint("Load Errors:");
ds@1952
  2187
ds@1952
  2188
				SourceHook::List<IPlugin *>::iterator _iter;
ds@1952
  2189
ds@1952
  2190
				CPlugin *pl;
ds@1952
  2191
ds@1952
  2192
				for (_iter=m_FailList.begin(); _iter!=m_FailList.end(); _iter++)
ds@1952
  2193
				{
ds@1952
  2194
					pl = (CPlugin *)*_iter;
ds@1952
  2195
					g_RootMenu.ConsolePrint("%s: %s",(IS_STR_FILLED(pl->GetPublicInfo()->name)) ? pl->GetPublicInfo()->name : pl->GetFilename(), pl->m_errormsg);
ds@1952
  2196
				}
ds@1952
  2197
			}
ds@1952
  2198
ds@1952
  2199
			return;
ds@1952
  2200
		}
ds@1952
  2201
		else if (strcmp(cmd, "load") == 0)
ds@1952
  2202
		{
ds@1952
  2203
			if (argcount < 4)
ds@1952
  2204
			{
ds@1952
  2205
				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins load <file>");
ds@1952
  2206
				return;
ds@1952
  2207
			}
ds@1952
  2208
ds@1952
  2209
			char error[128];
ds@1952
  2210
			bool wasloaded;
ds@1952
  2211
			const char *filename = command.Arg(3);
ds@1952
  2212
ds@1952
  2213
			char pluginfile[256];
ds@1952
  2214
			const char *ext = g_LibSys.GetFileExtension(filename) ? "" : ".smx";
ds@1952
  2215
			UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", filename, ext);
ds@1952
  2216
ds@1952
  2217
			IPlugin *pl = LoadPlugin(pluginfile, false, PluginType_MapUpdated, error, sizeof(error), &wasloaded);
ds@1952
  2218
ds@1952
  2219
			if (wasloaded)
ds@1952
  2220
			{
ds@1952
  2221
				g_RootMenu.ConsolePrint("[SM] Plugin %s is already loaded.", pluginfile);
ds@1952
  2222
				return;
ds@1952
  2223
			}
ds@1952
  2224
ds@1952
  2225
			if (pl)
ds@1952
  2226
			{
ds@1952
  2227
				g_RootMenu.ConsolePrint("[SM] Loaded plugin %s successfully.", pluginfile);
ds@1952
  2228
			}
ds@1952
  2229
			else
ds@1952
  2230
			{
ds@1952
  2231
				g_RootMenu.ConsolePrint("[SM] Plugin %s failed to load: %s.", pluginfile, error);
ds@1952
  2232
			}
ds@1952
  2233
ds@1952
  2234
			return;
ds@1952
  2235
		}
ds@1952
  2236
		else if (strcmp(cmd, "unload") == 0)
ds@1952
  2237
		{
ds@1952
  2238
			if (argcount < 4)
ds@1952
  2239
			{
ds@1952
  2240
				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins unload <#|file>");
ds@1952
  2241
				return;
ds@1952
  2242
			}
ds@1952
  2243
ds@1952
  2244
			CPlugin *pl;
ds@1952
  2245
			char *end;
ds@1952
  2246
			const char *arg = command.Arg(3);
ds@1952
  2247
			int id = strtol(arg, &end, 10);
ds@1952
  2248
ds@1952
  2249
			if (*end == '\0')
ds@1952
  2250
			{
ds@1952
  2251
				pl = GetPluginByOrder(id);
ds@1952
  2252
				if (!pl)
ds@1952
  2253
				{
ds@1952
  2254
					g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@1952
  2255
					return;
ds@1952
  2256
				}
ds@1952
  2257
			}
ds@1952
  2258
			else
ds@1952
  2259
			{
ds@1952
  2260
				char pluginfile[256];
ds@1952
  2261
				const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx";
ds@1952
  2262
				UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
ds@1952
  2263
ds@1952
  2264
				if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl))
ds@1952
  2265
				{
ds@1952
  2266
					g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
ds@1952
  2267
					return;
ds@1952
  2268
				}
ds@1952
  2269
			}
ds@1952
  2270
ds@1952
  2271
			char name[PLATFORM_MAX_PATH];
ds@1952
  2272
			const sm_plugininfo_t *info = pl->GetPublicInfo();
ds@1952
  2273
			strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
ds@1952
  2274
ds@1952
  2275
			if (UnloadPlugin(pl))
ds@1952
  2276
			{
ds@1952
  2277
				g_RootMenu.ConsolePrint("[SM] Plugin %s unloaded successfully.", name);
ds@1952
  2278
			}
ds@1952
  2279
			else
ds@1952
  2280
			{
ds@1952
  2281
				g_RootMenu.ConsolePrint("[SM] Failed to unload plugin %s.", name);
ds@1952
  2282
			}
ds@1952
  2283
ds@1952
  2284
			return;
ds@1952
  2285
		}
ds@1952
  2286
		else if (strcmp(cmd, "unload_all") == 0)
ds@1952
  2287
		{
ds@1952
  2288
			g_PluginSys.UnloadAll();
ds@1952
  2289
			g_RootMenu.ConsolePrint("[SM] All plugins have been unloaded.");
ds@1952
  2290
			return;
ds@1952
  2291
		}
ds@1952
  2292
		else if (strcmp(cmd, "load_lock") == 0)
ds@1952
  2293
		{
ds@1952
  2294
			if (m_LoadingLocked)
ds@1952
  2295
			{
ds@1952
  2296
				g_RootMenu.ConsolePrint("[SM] There is already a loading lock in effect.");
ds@1952
  2297
			}
ds@1952
  2298
			else
ds@1952
  2299
			{
ds@1952
  2300
				m_LoadingLocked = true;
ds@1952
  2301
				g_RootMenu.ConsolePrint("[SM] Loading is now locked; no plugins will be loaded or re-loaded.");
ds@1952
  2302
			}
ds@1952
  2303
			return;
ds@1952
  2304
		}
ds@1952
  2305
		else if (strcmp(cmd, "load_unlock") == 0)
ds@1952
  2306
		{
ds@1952
  2307
			if (m_LoadingLocked)
ds@1952
  2308
			{
ds@1952
  2309
				m_LoadingLocked = false;
ds@1952
  2310
				g_RootMenu.ConsolePrint("[SM] The loading lock is no longer in effect.");
ds@1952
  2311
			}
ds@1952
  2312
			else
ds@1952
  2313
			{
ds@1952
  2314
				g_RootMenu.ConsolePrint("[SM] There was no loading lock in effect.");
ds@1952
  2315
			}
ds@1952
  2316
			return;
ds@1952
  2317
		}
ds@1952
  2318
		else if (strcmp(cmd, "info") == 0)
ds@1952
  2319
		{
ds@1952
  2320
			if (argcount < 4)
ds@1952
  2321
			{
ds@1952
  2322
				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins info <#>");
ds@1952
  2323
				return;
ds@1952
  2324
			}
ds@1952
  2325
ds@1952
  2326
			CPlugin *pl;
ds@1952
  2327
			char *end;
ds@1952
  2328
			const char *arg = command.Arg(3);
ds@1952
  2329
			int id = strtol(arg, &end, 10);
ds@1952
  2330
ds@1952
  2331
			if (*end == '\0')
ds@1952
  2332
			{
ds@1952
  2333
				pl = GetPluginByOrder(id);
ds@1952
  2334
				if (!pl)
ds@1952
  2335
				{
ds@1952
  2336
					g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@1952
  2337
					return;
ds@1952
  2338
				}
ds@1952
  2339
			}
ds@1952
  2340
			else
ds@1952
  2341
			{
ds@1952
  2342
				char pluginfile[256];
ds@1952
  2343
				const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx";
ds@1952
  2344
				UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
ds@1952
  2345
ds@1952
  2346
				if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl))
ds@1952
  2347
				{
ds@1952
  2348
					g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
ds@1952
  2349
					return;
ds@1952
  2350
				}
ds@1952
  2351
			}
ds@1952
  2352
ds@1952
  2353
			const sm_plugininfo_t *info = pl->GetPublicInfo();
ds@1952
  2354
ds@1952
  2355
			g_RootMenu.ConsolePrint("  Filename: %s", pl->GetFilename());
ds@1952
  2356
			if (pl->GetStatus() <= Plugin_Error)
ds@1952
  2357
			{
ds@1952
  2358
				if (IS_STR_FILLED(info->name))
ds@1952
  2359
				{
ds@1952
  2360
					if (IS_STR_FILLED(info->description))
ds@1952
  2361
					{
ds@1952
  2362
						g_RootMenu.ConsolePrint("  Title: %s (%s)", info->name, info->description);
ds@1952
  2363
					} else {
ds@1952
  2364
						g_RootMenu.ConsolePrint("  Title: %s", info->name);
ds@1952
  2365
					}
ds@1952
  2366
				}
ds@1952
  2367
				if (IS_STR_FILLED(info->author))
ds@1952
  2368
				{
ds@1952
  2369
					g_RootMenu.ConsolePrint("  Author: %s", info->author);
ds@1952
  2370
				}
ds@1952
  2371
				if (IS_STR_FILLED(info->version))
ds@1952
  2372
				{
ds@1952
  2373
					g_RootMenu.ConsolePrint("  Version: %s", info->version);
ds@1952
  2374
				}
ds@1952
  2375
				if (IS_STR_FILLED(info->url))
ds@1952
  2376
				{
ds@1952
  2377
					g_RootMenu.ConsolePrint("  URL: %s", info->url);
ds@1952
  2378
				}
ds@1952
  2379
				if (pl->GetStatus() == Plugin_Error)
ds@1952
  2380
				{
ds@1952
  2381
					g_RootMenu.ConsolePrint("  Error: %s", pl->m_errormsg);
ds@1952
  2382
				}
ds@1952
  2383
				else
ds@1952
  2384
				{
ds@1952
  2385
					g_RootMenu.ConsolePrint("  Debugging: %s", pl->IsDebugging() ? "Yes" : "No");
ds@1952
  2386
					g_RootMenu.ConsolePrint("  Running: %s", pl->GetStatus() == Plugin_Running ? "Yes" : "No");
ds@1952
  2387
ds@1952
  2388
					const char *typestr = "";
ds@1952
  2389
					switch (pl->GetType())
ds@1952
  2390
					{
ds@1952
  2391
					case PluginType_MapUpdated:
ds@1952
  2392
						typestr = "Map Change if Updated";
ds@1952
  2393
						break;
ds@1952
  2394
					case PluginType_MapOnly:
ds@1952
  2395
						typestr = "Map Change";
ds@1952
  2396
						break;
ds@1952
  2397
					case PluginType_Private:
ds@1952
  2398
					case PluginType_Global:
ds@1952
  2399
						typestr = "Never";
ds@1952
  2400
						break;
ds@1952
  2401
					}
ds@1952
  2402
ds@1952
  2403
					g_RootMenu.ConsolePrint("  Reloads: %s", typestr);
ds@1952
  2404
				}
ds@1952
  2405
			}
ds@1952
  2406
			else
ds@1952
  2407
			{
ds@1952
  2408
				g_RootMenu.ConsolePrint("  Load error: %s", pl->m_errormsg);
ds@1952
  2409
				g_RootMenu.ConsolePrint("  File info: (title \"%s\") (version \"%s\")", 
ds@1952
  2410
										info->name ? info->name : "<none>",
ds@1952
  2411
										info->version ? info->version : "<none>");
ds@1952
  2412
				if (IS_STR_FILLED(info->url))
ds@1952
  2413
				{
ds@1952
  2414
					g_RootMenu.ConsolePrint("  File URL: %s", info->url);
ds@1952
  2415
				}
ds@1952
  2416
			}
ds@1952
  2417
ds@1952
  2418
			return;
ds@1952
  2419
		}
ds@1952
  2420
		else if (strcmp(cmd, "debug") == 0)
ds@1952
  2421
		{
ds@1952
  2422
			if (argcount < 5)
ds@1952
  2423
			{
ds@1952
  2424
				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins debug <#> [on|off]");
ds@1952
  2425
				return;
ds@1952
  2426
			}
ds@1952
  2427
ds@1952
  2428
			CPlugin *pl;
ds@1952
  2429
			char *end;
ds@1952
  2430
			const char *arg = command.Arg(3);
ds@1952
  2431
			int id = strtol(arg, &end, 10);
ds@1952
  2432
ds@1952
  2433
			if (*end == '\0')
ds@1952
  2434
			{
ds@1952
  2435
				pl = GetPluginByOrder(id);
ds@1952
  2436
				if (!pl)
ds@1952
  2437
				{
ds@1952
  2438
					g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@1952
  2439
					return;
ds@1952
  2440
				}
ds@1952
  2441
			}
ds@1952
  2442
			else
ds@1952
  2443
			{
ds@1952
  2444
				char pluginfile[256];
ds@1952
  2445
				const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx";
ds@1952
  2446
				UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
ds@1952
  2447
ds@1952
  2448
				if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl))
ds@1952
  2449
				{
ds@1952
  2450
					g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
ds@1952
  2451
					return;
ds@1952
  2452
				}
ds@1952
  2453
			}
ds@1952
  2454
ds@1952
  2455
			int res;
ds@1952
  2456
			const char *mode = command.Arg(4);
ds@1952
  2457
			if ((res=strcmp("on", mode)) && strcmp("off", mode))
ds@1952
  2458
			{
ds@1952
  2459
				g_RootMenu.ConsolePrint("[SM] The only possible options are \"on\" and \"off.\"");
ds@1952
  2460
				return;
ds@1952
  2461
			}
ds@1952
  2462
ds@1952
  2463
			bool debug;
ds@1952
  2464
			if (!res)
ds@1952
  2465
			{
ds@1952
  2466
				debug = true;
ds@1952
  2467
			}
ds@1952
  2468
			else
ds@1952
  2469
			{
ds@1952
  2470
				debug = false;
ds@1952
  2471
			}
ds@1952
  2472
ds@1952
  2473
			if (debug && pl->IsDebugging())
ds@1952
  2474
			{
ds@1952
  2475
				g_RootMenu.ConsolePrint("[SM] This plugin is already in debug mode.");
ds@1952
  2476
				return;
ds@1952
  2477
			}
ds@1952
  2478
			else if (!debug && !pl->IsDebugging())
ds@1952
  2479
			{
ds@1952
  2480
				g_RootMenu.ConsolePrint("[SM] Debug mode is already disabled in this plugin.");
ds@1952
  2481
				return;
ds@1952
  2482
			}
ds@1952
  2483
ds@1952
  2484
			char error[256];
ds@1952
  2485
			if (pl->ToggleDebugMode(debug, error, sizeof(error)))
ds@1952
  2486
			{
ds@1952
  2487
				g_RootMenu.ConsolePrint("[SM] Successfully toggled debug mode on plugin %s.", pl->GetFilename());
ds@1952
  2488
				return;
ds@1952
  2489
			}
ds@1952
  2490
			else
ds@1952
  2491
			{
ds@1952
  2492
				g_RootMenu.ConsolePrint("[SM] Could not toggle debug mode on plugin %s.", pl->GetFilename());
ds@1952
  2493
				g_RootMenu.ConsolePrint("[SM] Plugin returned error: %s", error);
ds@1952
  2494
				return;
ds@1952
  2495
			}
ds@1952
  2496
		}
ds@1952
  2497
		else if (strcmp(cmd, "refresh") == 0)
ds@1952
  2498
		{
ds@1952
  2499
			g_SourceMod.DoGlobalPluginLoads();
ds@1952
  2500
			g_RootMenu.ConsolePrint("[SM] The plugin list has been refreshed and reloaded.");
ds@1952
  2501
			return;
ds@1952
  2502
		}
ds@1952
  2503
		else if (strcmp(cmd, "reload") == 0)
ds@1952
  2504
		{
ds@1952
  2505
			if (argcount < 4)
ds@1952
  2506
			{
ds@1952
  2507
				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins reload <#|file>");
ds@1952
  2508
				return;
ds@1952
  2509
			}
ds@1952
  2510
ds@1952
  2511
			CPlugin *pl;
ds@1952
  2512
			char *end;
ds@1952
  2513
			const char *arg = command.Arg(3);
ds@1952
  2514
			int id = strtol(arg, &end, 10);
ds@1952
  2515
ds@1952
  2516
			if (*end == '\0')
ds@1952
  2517
			{
ds@1952
  2518
				pl = GetPluginByOrder(id);
ds@1952
  2519
				if (!pl)
ds@1952
  2520
				{
ds@1952
  2521
					g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
ds@1952
  2522
					return;
ds@1952
  2523
				}
ds@1952
  2524
			}
ds@1952
  2525
			else
ds@1952
  2526
			{
ds@1952
  2527
				char pluginfile[256];
ds@1952
  2528
				const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx";
ds@1952
  2529
				UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
ds@1952
  2530
ds@1952
  2531
				if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl))
ds@1952
  2532
				{
ds@1952
  2533
					g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
ds@1952
  2534
					return;
ds@1952
  2535
				}
ds@1952
  2536
			}
ds@1952
  2537
ds@1952
  2538
			char name[PLATFORM_MAX_PATH];
ds@1952
  2539
			const sm_plugininfo_t *info = pl->GetPublicInfo();
ds@1952
  2540
			strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
ds@1952
  2541
ds@1952
  2542
			if (ReloadPlugin(pl))
ds@1952
  2543
			{
ds@1952
  2544
				g_RootMenu.ConsolePrint("[SM] Plugin %s reloaded successfully.", name);
ds@1952
  2545
			}
ds@1952
  2546
			else
ds@1952
  2547
			{
ds@1952
  2548
				g_RootMenu.ConsolePrint("[SM] Failed to reload plugin %s.", name);
ds@1952
  2549
			}
ds@1952
  2550
ds@1952
  2551
			return;
ds@1952
  2552
		}
ds@1952
  2553
	}
ds@1952
  2554
ds@1952
  2555
	/* Draw the main menu */
ds@1952
  2556
	g_RootMenu.ConsolePrint("SourceMod Plugins Menu:");
ds@1952
  2557
	g_RootMenu.DrawGenericOption("debug", "Toggle debug mode on a plugin");
ds@1952
  2558
	g_RootMenu.DrawGenericOption("info", "Information about a plugin");
ds@1952
  2559
	g_RootMenu.DrawGenericOption("list", "Show loaded plugins");
ds@1952
  2560
	g_RootMenu.DrawGenericOption("load", "Load a plugin");
ds@1952
  2561
	g_RootMenu.DrawGenericOption("load_lock", "Prevents any more plugins from being loaded");
ds@1952
  2562
	g_RootMenu.DrawGenericOption("load_unlock", "Re-enables plugin loading");
ds@1952
  2563
	g_RootMenu.DrawGenericOption("refresh", "Reloads/refreshes all plugins in the plugins folder");
ds@1952
  2564
	g_RootMenu.DrawGenericOption("reload", "Reloads a plugin");
ds@1952
  2565
	g_RootMenu.DrawGenericOption("unload", "Unload a plugin");
ds@1952
  2566
	g_RootMenu.DrawGenericOption("unload_all", "Unloads all plugins");
ds@1952
  2567
}
ds@1952
  2568
ds@1952
  2569
bool CPluginManager::ReloadPlugin(CPlugin *pl)
ds@1952
  2570
{
ds@1952
  2571
	List<CPlugin *>::iterator iter;
ds@1952
  2572
	char filename[PLATFORM_MAX_PATH];
ds@1952
  2573
	bool debug, wasloaded;
ds@1952
  2574
	PluginType ptype;
ds@1952
  2575
	IPlugin *newpl;
ds@1952
  2576
	int id = 1;
ds@1952
  2577
ds@1952
  2578
	strcpy(filename, pl->m_filename);
ds@1952
  2579
	debug = pl->IsDebugging();
ds@1952
  2580
	ptype = pl->GetType();
ds@1952
  2581
ds@1952
  2582
	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++, id++)
ds@1952
  2583
	{
ds@1952
  2584
		if ((*iter) == pl)
ds@1952
  2585
		{
ds@1952
  2586
			break;
ds@1952
  2587
		}
ds@1952
  2588
	}
ds@1952
  2589
ds@1952
  2590
	if (!UnloadPlugin(pl))
ds@1952
  2591
	{
ds@1952
  2592
		return false;
ds@1952
  2593
	}
ds@1952
  2594
	if (!(newpl=LoadPlugin(filename, debug, ptype, NULL, 0, &wasloaded)))
ds@1952
  2595
	{
ds@1952
  2596
		return false;
ds@1952
  2597
	}
ds@1952
  2598
ds@1952
  2599
	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++)
ds@1952
  2600
	{
ds@1952
  2601
		if ((*iter) == (CPlugin *)newpl)
ds@1952
  2602
		{
ds@1952
  2603
			m_plugins.erase(iter);
ds@1952
  2604
			break;
ds@1952
  2605
		}
ds@1952
  2606
	}
ds@1952
  2607
ds@1952
  2608
	int i;
ds@1952
  2609
	for (i=1, iter=m_plugins.begin(); iter!=m_plugins.end() && i<id; iter++, i++)
ds@1952
  2610
	{
ds@1952
  2611
	}
ds@1952
  2612
	m_plugins.insert(iter, (CPlugin *)newpl);
ds@1952
  2613
ds@1952
  2614
	return true;
ds@1952
  2615
}
ds@1952
  2616
ds@1952
  2617
void CPluginManager::ReloadOrUnloadPlugins()
ds@1952
  2618
{
ds@1952
  2619
	/* If we're in a load lock, just skip this whole bit. */
ds@1952
  2620
	if (m_LoadingLocked)
ds@1952
  2621
	{
ds@1952
  2622
		return;
ds@1952
  2623
	}
ds@1952
  2624
ds@1952
  2625
	List<CPlugin *>::iterator iter;
ds@1952
  2626
	List<CPlugin *> tmp_list = m_plugins;
ds@1952
  2627
	CPlugin *pl;
ds@1952
  2628
	time_t t;
ds@1952
  2629
ds@1952
  2630
	for (iter=tmp_list.begin(); iter!=tmp_list.end(); iter++)
ds@1952
  2631
	{
ds@1952
  2632
		pl = (*iter);
ds@1952
  2633
		if (pl->GetType() ==  PluginType_MapOnly)
ds@1952
  2634
		{
ds@1952
  2635
			UnloadPlugin((IPlugin *)pl);
ds@1952
  2636
		}
ds@1952
  2637
		else if (pl->GetType() == PluginType_MapUpdated)
ds@1952
  2638
		{
ds@1952
  2639
			t = pl->GetFileTimeStamp();
ds@1952
  2640
			if (!t || t > pl->GetTimeStamp())
ds@1952
  2641
			{
ds@1952
  2642
				pl->SetTimeStamp(t);
ds@1952
  2643
				UnloadPlugin((IPlugin *)pl);
ds@1952
  2644
			}
ds@1952
  2645
		}
ds@1952
  2646
	}
ds@1952
  2647
}
ds@1952
  2648
ds@1952
  2649
void CPluginManager::_SetPauseState(CPlugin *pl, bool paused)
ds@1952
  2650
{
ds@1952
  2651
	List<IPluginsListener *>::iterator iter;
ds@1952
  2652
	IPluginsListener *pListener;
ds@1952
  2653
	for (iter=m_listeners.begin(); iter!=m_listeners.end(); iter++)
ds@1952
  2654
	{
ds@1952
  2655
		pListener = (*iter);
ds@1952
  2656
		pListener->OnPluginPauseChange(pl, paused);
ds@1952
  2657
	}	
ds@1952
  2658
}
ds@1952
  2659
ds@1952
  2660
bool CPluginManager::AddFakeNative(IPluginFunction *pFunction, const char *name, SPVM_FAKENATIVE_FUNC func)
ds@1952
  2661
{
ds@1952
  2662
	if (sm_trie_retrieve(m_pNativeLookup, name, NULL))
ds@1952
  2663
	{
ds@1952
  2664
		return false;
ds@1952
  2665
	}
ds@1952
  2666
ds@1952
  2667
	FakeNative *pNative = new FakeNative;
ds@1952
  2668
ds@1952
  2669
	pNative->func = g_pVM->CreateFakeNative(func, pNative);
ds@1952
  2670
	if (!pNative->func)
ds@1952
  2671
	{
ds@1952
  2672
		delete pNative;
ds@1952
  2673
		return false;
ds@1952
  2674
	}
ds@1952
  2675
ds@1952
  2676
	pNative->call = pFunction;
ds@1952
  2677
	pNative->name.assign(name);
ds@1952
  2678
	pNative->ctx = pFunction->GetParentContext();
ds@1952
  2679
ds@1952
  2680
	m_Natives.push_back(pNative);
ds@1952
  2681
	sm_trie_insert(m_pNativeLookup, name,pNative);
ds@1952
  2682
ds@1952
  2683
	CPlugin *pPlugin = GetPluginByCtx(pNative->ctx->GetContext());
ds@1952
  2684
	pPlugin->m_fakeNatives.push_back(pNative);
ds@1952
  2685
ds@1952
  2686
	return true;
ds@1952
  2687
}
ds@1952
  2688
ds@1952
  2689
void CPluginManager::AddFunctionsToForward(const char *name, IChangeableForward *pForward)
ds@1952
  2690
{
ds@1952
  2691
	List<CPlugin *>::iterator iter;
ds@1952
  2692
	CPlugin *pPlugin;
ds@1952
  2693
	IPluginFunction *pFunction;
ds@1952
  2694
ds@1952
  2695
	for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++)
ds@1952
  2696
	{
ds@1952
  2697
		pPlugin = (*iter);
ds@1952
  2698
ds@1952
  2699
		if (pPlugin->GetStatus() <= Plugin_Paused)
ds@1952
  2700
		{
ds@1952
  2701
			pFunction = pPlugin->GetBaseContext()->GetFunctionByName(name);
ds@1952
  2702
ds@1952
  2703
			if (pFunction)
ds@1952
  2704
			{
ds@1952
  2705
				pForward->AddFunction(pFunction);
ds@1952
  2706
			}
ds@1952
  2707
		}
ds@1952
  2708
	}
ds@1952
  2709
}
ds@1952
  2710
ds@1952
  2711
CPlugin *CPluginManager::GetPluginFromIdentity(IdentityToken_t *pToken)
ds@1952
  2712
{
ds@1952
  2713
	if (pToken->type != g_PluginIdent)
ds@1952
  2714
	{
ds@1952
  2715
		return NULL;
ds@1952
  2716
	}
ds@1952
  2717
ds@1952
  2718
	return (CPlugin *)(pToken->ptr);
ds@1952
  2719
}
ds@1952
  2720
ds@1952
  2721
void CPluginManager::OnLibraryAction(const char *lib, bool is_a_plugin, bool drop)
ds@1952
  2722
{
ds@1952
  2723
	List<CPlugin *>::iterator iter;
ds@1952
  2724
ds@1952
  2725
	struct _pl
ds@1952
  2726
	{
ds@1952
  2727
		cell_t name;
ds@1952
  2728
		cell_t file;
ds@1952
  2729
		cell_t required;
ds@1952
  2730
	} *plc;
ds@1952
  2731
ds@1952
  2732
	struct _ext
ds@1952
  2733
	{
ds@1952
  2734
		cell_t name;
ds@1952
  2735
		cell_t file;
ds@1952
  2736
		cell_t autoload;
ds@1952
  2737
		cell_t required;
ds@1952
  2738
	} *ext;
ds@1952
  2739
ds@1952
  2740
	const char *name = drop ? "OnLibraryRemoved" : "OnLibraryAdded";
ds@1952
  2741
ds@1952
  2742
	for (iter=m_plugins.begin();
ds@1952
  2743
		 iter!=m_plugins.end();
ds@1952
  2744
		 iter++)
ds@1952
  2745
	{
ds@1952
  2746
		CPlugin *pl = (*iter);
ds@1952
  2747
		if (pl->GetStatus() != Plugin_Running)
ds@1952
  2748
		{
ds@1952
  2749
			continue;
ds@1952
  2750
		}
ds@1952
  2751
		IPluginContext *pContext = pl->GetBaseContext();
ds@1952
  2752
		IPluginFunction *pf = pContext->GetFunctionByName(name);
ds@1952
  2753
		if (!pf)
ds@1952
  2754
		{
ds@1952
  2755
			continue;
ds@1952
  2756
		}
ds@1952
  2757
		uint32_t num_vars = pContext->GetPubVarsNum();
ds@1952
  2758
		for (uint32_t i=0; i<num_vars; i++)
ds@1952
  2759
		{
ds@1952
  2760
			sp_pubvar_t *pubvar;
ds@1952
  2761
			if (pContext->GetPubvarByIndex(i, &pubvar) != SP_ERROR_NONE)
ds@1952
  2762
			{
ds@1952
  2763
				continue;
ds@1952
  2764
			}
ds@1952
  2765
			if (is_a_plugin && strncmp(pubvar->name, "__pl_", 5) == 0)
ds@1952
  2766
			{
ds@1952
  2767
				plc = (_pl *)pubvar->offs;
ds@1952
  2768
				if (plc->required)
ds@1952
  2769
				{
ds@1952
  2770
					continue;
ds@1952
  2771
				}
ds@1952
  2772
				char *str;
ds@1952
  2773
				pContext->LocalToString(plc->name, &str);
ds@1952
  2774
				if (strcmp(str, lib) != 0)
ds@1952
  2775
				{
ds@1952
  2776
					continue;
ds@1952
  2777
				}
ds@1952
  2778
				pf->PushString(lib);
ds@1952
  2779
				pf->Execute(NULL);
ds@1952
  2780
			}
ds@1952
  2781
			else if (!is_a_plugin && strncmp(pubvar->name, "__ext_", 6) == 0)
ds@1952
  2782
			{
ds@1952
  2783
				ext = (_ext *)pubvar->offs;
ds@1952
  2784
				if (ext->required)
ds@1952
  2785
				{
ds@1952
  2786
					continue;
ds@1952
  2787
				}
ds@1952
  2788
				char *str;
ds@1952
  2789
				pContext->LocalToString(ext->name, &str);
ds@1952
  2790
				if (strcmp(str, lib) != 0)
ds@1952
  2791
				{
ds@1952
  2792
					continue;
ds@1952
  2793
				}
ds@1952
  2794
				pf->PushString(lib);
ds@1952
  2795
				pf->Execute(NULL);
ds@1952
  2796
			}
ds@1952
  2797
		}
ds@1952
  2798
	}
ds@1952
  2799
}
ds@1952
  2800
ds@1952
  2801
bool CPluginManager::LibraryExists(const char *lib)
ds@1952
  2802
{
ds@1952
  2803
	List<CPlugin *>::iterator iter;
ds@1952
  2804
ds@1952
  2805
	for (iter=m_plugins.begin();
ds@1952
  2806
		 iter!=m_plugins.end();
ds@1952
  2807
		 iter++)
ds@1952
  2808
	{
ds@1952
  2809
		CPlugin *pl = (*iter);
ds@1952
  2810
		if (pl->GetStatus() != Plugin_Running)
ds@1952
  2811
		{
ds@1952
  2812
			continue;
ds@1952
  2813
		}
ds@1952
  2814
		List<String>::iterator s_iter;
ds@1952
  2815
		for (s_iter = pl->m_Libraries.begin();
ds@1952
  2816
			 s_iter != pl->m_Libraries.end();
ds@1952
  2817
			 s_iter++)
ds@1952
  2818
		{
ds@1952
  2819
			if ((*s_iter).compare(lib) == 0)
ds@1952
  2820
			{
ds@1952
  2821
				return true;
ds@1952
  2822
			}
ds@1952
  2823
		}
ds@1952
  2824
	}
ds@1952
  2825
ds@1952
  2826
	return false;
ds@1952
  2827
}
ds@1952
  2828
ds@1952
  2829
void CPluginManager::AllPluginsLoaded()
ds@1952
  2830
{
ds@1952
  2831
	List<CPlugin *>::iterator iter;
ds@1952
  2832
	CPlugin *pl;
ds@1952
  2833
ds@1952
  2834
	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++)
ds@1952
  2835
	{
ds@1952
  2836
		pl = (*iter);
ds@1952
  2837
		pl->Call_OnAllPluginsLoaded();
ds@1952
  2838
	}
ds@1952
  2839
}
ds@1952
  2840
ds@1952
  2841
void CPluginManager::UnloadAll()
ds@1952
  2842
{
ds@1952
  2843
	List<CPlugin *>::iterator iter;
ds@1952
  2844
	while ( (iter = m_plugins.begin()) != m_plugins.end() )
ds@1952
  2845
	{
ds@1952
  2846
		UnloadPlugin((*iter));
ds@1952
  2847
	}
ds@1952
  2848
}
ds@1952
  2849
ds@1952
  2850
int CPluginManager::GetOrderOfPlugin(IPlugin *pl)
ds@1952
  2851
{
ds@1952
  2852
	int id = 1;
ds@1952
  2853
	List<CPlugin *>::iterator iter;
ds@1952
  2854
ds@1952
  2855
	for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++, id++)
ds@1952
  2856
	{
ds@1952
  2857
		if ((*iter) == pl)
ds@1952
  2858
		{
ds@1952
  2859
			return id;
ds@1952
  2860
		}
ds@1952
  2861
	}
ds@1952
  2862
ds@1952
  2863
	return -1;
ds@1952
  2864
}
ds@1952
  2865
ds@1952
  2866
CPlugin *CPluginManager::FindPluginByConsoleArg(const char *arg)
ds@1952
  2867
{
ds@1952
  2868
	int id;
ds@1952
  2869
	char *end;
ds@1952
  2870
	CPlugin *pl;
ds@1952
  2871
	
ds@1952
  2872
	id = strtol(arg, &end, 10);
ds@1952
  2873
ds@1952
  2874
	if (*end == '\0')
ds@1952
  2875
	{
ds@1952
  2876
		pl = GetPluginByOrder(id);
ds@1952
  2877
		if (!pl)
ds@1952
  2878
		{
ds@1952
  2879
			return NULL;
ds@1952
  2880
		}
ds@1952
  2881
	}
ds@1952
  2882
	else
ds@1952
  2883
	{
ds@1952
  2884
		char pluginfile[256];
ds@1952
  2885
		const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx";
ds@1952
  2886
		UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
ds@1952
  2887
ds@1952
  2888
		if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl))
ds@1952
  2889
		{
ds@1952
  2890
			return NULL;
ds@1952
  2891
		}
ds@1952
  2892
	}
ds@1952
  2893
ds@1952
  2894
	return pl;
ds@1952
  2895
}