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