VCMIDirs.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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. #include "json/JsonNode.h"
  13. #ifdef VCMI_IOS
  14. #include "iOS_utils.h"
  15. #elif defined(VCMI_ANDROID)
  16. #include "CAndroidVMHelper.h"
  17. #endif
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. namespace bfs = boost::filesystem;
  20. bfs::path IVCMIDirs::userLogsPath() const { return userCachePath(); }
  21. bfs::path IVCMIDirs::userSavePath() const { return userDataPath() / "saves"; }
  22. bfs::path IVCMIDirs::userExtractedPath() const { return userCachePath() / "extracted"; }
  23. bfs::path IVCMIDirs::fullLibraryPath(const std::string &desiredFolder, const std::string &baseLibName) const
  24. {
  25. return libraryPath() / desiredFolder / libraryName(baseLibName);
  26. }
  27. std::string IVCMIDirs::genHelpString() const
  28. {
  29. std::vector<std::string> tempVec;
  30. for (const bfs::path & path : dataPaths())
  31. tempVec.push_back(path.string());
  32. const auto gdStringA = boost::algorithm::join(tempVec, ":");
  33. return
  34. " game data: " + gdStringA + "\n"
  35. " libraries: " + libraryPath().string() + "\n"
  36. " server: " + serverPath().string() + "\n"
  37. "\n"
  38. " user data: " + userDataPath().string() + "\n"
  39. " user cache: " + userCachePath().string() + "\n"
  40. " user config: " + userConfigPath().string() + "\n"
  41. " user logs: " + userLogsPath().string() + "\n"
  42. " user saves: " + userSavePath().string() + "\n"
  43. " user extracted: " + userExtractedPath().string() + "\n";
  44. }
  45. void IVCMIDirs::init()
  46. {
  47. // TODO: Log errors
  48. bfs::create_directories(userDataPath());
  49. bfs::create_directories(userCachePath());
  50. bfs::create_directories(userConfigPath());
  51. bfs::create_directories(userLogsPath());
  52. bfs::create_directories(userSavePath());
  53. }
  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. class VCMIDirsWIN32 final : public IVCMIDirs
  64. {
  65. public:
  66. bfs::path userDataPath() const override;
  67. bfs::path userCachePath() const override;
  68. bfs::path userConfigPath() const override;
  69. bfs::path userLogsPath() const override;
  70. bfs::path userSavePath() const override;
  71. std::vector<bfs::path> dataPaths() const override;
  72. bfs::path clientPath() const override;
  73. bfs::path mapEditorPath() const override;
  74. bfs::path serverPath() const override;
  75. bfs::path libraryPath() const override;
  76. bfs::path binaryPath() const override;
  77. std::string libraryName(const std::string& basename) const override;
  78. void init() override;
  79. protected:
  80. mutable std::optional<JsonNode> dirsConfig;
  81. void loadDirsJsonIfNeeded() const;
  82. bfs::path getPathFromConfigOrDefault(const std::string& key, const std::function<bfs::path()>& fallbackFunc) const;
  83. std::wstring utf8ToWstring(const std::string& str) const;
  84. };
  85. std::wstring VCMIDirsWIN32::utf8ToWstring(const std::string& str) const
  86. {
  87. std::wstring result;
  88. int size_needed = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, nullptr, 0);
  89. if (size_needed > 0)
  90. {
  91. result.resize(size_needed - 1);
  92. MultiByteToWideChar(CP_UTF8, 0, str.c_str(), -1, &result[0], size_needed);
  93. }
  94. return result;
  95. }
  96. void VCMIDirsWIN32::loadDirsJsonIfNeeded() const
  97. {
  98. if (dirsConfig.has_value())
  99. return;
  100. wchar_t currentPath[MAX_PATH];
  101. GetModuleFileNameW(nullptr, currentPath, MAX_PATH);
  102. auto configPath = bfs::path(currentPath).parent_path() / "config" / "dirs.json";
  103. if (!bfs::exists(configPath))
  104. {
  105. dirsConfig = std::nullopt;
  106. return;
  107. }
  108. std::ifstream in(configPath.string(), std::ios::binary);
  109. if (!in)
  110. {
  111. dirsConfig = std::nullopt;
  112. return;
  113. }
  114. std::string buffer((std::istreambuf_iterator<char>(in)), {});
  115. dirsConfig = JsonNode(reinterpret_cast<const std::byte*>(buffer.data()), buffer.size(), configPath.string());
  116. }
  117. bfs::path VCMIDirsWIN32::getPathFromConfigOrDefault(const std::string& key, const std::function<bfs::path()>& fallbackFunc) const
  118. {
  119. loadDirsJsonIfNeeded();
  120. if (!dirsConfig || !dirsConfig->isStruct())
  121. return fallbackFunc();
  122. const auto& structMap = dirsConfig->Struct();
  123. auto it = structMap.find(key);
  124. if (it == structMap.end() || !it->second.isString())
  125. return fallbackFunc();
  126. std::wstring raw = utf8ToWstring(it->second.String());
  127. wchar_t expanded[MAX_PATH];
  128. if (ExpandEnvironmentStringsW(raw.c_str(), expanded, MAX_PATH))
  129. return bfs::path(expanded);
  130. else
  131. return bfs::path(raw);
  132. }
  133. void VCMIDirsWIN32::init()
  134. {
  135. // Call base (init dirs)
  136. IVCMIDirs::init();
  137. }
  138. static bfs::path getDefaultUserDataPath()
  139. {
  140. wchar_t profileDir[MAX_PATH];
  141. if (SHGetSpecialFolderPathW(nullptr, profileDir, CSIDL_MYDOCUMENTS, FALSE))
  142. return bfs::path(profileDir) / "My Games" / "vcmi";
  143. return bfs::path(".");
  144. }
  145. bfs::path VCMIDirsWIN32::userDataPath() const
  146. {
  147. return getPathFromConfigOrDefault("userDataPath", [this] { return getDefaultUserDataPath(); });
  148. }
  149. bfs::path VCMIDirsWIN32::userCachePath() const
  150. {
  151. return getPathFromConfigOrDefault("userCachePath", [this] { return userDataPath() / "cache"; });
  152. }
  153. bfs::path VCMIDirsWIN32::userConfigPath() const
  154. {
  155. return getPathFromConfigOrDefault("userConfigPath", [this] { return userDataPath() / "config"; });
  156. }
  157. bfs::path VCMIDirsWIN32::userLogsPath() const
  158. {
  159. return getPathFromConfigOrDefault("userLogsPath", [this] { return userDataPath() / "logs"; });
  160. }
  161. bfs::path VCMIDirsWIN32::userSavePath() const
  162. {
  163. return getPathFromConfigOrDefault("userSavePath", [this] { return userDataPath() / "saves"; });
  164. }
  165. std::vector<bfs::path> VCMIDirsWIN32::dataPaths() const
  166. {
  167. return std::vector<bfs::path>(1, bfs::path("."));
  168. }
  169. bfs::path VCMIDirsWIN32::clientPath() const { return binaryPath() / "VCMI_client.exe"; }
  170. bfs::path VCMIDirsWIN32::mapEditorPath() const { return binaryPath() / "VCMI_mapeditor.exe"; }
  171. bfs::path VCMIDirsWIN32::serverPath() const { return binaryPath() / "VCMI_server.exe"; }
  172. bfs::path VCMIDirsWIN32::libraryPath() const { return "."; }
  173. bfs::path VCMIDirsWIN32::binaryPath() const { return "."; }
  174. std::string VCMIDirsWIN32::libraryName(const std::string& basename) const { return basename + ".dll"; }
  175. #elif defined(VCMI_UNIX)
  176. class IVCMIDirsUNIX : public IVCMIDirs
  177. {
  178. public:
  179. bfs::path clientPath() const override;
  180. bfs::path mapEditorPath() const override;
  181. bfs::path serverPath() const override;
  182. virtual bool developmentMode() const;
  183. };
  184. bool IVCMIDirsUNIX::developmentMode() const
  185. {
  186. // We want to be able to run VCMI from single directory. E.g to run from build output directory
  187. const bool hasConfigs = bfs::exists("config") && bfs::exists("Mods");
  188. const bool hasBinaries = bfs::exists("vcmiclient") || bfs::exists("vcmiserver") || bfs::exists("vcmilobby");
  189. return hasConfigs && hasBinaries;
  190. }
  191. bfs::path IVCMIDirsUNIX::clientPath() const { return binaryPath() / "vcmiclient"; }
  192. bfs::path IVCMIDirsUNIX::mapEditorPath() const { return binaryPath() / "vcmieditor"; }
  193. bfs::path IVCMIDirsUNIX::serverPath() const { return binaryPath() / "vcmiserver"; }
  194. #ifdef VCMI_APPLE
  195. class VCMIDirsApple : public IVCMIDirsUNIX
  196. {
  197. public:
  198. bfs::path userConfigPath() const override;
  199. std::string libraryName(const std::string& basename) const override;
  200. };
  201. bfs::path VCMIDirsApple::userConfigPath() const { return userDataPath() / "config"; }
  202. std::string VCMIDirsApple::libraryName(const std::string& basename) const { return "lib" + basename + ".dylib"; }
  203. #ifdef VCMI_IOS
  204. class VCMIDirsIOS final : public VCMIDirsApple
  205. {
  206. public:
  207. bfs::path userDataPath() const override;
  208. bfs::path userCachePath() const override;
  209. bfs::path userLogsPath() const override;
  210. std::vector<bfs::path> dataPaths() const override;
  211. bfs::path libraryPath() const override;
  212. bfs::path fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const override;
  213. bfs::path binaryPath() const override;
  214. };
  215. bfs::path VCMIDirsIOS::userDataPath() const { return {iOS_utils::documentsPath()}; }
  216. bfs::path VCMIDirsIOS::userCachePath() const { return {iOS_utils::cachesPath()}; }
  217. bfs::path VCMIDirsIOS::userLogsPath() const { return {iOS_utils::documentsPath()}; }
  218. std::vector<bfs::path> VCMIDirsIOS::dataPaths() const
  219. {
  220. std::vector<bfs::path> paths;
  221. paths.reserve(4);
  222. #ifdef VCMI_IOS_SIM
  223. paths.emplace_back(iOS_utils::hostApplicationSupportPath());
  224. #endif
  225. paths.emplace_back(userDataPath());
  226. paths.emplace_back(iOS_utils::documentsPath());
  227. paths.emplace_back(binaryPath());
  228. return paths;
  229. }
  230. bfs::path VCMIDirsIOS::fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const
  231. {
  232. // iOS has flat libs directory structure
  233. return libraryPath() / libraryName(baseLibName);
  234. }
  235. bfs::path VCMIDirsIOS::libraryPath() const { return {iOS_utils::frameworksPath()}; }
  236. bfs::path VCMIDirsIOS::binaryPath() const { return {iOS_utils::bundlePath()}; }
  237. #elif defined(VCMI_MAC)
  238. class VCMIDirsOSX final : public VCMIDirsApple
  239. {
  240. public:
  241. bfs::path userDataPath() const override;
  242. bfs::path userCachePath() const override;
  243. bfs::path userLogsPath() const override;
  244. std::vector<bfs::path> dataPaths() const override;
  245. bfs::path libraryPath() const override;
  246. bfs::path binaryPath() const override;
  247. void init() override;
  248. };
  249. void VCMIDirsOSX::init()
  250. {
  251. // Call base (init dirs)
  252. IVCMIDirsUNIX::init();
  253. auto moveDirIfExists = [](const bfs::path& from, const bfs::path& to)
  254. {
  255. if (!bfs::is_directory(from))
  256. return; // Nothing to do here. Flies away.
  257. if (bfs::is_empty(from))
  258. {
  259. bfs::remove(from);
  260. return; // Nothing to do here. Flies away.
  261. }
  262. if (!bfs::is_directory(to))
  263. {
  264. // IVCMIDirs::init() should create all destination directories.
  265. // TODO: Log fact, that we shouldn't be here.
  266. bfs::create_directories(to);
  267. }
  268. for (bfs::directory_iterator file(from); file != bfs::directory_iterator(); ++file)
  269. {
  270. const bfs::path& srcFilePath = file->path();
  271. const bfs::path dstFilePath = to / srcFilePath.filename();
  272. // TODO: Application should ask user what to do when file exists:
  273. // replace/ignore/stop process/replace all/ignore all
  274. if (!bfs::exists(dstFilePath))
  275. bfs::rename(srcFilePath, dstFilePath);
  276. }
  277. if (!bfs::is_empty(from)); // TODO: Log warn. Some files not moved. User should try to move files.
  278. else
  279. bfs::remove(from);
  280. };
  281. moveDirIfExists(userDataPath() / "Games", userSavePath());
  282. }
  283. bfs::path VCMIDirsOSX::userDataPath() const
  284. {
  285. // This is Cocoa code that should be normally used to get path to Application Support folder but can't use it here for now...
  286. // NSArray* urls = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask];
  287. // UserPath = path([urls[0] path] + "/vcmi").string();
  288. // ...so here goes a bit of hardcode instead
  289. const char* homeDir = getenv("HOME"); // Should be std::getenv?
  290. if (homeDir == nullptr)
  291. homeDir = ".";
  292. return bfs::path(homeDir) / "Library" / "Application Support" / "vcmi";
  293. }
  294. bfs::path VCMIDirsOSX::userCachePath() const { return userDataPath(); }
  295. bfs::path VCMIDirsOSX::userLogsPath() const
  296. {
  297. // TODO: use proper objc code from Foundation framework
  298. if(const auto homeDir = std::getenv("HOME"))
  299. return bfs::path{homeDir} / "Library" / "Logs" / "vcmi";
  300. return IVCMIDirsUNIX::userLogsPath();
  301. }
  302. std::vector<bfs::path> VCMIDirsOSX::dataPaths() const
  303. {
  304. std::vector<bfs::path> ret;
  305. //FIXME: need some proper codepath for detecting running from build output directory
  306. if(developmentMode())
  307. {
  308. ret.push_back(".");
  309. }
  310. else
  311. {
  312. ret.push_back("../Resources/Data");
  313. }
  314. return ret;
  315. }
  316. bfs::path VCMIDirsOSX::libraryPath() const { return "."; }
  317. bfs::path VCMIDirsOSX::binaryPath() const { return "."; }
  318. #endif // VCMI_IOS, VCMI_MAC
  319. #elif defined(VCMI_ANDROID)
  320. class VCMIDirsAndroid : public IVCMIDirsUNIX
  321. {
  322. std::string basePath;
  323. std::string internalPath;
  324. std::string nativePath;
  325. public:
  326. std::string libraryName(const std::string & basename) const override;
  327. bfs::path fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const override;
  328. bfs::path binaryPath() const override;
  329. bfs::path libraryPath() const override;
  330. bfs::path userDataPath() const override;
  331. bfs::path userCachePath() const override;
  332. bfs::path userConfigPath() const override;
  333. std::vector<bfs::path> dataPaths() const override;
  334. void init() override;
  335. };
  336. std::string VCMIDirsAndroid::libraryName(const std::string & basename) const { return "lib" + basename + ".so"; }
  337. bfs::path VCMIDirsAndroid::binaryPath() const { return "."; }
  338. bfs::path VCMIDirsAndroid::libraryPath() const { return nativePath; }
  339. bfs::path VCMIDirsAndroid::userDataPath() const { return basePath; }
  340. bfs::path VCMIDirsAndroid::userCachePath() const { return userDataPath() / "cache"; }
  341. bfs::path VCMIDirsAndroid::userConfigPath() const { return userDataPath() / "config"; }
  342. bfs::path VCMIDirsAndroid::fullLibraryPath(const std::string & desiredFolder, const std::string & baseLibName) const
  343. {
  344. // ignore passed folder (all libraries in android are dumped into a single folder)
  345. return libraryPath() / libraryName(baseLibName);
  346. }
  347. std::vector<bfs::path> VCMIDirsAndroid::dataPaths() const
  348. {
  349. return {
  350. internalPath,
  351. userDataPath(),
  352. };
  353. }
  354. void VCMIDirsAndroid::init()
  355. {
  356. // asks java code to retrieve needed paths from environment
  357. CAndroidVMHelper envHelper;
  358. basePath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "dataRoot");
  359. internalPath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "internalDataRoot");
  360. nativePath = envHelper.callStaticStringMethod(CAndroidVMHelper::NATIVE_METHODS_DEFAULT_CLASS, "nativePath");
  361. IVCMIDirsUNIX::init();
  362. }
  363. #elif defined(VCMI_PORTMASTER)
  364. class VCMIDirsPM : public IVCMIDirsUNIX
  365. {
  366. public:
  367. bfs::path userDataPath() const override;
  368. bfs::path userCachePath() const override;
  369. bfs::path userConfigPath() const override;
  370. std::vector<bfs::path> dataPaths() const override;
  371. bfs::path libraryPath() const override;
  372. bfs::path binaryPath() const override;
  373. std::string libraryName(const std::string& basename) const override;
  374. };
  375. bfs::path VCMIDirsPM::userDataPath() const
  376. {
  377. const char* homeDir;
  378. if((homeDir = getenv("PORTMASTER_HOME")))
  379. return bfs::path(homeDir) / "data";
  380. else
  381. return bfs::path(".") / "data";
  382. }
  383. bfs::path VCMIDirsPM::userCachePath() const
  384. {
  385. // $XDG_CACHE_HOME, default: $HOME/.cache
  386. const char * tempResult;
  387. if ((tempResult = getenv("PORTMASTER_HOME")))
  388. return bfs::path(tempResult) / "cache";
  389. else
  390. return bfs::path(".") / "cache";
  391. }
  392. bfs::path VCMIDirsPM::userConfigPath() const
  393. {
  394. // $XDG_CONFIG_HOME, default: $HOME/.config
  395. const char * tempResult;
  396. if ((tempResult = getenv("PORTMASTER_HOME")))
  397. return bfs::path(tempResult) / "save";
  398. else
  399. return bfs::path(".") / "save";
  400. }
  401. std::vector<bfs::path> VCMIDirsPM::dataPaths() const
  402. {
  403. // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
  404. // construct list in reverse.
  405. // in specification first directory has highest priority
  406. // in vcmi fs last directory has highest priority
  407. std::vector<bfs::path> ret;
  408. const char * tempResult;
  409. if ((tempResult = getenv("PORTMASTER_HOME")))
  410. {
  411. ret.push_back(bfs::path(tempResult) / "data");
  412. ret.push_back(bfs::path(tempResult));
  413. }
  414. ret.push_back(bfs::path(".") / "data");
  415. ret.push_back(bfs::path("."));
  416. return ret;
  417. }
  418. bfs::path VCMIDirsPM::libraryPath() const
  419. {
  420. const char * tempResult;
  421. if ((tempResult = getenv("PORTMASTER_HOME")))
  422. return bfs::path(tempResult) / "libs";
  423. else
  424. return M_LIB_DIR;
  425. }
  426. bfs::path VCMIDirsPM::binaryPath() const
  427. {
  428. const char * tempResult;
  429. if ((tempResult = getenv("PORTMASTER_HOME")))
  430. return bfs::path(tempResult) / "bin";
  431. else
  432. return M_BIN_DIR;
  433. }
  434. std::string VCMIDirsPM::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
  435. #elif defined(VCMI_XDG)
  436. class VCMIDirsXDG : public IVCMIDirsUNIX
  437. {
  438. public:
  439. bfs::path userDataPath() const override;
  440. bfs::path userCachePath() const override;
  441. bfs::path userConfigPath() const override;
  442. std::vector<bfs::path> dataPaths() const override;
  443. bfs::path libraryPath() const override;
  444. bfs::path binaryPath() const override;
  445. std::string libraryName(const std::string& basename) const override;
  446. };
  447. bfs::path VCMIDirsXDG::userDataPath() const
  448. {
  449. // $XDG_DATA_HOME, default: $HOME/.local/share
  450. const char* homeDir;
  451. if((homeDir = getenv("XDG_DATA_HOME")))
  452. return bfs::path(homeDir) / "vcmi";
  453. else if((homeDir = getenv("HOME")))
  454. return bfs::path(homeDir) / ".local" / "share" / "vcmi";
  455. else
  456. return ".";
  457. }
  458. bfs::path VCMIDirsXDG::userCachePath() const
  459. {
  460. // $XDG_CACHE_HOME, default: $HOME/.cache
  461. const char * tempResult;
  462. if ((tempResult = getenv("XDG_CACHE_HOME")))
  463. return bfs::path(tempResult) / "vcmi";
  464. else if ((tempResult = getenv("HOME")))
  465. return bfs::path(tempResult) / ".cache" / "vcmi";
  466. else
  467. return ".";
  468. }
  469. bfs::path VCMIDirsXDG::userConfigPath() const
  470. {
  471. // $XDG_CONFIG_HOME, default: $HOME/.config
  472. const char * tempResult = getenv("XDG_CONFIG_HOME");
  473. if (tempResult)
  474. return bfs::path(tempResult) / "vcmi";
  475. tempResult = getenv("HOME");
  476. if (tempResult)
  477. return bfs::path(tempResult) / ".config" / "vcmi";
  478. return ".";
  479. }
  480. std::vector<bfs::path> VCMIDirsXDG::dataPaths() const
  481. {
  482. // $XDG_DATA_DIRS, default: /usr/local/share/:/usr/share/
  483. // construct list in reverse.
  484. // in specification first directory has highest priority
  485. // in vcmi fs last directory has highest priority
  486. std::vector<bfs::path> ret;
  487. if(developmentMode())
  488. {
  489. //For now we'll disable usage of system directories when VCMI running from bin directory
  490. ret.emplace_back(".");
  491. }
  492. else
  493. {
  494. ret.emplace_back(M_DATA_DIR);
  495. const char * tempResult;
  496. if((tempResult = getenv("XDG_DATA_DIRS")) != nullptr)
  497. {
  498. std::string dataDirsEnv = tempResult;
  499. std::vector<std::string> dataDirs;
  500. boost::split(dataDirs, dataDirsEnv, boost::is_any_of(":"));
  501. for (auto & entry : boost::adaptors::reverse(dataDirs))
  502. ret.push_back(bfs::path(entry) / "vcmi");
  503. }
  504. else
  505. {
  506. ret.push_back(bfs::path("/usr/share") / "vcmi");
  507. ret.push_back(bfs::path("/usr/local/share") / "vcmi");
  508. }
  509. // Debian and other distributions might want to use it while it's not part of XDG
  510. ret.push_back(bfs::path("/usr/share/games") / "vcmi");
  511. }
  512. return ret;
  513. }
  514. bfs::path VCMIDirsXDG::libraryPath() const
  515. {
  516. if(developmentMode())
  517. return ".";
  518. else
  519. return M_LIB_DIR;
  520. }
  521. bfs::path VCMIDirsXDG::binaryPath() const
  522. {
  523. if(developmentMode())
  524. return ".";
  525. else
  526. return M_BIN_DIR;
  527. }
  528. std::string VCMIDirsXDG::libraryName(const std::string& basename) const { return "lib" + basename + ".so"; }
  529. #endif // VCMI_APPLE, VCMI_ANDROID, VCMI_XDG
  530. #endif // VCMI_WINDOWS, VCMI_UNIX
  531. // Getters for interfaces are separated for clarity.
  532. namespace VCMIDirs
  533. {
  534. const IVCMIDirs& get()
  535. {
  536. #ifdef VCMI_WINDOWS
  537. static VCMIDirsWIN32 singleton;
  538. #elif defined(VCMI_ANDROID)
  539. static VCMIDirsAndroid singleton;
  540. #elif defined(VCMI_PORTMASTER)
  541. static VCMIDirsPM singleton;
  542. #elif defined(VCMI_XDG)
  543. static VCMIDirsXDG singleton;
  544. #elif defined(VCMI_MAC)
  545. static VCMIDirsOSX singleton;
  546. #elif defined(VCMI_IOS)
  547. static VCMIDirsIOS singleton;
  548. #endif
  549. static std::once_flag flag;
  550. std::call_once(flag, [] { singleton.init(); });
  551. return singleton;
  552. }
  553. }
  554. VCMI_LIB_NAMESPACE_END