From 132f397ae8b4b84cbb7a47dac2811ad639549d41 Mon Sep 17 00:00:00 2001 From: Alexander Borsuk Date: Sat, 19 Mar 2022 16:14:12 +0100 Subject: [PATCH] Minor changes to Mac platform initialization directories Signed-off-by: Alexander Borsuk --- platform/platform_mac.mm | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/platform/platform_mac.mm b/platform/platform_mac.mm index 16f2b8de9b..8faf8c7dd3 100644 --- a/platform/platform_mac.mm +++ b/platform/platform_mac.mm @@ -23,11 +23,24 @@ #import #import + +namespace +{ +// Checks if copyright.html file is present in the directory. +bool IsResourcesDir(std::string const & dir) +{ + return Platform::IsFileExistsByFullPath(base::JoinPath(dir, "copyright.html")); +} +} // namespace + Platform::Platform() { - // get resources directory path + // OMaps.app/Content/Resources or omim-build-debug for tests. std::string const resourcesPath = NSBundle.mainBundle.resourcePath.UTF8String; + // Omaps.app or omim-build-debug for tests. std::string const bundlePath = NSBundle.mainBundle.bundlePath.UTF8String; + // Current working directory, can be overrided for Xcode projects in the scheme's settings. + std::string const currentDir = [NSFileManager.defaultManager currentDirectoryPath].UTF8String; char const * envResourcesDir = ::getenv("MWM_RESOURCES_DIR"); char const * envWritableDir = ::getenv("MWM_WRITABLE_DIR"); @@ -39,9 +52,6 @@ Platform::Platform() } else if (resourcesPath == bundlePath) { -#ifdef STANDALONE_APP - m_resourcesDir = resourcesPath + "/"; -#else // STANDALONE_APP // we're the console app, probably unit test, and path is our directory m_resourcesDir = bundlePath + "/../../data/"; if (!IsFileExistsByFullPath(m_resourcesDir)) @@ -53,13 +63,11 @@ Platform::Platform() else m_resourcesDir = "./data/"; } -#endif // STANDALONE_APP m_writableDir = m_resourcesDir; } else { m_resourcesDir = resourcesPath + "/"; - std::string const currentDir = [NSFileManager.defaultManager currentDirectoryPath].UTF8String; std::string const paths[] = { // Developers can set a symlink to the data folder. @@ -79,17 +87,29 @@ Platform::Platform() for (auto const & path : paths) { if (IsFileExistsByFullPath(path)) - { + { m_writableDir = path; break; } } + // Xcode-launched Mac projects are built into a non-standard folder and may need + // a customized working directory. if (m_writableDir.empty()) { - auto const p = m_resourcesDir.find("/omim/"); - if (p != std::string::npos) - m_writableDir = m_resourcesDir.substr(0, p) + "/omim/data/"; + for (char const * keyword : {"/omim/", "/organicmaps/"}) + { + if (auto const p = currentDir.rfind(keyword); p != std::string::npos) + { + m_writableDir = m_resourcesDir = currentDir.substr(0, p) + keyword + "data/"; + break; + } + if (auto const p = m_resourcesDir.rfind(keyword); p != std::string::npos) + { + m_writableDir = m_resourcesDir.substr(0, p) + keyword + "data/"; + break; + } + } } if (m_writableDir.empty())