CFilesystemLoader.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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 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. boost::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. if (!FileStream::CreateFile(baseDirectory / filename))
  74. return false;
  75. }
  76. fileList[resID] = filename;
  77. return true;
  78. }
  79. std::unordered_map<ResourceID, bfs::path> CFilesystemLoader::listFiles(const std::string &mountPoint, size_t depth, bool initial) const
  80. {
  81. static const EResType::Type initArray[] = {
  82. EResType::DIRECTORY,
  83. EResType::TEXT,
  84. EResType::ARCHIVE_LOD,
  85. EResType::ARCHIVE_VID,
  86. EResType::ARCHIVE_SND,
  87. EResType::ARCHIVE_ZIP };
  88. static const std::set<EResType::Type> initialTypes(initArray, initArray + ARRAY_COUNT(initArray));
  89. assert(bfs::is_directory(baseDirectory));
  90. std::unordered_map<ResourceID, bfs::path> fileList;
  91. std::vector<bfs::path> path; //vector holding relative path to our file
  92. bfs::recursive_directory_iterator enddir;
  93. bfs::recursive_directory_iterator it(baseDirectory, bfs::symlink_option::recurse);
  94. for(; it != enddir; ++it)
  95. {
  96. EResType::Type type;
  97. if (bfs::is_directory(it->status()))
  98. {
  99. path.resize(it.level() + 1);
  100. path.back() = it->path().filename();
  101. // don't iterate into directory if depth limit reached
  102. it.no_push(depth <= it.level());
  103. type = EResType::DIRECTORY;
  104. }
  105. else
  106. type = EResTypeHelper::getTypeFromExtension(it->path().extension().string());
  107. if (!initial || vstd::contains(initialTypes, type))
  108. {
  109. //reconstruct relative filename (not possible via boost AFAIK)
  110. bfs::path filename;
  111. const size_t iterations = std::min((size_t)it.level(), path.size());
  112. if (iterations)
  113. {
  114. filename = path.front();
  115. for (size_t i = 1; i < iterations; ++i)
  116. filename /= path[i];
  117. filename /= it->path().filename();
  118. }
  119. else
  120. filename = it->path().filename();
  121. std::string resName;
  122. if (bfs::path::preferred_separator != '/')
  123. {
  124. // resource names are using UNIX slashes (/)
  125. resName.reserve(resName.size() + filename.native().size());
  126. resName = mountPoint;
  127. for (const char c : filename.string())
  128. if (c != bfs::path::preferred_separator)
  129. resName.push_back(c);
  130. else
  131. resName.push_back('/');
  132. }
  133. else
  134. resName = mountPoint + filename.string();
  135. fileList[ResourceID(resName, type)] = std::move(filename);
  136. }
  137. }
  138. return fileList;
  139. }