summaryrefslogtreecommitdiffstats
path: root/src/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins')
-rw-r--r--src/plugins/plugin.h33
-rw-r--r--src/plugins/plugininterface.h65
-rw-r--r--src/plugins/pluginloader.cpp126
-rw-r--r--src/plugins/pluginloader.h88
-rw-r--r--src/plugins/pluginmanager.cpp132
-rw-r--r--src/plugins/pluginmanager.h88
-rw-r--r--src/plugins/test/CMakeLists.txt12
-rw-r--r--src/plugins/test/colobot.ini3
-rw-r--r--src/plugins/test/manager_test.cpp31
9 files changed, 545 insertions, 33 deletions
diff --git a/src/plugins/plugin.h b/src/plugins/plugin.h
deleted file mode 100644
index f238122..0000000
--- a/src/plugins/plugin.h
+++ /dev/null
@@ -1,33 +0,0 @@
-// * This file is part of the COLOBOT source code
-// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
-// *
-// * 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 3 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, see http://www.gnu.org/licenses/.
-
-// plugin.h
-
-
-#pragma once
-
-
-#define PLUGIN_INTERFACE(class_type, interface_type) \
- extern "C" interface_type* installPlugin() { return (interface_type *)new class_type(); } \
- extern "C" void uninstallPlugin(class_type *_class) { delete _class; }
-
-
-class CPlugin {
- public:
- virtual char* PluginName() = 0;
- virtual int PluginVersion() = 0;
-};
-
diff --git a/src/plugins/plugininterface.h b/src/plugins/plugininterface.h
new file mode 100644
index 0000000..b8adddc
--- /dev/null
+++ b/src/plugins/plugininterface.h
@@ -0,0 +1,65 @@
+// * This file is part of the COLOBOT source code
+// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
+// *
+// * 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 3 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, see http://www.gnu.org/licenses/.
+
+// plugininterface.h
+
+/**
+ * @file plugin/plugininterface.h
+ * @brief Generic plugin interface
+ */
+
+#pragma once
+
+#include <string>
+
+#define PLUGIN_INTERFACE(class_type) \
+ static class_type* Plugin##class_type; \
+ extern "C" void InstallPluginEntry() { Plugin##class_type = new class_type(); Plugin##class_type->InstallPlugin(); } \
+ extern "C" bool UninstallPluginEntry(std::string &reason) { bool result = Plugin##class_type->UninstallPlugin(reason); \
+ if (!result) \
+ return false; \
+ delete Plugin##class_type; \
+ return true; } \
+ extern "C" CPluginInterface* GetPluginInterfaceEntry() { return static_cast<CPluginInterface*>(Plugin##class_type); }
+
+
+/**
+* @class CPluginInterface
+*
+* @brief Generic plugin interface. All plugins that will be managed by plugin manager have to derive from this class.
+*
+*/
+class CPluginInterface {
+ public:
+ /** Function to get plugin name or description
+ * @return returns plugin name
+ */
+ virtual std::string PluginName() = 0;
+
+ /** Function to get plugin version. 1 means version 0.01, 2 means 0.02 etc.
+ * @return number indicating plugin version
+ */
+ virtual int PluginVersion() = 0;
+
+ /** Function to initialize plugin
+ */
+ virtual void InstallPlugin() = 0;
+
+ /** Function called before removing plugin
+ */
+ virtual bool UninstallPlugin(std::string &) = 0;
+};
+
diff --git a/src/plugins/pluginloader.cpp b/src/plugins/pluginloader.cpp
new file mode 100644
index 0000000..fd8ce74
--- /dev/null
+++ b/src/plugins/pluginloader.cpp
@@ -0,0 +1,126 @@
+// * This file is part of the COLOBOT source code
+// * Copyright (C) 2012 Polish Portal of Colobot (PPC)
+// *
+// * 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 3 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, see http://www.gnu.org/licenses/.
+
+// pluginloader.cpp
+
+
+#include "pluginloader.h"
+
+
+CPluginLoader::CPluginLoader(std::string filename)
+{
+ mInterface = nullptr;
+ mFilename = filename;
+ mLoaded = false;
+}
+
+
+std::string CPluginLoader::GetName()
+{
+ if (mLoaded)
+ return mInterface->PluginName();
+ return "(not loaded)";
+}
+
+
+int CPluginLoader::GetVersion()
+{
+ if (mLoaded)
+ return mInterface->PluginVersion();
+ return 0;
+}
+
+
+bool CPluginLoader::IsLoaded()
+{
+ return mLoaded;
+}
+
+
+bool CPluginLoader::UnloadPlugin()
+{
+ if (!mLoaded) {
+ GetLogger()->Warn("Plugin %s is not loaded.\n");
+ return true;
+ }
+
+ bool (*uninstall)(std::string &) = (bool (*)(std::string &)) lt_dlsym(mHandle, "UninstallPluginEntry");
+ if (!uninstall) {
+ GetLogger()->Error("Error getting UninstallPluginEntry for plugin %s: %s\n", mFilename.c_str(), lt_dlerror());
+ return false;
+ }
+
+ std::string reason;
+ if (!uninstall(reason)) {
+ GetLogger()->Error("Could not unload plugin %s: %s\n", mFilename.c_str(), reason.c_str());
+ return false;
+ }
+
+ lt_dlclose(mHandle);
+ mLoaded = false;
+ return true;
+}
+
+
+bool CPluginLoader::LoadPlugin()
+{
+ if (mFilename.length() == 0) {
+ GetLogger()->Warn("No plugin filename specified.\n");
+ return false;
+ }
+
+ mHandle = lt_dlopenext(mFilename.c_str());
+ if (!mHandle) {
+ GetLogger()->Error("Error loading plugin %s: %s\n", mFilename.c_str(), lt_dlerror());
+ return false;
+ }
+
+ void (*install)() = (void (*)()) lt_dlsym(mHandle, "InstallPluginEntry");
+ if (!install) {
+ GetLogger()->Error("Error getting InstallPluginEntry for plugin %s: %s\n", mFilename.c_str(), lt_dlerror());
+ return false;
+ }
+
+ CPluginInterface* (*getInterface)() = (CPluginInterface* (*)()) lt_dlsym(mHandle, "GetPluginInterfaceEntry");
+
+ if (!getInterface) {
+ GetLogger()->Error("Error getting GetPluginInterfaceEntry for plugin %s: %s\n", mFilename.c_str(), lt_dlerror());
+ return false;
+ }
+
+ install();
+ mInterface = getInterface();
+ mLoaded = true;
+ return true;
+}
+
+
+bool CPluginLoader::SetFilename(std::string filename)
+{
+ bool ok = true;
+ if (mLoaded)
+ ok = UnloadPlugin();
+
+ if (ok)
+ mFilename = filename;
+ return ok;
+}
+
+
+std::string CPluginLoader::GetFilename()
+{
+ return mFilename;
+}
diff --git a/src/plugins/pluginloader.h b/src/plugins/pluginloader.h
new file mode 100644
index 0000000..40b19da
--- /dev/null
+++ b/src/plugins/pluginloader.h
@@ -0,0 +1,88 @@
+// * This file is part of the COLOBOT source code
+// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
+// *
+// * 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 3 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, see http://www.gnu.org/licenses/.
+
+// pluginloader.h
+
+/**
+ * @file plugin/pluginloader.h
+ * @brief Plugin loader interface
+ */
+
+#pragma once
+
+#include <ltdl.h>
+#include <string>
+
+#include <common/logger.h>
+
+#include "plugininterface.h"
+
+
+/**
+* @class CPluginLoader
+*
+* @brief Plugin loader interface. Plugin manager uses this class to load plugins.
+*
+*/
+class CPluginLoader {
+ public:
+ /** Class contructor
+ * @param std::string plugin filename
+ */
+ CPluginLoader(std::string);
+
+ /** Function to get plugin name or description
+ * @return returns plugin name
+ */
+ std::string GetName();
+
+ /** Function to get plugin version
+ * @return returns plugin version
+ */
+ int GetVersion();
+
+ /** Function to unload plugin
+ * @return returns true on success
+ */
+ bool UnloadPlugin();
+
+ /** Function to load plugin
+ * @return returns true on success
+ */
+ bool LoadPlugin();
+
+ /** Function to check if plugin is loaded
+ * @return returns true if plugin is loaded
+ */
+ bool IsLoaded();
+
+ /** Function to set plugin filename
+ * @return returns true on success. Action can fail if plugin was loaded and cannot be unloaded
+ */
+ bool SetFilename(std::string);
+
+ /** Function to get plugin filename
+ * @return returns plugin filename
+ */
+ std::string GetFilename();
+
+
+ private:
+ CPluginInterface* mInterface;
+ std::string mFilename;
+ lt_dlhandle mHandle;
+ bool mLoaded;
+};
diff --git a/src/plugins/pluginmanager.cpp b/src/plugins/pluginmanager.cpp
new file mode 100644
index 0000000..470ac2f
--- /dev/null
+++ b/src/plugins/pluginmanager.cpp
@@ -0,0 +1,132 @@
+// * This file is part of the COLOBOT source code
+// * Copyright (C) 2012 Polish Portal of Colobot (PPC)
+// *
+// * 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 3 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, see http://www.gnu.org/licenses/.
+
+// pluginmanager.cpp
+
+
+#include "pluginmanager.h"
+
+
+template<> CPluginManager* CSingleton<CPluginManager>::mInstance = nullptr;
+
+
+CPluginManager::CPluginManager()
+{
+ lt_dlinit();
+}
+
+
+CPluginManager::~CPluginManager()
+{
+ UnloadAllPlugins();
+ lt_dlexit();
+}
+
+
+
+void CPluginManager::LoadFromProfile()
+{
+ std::vector< std::string > dirs = GetProfile()->GetLocalProfileSection("Plugins", "Path");
+ std::vector< std::string > plugins = GetProfile()->GetLocalProfileSection("Plugins", "File");
+
+ for (std::string dir : dirs)
+ m_folders.insert(dir);
+
+ for (std::string plugin : plugins) {
+ GetLogger()->Info("Trying to load plugin %s...\n", plugin.c_str());
+ LoadPlugin(plugin);
+ }
+}
+
+
+bool CPluginManager::LoadPlugin(std::string filename)
+{
+ bool result = false;
+ CPluginLoader *loader = new CPluginLoader("");
+ for (std::string dir : m_folders) {
+ loader->SetFilename(dir + "/" + filename);
+ result = loader->LoadPlugin();
+ if (result) {
+ GetLogger()->Info("Plugin %s (%s) version %0.2f loaded!\n", filename.c_str(), loader->GetName().c_str(), loader->GetVersion() / 100.0f);
+ m_plugins.push_back(loader);
+ break;
+ }
+ }
+ return result;
+}
+
+
+bool CPluginManager::UnloadPlugin(std::string filename)
+{
+ std::vector<CPluginLoader *>::iterator it;
+ GetLogger()->Info("Trying to unload plugin %s...\n", filename.c_str());
+ for (it = m_plugins.begin(); it != m_plugins.end(); it++) {
+ CPluginLoader *plugin = *it;
+ if (NameEndsWith(plugin->GetFilename(), filename)) {
+ m_plugins.erase(it);
+ plugin->UnloadPlugin();
+ delete plugin;
+ return true;
+ }
+ }
+ return false;
+}
+
+
+bool CPluginManager::AddSearchDirectory(std::string dir)
+{
+ m_folders.insert(dir);
+ return true;
+}
+
+
+bool CPluginManager::RemoveSearchDirectory(std::string dir)
+{
+ m_folders.erase(dir);
+ return false;
+}
+
+
+bool CPluginManager::UnloadAllPlugins()
+{
+ bool allOk = true;
+ std::vector<CPluginLoader *>::iterator it;
+ for (it = m_plugins.begin(); it != m_plugins.end(); it++) {
+ CPluginLoader *plugin = *it;
+ bool result;
+
+ GetLogger()->Info("Trying to unload plugin %s (%s)...\n", plugin->GetFilename().c_str(), plugin->GetName().c_str());
+ result = plugin->UnloadPlugin();
+ if (!result) {
+ allOk = false;
+ continue;
+ }
+ delete plugin;
+ m_plugins.erase(it);
+ }
+
+ return allOk;
+}
+
+
+bool CPluginManager::NameEndsWith(std::string filename, std::string ending)
+{
+ if (filename.length() > ending.length()) {
+ std::string fileEnd = filename.substr(filename.length() - ending.length());
+ return (fileEnd == ending);
+ }
+ return false;
+}
diff --git a/src/plugins/pluginmanager.h b/src/plugins/pluginmanager.h
new file mode 100644
index 0000000..e425c62
--- /dev/null
+++ b/src/plugins/pluginmanager.h
@@ -0,0 +1,88 @@
+// * This file is part of the COLOBOT source code
+// * Copyright (C) 2012, Polish Portal of Colobot (PPC)
+// *
+// * 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 3 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, see http://www.gnu.org/licenses/.
+
+// pluginmanager.h
+
+/**
+ * @file plugin/pluginmanager.h
+ * @brief Plugin manager class.
+ */
+
+#pragma once
+
+#include <string>
+#include <set>
+#include <vector>
+
+#include <common/logger.h>
+#include <common/profile.h>
+
+#include <common/singleton.h>
+
+#include "pluginloader.h"
+
+
+/**
+* @class CPluginManager
+*
+* @brief Plugin manager class. Plugin manager can load plugins from colobot.ini or manually specified files.
+*
+*/
+class CPluginManager : public CSingleton<CPluginManager> {
+ public:
+ CPluginManager();
+ ~CPluginManager();
+
+ /** Function loads plugin list and path list from profile file
+ */
+ void LoadFromProfile();
+
+ /** Function loads specified plugin
+ * @param std::string plugin filename
+ * @return returns true on success
+ */
+ bool LoadPlugin(std::string);
+
+ /** Function unloads specified plugin
+ * @param std::string plugin filename
+ * @return returns true on success
+ */
+ bool UnloadPlugin(std::string);
+
+ /** Function adds path to be checked when searching for plugin file. If path was already added it will be ignored
+ * @param std::string plugin search path
+ * @return returns true on success
+ */
+ bool AddSearchDirectory(std::string);
+
+ /** Function removes path from list
+ * @param std::string plugin search path
+ * @return returns true on success
+ */
+ bool RemoveSearchDirectory(std::string);
+
+ /** Function tries to unload all plugins
+ * @return returns true on success
+ */
+ bool UnloadAllPlugins();
+
+ private:
+ bool NameEndsWith(std::string, std::string);
+
+ std::set< std::string > m_folders;
+ std::vector<CPluginLoader *> m_plugins;
+};
+
diff --git a/src/plugins/test/CMakeLists.txt b/src/plugins/test/CMakeLists.txt
new file mode 100644
index 0000000..5f86b6f
--- /dev/null
+++ b/src/plugins/test/CMakeLists.txt
@@ -0,0 +1,12 @@
+cmake_minimum_required(VERSION 2.8)
+
+set(CMAKE_BUILD_TYPE debug)
+set(CMAKE_CXX_FLAGS_DEBUG "-Wall -g -O0 -std=c++11 -rdynamic")
+
+add_executable(manager_test manager_test.cpp ../../common/logger.cpp ../../common/profile.cpp ../../common/iman.cpp ../pluginmanager.cpp ../pluginloader.cpp)
+
+include_directories(".")
+include_directories("../../")
+include_directories("../../../")
+
+target_link_libraries(manager_test ltdl)
diff --git a/src/plugins/test/colobot.ini b/src/plugins/test/colobot.ini
new file mode 100644
index 0000000..08956be
--- /dev/null
+++ b/src/plugins/test/colobot.ini
@@ -0,0 +1,3 @@
+[Plugins]
+Path=.
+File=libopenalsound.so
diff --git a/src/plugins/test/manager_test.cpp b/src/plugins/test/manager_test.cpp
new file mode 100644
index 0000000..d921c1d
--- /dev/null
+++ b/src/plugins/test/manager_test.cpp
@@ -0,0 +1,31 @@
+#include <common/logger.h>
+#include <common/profile.h>
+#include <common/iman.h>
+#include <plugins/pluginmanager.h>
+#include <sound/sound.h>
+
+
+int main() {
+ new CLogger();
+ new CProfile();
+ new CInstanceManager();
+ CPluginManager *mgr = new CPluginManager();
+
+ if (!GetProfile()->InitCurrentDirectory()) {
+ GetLogger()->Error("Config not found!\n");
+ return 1;
+ }
+
+ mgr->LoadFromProfile();
+ CSoundInterface *sound = static_cast<CSoundInterface*>(CInstanceManager::GetInstancePointer()->SearchInstance(CLASS_SOUND));
+
+ if (!sound) {
+ GetLogger()->Error("Sound not loaded!\n");
+ return 2;
+ }
+
+ sound->Create(true);
+ mgr->UnloadAllPlugins();
+
+ return 0;
+}