| 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" | 
|---|
| 34 | #include "StaticInitializationManager.h" | 
|---|
| 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; | 
|---|
| 41 |         this->moduleInstance_ = NULL; | 
|---|
| 42 |     } | 
|---|
| 43 |  | 
|---|
| 44 |     Plugin::~Plugin() | 
|---|
| 45 |     { | 
|---|
| 46 |         // force unloading of the module when the plugin is destroyed | 
|---|
| 47 |         if (this->moduleInstance_ != NULL) | 
|---|
| 48 |             this->unloadModule(); | 
|---|
| 49 |     } | 
|---|
| 50 |  | 
|---|
| 51 |     void Plugin::reference() | 
|---|
| 52 |     { | 
|---|
| 53 |         this->referenceCounter_++; | 
|---|
| 54 |         if (this->referenceCounter_ == 1) // increased from 0 to 1 -> load plugin | 
|---|
| 55 |             this->load(); | 
|---|
| 56 |         else | 
|---|
| 57 |             orxout(internal_info) << "Increased reference count for plugin " << this->name_ << " to " << this->referenceCounter_ << endl; | 
|---|
| 58 |     } | 
|---|
| 59 |  | 
|---|
| 60 |     void Plugin::dereference(bool bMerelyDeactivate) | 
|---|
| 61 |     { | 
|---|
| 62 |         if (this->referenceCounter_ == 0) | 
|---|
| 63 |         { | 
|---|
| 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 | 
|---|
| 70 |             this->unload(bMerelyDeactivate); | 
|---|
| 71 |         else | 
|---|
| 72 |             orxout(internal_info) << "Reduced reference count for plugin " << this->name_ << " to " << this->referenceCounter_ << endl; | 
|---|
| 73 |     } | 
|---|
| 74 |  | 
|---|
| 75 |     ////////////////////////////////////////// | 
|---|
| 76 |     //                 LOAD                 // | 
|---|
| 77 |     ////////////////////////////////////////// | 
|---|
| 78 |     void Plugin::load() | 
|---|
| 79 |     { | 
|---|
| 80 |         // only load module if it isn't already loaded. otherwise merely activate it. | 
|---|
| 81 |         if (this->moduleInstance_ == NULL) | 
|---|
| 82 |             this->loadModule(); | 
|---|
| 83 |         else | 
|---|
| 84 |             this->activateModule(); | 
|---|
| 85 |     } | 
|---|
| 86 |     void Plugin::loadModule() | 
|---|
| 87 |     { | 
|---|
| 88 |         orxout(internal_info) << "Loading plugin " << this->name_ << "..." << endl; | 
|---|
| 89 |         this->moduleInstance_ = new ModuleInstance(this->libraryName_); | 
|---|
| 90 |         Core::getInstance().loadModule(this->moduleInstance_); | 
|---|
| 91 |     } | 
|---|
| 92 |     void Plugin::activateModule() | 
|---|
| 93 |     { | 
|---|
| 94 |         orxout(internal_info) << "Activating plugin " << this->name_ << "..." << endl; | 
|---|
| 95 |         StaticInitializationManager::getInstance().loadModule(this->moduleInstance_); | 
|---|
| 96 |     } | 
|---|
| 97 |  | 
|---|
| 98 |     ////////////////////////////////////////// | 
|---|
| 99 |     //                UNLOAD                // | 
|---|
| 100 |     ////////////////////////////////////////// | 
|---|
| 101 |     void Plugin::unload(bool bMerelyDeactivate) | 
|---|
| 102 |     { | 
|---|
| 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 |     { | 
|---|
| 111 |         orxout(internal_info) << "Unloading plugin " << this->name_ << "..." << endl; | 
|---|
| 112 |         Core::getInstance().unloadModule(this->moduleInstance_); | 
|---|
| 113 |         delete this->moduleInstance_; | 
|---|
| 114 |         this->moduleInstance_ = NULL; | 
|---|
| 115 |     } | 
|---|
| 116 |     void Plugin::deactivateModule() | 
|---|
| 117 |     { | 
|---|
| 118 |         orxout(internal_info) << "Deactivating plugin " << this->name_ << "..." << endl; | 
|---|
| 119 |         StaticInitializationManager::getInstance().unloadModule(this->moduleInstance_); | 
|---|
| 120 |     } | 
|---|
| 121 | } | 
|---|