VCMIDirs.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. std::string VCMIDirs::userCachePath() const
  26. {
  27. return userDataPath();
  28. }
  29. std::string VCMIDirs::userConfigPath() const
  30. {
  31. return userDataPath() + "/config";
  32. }
  33. std::string VCMIDirs::userSavePath() const
  34. {
  35. return userDataPath() + "/Games";
  36. }
  37. std::vector<std::string> VCMIDirs::configPaths() const
  38. {
  39. return std::vector<std::string>(1, dataPaths()[0] + "/config");
  40. }
  41. //FIXME: find way to at least decrease size of this ifdef (along with cleanup in CMake)
  42. #if defined(_WIN32)
  43. std::string VCMIDirs::userDataPath() const
  44. {
  45. return dataPaths()[0];
  46. }
  47. std::string VCMIDirs::libraryPath() const
  48. {
  49. return userDataPath();
  50. }
  51. std::string VCMIDirs::serverPath() const
  52. {
  53. return userDataPath() + "\\" + "VCMI_server.exe";
  54. }
  55. std::vector<std::string> VCMIDirs::dataPaths() const
  56. {
  57. return std::vector<std::string>(1, ".");
  58. }
  59. std::string VCMIDirs::libraryName(std::string basename) const
  60. {
  61. return basename + ".dll";
  62. }
  63. #elif defined(__APPLE__)
  64. std::string VCMIDirs::userDataPath() const
  65. {
  66. // This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
  67. // NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
  68. // UserPath = path([urls[0] path] + "/vcmi").string();
  69. // ...so here goes a bit of hardcode instead
  70. std::string home_dir = ".";
  71. if (getenv("HOME") != nullptr )
  72. home_dir = getenv("HOME");
  73. return boost::filesystem::path(home_dir + "/Library/Application Support/vcmi").string();
  74. }
  75. std::string VCMIDirs::libraryPath() const
  76. {
  77. return ".";
  78. }
  79. std::string VCMIDirs::serverPath() const
  80. {
  81. return "./vcmiserver";
  82. }
  83. std::vector<std::string> VCMIDirs::dataPaths() const
  84. {
  85. return std::vector<std::string>(1, "../Data");
  86. }
  87. std::string VCMIDirs::libraryName(std::string basename) const
  88. {
  89. return "lib" + basename + ".dylib";
  90. }
  91. #else
  92. std::string VCMIDirs::userDataPath() const
  93. {
  94. if (getenv("HOME") != nullptr )
  95. return std::string(getenv("HOME")) + "/.vcmi";
  96. return ".";
  97. }
  98. std::string VCMIDirs::libraryPath() const
  99. {
  100. return M_LIB_DIR;
  101. }
  102. std::string VCMIDirs::serverPath() const
  103. {
  104. return std::string(M_BIN_DIR) + "/" + "vcmiserver";
  105. }
  106. std::vector<std::string> VCMIDirs::dataPaths() const
  107. {
  108. return std::vector<std::string>(1, M_DATA_DIR);
  109. }
  110. std::string VCMIDirs::libraryName(std::string basename) const
  111. {
  112. return "lib" + basename + ".so";
  113. }
  114. #endif