fixed plugins being loaded twice if they were loaded previously

--HG--
extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40389
This commit is contained in:
Borja Ferrer 2007-01-27 00:03:34 +00:00
parent 326ac67c68
commit 21123f4c74
3 changed files with 45 additions and 17 deletions

View File

@ -634,7 +634,7 @@ void CPluginManager::LoadPluginsFromDir(const char *basedir, const char *localpa
} }
//well i have discovered that gabe newell is very fat, so i wrote this comment now //well i have discovered that gabe newell is very fat, so i wrote this comment now
bool CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t err_max) LoadRes CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug, PluginType type, char error[], size_t err_max)
{ {
/** /**
* Does this plugin already exist? * Does this plugin already exist?
@ -642,19 +642,18 @@ bool CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug
CPlugin *pPlugin; CPlugin *pPlugin;
if (sm_trie_retrieve(m_LoadLookup, path, (void **)&pPlugin)) if (sm_trie_retrieve(m_LoadLookup, path, (void **)&pPlugin))
{ {
/* First check the type */
PluginType type = pPlugin->GetType();
if (type == PluginType_Private
|| type == PluginType_Global)
{
return true;
}
/* Check to see if we should try reloading it */ /* Check to see if we should try reloading it */
if (pPlugin->GetStatus() == Plugin_BadLoad if (pPlugin->GetStatus() == Plugin_BadLoad
|| pPlugin->GetStatus() == Plugin_Error || pPlugin->GetStatus() == Plugin_Error
|| pPlugin->GetStatus() == Plugin_Failed) || pPlugin->GetStatus() == Plugin_Failed)
{ {
UnloadPlugin(pPlugin); UnloadPlugin(pPlugin);
} else {
if (_plugin)
{
*_plugin = pPlugin;
}
return LoadRes_AlreadyLoaded;
} }
} }
@ -732,19 +731,27 @@ bool CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug
*_plugin = pPlugin; *_plugin = pPlugin;
} }
return (pPlugin->GetStatus() == Plugin_Loaded); return (pPlugin->GetStatus() == Plugin_Loaded) ? LoadRes_Successful : LoadRes_Failure;
} }
IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType type, char error[], size_t err_max) IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType type, char error[], size_t err_max, bool *wasloaded)
{ {
CPlugin *pl; CPlugin *pl;
LoadRes res;
if (!_LoadPlugin(&pl, path, debug, type, error, err_max)) *wasloaded = false;
if ((res=_LoadPlugin(&pl, path, debug, type, error, err_max)) == LoadRes_Failure)
{ {
delete pl; delete pl;
return NULL; return NULL;
} }
if (res == LoadRes_AlreadyLoaded)
{
*wasloaded = true;
return pl;
}
AddPlugin(pl); AddPlugin(pl);
/* Run second pass if we need to */ /* Run second pass if we need to */
@ -763,15 +770,19 @@ IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType typ
void CPluginManager::LoadAutoPlugin(const char *plugin) void CPluginManager::LoadAutoPlugin(const char *plugin)
{ {
CPlugin *pl; CPlugin *pl;
LoadRes res;
char error[255] = "Unknown error"; char error[255] = "Unknown error";
if (!_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) if ((res=_LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure)
{ {
g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error); g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s", plugin, error);
pl->SetErrorState(Plugin_Failed, "%s", error); pl->SetErrorState(Plugin_Failed, "%s", error);
} }
AddPlugin(pl); if (res == LoadRes_Successful)
{
AddPlugin(pl);
}
} }
void CPluginManager::AddPlugin(CPlugin *pPlugin) void CPluginManager::AddPlugin(CPlugin *pPlugin)
@ -1410,8 +1421,15 @@ void CPluginManager::OnRootConsoleCommand(const char *command, unsigned int argc
} }
char error[128]; char error[128];
bool wasloaded;
const char *filename = g_RootMenu.GetArgument(3); const char *filename = g_RootMenu.GetArgument(3);
IPlugin *pl = LoadPlugin(filename, false, PluginType_MapUpdated, error, sizeof(error)); IPlugin *pl = LoadPlugin(filename, false, PluginType_MapUpdated, error, sizeof(error), &wasloaded);
if (wasloaded)
{
g_RootMenu.ConsolePrint("[SM] Plugin %s is already loaded.", filename);
return;
}
if (pl) if (pl)
{ {

View File

@ -89,6 +89,13 @@ struct ContextPair
IVirtualMachine *vm; IVirtualMachine *vm;
}; };
enum LoadRes
{
LoadRes_Successful,
LoadRes_AlreadyLoaded,
LoadRes_Failure
};
class CPlugin : public IPlugin class CPlugin : public IPlugin
{ {
friend class CPluginManager; friend class CPluginManager;
@ -235,7 +242,8 @@ public: //IPluginManager
bool debug, bool debug,
PluginType type, PluginType type,
char error[], char error[],
size_t err_max); size_t err_max,
bool *wasloaded);
bool UnloadPlugin(IPlugin *plugin); bool UnloadPlugin(IPlugin *plugin);
IPlugin *FindPluginByContext(const sp_context_t *ctx); IPlugin *FindPluginByContext(const sp_context_t *ctx);
unsigned int GetPluginCount(); unsigned int GetPluginCount();
@ -302,7 +310,7 @@ public:
void ReloadOrUnloadPlugins(); void ReloadOrUnloadPlugins();
private: private:
bool _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t err_max); LoadRes _LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t err_max);
void LoadAutoPlugin(const char *plugin); void LoadAutoPlugin(const char *plugin);

View File

@ -251,13 +251,15 @@ namespace SourceMod
* @param type Lifetime of the plugin. * @param type Lifetime of the plugin.
* @param error Buffer to hold any error message. * @param error Buffer to hold any error message.
* @param err_max Maximum length of error message buffer. * @param err_max Maximum length of error message buffer.
* @param wasloaded Stores if the plugin is already loaded.
* @return A new plugin pointer on success, false otherwise. * @return A new plugin pointer on success, false otherwise.
*/ */
virtual IPlugin *LoadPlugin(const char *path, virtual IPlugin *LoadPlugin(const char *path,
bool debug, bool debug,
PluginType type, PluginType type,
char error[], char error[],
size_t err_max) =0; size_t err_max,
bool *wasloaded) =0;
/** /**
* @brief Attempts to unload a plugin. * @brief Attempts to unload a plugin.