Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Paths.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 #include "Paths.h"
20 
21 #include "lib/file/file_system.h"
22 #include "lib/sysdep/sysdep.h" // sys_get_executable_name
23 #include "lib/sysdep/filesystem.h" // wrealpath
24 #if OS_WIN
25 # include "lib/sysdep/os/win/wutil.h" // wutil_*Path
26 #elif OS_MACOSX
29 #endif
30 #include "ps/CLogger.h"
31 
32 
34 {
35  m_root = Root(args.GetArg0());
36 
37  m_rdata = RootData(args.GetArg0());
38 
39  const char* subdirectoryName = args.Has("writableRoot")? 0 : "0ad";
40 
41  if(!subdirectoryName)
42  {
43  // Note: if writableRoot option is passed to the game, then
44  // all the data is a subdirectory of the root
47  m_config = m_gameData / "config"/"";
48  m_cache = m_gameData / "cache"/"";
49  m_logs = m_root / "logs"/"";
50  }
51  else // OS-specific path handling
52  {
53 
54 #if OS_ANDROID
55 
56  const OsPath appdata = OsPath("/sdcard/0ad/appdata");
57 
58  // We don't make the game vs. user data distinction on Android
59  m_gameData = appdata/"data"/"";
61  m_config = appdata/"config"/"";
62  m_cache = appdata/"cache"/"";
63  m_logs = appdata/"logs"/"";
64 
65 #elif OS_WIN
66 
67  /* For reasoning behind our Windows paths, see the discussion here:
68  * http://www.wildfiregames.com/forum/index.php?showtopic=14759
69  *
70  * Summary:
71  * 1. Local appdata: for bulky unfriendly data like the cache,
72  * which can be recreated if deleted; doesn't need backing up.
73  * 2. Roaming appdata: for slightly less unfriendly data like config
74  * files that might theoretically be shared between different
75  * machines on a domain.
76  * 3. Personal / My Documents: for data explicitly created by the user,
77  * and which should be visible and easily accessed. We use a non-
78  * localized My Games subfolder for improved organization.
79  */
80 
81  // %localappdata%/0ad/
82  const OsPath localAppdata = wutil_LocalAppdataPath() / subdirectoryName/"";
83  // %appdata%/0ad/
84  const OsPath roamingAppData = wutil_RoamingAppdataPath() / subdirectoryName/"";
85  // My Documents/My Games/0ad/
86  const OsPath personalData = wutil_PersonalPath() / "My Games" / subdirectoryName/"";
87 
88  m_cache = localAppdata / "cache"/"";
89  m_gameData = roamingAppData / "data"/"";
90  m_userData = personalData/"";
91  m_config = roamingAppData / "config"/"";
92  m_logs = localAppdata / "logs"/"";
93 
94 #elif OS_MACOSX
95 
96  /* For reasoning behind our OS X paths, see the discussion here:
97  * http://www.wildfiregames.com/forum/index.php?showtopic=15511
98  *
99  * Summary:
100  * 1. Application Support: most data associated with the app
101  * should be stored here, with few exceptions (e.g. temporary
102  * data, cached data, and managed media files).
103  * 2. Caches: used for non-critial app data that can be easily
104  * regenerated if this directory is deleted. It is not
105  * included in backups by default.
106  *
107  * Note: the paths returned by osx_Get*Path are not guaranteed to exist,
108  * but that's OK since we always create them on demand.
109  */
110 
111  // We probably want to use the same subdirectoryName regardless
112  // of whether running a bundle or from SVN. Apple recommends using
113  // company name, bundle name or bundle identifier.
114  OsPath appSupportPath; // ~/Library/Application Support/0ad
115  OsPath cachePath; // ~/Library/Caches/0ad
116 
117  {
118  std::string path = osx_GetAppSupportPath();
119  ENSURE(!path.empty());
120  appSupportPath = OsPath(path) / subdirectoryName;
121  }
122  {
123  std::string path = osx_GetCachesPath();
124  ENSURE(!path.empty());
125  cachePath = OsPath(path) / subdirectoryName;
126  }
127 
128  // We don't make the game vs. user data distinction on OS X
129  m_gameData = appSupportPath /"";
131  m_cache = cachePath/"";
132  m_config = appSupportPath / "config"/"";
133  m_logs = appSupportPath / "logs"/"";
134 
135 #else // OS_UNIX
136 
137  const char* envHome = getenv("HOME");
138  ENSURE(envHome);
139  const OsPath home(envHome);
140  const OsPath xdgData = XDG_Path("XDG_DATA_HOME", home, home/".local/share/") / subdirectoryName;
141  const OsPath xdgConfig = XDG_Path("XDG_CONFIG_HOME", home, home/".config/" ) / subdirectoryName;
142  const OsPath xdgCache = XDG_Path("XDG_CACHE_HOME", home, home/".cache/" ) / subdirectoryName;
143 
144  // We don't make the game vs. user data distinction on Unix
145  m_gameData = xdgData/"";
147  m_cache = xdgCache/"";
148  m_config = xdgConfig / "config"/"";
149  m_logs = xdgConfig / "logs"/"";
150 
151 #endif
152  }
153 }
154 
155 
156 /*static*/ OsPath Paths::Root(const OsPath& argv0)
157 {
158 #if OS_ANDROID
159  return OsPath("/sdcard/0ad"); // TODO: this is kind of bogus
160 #else
161 
162  // get full path to executable
163  OsPath pathname = sys_ExecutablePathname(); // safe, but requires OS-specific implementation
164  if(pathname.empty()) // failed, use argv[0] instead
165  {
166  errno = 0;
167  pathname = wrealpath(argv0);
168  if(pathname.empty())
170  }
171 
172  // make sure it's valid
173  if(!FileExists(pathname))
174  {
175  LOGERROR(L"Cannot find executable (expected at '%ls')", pathname.string().c_str());
177  }
178 
179  for(size_t i = 0; i < 2; i++) // remove "system/name.exe"
180  pathname = pathname.Parent();
181  return pathname;
182 
183 #endif
184 }
185 
186 /*static*/ OsPath Paths::RootData(const OsPath& argv0)
187 {
188 
189 #ifdef INSTALLED_DATADIR
190  UNUSED2(argv0);
191  return OsPath(STRINGIZE(INSTALLED_DATADIR))/"";
192 #else
193 
194 # if OS_MACOSX
195  if (osx_IsAppBundleValid())
196  {
197  debug_printf(L"Valid app bundle detected\n");
198 
199  std::string resourcesPath = osx_GetBundleResourcesPath();
200  // Ensure we have a valid resources path
201  ENSURE(!resourcesPath.empty());
202 
203  return OsPath(resourcesPath)/"data"/"";
204  }
205 # endif // OS_MACOSX
206 
207  return Root(argv0)/"data"/"";
208 
209 #endif // INSTALLED_DATADIR
210 }
211 
212 /*static*/ OsPath Paths::XDG_Path(const char* envname, const OsPath& home, const OsPath& defaultPath)
213 {
214  const char* path = getenv(envname);
215  // Use if set and non-empty
216  if(path && path[0] != '\0')
217  {
218  if(path[0] != '/') // relative to $HOME
219  return home / path/"";
220  return OsPath(path)/"";
221  }
222  return defaultPath/"";
223 }
#define WARN_IF_ERR(expression)
Definition: status.h:265
#define LOGERROR
Definition: CLogger.h:35
bool osx_IsAppBundleValid()
Check if app is running in a valid bundle.
Definition: osx_bundle.mm:40
OsPath GetArg0() const
Get the value of argv[0], which is typically meant to be the name/path of the program (but the actual...
Definition: CmdLineArgs.cpp:95
const OsPath & Root() const
Returns the game&#39;s root directory.
Definition: Paths.h:35
Paths(const CmdLineArgs &args)
Definition: Paths.cpp:33
OsPath m_logs
Definition: Paths.h:104
Path Parent() const
Definition: path.h:150
OsPath m_cache
Definition: Paths.h:103
C++ interface to Cocoa implementation for getting bundle information.
bool FileExists(const OsPath &pathname)
Definition: file_system.cpp:49
OsPath m_userData
Definition: Paths.h:101
const OsPath & wutil_RoamingAppdataPath()
Definition: wutil.cpp:273
OsPath m_rdata
Definition: Paths.h:97
const OsPath & wutil_LocalAppdataPath()
Definition: wutil.cpp:268
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
#define UNUSED2(param)
mark a function local variable or parameter as unused and avoid the corresponding compiler warning...
static OsPath XDG_Path(const char *envname, const OsPath &home, const OsPath &defaultPath)
Definition: Paths.cpp:212
bool Has(const char *name) const
Test whether the given name was specified, as either -name or -name=value
Definition: CmdLineArgs.cpp:66
Definition: path.h:75
const String & string() const
Definition: path.h:123
Path OsPath
Definition: os_path.h:31
LIB_API OsPath wrealpath(const OsPath &pathname)
OsPath m_root
Definition: Paths.h:96
OsPath m_config
Definition: Paths.h:102
std::string osx_GetCachesPath()
Get the user&#39;s Caches path (typically ~/Library/Caches)
Definition: osx_paths.mm:64
static OsPath RootData(const OsPath &argv0)
Definition: Paths.cpp:186
Status StatusFromErrno()
Definition: status.cpp:105
OsPath m_gameData
Definition: Paths.h:100
bool empty() const
Definition: path.h:118
#define STRINGIZE(id)
OsPath sys_ExecutablePathname()
Definition: bsd.cpp:33
std::string osx_GetAppSupportPath()
Get the user&#39;s Application Support path (typically ~/Library/Application Support) ...
Definition: osx_paths.mm:59
std::string osx_GetBundleResourcesPath()
Get the system path to the bundle&#39;s Resources directory.
Definition: osx_bundle.mm:79
const OsPath & wutil_PersonalPath()
Definition: wutil.cpp:278
void debug_printf(const wchar_t *fmt,...)
write a formatted string to the debug channel, subject to filtering (see below).
Definition: debug.cpp:142
C++ interface to Cocoa implementation for retrieving standard OS X paths.