Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 29, 2009, 10:19:38 PM (15 years ago)
Author:
landauf
Message:

merged libraries branch back to trunk

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/core/Core.cc

    r3370 r5693  
    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{
     
    241250        boost::filesystem::path rootPath_;
    242251        boost::filesystem::path executablePath_;        //!< Path to the executable
     252        boost::filesystem::path modulePath_;            //!< Path to the modules
    243253        boost::filesystem::path mediaPath_;             //!< Path to the media file folder
    244254        boost::filesystem::path configPath_;            //!< Path to the config file folder
     
    256266        , bGraphicsLoaded_(false)
    257267    {
    258         // Parse command line arguments first
     268        // Set the hard coded fixed paths
     269        this->setFixedPaths();
     270
     271        // Create a new dynamic library manager
     272        this->dynLibManager_.reset(new DynLibManager());
     273
     274        // Load modules
     275        try
     276        {
     277            // We search for helper files with the following extension
     278            std::string moduleextension = ORXONOX_MODULE_EXTENSION;
     279            size_t moduleextensionlength = moduleextension.size();
     280
     281            // Search in the directory of our executable
     282            boost::filesystem::path searchpath = this->configuration_->modulePath_;
     283
     284            // Add that path to the PATH variable in case a module depends on another one
     285            std::string pathVariable = getenv("PATH");
     286            putenv(const_cast<char*>(("PATH=" + pathVariable + ";" + configuration_->modulePath_.string()).c_str()));
     287
     288            boost::filesystem::directory_iterator file(searchpath);
     289            boost::filesystem::directory_iterator end;
     290
     291            // Iterate through all files
     292            while (file != end)
     293            {
     294                std::string filename = file->BOOST_LEAF_FUNCTION();
     295
     296                // Check if the file ends with the exension in question
     297                if (filename.size() > moduleextensionlength)
     298                {
     299                    if (filename.substr(filename.size() - moduleextensionlength) == moduleextension)
     300                    {
     301                        // We've found a helper file - now load the library with the same name
     302                        std::string library = filename.substr(0, filename.size() - moduleextensionlength);
     303                        boost::filesystem::path librarypath = searchpath / library;
     304
     305                        try
     306                        {
     307                            DynLibManager::getInstance().load(librarypath.string());
     308                        }
     309                        catch (const std::exception& e)
     310                        {
     311                            COUT(1) << "Couldn't load module \"" << librarypath.string() << "\": " << e.what() << std::endl;
     312                        }
     313                        catch (...)
     314                        {
     315                            COUT(1) << "Couldn't load module \"" << librarypath.string() << "\"" << std::endl;
     316                        }
     317                    }
     318                }
     319
     320                ++file;
     321            }
     322        }
     323        catch (const std::exception& e)
     324        {
     325            COUT(1) << "An error occurred while loading modules: " << e.what() << std::endl;
     326        }
     327        catch (...)
     328        {
     329            COUT(1) << "An error occurred while loading modules." << std::endl;
     330        }
     331
     332        // Parse command line arguments AFTER the modules have been loaded (static code!)
    259333        CommandLine::parseCommandLine(cmdLine);
    260334
    261         // Determine and set the location of the executable
    262         setExecutablePath();
    263 
    264         // Determine whether we have an installed or a binary dir run
    265         // The latter occurs when simply running from the build directory
    266         checkDevBuild();
    267 
    268         // Make sure the directories we write in exist or else make them
    269         createDirectories();
     335        // Set configurable paths like log, config and media
     336        this->setConfigurablePaths();
    270337
    271338        // create a signal handler (only active for linux)
     
    507574    /**
    508575    @brief
    509         Compares the executable path with the working directory
    510     */
    511     void Core::setExecutablePath()
    512     {
     576        Retrievs the executable path and sets all hard coded fixed path (currently only the module path)
     577        Also checks for "orxonox_dev_build.keep_me" in the executable diretory.
     578        If found it means that this is not an installed run, hence we
     579        don't write the logs and config files to ~/.orxonox
     580    @throw
     581        GeneralException
     582    */
     583    void Core::setFixedPaths()
     584    {
     585        //////////////////////////
     586        // FIND EXECUTABLE PATH //
     587        //////////////////////////
     588
    513589#ifdef ORXONOX_PLATFORM_WINDOWS
    514590        // get executable module
     
    553629        configuration_->executablePath_ = configuration_->executablePath_.branch_path(); // remove executable name
    554630#endif
    555     }
    556 
    557     /**
    558     @brief
    559         Checks for "orxonox_dev_build.keep_me" in the executable diretory.
    560         If found it means that this is not an installed run, hence we
    561         don't write the logs and config files to ~/.orxonox
    562     @throws
    563         GeneralException
    564     */
    565     void Core::checkDevBuild()
    566     {
     631
     632        /////////////////////
     633        // SET MODULE PATH //
     634        /////////////////////
     635
    567636        if (boost::filesystem::exists(configuration_->executablePath_ / "orxonox_dev_build.keep_me"))
    568637        {
    569638            COUT(1) << "Running from the build tree." << std::endl;
    570639            Core::bDevRun_ = true;
    571             configuration_->mediaPath_  = ORXONOX_MEDIA_DEV_PATH;
    572             configuration_->configPath_ = ORXONOX_CONFIG_DEV_PATH;
    573             configuration_->logPath_    = ORXONOX_LOG_DEV_PATH;
     640            configuration_->modulePath_ = ORXONOX_MODULE_DEV_PATH;
    574641        }
    575642        else
    576643        {
     644
    577645#ifdef INSTALL_COPYABLE // --> relative paths
     646
    578647            // Also set the root path
    579648            boost::filesystem::path relativeExecutablePath(ORXONOX_RUNTIME_INSTALL_PATH);
     
    585654                ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?");
    586655
     656            // Module path is fixed as well
     657            configuration_->modulePath_ = configuration_->rootPath_ / ORXONOX_MODULE_INSTALL_PATH;
     658
     659#else
     660
     661            // There is no root path, so don't set it at all
     662            // Module path is fixed as well
     663            configuration_->modulePath_ = ORXONOX_MODULE_INSTALL_PATH;
     664
     665#endif
     666        }
     667    }
     668
     669    /**
     670    @brief
     671        Sets config, log and media path and creates folders if necessary.
     672    @throws
     673        GeneralException
     674    */
     675    void Core::setConfigurablePaths()
     676    {
     677        if (Core::isDevelopmentRun())
     678        {
     679            configuration_->mediaPath_  = ORXONOX_MEDIA_DEV_PATH;
     680            configuration_->configPath_ = ORXONOX_CONFIG_DEV_PATH;
     681            configuration_->logPath_    = ORXONOX_LOG_DEV_PATH;
     682        }
     683        else
     684        {
     685
     686#ifdef INSTALL_COPYABLE // --> relative paths
     687
    587688            // Using paths relative to the install prefix, complete them
    588689            configuration_->mediaPath_  = configuration_->rootPath_ / ORXONOX_MEDIA_INSTALL_PATH;
    589690            configuration_->configPath_ = configuration_->rootPath_ / ORXONOX_CONFIG_INSTALL_PATH;
    590691            configuration_->logPath_    = configuration_->rootPath_ / ORXONOX_LOG_INSTALL_PATH;
     692
    591693#else
    592             // There is no root path, so don't set it at all
    593694
    594695            configuration_->mediaPath_  = ORXONOX_MEDIA_INSTALL_PATH;
     
    607708            configuration_->configPath_ = userDataPath / ORXONOX_CONFIG_INSTALL_PATH;
    608709            configuration_->logPath_    = userDataPath / ORXONOX_LOG_INSTALL_PATH;
    609 #endif
     710
     711#endif
     712
    610713        }
    611714
     
    617720            configuration_->logPath_    = configuration_->logPath_    / directory;
    618721        }
    619     }
    620 
    621     /*
    622     @brief
    623         Checks for the log and the config directory and creates them
    624         if necessary. Otherwise me might have problems opening those files.
    625     @throws
    626         orxonox::GeneralException if the directory to be created is a file.
    627     */
    628     void Core::createDirectories()
    629     {
     722
     723        // Create directories to avoid problems when opening files in non existent folders.
    630724        std::vector<std::pair<boost::filesystem::path, std::string> > directories;
    631725        directories.push_back(std::make_pair(boost::filesystem::path(configuration_->configPath_), "config"));
Note: See TracChangeset for help on using the changeset viewer.