CFilesystemLoader.cpp 6.4 KB

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