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