Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v2/src/libraries/core/module/PluginManager.cc @ 10916

Last change on this file since 10916 was 10916, checked in by landauf, 10 years ago

use actual types instead of 'auto'. only exception is for complicated template types, e.g. when iterating over a map

  • Property svn:eol-style set to native
File size: 4.6 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/ApplicationPaths.h"
37#include "core/command/ConsoleCommandIncludes.h"
38#include "core/object/Context.h"
39
40namespace orxonox
41{
42    static const std::string __CC_PluginManager_load_name  = "load";
43    static const std::string __CC_PluginManager_unload_name  = "unload";
44
45    SetConsoleCommand("PluginManager", __CC_PluginManager_load_name, &PluginManager::loadPlugin);
46    SetConsoleCommand("PluginManager", __CC_PluginManager_unload_name, &PluginManager::unloadPlugin);
47
48    PluginManager* PluginManager::singletonPtr_s  = nullptr;
49
50    PluginManager::PluginManager()
51    {
52        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(this);
53        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(this);
54    }
55
56    PluginManager::~PluginManager()
57    {
58        ModifyConsoleCommand("PluginManager", __CC_PluginManager_load_name).setObject(nullptr);
59        ModifyConsoleCommand("PluginManager", __CC_PluginManager_unload_name).setObject(nullptr);
60
61        for (auto& mapEntry : this->references_)
62            delete mapEntry.second;
63        for (auto& mapEntry : this->plugins_)
64            delete mapEntry.second;
65    }
66
67    void PluginManager::findPlugins()
68    {
69        const std::vector<std::string>& pluginPaths = ApplicationPaths::getInstance().getPluginPaths();
70        for (const std::string& libraryName : pluginPaths)
71        {
72            std::string name;
73            std::string filename = libraryName +  + specialConfig::pluginExtension;
74            std::ifstream infile(filename.c_str());
75            if (infile >> name)
76            {
77                orxout(internal_info) << "Found plugin with name '" << name << "' in module " << libraryName << endl;
78                this->plugins_[name] = new Plugin(name, libraryName);
79            }
80            else
81            {
82                orxout(internal_warning) << "Could not read plugin file " << filename << endl;
83            }
84        }
85    }
86
87    void PluginManager::referencePlugin(const std::string& name)
88    {
89        Plugin* plugin = this->plugins_[name];
90        if (plugin != nullptr)
91            plugin->load();
92        else
93            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
94    }
95
96    void PluginManager::dereferencePlugin(const std::string& name)
97    {
98        Plugin* plugin = this->plugins_[name];
99        if (plugin != nullptr)
100            plugin->unload();
101        else
102            orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
103    }
104
105    /**
106     * @brief Console command to manually load a plugin. The plugin stays loaded until @ref unloadPlugin is called.
107     */
108    void PluginManager::loadPlugin(const std::string& name)
109    {
110        if (this->references_[name] == nullptr)
111        {
112            this->references_[name] = new PluginReference(name);
113        }
114        else
115            orxout(internal_warning) << "Plugin " << name << " is already loaded" << endl;
116    }
117
118    /**
119     * @brief Console command to unload a plugin if it was previously loaded manually by calling @ref loadPlugin.
120     * Does not unload the plugin immediately if it is still used by another @ref PluginReference (e.g. by a @ref Level).
121     */
122    void PluginManager::unloadPlugin(const std::string& name)
123    {
124        PluginReference* reference = this->references_[name];
125        if (reference != nullptr)
126        {
127            this->references_[name] = nullptr;
128            delete reference;
129        }
130        else
131            orxout(internal_warning) << "Plugin " << name << " is already unloaded" << endl;
132    }
133}
Note: See TracBrowser for help on using the repository browser.