[Orxonox-commit 5893] r10552 - in code/branches/core7/src/libraries/core: . module
landauf at orxonox.net
landauf at orxonox.net
Thu Aug 27 23:05:18 CEST 2015
Author: landauf
Date: 2015-08-27 23:05:18 +0200 (Thu, 27 Aug 2015)
New Revision: 10552
Added:
code/branches/core7/src/libraries/core/module/Plugin.cc
code/branches/core7/src/libraries/core/module/Plugin.h
code/branches/core7/src/libraries/core/module/PluginManager.cc
code/branches/core7/src/libraries/core/module/PluginManager.h
Modified:
code/branches/core7/src/libraries/core/Core.cc
code/branches/core7/src/libraries/core/Core.h
code/branches/core7/src/libraries/core/CorePrereqs.h
code/branches/core7/src/libraries/core/module/CMakeLists.txt
Log:
added PluginManager to load/unload plugins at runtime
Modified: code/branches/core7/src/libraries/core/Core.cc
===================================================================
--- code/branches/core7/src/libraries/core/Core.cc 2015-08-27 20:50:34 UTC (rev 10551)
+++ code/branches/core7/src/libraries/core/Core.cc 2015-08-27 21:05:18 UTC (rev 10552)
@@ -75,6 +75,7 @@
#include "module/DynLibManager.h"
#include "module/ModuleInstance.h"
#include "module/StaticInitializationManager.h"
+#include "module/PluginManager.h"
#include "CoreStaticInitializationHandler.h"
#include "UpdateListener.h"
@@ -110,6 +111,7 @@
, graphicsScope_(NULL)
, bGraphicsLoaded_(false)
, staticInitHandler_(NULL)
+ , pluginManager_(NULL)
, rootModule_(NULL)
, config_(NULL)
, destructionHelper_(this)
@@ -197,6 +199,10 @@
this->staticInitHandler_->initInstances(this->rootModule_);
this->staticInitHandler_->setInitInstances(true);
+ // Create plugin manager and search for plugins
+ this->pluginManager_ = new PluginManager();
+ this->pluginManager_->findPlugins();
+
// Loader
this->loaderInstance_ = new Loader();
@@ -247,6 +253,7 @@
safeObjectDelete(&languageInstance_);
safeObjectDelete(&configFileManager_);
safeObjectDelete(&signalHandler_);
+ safeObjectDelete(&pluginManager_);
Context::getRootContext()->unregisterObject(); // unregister context from object lists - otherwise the root context would be destroyed while unloading the root module
if (this->rootModule_)
{
Modified: code/branches/core7/src/libraries/core/Core.h
===================================================================
--- code/branches/core7/src/libraries/core/Core.h 2015-08-27 20:50:34 UTC (rev 10551)
+++ code/branches/core7/src/libraries/core/Core.h 2015-08-27 21:05:18 UTC (rev 10552)
@@ -113,6 +113,7 @@
bool bGraphicsLoaded_;
CoreStaticInitializationHandler* staticInitHandler_;
+ PluginManager* pluginManager_;
ModuleInstance* rootModule_;
std::list<ModuleInstance*> modules_;
Modified: code/branches/core7/src/libraries/core/CorePrereqs.h
===================================================================
--- code/branches/core7/src/libraries/core/CorePrereqs.h 2015-08-27 20:50:34 UTC (rev 10551)
+++ code/branches/core7/src/libraries/core/CorePrereqs.h 2015-08-27 21:05:18 UTC (rev 10552)
@@ -209,6 +209,8 @@
class OgreWindowEventListener;
class OrxonoxClass;
class OrxonoxInterface;
+ class Plugin;
+ class PluginManager;
struct ResourceInfo;
template <ScopeID::Value>
class Scope;
Modified: code/branches/core7/src/libraries/core/module/CMakeLists.txt
===================================================================
--- code/branches/core7/src/libraries/core/module/CMakeLists.txt 2015-08-27 20:50:34 UTC (rev 10551)
+++ code/branches/core7/src/libraries/core/module/CMakeLists.txt 2015-08-27 21:05:18 UTC (rev 10552)
@@ -2,6 +2,8 @@
DynLib.cc
DynLibManager.cc
ModuleInstance.cc
+ Plugin.cc
+ PluginManager.cc
StaticallyInitializedInstance.cc
StaticInitializationHandlerIncludes.cc
StaticInitializationManager.cc
Added: code/branches/core7/src/libraries/core/module/Plugin.cc
===================================================================
--- code/branches/core7/src/libraries/core/module/Plugin.cc (rev 0)
+++ code/branches/core7/src/libraries/core/module/Plugin.cc 2015-08-27 21:05:18 UTC (rev 10552)
@@ -0,0 +1,88 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "Plugin.h"
+
+#include "ModuleInstance.h"
+#include "util/Output.h"
+#include "core/Core.h"
+
+namespace orxonox
+{
+ Plugin::Plugin(const std::string& name, const std::string& libraryName) : name_(name), libraryName_(libraryName)
+ {
+ this->referenceCounter_ = 0;
+ this->moduleInstance_ = NULL;
+ }
+
+ Plugin::~Plugin()
+ {
+ if (this->moduleInstance_ != NULL)
+ {
+ this->referenceCounter_ = 1; // force unloading
+ this->unload();
+ }
+ }
+
+ void Plugin::load()
+ {
+ if (this->referenceCounter_ == 0)
+ {
+ orxout(internal_info) << "Loading plugin " << this->name_ << "..." << endl;
+ this->moduleInstance_ = new ModuleInstance(this->libraryName_);
+ Core::getInstance().loadModule(this->moduleInstance_);
+ }
+ else
+ {
+ orxout(internal_info) << "Plugin " << this->name_ << " is already referenced" << endl;
+ }
+
+ this->referenceCounter_++;
+ }
+
+ void Plugin::unload()
+ {
+ this->referenceCounter_--;
+ if (this->referenceCounter_ == 0)
+ {
+ orxout(internal_info) << "Unloading plugin " << this->name_ << "..." << endl;
+ Core::getInstance().unloadModule(this->moduleInstance_);
+ delete this->moduleInstance_;
+ this->moduleInstance_ = NULL;
+ }
+ else if (this->referenceCounter_ > 0)
+ {
+ orxout(internal_info) << "Plugin " << this->name_ << " is still referenced" << endl;
+ }
+ else
+ {
+ orxout(internal_warning) << "Plugin " << this->name_ << " was dereferenced more often than it was reference" << endl;
+ this->referenceCounter_ = 0;
+ }
+ }
+}
Property changes on: code/branches/core7/src/libraries/core/module/Plugin.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/core7/src/libraries/core/module/Plugin.h
===================================================================
--- code/branches/core7/src/libraries/core/module/Plugin.h (rev 0)
+++ code/branches/core7/src/libraries/core/module/Plugin.h 2015-08-27 21:05:18 UTC (rev 10552)
@@ -0,0 +1,53 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _Plugin_H__
+#define _Plugin_H__
+
+#include "core/CorePrereqs.h"
+
+namespace orxonox
+{
+ class _CoreExport Plugin
+ {
+ public:
+ Plugin(const std::string& name, const std::string& libraryName);
+ ~Plugin();
+
+ void load();
+ void unload();
+
+ private:
+ std::string name_;
+ std::string libraryName_;
+ unsigned int referenceCounter_;
+ ModuleInstance* moduleInstance_;
+ };
+}
+
+#endif /* _Plugin_H__ */
Property changes on: code/branches/core7/src/libraries/core/module/Plugin.h
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/core7/src/libraries/core/module/PluginManager.cc
===================================================================
--- code/branches/core7/src/libraries/core/module/PluginManager.cc (rev 0)
+++ code/branches/core7/src/libraries/core/module/PluginManager.cc 2015-08-27 21:05:18 UTC (rev 10552)
@@ -0,0 +1,93 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+#include "PluginManager.h"
+
+#include <fstream>
+
+#include "SpecialConfig.h"
+#include "Plugin.h"
+#include "core/ApplicationPaths.h"
+#include "core/command/ConsoleCommandIncludes.h"
+
+namespace orxonox
+{
+ SetConsoleCommand("PluginManager", "load", &PluginManager::loadPlugin);
+ SetConsoleCommand("PluginManager", "unload", &PluginManager::unloadPlugin);
+
+ PluginManager* PluginManager::singletonPtr_s = 0;
+
+ PluginManager::PluginManager()
+ {
+ }
+
+ PluginManager::~PluginManager()
+ {
+ for (std::map<std::string, Plugin*>::iterator it = this->plugins_.begin(); it != this->plugins_.end(); ++it)
+ delete it->second;
+ }
+
+ void PluginManager::findPlugins()
+ {
+ const std::vector<std::string>& pluginPaths = ApplicationPaths::getInstance().getPluginPaths();
+ for (std::vector<std::string>::const_iterator it = pluginPaths.begin(); it != pluginPaths.end(); ++it)
+ {
+ std::string name;
+ std::string libraryName = (*it);
+ std::string filename = libraryName + + specialConfig::pluginExtension;
+ std::ifstream infile(filename.c_str());
+ if (infile >> name)
+ {
+ orxout(internal_info) << "Found plugin with name '" << name << "' in module " << libraryName << endl;
+ this->plugins_[name] = new Plugin(name, libraryName);
+ }
+ else
+ {
+ orxout(internal_warning) << "Could not read plugin file " << filename << endl;
+ }
+ }
+ }
+
+ /*static*/ void PluginManager::loadPlugin(const std::string& name)
+ {
+ Plugin* plugin = PluginManager::getInstance().plugins_[name];
+ if (plugin != NULL)
+ plugin->load();
+ else
+ orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
+ }
+
+ /*static*/ void PluginManager::unloadPlugin(const std::string& name)
+ {
+ Plugin* plugin = PluginManager::getInstance().plugins_[name];
+ if (plugin != NULL)
+ plugin->unload();
+ else
+ orxout(internal_warning) << "Cannot find plugin with name " << name << endl;
+ }
+}
Property changes on: code/branches/core7/src/libraries/core/module/PluginManager.cc
___________________________________________________________________
Added: svn:eol-style
+ native
Added: code/branches/core7/src/libraries/core/module/PluginManager.h
===================================================================
--- code/branches/core7/src/libraries/core/module/PluginManager.h (rev 0)
+++ code/branches/core7/src/libraries/core/module/PluginManager.h 2015-08-27 21:05:18 UTC (rev 10552)
@@ -0,0 +1,60 @@
+/*
+ * ORXONOX - the hottest 3D action shooter ever to exist
+ * > www.orxonox.net <
+ *
+ *
+ * License notice:
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Author:
+ * Fabian 'x3n' Landau
+ * Co-authors:
+ * ...
+ *
+ */
+
+#ifndef _PluginManager_H__
+#define _PluginManager_H__
+
+#include "core/CorePrereqs.h"
+
+#include <map>
+#include <string>
+#include "util/Singleton.h"
+
+namespace orxonox
+{
+ class _CoreExport PluginManager : public Singleton<PluginManager>
+ {
+ friend class Singleton<PluginManager>;
+
+ public:
+ PluginManager();
+ ~PluginManager();
+
+ void findPlugins();
+
+ static void loadPlugin(const std::string& name);
+ static void unloadPlugin(const std::string& name);
+
+ private:
+ std::map<std::string, Plugin*> plugins_;
+
+ static PluginManager* singletonPtr_s;
+ };
+}
+
+#endif /* _PluginManager_H__ */
Property changes on: code/branches/core7/src/libraries/core/module/PluginManager.h
___________________________________________________________________
Added: svn:eol-style
+ native
More information about the Orxonox-commit
mailing list