CFilesystemLoader.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. std::unordered_map<ResourcePath, boost::filesystem::path> fileList;
  101. if(!boost::filesystem::is_directory(baseDirectory))
  102. return fileList;
  103. std::vector<boost::filesystem::path> path; //vector holding relative path to our file
  104. boost::filesystem::recursive_directory_iterator enddir;
  105. #if BOOST_VERSION >= 107200 // 1.72
  106. boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::directory_options::follow_directory_symlink);
  107. #else
  108. boost::filesystem::recursive_directory_iterator it(baseDirectory, boost::filesystem::symlink_option::recurse);
  109. #endif
  110. for(; it != enddir; ++it)
  111. {
  112. EResType type;
  113. #if BOOST_VERSION >= 107200
  114. const auto currentDepth = it.depth();
  115. #else
  116. const auto currentDepth = it.level();
  117. #endif
  118. if (boost::filesystem::is_directory(it->status()))
  119. {
  120. path.resize(currentDepth + 1);
  121. path.back() = it->path().filename();
  122. // don't iterate into directory if depth limit reached
  123. #if BOOST_VERSION >= 107200
  124. it.disable_recursion_pending(depth <= currentDepth);
  125. #else
  126. it.no_push(depth <= currentDepth);
  127. #endif
  128. type = EResType::DIRECTORY;
  129. }
  130. else
  131. type = EResTypeHelper::getTypeFromExtension(it->path().extension().string());
  132. if (!initial || vstd::contains(initialTypes, type))
  133. {
  134. //reconstruct relative filename (not possible via boost AFAIK)
  135. boost::filesystem::path filename;
  136. const size_t iterations = std::min(static_cast<size_t>(currentDepth), path.size());
  137. if (iterations)
  138. {
  139. filename = path.front();
  140. for (size_t i = 1; i < iterations; ++i)
  141. filename /= path[i];
  142. filename /= it->path().filename();
  143. }
  144. else
  145. filename = it->path().filename();
  146. std::string resName;
  147. if (boost::filesystem::path::preferred_separator != '/')
  148. {
  149. // resource names are using UNIX slashes (/)
  150. resName.reserve(resName.size() + filename.native().size());
  151. resName = mountPoint;
  152. for (const char c : filename.string())
  153. if (c != boost::filesystem::path::preferred_separator)
  154. resName.push_back(c);
  155. else
  156. resName.push_back('/');
  157. }
  158. else
  159. resName = mountPoint + filename.string();
  160. fileList[ResourcePath(resName, type)] = std::move(filename);
  161. }
  162. }
  163. return fileList;
  164. }
  165. VCMI_LIB_NAMESPACE_END