Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file was 11692, checked in by landauf, 6 years ago

with the latest CMake there are a lot of warnings about the usage of CMP0026. therefore the naming and content of the module/plugin-files had to be changed:
old: <library-filename>.module with content <target-name>
new: <target-name>.module with content <library-filename>
this seems to comply better with cmake and works equally well in c++ (with some small adjustments).
reference: https://cmake.org/cmake/help/v3.0/policy/CMP0026.html

  • Property svn:eol-style set to native
File size: 5.3 KB
RevLine 
[10552]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 "SpecialConfig.h"
32#include "Plugin.h"
[10580]33#include "PluginReference.h"
[11016]34#include "core/CoreIncludes.h"
[10552]35#include "core/ApplicationPaths.h"
36#include "core/command/ConsoleCommandIncludes.h"
[11016]37#include "core/config/ConfigValueIncludes.h"
[10580]38#include "core/object/Context.h"
[10552]39
[11016]40#ifdef DO_NOT_UNLOAD_PLUGINS
41#  define MERELY_DEACTIVATE_PLUGINS true
42#else
43#  define MERELY_DEACTIVATE_PLUGINS false
44#endif
45
[10552]46namespace orxonox
47{
[10580]48    static const std::string __CC_PluginManager_load_name  = "load";
49    static const std::string __CC_PluginManager_unload_name  = "unload";
[10552]50
[10580]51    SetConsoleCommand("PluginManager", __CC_PluginManager_load_name, &PluginManager::loadPlugin);
52    SetConsoleCommand("PluginManager", __CC_PluginManager_unload_name, &PluginManager::unloadPlugin);
53
[11071]54    PluginManager* PluginManager::singletonPtr_s  = nullptr;
[10552]55
[11016]56    RegisterAbstractClass(PluginManager).inheritsFrom<Configurable>();
57
[10552]58    PluginManager::PluginManager()
59    {
[11016]60        RegisterObject(PluginManager);
61
[10580]62        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(this);
63        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(this);
[11016]64
65        this->setConfigValues();
[10552]66    }
67
68    PluginManager::~PluginManager()
69    {
[11071]70        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(nullptr);
71        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(nullptr);
[10580]72
[11071]73        for (const auto& mapEntry : this->references_)
74            delete mapEntry.second;
75        for (const auto& mapEntry : this->plugins_)
76            delete mapEntry.second;
[10552]77    }
78
[11016]79    void PluginManager::setConfigValues()
80    {
[11017]81        SetConfigValue(bMerelyDeactivatePlugins_, MERELY_DEACTIVATE_PLUGINS).callback(this, &PluginManager::changedConfigValue);
[11016]82    }
83
[11017]84    void PluginManager::changedConfigValue()
85    {
86        if (this->bMerelyDeactivatePlugins_)
87        {
88            orxout(internal_warning) << "Orxonox is configured to NOT completely unload plugins."
89                " This means that it's not possible to re-compile and reload a plugin at runtime." << endl;
90        }
91    }
92
[10552]93    void PluginManager::findPlugins()
94    {
[11692]95        const std::map<std::string, std::string>& pluginPaths = ApplicationPaths::getInstance().getPluginPaths();
96        for (const std::pair<std::string, std::string>& pluginPath : pluginPaths)
[10552]97        {
[11692]98            const std::string& name = pluginPath.first;
99            const std::string& libraryName = pluginPath.second;
100
101            orxout(internal_info) << "Found plugin with name '" << name << "' in module " << libraryName << endl;
102            this->plugins_[name] = new Plugin(name, libraryName);
[10552]103        }
104    }
105
[10580]106    void PluginManager::referencePlugin(const std::string& name)
[10552]107    {
[10580]108        Plugin* plugin = this->plugins_[name];
[11071]109        if (plugin != nullptr)
[11014]110            plugin->reference();
[10552]111        else
112            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
113    }
114
[10580]115    void PluginManager::dereferencePlugin(const std::string& name)
[10552]116    {
[10580]117        Plugin* plugin = this->plugins_[name];
[11071]118        if (plugin != nullptr)
[11016]119            plugin->dereference(this->bMerelyDeactivatePlugins_);
[10552]120        else
121            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
122    }
[10580]123
124    /**
125     * @brief Console command to manually load a plugin. The plugin stays loaded until @ref unloadPlugin is called.
126     */
127    void PluginManager::loadPlugin(const std::string& name)
128    {
[11071]129        if (this->references_[name] == nullptr)
[10580]130        {
131            this->references_[name] = new PluginReference(name);
132        }
133        else
134            orxout(internal_warning) << "Plugin " << name << " is already loaded" << endl;
135    }
136
137    /**
138     * @brief Console command to unload a plugin if it was previously loaded manually by calling @ref loadPlugin.
139     * Does not unload the plugin immediately if it is still used by another @ref PluginReference (e.g. by a @ref Level).
140     */
141    void PluginManager::unloadPlugin(const std::string& name)
142    {
143        PluginReference* reference = this->references_[name];
[11071]144        if (reference != nullptr)
[10580]145        {
[11071]146            this->references_[name] = nullptr;
[10580]147            delete reference;
148        }
149        else
150            orxout(internal_warning) << "Plugin " << name << " is already unloaded" << endl;
151    }
[10552]152}
Note: See TracBrowser for help on using the repository browser.