Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 11, 2009, 10:22:55 PM (15 years ago)
Author:
landauf
Message:

Added a dynamic library loader (more or less a copy of Ogre::DynLibManager but with some adjustments for Orxonox). This allows us to load plugins at runtime. Plugin-libraries must be declared with the "PLUGIN" switch in ORXONOX_ADD_LIBRARY in CMake.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/libraries/src/core/Core.cc

    r5625 r5626  
    4040#include <cstdlib>
    4141#include <cstdio>
     42#include <boost/version.hpp>
    4243#include <boost/filesystem.hpp>
    4344#include <OgreRenderWindow.h>
     
    6869#include "ConfigValueIncludes.h"
    6970#include "CoreIncludes.h"
     71#include "DynLibManager.h"
    7072#include "Factory.h"
    7173#include "GameMode.h"
     
    8082#include "input/InputManager.h"
    8183
     84// Boost 1.36 has some issues with deprecated functions that have been omitted
     85#if (BOOST_VERSION == 103600)
     86#  define BOOST_LEAF_FUNCTION filename
     87#else
     88#  define BOOST_LEAF_FUNCTION leaf
     89#endif
     90
    8291namespace orxonox
    8392{
     
    260269        this->setFixedPaths();
    261270
    262         // TODO: Load plugins
     271        // Create a new dynamic library manager
     272        this->dynLibManager_.reset(new DynLibManager());
     273
     274        // Load plugins
     275        try
     276        {
     277            // We search for helper files with the following extension
     278            std::string pluginextension = ".plugin";
     279            size_t pluginextensionlength = pluginextension.size();
     280
     281            // Search in the directory of our executable
     282            boost::filesystem::path searchpath = this->getRootPath() / ORXONOX_PLUGIN_INSTALL_PATH;
     283
     284            boost::filesystem::directory_iterator file(searchpath);
     285            boost::filesystem::directory_iterator end;
     286
     287            // Iterate through all files
     288            while (file != end)
     289            {
     290                std::string filename = file->BOOST_LEAF_FUNCTION();
     291
     292                // Check if the file ends with the exension in question
     293                if (filename.size() > pluginextensionlength)
     294                {
     295                    if (filename.substr(filename.size() - pluginextensionlength) == pluginextension)
     296                    {
     297                        // We've found a helper file - now load the library with the same name
     298                        std::string library = filename.substr(0, filename.size() - pluginextensionlength);
     299                        boost::filesystem::path librarypath = searchpath / library;
     300
     301                        this->dynLibManager_->load(librarypath.string());
     302                    }
     303                }
     304
     305                ++file;
     306            }
     307        }
     308        catch (const std::exception& e)
     309        {
     310            COUT(1) << "An error occurred while loading plugins: " << e.what() << std::endl;
     311        }
     312        catch (...)
     313        {
     314            COUT(1) << "An error occurred while loading plugins." << std::endl;
     315        }
    263316
    264317        // Parse command line arguments AFTER the plugins have been loaded (static code!)
Note: See TracChangeset for help on using the changeset viewer.