Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/module/PluginManager.cc @ 11016

Last change on this file since 11016 was 11016, checked in by landauf, 8 years ago

Added config value to PluginManager to define whether a dereferenced plugin should be completely unloaded (i.e. the shared library is closed) or merely deactivated (i.e. the library remains in the process, but Identifiers and other framework components are removed).
This option is true by default on tardis machines because there it seems impossible to completely unload a shared library due to the use of STB_GNU_UNIQUE which prevents that dlclose() unloads the library. This caused errors when a plugin should have been reloaded, e.g. when the pong level was restarted.

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "PluginManager.h"
30
31#include <fstream>
32
33#include "SpecialConfig.h"
34#include "Plugin.h"
35#include "PluginReference.h"
36#include "core/CoreIncludes.h"
37#include "core/ApplicationPaths.h"
38#include "core/command/ConsoleCommandIncludes.h"
39#include "core/config/ConfigValueIncludes.h"
40#include "core/object/Context.h"
41
42#ifdef DO_NOT_UNLOAD_PLUGINS
43#  define MERELY_DEACTIVATE_PLUGINS true
44#else
45#  define MERELY_DEACTIVATE_PLUGINS false
46#endif
47
48namespace orxonox
49{
50    static const std::string __CC_PluginManager_load_name  = "load";
51    static const std::string __CC_PluginManager_unload_name  = "unload";
52
53    SetConsoleCommand("PluginManager", __CC_PluginManager_load_name, &PluginManager::loadPlugin);
54    SetConsoleCommand("PluginManager", __CC_PluginManager_unload_name, &PluginManager::unloadPlugin);
55
56    PluginManager* PluginManager::singletonPtr_s  = 0;
57
58    RegisterAbstractClass(PluginManager).inheritsFrom<Configurable>();
59
60    PluginManager::PluginManager()
61    {
62        RegisterObject(PluginManager);
63
64        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(this);
65        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(this);
66
67        this->setConfigValues();
68    }
69
70    PluginManager::~PluginManager()
71    {
72        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(NULL);
73        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(NULL);
74
75        for (std::map<std::string, PluginReference*>::iterator it = this->references_.begin(); it != this->references_.end(); ++it)
76            delete it->second;
77        for (std::map<std::string, Plugin*>::iterator it = this->plugins_.begin(); it != this->plugins_.end(); ++it)
78            delete it->second;
79    }
80
81    void PluginManager::setConfigValues()
82    {
83        SetConfigValue(bMerelyDeactivatePlugins_, MERELY_DEACTIVATE_PLUGINS);
84    }
85
86    void PluginManager::findPlugins()
87    {
88        const std::vector<std::string>& pluginPaths = ApplicationPaths::getInstance().getPluginPaths();
89        for (std::vector<std::string>::const_iterator it = pluginPaths.begin(); it != pluginPaths.end(); ++it)
90        {
91            std::string name;
92            std::string libraryName = (*it);
93            std::string filename = libraryName +  + specialConfig::pluginExtension;
94            std::ifstream infile(filename.c_str());
95            if (infile >> name)
96            {
97                orxout(internal_info) << "Found plugin with name '" << name << "' in module " << libraryName << endl;
98                this->plugins_[name] = new Plugin(name, libraryName);
99            }
100            else
101            {
102                orxout(internal_warning) << "Could not read plugin file " << filename << endl;
103            }
104        }
105    }
106
107    void PluginManager::referencePlugin(const std::string& name)
108    {
109        Plugin* plugin = this->plugins_[name];
110        if (plugin != NULL)
111            plugin->reference();
112        else
113            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
114    }
115
116    void PluginManager::dereferencePlugin(const std::string& name)
117    {
118        Plugin* plugin = this->plugins_[name];
119        if (plugin != NULL)
120            plugin->dereference(this->bMerelyDeactivatePlugins_);
121        else
122            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
123    }
124
125    /**
126     * @brief Console command to manually load a plugin. The plugin stays loaded until @ref unloadPlugin is called.
127     */
128    void PluginManager::loadPlugin(const std::string& name)
129    {
130        if (this->references_[name] == NULL)
131        {
132            this->references_[name] = new PluginReference(name);
133        }
134        else
135            orxout(internal_warning) << "Plugin " << name << " is already loaded" << endl;
136    }
137
138    /**
139     * @brief Console command to unload a plugin if it was previously loaded manually by calling @ref loadPlugin.
140     * Does not unload the plugin immediately if it is still used by another @ref PluginReference (e.g. by a @ref Level).
141     */
142    void PluginManager::unloadPlugin(const std::string& name)
143    {
144        PluginReference* reference = this->references_[name];
145        if (reference != NULL)
146        {
147            this->references_[name] = NULL;
148            delete reference;
149        }
150        else
151            orxout(internal_warning) << "Plugin " << name << " is already unloaded" << endl;
152    }
153}
Note: See TracBrowser for help on using the repository browser.