VCMIDirs.cpp 19 KB

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