VCMIDirs.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "StdInc.h"
  2. #include "VCMIDirs.h"
  3. /*
  4. * VCMIDirs.cpp, part of VCMI engine
  5. *
  6. * Authors: listed in file AUTHORS in main folder
  7. *
  8. * License: GNU General Public License v2.0 or later
  9. * Full text of license available in license.txt file, in main folder
  10. *
  11. */
  12. static VCMIDirs VCMIDirsGlobal;
  13. VCMIDirs::VCMIDirs()
  14. {
  15. // initialize local directory and create folders to which VCMI needs write access
  16. boost::filesystem::create_directory(userDataPath());
  17. boost::filesystem::create_directory(userCachePath());
  18. boost::filesystem::create_directory(userConfigPath());
  19. boost::filesystem::create_directory(userSavePath());
  20. }
  21. VCMIDirs & VCMIDirs::get()
  22. {
  23. return VCMIDirsGlobal;
  24. }
  25. //FIXME: find way to at least decrease size of this ifdef (along with cleanup in CMake)
  26. #if defined(_WIN32)
  27. std::string VCMIDirs::userCachePath() const
  28. {
  29. return userDataPath();
  30. }
  31. std::string VCMIDirs::userConfigPath() const
  32. {
  33. return userDataPath() + "/config";
  34. }
  35. std::string VCMIDirs::userSavePath() const
  36. {
  37. return userDataPath() + "/Games";
  38. }
  39. std::string VCMIDirs::userDataPath() const
  40. {
  41. const std::string homeDir = std::getenv("userprofile");
  42. return homeDir + "\\vcmi";
  43. //return dataPaths()[0];
  44. }
  45. std::string VCMIDirs::libraryPath() const
  46. {
  47. return ".";
  48. }
  49. std::string VCMIDirs::clientPath() const
  50. {
  51. return libraryPath() + "\\" + "VCMI_client.exe";
  52. }
  53. std::string VCMIDirs::serverPath() const
  54. {
  55. return libraryPath() + "\\" + "VCMI_server.exe";
  56. }
  57. std::vector<std::string> VCMIDirs::dataPaths() const
  58. {
  59. return std::vector<std::string>(1, ".");
  60. }
  61. std::string VCMIDirs::libraryName(std::string basename) const
  62. {
  63. return basename + ".dll";
  64. }
  65. #elif defined(__APPLE__)
  66. std::string VCMIDirs::userCachePath() const
  67. {
  68. return userDataPath();
  69. }
  70. std::string VCMIDirs::userConfigPath() const
  71. {
  72. return userDataPath() + "/config";
  73. }
  74. std::string VCMIDirs::userSavePath() const
  75. {
  76. return userDataPath() + "/Games";
  77. }
  78. std::string VCMIDirs::userDataPath() const
  79. {
  80. // This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
  81. // NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
  82. // UserPath = path([urls[0] path] + "/vcmi").string();
  83. // ...so here goes a bit of hardcode instead
  84. std::string home_dir = ".";
  85. if (getenv("HOME") != nullptr )
  86. home_dir = getenv("HOME");
  87. return boost::filesystem::path(home_dir + "/Library/Application Support/vcmi").string();
  88. }
  89. std::string VCMIDirs::libraryPath() const
  90. {
  91. return ".";
  92. }
  93. std::string VCMIDirs::clientPath() const
  94. {
  95. return "./vcmiclient";
  96. }
  97. std::string VCMIDirs::serverPath() const
  98. {
  99. return "./vcmiserver";
  100. }
  101. std::vector<std::string> VCMIDirs::dataPaths() const
  102. {
  103. return std::vector<std::string>(1, "../Data");
  104. }
  105. std::string VCMIDirs::libraryName(std::string basename) const
  106. {
  107. return "lib" + basename + ".dylib";
  108. }
  109. #else
  110. std::string VCMIDirs::libraryName(std::string basename) const
  111. {
  112. return "lib" + basename + ".so";
  113. }
  114. std::string VCMIDirs::libraryPath() const
  115. {
  116. return M_LIB_DIR;
  117. }
  118. std::string VCMIDirs::clientPath() const
  119. {
  120. return std::string(M_BIN_DIR) + "/" + "vcmiclient";
  121. }
  122. std::string VCMIDirs::serverPath() const
  123. {
  124. return std::string(M_BIN_DIR) + "/" + "vcmiserver";
  125. }
  126. // $XDG_DATA_HOME, default: $HOME/.local/share
  127. std::string VCMIDirs::userDataPath() const
  128. {
  129. #ifdef __ANDROID__
  130. // on Android HOME will be set to something like /sdcard/data/Android/is.xyz.vcmi/files/
  131. return std::string(getenv("HOME"));
  132. #else
  133. if (getenv("XDG_DATA_HOME") != nullptr )
  134. return std::string(getenv("XDG_DATA_HOME")) + "/vcmi";
  135. if (getenv("HOME") != nullptr )
  136. return std::string(getenv("HOME")) + "/.local/share" + "/vcmi";
  137. return ".";
  138. #endif
  139. }
  140. std::string VCMIDirs::userSavePath() const
  141. {
  142. return userDataPath() + "/Saves";
  143. }
  144. // $XDG_CACHE_HOME, default: $HOME/.cache
  145. std::string VCMIDirs::userCachePath() const
  146. {
  147. #ifdef __ANDROID__
  148. return userDataPath() + "/cache";
  149. #else
  150. if (getenv("XDG_CACHE_HOME") != nullptr )
  151. return std::string(getenv("XDG_CACHE_HOME")) + "/vcmi";
  152. if (getenv("HOME") != nullptr )
  153. return std::string(getenv("HOME")) + "/.cache" + "/vcmi";
  154. return ".";
  155. #endif
  156. }
  157. // $XDG_CONFIG_HOME, default: $HOME/.config
  158. std::string VCMIDirs::userConfigPath() const
  159. {
  160. #ifdef __ANDROID__
  161. return userDataPath() + "/config";
  162. #else
  163. if (getenv("XDG_CONFIG_HOME") != nullptr )
  164. return std::string(getenv("XDG_CONFIG_HOME")) + "/vcmi";
  165. if (getenv("HOME") != nullptr )
  166. return std::string(getenv("HOME")) + "/.config" + "/vcmi";
  167. return ".";
  168. #endif
  169. }
  170. // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
  171. std::vector<std::string> VCMIDirs::dataPaths() const
  172. {
  173. // construct list in reverse.
  174. // in specification first directory has highest priority
  175. // in vcmi fs last directory has highest priority
  176. std::vector<std::string> ret;
  177. #ifdef __ANDROID__
  178. ret.push_back(userDataPath());
  179. #else
  180. if (getenv("HOME") != nullptr ) // compatibility, should be removed after 0.96
  181. ret.push_back(std::string(getenv("HOME")) + "/.vcmi");
  182. ret.push_back(M_DATA_DIR);
  183. if (getenv("XDG_DATA_DIRS") != nullptr)
  184. {
  185. std::string dataDirsEnv = getenv("XDG_DATA_DIRS");
  186. std::vector<std::string> dataDirs;
  187. boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
  188. for (auto & entry : boost::adaptors::reverse(dataDirs))
  189. ret.push_back(entry + "/vcmi");
  190. }
  191. else
  192. {
  193. ret.push_back("/usr/share/");
  194. ret.push_back("/usr/local/share/");
  195. }
  196. #endif
  197. return ret;
  198. }
  199. #endif
  200. std::string VCMIDirs::genHelpString() const
  201. {
  202. return
  203. " game data: " + boost::algorithm::join(dataPaths(), ":") + "\n" +
  204. " libraries: " + libraryPath() + "\n" +
  205. " server: " + serverPath() + "\n" +
  206. "\n" +
  207. " user data: " + userDataPath() + "\n" +
  208. " user cache: " + userCachePath() + "\n" +
  209. " user config: " + userConfigPath() + "\n" +
  210. " user saves: " + userSavePath() + "\n";
  211. }