VCMIDirs.cpp 22 KB

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