Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox/src/libraries/core/PathConfig.cc @ 7324

Last change on this file since 7324 was 6039, checked in by rgrieder, 15 years ago

Applied crucial fix from console branch

  • Property svn:eol-style set to native
File size: 10.2 KB
RevLine 
[1505]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:
[2896]23 *      Reto Grieder
[1505]24 *   Co-authors:
[2896]25 *      ...
[1505]26 *
27 */
28
[5836]29#include "PathConfig.h"
[1505]30
[1756]31#include <cassert>
[2710]32#include <cstdlib>
33#include <cstdio>
[5836]34#include <vector>
[5693]35#include <boost/version.hpp>
[2710]36#include <boost/filesystem.hpp>
37
38#ifdef ORXONOX_PLATFORM_WINDOWS
[2896]39#  ifndef WIN32_LEAN_AND_MEAN
40#    define WIN32_LEAN_AND_MEAN
41#  endif
[2710]42#  include <windows.h>
[3214]43#  undef min
44#  undef max
[2710]45#elif defined(ORXONOX_PLATFORM_APPLE)
46#  include <sys/param.h>
47#  include <mach-o/dyld.h>
48#else /* Linux */
49#  include <sys/types.h>
50#  include <unistd.h>
51#endif
52
53#include "SpecialConfig.h"
[2896]54#include "util/Debug.h"
[2710]55#include "util/Exception.h"
[6021]56#include "CommandLineParser.h"
[1505]57
[5693]58// Boost 1.36 has some issues with deprecated functions that have been omitted
59#if (BOOST_VERSION == 103600)
60#  define BOOST_LEAF_FUNCTION filename
61#else
62#  define BOOST_LEAF_FUNCTION leaf
63#endif
64
[1505]65namespace orxonox
66{
[5836]67    namespace bf = boost::filesystem;
68
[3196]69    //! Static pointer to the singleton
[5836]70    PathConfig* PathConfig::singletonPtr_s  = 0;
[2662]71
[3280]72    SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
[2710]73
[5836]74    PathConfig::PathConfig()
75        : rootPath_(*(new bf::path()))
76        , executablePath_(*(new bf::path()))
77        , modulePath_(*(new bf::path()))
78        , dataPath_(*(new bf::path()))
79        , configPath_(*(new bf::path()))
80        , logPath_(*(new bf::path()))
[3370]81        , bDevRun_(false)
[3280]82    {
[5693]83        //////////////////////////
84        // FIND EXECUTABLE PATH //
85        //////////////////////////
86
[2710]87#ifdef ORXONOX_PLATFORM_WINDOWS
88        // get executable module
89        TCHAR buffer[1024];
90        if (GetModuleFileName(NULL, buffer, 1024) == 0)
91            ThrowException(General, "Could not retrieve executable path.");
92
93#elif defined(ORXONOX_PLATFORM_APPLE)
94        char buffer[1024];
95        unsigned long path_len = 1023;
96        if (_NSGetExecutablePath(buffer, &path_len))
97            ThrowException(General, "Could not retrieve executable path.");
98
99#else /* Linux */
100        /* written by Nicolai Haehnle <prefect_@gmx.net> */
101
102        /* Get our PID and build the name of the link in /proc */
103        char linkname[64]; /* /proc/<pid>/exe */
104        if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0)
105        {
106            /* This should only happen on large word systems. I'm not sure
107               what the proper response is here.
108               Since it really is an assert-like condition, aborting the
109               program seems to be in order. */
110            assert(false);
111        }
112
113        /* Now read the symbolic link */
114        char buffer[1024];
115        int ret;
116        ret = readlink(linkname, buffer, 1024);
117        /* In case of an error, leave the handling up to the caller */
118        if (ret == -1)
119            ThrowException(General, "Could not retrieve executable path.");
120
121        /* Ensure proper NUL termination */
122        buffer[ret] = 0;
123#endif
124
[5836]125        executablePath_ = bf::path(buffer);
[2710]126#ifndef ORXONOX_PLATFORM_APPLE
[5836]127        executablePath_ = executablePath_.branch_path(); // remove executable name
[2710]128#endif
129
[5693]130        /////////////////////
131        // SET MODULE PATH //
132        /////////////////////
133
[5836]134        if (bf::exists(executablePath_ / "orxonox_dev_build.keep_me"))
[2710]135        {
136            COUT(1) << "Running from the build tree." << std::endl;
[5836]137            PathConfig::bDevRun_ = true;
138            modulePath_ = specialConfig::moduleDevDirectory;
[2710]139        }
140        else
141        {
[5693]142
[2710]143#ifdef INSTALL_COPYABLE // --> relative paths
[5693]144
[2710]145            // Also set the root path
[5836]146            bf::path relativeExecutablePath(specialConfig::defaultRuntimePath);
147            rootPath_ = executablePath_;
148            while (!bf::equivalent(rootPath_ / relativeExecutablePath, executablePath_) && !rootPath_.empty())
149                rootPath_ = rootPath_.branch_path();
150            if (rootPath_.empty())
[2710]151                ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?");
152
[5693]153            // Module path is fixed as well
[5836]154            modulePath_ = rootPath_ / specialConfig::defaultModulePath;
[5693]155
156#else
157
158            // There is no root path, so don't set it at all
159            // Module path is fixed as well
[5836]160            modulePath_ = specialConfig::moduleInstallDirectory;
[5693]161
162#endif
163        }
164    }
165
[5836]166    PathConfig::~PathConfig()
[5693]167    {
[5836]168        delete &rootPath_;
169        delete &executablePath_;
170        delete &modulePath_;
171        delete &dataPath_;
172        delete &configPath_;
173        delete &logPath_;
174    }
175
176    void PathConfig::setConfigurablePaths()
177    {
178        if (bDevRun_)
[5693]179        {
[5836]180            dataPath_         = specialConfig::dataDevDirectory;
181            configPath_       = specialConfig::configDevDirectory;
182            logPath_          = specialConfig::logDevDirectory;
[5693]183        }
184        else
185        {
186
187#ifdef INSTALL_COPYABLE // --> relative paths
188
[2710]189            // Using paths relative to the install prefix, complete them
[5836]190            dataPath_   = rootPath_ / specialConfig::defaultDataPath;
191            configPath_ = rootPath_ / specialConfig::defaultConfigPath;
192            logPath_    = rootPath_ / specialConfig::defaultLogPath;
[5693]193
[2710]194#else
195
[5836]196            dataPath_  = specialConfig::dataInstallDirectory;
[2710]197
198            // Get user directory
199#  ifdef ORXONOX_PLATFORM_UNIX /* Apple? */
200            char* userDataPathPtr(getenv("HOME"));
201#  else
202            char* userDataPathPtr(getenv("APPDATA"));
203#  endif
204            if (userDataPathPtr == NULL)
205                ThrowException(General, "Could not retrieve user data path.");
[5836]206            bf::path userDataPath(userDataPathPtr);
[2710]207            userDataPath /= ".orxonox";
208
[5836]209            configPath_ = userDataPath / specialConfig::defaultConfigPath;
210            logPath_    = userDataPath / specialConfig::defaultLogPath;
[5693]211
[2710]212#endif
[5693]213
[2710]214        }
215
216        // Option to put all the config and log files in a separate folder
[6021]217        if (!CommandLineParser::getArgument("writingPathSuffix")->hasDefaultValue())
[2710]218        {
[6021]219            std::string directory(CommandLineParser::getValue("writingPathSuffix").getString());
[5836]220            configPath_ = configPath_ / directory;
221            logPath_    = logPath_    / directory;
[2710]222        }
223
[5693]224        // Create directories to avoid problems when opening files in non existent folders.
[5836]225        std::vector<std::pair<bf::path, std::string> > directories;
226        directories.push_back(std::make_pair(bf::path(configPath_), "config"));
227        directories.push_back(std::make_pair(bf::path(logPath_), "log"));
[2710]228
[5836]229        for (std::vector<std::pair<bf::path, std::string> >::iterator it = directories.begin();
[2710]230            it != directories.end(); ++it)
231        {
[5836]232            if (bf::exists(it->first) && !bf::is_directory(it->first))
[2710]233            {
234                ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \
[2759]235                                         Please remove " + it->first.string());
[2710]236            }
[5836]237            if (bf::create_directories(it->first)) // function may not return true at all (bug?)
[2710]238            {
239                COUT(4) << "Created " << it->second << " directory" << std::endl;
240            }
241        }
242    }
[2896]243
[5836]244    std::vector<std::string> PathConfig::getModulePaths()
[2896]245    {
[5836]246        std::vector<std::string> modulePaths;
247
248        // We search for helper files with the following extension
249        std::string moduleextension = specialConfig::moduleExtension;
250        size_t moduleextensionlength = moduleextension.size();
251
252        // Add that path to the PATH variable in case a module depends on another one
253        std::string pathVariable = getenv("PATH");
254        putenv(const_cast<char*>(("PATH=" + pathVariable + ";" + modulePath_.string()).c_str()));
255
[6039]256        // Make sure the path exists, otherwise don't load modules
257        if (!boost::filesystem::exists(modulePath_))
258            return modulePaths;
259
[5836]260        boost::filesystem::directory_iterator file(modulePath_);
261        boost::filesystem::directory_iterator end;
262
263        // Iterate through all files
264        while (file != end)
[3370]265        {
[5836]266            std::string filename = file->BOOST_LEAF_FUNCTION();
267
268            // Check if the file ends with the exension in question
269            if (filename.size() > moduleextensionlength)
270            {
271                if (filename.substr(filename.size() - moduleextensionlength) == moduleextension)
272                {
273                    // We've found a helper file
274                    std::string library = filename.substr(0, filename.size() - moduleextensionlength);
[5927]275                    modulePaths.push_back((modulePath_ / library).file_string());
[5836]276                }
277            }
278            ++file;
[3370]279        }
[5836]280
281        return modulePaths;
[2896]282    }
[3370]283
[5836]284    /*static*/ std::string PathConfig::getRootPathString()
[3370]285    {
[5836]286        return getInstance().rootPath_.string() + '/';
[3370]287    }
[5836]288
289    /*static*/ std::string PathConfig::getExecutablePathString()
290    {
291        return getInstance().executablePath_.string() + '/';
292    }
293
294    /*static*/ std::string PathConfig::getDataPathString()
295    {
296        return getInstance().dataPath_.string() + '/';
297    }
298
299    /*static*/ std::string PathConfig::getConfigPathString()
300    {
301        return getInstance().configPath_.string() + '/';
302    }
303
304    /*static*/ std::string PathConfig::getLogPathString()
305    {
306        return getInstance().logPath_.string() + '/';
307    }
308
309    /*static*/ std::string PathConfig::getModulePathString()
310    {
311        return getInstance().modulePath_.string() + '/';
312    }
[1505]313}
Note: See TracBrowser for help on using the repository browser.