CFilesystemLoader.cpp 4.9 KB

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