2
0

VCMIDirs.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if (getenv("XDG_DATA_HOME") != nullptr )
  130. return std::string(getenv("XDG_DATA_HOME")) + "/vcmi";
  131. if (getenv("HOME") != nullptr )
  132. return std::string(getenv("HOME")) + "/.local/share" + "/vcmi";
  133. return ".";
  134. }
  135. std::string VCMIDirs::userSavePath() const
  136. {
  137. return userDataPath() + "/Saves";
  138. }
  139. // $XDG_CACHE_HOME, default: $HOME/.cache
  140. std::string VCMIDirs::userCachePath() const
  141. {
  142. if (getenv("XDG_CACHE_HOME") != nullptr )
  143. return std::string(getenv("XDG_CACHE_HOME")) + "/vcmi";
  144. if (getenv("HOME") != nullptr )
  145. return std::string(getenv("HOME")) + "/.cache" + "/vcmi";
  146. return ".";
  147. }
  148. // $XDG_CONFIG_HOME, default: $HOME/.config
  149. std::string VCMIDirs::userConfigPath() const
  150. {
  151. if (getenv("XDG_CONFIG_HOME") != nullptr )
  152. return std::string(getenv("XDG_CONFIG_HOME")) + "/vcmi";
  153. if (getenv("HOME") != nullptr )
  154. return std::string(getenv("HOME")) + "/.config" + "/vcmi";
  155. return ".";
  156. }
  157. // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
  158. std::vector<std::string> VCMIDirs::dataPaths() const
  159. {
  160. // construct list in reverse.
  161. // in specification first directory has highest priority
  162. // in vcmi fs last directory has highest priority
  163. std::vector<std::string> ret;
  164. if (getenv("HOME") != nullptr ) // compatibility, should be removed after 0.96
  165. ret.push_back(std::string(getenv("HOME")) + "/.vcmi");
  166. ret.push_back(M_DATA_DIR);
  167. if (getenv("XDG_DATA_DIRS") != nullptr)
  168. {
  169. std::string dataDirsEnv = getenv("XDG_DATA_DIRS");
  170. std::vector<std::string> dataDirs;
  171. boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
  172. for (auto & entry : boost::adaptors::reverse(dataDirs))
  173. ret.push_back(entry + "/vcmi");
  174. }
  175. else
  176. {
  177. ret.push_back("/usr/share/");
  178. ret.push_back("/usr/local/share/");
  179. }
  180. return ret;
  181. }
  182. #endif
  183. std::string VCMIDirs::genHelpString() const
  184. {
  185. return
  186. " game data: " + boost::algorithm::join(dataPaths(), ":") + "\n" +
  187. " libraries: " + libraryPath() + "\n" +
  188. " server: " + serverPath() + "\n" +
  189. "\n" +
  190. " user data: " + userDataPath() + "\n" +
  191. " user cache: " + userCachePath() + "\n" +
  192. " user config: " + userConfigPath() + "\n" +
  193. " user saves: " + userSavePath() + "\n";
  194. }