Line data Source code
1 : /* Copyright (C) 2026 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "Paths.h" 21 : 22 : #include "lib/debug.h" 23 : #include "lib/file/file_system.h" 24 : #include "lib/path.h" 25 : #include "lib/status.h" 26 : #include "lib/sysdep/os.h" 27 : #include "lib/sysdep/sysdep.h" // sys_get_executable_name 28 : #include "ps/CLogger.h" 29 : #include "ps/CStr.h" 30 : #include "ps/GameSetup/CmdLineArgs.h" 31 : 32 : #include <cerrno> 33 : #include <cstdlib> 34 : #include <filesystem> 35 : #include <string> 36 : #include <system_error> 37 : 38 : #if OS_WIN 39 : # include "lib/sysdep/os/win/wutil.h" // wutil_*Path 40 : #elif OS_MACOSX 41 : # include "lib/sysdep/os/osx/osx_paths.h" 42 : # include "lib/sysdep/os/osx/osx_bundle.h" 43 : #endif 44 : 45 : 46 0 : Paths::Paths(const CmdLineArgs& args) 47 : { 48 0 : m_root = Root(args.GetArg0()); 49 : 50 0 : m_rdata = RootData(args.GetArg0()); 51 : 52 0 : const char* subdirectoryName = args.Has("writableRoot")? 0 : "0ad"; 53 : 54 0 : if(!subdirectoryName) 55 : { 56 : // Note: if writableRoot option is passed to the game, then 57 : // all the data is a subdirectory of the root 58 0 : m_gameData = m_rdata; 59 0 : m_userData = m_gameData; 60 0 : m_config = m_gameData / "config"/""; 61 0 : m_cache = m_gameData / "cache"/""; 62 0 : m_logs = m_root / "logs"/""; 63 : } 64 : else // OS-specific path handling 65 : { 66 : 67 : #if OS_ANDROID 68 : 69 : const OsPath appdata = OsPath("/sdcard/0ad/appdata"); 70 : 71 : // We don't make the game vs. user data distinction on Android 72 : m_gameData = appdata/"data"/""; 73 : m_userData = m_gameData; 74 : m_config = appdata/"config"/""; 75 : m_cache = appdata/"cache"/""; 76 : m_logs = appdata/"logs"/""; 77 : 78 : #elif OS_WIN 79 : 80 : /* For reasoning behind our Windows paths, see the discussion here: 81 : * http://www.wildfiregames.com/forum/index.php?showtopic=14759 82 : * 83 : * Summary: 84 : * 1. Local appdata: for bulky unfriendly data like the cache, 85 : * which can be recreated if deleted; doesn't need backing up. 86 : * 2. Roaming appdata: for slightly less unfriendly data like config 87 : * files that might theoretically be shared between different 88 : * machines on a domain. 89 : * 3. Personal / My Documents: for data explicitly created by the user, 90 : * and which should be visible and easily accessed. We use a non- 91 : * localized My Games subfolder for improved organization. 92 : */ 93 : 94 : // %localappdata%/0ad/ 95 : const OsPath localAppdata = wutil_LocalAppdataPath() / subdirectoryName/""; 96 : // %appdata%/0ad/ 97 : const OsPath roamingAppData = wutil_RoamingAppdataPath() / subdirectoryName/""; 98 : // My Documents/My Games/0ad/ 99 : const OsPath personalData = wutil_PersonalPath() / "My Games" / subdirectoryName/""; 100 : 101 : m_cache = localAppdata / "cache"/""; 102 : m_gameData = roamingAppData / "data"/""; 103 : m_userData = personalData/""; 104 : m_config = roamingAppData / "config"/""; 105 : m_logs = localAppdata / "logs"/""; 106 : 107 : #elif OS_MACOSX 108 : 109 : /* For reasoning behind our OS X paths, see the discussion here: 110 : * http://www.wildfiregames.com/forum/index.php?showtopic=15511 111 : * 112 : * Summary: 113 : * 1. Application Support: most data associated with the app 114 : * should be stored here, with few exceptions (e.g. temporary 115 : * data, cached data, and managed media files). 116 : * 2. Caches: used for non-critial app data that can be easily 117 : * regenerated if this directory is deleted. It is not 118 : * included in backups by default. 119 : * 120 : * Note: the paths returned by osx_Get*Path are not guaranteed to exist, 121 : * but that's OK since we always create them on demand. 122 : */ 123 : 124 : // We probably want to use the same subdirectoryName regardless 125 : // of whether running a bundle or from SVN. Apple recommends using 126 : // company name, bundle name or bundle identifier. 127 : OsPath appSupportPath; // ~/Library/Application Support/0ad 128 : OsPath cachePath; // ~/Library/Caches/0ad 129 : 130 : { 131 : std::string path = osx_GetAppSupportPath(); 132 : ENSURE(!path.empty()); 133 : appSupportPath = OsPath(path) / subdirectoryName; 134 : } 135 : { 136 : std::string path = osx_GetCachesPath(); 137 : ENSURE(!path.empty()); 138 : cachePath = OsPath(path) / subdirectoryName; 139 : } 140 : 141 : // We don't make the game vs. user data distinction on OS X 142 : m_gameData = appSupportPath /""; 143 : m_userData = m_gameData; 144 : m_cache = cachePath/""; 145 : m_config = appSupportPath / "config"/""; 146 : m_logs = appSupportPath / "logs"/""; 147 : 148 : #else // OS_UNIX 149 : 150 0 : const char* envHome = getenv("HOME"); 151 0 : ENSURE(envHome); 152 0 : const OsPath home(envHome); 153 0 : const OsPath xdgData = XDG_Path("XDG_DATA_HOME", home, home/".local/share/") / subdirectoryName; 154 0 : const OsPath xdgConfig = XDG_Path("XDG_CONFIG_HOME", home, home/".config/" ) / subdirectoryName; 155 0 : const OsPath xdgCache = XDG_Path("XDG_CACHE_HOME", home, home/".cache/" ) / subdirectoryName; 156 0 : const OsPath xdgState = XDG_Path("XDG_STATE_HOME", home, home/".local/state/") / subdirectoryName; 157 : 158 : // We don't make the game vs. user data distinction on Unix 159 0 : m_gameData = xdgData/""; 160 0 : m_userData = m_gameData; 161 0 : m_cache = xdgCache/""; 162 0 : m_config = xdgConfig / "config"/""; 163 0 : m_logs = xdgState / "log"/""; 164 : 165 : #endif 166 : } 167 0 : } 168 : 169 : 170 0 : /*static*/ OsPath Paths::Root(const OsPath& argv0) 171 : { 172 : #if OS_ANDROID 173 : return OsPath("/sdcard/0ad"); // TODO: this is kind of bogus 174 : #else 175 : 176 : // get full path to executable 177 0 : OsPath pathname = sys_ExecutablePathname(); // safe, but requires OS-specific implementation 178 0 : if(pathname.empty()) // failed, use argv[0] instead 179 : { 180 0 : const std::filesystem::path rpath{argv0.string()}; 181 0 : std::error_code ec{}; 182 0 : const std::filesystem::path cpath{std::filesystem::canonical(rpath, ec)}; 183 0 : if (ec) 184 0 : LOGERROR("Failed to get absolute path of argv0, reason: {}", ec.message()); 185 : else 186 0 : pathname = OsPath(cpath.wstring()); 187 0 : } 188 : 189 : // make sure it's valid 190 0 : if(!std::filesystem::is_regular_file(pathname.string())) 191 : { 192 0 : LOGERROR("Cannot find executable (expected at '{}')", pathname.string8()); 193 0 : WARN_IF_ERR(StatusFromErrno()); 194 : } 195 : 196 0 : for(size_t i = 0; i < 2; i++) // remove "system/name.exe" 197 0 : pathname = pathname.Parent(); 198 0 : return pathname; 199 : 200 : #endif 201 0 : } 202 : 203 0 : /*static*/ OsPath Paths::RootData([[maybe_unused]] const OsPath& argv0) 204 : { 205 : 206 : #ifdef INSTALLED_DATADIR 207 : return OsPath(STRINGIZE(INSTALLED_DATADIR))/""; 208 : #else 209 : 210 : # if OS_MACOSX 211 : if (osx_IsAppBundleValid()) 212 : { 213 : debug_printf("Valid app bundle detected\n"); 214 : 215 : std::string resourcesPath = osx_GetBundleResourcesPath(); 216 : // Ensure we have a valid resources path 217 : ENSURE(!resourcesPath.empty()); 218 : 219 : return OsPath(resourcesPath)/"data"/""; 220 : } 221 : # endif // OS_MACOSX 222 : 223 0 : return Root(argv0)/"data"/""; 224 : 225 : #endif // INSTALLED_DATADIR 226 : } 227 : 228 0 : /*static*/ OsPath Paths::XDG_Path(const char* envname, const OsPath& home, const OsPath& defaultPath) 229 : { 230 0 : const char* path = getenv(envname); 231 : // Use if set and non-empty 232 0 : if(path && path[0] != '\0') 233 : { 234 0 : if(path[0] != '/') // relative to $HOME 235 0 : return home / path/""; 236 0 : return OsPath(path)/""; 237 : } 238 0 : return defaultPath/""; 239 : }