added amb374, reload plugin command
authorBorja Ferrer <faluco@alliedmods.net>
Sat Aug 18 17:15:22 2007 +0000 (2007-08-18)
changeset 1348ad1199b211ac
parent 1347 b1e02bc27316
child 1349 0d84046ec139
added amb374, reload plugin command
core/systems/PluginSys.cpp
core/systems/PluginSys.h
     1.1 --- a/core/systems/PluginSys.cpp	Sat Aug 18 14:14:48 2007 +0000
     1.2 +++ b/core/systems/PluginSys.cpp	Sat Aug 18 17:15:22 2007 +0000
     1.3 @@ -2169,6 +2169,50 @@
     1.4  				g_RootMenu.ConsolePrint("[SM] Plugin returned error: %s", error);
     1.5  				return;
     1.6  			}
     1.7 +		} else if (strcmp(cmd, "reload") == 0) {
     1.8 +			if (argcount < 4)
     1.9 +			{
    1.10 +				g_RootMenu.ConsolePrint("[SM] Usage: sm plugins reload <#|file>");
    1.11 +				return;
    1.12 +			}
    1.13 +
    1.14 +			CPlugin *pl;
    1.15 +			char *end;
    1.16 +			const char *arg = g_RootMenu.GetArgument(3);
    1.17 +			int id = strtol(arg, &end, 10);
    1.18 +
    1.19 +			if (*end == '\0')
    1.20 +			{
    1.21 +				pl = GetPluginByOrder(id);
    1.22 +				if (!pl)
    1.23 +				{
    1.24 +					g_RootMenu.ConsolePrint("[SM] Plugin index %d not found.", id);
    1.25 +					return;
    1.26 +				}
    1.27 +			} else {
    1.28 +				char pluginfile[256];
    1.29 +				const char *ext = g_LibSys.GetFileExtension(arg) ? "" : ".smx";
    1.30 +				UTIL_Format(pluginfile, sizeof(pluginfile), "%s%s", arg, ext);
    1.31 +
    1.32 +				if (!sm_trie_retrieve(m_LoadLookup, pluginfile, (void **)&pl))
    1.33 +				{
    1.34 +					g_RootMenu.ConsolePrint("[SM] Plugin %s is not loaded.", pluginfile);
    1.35 +					return;
    1.36 +				}
    1.37 +			}
    1.38 +
    1.39 +			char name[PLATFORM_MAX_PATH];
    1.40 +			const sm_plugininfo_t *info = pl->GetPublicInfo();
    1.41 +			strcpy(name, (IS_STR_FILLED(info->name)) ? info->name : pl->GetFilename());
    1.42 +
    1.43 +			if (ReloadPlugin(pl))
    1.44 +			{
    1.45 +				g_RootMenu.ConsolePrint("[SM] Plugin %s reloaded successfully.", name);
    1.46 +			} else {
    1.47 +				g_RootMenu.ConsolePrint("[SM] Failed to reload plugin %s.", name);
    1.48 +			}
    1.49 +
    1.50 +			return;
    1.51  		}
    1.52  	}
    1.53  
    1.54 @@ -2179,6 +2223,53 @@
    1.55  	g_RootMenu.DrawGenericOption("unload", "Unload a plugin");
    1.56  	g_RootMenu.DrawGenericOption("info", "Information about a plugin");
    1.57  	g_RootMenu.DrawGenericOption("debug", "Toggle debug mode on a plugin");
    1.58 +	g_RootMenu.DrawGenericOption("reload", "Reloads a plugin");
    1.59 +}
    1.60 +
    1.61 +bool CPluginManager::ReloadPlugin(CPlugin *pl)
    1.62 +{
    1.63 +	List<CPlugin *>::iterator iter;
    1.64 +	char filename[PLATFORM_MAX_PATH];
    1.65 +	bool debug, wasloaded;
    1.66 +	PluginType ptype;
    1.67 +	IPlugin *newpl;
    1.68 +	int id = 1;
    1.69 +
    1.70 +	strcpy(filename, pl->m_filename);
    1.71 +	debug = pl->IsDebugging();
    1.72 +	ptype = pl->GetType();
    1.73 +
    1.74 +	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++, id++)
    1.75 +	{
    1.76 +		if ((*iter) == pl)
    1.77 +		{
    1.78 +			break;
    1.79 +		}
    1.80 +	}
    1.81 +
    1.82 +	if (!UnloadPlugin(pl))
    1.83 +	{
    1.84 +		return false;
    1.85 +	}
    1.86 +	if (!(newpl=LoadPlugin(filename, debug, ptype, NULL, 0, &wasloaded)))
    1.87 +	{
    1.88 +		return false;
    1.89 +	}
    1.90 +
    1.91 +	for (iter=m_plugins.begin(); iter!=m_plugins.end(); iter++)
    1.92 +	{
    1.93 +		if ((*iter) == (CPlugin *)newpl)
    1.94 +		{
    1.95 +			m_plugins.erase(iter);
    1.96 +			break;
    1.97 +		}
    1.98 +	}
    1.99 +
   1.100 +	int i;
   1.101 +	for (i=1, iter=m_plugins.begin(); iter!=m_plugins.end() && i<id; iter++, i++) {}
   1.102 +	m_plugins.insert(iter, (CPlugin *)newpl);
   1.103 +
   1.104 +	return true;
   1.105  }
   1.106  
   1.107  void CPluginManager::ReloadOrUnloadPlugins()
     2.1 --- a/core/systems/PluginSys.h	Sat Aug 18 14:14:48 2007 +0000
     2.2 +++ b/core/systems/PluginSys.h	Sat Aug 18 17:15:22 2007 +0000
     2.3 @@ -429,6 +429,8 @@
     2.4  	void OnLibraryAction(const char *lib, bool drop);
     2.5  
     2.6  	bool LibraryExists(const char *lib);
     2.7 +
     2.8 +	bool ReloadPlugin(CPlugin *pl);
     2.9  private:
    2.10  	LoadRes _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength);
    2.11