CFilesystemLoader.cpp 6.1 KB

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