2
0

CFilesystemLoader.cpp 4.3 KB

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