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