diff --git a/platform/platform.cpp b/platform/platform.cpp index 2eabee06c5..d8fd6f1030 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -6,22 +6,26 @@ #include "../base/logging.hpp" -string Platform::ReadPathForFile(string const & file) const +string Platform::ReadPathForFile(string const & file, char const * searchScope) const { - string fullPath = m_writableDir + file; - if (!IsFileExistsByFullPath(fullPath)) + ASSERT(searchScope, ()); + + string const strScope(searchScope); + string fullPath; + for (size_t i = 0; i < strScope.size(); ++i) { - fullPath = m_resourcesDir + file; - if (!IsFileExistsByFullPath(fullPath)) + switch (strScope[i]) { - // default behaviour - assume that we have full path here - if (!IsFileExistsByFullPath(file)) - MYTHROW(FileAbsentException, ("File doesn't exist", file)); - else - return file; + case 'w': fullPath = m_writableDir + file; break; + case 'r': fullPath = m_resourcesDir + file; break; + case 'f': fullPath = file; break; + default : CHECK(false, ("Unsupported searchScope:", searchScope)); break; } + if (IsFileExistsByFullPath(fullPath)) + return fullPath; } - return fullPath; + + MYTHROW(FileAbsentException, ("File", file, "doesn't exist in the scope", searchScope)); } string Platform::HashUniqueID(string const & s) diff --git a/platform/platform.hpp b/platform/platform.hpp index 9c24c7e92d..ec7399edc1 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -45,7 +45,7 @@ protected: /// Internal function to get full path for input file. /// Uses m_writeableDir and m_resourcesDir. - string ReadPathForFile(string const & file) const; + string ReadPathForFile(string const & file, char const * searchScope) const; /// Hash some unique string into uniform format. static string HashUniqueID(string const & s); @@ -76,8 +76,10 @@ public: /// @return reader for file decriptor. /// @throws FileAbsentException - /// @param[in] file descriptor which we want to read - ModelReader * GetReader(string const & file) const; + /// @param[in] file name or full path which we want to read, don't forget to free memory or wrap it to ReaderPtr + /// @param[in] searchScope looks for file in dirs in given order: [w]ritable, [r]esources, by [f]ull path + /// @TODO add [e]xternal resource scope for Android (obb support) + ModelReader * GetReader(string const & file, char const * searchScope = "wrf") const; /// @name File operations //@{ diff --git a/platform/platform_android.cpp b/platform/platform_android.cpp index f7c1734305..b038dd5e65 100644 --- a/platform/platform_android.cpp +++ b/platform/platform_android.cpp @@ -76,8 +76,25 @@ public: } -ModelReader * Platform::GetReader(string const & file) const +ModelReader * Platform::GetReader(string const & file, char const * searchScope) const { + // @TODO now we handle only two specific cases needed to release guides ads + string const strScope(searchScope); + if (strScope == "w") + { + string const path = m_writableDir + file; + if (IsFileExistsByFullPath(path)) + return new FileReader(path, READER_CHUNK_LOG_SIZE, READER_CHUNK_LOG_COUNT); + MYTHROW(FileAbsentException, ("File not found", file)); + } + else if (strScope == "r") + { + return new ZipFileReader(m_resourcesDir, "assets/" + file); + } + // @TODO refactor code below and some other parts too, like fonts and maps detection and loading, + // as it can be done much better + + SourceT sources[4]; size_t const n = GetSearchSources(file, sources); diff --git a/platform/platform_ios.mm b/platform/platform_ios.mm index 13f6b2364b..696e05653f 100644 --- a/platform/platform_ios.mm +++ b/platform/platform_ios.mm @@ -64,7 +64,7 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const { try { - return GetFileSizeByFullPath(ReadPathForFile(fileName), size); + return GetFileSizeByFullPath(ReadPathForFile(fileName, "wr"), size); } catch (RootException const &) { @@ -72,9 +72,9 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const } } -ModelReader * Platform::GetReader(string const & file) const +ModelReader * Platform::GetReader(string const & file, char const * searchScope) const { - return new FileReader(ReadPathForFile(file), + return new FileReader(ReadPathForFile(file, searchScope), READER_CHUNK_LOG_SIZE, READER_CHUNK_LOG_COUNT); } diff --git a/platform/platform_qt.cpp b/platform/platform_qt.cpp index 4e6d202245..e01b725cf8 100644 --- a/platform/platform_qt.cpp +++ b/platform/platform_qt.cpp @@ -12,9 +12,9 @@ #include -ModelReader * Platform::GetReader(string const & file) const +ModelReader * Platform::GetReader(string const & file, char const * searchScope) const { - return new FileReader(ReadPathForFile(file), + return new FileReader(ReadPathForFile(file, searchScope), READER_CHUNK_LOG_SIZE, READER_CHUNK_LOG_COUNT); } @@ -22,7 +22,7 @@ bool Platform::GetFileSizeByName(string const & fileName, uint64_t & size) const { try { - return GetFileSizeByFullPath(ReadPathForFile(fileName), size); + return GetFileSizeByFullPath(ReadPathForFile(fileName, "wr"), size); } catch (RootException const &) {