core/sm_srvcmds.cpp
changeset 216 38b1bdbc5bc9
child 217 e61c933190a2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/core/sm_srvcmds.cpp	Sat Dec 16 02:16:21 2006 +0000
     1.3 @@ -0,0 +1,171 @@
     1.4 +#include "sm_srvcmds.h"
     1.5 +
     1.6 +ConVarAccessor g_ConCmdAccessor;
     1.7 +
     1.8 +void ConVarAccessor::OnSourceModStartup(bool late)
     1.9 +{
    1.10 +	ConCommandBaseMgr::OneTimeInit(&g_ConCmdAccessor);
    1.11 +}
    1.12 +
    1.13 +bool ConVarAccessor::RegisterConCommandBase(ConCommandBase *pCommand)
    1.14 +{
    1.15 +	META_REGCVAR(pCommand);
    1.16 +
    1.17 +	return true;
    1.18 +}
    1.19 +
    1.20 +CON_COMMAND(sm, "SourceMod Menu")
    1.21 +{
    1.22 +	int argnum = engine->Cmd_Argc();
    1.23 +
    1.24 +	if (argnum >= 2)
    1.25 +	{
    1.26 +		const char *cmd = engine->Cmd_Argv(1);
    1.27 +		if (!strcmp("plugins", cmd))
    1.28 +		{
    1.29 +			if (argnum >= 3)
    1.30 +			{
    1.31 +				const char *cmd2 = engine->Cmd_Argv(2);
    1.32 +				if (!strcmp("list", cmd2))
    1.33 +				{
    1.34 +					char buffer[256];
    1.35 +					unsigned int id = 1;
    1.36 +					int plnum = g_PluginSys.GetPluginCount();
    1.37 +
    1.38 +					if (!plnum)
    1.39 +					{
    1.40 +						META_CONPRINT("[SM] No plugins loaded\n");
    1.41 +						return;
    1.42 +					} else {
    1.43 +						META_CONPRINTF("[SM] Displaying %d plugin%s:\n", g_PluginSys.GetPluginCount(), (plnum > 1) ? "s" : "");
    1.44 +					}
    1.45 +
    1.46 +					IPluginIterator *iter = g_PluginSys.GetPluginIterator();
    1.47 +					for (; iter->MorePlugins(); iter->NextPlugin(), id++)
    1.48 +					{
    1.49 +						assert(iter->GetPlugin()->GetStatus() != Plugin_Created);
    1.50 +						int len = 0;
    1.51 +						const sm_plugininfo_t *info = iter->GetPlugin()->GetPublicInfo();
    1.52 +
    1.53 +						len += snprintf(&buffer[len], sizeof(buffer)-len, "  %02d <%s>", id, "status"); //:TODO: status
    1.54 +						len += snprintf(&buffer[len], sizeof(buffer)-len, " \"%s\"", (info->name) ? info->name : iter->GetPlugin()->GetFilename());
    1.55 +						if (info->version)
    1.56 +						{
    1.57 +							len += snprintf(&buffer[len], sizeof(buffer)-len, " (%s)", info->version);
    1.58 +						}
    1.59 +						if (info->author)
    1.60 +						{
    1.61 +							snprintf(&buffer[len], sizeof(buffer)-len, " by %s", info->author);
    1.62 +						}
    1.63 +						META_CONPRINTF("%s\n", buffer);
    1.64 +					}
    1.65 +
    1.66 +					iter->Release();
    1.67 +					return;
    1.68 +				} else if (!strcmp("load", cmd2)) {
    1.69 +					if (argnum < 4)
    1.70 +					{
    1.71 +						META_CONPRINT("Usage: sm plugins load <file>\n");
    1.72 +						return;
    1.73 +					}
    1.74 +
    1.75 +					char error[100];
    1.76 +					const char *filename = engine->Cmd_Argv(3);
    1.77 +					IPlugin *pl = g_PluginSys.LoadPlugin(filename, false, PluginType_MapUpdated, error, sizeof(error));
    1.78 +
    1.79 +					if (pl)
    1.80 +					{
    1.81 +						META_CONPRINTF("Loaded plugin %s successfully.\n", filename);
    1.82 +					} else {
    1.83 +						META_CONPRINTF("Plugin %s failed to load: %s.\n", filename, error);
    1.84 +					}
    1.85 +
    1.86 +					return;
    1.87 +
    1.88 +				} else if (!strcmp("unload", cmd2)) {
    1.89 +					if (argnum < 4)
    1.90 +					{
    1.91 +						META_CONPRINT("Usage: sm plugins unload <#>\n");
    1.92 +						return;
    1.93 +					}
    1.94 +
    1.95 +					IPlugin *pl = NULL;
    1.96 +					int id = 1;
    1.97 +					int num = atoi(engine->Cmd_Argv(3));
    1.98 +					if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
    1.99 +					{
   1.100 +						META_CONPRINT("Plugin index not found.\n");
   1.101 +						return;
   1.102 +					}
   1.103 +
   1.104 +					IPluginIterator *iter = g_PluginSys.GetPluginIterator();
   1.105 +					for (; iter->MorePlugins() && id<num; iter->NextPlugin(), id++) {}
   1.106 +					pl = iter->GetPlugin();
   1.107 +
   1.108 +					char name[64];
   1.109 +					const sm_plugininfo_t *info = pl->GetPublicInfo();
   1.110 +					strcpy(name, (info->name) ? info->name : pl->GetFilename());
   1.111 +
   1.112 +					if (g_PluginSys.UnloadPlugin(pl))
   1.113 +					{
   1.114 +						META_CONPRINTF("Plugin %s unloaded successfully.\n", name);
   1.115 +					} else {
   1.116 +						META_CONPRINTF("Failed to unload plugin %s.\n", name);
   1.117 +					}
   1.118 +
   1.119 +					iter->Release();
   1.120 +					return;
   1.121 +
   1.122 +				} else if (!strcmp("info", cmd2)) {
   1.123 +					if (argnum < 4)
   1.124 +					{
   1.125 +						META_CONPRINT("Usage: sm plugins info <#>\n");
   1.126 +						return;
   1.127 +					}
   1.128 +
   1.129 +					IPlugin *pl = NULL;
   1.130 +					int id = 1;
   1.131 +					int num = atoi(engine->Cmd_Argv(3));
   1.132 +					if (num < 1 || num > (int)g_PluginSys.GetPluginCount())
   1.133 +					{
   1.134 +						META_CONPRINT("Plugin index not found.\n");
   1.135 +						return;
   1.136 +					}
   1.137 +
   1.138 +					IPluginIterator *iter = g_PluginSys.GetPluginIterator();
   1.139 +					for (; iter->MorePlugins() && id<num; iter->NextPlugin(), id++) {}
   1.140 +					pl = iter->GetPlugin();
   1.141 +					const sm_plugininfo_t *info = pl->GetPublicInfo();
   1.142 +
   1.143 +					META_CONPRINTF("  Filename: %s\n", pl->GetFilename());
   1.144 +					if (info->name)
   1.145 +					{
   1.146 +						META_CONPRINTF("  Title: %s\n", info->name);
   1.147 +					}
   1.148 +					if (info->author)
   1.149 +					{
   1.150 +						META_CONPRINTF("  Author: %s\n", info->author);
   1.151 +					}
   1.152 +					if (info->version)
   1.153 +					{
   1.154 +						META_CONPRINTF("  Version: %s\n", info->version);
   1.155 +					}
   1.156 +					if (info->description)
   1.157 +					{
   1.158 +						META_CONPRINTF("  Description: %s\n", info->description);
   1.159 +					}
   1.160 +					if (info->url)
   1.161 +					{
   1.162 +						META_CONPRINTF("  URL: %s\n", info->url);
   1.163 +					}
   1.164 +					//:TODO: write if it's in debug mode, or do it inside LIST ?
   1.165 +
   1.166 +					iter->Release();
   1.167 +					return;
   1.168 +				}
   1.169 +			}
   1.170 +			//:TODO: print plugins cmd list
   1.171 +		}
   1.172 +	}
   1.173 +	//:TODO: print cmd list or something
   1.174 +}
   1.175 \ No newline at end of file