Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/mergeFS18/src/libraries/core/module/PluginManager.cc @ 12351

Last change on this file since 12351 was 11692, checked in by landauf, 7 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
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 "SpecialConfig.h"
32#include "Plugin.h"
33#include "PluginReference.h"
34#include "core/CoreIncludes.h"
35#include "core/ApplicationPaths.h"
36#include "core/command/ConsoleCommandIncludes.h"
37#include "core/config/ConfigValueIncludes.h"
38#include "core/object/Context.h"
39
40#ifdef DO_NOT_UNLOAD_PLUGINS
41#  define MERELY_DEACTIVATE_PLUGINS true
42#else
43#  define MERELY_DEACTIVATE_PLUGINS false
44#endif
45
46namespace orxonox
47{
48    static const std::string __CC_PluginManager_load_name  = "load";
49    static const std::string __CC_PluginManager_unload_name  = "unload";
50
51    SetConsoleCommand("PluginManager", __CC_PluginManager_load_name, &PluginManager::loadPlugin);
52    SetConsoleCommand("PluginManager", __CC_PluginManager_unload_name, &PluginManager::unloadPlugin);
53
54    PluginManager* PluginManager::singletonPtr_s  = nullptr;
55
56    RegisterAbstractClass(PluginManager).inheritsFrom<Configurable>();
57
58    PluginManager::PluginManager()
59    {
60        RegisterObject(PluginManager);
61
62        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(this);
63        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(this);
64
65        this->setConfigValues();
66    }
67
68    PluginManager::~PluginManager()
69    {
70        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(nullptr);
71        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(nullptr);
72
73        for (const auto& mapEntry : this->references_)
74            delete mapEntry.second;
75        for (const auto& mapEntry : this->plugins_)
76            delete mapEntry.second;
77    }
78
79    void PluginManager::setConfigValues()
80    {
81        SetConfigValue(bMerelyDeactivatePlugins_, MERELY_DEACTIVATE_PLUGINS).callback(this, &PluginManager::changedConfigValue);
82    }
83
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
93    void PluginManager::findPlugins()
94    {
95        const std::map<std::string, std::string>& pluginPaths = ApplicationPaths::getInstance().getPluginPaths();
96        for (const std::pair<std::string, std::string>& pluginPath : pluginPaths)
97        {
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);
103        }
104    }
105
106    void PluginManager::referencePlugin(const std::string& name)
107    {
108        Plugin* plugin = this->plugins_[name];
109        if (plugin != nullptr)
110            plugin->reference();
111        else
112            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
113    }
114
115    void PluginManager::dereferencePlugin(const std::string& name)
116    {
117        Plugin* plugin = this->plugins_[name];
118        if (plugin != nullptr)
119            plugin->dereference(this->bMerelyDeactivatePlugins_);
120        else
121            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
122    }
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    {
129        if (this->references_[name] == nullptr)
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];
144        if (reference != nullptr)
145        {
146            this->references_[name] = nullptr;
147            delete reference;
148        }
149        else
150            orxout(internal_warning) << "Plugin " << name << " is already unloaded" << endl;
151    }
152}
Note: See TracBrowser for help on using the repository browser.