VCMIDirs.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. std::locale::global(boost::locale::generator().generate("en_US.UTF-8"));
  144. // Call base (init dirs)
  145. IVCMIDirs::init();
  146. // Moves one directory (from) contents to another directory (to)
  147. // Shows user the "moving file dialog" and ask to resolve conflits.
  148. // If necessary updates current directory.
  149. auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to) -> bool
  150. {
  151. if (!bfs::is_directory(from))
  152. return true; // Nothing to do here. Flies away.
  153. if (bfs::is_empty(from))
  154. {
  155. if (bfs::current_path() == from)
  156. bfs::current_path(to);
  157. bfs::remove(from);
  158. return true; // Nothing to do here. Flies away.
  159. }
  160. if (!bfs::is_directory(to))
  161. {
  162. // IVCMIDirs::init() should create all destination directories.
  163. // TODO: Log fact, that we shouldn't be here.
  164. bfs::create_directories(to);
  165. }
  166. // Why the hell path strings should be end with double null :/
  167. auto makeDoubleNulled = [](const bfs::path& path) -> std::unique_ptr<wchar_t[]>
  168. {
  169. const std::wstring& pathStr = path.native();
  170. std::unique_ptr<wchar_t[]> result(new wchar_t[pathStr.length() + 2]);
  171. size_t i = 0;
  172. for (const wchar_t ch : pathStr)
  173. result[i++] = ch;
  174. result[i++] = L'\0';
  175. result[i++] = L'\0';
  176. return result;
  177. };
  178. auto fromDNulled = makeDoubleNulled(from / L"*.*");
  179. auto toDNulled = makeDoubleNulled(to);
  180. SHFILEOPSTRUCTW fileOp;
  181. fileOp.hwnd = GetConsoleWindow();
  182. fileOp.wFunc = FO_MOVE;
  183. fileOp.pFrom = fromDNulled.get();
  184. fileOp.pTo = toDNulled.get();
  185. fileOp.fFlags = 0;
  186. fileOp.hNameMappings = nullptr;
  187. fileOp.lpszProgressTitle = nullptr;
  188. const int errorCode = SHFileOperationW(&fileOp);
  189. if (errorCode != 0) // TODO: Log error. User should try to move files.
  190. return false;
  191. else if (fileOp.fAnyOperationsAborted) // TODO: Log warn. User aborted operation. User should move files.
  192. return false;
  193. else if (!bfs::is_empty(from)) // TODO: Log warn. Some files not moved. User should try to move files.
  194. return false;
  195. if (bfs::current_path() == from)
  196. bfs::current_path(to);
  197. // TODO: Log fact that we moved files succefully.
  198. bfs::remove(from);
  199. return true;
  200. };
  201. // Retrieves the fully qualified path for the file that contains the specified module.
  202. // The module must have been loaded by the current process.
  203. // If this parameter is nullptr, retrieves the path of the executable file of the current process.
  204. auto getModulePath = [](HMODULE hModule) -> bfs::path
  205. {
  206. wchar_t exePathW[MAX_PATH];
  207. DWORD nSize = GetModuleFileNameW(hModule, exePathW, MAX_PATH);
  208. DWORD error = GetLastError();
  209. // WARN: Windows XP don't set ERROR_INSUFFICIENT_BUFFER error.
  210. if (nSize != 0 && error != ERROR_INSUFFICIENT_BUFFER)
  211. return bfs::path(std::wstring(exePathW, nSize));
  212. // TODO: Error handling
  213. return bfs::path();
  214. };
  215. // Moves one directory contents to another directory
  216. // Shows user the "moving file dialog" and ask to resolve conflicts.
  217. // It takes into account that 'from' path can contain current executable.
  218. // If necessary closes program and starts update script.
  219. auto advancedMoveDirIfExists = [getModulePath, moveDirIfExists](const bfs::path& from, const bfs::path& to) -> bool
  220. {
  221. const bfs::path executablePath = getModulePath(nullptr);
  222. // VCMI cann't determine executable path.
  223. // Use standard way to move directory and exit function.
  224. if (executablePath.empty())
  225. return moveDirIfExists(from, to);
  226. const bfs::path executableName = executablePath.filename();
  227. // Current executabl isn't in 'from' path.
  228. // Use standard way to move directory and exit function.
  229. if (!bfs::equivalent(executablePath, from / executableName))
  230. return moveDirIfExists(from, to);
  231. // Try standard way to move directory.
  232. // I don't know how other systems, but Windows 8.1 allow to move running executable.
  233. if (moveDirIfExists(from, to))
  234. return true;
  235. // Start copying script and exit program.
  236. if (StartBatchCopyDataProgram(from, to, executableName))
  237. exit(ERROR_SUCCESS);
  238. // Everything failed :C
  239. return false;
  240. };
  241. moveDirIfExists(oldUserSavePath(), userSavePath());
  242. advancedMoveDirIfExists(oldUserDataPath(), userDataPath());
  243. }
  244. bfs::path VCMIDirsWIN32::userDataPath() const
  245. {
  246. wchar_t profileDir[MAX_PATH];
  247. if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_MYDOCUMENTS, FALSE) != FALSE)
  248. return bfs::path(profileDir) / "My Games" / "vcmi";
  249. return ".";
  250. }
  251. bfs::path VCMIDirsWIN32::oldUserDataPath() const
  252. {
  253. wchar_t profileDir[MAX_PATH];
  254. if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_PROFILE, FALSE) == FALSE) // WinAPI way failed
  255. {
  256. #if defined(_MSC_VER) && _MSC_VER >= 1700
  257. wchar_t* buffer;
  258. size_t bufferSize;
  259. errno_t result = _wdupenv_s(&buffer, &bufferSize, L"userprofile");
  260. if (result == 0)
  261. {
  262. bfs::path result(std::wstring(buffer, bufferSize));
  263. free(buffer);
  264. return result;
  265. }
  266. #else
  267. const char* profileDirA;
  268. if ((profileDirA = std::getenv("userprofile"))) // STL way succeed
  269. return bfs::path(profileDirA) / "vcmi";
  270. #endif
  271. else
  272. return "."; // Every thing failed, return current directory.
  273. }
  274. else
  275. return bfs::path(profileDir) / "vcmi";
  276. //return dataPaths()[0] ???;
  277. }
  278. bfs::path VCMIDirsWIN32::oldUserSavePath() const { return userDataPath() / "Games"; }
  279. bfs::path VCMIDirsWIN32::userCachePath() const { return userDataPath(); }
  280. bfs::path VCMIDirsWIN32::userConfigPath() const { return userDataPath() / "config"; }
  281. std::vector<bfs::path> VCMIDirsWIN32::dataPaths() const
  282. {
  283. return std::vector<bfs::path>(1, bfs::path("."));
  284. }
  285. bfs::path VCMIDirsWIN32::clientPath() const { return binaryPath() / "VCMI_client.exe"; }
  286. bfs::path VCMIDirsWIN32::serverPath() const { return binaryPath() / "VCMI_server.exe"; }
  287. bfs::path VCMIDirsWIN32::libraryPath() const { return "."; }
  288. bfs::path VCMIDirsWIN32::binaryPath() const { return "."; }
  289. std::string VCMIDirsWIN32::libraryName(const std::string& basename) const { return basename + ".dll"; }
  290. #elif defined(VCMI_UNIX)
  291. class IVCMIDirsUNIX : public IVCMIDirs
  292. {
  293. public:
  294. boost::filesystem::path clientPath() const override;
  295. boost::filesystem::path serverPath() const override;
  296. virtual bool developmentMode() const;
  297. };
  298. bool IVCMIDirsUNIX::developmentMode() const
  299. {
  300. // We want to be able to run VCMI from single directory. E.g to run from build output directory
  301. return bfs::exists("AI") && bfs::exists("config") && bfs::exists("Mods") && bfs::exists("vcmiserver") && bfs::exists("vcmiclient");
  302. }
  303. bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiclient"; }
  304. bfs::path IVCMIDirsUNIX::serverPath() const { return binaryPath() / "vcmiserver"; }
  305. #ifdef VCMI_APPLE
  306. class VCMIDirsApple : public IVCMIDirsUNIX
  307. {
  308. public:
  309. bfs::path userConfigPath() const override;
  310. std::string libraryName(const std::string& basename) const override;
  311. };
  312. bfs::path VCMIDirsApple::userConfigPath() const { return userDataPath() / "config"; }
  313. std::string VCMIDirsApple::libraryName(const std::string& basename) const { return "lib" + basename + ".dylib"; }
  314. #ifdef VCMI_IOS
  315. class VCMIDirsIOS final : public VCMIDirsApple
  316. {
  317. public:
  318. bfs::path userDataPath() const override;
  319. bfs::path userCachePath() const override;
  320. bfs::path userLogsPath() const override;
  321. std::vector<bfs::path> dataPaths() const override;
  322. bfs::path libraryPath() const override;
  323. bfs::path fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const override;
  324. bfs::path binaryPath() const override;
  325. };
  326. bfs::path VCMIDirsIOS::userDataPath() const { return {iOS_utils::documentsPath()}; }
  327. bfs::path VCMIDirsIOS::userCachePath() const { return {iOS_utils::cachesPath()}; }
  328. bfs::path VCMIDirsIOS::userLogsPath() const { return {iOS_utils::documentsPath()}; }
  329. std::vector<bfs::path> VCMIDirsIOS::dataPaths() const
  330. {
  331. std::vector<bfs::path> paths;
  332. paths.reserve(4);
  333. #ifdef VCMI_IOS_SIM
  334. paths.emplace_back(iOS_utils::hostApplicationSupportPath());
  335. #endif
  336. paths.emplace_back(userDataPath());
  337. paths.emplace_back(iOS_utils::documentsPath());
  338. paths.emplace_back(binaryPath());
  339. return paths;
  340. }
  341. bfs::path VCMIDirsIOS::fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const
  342. {
  343. // iOS has flat libs directory structure
  344. return libraryPath() / libraryName(baseLibName);
  345. }
  346. bfs::path VCMIDirsIOS::libraryPath() const { return {iOS_utils::frameworksPath()}; }
  347. bfs::path VCMIDirsIOS::binaryPath() const { return {iOS_utils::bundlePath()}; }
  348. #elif defined(VCMI_MAC)
  349. class VCMIDirsOSX final : public VCMIDirsApple
  350. {
  351. public:
  352. boost::filesystem::path userDataPath() const override;
  353. boost::filesystem::path userCachePath() const override;
  354. boost::filesystem::path userLogsPath() const override;
  355. std::vector<boost::filesystem::path> dataPaths() const override;
  356. boost::filesystem::path libraryPath() const override;
  357. boost::filesystem::path binaryPath() const override;
  358. void init() override;
  359. };
  360. void VCMIDirsOSX::init()
  361. {
  362. // Call base (init dirs)
  363. IVCMIDirsUNIX::init();
  364. auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to)
  365. {
  366. if (!bfs::is_directory(from))
  367. return; // Nothing to do here. Flies away.
  368. if (bfs::is_empty(from))
  369. {
  370. bfs::remove(from);
  371. return; // Nothing to do here. Flies away.
  372. }
  373. if (!bfs::is_directory(to))
  374. {
  375. // IVCMIDirs::init() should create all destination directories.
  376. // TODO: Log fact, that we shouldn't be here.
  377. bfs::create_directories(to);
  378. }
  379. for (bfs::directory_iterator file(from); file != bfs::directory_iterator(); ++file)
  380. {
  381. const boost::filesystem::path& srcFilePath = file->path();
  382. const boost::filesystem::path dstFilePath = to / srcFilePath.filename();
  383. // TODO: Aplication should ask user what to do when file exists:
  384. // replace/ignore/stop process/replace all/ignore all
  385. if (!boost::filesystem::exists(dstFilePath))
  386. bfs::rename(srcFilePath, dstFilePath);
  387. }
  388. if (!bfs::is_empty(from)); // TODO: Log warn. Some files not moved. User should try to move files.
  389. else
  390. bfs::remove(from);
  391. };
  392. moveDirIfExists(userDataPath() / "Games", userSavePath());
  393. }
  394. bfs::path VCMIDirsOSX::userDataPath() const
  395. {
  396. // This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
  397. // NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
  398. // UserPath = path([urls[0] path] + "/vcmi").string();
  399. // ...so here goes a bit of hardcode instead
  400. const char* homeDir = getenv("HOME"); // Should be std::getenv?
  401. if (homeDir == nullptr)
  402. homeDir = ".";
  403. return bfs::path(homeDir) / "Library" / "Application Support" / "vcmi";
  404. }
  405. bfs::path VCMIDirsOSX::userCachePath() const { return userDataPath(); }
  406. bfs::path VCMIDirsOSX::userLogsPath() const
  407. {
  408. // TODO: use proper objc code from Foundation framework
  409. if(const auto homeDir = std::getenv("HOME"))
  410. return bfs::path{homeDir} / "Library" / "Logs" / "vcmi";
  411. return IVCMIDirsUNIX::userLogsPath();
  412. }
  413. std::vector<bfs::path> VCMIDirsOSX::dataPaths() const
  414. {
  415. std::vector<bfs::path> ret;
  416. //FIXME: need some proper codepath for detecting running from build output directory
  417. if(developmentMode())
  418. {
  419. ret.push_back(".");
  420. }
  421. else
  422. {
  423. ret.push_back("../Resources/Data");
  424. }
  425. return ret;
  426. }
  427. bfs::path VCMIDirsOSX::libraryPath() const { return "."; }
  428. bfs::path VCMIDirsOSX::binaryPath() const { return "."; }
  429. #endif // VCMI_IOS, VCMI_MAC
  430. #elif defined(VCMI_XDG)
  431. class VCMIDirsXDG : public IVCMIDirsUNIX
  432. {
  433. public:
  434. boost::filesystem::path userDataPath() const override;
  435. boost::filesystem::path userCachePath() const override;
  436. boost::filesystem::path userConfigPath() const override;
  437. std::vector<boost::filesystem::path> dataPaths() const override;
  438. boost::filesystem::path libraryPath() const override;
  439. boost::filesystem::path binaryPath() const override;
  440. std::string libraryName(const std::string& basename) const override;
  441. };
  442. bfs::path VCMIDirsXDG::userDataPath() const
  443. {
  444. // $XDG_DATA_HOME, default: $HOME/.local/share
  445. const char* homeDir;
  446. if((homeDir = getenv("XDG_DATA_HOME")))
  447. return bfs::path(homeDir) / "vcmi";
  448. else if((homeDir = getenv("HOME")))
  449. return bfs::path(homeDir) / ".local" / "share" / "vcmi";
  450. else
  451. return ".";
  452. }
  453. bfs::path VCMIDirsXDG::userCachePath() const
  454. {
  455. // $XDG_CACHE_HOME, default: $HOME/.cache
  456. const char * tempResult;
  457. if ((tempResult = getenv("XDG_CACHE_HOME")))
  458. return bfs::path(tempResult) / "vcmi";
  459. else if ((tempResult = getenv("HOME")))
  460. return bfs::path(tempResult) / ".cache" / "vcmi";
  461. else
  462. return ".";
  463. }
  464. bfs::path VCMIDirsXDG::userConfigPath() const
  465. {
  466. // $XDG_CONFIG_HOME, default: $HOME/.config
  467. const char * tempResult;
  468. if ((tempResult = getenv("XDG_CONFIG_HOME")))
  469. return bfs::path(tempResult) / "vcmi";
  470. else if ((tempResult = getenv("HOME")))
  471. return bfs::path(tempResult) / ".config" / "vcmi";
  472. else
  473. return ".";
  474. }
  475. std::vector<bfs::path> VCMIDirsXDG::dataPaths() const
  476. {
  477. // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
  478. // construct list in reverse.
  479. // in specification first directory has highest priority
  480. // in vcmi fs last directory has highest priority
  481. std::vector<bfs::path> ret;
  482. if(developmentMode())
  483. {
  484. //For now we'll disable usage of system directories when VCMI running from bin directory
  485. ret.push_back(".");
  486. }
  487. else
  488. {
  489. ret.push_back(M_DATA_DIR);
  490. const char * tempResult;
  491. if((tempResult = getenv("XDG_DATA_DIRS")) != nullptr)
  492. {
  493. std::string dataDirsEnv = tempResult;
  494. std::vector<std::string> dataDirs;
  495. boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
  496. for (auto & entry : boost::adaptors::reverse(dataDirs))
  497. ret.push_back(bfs::path(entry) / "vcmi");
  498. }
  499. else
  500. {
  501. ret.push_back(bfs::path("/usr/share") / "vcmi");
  502. ret.push_back(bfs::path("/usr/local/share") / "vcmi");
  503. }
  504. // Debian and other distributions might want to use it while it's not part of XDG
  505. ret.push_back(bfs::path("/usr/share/games") / "vcmi");
  506. }
  507. return ret;
  508. }
  509. bfs::path VCMIDirsXDG::libraryPath() const
  510. {
  511. if(developmentMode())
  512. return ".";
  513. else
  514. return M_LIB_DIR;
  515. }
  516. bfs::path VCMIDirsXDG::binaryPath() const
  517. {
  518. if(developmentMode())
  519. return ".";
  520. else
  521. return M_BIN_DIR;
  522. }
  523. std::string VCMIDirsXDG::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
  524. #ifdef VCMI_ANDROID
  525. class VCMIDirsAndroid : public VCMIDirsXDG
  526. {
  527. std::string basePath;
  528. std::string internalPath;
  529. std::string nativePath;
  530. public:
  531. bfs::path fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const override;
  532. bfs::path libraryPath() const override;
  533. bfs::path userDataPath() const override;
  534. bfs::path userCachePath() const override;
  535. bfs::path userConfigPath() const override;
  536. std::vector<bfs::path> dataPaths() const override;
  537. void init() override;
  538. };
  539. bfs::path VCMIDirsAndroid::libraryPath() const { return nativePath; }
  540. bfs::path VCMIDirsAndroid::userDataPath() const { return basePath; }
  541. bfs::path VCMIDirsAndroid::userCachePath() const { return userDataPath() / "cache"; }
  542. bfs::path VCMIDirsAndroid::userConfigPath() const { return userDataPath() / "config"; }
  543. bfs::path VCMIDirsAndroid::fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const
  544. {
  545. // ignore passed folder (all libraries in android are dumped into a single folder)
  546. return libraryPath() / libraryName(baseLibName);
  547. }
  548. std::vector<bfs::path> VCMIDirsAndroid::dataPaths() const
  549. {
  550. std::vector<bfs::path> paths(2);
  551. paths.push_back(internalPath);
  552. paths.push_back(userDataPath());
  553. return paths;
  554. }
  555. void VCMIDirsAndroid::init()
  556. {
  557. // asks java code to retrieve needed paths from environment
  558. CAndroidVMHelper envHelper;
  559. basePath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "dataRoot");
  560. internalPath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "internalDataRoot");
  561. nativePath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "nativePath");
  562. IVCMIDirs::init();
  563. }
  564. #endif // VCMI_ANDROID
  565. #endif // VCMI_APPLE, VCMI_XDG
  566. #endif // VCMI_WINDOWS, VCMI_UNIX
  567. // Getters for interfaces are separated for clarity.
  568. namespace VCMIDirs
  569. {
  570. const IVCMIDirs& get()
  571. {
  572. #ifdef VCMI_WINDOWS
  573. static VCMIDirsWIN32 singleton;
  574. #elif defined(VCMI_ANDROID)
  575. static VCMIDirsAndroid singleton;
  576. #elif defined(VCMI_XDG)
  577. static VCMIDirsXDG singleton;
  578. #elif defined(VCMI_MAC)
  579. static VCMIDirsOSX singleton;
  580. #elif defined(VCMI_IOS)
  581. static VCMIDirsIOS singleton;
  582. #endif
  583. static bool initialized = false;
  584. if (!initialized)
  585. {
  586. singleton.init();
  587. initialized = true;
  588. }
  589. return singleton;
  590. }
  591. }
  592. VCMI_LIB_NAMESPACE_END