Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox_light/src/libraries/core/PathConfig.cc @ 7908

Last change on this file since 7908 was 7908, checked in by rgrieder, 13 years ago

Stripped down trunk to form a new light sandbox.

  • Property svn:eol-style set to native
File size: 8.3 KB
Line 
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:
23 *      Reto Grieder
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "PathConfig.h"
30
31#include <cassert>
32#include <cstdlib>
33#include <cstdio>
34#include <vector>
35#include <boost/version.hpp>
36#include <boost/filesystem.hpp>
37
38#ifdef ORXONOX_PLATFORM_WINDOWS
39#  ifndef WIN32_LEAN_AND_MEAN
40#    define WIN32_LEAN_AND_MEAN
41#  endif
42#  include <windows.h>
43#  undef min
44#  undef max
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"
54#include "util/Debug.h"
55#include "util/Exception.h"
56#include "CommandLineParser.h"
57
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
65namespace orxonox
66{
67    namespace bf = boost::filesystem;
68
69    //! Static pointer to the singleton
70    PathConfig* PathConfig::singletonPtr_s  = 0;
71
72    SetCommandLineArgument(externalDataPath, "").information("Path to the external data files");
73    SetCommandLineOnlyArgument(writingPathSuffix, "").information("Additional subfolder for config and log files");
74
75    PathConfig::PathConfig()
76        : rootPath_(*(new bf::path()))
77        , executablePath_(*(new bf::path()))
78        , dataPath_(*(new bf::path()))
79        , configPath_(*(new bf::path()))
80        , logPath_(*(new bf::path()))
81        , bDevRun_(false)
82    {
83        //////////////////////////
84        // FIND EXECUTABLE PATH //
85        //////////////////////////
86
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
125        executablePath_ = bf::path(buffer);
126#ifndef ORXONOX_PLATFORM_APPLE
127        executablePath_ = executablePath_.branch_path(); // remove executable name
128#endif
129
130        /////////////////////
131        // SET MODULE PATH //
132        /////////////////////
133
134        if (bf::exists(executablePath_ / "orxonox_dev_build.keep_me"))
135        {
136            COUT(1) << "Running from the build tree." << std::endl;
137            PathConfig::bDevRun_ = true;
138        }
139        else
140        {
141
142#ifdef INSTALL_COPYABLE // --> relative paths
143
144            // Also set the root path
145            bf::path relativeExecutablePath(specialConfig::defaultRuntimePath);
146            rootPath_ = executablePath_;
147            while (!bf::equivalent(rootPath_ / relativeExecutablePath, executablePath_) && !rootPath_.empty())
148                rootPath_ = rootPath_.branch_path();
149            if (rootPath_.empty())
150                ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?");
151
152#else
153
154            // There is no root path, so don't set it at all
155
156#endif
157        }
158    }
159
160    PathConfig::~PathConfig()
161    {
162        delete &rootPath_;
163        delete &executablePath_;
164        delete &dataPath_;
165        delete &configPath_;
166        delete &logPath_;
167    }
168
169    void PathConfig::setConfigurablePaths()
170    {
171        if (bDevRun_)
172        {
173            dataPath_         = specialConfig::dataDevDirectory;
174            configPath_       = specialConfig::configDevDirectory;
175            logPath_          = specialConfig::logDevDirectory;
176        }
177        else
178        {
179
180#ifdef INSTALL_COPYABLE // --> relative paths
181
182            // Using paths relative to the install prefix, complete them
183            dataPath_   = rootPath_ / specialConfig::defaultDataPath;
184            configPath_ = rootPath_ / specialConfig::defaultConfigPath;
185            logPath_    = rootPath_ / specialConfig::defaultLogPath;
186
187#else
188
189            dataPath_  = specialConfig::dataInstallDirectory;
190
191            // Get user directory
192#  ifdef ORXONOX_PLATFORM_UNIX /* Apple? */
193            char* userDataPathPtr(getenv("HOME"));
194#  else
195            char* userDataPathPtr(getenv("APPDATA"));
196#  endif
197            if (userDataPathPtr == NULL)
198                ThrowException(General, "Could not retrieve user data path.");
199            bf::path userDataPath(userDataPathPtr);
200            userDataPath /= ".orxonox";
201
202            configPath_ = userDataPath / specialConfig::defaultConfigPath;
203            logPath_    = userDataPath / specialConfig::defaultLogPath;
204
205#endif
206
207        }
208
209        // Option to put all the config and log files in a separate folder
210        if (!CommandLineParser::getArgument("writingPathSuffix")->hasDefaultValue())
211        {
212            const std::string& directory(CommandLineParser::getValue("writingPathSuffix").getString());
213            configPath_ = configPath_ / directory;
214            logPath_    = logPath_    / directory;
215        }
216
217        // Create directories to avoid problems when opening files in non existent folders.
218        std::vector<std::pair<bf::path, std::string> > directories;
219        directories.push_back(std::make_pair(bf::path(configPath_), "config"));
220        directories.push_back(std::make_pair(bf::path(logPath_), "log"));
221
222        for (std::vector<std::pair<bf::path, std::string> >::iterator it = directories.begin();
223            it != directories.end(); ++it)
224        {
225            if (bf::exists(it->first) && !bf::is_directory(it->first))
226            {
227                ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \
228                                         Please remove " + it->first.string());
229            }
230            if (bf::create_directories(it->first)) // function may not return true at all (bug?)
231            {
232                COUT(4) << "Created " << it->second << " directory" << std::endl;
233            }
234        }
235    }
236
237    /*static*/ std::string PathConfig::getRootPathString()
238    {
239        return getInstance().rootPath_.string() + '/';
240    }
241
242    /*static*/ std::string PathConfig::getExecutablePathString()
243    {
244        return getInstance().executablePath_.string() + '/';
245    }
246
247    /*static*/ std::string PathConfig::getDataPathString()
248    {
249        return getInstance().dataPath_.string() + '/';
250    }
251
252    /*static*/ std::string PathConfig::getConfigPathString()
253    {
254        return getInstance().configPath_.string() + '/';
255    }
256
257    /*static*/ std::string PathConfig::getLogPathString()
258    {
259        return getInstance().logPath_.string() + '/';
260    }
261}
Note: See TracBrowser for help on using the repository browser.