From 21123f4c741c65f0d2fc336d00cf06bf79d509e6 Mon Sep 17 00:00:00 2001 From: Borja Ferrer Date: Sat, 27 Jan 2007 00:03:34 +0000 Subject: [PATCH] fixed plugins being loaded twice if they were loaded previously --HG-- extra : convert_revision : svn%3A39bc706e-5318-0410-9160-8a85361fbb7c/trunk%40389 --- core/systems/PluginSys.cpp | 46 ++++++++++++++++++++++++++------------ core/systems/PluginSys.h | 12 ++++++++-- public/IPluginSys.h | 4 +++- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/core/systems/PluginSys.cpp b/core/systems/PluginSys.cpp index b1818118..c75fd680 100644 --- a/core/systems/PluginSys.cpp +++ b/core/systems/PluginSys.cpp @@ -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 -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? @@ -642,19 +642,18 @@ bool CPluginManager::_LoadPlugin(CPlugin **_plugin, const char *path, bool debug CPlugin *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 */ if (pPlugin->GetStatus() == Plugin_BadLoad || pPlugin->GetStatus() == Plugin_Error || pPlugin->GetStatus() == Plugin_Failed) { 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; } - 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; + 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; return NULL; } + if (res == LoadRes_AlreadyLoaded) + { + *wasloaded = true; + return pl; + } + AddPlugin(pl); /* 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) { CPlugin *pl; + LoadRes res; 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); pl->SetErrorState(Plugin_Failed, "%s", error); } - AddPlugin(pl); + if (res == LoadRes_Successful) + { + AddPlugin(pl); + } } void CPluginManager::AddPlugin(CPlugin *pPlugin) @@ -1410,8 +1421,15 @@ void CPluginManager::OnRootConsoleCommand(const char *command, unsigned int argc } char error[128]; + bool wasloaded; 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) { diff --git a/core/systems/PluginSys.h b/core/systems/PluginSys.h index f463dade..1cbb05cf 100644 --- a/core/systems/PluginSys.h +++ b/core/systems/PluginSys.h @@ -89,6 +89,13 @@ struct ContextPair IVirtualMachine *vm; }; +enum LoadRes +{ + LoadRes_Successful, + LoadRes_AlreadyLoaded, + LoadRes_Failure +}; + class CPlugin : public IPlugin { friend class CPluginManager; @@ -235,7 +242,8 @@ public: //IPluginManager bool debug, PluginType type, char error[], - size_t err_max); + size_t err_max, + bool *wasloaded); bool UnloadPlugin(IPlugin *plugin); IPlugin *FindPluginByContext(const sp_context_t *ctx); unsigned int GetPluginCount(); @@ -302,7 +310,7 @@ public: void ReloadOrUnloadPlugins(); 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); diff --git a/public/IPluginSys.h b/public/IPluginSys.h index ea63795b..1feb5858 100644 --- a/public/IPluginSys.h +++ b/public/IPluginSys.h @@ -251,13 +251,15 @@ namespace SourceMod * @param type Lifetime of the plugin. * @param error Buffer to hold any error message. * @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. */ virtual IPlugin *LoadPlugin(const char *path, bool debug, PluginType type, char error[], - size_t err_max) =0; + size_t err_max, + bool *wasloaded) =0; /** * @brief Attempts to unload a plugin.