sourcemm/concommands.cpp
author Scott Ehlert <ds@alliedmods.net>
Fri Aug 10 23:43:16 2007 +0000 (2007-08-10)
changeset 346 d0868271663a
parent 345 600e30820ea7
permissions -rw-r--r--
Reformatted output of meta list command
dvander@7
     1
/* ======== SourceMM ========
dvander@282
     2
 * Copyright (C) 2004-2007 Metamod:Source Development Team
dvander@282
     3
 * No warranties of any kind
dvander@282
     4
 *
dvander@282
     5
 * License: zlib/libpng
dvander@282
     6
 *
dvander@282
     7
 * Author(s): David "BAILOPAN" Anderson
dvander@282
     8
 * ============================
dvander@282
     9
 */
dvander@7
    10
dvander@67
    11
#include <ctype.h>
dvander@76
    12
#include "CSmmAPI.h"
dvander@7
    13
#include "concommands.h"
dvander@7
    14
#include "CPlugin.h"
dvander@98
    15
#include "sh_string.h"
dvander@98
    16
#include "sh_list.h"
dvander@7
    17
ds@346
    18
using namespace SourceMM;
ds@346
    19
using namespace SourceHook;
ds@346
    20
dvander@7
    21
/**
dvander@7
    22
 * @brief Console Command Implementations
dvander@7
    23
 * @file concommands.cpp
dvander@7
    24
 */
dvander@7
    25
dvander@67
    26
CAlwaysRegisterableCommand g_EternalCommand;
dvander@7
    27
SMConVarAccessor g_SMConVarAccessor;
dvander@7
    28
dvander@7
    29
bool SMConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
dvander@7
    30
{
pavolmarko@16
    31
	// Add the FCVAR_GAMEDLL flag 
pavolmarko@16
    32
	// => No crash on exit!
pavolmarko@63
    33
	// UPDATE: Do _not_ add the FCVAR_GAMEDLL flag here, as it
pavolmarko@63
    34
	// causes the command to be unusable on listenservers until you load a map
pavolmarko@63
    35
	// We will set the FCVAR_GAMEDLL flag on all commands we have registered once we are being unloaded
pavolmarko@63
    36
	//pCommand->AddFlags(FCVAR_GAMEDLL);
pavolmarko@63
    37
	m_RegisteredCommands.push_back(pCommand);
pavolmarko@63
    38
dvander@7
    39
	pCommand->SetNext( NULL );
dvander@7
    40
	g_Engine.icvar->RegisterConCommandBase(pCommand);
dvander@7
    41
dvander@7
    42
	return true;
dvander@7
    43
}
dvander@7
    44
dvander@67
    45
bool SMConVarAccessor::Register(ConCommandBase *pCommand)
dvander@67
    46
{
dvander@67
    47
	//simple, don't mark as part of sourcemm!
dvander@67
    48
	pCommand->SetNext( NULL );
dvander@67
    49
	g_Engine.icvar->RegisterConCommandBase(pCommand);
dvander@67
    50
dvander@67
    51
	return true;
dvander@67
    52
}
dvander@67
    53
pavolmarko@63
    54
void SMConVarAccessor::MarkCommandsAsGameDLL()
pavolmarko@63
    55
{
ds@346
    56
	for (List<ConCommandBase*>::iterator iter = m_RegisteredCommands.begin();
pavolmarko@63
    57
		iter != m_RegisteredCommands.end(); ++iter)
pavolmarko@63
    58
	{
pavolmarko@63
    59
		(*iter)->AddFlags(FCVAR_GAMEDLL);
pavolmarko@63
    60
	}
pavolmarko@63
    61
}
pavolmarko@63
    62
ds@345
    63
void SMConVarAccessor::Unregister(PluginId id, ConCommandBase *pCommand)
dvander@64
    64
{
ds@345
    65
	/* Notify via IMetamodListener */
ds@345
    66
	PluginIter iter;
ds@346
    67
	CPluginManager::CPlugin *pPlugin;
ds@346
    68
	List<IMetamodListener *>::iterator event;
ds@345
    69
	IMetamodListener *pML;
ds@345
    70
	for (iter=g_PluginMngr._begin(); iter!=g_PluginMngr._end(); iter++)
ds@345
    71
	{
ds@345
    72
		pPlugin = (*iter);
ds@345
    73
		if (pPlugin->m_Status < Pl_Paused)
ds@345
    74
		{
ds@345
    75
			continue;
ds@345
    76
		}
ds@345
    77
		/* Only valid for plugins >= 12 (v1:6, SourceMM 1.5) */
ds@345
    78
		if (pPlugin->m_API->GetApiVersion() < 12)
ds@345
    79
		{
ds@345
    80
			continue;
ds@345
    81
		}
ds@345
    82
		for (event=pPlugin->m_Events.begin();
ds@345
    83
			event!=pPlugin->m_Events.end();
ds@345
    84
			event++)
ds@345
    85
		{
ds@345
    86
			pML = (*event);
ds@345
    87
			pML->OnUnlinkConCommandBase(id, pCommand);
ds@345
    88
		}
ds@345
    89
	}
ds@345
    90
dvander@64
    91
	ICvar *cv = g_Engine.icvar;
dvander@64
    92
	ConCommandBase *ptr = cv->GetCommands();
dvander@64
    93
dvander@67
    94
	if (ptr == pCommand)
dvander@64
    95
	{
ds@345
    96
		/* First in list */
dvander@67
    97
		g_EternalCommand.BringToFront();
dvander@67
    98
		g_EternalCommand.SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
dvander@67
    99
	} else {
ds@345
   100
		/* Find us and unregister us */
dvander@67
   101
		ConCommandBase *pPrev = NULL;
dvander@67
   102
		while (ptr)
dvander@67
   103
		{
dvander@67
   104
			if (ptr == pCommand)
dvander@67
   105
				break;
dvander@67
   106
			pPrev = ptr;
dvander@67
   107
			ptr = const_cast<ConCommandBase *>(ptr->GetNext());
dvander@67
   108
		}
dvander@67
   109
		if (pPrev && ptr == pCommand)
dvander@67
   110
		{
dvander@67
   111
			pPrev->SetNext(const_cast<ConCommandBase *>(pCommand->GetNext()));
dvander@67
   112
		}
dvander@64
   113
	}
dvander@64
   114
}
dvander@64
   115
pavolmarko@114
   116
void SMConVarAccessor::UnregisterGameDLLCommands()
pavolmarko@114
   117
{
pavolmarko@114
   118
	ConCommandBase *begin = g_Engine.icvar->GetCommands();
pavolmarko@114
   119
	ConCommandBase *iter = begin;
pavolmarko@114
   120
	ConCommandBase *prev = NULL;
pavolmarko@114
   121
	while (iter)
pavolmarko@114
   122
	{
ds@345
   123
		/* Watch out for the ETERNAL COMMAND! */
pavolmarko@114
   124
		if (iter != &g_EternalCommand && iter->IsBitSet(FCVAR_GAMEDLL))
pavolmarko@114
   125
		{
ds@345
   126
			/* Remove it! */
pavolmarko@114
   127
			if (iter == begin)
pavolmarko@114
   128
			{
pavolmarko@114
   129
				g_EternalCommand.BringToFront();
pavolmarko@114
   130
				iter = const_cast<ConCommandBase*>(iter->GetNext());
pavolmarko@114
   131
				g_EternalCommand.SetNext(iter);
pavolmarko@114
   132
				prev = &g_EternalCommand;
pavolmarko@114
   133
				continue;
pavolmarko@114
   134
			}
pavolmarko@114
   135
			else
pavolmarko@114
   136
			{
pavolmarko@114
   137
				iter = const_cast<ConCommandBase*>(iter->GetNext());
pavolmarko@114
   138
				prev->SetNext(iter);
pavolmarko@114
   139
				continue;
pavolmarko@114
   140
			}
pavolmarko@114
   141
		}
pavolmarko@114
   142
		prev = iter;
pavolmarko@114
   143
		iter = const_cast<ConCommandBase*>(iter->GetNext());
pavolmarko@114
   144
	}
pavolmarko@114
   145
}
pavolmarko@114
   146
dvander@67
   147
ConVar metamod_version("metamod_version", SOURCEMM_VERSION, FCVAR_REPLICATED | FCVAR_SPONLY | FCVAR_NOTIFY, "Metamod:Source Version");
dvander@89
   148
#if defined WIN32 || defined _WIN32
dvander@89
   149
ConVar mm_pluginsfile("mm_pluginsfile", "addons\\metamod\\metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
dvander@89
   150
#else
dvander@89
   151
ConVar mm_pluginsfile("mm_pluginsfile", "addons/metamod/metaplugins.ini", FCVAR_SPONLY, "Metamod:Source Plugins File");
dvander@89
   152
#endif
dvander@7
   153
dvander@7
   154
CON_COMMAND(meta, "Metamod:Source Menu")
dvander@7
   155
{
dvander@7
   156
	IVEngineServer *e = g_Engine.engine;
dvander@7
   157
dvander@7
   158
	int args = e->Cmd_Argc();
dvander@7
   159
dvander@7
   160
	if (args >= 2)
dvander@7
   161
	{
dvander@7
   162
		const char *command = e->Cmd_Argv(1);
dvander@7
   163
		if (strcmp(command, "credits") == 0)
dvander@7
   164
		{
dvander@76
   165
			CONMSG("Metamod:Source was developed by:\n");
dvander@76
   166
			CONMSG("  SourceHook: Pavol \"PM OnoTo\" Marko\n");
dvander@76
   167
			CONMSG("  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
dvander@76
   168
			CONMSG("  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
dvander@76
   169
			CONMSG("For more information, see the official website\n");
dvander@76
   170
			CONMSG("http://www.sourcemm.net/\n");
dvander@7
   171
			
dvander@7
   172
			return;
dvander@7
   173
		} else if (strcmp(command, "version") == 0) {
dvander@76
   174
			CONMSG("Metamod:Source version %s\n", SOURCEMM_VERSION);
dvander@76
   175
			CONMSG("Compiled on: %s\n", SOURCEMM_DATE);
dvander@76
   176
			CONMSG("Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
dvander@76
   177
			CONMSG("SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
dvander@76
   178
			CONMSG("http://www.sourcemm.net/\n");
dvander@7
   179
dvander@7
   180
			return;
dvander@7
   181
		} else if (strcmp(command, "game") == 0) {
dvander@76
   182
			CONMSG("GameDLL Information\n");
ds@216
   183
			CONMSG("  Description: %s\n", g_GameDll.pGameDLL->GetGameDescription());
ds@216
   184
			CONMSG("  Mod Path: %s\n", g_ModPath.c_str());
ds@216
   185
			CONMSG("  DLL Path: %s\n", g_BinPath.c_str());
dvander@340
   186
			CONMSG("  Interface: ServerGameDLL%03d, ServerGameClients%03d\n", g_GameDllVersion, g_GameClientsVersion);
dvander@7
   187
ds@287
   188
			// Display user messages
ds@287
   189
			if (g_SmmAPI.MsgCacheSuccessful())
ds@287
   190
			{
ds@287
   191
				const char *msgname;
ds@287
   192
				int msgsize;
ds@287
   193
				int msgcount = g_SmmAPI.GetUserMessageCount();
ds@287
   194
ds@287
   195
				if (msgcount > 0)
ds@287
   196
				{
ds@287
   197
					CONMSG("  User Messages:  %-32.31s  %-5s  %-5s\n", "Name", "Index", "Size");
ds@287
   198
ds@287
   199
					for (int i = 0; i < msgcount; i++)
ds@287
   200
					{
ds@287
   201
						msgname = g_SmmAPI.GetUserMessage(i, &msgsize);
ds@287
   202
ds@287
   203
						CONMSG("                  %-32.31s  %-5d  %-5d\n", msgname, i, msgsize); 
ds@287
   204
					}
ds@287
   205
ds@287
   206
					CONMSG("  %d user message%s in total\n", msgcount, (msgcount > 1) ? "s" : "");
ds@287
   207
				} else {
ds@287
   208
					CONMSG("  User Messages: None\n");
ds@287
   209
				}
ds@287
   210
			} else {
ds@287
   211
				CONMSG("  User Messages: Failed to get list of user messages\n");
ds@287
   212
			}
ds@287
   213
dvander@7
   214
			return;
dvander@7
   215
		} else if (strcmp(command, "refresh") == 0) {
dvander@7
   216
			char full_path[255];
ds@296
   217
			g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s", g_ModPath.c_str(), GetPluginsFile());
ds@190
   218
dvander@7
   219
			LoadPluginsFromFile(full_path);
dvander@7
   220
dvander@7
   221
			return;
dvander@7
   222
		} else if (strcmp(command, "list") == 0) {
ds@346
   223
			CPluginManager::CPlugin *pl;
ds@346
   224
			ISmmPlugin *plapi;
ds@346
   225
			const char *plname;
dvander@7
   226
			PluginIter i;
ds@346
   227
			char buffer[256];
ds@346
   228
			int len;
ds@346
   229
			int plnum = g_PluginMngr.GetPluginCount();
dvander@7
   230
ds@346
   231
			if (!plnum)
ds@346
   232
			{
ds@346
   233
				CONMSG("No plugins loaded.\n");
ds@346
   234
				return;
ds@346
   235
			} else {
ds@346
   236
				CONMSG("Listing %d plugin%s:\n", plnum, (plnum > 1) ? "s" : "");
ds@346
   237
			}
ds@346
   238
ds@346
   239
			for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++)
dvander@7
   240
			{
dvander@7
   241
				pl = (*i);
dvander@7
   242
				if (!pl)
dvander@7
   243
				{
ds@346
   244
					break;
ds@346
   245
				}
ds@346
   246
ds@346
   247
				len = 0;
ds@346
   248
ds@346
   249
				if (pl->m_Status != Pl_Running)
ds@346
   250
				{
ds@346
   251
					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d] <%s>", pl->m_Id, g_PluginMngr.GetStatusText(pl));
ds@346
   252
				} else {
ds@346
   253
					len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", pl->m_Id);
dvander@7
   254
				}
dvander@12
   255
ds@346
   256
				if ((plapi = pl->m_API))
dvander@12
   257
				{
ds@346
   258
					plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
ds@346
   259
					len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
ds@346
   260
ds@346
   261
					if (IS_STR_FILLED(plapi->GetVersion()))
ds@346
   262
					{
ds@346
   263
						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
ds@346
   264
					}
ds@346
   265
					if (IS_STR_FILLED(plapi->GetAuthor()))
ds@346
   266
					{
ds@346
   267
						UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
ds@346
   268
					}
dvander@12
   269
				}
dvander@12
   270
ds@346
   271
				CONMSG("%s\n", buffer);
dvander@7
   272
			}
dvander@7
   273
dvander@7
   274
			return;
dvander@67
   275
		} else if (strcmp(command, "cmds") == 0) {
dvander@67
   276
			if (args >= 3)
dvander@67
   277
			{
dvander@67
   278
				int id = atoi(e->Cmd_Argv(2));
ds@346
   279
				CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
dvander@67
   280
dvander@67
   281
				if (!pl)
dvander@67
   282
				{
dvander@76
   283
					CONMSG("Plugin %d not found.\n", id);
dvander@67
   284
					return;
dvander@67
   285
				}
dvander@67
   286
dvander@67
   287
				if (!pl->m_API)
dvander@67
   288
				{
dvander@76
   289
					CONMSG("Plugin %d is not loaded.\n", id);
dvander@67
   290
				} else {
dvander@76
   291
					CONMSG("Console commands for %s:\n", pl->m_API->GetName());
ds@346
   292
					List<ConCommandBase *>::iterator ci;
dvander@67
   293
					size_t count = 0;
dvander@67
   294
dvander@67
   295
					for (ci=pl->m_Cmds.begin(); ci!=pl->m_Cmds.end(); ci++)
dvander@67
   296
					{
dvander@67
   297
						count++;
dvander@76
   298
						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
dvander@67
   299
					}
dvander@67
   300
				}
dvander@67
   301
			} else {
dvander@76
   302
				CONMSG("Usage: meta cmds <id>\n");
dvander@67
   303
			}
dvander@67
   304
dvander@67
   305
			return;
dvander@67
   306
		} else if (strcmp(command, "cvars") == 0) {
dvander@67
   307
			if (args >= 3)
dvander@67
   308
			{
dvander@67
   309
				int id = atoi(e->Cmd_Argv(2));
ds@346
   310
				CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
dvander@67
   311
dvander@67
   312
				if (!pl)
dvander@67
   313
				{
dvander@76
   314
					CONMSG("Plugin %d not found.\n", id);
dvander@67
   315
					return;
dvander@67
   316
				}
dvander@67
   317
dvander@67
   318
				if (!pl->m_API)
dvander@67
   319
				{
dvander@76
   320
					CONMSG("Plugin %d is not loaded.\n", id);
dvander@67
   321
				} else {
dvander@76
   322
					CONMSG("Registered cvars for %s:\n", pl->m_API->GetName());
ds@346
   323
					List<ConCommandBase *>::iterator ci;
dvander@67
   324
					size_t count = 0;
dvander@67
   325
dvander@67
   326
					for (ci=pl->m_Cvars.begin(); ci!=pl->m_Cvars.end(); ci++)
dvander@67
   327
					{
dvander@67
   328
						count++;
dvander@76
   329
						CONMSG(" [%5d] %-s\n", count, (*ci)->GetName());
dvander@67
   330
					}
dvander@67
   331
				}
dvander@67
   332
			} else {
dvander@76
   333
				CONMSG("Usage: meta cvars <id>\n");
dvander@67
   334
			}
dvander@67
   335
dvander@67
   336
			return;
dvander@7
   337
		} else if (strcmp(command, "info") == 0) {
dvander@7
   338
			if (args >= 3)
dvander@7
   339
			{
dvander@7
   340
				int id = atoi(e->Cmd_Argv(2));
ds@346
   341
				CPluginManager::CPlugin *pl = g_PluginMngr.FindById(id);
dvander@7
   342
				if (!pl)
dvander@7
   343
				{
dvander@76
   344
					CONMSG("Plugin %d not found.\n", id);
dvander@7
   345
					return;
dvander@7
   346
				}
dvander@7
   347
dvander@7
   348
				if (!pl->m_API)
dvander@7
   349
				{
dvander@76
   350
					CONMSG("Plugin %d is not loaded.\n", id);
dvander@7
   351
				} else {
dvander@7
   352
					if (pl->m_Status == Pl_Paused)
dvander@7
   353
					{
dvander@76
   354
						CONMSG("Plugin %d is paused.\n", id);
dvander@7
   355
					} else if (pl->m_Status == Pl_Running) {
dvander@124
   356
						char run_msg[255];
dvander@124
   357
						bool run = false;
dvander@124
   358
						if (pl->m_API && pl->m_API->QueryRunning(run_msg, sizeof(run_msg)-1))
dvander@124
   359
							run = true;
dvander@124
   360
						if (run)
dvander@124
   361
						{
dvander@124
   362
							CONMSG("Plugin %d is running.\n", id);
dvander@124
   363
						} else {
dvander@124
   364
							CONMSG("Plugin %d is stopped: %s\n", id, run_msg);
dvander@124
   365
						}
dvander@7
   366
					}
dvander@76
   367
					CONMSG("  Name: \"%s\" by %s\n", pl->m_API->GetName(), pl->m_API->GetAuthor());
dvander@76
   368
					CONMSG("  Version: %s\n", pl->m_API->GetVersion());
dvander@76
   369
					CONMSG("  Description: %s\n", pl->m_API->GetDescription());
dvander@76
   370
					CONMSG("  License: %s\n", pl->m_API->GetLicense());
dvander@76
   371
					CONMSG("  URL: %s\n", pl->m_API->GetURL());
dvander@76
   372
					CONMSG("  Details: API %03d, Date: %s\n", pl->m_API->GetApiVersion(), pl->m_API->GetDate());
dvander@7
   373
				}
dvander@76
   374
				CONMSG("File: %s\n\n", pl->m_File.c_str());
dvander@7
   375
dvander@7
   376
				return;
dvander@7
   377
			} else {
dvander@76
   378
				CONMSG("Usage: meta info <id>\n");
dvander@7
   379
dvander@7
   380
				return;
dvander@7
   381
			}
dvander@9
   382
		} else if (strcmp(command, "pause") == 0) {
dvander@9
   383
			if (args >= 3)
dvander@9
   384
			{
dvander@9
   385
				int id = atoi(e->Cmd_Argv(2));
dvander@12
   386
dvander@12
   387
				char error[255];
dvander@12
   388
ds@296
   389
				if (!g_PluginMngr.Pause(id, error, sizeof(error)))
dvander@9
   390
				{
dvander@76
   391
					CONMSG("Pause failed: %s\n", error);
dvander@9
   392
					return;
dvander@9
   393
				}
dvander@9
   394
				
dvander@76
   395
				CONMSG("Plugin %d has been paused.\n", id);
dvander@9
   396
dvander@9
   397
				return;
dvander@9
   398
			} else {
dvander@76
   399
				CONMSG("Usage: meta pause <id>\n");
dvander@9
   400
dvander@9
   401
				return;
dvander@9
   402
			}
dvander@9
   403
		} else if (strcmp(command, "unpause") == 0) {
dvander@9
   404
			if (args >= 3)
dvander@9
   405
			{
dvander@9
   406
				int id = atoi(e->Cmd_Argv(2));
dvander@12
   407
				char error[255];
dvander@12
   408
ds@296
   409
				if (!g_PluginMngr.Unpause(id, error, sizeof(error)))
dvander@9
   410
				{
dvander@76
   411
					CONMSG("Unpause failed: %s\n", error);
dvander@9
   412
					return;
dvander@9
   413
				}
dvander@9
   414
dvander@76
   415
				CONMSG("Plugin %d has been unpaused.\n", id);
dvander@9
   416
dvander@9
   417
				return;
dvander@9
   418
			} else {
dvander@76
   419
				CONMSG("Usage: meta unpause <id>\n");
dvander@9
   420
dvander@9
   421
				return;
dvander@9
   422
			}
dvander@9
   423
		} else if (strcmp(command, "load") == 0) {
dvander@9
   424
			if (args >= 3)
dvander@9
   425
			{
dvander@9
   426
				const char *file = e->Cmd_Argv(2);
dvander@9
   427
				char full_path[255];
dvander@9
   428
dvander@205
   429
				const char *alias = g_PluginMngr.LookupAlias(file);
dvander@205
   430
				if (alias)
dvander@205
   431
				{
dvander@205
   432
					file = alias;
dvander@205
   433
				}
dvander@205
   434
dvander@9
   435
				if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
dvander@9
   436
				{
ds@296
   437
					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
dvander@9
   438
				} else {
dvander@9
   439
					const char *ext = UTIL_GetExtension(file);
dvander@9
   440
#if defined WIN32 || defined _WIN32
dvander@12
   441
					ext = ext ? "" : ".dll";
dvander@9
   442
#else
dvander@12
   443
					ext = ext ? "" : "_i486.so";
dvander@9
   444
#endif
ds@296
   445
					g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
dvander@9
   446
				}
dvander@9
   447
dvander@9
   448
				char error[255]={0};
dvander@9
   449
				bool already;
ds@346
   450
				CPluginManager::CPlugin *pl;
dvander@9
   451
ds@296
   452
				PluginId id = g_PluginMngr.Load(full_path, Pl_Console, already, error, sizeof(error));
dvander@9
   453
				pl = g_PluginMngr.FindById(id);
dvander@9
   454
				if (!pl || id < Pl_MinId || (pl->m_Status < Pl_Paused))
dvander@9
   455
				{
dvander@76
   456
					CONMSG("Failed to load plugin %s (%s).\n", file, error);
dvander@9
   457
					return;
dvander@9
   458
				}
dvander@9
   459
dvander@9
   460
				if (!already)
dvander@9
   461
				{
dvander@76
   462
					CONMSG("Plugin \"%s\" loaded with id %d.\n", pl->m_API->GetName(), pl->m_Id);
dvander@9
   463
				} else {
dvander@76
   464
					CONMSG("Plugin \"%s\" is already loaded as %d.\n", pl->m_API->GetName(), pl->m_Id);
dvander@9
   465
				}
dvander@9
   466
				
dvander@9
   467
				return;
dvander@9
   468
			} else {
dvander@76
   469
				CONMSG("Usage: meta load <path>\n");
dvander@9
   470
dvander@9
   471
				return;
dvander@9
   472
			}
dvander@205
   473
		} else if ( (strcmp(command, "alias") == 0) ||
dvander@205
   474
					(strcmp(command, "aliases") == 0) ) {
dvander@205
   475
			if (args >= 4)
dvander@205
   476
			{
dvander@205
   477
				const char *alias = e->Cmd_Argv(2);
dvander@205
   478
				const char *value = e->Cmd_Argv(3);
dvander@205
   479
dvander@205
   480
				g_PluginMngr.SetAlias(alias, value);
dvander@205
   481
				if (value[0] == '\0')
dvander@205
   482
				{
dvander@205
   483
					CONMSG("Deleted alias: %s.\n", alias);
dvander@205
   484
				} else {
dvander@205
   485
					CONMSG("Set alias \"%s\" to: %s\n", alias, value);
dvander@205
   486
				}
dvander@205
   487
			} else if (args >= 3) {
dvander@205
   488
				const char *alias = e->Cmd_Argv(2);
dvander@205
   489
				const char *value = g_PluginMngr.LookupAlias(alias);
dvander@205
   490
				if (value)
dvander@205
   491
				{
dvander@205
   492
					CONMSG("Alias \"%s\" is set to: %s\n", alias, value);
dvander@205
   493
				} else {
dvander@205
   494
					CONMSG("Alias \"%s\" was not found.\n", alias);
dvander@205
   495
				}
dvander@205
   496
			} else {
ds@346
   497
				List<CNameAlias *>::iterator iter, end;
ds@346
   498
				CNameAlias *p;
dvander@205
   499
dvander@205
   500
				iter = g_PluginMngr._alias_begin();
dvander@205
   501
				end = g_PluginMngr._alias_end();
dvander@205
   502
				size_t total = 0;
dvander@205
   503
				if (iter != end)
dvander@205
   504
				{
dvander@205
   505
					CONMSG("%-10.9s %s\n", "Alias", "File");
dvander@205
   506
					CONMSG(" --- \n");
dvander@205
   507
					for (; iter!=end; iter++)
dvander@205
   508
					{
dvander@205
   509
						p = (*iter);
dvander@205
   510
						CONMSG("%-10.9s %s\n", p->alias.c_str(), p->value.c_str());
dvander@205
   511
						total++;
dvander@205
   512
					}
dvander@205
   513
					CONMSG(" --- \n");
dvander@205
   514
					CONMSG("%d aliases total.\n", total);
dvander@205
   515
				} else {
dvander@205
   516
					CONMSG("No aliases found.\n");
dvander@205
   517
				}
dvander@205
   518
			}
dvander@205
   519
			return;
dvander@9
   520
		} else if (strcmp(command, "unload") == 0) {
dvander@9
   521
			if (args >= 3)
dvander@9
   522
			{
dvander@205
   523
				const char *file = e->Cmd_Argv(2);
dvander@205
   524
				int id = atoi(file);
dvander@9
   525
				char error[255]={0};
dvander@9
   526
dvander@205
   527
				if (id == 0 && isalpha(file[0]))
dvander@205
   528
				{
dvander@205
   529
					char full_path[255];
dvander@205
   530
					const char *alias = g_PluginMngr.LookupAlias(file);
dvander@205
   531
dvander@205
   532
					if (alias)
dvander@205
   533
					{
dvander@205
   534
						file = alias;
dvander@205
   535
					}
dvander@205
   536
dvander@205
   537
					/* first check if it's a known filename */
dvander@205
   538
					if (file[0] == '/' || strcmp(&(file[1]), ":\\") == 0)
dvander@205
   539
					{
ds@296
   540
						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s", file);
dvander@205
   541
					} else {
dvander@205
   542
						const char *ext = UTIL_GetExtension(file);
dvander@205
   543
#if defined WIN32 || defined _WIN32
dvander@205
   544
						ext = ext ? "" : ".dll";
dvander@205
   545
#else
dvander@205
   546
						ext = ext ? "" : "_i486.so";
dvander@205
   547
#endif
ds@296
   548
						g_SmmAPI.PathFormat(full_path, sizeof(full_path), "%s/%s%s", g_ModPath.c_str(), file, ext);
dvander@205
   549
					}
dvander@205
   550
ds@346
   551
					List<CPluginManager::CPlugin *>::iterator iter, end;
ds@346
   552
					CPluginManager::CPlugin *pl;
dvander@205
   553
					iter = g_PluginMngr._begin();
dvander@205
   554
					end = g_PluginMngr._end();
dvander@205
   555
					for (; iter!=end; iter++)
dvander@205
   556
					{
dvander@205
   557
						pl = (*iter);
dvander@205
   558
						if (strcmp(pl->m_File.c_str(), full_path) == 0)
dvander@205
   559
						{
dvander@205
   560
							id = pl->m_Id;
dvander@205
   561
							break;
dvander@205
   562
						}
dvander@205
   563
					}
dvander@205
   564
					if (id == 0)
dvander@205
   565
					{
dvander@205
   566
						CONMSG("Plugin \"%s\" not found.\n", full_path);
dvander@205
   567
						return;
dvander@205
   568
					}
dvander@205
   569
				}
dvander@205
   570
ds@296
   571
				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
dvander@9
   572
				{
dvander@76
   573
					CONMSG("Unload failed: %s\n", error);
dvander@9
   574
					return;
dvander@9
   575
				}
dvander@9
   576
dvander@76
   577
				CONMSG("Plugin %d unloaded.\n", id);
dvander@9
   578
			} else {
dvander@76
   579
				CONMSG("Usage: meta unload <id>\n");
dvander@9
   580
			}
dvander@205
   581
			return;
dvander@9
   582
		} else if (strcmp(command, "force_unload") == 0) {
dvander@9
   583
			if (args >= 3)
dvander@9
   584
			{
dvander@9
   585
				int id = atoi(e->Cmd_Argv(2));
dvander@9
   586
				char error[255]={0};
dvander@9
   587
ds@296
   588
				if (!g_PluginMngr.Unload(id, false, error, sizeof(error)))
dvander@9
   589
				{
dvander@76
   590
					CONMSG("Force unload failed: %s\n", error);
dvander@9
   591
					return;
dvander@9
   592
				}
dvander@9
   593
dvander@76
   594
				CONMSG("Plugin %d force unloaded.\n", id);
dvander@9
   595
dvander@9
   596
				return;
dvander@9
   597
			} else {
dvander@76
   598
				CONMSG("Usage: meta force_unload <id>\n");
dvander@9
   599
dvander@9
   600
				return;
dvander@9
   601
			}
dvander@9
   602
		} else if (strcmp(command, "clear") == 0) {
dvander@9
   603
			if (!g_PluginMngr.UnloadAll())
dvander@9
   604
			{
dvander@76
   605
				CONMSG("One or more plugins resisted removal (cleaned anyway).\n");
dvander@9
   606
				return;
dvander@9
   607
			} 
dvander@9
   608
dvander@76
   609
			CONMSG("All plugins unloaded.\n");
dvander@9
   610
dvander@9
   611
			return;
dvander@13
   612
		} else if (strcmp(command, "retry") == 0) {
dvander@13
   613
			if (args >= 3)
dvander@13
   614
			{
dvander@13
   615
				int id = atoi(e->Cmd_Argv(2));
dvander@29
   616
				char error[255];
dvander@13
   617
ds@296
   618
				if (!g_PluginMngr.Retry(id, error, sizeof(error)))
dvander@13
   619
				{
dvander@76
   620
					CONMSG("Error reloading plugin: %s\n", error);
dvander@13
   621
					return;
dvander@13
   622
				}
dvander@13
   623
dvander@76
   624
				CONMSG("Plugin %d successfully reloaded.\n", id);
dvander@13
   625
dvander@13
   626
				return;
dvander@13
   627
			} else {
dvander@76
   628
				CONMSG("Usage: meta retry <id>\n");
dvander@13
   629
dvander@13
   630
				return;
dvander@13
   631
			}
dvander@7
   632
		}
dvander@7
   633
	}
dvander@7
   634
dvander@76
   635
	CONMSG("Metamod:Source Menu\n");
dvander@76
   636
	CONMSG("usage: meta <command> [arguments]\n");
dvander@207
   637
	CONMSG("  alias        - List or set an alias\n");
dvander@76
   638
	CONMSG("  clear        - Unload all plugins forcefully\n");
dvander@76
   639
	CONMSG("  cmds         - Show plugin commands\n");
dvander@76
   640
	CONMSG("  cvars        - Show plugin cvars\n");
dvander@76
   641
	CONMSG("  credits      - About Metamod:Source\n");
dvander@76
   642
	CONMSG("  force_unload - Forcefully unload a plugin\n");
dvander@76
   643
	CONMSG("  game         - Information about GameDLL\n");
dvander@76
   644
	CONMSG("  info         - Information about a plugin\n");
dvander@76
   645
	CONMSG("  list         - List plugins\n");
dvander@76
   646
	CONMSG("  load         - Load a plugin\n");
dvander@76
   647
	CONMSG("  pause        - Pause a running plugin\n");
dvander@76
   648
	CONMSG("  refresh      - Reparse plugins file\n");
dvander@76
   649
	CONMSG("  retry        - Attempt to reload a plugin\n");
dvander@76
   650
	CONMSG("  unload       - Unload a loaded plugin\n");
dvander@76
   651
	CONMSG("  unpause      - Unpause a paused plugin\n");
dvander@76
   652
	CONMSG("  version      - Version information\n");
dvander@7
   653
}
dvander@67
   654
dvander@67
   655
CAlwaysRegisterableCommand::CAlwaysRegisterableCommand()
dvander@67
   656
{
dvander@67
   657
	Create("", NULL, FCVAR_UNREGISTERED|FCVAR_GAMEDLL);
dvander@67
   658
	m_pICvar = NULL;
dvander@67
   659
}
dvander@67
   660
dvander@67
   661
bool CAlwaysRegisterableCommand::IsRegistered( void ) const
dvander@67
   662
{
dvander@67
   663
	return false;
dvander@67
   664
}
dvander@67
   665
dvander@67
   666
void CAlwaysRegisterableCommand::BringToFront()
dvander@67
   667
{
dvander@67
   668
	if (!m_pICvar)
dvander@67
   669
		m_pICvar = g_Engine.icvar;
dvander@67
   670
dvander@67
   671
	// First, let's try to find us!
dvander@67
   672
	ConCommandBase *pPtr = m_pICvar->GetCommands();
dvander@67
   673
dvander@67
   674
	if (pPtr == this)
dvander@67
   675
	{
dvander@67
   676
		// We are already at the beginning; Nothing to do
dvander@67
   677
		return;
dvander@67
   678
	}
dvander@67
   679
dvander@67
   680
	while (pPtr)
dvander@67
   681
	{
dvander@97
   682
		if (pPtr == this && pPtr->IsCommand() && stricmp(GetName(), pPtr->GetName()) == 0)
dvander@67
   683
			break;
dvander@67
   684
		ConCommandBase *pPrev = NULL;
dvander@67
   685
		while (pPtr)
dvander@67
   686
		{
dvander@67
   687
			if (pPtr == this)
dvander@67
   688
				break;
dvander@67
   689
			pPrev = pPtr;
dvander@67
   690
			pPtr = const_cast<ConCommandBase*>(pPtr->GetNext());
dvander@67
   691
		}
dvander@67
   692
		if (pPrev && pPtr == this)
dvander@67
   693
		{
dvander@67
   694
			pPrev->SetNext(m_pNext);		// Remove us from the list
dvander@67
   695
		}
dvander@67
   696
		// Now, register us
dvander@132
   697
		SetNext(NULL);
dvander@67
   698
		m_pICvar->RegisterConCommandBase(this);
dvander@67
   699
	}
dvander@67
   700
}
dvander@89
   701
ds@192
   702
void ClientCommand_handler(edict_t *client)
ds@192
   703
{
ds@192
   704
	IVEngineServer *e = g_Engine.engine;
ds@192
   705
	const char *cmd = e->Cmd_Argv(0);
ds@192
   706
ds@192
   707
	if (strcmp(cmd, "meta") == 0)
ds@192
   708
	{
ds@192
   709
		int args = e->Cmd_Argc();
ds@192
   710
		if (args == 2)
ds@192
   711
		{
ds@192
   712
			const char *subcmd = e->Cmd_Argv(1);
ds@192
   713
ds@192
   714
			if (strcmp(subcmd, "credits") == 0)
ds@192
   715
			{
ds@192
   716
				CLIENT_CONMSG(client, "Metamod:Source was developed by:\n");
ds@192
   717
				CLIENT_CONMSG(client, "  SourceHook: Pavol \"PM OnoTo\" Marko\n");
ds@192
   718
				CLIENT_CONMSG(client, "  GameDLL/Plugins: David \"BAILOPAN\" Anderson\n");
ds@192
   719
				CLIENT_CONMSG(client, "  GameDLL: Scott \"Damaged Soul\" Ehlert\n");
ds@192
   720
				CLIENT_CONMSG(client, "For more information, see the official website\n");
ds@192
   721
				CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
ds@303
   722
ds@303
   723
				RETURN_META(MRES_SUPERCEDE);
ds@303
   724
			} else if(strcmp(subcmd, "version") == 0) {
ds@192
   725
				CLIENT_CONMSG(client, "Metamod:Source version %s\n", SOURCEMM_VERSION);
ds@192
   726
				CLIENT_CONMSG(client, "Compiled on: %s\n", SOURCEMM_DATE);
ds@192
   727
				CLIENT_CONMSG(client, "Plugin interface version: %d:%d\n", PLAPI_VERSION, PLAPI_MIN_VERSION);
ds@192
   728
				CLIENT_CONMSG(client, "SourceHook version: %d:%d\n", g_SourceHook.GetIfaceVersion(), g_SourceHook.GetImplVersion());
ds@192
   729
				CLIENT_CONMSG(client, "http://www.sourcemm.net/\n");
ds@303
   730
ds@303
   731
				RETURN_META(MRES_SUPERCEDE);
ds@303
   732
			} else if(strcmp(subcmd, "list") == 0) {
ds@346
   733
				CPluginManager::CPlugin *pl;
ds@346
   734
				ISmmPlugin *plapi;
ds@346
   735
				const char *plname;
ds@192
   736
				PluginIter i;
ds@346
   737
				char buffer[256];
ds@346
   738
				int len = 0;
ds@346
   739
				int plnum = 0;
ds@192
   740
ds@346
   741
				for (i = g_PluginMngr._begin(); i != g_PluginMngr._end(); i++, len=0)
ds@192
   742
				{
ds@192
   743
					pl = (*i);
ds@346
   744
					if (pl && pl->m_Status == Pl_Running)
ds@192
   745
					{
ds@346
   746
						plapi = pl->m_API;
ds@346
   747
						if (!plapi || !plapi->QueryRunning(NULL, 0))
ds@346
   748
						{
ds@346
   749
							continue;
ds@346
   750
						}
ds@346
   751
						plnum++;
ds@346
   752
ds@346
   753
						len += UTIL_Format(buffer, sizeof(buffer), "  [%02d]", plnum);
ds@192
   754
ds@346
   755
						plname = IS_STR_FILLED(plapi->GetName()) ? plapi->GetName() : pl->m_File.c_str();
ds@346
   756
						len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " %s", plname);
ds@346
   757
ds@346
   758
						if (IS_STR_FILLED(plapi->GetVersion()))
ds@303
   759
						{
ds@346
   760
							len += UTIL_Format(&buffer[len], sizeof(buffer)-len, " (%s)", plapi->GetVersion());
ds@346
   761
						}
ds@346
   762
						if (IS_STR_FILLED(plapi->GetAuthor()))
ds@346
   763
						{
ds@346
   764
							UTIL_Format(&buffer[len], sizeof(buffer)-len, " by %s", plapi->GetAuthor());
ds@303
   765
						}
ds@303
   766
ds@346
   767
						CLIENT_CONMSG(client, "%s\n", buffer);
ds@346
   768
					}
ds@346
   769
				}
ds@192
   770
ds@346
   771
				if (!plnum)
ds@346
   772
				{
ds@346
   773
					CLIENT_CONMSG(client, "No active plugins loaded.\n");
ds@192
   774
				}
ds@303
   775
ds@303
   776
				RETURN_META(MRES_SUPERCEDE);
ds@192
   777
			}
ds@192
   778
		}
ds@303
   779
ds@303
   780
		CLIENT_CONMSG(client, "Metamod:Source Menu\n");
ds@303
   781
		CLIENT_CONMSG(client, "usage: meta <command>\n");
ds@303
   782
		CLIENT_CONMSG(client, "  credits - About Metamod:Source\n");
ds@303
   783
		CLIENT_CONMSG(client, "  list    - List plugins\n");
ds@303
   784
		CLIENT_CONMSG(client, "  version - Version information\n");
ds@192
   785
ds@192
   786
		RETURN_META(MRES_SUPERCEDE);
ds@192
   787
	}
ds@192
   788
ds@192
   789
	RETURN_META(MRES_IGNORED);
ds@192
   790
}
ds@192
   791
dvander@89
   792
const char *GetPluginsFile()
dvander@89
   793
{
dvander@89
   794
	return mm_pluginsfile.GetString();
dvander@89
   795
}