[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 | |
---|
[10509] | 29 | #include "ConfigurablePaths.h" |
---|
[1505] | 30 | |
---|
[1756] | 31 | #include <cassert> |
---|
[2710] | 32 | #include <cstdlib> |
---|
| 33 | #include <cstdio> |
---|
[5836] | 34 | #include <vector> |
---|
[2710] | 35 | #include <boost/filesystem.hpp> |
---|
| 36 | |
---|
| 37 | #ifdef ORXONOX_PLATFORM_WINDOWS |
---|
[2896] | 38 | # ifndef WIN32_LEAN_AND_MEAN |
---|
| 39 | # define WIN32_LEAN_AND_MEAN |
---|
| 40 | # endif |
---|
[2710] | 41 | # include <windows.h> |
---|
[3214] | 42 | # undef min |
---|
| 43 | # undef max |
---|
[2710] | 44 | #elif defined(ORXONOX_PLATFORM_APPLE) |
---|
| 45 | # include <sys/param.h> |
---|
| 46 | # include <mach-o/dyld.h> |
---|
| 47 | #else /* Linux */ |
---|
| 48 | # include <sys/types.h> |
---|
| 49 | # include <unistd.h> |
---|
| 50 | #endif |
---|
| 51 | |
---|
| 52 | #include "SpecialConfig.h" |
---|
[8858] | 53 | #include "util/Output.h" |
---|
[2710] | 54 | #include "util/Exception.h" |
---|
[10345] | 55 | #include "commandline/CommandLineIncludes.h" |
---|
[1505] | 56 | |
---|
[8351] | 57 | // Differentiate Boost Filesystem v2 and v3 |
---|
| 58 | #if (BOOST_FILESYSTEM_VERSION < 3) |
---|
| 59 | # define BF_GENERIC_STRING string |
---|
[5693] | 60 | #else |
---|
[8351] | 61 | # define BF_GENERIC_STRING generic_string |
---|
[5693] | 62 | #endif |
---|
| 63 | |
---|
[1505] | 64 | namespace orxonox |
---|
| 65 | { |
---|
[5836] | 66 | namespace bf = boost::filesystem; |
---|
| 67 | |
---|
[3196] | 68 | //! Static pointer to the singleton |
---|
[10509] | 69 | ConfigurablePaths* ConfigurablePaths::singletonPtr_s = 0; |
---|
[2662] | 70 | |
---|
[5695] | 71 | SetCommandLineArgument(externalDataPath, "").information("Path to the external data files"); |
---|
[8729] | 72 | SetCommandLineArgument(writingPathSuffix, "").information("Additional subfolder for config and log files"); |
---|
[2710] | 73 | |
---|
[10509] | 74 | ConfigurablePaths::ConfigurablePaths() |
---|
| 75 | : dataPath_(*(new bf::path())) |
---|
[5836] | 76 | , externalDataPath_(*(new bf::path())) |
---|
| 77 | , configPath_(*(new bf::path())) |
---|
| 78 | , logPath_(*(new bf::path())) |
---|
[3280] | 79 | { |
---|
[5693] | 80 | } |
---|
| 81 | |
---|
[10509] | 82 | ConfigurablePaths::~ConfigurablePaths() |
---|
[5693] | 83 | { |
---|
[5836] | 84 | delete &dataPath_; |
---|
| 85 | delete &externalDataPath_; |
---|
| 86 | delete &configPath_; |
---|
| 87 | delete &logPath_; |
---|
| 88 | } |
---|
| 89 | |
---|
[10509] | 90 | void ConfigurablePaths::setConfigurablePaths(const ApplicationPaths& applicationPaths) |
---|
[5836] | 91 | { |
---|
[10509] | 92 | if (applicationPaths.buildDirectoryRun()) |
---|
[5693] | 93 | { |
---|
[5836] | 94 | dataPath_ = specialConfig::dataDevDirectory; |
---|
| 95 | configPath_ = specialConfig::configDevDirectory; |
---|
| 96 | logPath_ = specialConfig::logDevDirectory; |
---|
| 97 | |
---|
| 98 | // Check for data path override by the command line |
---|
[6021] | 99 | if (!CommandLineParser::getArgument("externalDataPath")->hasDefaultValue()) |
---|
[9550] | 100 | externalDataPath_ = CommandLineParser::getValue("externalDataPath").get<std::string>(); |
---|
[5836] | 101 | else |
---|
| 102 | externalDataPath_ = specialConfig::externalDataDevDirectory; |
---|
[5693] | 103 | } |
---|
| 104 | else |
---|
| 105 | { |
---|
| 106 | |
---|
| 107 | #ifdef INSTALL_COPYABLE // --> relative paths |
---|
| 108 | |
---|
[2710] | 109 | // Using paths relative to the install prefix, complete them |
---|
[10509] | 110 | dataPath_ = applicationPaths.getRootPath() / specialConfig::defaultDataPath; |
---|
| 111 | configPath_ = applicationPaths.getRootPath() / specialConfig::defaultConfigPath; |
---|
| 112 | logPath_ = applicationPaths.getRootPath() / specialConfig::defaultLogPath; |
---|
[5693] | 113 | |
---|
[2710] | 114 | #else |
---|
| 115 | |
---|
[5836] | 116 | dataPath_ = specialConfig::dataInstallDirectory; |
---|
[2710] | 117 | |
---|
| 118 | // Get user directory |
---|
[8351] | 119 | #ifdef ORXONOX_PLATFORM_UNIX |
---|
[2710] | 120 | char* userDataPathPtr(getenv("HOME")); |
---|
[8351] | 121 | #else |
---|
[2710] | 122 | char* userDataPathPtr(getenv("APPDATA")); |
---|
[8351] | 123 | #endif |
---|
[2710] | 124 | if (userDataPathPtr == NULL) |
---|
| 125 | ThrowException(General, "Could not retrieve user data path."); |
---|
[5836] | 126 | bf::path userDataPath(userDataPathPtr); |
---|
[2710] | 127 | userDataPath /= ".orxonox"; |
---|
| 128 | |
---|
[5836] | 129 | configPath_ = userDataPath / specialConfig::defaultConfigPath; |
---|
| 130 | logPath_ = userDataPath / specialConfig::defaultLogPath; |
---|
[5693] | 131 | |
---|
[2710] | 132 | #endif |
---|
[5693] | 133 | |
---|
[2710] | 134 | } |
---|
| 135 | |
---|
| 136 | // Option to put all the config and log files in a separate folder |
---|
[6021] | 137 | if (!CommandLineParser::getArgument("writingPathSuffix")->hasDefaultValue()) |
---|
[2710] | 138 | { |
---|
[9550] | 139 | const std::string& directory(CommandLineParser::getValue("writingPathSuffix").get<std::string>()); |
---|
[5836] | 140 | configPath_ = configPath_ / directory; |
---|
| 141 | logPath_ = logPath_ / directory; |
---|
[2710] | 142 | } |
---|
| 143 | |
---|
[5693] | 144 | // Create directories to avoid problems when opening files in non existent folders. |
---|
[5836] | 145 | std::vector<std::pair<bf::path, std::string> > directories; |
---|
[8351] | 146 | directories.push_back(std::make_pair(bf::path(configPath_), std::string("config"))); |
---|
| 147 | directories.push_back(std::make_pair(bf::path(logPath_), std::string("log"))); |
---|
[2710] | 148 | |
---|
[5836] | 149 | for (std::vector<std::pair<bf::path, std::string> >::iterator it = directories.begin(); |
---|
[2710] | 150 | it != directories.end(); ++it) |
---|
| 151 | { |
---|
[5836] | 152 | if (bf::exists(it->first) && !bf::is_directory(it->first)) |
---|
[2710] | 153 | { |
---|
| 154 | ThrowException(General, std::string("The ") + it->second + " directory has been preoccupied by a file! \ |
---|
[8351] | 155 | Please remove " + it->first.BF_GENERIC_STRING()); |
---|
[2710] | 156 | } |
---|
[5836] | 157 | if (bf::create_directories(it->first)) // function may not return true at all (bug?) |
---|
[2710] | 158 | { |
---|
[8858] | 159 | orxout(internal_info) << "Created " << it->second << " directory" << endl; |
---|
[2710] | 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
[2896] | 163 | |
---|
[10509] | 164 | /*static*/ std::string ConfigurablePaths::getDataPathString() |
---|
[2896] | 165 | { |
---|
[8351] | 166 | return getInstance().dataPath_.BF_GENERIC_STRING() + '/'; |
---|
[5836] | 167 | } |
---|
| 168 | |
---|
[10509] | 169 | /*static*/ std::string ConfigurablePaths::getExternalDataPathString() |
---|
[5836] | 170 | { |
---|
[8351] | 171 | return getInstance().externalDataPath_.BF_GENERIC_STRING() + '/'; |
---|
[5836] | 172 | } |
---|
| 173 | |
---|
[10509] | 174 | /*static*/ std::string ConfigurablePaths::getConfigPathString() |
---|
[5836] | 175 | { |
---|
[8351] | 176 | return getInstance().configPath_.BF_GENERIC_STRING() + '/'; |
---|
[5836] | 177 | } |
---|
| 178 | |
---|
[10509] | 179 | /*static*/ std::string ConfigurablePaths::getLogPathString() |
---|
[5836] | 180 | { |
---|
[8351] | 181 | return getInstance().logPath_.BF_GENERIC_STRING() + '/'; |
---|
[5836] | 182 | } |
---|
[1505] | 183 | } |
---|