VCMIDirs.cpp 21 KB

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