2
0

CFilesystemLoader.cpp 4.4 KB

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