VCMIDirs.cpp 20 KB

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