Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox_qt/src/libraries/core/PathConfig.cc @ 7428

Last change on this file since 7428 was 7421, checked in by rgrieder, 14 years ago

Basic stuff up and running for the Qt sandbox.
No GUI support yet.

  • Property svn:eol-style set to native
File size: 6.7 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
36#ifdef ORXONOX_PLATFORM_WINDOWS
37#  ifndef WIN32_LEAN_AND_MEAN
38#    define WIN32_LEAN_AND_MEAN
39#  endif
40#  include <windows.h>
41#  undef min
42#  undef max
43#elif defined(ORXONOX_PLATFORM_APPLE)
44#  include <sys/param.h>
45#  include <mach-o/dyld.h>
46#else /* Linux */
47#  include <sys/types.h>
48#  include <unistd.h>
49#endif
50
51#include "SpecialConfig.h"
52#include "util/Debug.h"
53#include "util/Exception.h"
54
55namespace orxonox
56{
57    //! Static pointer to the singleton
58    PathConfig* PathConfig::singletonPtr_s  = 0;
59
60    PathConfig::PathConfig()
61    {
62        //////////////////////////
63        // FIND EXECUTABLE PATH //
64        //////////////////////////
65
66#ifdef ORXONOX_PLATFORM_WINDOWS
67        // get executable module
68        TCHAR buffer[1024];
69        if (GetModuleFileName(NULL, buffer, 1024) == 0)
70            ThrowException(General, "Could not retrieve executable path.");
71
72#elif defined(ORXONOX_PLATFORM_APPLE)
73        char buffer[1024];
74        unsigned long path_len = 1023;
75        if (_NSGetExecutablePath(buffer, &path_len))
76            ThrowException(General, "Could not retrieve executable path.");
77
78#else /* Linux */
79        /* written by Nicolai Haehnle <prefect_@gmx.net> */
80
81        /* Get our PID and build the name of the link in /proc */
82        char linkname[64]; /* /proc/<pid>/exe */
83        if (snprintf(linkname, sizeof(linkname), "/proc/%i/exe", getpid()) < 0)
84        {
85            /* This should only happen on large word systems. I'm not sure
86               what the proper response is here.
87               Since it really is an assert-like condition, aborting the
88               program seems to be in order. */
89            assert(false);
90        }
91
92        /* Now read the symbolic link */
93        char buffer[1024];
94        int ret;
95        ret = readlink(linkname, buffer, 1024);
96        /* In case of an error, leave the handling up to the caller */
97        if (ret == -1)
98            ThrowException(General, "Could not retrieve executable path.");
99
100        /* Ensure proper NUL termination */
101        buffer[ret] = 0;
102#endif
103
104        executablePath_ = QDir(buffer);
105#ifndef ORXONOX_PLATFORM_APPLE
106        executablePath_.cdUp(); // remove executable name
107#endif
108
109        if (executablePath_.exists("orxonox_dev_build.keep_me"))
110        {
111            COUT(1) << "Running from the build tree." << std::endl;
112            PathConfig::bDevRun_ = true;
113        }
114        else
115        {
116
117#ifdef INSTALL_COPYABLE // --> relative paths
118
119            // Also set the root path
120            QDir relativeExecutablePath(specialConfig::defaultRuntimePath);
121            rootPath_ = executablePath_;
122            while (rootPath_ / relativeExecutablePath != executablePath_)
123            {
124                if (!rootPath_.cdUp())
125                    ThrowException(General, "Could not derive a root directory. Might the binary installation directory contain '..' when taken relative to the installation prefix path?");
126            }
127
128#else
129
130            // There is no root path, so don't set it at all
131
132#endif
133        }
134    }
135
136    PathConfig::~PathConfig()
137    {
138    }
139
140    void PathConfig::setConfigurablePaths()
141    {
142        if (bDevRun_)
143        {
144            dataPath_         = specialConfig::dataDevDirectory;
145            configPath_       = specialConfig::configDevDirectory;
146            logPath_          = specialConfig::logDevDirectory;
147        }
148        else
149        {
150
151#ifdef INSTALL_COPYABLE // --> relative paths
152
153            // Using paths relative to the install prefix, complete them
154            dataPath_   = rootPath_ / specialConfig::defaultDataPath;
155            configPath_ = rootPath_ / specialConfig::defaultConfigPath;
156            logPath_    = rootPath_ / specialConfig::defaultLogPath;
157
158#else
159
160            dataPath_  = specialConfig::dataInstallDirectory;
161
162            // Get user directory
163#  ifdef ORXONOX_PLATFORM_UNIX /* Apple? */
164            char* userDataPathPtr(getenv("HOME"));
165#  else
166            char* userDataPathPtr(getenv("APPDATA"));
167#  endif
168            if (userDataPathPtr == NULL)
169                ThrowException(General, "Could not retrieve user data path.");
170            QDir userDataPath(userDataPathPtr);
171            userDataPath /= ".orxonox";
172
173            configPath_ = userDataPath / specialConfig::defaultConfigPath;
174            logPath_    = userDataPath / specialConfig::defaultLogPath;
175
176#endif
177
178        }
179
180        // Create directories to avoid problems when opening files in non existent folders.
181        std::vector<std::pair<QDir, std::string> > directories;
182        directories.push_back(std::make_pair(QDir(configPath_), "config"));
183        directories.push_back(std::make_pair(QDir(logPath_), "log"));
184
185        for (std::vector<std::pair<QDir, std::string> >::iterator it = directories.begin();
186            it != directories.end(); ++it)
187        {
188            if (!it->first.exists())
189            {
190                if (!it->first.mkpath("."))
191                    ThrowException(General, std::string("The ") + it->second + " directory could not be created.");
192                COUT(4) << "Created " << it->second << " directory" << std::endl;
193            }
194        }
195    }
196
197    /*static*/ std::string PathConfig::getRootPathString()
198    {
199        return getInstance().rootPath_.path().toStdString() + '/';
200    }
201
202    /*static*/ std::string PathConfig::getExecutablePathString()
203    {
204        return getInstance().executablePath_.path().toStdString() + '/';
205    }
206
207    /*static*/ std::string PathConfig::getDataPathString()
208    {
209        return getInstance().dataPath_.path().toStdString() + '/';
210    }
211
212    /*static*/ std::string PathConfig::getConfigPathString()
213    {
214        return getInstance().configPath_.path().toStdString() + '/';
215    }
216
217    /*static*/ std::string PathConfig::getLogPathString()
218    {
219        return getInstance().logPath_.path().toStdString() + '/';
220    }
221}
Note: See TracBrowser for help on using the repository browser.