core/systems/ExtensionSys.cpp
author David Anderson <dvander@alliedmods.net>
Sun Apr 13 04:00:44 2008 +0000 (2008-04-13)
changeset 1998 cf2fdf09195a
parent 1994 985f914c20fc
child 2086 debb2ab2c782
permissions -rw-r--r--
fixed crash bug (regression) when extensions failed to load
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 <stdlib.h>
ds@1952
    33
#include "ExtensionSys.h"
ds@1952
    34
#include "LibrarySys.h"
ds@1952
    35
#include "ShareSys.h"
ds@1952
    36
#include "Logger.h"
ds@1952
    37
#include "sourcemm_api.h"
ds@1952
    38
#include "sm_srvcmds.h"
ds@1952
    39
#include "sm_stringutil.h"
ds@1952
    40
ds@1952
    41
CExtensionManager g_Extensions;
ds@1952
    42
IdentityType_t g_ExtType;
ds@1952
    43
ds@1952
    44
void CExtension::Initialize(const char *filename, const char *path)
ds@1952
    45
{
ds@1952
    46
	m_pAPI = NULL;
ds@1952
    47
	m_pIdentToken = NULL;
ds@1952
    48
	unload_code = 0;
ds@1952
    49
	m_bFullyLoaded = false;
ds@1952
    50
	m_File.assign(filename);
ds@1952
    51
	m_Path.assign(path);
ds@1952
    52
}
ds@1952
    53
ds@1952
    54
CRemoteExtension::CRemoteExtension(IExtensionInterface *pAPI, const char *filename, const char *path)
ds@1952
    55
{
ds@1952
    56
	Initialize(filename, path);
ds@1952
    57
	m_pAPI = pAPI;
ds@1952
    58
}
ds@1952
    59
ds@1952
    60
CLocalExtension::CLocalExtension(const char *filename)
ds@1952
    61
{
ds@1952
    62
	m_PlId = 0;
ds@1952
    63
	m_pLib = NULL;
ds@1952
    64
ds@1952
    65
	char path[PLATFORM_MAX_PATH];
ds@1952
    66
ds@1952
    67
	/* First see if there is an engine specific build! */
ds@1952
    68
	g_SourceMod.BuildPath(Path_SM, 
ds@1952
    69
		path, 
ds@1952
    70
		PLATFORM_MAX_PATH, 
ds@1952
    71
#if defined METAMOD_PLAPI_VERSION
ds@1952
    72
#if defined ORANGEBOX_BUILD
ds@1952
    73
		"extensions/auto.2.ep2/%s",
ds@1952
    74
#else
ds@1952
    75
		"extensions/auto.2.ep1/%s",
ds@1952
    76
#endif //ORANGEBOX_BUILD
ds@1952
    77
#else  //METAMOD_PLAPI_VERSION
ds@1952
    78
		"extensions/auto.1.ep1/%s",
ds@1952
    79
#endif //METAMOD_PLAPI_VERSION
ds@1952
    80
		filename);
ds@1952
    81
ds@1952
    82
	/* Try the "normal" version */
ds@1952
    83
	if (!g_LibSys.IsPathFile(path))
ds@1952
    84
	{
ds@1952
    85
		g_SourceMod.BuildPath(Path_SM, path, PLATFORM_MAX_PATH, "extensions/%s", filename);
ds@1952
    86
	}
ds@1952
    87
ds@1952
    88
	Initialize(filename, path);
ds@1952
    89
}
ds@1952
    90
ds@1952
    91
bool CRemoteExtension::Load(char *error, size_t maxlength)
ds@1952
    92
{
ds@1952
    93
	if (!PerformAPICheck(error, maxlength))
ds@1952
    94
	{
ds@1952
    95
		m_pAPI = NULL;
ds@1952
    96
		return false;
ds@1952
    97
	}
ds@1952
    98
ds@1952
    99
	if (!CExtension::Load(error, maxlength))
ds@1952
   100
	{
ds@1952
   101
		m_pAPI = NULL;
ds@1952
   102
		return false;
ds@1952
   103
	}
ds@1952
   104
ds@1952
   105
	return true;
ds@1952
   106
}
ds@1952
   107
ds@1952
   108
bool CLocalExtension::Load(char *error, size_t maxlength)
ds@1952
   109
{
ds@1952
   110
	m_pLib = g_LibSys.OpenLibrary(m_Path.c_str(), error, maxlength);
ds@1952
   111
ds@1952
   112
	if (m_pLib == NULL)
ds@1952
   113
	{
ds@1952
   114
		return false;
ds@1952
   115
	}
ds@1952
   116
ds@1952
   117
	typedef IExtensionInterface *(*GETAPI)();
ds@1952
   118
	GETAPI pfnGetAPI = NULL;
ds@1952
   119
ds@1952
   120
	if ((pfnGetAPI=(GETAPI)m_pLib->GetSymbolAddress("GetSMExtAPI")) == NULL)
ds@1952
   121
	{
ds@1952
   122
		m_pLib->CloseLibrary();
ds@1952
   123
		m_pLib = NULL;
ds@1952
   124
		snprintf(error, maxlength, "Unable to find extension entry point");
ds@1952
   125
		return false;
ds@1952
   126
	}
ds@1952
   127
ds@1952
   128
	m_pAPI = pfnGetAPI();
ds@1952
   129
ds@1952
   130
	/* Check pointer and version */
ds@1952
   131
	if (!PerformAPICheck(error, maxlength))
ds@1952
   132
	{
ds@1952
   133
		m_pLib->CloseLibrary();
ds@1952
   134
		m_pLib = NULL;
ds@1952
   135
		m_pAPI = NULL;
ds@1952
   136
		return false;
ds@1952
   137
	}
ds@1952
   138
ds@1952
   139
	/* Load as MM:S */
ds@1952
   140
	if (m_pAPI->IsMetamodExtension())
ds@1952
   141
	{
ds@1952
   142
		bool already;
ds@1952
   143
		m_PlId = g_pMMPlugins->Load(m_Path.c_str(), g_PLID, already, error, maxlength);
ds@1952
   144
	}
ds@1952
   145
ds@1952
   146
	if (!CExtension::Load(error, maxlength))
ds@1952
   147
	{
ds@1952
   148
		if (m_pAPI->IsMetamodExtension())
ds@1952
   149
		{
ds@1952
   150
			if (m_PlId)
ds@1952
   151
			{
ds@1952
   152
				char dummy[255];
ds@1952
   153
				g_pMMPlugins->Unload(m_PlId, true, dummy, sizeof(dummy));
ds@1952
   154
				m_PlId = 0;
ds@1952
   155
			}
ds@1952
   156
		}
ds@1952
   157
		m_pLib->CloseLibrary();
ds@1952
   158
		m_pLib = NULL;
ds@1952
   159
		m_pAPI = NULL;
ds@1952
   160
		return false;
ds@1952
   161
	}
ds@1952
   162
ds@1952
   163
	return true;
ds@1952
   164
}
ds@1952
   165
ds@1952
   166
void CRemoteExtension::Unload()
ds@1952
   167
{
ds@1952
   168
}
ds@1952
   169
ds@1952
   170
void CLocalExtension::Unload()
ds@1952
   171
{
ds@1952
   172
	if (m_pAPI != NULL && m_PlId)
ds@1952
   173
	{
ds@1952
   174
		char temp_buffer[255];
ds@1952
   175
		g_pMMPlugins->Unload(m_PlId, true, temp_buffer, sizeof(temp_buffer));
ds@1952
   176
		m_PlId = 0;
ds@1952
   177
	}
ds@1952
   178
ds@1952
   179
	if (m_pLib != NULL)
ds@1952
   180
	{
ds@1952
   181
		m_pLib->CloseLibrary();
ds@1952
   182
		m_pLib = NULL;
ds@1952
   183
	}
ds@1952
   184
}
ds@1952
   185
ds@1952
   186
bool CRemoteExtension::IsExternal()
ds@1952
   187
{
ds@1952
   188
	return true;
ds@1952
   189
}
ds@1952
   190
ds@1952
   191
bool CLocalExtension::IsExternal()
ds@1952
   192
{
ds@1952
   193
	return false;
ds@1952
   194
}
ds@1952
   195
ds@1952
   196
CExtension::~CExtension()
ds@1952
   197
{
ds@1952
   198
	DestroyIdentity();
ds@1952
   199
}
ds@1952
   200
ds@1952
   201
bool CExtension::PerformAPICheck(char *error, size_t maxlength)
ds@1952
   202
{
ds@1952
   203
	if (!m_pAPI)
ds@1952
   204
	{
ds@1952
   205
		snprintf(error, maxlength, "No IExtensionInterface instance provided");
ds@1952
   206
		return false;
ds@1952
   207
	}
ds@1952
   208
	
ds@1952
   209
	if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
ds@1952
   210
	{
ds@1952
   211
		snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
ds@1952
   212
		return false;
ds@1952
   213
	}
ds@1952
   214
ds@1952
   215
	return true;
ds@1952
   216
}
ds@1952
   217
ds@1952
   218
bool CExtension::Load(char *error, size_t maxlength)
ds@1952
   219
{
ds@1952
   220
	CreateIdentity();
ds@1952
   221
	if (!m_pAPI->OnExtensionLoad(this, &g_ShareSys, error, maxlength, !g_SourceMod.IsMapLoading()))
ds@1952
   222
	{
ds@1952
   223
		DestroyIdentity();
ds@1952
   224
		return false;
ds@1952
   225
	}
ds@1952
   226
	else
ds@1952
   227
	{
ds@1952
   228
		/* Check if we're past load time */
ds@1952
   229
		if (!g_SourceMod.IsMapLoading())
ds@1952
   230
		{
ds@1952
   231
			m_pAPI->OnExtensionsAllLoaded();
ds@1952
   232
		}
ds@1952
   233
	}
ds@1952
   234
ds@1952
   235
	return true;
ds@1952
   236
}
ds@1952
   237
ds@1952
   238
void CExtension::CreateIdentity()
ds@1952
   239
{
ds@1952
   240
	if (m_pIdentToken != NULL)
ds@1952
   241
	{
ds@1952
   242
		return;
ds@1952
   243
	}
ds@1952
   244
ds@1952
   245
	m_pIdentToken = g_ShareSys.CreateIdentity(g_ExtType, this);
ds@1952
   246
}
ds@1952
   247
ds@1952
   248
void CExtension::DestroyIdentity()
ds@1952
   249
{
ds@1952
   250
	if (m_pIdentToken == NULL)
ds@1952
   251
	{
ds@1952
   252
		return;
ds@1952
   253
	}
ds@1952
   254
ds@1952
   255
	g_ShareSys.DestroyIdentity(m_pIdentToken);
ds@1952
   256
	m_pIdentToken = NULL;
ds@1952
   257
}
ds@1952
   258
ds@1952
   259
void CExtension::MarkAllLoaded()
ds@1952
   260
{
ds@1952
   261
	assert(m_bFullyLoaded == false);
ds@1952
   262
	if (!m_bFullyLoaded)
ds@1952
   263
	{
ds@1952
   264
		m_bFullyLoaded = true;
ds@1952
   265
		m_pAPI->OnExtensionsAllLoaded();
ds@1952
   266
	}
ds@1952
   267
}
ds@1952
   268
ds@1952
   269
void CExtension::AddPlugin(IPlugin *pPlugin)
ds@1952
   270
{
ds@1952
   271
	/* Unfortunately we have to do this :( */
ds@1952
   272
	if (m_Plugins.find(pPlugin) != m_Plugins.end())
ds@1952
   273
	{
ds@1952
   274
		m_Plugins.push_back(pPlugin);
ds@1952
   275
	}
ds@1952
   276
}
ds@1952
   277
ds@1952
   278
void CExtension::RemovePlugin(IPlugin *pPlugin)
ds@1952
   279
{
ds@1952
   280
	m_Plugins.remove(pPlugin);
ds@1952
   281
ds@1952
   282
	List<WeakNative>::iterator iter = m_WeakNatives.begin();
ds@1952
   283
	while (iter != m_WeakNatives.end())
ds@1952
   284
	{
ds@1952
   285
		if ((*iter).pl == pPlugin)
ds@1952
   286
		{
ds@1952
   287
			iter = m_WeakNatives.erase(iter);
ds@1952
   288
		}
ds@1952
   289
		else
ds@1952
   290
		{
ds@1952
   291
			iter++;
ds@1952
   292
		}
ds@1952
   293
	}
ds@1952
   294
ds@1952
   295
	iter = m_ReplacedNatives.begin();
ds@1952
   296
	while (iter != m_ReplacedNatives.end())
ds@1952
   297
	{
ds@1952
   298
		if ((*iter).pl == pPlugin)
ds@1952
   299
		{
ds@1952
   300
			iter = m_ReplacedNatives.erase(iter);
ds@1952
   301
		}
ds@1952
   302
		else
ds@1952
   303
		{
ds@1952
   304
			iter++;
ds@1952
   305
		}
ds@1952
   306
	}
ds@1952
   307
}
ds@1952
   308
ds@1952
   309
void CExtension::SetError(const char *error)
ds@1952
   310
{
ds@1952
   311
	m_Error.assign(error);
ds@1952
   312
}
ds@1952
   313
ds@1952
   314
IExtensionInterface *CExtension::GetAPI()
ds@1952
   315
{
ds@1952
   316
	return m_pAPI;
ds@1952
   317
}
ds@1952
   318
ds@1952
   319
const char *CExtension::GetFilename()
ds@1952
   320
{
ds@1952
   321
	return m_File.c_str();
ds@1952
   322
}
ds@1952
   323
ds@1952
   324
IdentityToken_t *CExtension::GetIdentity()
ds@1952
   325
{
ds@1952
   326
	return m_pIdentToken;
ds@1952
   327
}
ds@1952
   328
ds@1952
   329
bool CRemoteExtension::IsLoaded()
ds@1952
   330
{
ds@1952
   331
	return (m_pAPI != NULL);
ds@1952
   332
}
ds@1952
   333
ds@1952
   334
bool CLocalExtension::IsLoaded()
ds@1952
   335
{
ds@1952
   336
	return (m_pLib != NULL);
ds@1952
   337
}
ds@1952
   338
ds@1952
   339
void CExtension::AddDependency(IfaceInfo *pInfo)
ds@1952
   340
{
ds@1952
   341
	if (m_Deps.find(*pInfo) == m_Deps.end())
ds@1952
   342
	{
ds@1952
   343
		m_Deps.push_back(*pInfo);
ds@1952
   344
	}
ds@1952
   345
}
ds@1952
   346
ds@1952
   347
bool operator ==(const IfaceInfo &i1, const IfaceInfo &i2)
ds@1952
   348
{
ds@1952
   349
	return (i1.iface == i2.iface) && (i1.owner == i2.owner);
ds@1952
   350
}
ds@1952
   351
ds@1952
   352
void CExtension::AddChildDependent(CExtension *pOther, SMInterface *iface)
ds@1952
   353
{
ds@1952
   354
	IfaceInfo info;
ds@1952
   355
	info.iface = iface;
ds@1952
   356
	info.owner = pOther;
ds@1952
   357
ds@1952
   358
	List<IfaceInfo>::iterator iter;
ds@1952
   359
	for (iter = m_ChildDeps.begin();
ds@1952
   360
		 iter != m_ChildDeps.end();
ds@1952
   361
		 iter++)
ds@1952
   362
	{
ds@1952
   363
		IfaceInfo &other = (*iter);
ds@1952
   364
		if (other == info)
ds@1952
   365
		{
ds@1952
   366
			return;
ds@1952
   367
		}
ds@1952
   368
	}
ds@1952
   369
ds@1952
   370
	m_ChildDeps.push_back(info);
ds@1952
   371
}
ds@1952
   372
ds@1952
   373
ITERATOR *CExtension::FindFirstDependency(IExtension **pOwner, SMInterface **pInterface)
ds@1952
   374
{
ds@1952
   375
	List<IfaceInfo>::iterator iter = m_Deps.begin();
ds@1952
   376
ds@1952
   377
	if (iter == m_Deps.end())
ds@1952
   378
	{
ds@1952
   379
		return NULL;
ds@1952
   380
	}
ds@1952
   381
ds@1952
   382
	if (pOwner)
ds@1952
   383
	{
ds@1952
   384
		*pOwner = (*iter).owner;
ds@1952
   385
	}
ds@1952
   386
	if (pInterface)
ds@1952
   387
	{
ds@1952
   388
		*pInterface = (*iter).iface;
ds@1952
   389
	}
ds@1952
   390
ds@1952
   391
	List<IfaceInfo>::iterator *pIter = new List<IfaceInfo>::iterator(iter);
ds@1952
   392
ds@1952
   393
	return (ITERATOR *)pIter;
ds@1952
   394
}
ds@1952
   395
ds@1952
   396
bool CExtension::FindNextDependency(ITERATOR *iter, IExtension **pOwner, SMInterface **pInterface)
ds@1952
   397
{
ds@1952
   398
	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
ds@1952
   399
	List<IfaceInfo>::iterator _iter;
ds@1952
   400
ds@1952
   401
	if (_iter == m_Deps.end())
ds@1952
   402
	{
ds@1952
   403
		return false;
ds@1952
   404
	}
ds@1952
   405
ds@1952
   406
	_iter++;
ds@1952
   407
ds@1952
   408
	if (pOwner)
ds@1952
   409
	{
ds@1952
   410
		*pOwner = (*_iter).owner;
ds@1952
   411
	}
ds@1952
   412
	if (pInterface)
ds@1952
   413
	{
ds@1952
   414
		*pInterface = (*_iter).iface;
ds@1952
   415
	}
ds@1952
   416
ds@1952
   417
	*pIter = _iter;
ds@1952
   418
ds@1952
   419
	if (_iter == m_Deps.end())
ds@1952
   420
	{
ds@1952
   421
		return false;
ds@1952
   422
	}
ds@1952
   423
ds@1952
   424
	return true;
ds@1952
   425
}
ds@1952
   426
ds@1952
   427
void CExtension::FreeDependencyIterator(ITERATOR *iter)
ds@1952
   428
{
ds@1952
   429
	List<IfaceInfo>::iterator *pIter = (List<IfaceInfo>::iterator *)iter;
ds@1952
   430
ds@1952
   431
	delete pIter;
ds@1952
   432
}
ds@1952
   433
ds@1952
   434
void CExtension::AddInterface(SMInterface *pInterface)
ds@1952
   435
{
ds@1952
   436
	m_Interfaces.push_back(pInterface);
ds@1952
   437
}
ds@1952
   438
ds@1952
   439
bool CExtension::IsRunning(char *error, size_t maxlength)
ds@1952
   440
{
ds@1952
   441
	if (!IsLoaded())
ds@1952
   442
	{
ds@1952
   443
		if (error)
ds@1952
   444
		{
ds@1952
   445
			snprintf(error, maxlength, "%s", m_Error.c_str());
ds@1952
   446
		}
ds@1952
   447
		return false;
ds@1952
   448
	}
ds@1952
   449
ds@1952
   450
	return m_pAPI->QueryRunning(error, maxlength);
ds@1952
   451
}
ds@1952
   452
ds@1952
   453
void CExtension::AddLibrary(const char *library)
ds@1952
   454
{
ds@1952
   455
	m_Libraries.push_back(library);
ds@1952
   456
}
ds@1952
   457
ds@1952
   458
/*********************
ds@1952
   459
 * EXTENSION MANAGER *
ds@1952
   460
 *********************/
ds@1952
   461
ds@1952
   462
void CExtensionManager::OnSourceModAllInitialized()
ds@1952
   463
{
ds@1952
   464
	g_ExtType = g_ShareSys.CreateIdentType("EXTENSION");
ds@1952
   465
	g_PluginSys.AddPluginsListener(this);
ds@1952
   466
	g_RootMenu.AddRootConsoleCommand("exts", "Manage extensions", this);
ds@1952
   467
	g_ShareSys.AddInterface(NULL, this);
ds@1952
   468
}
ds@1952
   469
ds@1952
   470
void CExtensionManager::OnSourceModShutdown()
ds@1952
   471
{
ds@1952
   472
	g_RootMenu.RemoveRootConsoleCommand("exts", this);
ds@1952
   473
	g_ShareSys.DestroyIdentType(g_ExtType);
ds@1952
   474
}
ds@1952
   475
ds@1952
   476
void CExtensionManager::Shutdown()
ds@1952
   477
{
ds@1952
   478
	List<CExtension *>::iterator iter;
ds@1952
   479
ds@1952
   480
	while ((iter = m_Libs.begin()) != m_Libs.end())
ds@1952
   481
	{
ds@1952
   482
		UnloadExtension((*iter));
ds@1952
   483
	}
ds@1952
   484
}
ds@1952
   485
ds@1952
   486
void CExtensionManager::TryAutoload()
ds@1952
   487
{
ds@1952
   488
	char path[PLATFORM_MAX_PATH];
ds@1952
   489
ds@1952
   490
	g_SourceMod.BuildPath(Path_SM, path, sizeof(path), "extensions");
ds@1952
   491
ds@1952
   492
	IDirectory *pDir = g_LibSys.OpenDirectory(path);
ds@1952
   493
	if (!pDir)
ds@1952
   494
	{
ds@1952
   495
		return;
ds@1952
   496
	}
ds@1952
   497
ds@1952
   498
	const char *lfile;
ds@1952
   499
	size_t len;
ds@1952
   500
	while (pDir->MoreFiles())
ds@1952
   501
	{
ds@1952
   502
		if (pDir->IsEntryDirectory())
ds@1952
   503
		{
ds@1952
   504
			pDir->NextEntry();
ds@1952
   505
			continue;
ds@1952
   506
		}
ds@1952
   507
ds@1952
   508
		lfile = pDir->GetEntryName();
ds@1952
   509
		len = strlen(lfile);
ds@1952
   510
		if (len <= 9) /* size of ".autoload" */
ds@1952
   511
		{
ds@1952
   512
			pDir->NextEntry();
ds@1952
   513
			continue;
ds@1952
   514
		}
ds@1952
   515
ds@1952
   516
		if (strcmp(&lfile[len - 9], ".autoload") != 0)
ds@1952
   517
		{
ds@1952
   518
			pDir->NextEntry();
ds@1952
   519
			continue;
ds@1952
   520
		}
ds@1952
   521
ds@1952
   522
		char file[PLATFORM_MAX_PATH];
ds@1952
   523
		len = UTIL_Format(file, sizeof(file), "%s", lfile);
ds@1952
   524
		strcpy(&file[len - 9], ".ext." PLATFORM_LIB_EXT);
ds@1952
   525
ds@1952
   526
		LoadAutoExtension(file);
ds@1952
   527
		
ds@1952
   528
		pDir->NextEntry();
ds@1952
   529
	}
ds@1952
   530
}
ds@1952
   531
ds@1952
   532
IExtension *CExtensionManager::LoadAutoExtension(const char *path)
ds@1952
   533
{
ds@1952
   534
	if (!strstr(path, "." PLATFORM_LIB_EXT))
ds@1952
   535
	{
ds@1952
   536
		char newpath[PLATFORM_MAX_PATH];
ds@1952
   537
		g_LibSys.PathFormat(newpath, sizeof(newpath), "%s.%s", path, PLATFORM_LIB_EXT);
ds@1952
   538
		return LoadAutoExtension(newpath);
ds@1952
   539
	}
ds@1952
   540
ds@1952
   541
	IExtension *pAlready;
ds@1952
   542
	if ((pAlready=FindExtensionByFile(path)) != NULL)
ds@1952
   543
	{
ds@1952
   544
		return pAlready;
ds@1952
   545
	}
ds@1952
   546
ds@1952
   547
	char error[256];
ds@1952
   548
	CExtension *p = new CLocalExtension(path);
ds@1952
   549
ds@1952
   550
	/* We put us in the list beforehand so extensions that check for each other
ds@1952
   551
	 * won't recursively load each other.
ds@1952
   552
	 */
ds@1952
   553
	m_Libs.push_back(p);
ds@1952
   554
ds@1952
   555
	if (!p->Load(error, sizeof(error)) || !p->IsLoaded())
ds@1952
   556
	{
ds@1952
   557
		g_Logger.LogError("[SM] Unable to load extension \"%s\": %s", path, error);
ds@1952
   558
		p->SetError(error);
ds@1952
   559
	}
ds@1952
   560
ds@1952
   561
	return p;
ds@1952
   562
}
ds@1952
   563
ds@1952
   564
IExtension *CExtensionManager::FindExtensionByFile(const char *file)
ds@1952
   565
{
ds@1952
   566
	List<CExtension *>::iterator iter;
ds@1952
   567
	CExtension *pExt;
ds@1952
   568
ds@1952
   569
	/* Chomp off the path */
ds@1952
   570
	char lookup[PLATFORM_MAX_PATH];
ds@1952
   571
	g_LibSys.GetFileFromPath(lookup, sizeof(lookup), file);
ds@1952
   572
ds@1952
   573
	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
ds@1952
   574
	{
ds@1952
   575
		pExt = (*iter);
ds@1952
   576
		char short_file[PLATFORM_MAX_PATH];
ds@1952
   577
		g_LibSys.GetFileFromPath(short_file, sizeof(short_file), pExt->GetFilename());
ds@1952
   578
		if (strcmp(lookup, short_file) == 0)
ds@1952
   579
		{
ds@1952
   580
			return pExt;
ds@1952
   581
		}
ds@1952
   582
	}
ds@1952
   583
ds@1952
   584
	/* If we got no results, test if there was a platform extension.
ds@1952
   585
	 * If not, add one.
ds@1952
   586
	 */
ds@1952
   587
	if (!strstr(file, "." PLATFORM_LIB_EXT))
ds@1952
   588
	{
ds@1952
   589
		char path[PLATFORM_MAX_PATH];
ds@1952
   590
		UTIL_Format(path, sizeof(path), "%s.%s", file, PLATFORM_LIB_EXT);
ds@1952
   591
		return FindExtensionByFile(path);
ds@1952
   592
	}
ds@1952
   593
ds@1952
   594
	return NULL;
ds@1952
   595
}
ds@1952
   596
ds@1952
   597
IExtension *CExtensionManager::FindExtensionByName(const char *ext)
ds@1952
   598
{
ds@1952
   599
	List<CExtension *>::iterator iter;
ds@1952
   600
	CExtension *pExt;
ds@1952
   601
	IExtensionInterface *pAPI;
ds@1952
   602
	const char *name;
ds@1952
   603
ds@1952
   604
	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
ds@1952
   605
	{
ds@1952
   606
		pExt = (*iter);
ds@1952
   607
		if (!pExt->IsLoaded())
ds@1952
   608
		{
ds@1952
   609
			continue;
ds@1952
   610
		}
ds@1952
   611
		if ((pAPI = pExt->GetAPI()) == NULL)
ds@1952
   612
		{
ds@1952
   613
			continue;
ds@1952
   614
		}
ds@1952
   615
		name = pAPI->GetExtensionName();
ds@1952
   616
		if (!name)
ds@1952
   617
		{
ds@1952
   618
			continue;
ds@1952
   619
		}
ds@1952
   620
		if (strcmp(name, ext) == 0)
ds@1952
   621
		{
ds@1952
   622
			return pExt;
ds@1952
   623
		}
ds@1952
   624
	}
ds@1952
   625
ds@1952
   626
	return NULL;
ds@1952
   627
}
ds@1952
   628
ds@1952
   629
IExtension *CExtensionManager::LoadExtension(const char *file, char *error, size_t maxlength)
ds@1952
   630
{
ds@1952
   631
	IExtension *pAlready;
ds@1952
   632
	if ((pAlready=FindExtensionByFile(file)) != NULL)
ds@1952
   633
	{
ds@1952
   634
		return pAlready;
ds@1952
   635
	}
ds@1952
   636
ds@1952
   637
	CExtension *pExt = new CLocalExtension(file);
ds@1952
   638
ds@1952
   639
	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
ds@1952
   640
	{
ds@1952
   641
		pExt->Unload();
ds@1952
   642
		delete pExt;
ds@1952
   643
		return NULL;
ds@1952
   644
	}
ds@1952
   645
ds@1952
   646
	/* :TODO: do QueryRunning check if the map is loaded */
ds@1952
   647
ds@1952
   648
	m_Libs.push_back(pExt);
ds@1952
   649
ds@1952
   650
	return pExt;
ds@1952
   651
}
ds@1952
   652
ds@1952
   653
void CExtensionManager::BindDependency(IExtension *pRequester, IfaceInfo *pInfo)
ds@1952
   654
{
ds@1952
   655
	CExtension *pExt = (CExtension *)pRequester;
ds@1952
   656
	CExtension *pOwner = (CExtension *)pInfo->owner;
ds@1952
   657
ds@1952
   658
	pExt->AddDependency(pInfo);
ds@1952
   659
ds@1952
   660
	IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
   661
	if (pAPI && !pAPI->QueryInterfaceDrop(pInfo->iface))
ds@1952
   662
	{
ds@1952
   663
		pOwner->AddChildDependent(pExt, pInfo->iface);
ds@1952
   664
	}
ds@1952
   665
}
ds@1952
   666
ds@1952
   667
void CExtensionManager::AddInterface(IExtension *pOwner, SMInterface *pInterface)
ds@1952
   668
{
ds@1952
   669
	CExtension *pExt = (CExtension *)pOwner;
ds@1952
   670
ds@1952
   671
	pExt->AddInterface(pInterface);
ds@1952
   672
}
ds@1952
   673
ds@1952
   674
void CExtensionManager::BindChildPlugin(IExtension *pParent, IPlugin *pPlugin)
ds@1952
   675
{
ds@1952
   676
	CExtension *pExt = (CExtension *)pParent;
ds@1952
   677
ds@1952
   678
	pExt->AddPlugin(pPlugin);
ds@1952
   679
}
ds@1952
   680
ds@1952
   681
void CExtensionManager::OnPluginDestroyed(IPlugin *plugin)
ds@1952
   682
{
ds@1952
   683
	List<CExtension *>::iterator iter;
ds@1952
   684
ds@1952
   685
	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
ds@1952
   686
	{
ds@1952
   687
		(*iter)->RemovePlugin(plugin);
ds@1952
   688
	}
ds@1952
   689
}
ds@1952
   690
ds@1952
   691
CExtension *CExtensionManager::FindByOrder(unsigned int num)
ds@1952
   692
{
ds@1952
   693
	if (num < 1 || num > m_Libs.size())
ds@1952
   694
	{
ds@1952
   695
		return NULL;
ds@1952
   696
	}
ds@1952
   697
ds@1952
   698
	List<CExtension *>::iterator iter = m_Libs.begin();
ds@1952
   699
ds@1952
   700
	while (iter != m_Libs.end())
ds@1952
   701
	{
ds@1952
   702
		if (--num == 0)
ds@1952
   703
		{
ds@1952
   704
			return (*iter);
ds@1952
   705
		}
ds@1952
   706
		iter++;
ds@1952
   707
	}
ds@1952
   708
ds@1952
   709
	return NULL;
ds@1952
   710
}
ds@1952
   711
ds@1952
   712
void CExtensionManager::BindAllNativesToPlugin(IPlugin *pPlugin)
ds@1952
   713
{
ds@1952
   714
	IPluginContext *pContext = pPlugin->GetBaseContext();
ds@1952
   715
ds@1952
   716
	List<CExtension *> exts;
ds@1952
   717
ds@1952
   718
	uint32_t natives = pContext->GetNativesNum();
ds@1952
   719
	sp_native_t *native;
ds@1952
   720
	sm_extnative_t *x_native;
ds@1952
   721
	sm_repnative_t *r_native;
ds@1952
   722
	for (uint32_t i=0; i<natives; i++)
ds@1952
   723
	{
ds@1952
   724
		/* Make sure the native is valid */
ds@1952
   725
		if (pContext->GetNativeByIndex(i, &native) != SP_ERROR_NONE)
ds@1952
   726
		{
ds@1952
   727
			continue;
ds@1952
   728
		}
ds@1952
   729
ds@1952
   730
		/* Make sure the native is not already bound */
ds@1952
   731
		if (native->status == SP_NATIVE_BOUND)
ds@1952
   732
		{
ds@1952
   733
			/* If it is bound, see if there is a replacement. */
ds@1952
   734
			if ((r_native = m_RepNatives.retrieve(native->name)) == NULL)
ds@1952
   735
			{
ds@1952
   736
				continue;
ds@1952
   737
			}
ds@1952
   738
ds@1952
   739
			/* Rewrite the address.  Whee! */
ds@1952
   740
			native->pfn = r_native->info.func;
ds@1952
   741
ds@1952
   742
			/* Make sure this will unload safely */
ds@1952
   743
			WeakNative wn((CPlugin *)pPlugin, i);
ds@1952
   744
			r_native->owner->m_ReplacedNatives.push_back(wn);
ds@1952
   745
ds@1952
   746
			continue;
ds@1952
   747
		}
ds@1952
   748
ds@1952
   749
		/* See if we've got this native in our cache */
ds@1952
   750
		if ((x_native = m_ExtNatives.retrieve(native->name)) == NULL)
ds@1952
   751
		{
ds@1952
   752
			continue;
ds@1952
   753
		}
ds@1952
   754
		/* Try and bind it */
ds@1952
   755
		if (pContext->BindNativeToIndex(i, x_native->info->func) != SP_ERROR_NONE)
ds@1952
   756
		{
ds@1952
   757
			continue;
ds@1952
   758
		}
ds@1952
   759
		/* See if it's optional */
ds@1952
   760
		if (native->flags & SP_NTVFLAG_OPTIONAL)
ds@1952
   761
		{
ds@1952
   762
			WeakNative wkn = WeakNative((CPlugin *)pPlugin, i);
ds@1952
   763
			x_native->owner->m_WeakNatives.push_back(wkn);
ds@1952
   764
		}
ds@1952
   765
		else if (exts.find(x_native->owner) == exts.end())
ds@1952
   766
		{
ds@1952
   767
			exts.push_back(x_native->owner);
ds@1952
   768
		}
ds@1952
   769
	}
ds@1952
   770
ds@1952
   771
	List<CExtension *>::iterator iter;
ds@1952
   772
	for (iter = exts.begin(); iter != exts.end(); iter++)
ds@1952
   773
	{
ds@1952
   774
		CExtension *pExt = (*iter);
ds@1952
   775
		if (pExt->m_Plugins.find(pPlugin) != pExt->m_Plugins.end())
ds@1952
   776
		{
ds@1952
   777
			continue;
ds@1952
   778
		}
ds@1952
   779
		pExt->m_Plugins.push_back(pPlugin);
ds@1952
   780
	}
ds@1952
   781
}
ds@1952
   782
ds@1952
   783
bool CExtensionManager::UnloadExtension(IExtension *_pExt)
ds@1952
   784
{
ds@1952
   785
	if (!_pExt)
ds@1952
   786
	{
ds@1952
   787
		return false;
ds@1952
   788
	}
ds@1952
   789
ds@1952
   790
	CExtension *pExt = (CExtension *)_pExt;
ds@1952
   791
ds@1952
   792
	if (m_Libs.find(pExt) == m_Libs.end())
ds@1952
   793
	{
ds@1952
   794
		return false;
ds@1952
   795
	}
ds@1952
   796
ds@1952
   797
	/* First remove us from internal lists */
ds@1952
   798
	g_ShareSys.RemoveInterfaces(_pExt);
ds@1952
   799
	m_Libs.remove(pExt);
ds@1952
   800
ds@1952
   801
	List<CExtension *> UnloadQueue;
ds@1952
   802
ds@1952
   803
	/* Handle dependencies */
ds@1952
   804
	if (pExt->IsLoaded())
ds@1952
   805
	{
ds@1952
   806
		/* Unload any dependent plugins */
ds@1952
   807
		List<IPlugin *>::iterator p_iter = pExt->m_Plugins.begin();
ds@1952
   808
		while (p_iter != pExt->m_Plugins.end())
ds@1952
   809
		{
ds@1952
   810
			/* We have to manually unlink ourselves here, since we're no longer being managed */
ds@1952
   811
			g_PluginSys.UnloadPlugin((*p_iter));
ds@1952
   812
			p_iter = pExt->m_Plugins.erase(p_iter);
ds@1952
   813
		}
ds@1952
   814
ds@1952
   815
		List<String>::iterator s_iter;
ds@1952
   816
		for (s_iter = pExt->m_Libraries.begin();
ds@1952
   817
			 s_iter != pExt->m_Libraries.end();
ds@1952
   818
			 s_iter++)
ds@1952
   819
		{
ds@1952
   820
			g_PluginSys.OnLibraryAction((*s_iter).c_str(), false, true);
ds@1952
   821
		}
ds@1952
   822
ds@1952
   823
		/* Unbind weak natives */
ds@1952
   824
		List<WeakNative>::iterator wkn_iter;
ds@1952
   825
		for (wkn_iter=pExt->m_WeakNatives.begin(); wkn_iter!=pExt->m_WeakNatives.end(); wkn_iter++)
ds@1952
   826
		{
ds@1952
   827
			WeakNative & wkn = (*wkn_iter);
ds@1952
   828
			sp_context_t *ctx = wkn.pl->GetContext();
ds@1952
   829
			ctx->natives[wkn.idx].status = SP_NATIVE_UNBOUND;
ds@1952
   830
		}
ds@1952
   831
ds@1952
   832
		/* Unbind replacement natives, link them back to their originals */
ds@1952
   833
		for (wkn_iter = pExt->m_ReplacedNatives.begin();
ds@1952
   834
			 wkn_iter != pExt->m_ReplacedNatives.end();
ds@1952
   835
			 wkn_iter++)
ds@1952
   836
		{
ds@1952
   837
			WeakNative & wkn = (*wkn_iter);
ds@1952
   838
			sp_context_t *ctx = wkn.pl->GetContext();
ds@1952
   839
			sm_repnative_t *r_native = m_RepNatives.retrieve(ctx->natives[wkn.idx].name);
ds@1952
   840
			if (r_native == NULL || ctx->natives[wkn.idx].pfn != r_native->info.func)
ds@1952
   841
			{
ds@1952
   842
				continue;
ds@1952
   843
			}
ds@1952
   844
			ctx->natives[wkn.idx].pfn = r_native->original;
ds@1952
   845
		}
ds@1952
   846
ds@1952
   847
		/* Notify and/or unload all dependencies */
ds@1952
   848
		List<CExtension *>::iterator c_iter;
ds@1952
   849
		CExtension *pDep;
ds@1952
   850
		IExtensionInterface *pAPI;
ds@1952
   851
		for (c_iter = m_Libs.begin(); c_iter != m_Libs.end(); c_iter++)
ds@1952
   852
		{
ds@1952
   853
			pDep = (*c_iter);
ds@1952
   854
			if ((pAPI=pDep->GetAPI()) == NULL)
ds@1952
   855
			{
ds@1952
   856
				continue;
ds@1952
   857
			}
ds@1952
   858
			if (pDep == pExt)
ds@1952
   859
			{
ds@1952
   860
				continue;
ds@1952
   861
			}
ds@1952
   862
			/* Now, get its dependency list */
ds@1952
   863
			bool dropped = false;
ds@1952
   864
			List<IfaceInfo>::iterator i_iter = pDep->m_Deps.begin();
ds@1952
   865
			while (i_iter != pDep->m_Deps.end())
ds@1952
   866
			{
ds@1952
   867
				if ((*i_iter).owner == _pExt)
ds@1952
   868
				{
ds@1952
   869
					if (!pAPI->QueryInterfaceDrop((*i_iter).iface))
ds@1952
   870
					{
ds@1952
   871
						if (!dropped)
ds@1952
   872
						{
ds@1952
   873
							dropped = true;
ds@1952
   874
							UnloadQueue.push_back(pDep);
ds@1952
   875
						}
ds@1952
   876
					}
ds@1952
   877
					pAPI->NotifyInterfaceDrop((*i_iter).iface);
ds@1952
   878
					i_iter = pDep->m_Deps.erase(i_iter);
ds@1952
   879
				}
ds@1952
   880
				else
ds@1952
   881
				{
ds@1952
   882
					i_iter++;
ds@1952
   883
				}
ds@1952
   884
			}
ds@1952
   885
			/* Flush out any back references to this plugin */
ds@1952
   886
			i_iter = pDep->m_ChildDeps.begin();
ds@1952
   887
			while (i_iter != pDep->m_ChildDeps.end())
ds@1952
   888
			{
ds@1952
   889
				if ((*i_iter).owner == pExt)
ds@1952
   890
				{
ds@1952
   891
					i_iter = pDep->m_ChildDeps.erase(i_iter);
ds@1952
   892
				}
ds@1952
   893
				else
ds@1952
   894
				{
ds@1952
   895
					i_iter++;
ds@1952
   896
				}
ds@1952
   897
			}
ds@1952
   898
		}
ds@1952
   899
ds@1952
   900
		/* Unbind our natives from Core */
ds@1952
   901
		List<const sp_nativeinfo_t *>::iterator native_iter;
ds@1952
   902
		for (native_iter = pExt->m_Natives.begin();
ds@1952
   903
			 native_iter != pExt->m_Natives.end();
ds@1952
   904
			 native_iter++)
ds@1952
   905
		{
ds@1952
   906
			const sp_nativeinfo_t *natives = (*native_iter);
ds@1952
   907
			sm_extnative_t *x_native;
ds@1952
   908
			for (unsigned int n = 0;
ds@1952
   909
				 natives[n].name != NULL && natives[n].func != NULL;
ds@1952
   910
				 n++)
ds@1952
   911
			{
ds@1952
   912
				x_native = m_ExtNatives.retrieve(natives[n].name);
ds@1952
   913
				if (x_native && x_native->owner == pExt)
ds@1952
   914
				{
ds@1952
   915
					m_ExtNatives.remove(natives[n].name);
ds@1952
   916
				}
ds@1952
   917
			}
ds@1952
   918
		}
ds@1952
   919
ds@1952
   920
		/* Unbind our replacement natives */
ds@1952
   921
		List<sm_repnative_t>::iterator rep_iter = m_RepNativeList.begin();
ds@1952
   922
		while (rep_iter != m_RepNativeList.end())
ds@1952
   923
		{
ds@1952
   924
			sm_repnative_t & r_native = (*rep_iter);
ds@1952
   925
			if (r_native.owner == pExt)
ds@1952
   926
			{
ds@1952
   927
				m_RepNatives.remove(r_native.info.name);
ds@1952
   928
				rep_iter = m_RepNativeList.erase(rep_iter);
ds@1952
   929
			}
ds@1952
   930
			else
ds@1952
   931
			{
ds@1952
   932
				rep_iter++;
ds@1952
   933
			}
ds@1952
   934
		}
ds@1952
   935
ds@1952
   936
		/* Tell it to unload */
ds@1952
   937
		pAPI = pExt->GetAPI();
ds@1952
   938
		pAPI->OnExtensionUnload();
ds@1952
   939
	}
ds@1952
   940
ds@1952
   941
	IdentityToken_t *pIdentity;
ds@1952
   942
	if ((pIdentity = pExt->GetIdentity()) != NULL)
ds@1952
   943
	{
ds@1952
   944
		SMGlobalClass *glob = SMGlobalClass::head;
ds@1952
   945
		while (glob)
ds@1952
   946
		{
ds@1952
   947
			glob->OnSourceModIdentityDropped(pIdentity);
ds@1952
   948
			glob = glob->m_pGlobalClassNext;
ds@1952
   949
		}
ds@1952
   950
	}
ds@1952
   951
ds@1952
   952
	pExt->Unload();
ds@1952
   953
	delete pExt;
ds@1952
   954
ds@1952
   955
	List<CExtension *>::iterator iter;
ds@1952
   956
	for (iter=UnloadQueue.begin(); iter!=UnloadQueue.end(); iter++)
ds@1952
   957
	{
ds@1952
   958
		/* NOTE: This is safe because the unload function backs out of anything not present */
ds@1952
   959
		UnloadExtension((*iter));
ds@1952
   960
	}
ds@1952
   961
ds@1952
   962
	return true;
ds@1952
   963
}
ds@1952
   964
ds@1952
   965
void CExtensionManager::AddNatives(IExtension *pOwner, const sp_nativeinfo_t *natives)
ds@1952
   966
{
ds@1952
   967
	CExtension *pExt = (CExtension *)pOwner;
ds@1952
   968
ds@1952
   969
	pExt->m_Natives.push_back(natives);
ds@1952
   970
ds@1952
   971
	for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
ds@1952
   972
	{
ds@1952
   973
		if (m_ExtNatives.retrieve(natives[i].name) != NULL)
ds@1952
   974
		{
ds@1952
   975
			continue;
ds@1952
   976
		}
ds@1952
   977
		sm_extnative_t x_native;
ds@1952
   978
		x_native.info = &natives[i];
ds@1952
   979
		x_native.owner = pExt;
ds@1952
   980
		m_ExtNatives.insert(natives[i].name, x_native);
ds@1952
   981
	}
ds@1952
   982
}
ds@1952
   983
ds@1952
   984
void CExtensionManager::OverrideNatives(IExtension *myself, const sp_nativeinfo_t *natives)
ds@1952
   985
{
ds@1952
   986
	SPVM_NATIVE_FUNC orig;
ds@1952
   987
ds@1952
   988
	for (unsigned int i = 0; natives[i].func != NULL && natives[i].name != NULL; i++)
ds@1952
   989
	{
ds@1952
   990
		if ((orig = g_PluginSys.FindCoreNative(natives[i].name)) == NULL)
ds@1952
   991
		{
ds@1952
   992
			continue;
ds@1952
   993
		}
ds@1952
   994
		sm_repnative_t rep;
ds@1952
   995
		rep.info = natives[i];
ds@1952
   996
		rep.owner = (CExtension *)myself;
ds@1952
   997
		rep.original = orig;
ds@1952
   998
		m_RepNativeList.push_back(rep);
ds@1952
   999
		m_RepNatives.insert(natives[i].name, rep);
ds@1952
  1000
	}
ds@1952
  1001
}
ds@1952
  1002
ds@1952
  1003
void CExtensionManager::MarkAllLoaded()
ds@1952
  1004
{
ds@1952
  1005
	List<CExtension *>::iterator iter;
ds@1952
  1006
	CExtension *pExt;
ds@1952
  1007
ds@1952
  1008
	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
ds@1952
  1009
	{
ds@1952
  1010
		pExt = (*iter);
ds@1952
  1011
		if (!pExt->IsLoaded())
ds@1952
  1012
		{
ds@1952
  1013
			continue;
ds@1952
  1014
		}
ds@1952
  1015
		if (pExt->m_bFullyLoaded)
ds@1952
  1016
		{
ds@1952
  1017
			continue;
ds@1952
  1018
		}
ds@1952
  1019
		pExt->MarkAllLoaded();
ds@1952
  1020
	}
ds@1952
  1021
}
ds@1952
  1022
ds@1952
  1023
void CExtensionManager::AddDependency(IExtension *pSource, const char *file, bool required, bool autoload)
ds@1952
  1024
{
ds@1952
  1025
	/* This function doesn't really need to do anything now.  We make sure the 
ds@1952
  1026
	 * other extension is loaded, but handling of dependencies is really done 
ds@1952
  1027
	 * by the interface fetcher.
ds@1952
  1028
	 */
ds@1952
  1029
	if (required || autoload)
ds@1952
  1030
	{
ds@1952
  1031
		LoadAutoExtension(file);
ds@1952
  1032
	}
ds@1952
  1033
}
ds@1952
  1034
ds@1952
  1035
void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const CCommand &command)
ds@1952
  1036
{
ds@1952
  1037
	int argcount = command.ArgC();
ds@1952
  1038
	if (argcount >= 3)
ds@1952
  1039
	{
ds@1952
  1040
		const char *cmd = command.Arg(2);
ds@1952
  1041
		if (strcmp(cmd, "list") == 0)
ds@1952
  1042
		{
ds@1952
  1043
			List<CExtension *>::iterator iter;
ds@1952
  1044
			CExtension *pExt;
ds@1952
  1045
			unsigned int num = 1;
ds@1952
  1046
			switch (m_Libs.size())
ds@1952
  1047
			{
ds@1952
  1048
			case 1:
ds@1952
  1049
				{
ds@1952
  1050
					g_RootMenu.ConsolePrint("[SM] Displaying 1 extension:");
ds@1952
  1051
					break;
ds@1952
  1052
				}
ds@1952
  1053
			case 0:
ds@1952
  1054
				{
ds@1952
  1055
					g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
ds@1952
  1056
					break;
ds@1952
  1057
				}
ds@1952
  1058
			default:
ds@1952
  1059
				{
ds@1952
  1060
					g_RootMenu.ConsolePrint("[SM] Displaying %d extensions:", m_Libs.size());
ds@1952
  1061
					break;
ds@1952
  1062
				}
ds@1952
  1063
			}
ds@1952
  1064
			for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++,num++)
ds@1952
  1065
			{
ds@1952
  1066
				pExt = (*iter);
ds@1952
  1067
				if (pExt->IsLoaded())
ds@1952
  1068
				{
ds@1952
  1069
					char error[255];
ds@1952
  1070
					if (!pExt->IsRunning(error, sizeof(error)))
ds@1952
  1071
					{
ds@1952
  1072
						g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), error);
ds@1952
  1073
					}
ds@1952
  1074
					else
ds@1952
  1075
					{
ds@1952
  1076
						IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
  1077
						const char *name = pAPI->GetExtensionName();
ds@1952
  1078
						const char *version = pAPI->GetExtensionVerString();
ds@1952
  1079
						const char *descr = pAPI->GetExtensionDescription();
ds@1952
  1080
						g_RootMenu.ConsolePrint("[%02d] %s (%s): %s", num, name, version, descr);
ds@1952
  1081
					}
ds@1952
  1082
				} else {
ds@1952
  1083
					g_RootMenu.ConsolePrint("[%02d] <FAILED> file \"%s\": %s", num, pExt->GetFilename(), pExt->m_Error.c_str());
ds@1952
  1084
				}
ds@1952
  1085
			}
ds@1952
  1086
			return;
ds@1952
  1087
		}
ds@1952
  1088
		else if (strcmp(cmd, "info") == 0)
ds@1952
  1089
		{
ds@1952
  1090
			if (argcount < 4)
ds@1952
  1091
			{
ds@1952
  1092
				g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
ds@1952
  1093
				return;
ds@1952
  1094
			}
ds@1952
  1095
ds@1952
  1096
			const char *sId = command.Arg(3);
ds@1952
  1097
			unsigned int id = atoi(sId);
ds@1952
  1098
			if (id <= 0)
ds@1952
  1099
			{
ds@1952
  1100
				g_RootMenu.ConsolePrint("[SM] Usage: sm info <#>");
ds@1952
  1101
				return;
ds@1952
  1102
			}
ds@1952
  1103
ds@1952
  1104
			if (m_Libs.size() == 0)
ds@1952
  1105
			{
ds@1952
  1106
				g_RootMenu.ConsolePrint("[SM] No extensions are loaded.");
ds@1952
  1107
				return;
ds@1952
  1108
			}
ds@1952
  1109
ds@1952
  1110
			if (id > m_Libs.size())
ds@1952
  1111
			{
ds@1952
  1112
				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
ds@1952
  1113
				return;
ds@1952
  1114
			}
ds@1952
  1115
ds@1952
  1116
			List<CExtension *>::iterator iter = m_Libs.begin();
ds@1952
  1117
			CExtension *pExt = NULL;
ds@1952
  1118
			while (iter != m_Libs.end())
ds@1952
  1119
			{
ds@1952
  1120
				if (--id == 0)
ds@1952
  1121
				{
ds@1952
  1122
					pExt = (*iter);
ds@1952
  1123
					break;
ds@1952
  1124
				}
ds@1952
  1125
				iter++;
ds@1952
  1126
			}
ds@1952
  1127
			/* This should never happen */
ds@1952
  1128
			if (!pExt)
ds@1952
  1129
			{
ds@1952
  1130
				g_RootMenu.ConsolePrint("[SM] No extension was found with id %d.", id);
ds@1952
  1131
				return;
ds@1952
  1132
			}
ds@1952
  1133
ds@1952
  1134
			if (!pExt->IsLoaded())
ds@1952
  1135
			{
ds@1952
  1136
				g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
ds@1952
  1137
				g_RootMenu.ConsolePrint(" Loaded: No (%s)", pExt->m_Error.c_str());
ds@1952
  1138
			}
ds@1952
  1139
			else
ds@1952
  1140
			{
ds@1952
  1141
				char error[255];
ds@1952
  1142
				if (!pExt->IsRunning(error, sizeof(error)))
ds@1952
  1143
				{
ds@1952
  1144
					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
ds@1952
  1145
					g_RootMenu.ConsolePrint(" Loaded: Yes");
ds@1952
  1146
					g_RootMenu.ConsolePrint(" Running: No (%s)", error);
ds@1952
  1147
				}
ds@1952
  1148
				else
ds@1952
  1149
				{
ds@1952
  1150
					IExtensionInterface *pAPI = pExt->GetAPI();
ds@1952
  1151
					g_RootMenu.ConsolePrint(" File: %s", pExt->GetFilename());
ds@1952
  1152
					g_RootMenu.ConsolePrint(" Loaded: Yes (version %s)", pAPI->GetExtensionVerString());
ds@1952
  1153
					g_RootMenu.ConsolePrint(" Name: %s (%s)", pAPI->GetExtensionName(), pAPI->GetExtensionDescription());
ds@1952
  1154
					g_RootMenu.ConsolePrint(" Author: %s (%s)", pAPI->GetExtensionAuthor(), pAPI->GetExtensionURL());
ds@1952
  1155
					g_RootMenu.ConsolePrint(" Binary info: API version %d (compiled %s)", pAPI->GetExtensionVersion(), pAPI->GetExtensionDateString());
ds@1952
  1156
ds@1952
  1157
					if (pExt->IsExternal())
ds@1952
  1158
					{
ds@1952
  1159
						g_RootMenu.ConsolePrint(" Method: Loaded by Metamod:Source, attached to SourceMod");
ds@1952
  1160
					}
ds@1952
  1161
					else if (pAPI->IsMetamodExtension())
ds@1952
  1162
					{
ds@1952
  1163
						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod, attached to Metamod:Source");
ds@1952
  1164
					}
ds@1952
  1165
					else
ds@1952
  1166
					{
ds@1952
  1167
						g_RootMenu.ConsolePrint(" Method: Loaded by SourceMod");
ds@1952
  1168
					}
ds@1952
  1169
				}
ds@1952
  1170
			}
ds@1952
  1171
			return;
ds@1952
  1172
		}
ds@1952
  1173
		else if (strcmp(cmd, "unload") == 0)
ds@1952
  1174
		{
ds@1952
  1175
			if (argcount < 4)
ds@1952
  1176
			{
ds@1952
  1177
				g_RootMenu.ConsolePrint("[SM] Usage: sm unload <#> [code]");
ds@1952
  1178
				return;
ds@1952
  1179
			}
ds@1952
  1180
ds@1952
  1181
			const char *arg = command.Arg(3);
ds@1952
  1182
			unsigned int num = atoi(arg);
ds@1952
  1183
			CExtension *pExt = FindByOrder(num);
ds@1952
  1184
ds@1952
  1185
			if (!pExt)
ds@1952
  1186
			{
ds@1952
  1187
				g_RootMenu.ConsolePrint("[SM] Extension number %d was not found.", num);
ds@1952
  1188
				return;
ds@1952
  1189
			}
ds@1952
  1190
ds@1952
  1191
			if (argcount > 4 && pExt->unload_code)
ds@1952
  1192
			{
ds@1952
  1193
				const char *unload = command.Arg(4);
ds@1952
  1194
				if (pExt->unload_code == (unsigned)atoi(unload))
ds@1952
  1195
				{
ds@1952
  1196
					char filename[PLATFORM_MAX_PATH];
ds@1952
  1197
					snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
ds@1952
  1198
					UnloadExtension(pExt);
ds@1952
  1199
					g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
ds@1952
  1200
				}
ds@1952
  1201
				else
ds@1952
  1202
				{
ds@1952
  1203
					g_RootMenu.ConsolePrint("[SM] Please try again, the correct unload code is \"%d\"", pExt->unload_code);
ds@1952
  1204
				}
ds@1952
  1205
				return;
ds@1952
  1206
			}
ds@1952
  1207
ds@1952
  1208
			if (!pExt->IsLoaded() 
ds@1952
  1209
				|| (!pExt->m_ChildDeps.size() && !pExt->m_Plugins.size()))
ds@1952
  1210
			{
ds@1952
  1211
				char filename[PLATFORM_MAX_PATH];
ds@1952
  1212
				snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
ds@1952
  1213
				UnloadExtension(pExt);
ds@1952
  1214
				g_RootMenu.ConsolePrint("[SM] Extension %s is now unloaded.", filename);
ds@1952
  1215
				return;
ds@1952
  1216
			}
ds@1952
  1217
			else
ds@1952
  1218
			{
ds@1952
  1219
				List<IPlugin *> plugins;
ds@1952
  1220
				if (pExt->m_ChildDeps.size())
ds@1952
  1221
				{
ds@1952
  1222
					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following extensions: ", pExt->GetFilename());
ds@1952
  1223
					List<CExtension *>::iterator iter;
ds@1952
  1224
					CExtension *pOther;
ds@1952
  1225
					/* Get list of all extensions */
ds@1952
  1226
					for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
ds@1952
  1227
					{
ds@1952
  1228
						List<IfaceInfo>::iterator i_iter;
ds@1952
  1229
						pOther = (*iter);
ds@1952
  1230
						if (!pOther->IsLoaded() || pOther == pExt)
ds@1952
  1231
						{
ds@1952
  1232
							continue;
ds@1952
  1233
						}
ds@1952
  1234
						/* Get their dependencies */
ds@1952
  1235
						for (i_iter=pOther->m_Deps.begin();
ds@1952
  1236
							 i_iter!=pOther->m_Deps.end();
ds@1952
  1237
							 i_iter++)
ds@1952
  1238
						{
ds@1952
  1239
							/* Is this dependency to us? */
ds@1952
  1240
							if ((*i_iter).owner != pExt)
ds@1952
  1241
							{
ds@1952
  1242
								continue;
ds@1952
  1243
							}
ds@1952
  1244
							/* Will our dependent care? */
ds@1952
  1245
							if (!pExt->GetAPI()->QueryInterfaceDrop((*i_iter).iface))
ds@1952
  1246
							{
ds@1952
  1247
								g_RootMenu.ConsolePrint(" -> %s", pExt->GetFilename());
ds@1952
  1248
								/* Add to plugin unload list */
ds@1952
  1249
								List<IPlugin *>::iterator p_iter;
ds@1952
  1250
								for (p_iter=pOther->m_Plugins.begin();
ds@1952
  1251
									 p_iter!=pOther->m_Plugins.end();
ds@1952
  1252
									 p_iter++)
ds@1952
  1253
								{
ds@1952
  1254
									if (plugins.find((*p_iter)) == plugins.end())
ds@1952
  1255
									{
ds@1952
  1256
										plugins.push_back((*p_iter));
ds@1952
  1257
									}
ds@1952
  1258
								}
ds@1952
  1259
							}
ds@1952
  1260
						}
ds@1952
  1261
					}
ds@1952
  1262
				}
ds@1952
  1263
				if (pExt->m_Plugins.size())
ds@1952
  1264
				{
ds@1952
  1265
					g_RootMenu.ConsolePrint("[SM] Unloading %s will unload the following plugins: ", pExt->GetFilename());
ds@1952
  1266
					List<IPlugin *>::iterator iter;
ds@1952
  1267
					IPlugin *pPlugin;
ds@1952
  1268
					for (iter = pExt->m_Plugins.begin(); iter != pExt->m_Plugins.end(); iter++)
ds@1952
  1269
					{
ds@1952
  1270
						pPlugin = (*iter);
ds@1952
  1271
						if (plugins.find(pPlugin) == plugins.end())
ds@1952
  1272
						{
ds@1952
  1273
							plugins.push_back(pPlugin);
ds@1952
  1274
						}
ds@1952
  1275
					}
ds@1952
  1276
					for (iter = plugins.begin(); iter != plugins.end(); iter++)
ds@1952
  1277
					{
ds@1952
  1278
						pPlugin = (*iter);
ds@1952
  1279
						g_RootMenu.ConsolePrint(" -> %s", pPlugin->GetFilename());
ds@1952
  1280
					}
ds@1952
  1281
				}
ds@1952
  1282
				srand(static_cast<int>(time(NULL)));
ds@1952
  1283
				pExt->unload_code = (rand() % 877) + 123;	//123 to 999
ds@1952
  1284
				g_RootMenu.ConsolePrint("[SM] To verify unloading %s, please use the following: ", pExt->GetFilename());
ds@1952
  1285
				g_RootMenu.ConsolePrint("[SM] sm exts unload %d %d", num, pExt->unload_code);
ds@1952
  1286
ds@1952
  1287
				return;
ds@1952
  1288
			}
ds@1952
  1289
		}
ds@1952
  1290
	}
ds@1952
  1291
ds@1952
  1292
	g_RootMenu.ConsolePrint("SourceMod Extensions Menu:");
ds@1952
  1293
	g_RootMenu.DrawGenericOption("info", "Extra extension information");
ds@1952
  1294
	g_RootMenu.DrawGenericOption("list", "List extensions");
ds@1952
  1295
	g_RootMenu.DrawGenericOption("unload", "Unload an extension");
ds@1952
  1296
}
ds@1952
  1297
ds@1952
  1298
CExtension *CExtensionManager::GetExtensionFromIdent(IdentityToken_t *ptr)
ds@1952
  1299
{
ds@1952
  1300
	if (ptr->type == g_ExtType)
ds@1952
  1301
	{
ds@1952
  1302
		return (CExtension *)(ptr->ptr);
ds@1952
  1303
	}
ds@1952
  1304
ds@1952
  1305
	return NULL;
ds@1952
  1306
}
ds@1952
  1307
ds@1952
  1308
CExtensionManager::CExtensionManager()
ds@1952
  1309
{
ds@1952
  1310
}
ds@1952
  1311
ds@1952
  1312
CExtensionManager::~CExtensionManager()
ds@1952
  1313
{
ds@1952
  1314
}
ds@1952
  1315
ds@1952
  1316
void CExtensionManager::AddLibrary(IExtension *pSource, const char *library)
ds@1952
  1317
{
ds@1952
  1318
	CExtension *pExt = (CExtension *)pSource;
ds@1952
  1319
	pExt->AddLibrary(library);
ds@1952
  1320
	g_PluginSys.OnLibraryAction(library, false, false);
ds@1952
  1321
}
ds@1952
  1322
ds@1952
  1323
bool CExtensionManager::LibraryExists(const char *library)
ds@1952
  1324
{
ds@1952
  1325
	CExtension *pExt;
ds@1952
  1326
ds@1952
  1327
	for (List<CExtension *>::iterator iter = m_Libs.begin();
ds@1952
  1328
		 iter != m_Libs.end();
ds@1952
  1329
		 iter++)
ds@1952
  1330
	{
ds@1952
  1331
		pExt = (*iter);
ds@1952
  1332
		for (List<String>::iterator s_iter = pExt->m_Libraries.begin();
ds@1952
  1333
			 s_iter != pExt->m_Libraries.end();
ds@1952
  1334
			 s_iter++)
ds@1952
  1335
		{
ds@1952
  1336
			if ((*s_iter).compare(library) == 0)
ds@1952
  1337
			{
ds@1952
  1338
				return true;
ds@1952
  1339
			}
ds@1952
  1340
		}
ds@1952
  1341
	}
ds@1952
  1342
ds@1952
  1343
	return false;
ds@1952
  1344
}
ds@1952
  1345
ds@1952
  1346
IExtension *CExtensionManager::LoadExternal(IExtensionInterface *pInterface,
ds@1952
  1347
											const char *filepath,
ds@1952
  1348
											const char *filename,
ds@1952
  1349
											char *error,
ds@1952
  1350
											size_t maxlength)
ds@1952
  1351
{
ds@1952
  1352
	IExtension *pAlready;
ds@1952
  1353
	if ((pAlready=FindExtensionByFile(filename)) != NULL)
ds@1952
  1354
	{
ds@1952
  1355
		return pAlready;
ds@1952
  1356
	}
ds@1952
  1357
ds@1952
  1358
	CExtension *pExt = new CRemoteExtension(pInterface, filename, filepath);
ds@1952
  1359
ds@1952
  1360
	if (!pExt->Load(error, maxlength) || !pExt->IsLoaded())
ds@1952
  1361
	{
ds@1952
  1362
		pExt->Unload();
ds@1952
  1363
		delete pExt;
ds@1952
  1364
		return NULL;
ds@1952
  1365
	}
ds@1952
  1366
ds@1952
  1367
	m_Libs.push_back(pExt);
ds@1952
  1368
ds@1952
  1369
	return pExt;
ds@1952
  1370
}
ds@1952
  1371
faluco@1993
  1372
void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax)
faluco@1993
  1373
{
dvander@1998
  1374
	IExtensionInterface *pAPI;
faluco@1993
  1375
	List<CExtension *>::iterator iter;
faluco@1993
  1376
faluco@1993
  1377
	for (iter=m_Libs.begin(); iter!=m_Libs.end(); iter++)
faluco@1993
  1378
	{
dvander@1998
  1379
		if ((pAPI = (*iter)->GetAPI()) == NULL)
faluco@1993
  1380
		{
dvander@1998
  1381
			continue;
dvander@1998
  1382
		}
dvander@1998
  1383
		if (pAPI->GetExtensionVersion() > 3)
dvander@1998
  1384
		{
dvander@1998
  1385
			pAPI->OnCoreMapStart(pEdictList, edictCount, clientMax);
faluco@1993
  1386
		}
faluco@1993
  1387
	}
faluco@1993
  1388
}