VCMIDirs.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * VCMIDirs.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "VCMIDirs.h"
  12. #ifdef VCMI_IOS
  13. #include "iOS_utils.h"
  14. #endif
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. namespace bfs = boost::filesystem;
  17. bfs::path IVCMIDirs::userLogsPath() const { return userCachePath(); }
  18. bfs::path IVCMIDirs::userSavePath() const { return userDataPath() / "Saves"; }
  19. bfs::path IVCMIDirs::fullLibraryPath(const std::string &desiredFolder, const std::string &baseLibName) const
  20. {
  21. return libraryPath() / desiredFolder / libraryName(baseLibName);
  22. }
  23. std::string IVCMIDirs::genHelpString() const
  24. {
  25. std::vector<std::string> tempVec;
  26. for (const bfs::path & path : dataPaths())
  27. tempVec.push_back(path.string());
  28. const auto gdStringA = boost::algorithm::join(tempVec, ":");
  29. return
  30. " game data: " + gdStringA + "\n"
  31. " libraries: " + libraryPath().string() + "\n"
  32. " server: " + serverPath().string() + "\n"
  33. "\n"
  34. " user data: " + userDataPath().string() + "\n"
  35. " user cache: " + userCachePath().string() + "\n"
  36. " user config: " + userConfigPath().string() + "\n"
  37. " user logs: " + userLogsPath().string() + "\n"
  38. " user saves: " + userSavePath().string() + "\n"; // Should end without new-line?
  39. }
  40. void IVCMIDirs::init()
  41. {
  42. // TODO: Log errors
  43. bfs::create_directories(userDataPath());
  44. bfs::create_directories(userCachePath());
  45. bfs::create_directories(userConfigPath());
  46. bfs::create_directories(userLogsPath());
  47. bfs::create_directories(userSavePath());
  48. }
  49. #ifdef VCMI_ANDROID
  50. #include "CAndroidVMHelper.h"
  51. #endif
  52. #ifdef VCMI_WINDOWS
  53. #ifdef __MINGW32__
  54. #define _WIN32_IE 0x0500
  55. #ifndef CSIDL_MYDOCUMENTS
  56. #define CSIDL_MYDOCUMENTS CSIDL_PERSONAL
  57. #endif
  58. #endif // __MINGW32__
  59. #include <windows.h>
  60. #include <shlobj.h>
  61. #include <shellapi.h>
  62. // Generates script file named _temp.bat in 'to' directory and runs it
  63. // Script will:
  64. // - Wait util 'exeName' ends.
  65. // - Copy all files from 'from' to 'to'
  66. // - Ask user to replace files existed in 'to'.
  67. // - Run 'exeName'
  68. // - Delete itself.
  69. bool StartBatchCopyDataProgram(
  70. const bfs::path& from, const bfs::path& to, const bfs::path& exeName,
  71. const bfs::path& currentPath = bfs::current_path())
  72. {
  73. static const char base[] =
  74. "@echo off" "\n"
  75. "echo Preparing to move VCMI data system." "\n"
  76. ":CLIENT_RUNNING_LOOP" "\n"
  77. "TASKLIST | FIND /I %1% > nul" "\n"
  78. "IF ERRORLEVEL 1 (" "\n"
  79. "GOTO CLIENT_NOT_RUNNING" "\n"
  80. ") ELSE (" "\n"
  81. "echo %1% is still running..." "\n"
  82. "echo Waiting until process ends..." "\n"
  83. "ping 1.1.1.1 -n 1 -w 3000 > nul" "\n" // Sleep ~3 seconds. I love Windows :)
  84. "goto :CLIENT_RUNNING_LOOP" "\n"
  85. ")" "\n"
  86. ":CLIENT_NOT_RUNNING" "\n"
  87. "echo %1% turned off..." "\n"
  88. "echo Attempt to move datas." "\n"
  89. "echo From: %2%" "\n"
  90. "echo To: %4%" "\n"
  91. "echo Please resolve any conflicts..." "\n"
  92. "move /-Y %3% %4%" "\n" // Move all files from %3% to %4%.
  93. // /-Y ask what to do when file exists in %4%
  94. ":REMOVE_OLD_DIR" "\n"
  95. "rd %2% || rem" "\n" // Remove empty directory. Sets error flag if fail.
  96. "IF ERRORLEVEL 145 (" "\n" // Directory not empty
  97. "echo Directory %2% is not empty." "\n"
  98. "echo Please move rest of files manually now." "\n"
  99. "pause" "\n" // Press any key to continue...
  100. "goto REMOVE_OLD_DIR" "\n"
  101. ")" "\n"
  102. "echo Game data updated succefully." "\n"
  103. "echo Please update your shortcuts." "\n"
  104. "echo Press any key to start a game . . ." "\n"
  105. "pause > nul" "\n"
  106. "%5%" "\n"
  107. "del \"%%~f0\"&exit" "\n" // Script deletes itself
  108. ;
  109. const auto startGameString =
  110. bfs::equivalent(currentPath, from) ?
  111. (boost::format("start \"\" %1%") % (to / exeName)) : // Start game in new path.
  112. (boost::format("start \"\" /D %1% %2%") % currentPath % (to / exeName)); // Start game in 'currentPath"
  113. const bfs::path bathFilename = to / "_temp.bat";
  114. bfs::ofstream bathFile(bathFilename, bfs::ofstream::trunc | bfs::ofstream::out);
  115. if (!bathFile.is_open())
  116. return false;
  117. bathFile << (boost::format(base) % exeName % from % (from / "*.*") % to % startGameString.str()).str();
  118. bathFile.close();
  119. std::system(("start \"Updating VCMI datas\" /D \"" + to.string() + "\" \"" + bathFilename.string() + '\"').c_str());
  120. // start won't block std::system
  121. // /D start bat in other directory insteand of current directory.
  122. return true;
  123. }
  124. class VCMIDirsWIN32 final : public IVCMIDirs
  125. {
  126. public:
  127. boost::filesystem::path userDataPath() const override;
  128. boost::filesystem::path userCachePath() const override;
  129. boost::filesystem::path userConfigPath() const override;
  130. std::vector<boost::filesystem::path> dataPaths() const override;
  131. boost::filesystem::path clientPath() const override;
  132. boost::filesystem::path serverPath() const override;
  133. boost::filesystem::path libraryPath() const override;
  134. boost::filesystem::path binaryPath() const override;
  135. std::string libraryName(const std::string& basename) const override;
  136. void init() override;
  137. protected:
  138. boost::filesystem::path oldUserDataPath() const;
  139. boost::filesystem::path oldUserSavePath() const;
  140. };
  141. void VCMIDirsWIN32::init()
  142. {
  143. // Call base (init dirs)
  144. IVCMIDirs::init();
  145. // Moves one directory (from) contents to another directory (to)
  146. // Shows user the "moving file dialog" and ask to resolve conflits.
  147. // If necessary updates current directory.
  148. auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to) -> bool
  149. {
  150. if (!bfs::is_directory(from))
  151. return true; // Nothing to do here. Flies away.
  152. if (bfs::is_empty(from))
  153. {
  154. if (bfs::current_path() == from)
  155. bfs::current_path(to);
  156. bfs::remove(from);
  157. return true; // Nothing to do here. Flies away.
  158. }
  159. if (!bfs::is_directory(to))
  160. {
  161. // IVCMIDirs::init() should create all destination directories.
  162. // TODO: Log fact, that we shouldn't be here.
  163. bfs::create_directories(to);
  164. }
  165. // Why the hell path strings should be end with double null :/
  166. auto makeDoubleNulled = [](const bfs::path& path) -> std::unique_ptr<wchar_t[]>
  167. {
  168. const std::wstring& pathStr = path.native();
  169. std::unique_ptr<wchar_t[]> result(new wchar_t[pathStr.length() + 2]);
  170. size_t i = 0;
  171. for (const wchar_t ch : pathStr)
  172. result[i++] = ch;
  173. result[i++] = L'\0';
  174. result[i++] = L'\0';
  175. return result;
  176. };
  177. auto fromDNulled = makeDoubleNulled(from / L"*.*");
  178. auto toDNulled = makeDoubleNulled(to);
  179. SHFILEOPSTRUCTW fileOp;
  180. fileOp.hwnd = GetConsoleWindow();
  181. fileOp.wFunc = FO_MOVE;
  182. fileOp.pFrom = fromDNulled.get();
  183. fileOp.pTo = toDNulled.get();
  184. fileOp.fFlags = 0;
  185. fileOp.hNameMappings = nullptr;
  186. fileOp.lpszProgressTitle = nullptr;
  187. const int errorCode = SHFileOperationW(&fileOp);
  188. if (errorCode != 0) // TODO: Log error. User should try to move files.
  189. return false;
  190. else if (fileOp.fAnyOperationsAborted) // TODO: Log warn. User aborted operation. User should move files.
  191. return false;
  192. else if (!bfs::is_empty(from)) // TODO: Log warn. Some files not moved. User should try to move files.
  193. return false;
  194. if (bfs::current_path() == from)
  195. bfs::current_path(to);
  196. // TODO: Log fact that we moved files succefully.
  197. bfs::remove(from);
  198. return true;
  199. };
  200. // Retrieves the fully qualified path for the file that contains the specified module.
  201. // The module must have been loaded by the current process.
  202. // If this parameter is nullptr, retrieves the path of the executable file of the current process.
  203. auto getModulePath = [](HMODULE hModule) -> bfs::path
  204. {
  205. wchar_t exePathW[MAX_PATH];
  206. DWORD nSize = GetModuleFileNameW(hModule, exePathW, MAX_PATH);
  207. DWORD error = GetLastError();
  208. // WARN: Windows XP don't set ERROR_INSUFFICIENT_BUFFER error.
  209. if (nSize != 0 && error != ERROR_INSUFFICIENT_BUFFER)
  210. return bfs::path(std::wstring(exePathW, nSize));
  211. // TODO: Error handling
  212. return bfs::path();
  213. };
  214. // Moves one directory contents to another directory
  215. // Shows user the "moving file dialog" and ask to resolve conflicts.
  216. // It takes into account that 'from' path can contain current executable.
  217. // If necessary closes program and starts update script.
  218. auto advancedMoveDirIfExists = [getModulePath, moveDirIfExists](const bfs::path& from, const bfs::path& to) -> bool
  219. {
  220. const bfs::path executablePath = getModulePath(nullptr);
  221. // VCMI cann't determine executable path.
  222. // Use standard way to move directory and exit function.
  223. if (executablePath.empty())
  224. return moveDirIfExists(from, to);
  225. const bfs::path executableName = executablePath.filename();
  226. // Current executabl isn't in 'from' path.
  227. // Use standard way to move directory and exit function.
  228. if (!bfs::equivalent(executablePath, from / executableName))
  229. return moveDirIfExists(from, to);
  230. // Try standard way to move directory.
  231. // I don't know how other systems, but Windows 8.1 allow to move running executable.
  232. if (moveDirIfExists(from, to))
  233. return true;
  234. // Start copying script and exit program.
  235. if (StartBatchCopyDataProgram(from, to, executableName))
  236. exit(ERROR_SUCCESS);
  237. // Everything failed :C
  238. return false;
  239. };
  240. moveDirIfExists(oldUserSavePath(), userSavePath());
  241. advancedMoveDirIfExists(oldUserDataPath(), userDataPath());
  242. }
  243. bfs::path VCMIDirsWIN32::userDataPath() const
  244. {
  245. wchar_t profileDir[MAX_PATH];
  246. if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_MYDOCUMENTS, FALSE) != FALSE)
  247. return bfs::path(profileDir) / "My Games" / "vcmi";
  248. return ".";
  249. }
  250. bfs::path VCMIDirsWIN32::oldUserDataPath() const
  251. {
  252. wchar_t profileDir[MAX_PATH];
  253. if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
  254. {
  255. #if defined(_MSC_VER) && _MSC_VER >= 1700
  256. wchar_t* buffer;
  257. size_t bufferSize;
  258. errno_t result = _wdupenv_s(&buffer, &bufferSize, L"userprofile");
  259. if (result == 0)
  260. {
  261. bfs::path result(std::wstring(buffer, bufferSize));
  262. free(buffer);
  263. return result;
  264. }
  265. #else
  266. const char* profileDirA;
  267. if ((profileDirA = std::getenv("userprofile"))) // STL way succeed
  268. return bfs::path(profileDirA) / "vcmi";
  269. #endif
  270. else
  271. return "."; // Every thing failed, return current directory.
  272. }
  273. else
  274. return bfs::path(profileDir) / "vcmi";
  275. //return dataPaths()[0] ???;
  276. }
  277. bfs::path VCMIDirsWIN32::oldUserSavePath() const { return userDataPath() / "Games"; }
  278. bfs::path VCMIDirsWIN32::userCachePath() const { return userDataPath(); }
  279. bfs::path VCMIDirsWIN32::userConfigPath() const { return userDataPath() / "config"; }
  280. std::vector<bfs::path> VCMIDirsWIN32::dataPaths() const
  281. {
  282. return std::vector<bfs::path>(1, bfs::path("."));
  283. }
  284. bfs::path VCMIDirsWIN32::clientPath() const { return binaryPath() / "VCMI_client.exe"; }
  285. bfs::path VCMIDirsWIN32::serverPath() const { return binaryPath() / "VCMI_server.exe"; }
  286. bfs::path VCMIDirsWIN32::libraryPath() const { return "."; }
  287. bfs::path VCMIDirsWIN32::binaryPath() const { return "."; }
  288. std::string VCMIDirsWIN32::libraryName(const std::string& basename) const { return basename + ".dll"; }
  289. #elif defined(VCMI_UNIX)
  290. class IVCMIDirsUNIX : public IVCMIDirs
  291. {
  292. public:
  293. boost::filesystem::path clientPath() const override;
  294. boost::filesystem::path serverPath() const override;
  295. virtual bool developmentMode() const;
  296. };
  297. bool IVCMIDirsUNIX::developmentMode() const
  298. {
  299. // We want to be able to run VCMI from single directory. E.g to run from build output directory
  300. return bfs::exists("AI") && bfs::exists("config") && bfs::exists("Mods") && bfs::exists("vcmiserver") && bfs::exists("vcmiclient");
  301. }
  302. bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiclient"; }
  303. bfs::path IVCMIDirsUNIX::serverPath() const { return binaryPath() / "vcmiserver"; }
  304. #ifdef VCMI_APPLE
  305. class VCMIDirsApple : public IVCMIDirsUNIX
  306. {
  307. public:
  308. bfs::path userConfigPath() const override;
  309. std::string libraryName(const std::string& basename) const override;
  310. };
  311. bfs::path VCMIDirsApple::userConfigPath() const { return userDataPath() / "config"; }
  312. std::string VCMIDirsApple::libraryName(const std::string& basename) const { return "lib" + basename + ".dylib"; }
  313. #ifdef VCMI_IOS
  314. class VCMIDirsIOS final : public VCMIDirsApple
  315. {
  316. public:
  317. bfs::path userDataPath() const override;
  318. bfs::path userCachePath() const override;
  319. bfs::path userLogsPath() const override;
  320. std::vector<bfs::path> dataPaths() const override;
  321. bfs::path libraryPath() const override;
  322. bfs::path binaryPath() const override;
  323. };
  324. bfs::path VCMIDirsIOS::userDataPath() const { return {iOS_utils::documentsPath()}; }
  325. bfs::path VCMIDirsIOS::userCachePath() const { return {iOS_utils::cachesPath()}; }
  326. bfs::path VCMIDirsIOS::userLogsPath() const { return {iOS_utils::documentsPath()}; }
  327. std::vector<bfs::path> VCMIDirsIOS::dataPaths() const
  328. {
  329. std::vector<bfs::path> paths;
  330. paths.reserve(4);
  331. #ifdef VCMI_IOS_SIM
  332. paths.emplace_back(iOS_utils::hostApplicationSupportPath());
  333. #endif
  334. paths.emplace_back(userDataPath());
  335. paths.emplace_back(iOS_utils::documentsPath());
  336. paths.emplace_back(binaryPath());
  337. return paths;
  338. }
  339. bfs::path VCMIDirsIOS::libraryPath() const { return {iOS_utils::frameworksPath()}; }
  340. bfs::path VCMIDirsIOS::binaryPath() const { return {iOS_utils::bundlePath()}; }
  341. #elif defined(VCMI_MAC)
  342. class VCMIDirsOSX final : public VCMIDirsApple
  343. {
  344. public:
  345. boost::filesystem::path userDataPath() const override;
  346. boost::filesystem::path userCachePath() const override;
  347. boost::filesystem::path userLogsPath() const override;
  348. std::vector<boost::filesystem::path> dataPaths() const override;
  349. boost::filesystem::path libraryPath() const override;
  350. boost::filesystem::path binaryPath() const override;
  351. void init() override;
  352. };
  353. void VCMIDirsOSX::init()
  354. {
  355. // Call base (init dirs)
  356. IVCMIDirsUNIX::init();
  357. auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to)
  358. {
  359. if (!bfs::is_directory(from))
  360. return; // Nothing to do here. Flies away.
  361. if (bfs::is_empty(from))
  362. {
  363. bfs::remove(from);
  364. return; // Nothing to do here. Flies away.
  365. }
  366. if (!bfs::is_directory(to))
  367. {
  368. // IVCMIDirs::init() should create all destination directories.
  369. // TODO: Log fact, that we shouldn't be here.
  370. bfs::create_directories(to);
  371. }
  372. for (bfs::directory_iterator file(from); file != bfs::directory_iterator(); ++file)
  373. {
  374. const boost::filesystem::path& srcFilePath = file->path();
  375. const boost::filesystem::path dstFilePath = to / srcFilePath.filename();
  376. // TODO: Aplication should ask user what to do when file exists:
  377. // replace/ignore/stop process/replace all/ignore all
  378. if (!boost::filesystem::exists(dstFilePath))
  379. bfs::rename(srcFilePath, dstFilePath);
  380. }
  381. if (!bfs::is_empty(from)); // TODO: Log warn. Some files not moved. User should try to move files.
  382. else
  383. bfs::remove(from);
  384. };
  385. moveDirIfExists(userDataPath() / "Games", userSavePath());
  386. }
  387. bfs::path VCMIDirsOSX::userDataPath() const
  388. {
  389. // This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
  390. // NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
  391. // UserPath = path([urls[0] path] + "/vcmi").string();
  392. // ...so here goes a bit of hardcode instead
  393. const char* homeDir = getenv("HOME"); // Should be std::getenv?
  394. if (homeDir == nullptr)
  395. homeDir = ".";
  396. return bfs::path(homeDir) / "Library" / "Application Support" / "vcmi";
  397. }
  398. bfs::path VCMIDirsOSX::userCachePath() const { return userDataPath(); }
  399. bfs::path VCMIDirsOSX::userLogsPath() const
  400. {
  401. // TODO: use proper objc code from Foundation framework
  402. if(const auto homeDir = std::getenv("HOME"))
  403. return bfs::path{homeDir} / "Library" / "Logs" / "vcmi";
  404. return IVCMIDirsUNIX::userLogsPath();
  405. }
  406. std::vector<bfs::path> VCMIDirsOSX::dataPaths() const
  407. {
  408. std::vector<bfs::path> ret;
  409. //FIXME: need some proper codepath for detecting running from build output directory
  410. if(developmentMode())
  411. {
  412. ret.push_back(".");
  413. }
  414. else
  415. {
  416. ret.push_back("../Resources/Data");
  417. }
  418. return ret;
  419. }
  420. bfs::path VCMIDirsOSX::libraryPath() const { return "."; }
  421. bfs::path VCMIDirsOSX::binaryPath() const { return "."; }
  422. #endif // VCMI_IOS, VCMI_MAC
  423. #elif defined(VCMI_XDG)
  424. class VCMIDirsXDG : public IVCMIDirsUNIX
  425. {
  426. public:
  427. boost::filesystem::path userDataPath() const override;
  428. boost::filesystem::path userCachePath() const override;
  429. boost::filesystem::path userConfigPath() const override;
  430. std::vector<boost::filesystem::path> dataPaths() const override;
  431. boost::filesystem::path libraryPath() const override;
  432. boost::filesystem::path binaryPath() const override;
  433. std::string libraryName(const std::string& basename) const override;
  434. };
  435. bfs::path VCMIDirsXDG::userDataPath() const
  436. {
  437. // $XDG_DATA_HOME, default: $HOME/.local/share
  438. const char* homeDir;
  439. if((homeDir = getenv("XDG_DATA_HOME")))
  440. return bfs::path(homeDir) / "vcmi";
  441. else if((homeDir = getenv("HOME")))
  442. return bfs::path(homeDir) / ".local" / "share" / "vcmi";
  443. else
  444. return ".";
  445. }
  446. bfs::path VCMIDirsXDG::userCachePath() const
  447. {
  448. // $XDG_CACHE_HOME, default: $HOME/.cache
  449. const char * tempResult;
  450. if ((tempResult = getenv("XDG_CACHE_HOME")))
  451. return bfs::path(tempResult) / "vcmi";
  452. else if ((tempResult = getenv("HOME")))
  453. return bfs::path(tempResult) / ".cache" / "vcmi";
  454. else
  455. return ".";
  456. }
  457. bfs::path VCMIDirsXDG::userConfigPath() const
  458. {
  459. // $XDG_CONFIG_HOME, default: $HOME/.config
  460. const char * tempResult;
  461. if ((tempResult = getenv("XDG_CONFIG_HOME")))
  462. return bfs::path(tempResult) / "vcmi";
  463. else if ((tempResult = getenv("HOME")))
  464. return bfs::path(tempResult) / ".config" / "vcmi";
  465. else
  466. return ".";
  467. }
  468. std::vector<bfs::path> VCMIDirsXDG::dataPaths() const
  469. {
  470. // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
  471. // construct list in reverse.
  472. // in specification first directory has highest priority
  473. // in vcmi fs last directory has highest priority
  474. std::vector<bfs::path> ret;
  475. if(developmentMode())
  476. {
  477. //For now we'll disable usage of system directories when VCMI running from bin directory
  478. ret.push_back(".");
  479. }
  480. else
  481. {
  482. ret.push_back(M_DATA_DIR);
  483. const char * tempResult;
  484. if((tempResult = getenv("XDG_DATA_DIRS")) != nullptr)
  485. {
  486. std::string dataDirsEnv = tempResult;
  487. std::vector<std::string> dataDirs;
  488. boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
  489. for (auto & entry : boost::adaptors::reverse(dataDirs))
  490. ret.push_back(bfs::path(entry) / "vcmi");
  491. }
  492. else
  493. {
  494. ret.push_back(bfs::path("/usr/share") / "vcmi");
  495. ret.push_back(bfs::path("/usr/local/share") / "vcmi");
  496. }
  497. // Debian and other distributions might want to use it while it's not part of XDG
  498. ret.push_back(bfs::path("/usr/share/games") / "vcmi");
  499. }
  500. return ret;
  501. }
  502. bfs::path VCMIDirsXDG::libraryPath() const
  503. {
  504. if(developmentMode())
  505. return ".";
  506. else
  507. return M_LIB_DIR;
  508. }
  509. bfs::path VCMIDirsXDG::binaryPath() const
  510. {
  511. if(developmentMode())
  512. return ".";
  513. else
  514. return M_BIN_DIR;
  515. }
  516. std::string VCMIDirsXDG::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
  517. #ifdef VCMI_ANDROID
  518. class VCMIDirsAndroid : public VCMIDirsXDG
  519. {
  520. std::string basePath;
  521. std::string internalPath;
  522. std::string nativePath;
  523. public:
  524. bfs::path fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const override;
  525. bfs::path libraryPath() const override;
  526. bfs::path userDataPath() const override;
  527. bfs::path userCachePath() const override;
  528. bfs::path userConfigPath() const override;
  529. std::vector<bfs::path> dataPaths() const override;
  530. void init() override;
  531. };
  532. bfs::path VCMIDirsAndroid::libraryPath() const { return nativePath; }
  533. bfs::path VCMIDirsAndroid::userDataPath() const { return basePath; }
  534. bfs::path VCMIDirsAndroid::userCachePath() const { return userDataPath() / "cache"; }
  535. bfs::path VCMIDirsAndroid::userConfigPath() const { return userDataPath() / "config"; }
  536. bfs::path VCMIDirsAndroid::fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const
  537. {
  538. // ignore passed folder (all libraries in android are dumped into a single folder)
  539. return libraryPath() / libraryName(baseLibName);
  540. }
  541. std::vector<bfs::path> VCMIDirsAndroid::dataPaths() const
  542. {
  543. std::vector<bfs::path> paths(2);
  544. paths.push_back(internalPath);
  545. paths.push_back(userDataPath());
  546. return paths;
  547. }
  548. void VCMIDirsAndroid::init()
  549. {
  550. // asks java code to retrieve needed paths from environment
  551. CAndroidVMHelper envHelper;
  552. basePath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "dataRoot");
  553. internalPath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "internalDataRoot");
  554. nativePath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "nativePath");
  555. IVCMIDirs::init();
  556. }
  557. #endif // VCMI_ANDROID
  558. #endif // VCMI_APPLE, VCMI_XDG
  559. #endif // VCMI_WINDOWS, VCMI_UNIX
  560. // Getters for interfaces are separated for clarity.
  561. namespace VCMIDirs
  562. {
  563. const IVCMIDirs& get()
  564. {
  565. #ifdef VCMI_WINDOWS
  566. static VCMIDirsWIN32 singleton;
  567. #elif defined(VCMI_ANDROID)
  568. static VCMIDirsAndroid singleton;
  569. #elif defined(VCMI_XDG)
  570. static VCMIDirsXDG singleton;
  571. #elif defined(VCMI_MAC)
  572. static VCMIDirsOSX singleton;
  573. #elif defined(VCMI_IOS)
  574. static VCMIDirsIOS singleton;
  575. #endif
  576. static bool initialized = false;
  577. if (!initialized)
  578. {
  579. #ifdef VCMI_WINDOWS
  580. std::locale::global(boost::locale::generator().generate("en_US.UTF-8"));
  581. #endif
  582. boost::filesystem::path::imbue(std::locale());
  583. singleton.init();
  584. initialized = true;
  585. }
  586. return singleton;
  587. }
  588. }
  589. VCMI_LIB_NAMESPACE_END