VCMIDirs.cpp 20 KB

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