core/systems/PluginSys.cpp
author David Anderson <dvander@alliedmods.net>
Fri Jul 11 08:18:43 2008 +0000 (2008-07-11)
changeset 2193 db7c6362f3f2
parent 2113 8e8b28162e23
child 2210 79822782197b
permissions -rw-r--r--
landed massive refactoring of the JIT. contexts are actually contexts now, and a higher level structure wraps sp_plugin_t info. on that note, both sp_plugin_t and sp_context_t are entirely opaque, and not even core has access to them. amazingly, i managed to keep binary compatibility here although a large number of functions are deprecated (and core should eventually stop calling them).

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