CFilesystemLoader.cpp 5.5 KB

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