[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 "Plugin.h" |
---|
| 30 | |
---|
| 31 | #include "ModuleInstance.h" |
---|
| 32 | #include "util/Output.h" |
---|
| 33 | #include "core/Core.h" |
---|
[11015] | 34 | #include "StaticInitializationManager.h" |
---|
[10552] | 35 | |
---|
| 36 | namespace orxonox |
---|
| 37 | { |
---|
| 38 | Plugin::Plugin(const std::string& name, const std::string& libraryName) : name_(name), libraryName_(libraryName) |
---|
| 39 | { |
---|
| 40 | this->referenceCounter_ = 0; |
---|
[11071] | 41 | this->moduleInstance_ = nullptr; |
---|
[10552] | 42 | } |
---|
| 43 | |
---|
| 44 | Plugin::~Plugin() |
---|
| 45 | { |
---|
[11015] | 46 | // force unloading of the module when the plugin is destroyed |
---|
[11071] | 47 | if (this->moduleInstance_ != nullptr) |
---|
[11015] | 48 | this->unloadModule(); |
---|
[10552] | 49 | } |
---|
| 50 | |
---|
[11014] | 51 | void Plugin::reference() |
---|
[10552] | 52 | { |
---|
[10553] | 53 | this->referenceCounter_++; |
---|
| 54 | if (this->referenceCounter_ == 1) // increased from 0 to 1 -> load plugin |
---|
[11014] | 55 | this->load(); |
---|
[10552] | 56 | else |
---|
[10553] | 57 | orxout(internal_info) << "Increased reference count for plugin " << this->name_ << " to " << this->referenceCounter_ << endl; |
---|
[10552] | 58 | } |
---|
| 59 | |
---|
[11015] | 60 | void Plugin::dereference(bool bMerelyDeactivate) |
---|
[10552] | 61 | { |
---|
| 62 | if (this->referenceCounter_ == 0) |
---|
| 63 | { |
---|
[10553] | 64 | orxout(internal_warning) << "Tried to dereference plugin " << this->name_ << " more often than it was referenced" << endl; |
---|
| 65 | return; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | this->referenceCounter_--; |
---|
| 69 | if (this->referenceCounter_ == 0) // reduced from 1 to 0 -> load plugin |
---|
[11015] | 70 | this->unload(bMerelyDeactivate); |
---|
[10552] | 71 | else |
---|
[10553] | 72 | orxout(internal_info) << "Reduced reference count for plugin " << this->name_ << " to " << this->referenceCounter_ << endl; |
---|
[10552] | 73 | } |
---|
[11014] | 74 | |
---|
[11015] | 75 | ////////////////////////////////////////// |
---|
| 76 | // LOAD // |
---|
| 77 | ////////////////////////////////////////// |
---|
[11014] | 78 | void Plugin::load() |
---|
| 79 | { |
---|
[11015] | 80 | // only load module if it isn't already loaded. otherwise merely activate it. |
---|
[11071] | 81 | if (this->moduleInstance_ == nullptr) |
---|
[11015] | 82 | this->loadModule(); |
---|
| 83 | else |
---|
| 84 | this->activateModule(); |
---|
| 85 | } |
---|
| 86 | void Plugin::loadModule() |
---|
| 87 | { |
---|
[11014] | 88 | orxout(internal_info) << "Loading plugin " << this->name_ << "..." << endl; |
---|
| 89 | this->moduleInstance_ = new ModuleInstance(this->libraryName_); |
---|
| 90 | Core::getInstance().loadModule(this->moduleInstance_); |
---|
| 91 | } |
---|
[11015] | 92 | void Plugin::activateModule() |
---|
| 93 | { |
---|
| 94 | orxout(internal_info) << "Activating plugin " << this->name_ << "..." << endl; |
---|
| 95 | StaticInitializationManager::getInstance().loadModule(this->moduleInstance_); |
---|
| 96 | } |
---|
[11014] | 97 | |
---|
[11015] | 98 | ////////////////////////////////////////// |
---|
| 99 | // UNLOAD // |
---|
| 100 | ////////////////////////////////////////// |
---|
| 101 | void Plugin::unload(bool bMerelyDeactivate) |
---|
[11014] | 102 | { |
---|
[11015] | 103 | // fully unload the module unless otherwise requested. |
---|
| 104 | if (bMerelyDeactivate) |
---|
| 105 | this->deactivateModule(); |
---|
| 106 | else |
---|
| 107 | this->unloadModule(); |
---|
| 108 | } |
---|
| 109 | void Plugin::unloadModule() |
---|
| 110 | { |
---|
[11014] | 111 | orxout(internal_info) << "Unloading plugin " << this->name_ << "..." << endl; |
---|
| 112 | Core::getInstance().unloadModule(this->moduleInstance_); |
---|
| 113 | delete this->moduleInstance_; |
---|
[11071] | 114 | this->moduleInstance_ = nullptr; |
---|
[11014] | 115 | } |
---|
[11015] | 116 | void Plugin::deactivateModule() |
---|
| 117 | { |
---|
| 118 | orxout(internal_info) << "Deactivating plugin " << this->name_ << "..." << endl; |
---|
| 119 | StaticInitializationManager::getInstance().unloadModule(this->moduleInstance_); |
---|
| 120 | } |
---|
[10552] | 121 | } |
---|