CFilesystemLoader.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * CFilesystemLoader.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 "CFilesystemLoader.h"
  12. #include "CFileInputStream.h"
  13. #include "../ExceptionsCommon.h"
  14. #include <boost/locale/encoding_utf.hpp>
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. CFilesystemLoader::CFilesystemLoader(std::string _mountPoint, boost::filesystem::path baseDirectory, size_t depth, bool initial):
  17. baseDirectory(std::move(baseDirectory)),
  18. mountPoint(std::move(_mountPoint)),
  19. recursiveDepth(depth)
  20. {
  21. try {
  22. fileList = listFiles(mountPoint, depth, initial);
  23. }
  24. catch (const boost::filesystem::filesystem_error & e) {
  25. throw DataLoadingException("Failed to load content of '" + baseDirectory.string() + "'. Reason: " + e.what());
  26. }
  27. logGlobal->trace("File system loaded, %d files found", fileList.size());
  28. }
  29. std::unique_ptr<CInputStream> CFilesystemLoader::load(const ResourcePath & resourceName) const
  30. {
  31. assert(fileList.count(resourceName));
  32. boost::filesystem::path file = baseDirectory / fileList.at(resourceName);
  33. logGlobal->trace("loading %s", file.string());
  34. return std::make_unique<CFileInputStream>(file);
  35. }
  36. bool CFilesystemLoader::existsResource(const ResourcePath & resourceName) const
  37. {
  38. return fileList.count(resourceName);
  39. }
  40. std::string CFilesystemLoader::getMountPoint() const
  41. {
  42. return mountPoint;
  43. }
  44. std::optional<boost::filesystem::path> CFilesystemLoader::getResourceName(const ResourcePath & resourceName) const
  45. {
  46. assert(existsResource(resourceName));
  47. return baseDirectory / fileList.at(resourceName);
  48. }
  49. void CFilesystemLoader::updateFilteredFiles(std::function<bool(const std::string &)> filter) const
  50. {
  51. if (filter(mountPoint))
  52. {
  53. fileList = listFiles(mountPoint, recursiveDepth, false);
  54. }
  55. }
  56. std::unordered_set<ResourcePath> CFilesystemLoader::getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const
  57. {
  58. std::unordered_set<ResourcePath> foundID;
  59. for (auto & file : fileList)
  60. {
  61. if (filter(file.first))
  62. foundID.insert(file.first);
  63. }
  64. return foundID;
  65. }
  66. bool CFilesystemLoader::createResource(const std::string & requestedFilename, bool update)
  67. {
  68. std::string filename = requestedFilename;
  69. ResourcePath resID(filename);
  70. if (fileList.find(resID) != fileList.end())
  71. return true;
  72. if (!boost::iequals(mountPoint, filename.substr(0, mountPoint.size())))
  73. {
  74. logGlobal->trace("Can't create file: wrong mount point: %s", mountPoint);
  75. return false;
  76. }
  77. filename = filename.substr(mountPoint.size());
  78. #ifdef VCMI_WINDOWS
  79. boost::filesystem::path filePath(boost::locale::conv::utf_to_utf<wchar_t>(filename));
  80. #else
  81. boost::filesystem::path filePath(filename.string());
  82. #endif
  83. if (!update)
  84. {
  85. // create folders if not exists
  86. boost::filesystem::path p((baseDirectory / filePath).c_str());
  87. boost::filesystem::create_directories(p.parent_path());
  88. // create file, if not exists
  89. std::ofstream file((baseDirectory / filePath).c_str(), std::ofstream::binary);
  90. if (!file.is_open())
  91. return false;
  92. }
  93. fileList[resID] = filePath;
  94. return true;
  95. }
  96. std::unordered_map<ResourcePath, boost::filesystem::path> CFilesystemLoader::listFiles(const std::string &mountPoint, size_t depth, bool initial) const
  97. {
  98. static const EResType initArray[] = {
  99. EResType::DIRECTORY,
  100. EResType::JSON,
  101. EResType::ARCHIVE_LOD,
  102. EResType::ARCHIVE_VID,
  103. EResType::ARCHIVE_SND,
  104. EResType::ARCHIVE_ZIP };
  105. static const std::set<EResType> initialTypes(initArray, initArray + std::size(initArray));
  106. std::unordered_map<ResourcePath, boost::filesystem::path> fileList;
  107. if(!boost::filesystem::is_directory(baseDirectory))
  108. return fileList;
  109. std::vector<boost::filesystem::path> path; //vector holding relative path to our file
  110. boost::filesystem::recursive_directory_iterator enddir;
  111. #if BOOST_VERSION >= 107200 // 1.72
  112. boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::directory_options::follow_directory_symlink);
  113. #else
  114. boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::symlink_option::recurse);
  115. #endif
  116. for(; it != enddir; ++it)
  117. {
  118. EResType type;
  119. #if BOOST_VERSION >= 107200
  120. const auto currentDepth = it.depth();
  121. #else
  122. const auto currentDepth = it.level();
  123. #endif
  124. if (boost::filesystem::is_directory(it->status()))
  125. {
  126. path.resize(currentDepth + 1);
  127. path.back() = it->path().filename();
  128. // don't iterate into directory if depth limit reached
  129. #if BOOST_VERSION >= 107200
  130. it.disable_recursion_pending(depth <= currentDepth);
  131. #else
  132. it.no_push(depth <= currentDepth);
  133. #endif
  134. type = EResType::DIRECTORY;
  135. }
  136. else
  137. type = EResTypeHelper::getTypeFromExtension(it->path().extension().string());
  138. if (!initial || vstd::contains(initialTypes, type))
  139. {
  140. //reconstruct relative filename (not possible via boost AFAIK)
  141. boost::filesystem::path filename;
  142. const size_t iterations = std::min(static_cast<size_t>(currentDepth), path.size());
  143. if (iterations)
  144. {
  145. filename = path.front();
  146. for (size_t i = 1; i < iterations; ++i)
  147. filename /= path[i];
  148. filename /= it->path().filename();
  149. }
  150. else
  151. filename = it->path().filename();
  152. std::string resName;
  153. #ifdef VCMI_WINDOWS
  154. std::string filenameUtf8 = boost::locale::conv::utf_to_utf<char>(filename.native());
  155. #else
  156. std::string filenameUtf8 = filename.string();
  157. #endif
  158. if (boost::filesystem::path::preferred_separator != '/')
  159. {
  160. // resource names are using UNIX slashes (/)
  161. resName.reserve(resName.size() + filename.native().size());
  162. resName = mountPoint;
  163. for (const char c : filenameUtf8)
  164. if (c != boost::filesystem::path::preferred_separator)
  165. resName.push_back(c);
  166. else
  167. resName.push_back('/');
  168. }
  169. else
  170. resName = mountPoint + filenameUtf8;
  171. fileList[ResourcePath(resName, type)] = std::move(filename);
  172. }
  173. }
  174. return fileList;
  175. }
  176. VCMI_LIB_NAMESPACE_END