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