CFilesystemLoader.cpp 4.4 KB

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