2
0

CFilesystemLoader.cpp 4.8 KB

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