CFilesystemLoader.cpp 5.3 KB

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