VCMIDirs.cpp 19 KB

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