2
0

AdapterLoaders.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "StdInc.h"
  2. #include "AdapterLoaders.h"
  3. #include "../JsonNode.h"
  4. #include "Filesystem.h"
  5. CMappedFileLoader::CMappedFileLoader(const std::string & mountPoint, const JsonNode &config)
  6. {
  7. for(auto entry : config.Struct())
  8. {
  9. //fileList[ResourceID(mountPoint + entry.first)] = ResourceID(mountPoint + entry.second.String());
  10. fileList.emplace(ResourceID(mountPoint + entry.first), ResourceID(mountPoint + entry.second.String()));
  11. }
  12. }
  13. std::unique_ptr<CInputStream> CMappedFileLoader::load(const ResourceID & resourceName) const
  14. {
  15. return CResourceHandler::get()->load(fileList.at(resourceName));
  16. }
  17. bool CMappedFileLoader::existsResource(const ResourceID & resourceName) const
  18. {
  19. return fileList.count(resourceName) != 0;
  20. }
  21. std::string CMappedFileLoader::getMountPoint() const
  22. {
  23. return ""; // does not have any meaning with this type of data source
  24. }
  25. boost::optional<boost::filesystem::path> CMappedFileLoader::getResourceName(const ResourceID & resourceName) const
  26. {
  27. return CResourceHandler::get()->getResourceName(fileList.at(resourceName));
  28. }
  29. std::unordered_set<ResourceID> CMappedFileLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
  30. {
  31. std::unordered_set<ResourceID> foundID;
  32. for (auto & file : fileList)
  33. {
  34. if (filter(file.first))
  35. foundID.insert(file.first);
  36. }
  37. return foundID;
  38. }
  39. CFilesystemList::CFilesystemList()
  40. {
  41. //loaders = new std::vector<std::unique_ptr<ISimpleResourceLoader> >;
  42. }
  43. CFilesystemList::~CFilesystemList()
  44. {
  45. //delete loaders;
  46. }
  47. std::unique_ptr<CInputStream> CFilesystemList::load(const ResourceID & resourceName) const
  48. {
  49. // load resource from last loader that have it (last overridden version)
  50. for (auto & loader : boost::adaptors::reverse(loaders))
  51. {
  52. if (loader->existsResource(resourceName))
  53. return loader->load(resourceName);
  54. }
  55. throw std::runtime_error("Resource with name " + resourceName.getName() + " and type "
  56. + EResTypeHelper::getEResTypeAsString(resourceName.getType()) + " wasn't found.");
  57. }
  58. bool CFilesystemList::existsResource(const ResourceID & resourceName) const
  59. {
  60. for (auto & loader : loaders)
  61. if (loader->existsResource(resourceName))
  62. return true;
  63. return false;
  64. }
  65. std::string CFilesystemList::getMountPoint() const
  66. {
  67. return "";
  68. }
  69. boost::optional<boost::filesystem::path> CFilesystemList::getResourceName(const ResourceID & resourceName) const
  70. {
  71. if (existsResource(resourceName))
  72. return getResourcesWithName(resourceName).back()->getResourceName(resourceName);
  73. return boost::optional<boost::filesystem::path>();
  74. }
  75. std::set<boost::filesystem::path> CFilesystemList::getResourceNames(const ResourceID & resourceName) const
  76. {
  77. std::set<boost::filesystem::path> paths;
  78. for(auto& loader : getResourcesWithName(resourceName))
  79. {
  80. auto rn = loader->getResourceName(resourceName);
  81. if(rn)
  82. {
  83. paths.insert(rn->string());
  84. }
  85. }
  86. return std::move(paths);
  87. }
  88. std::unordered_set<ResourceID> CFilesystemList::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
  89. {
  90. std::unordered_set<ResourceID> ret;
  91. for (auto & loader : loaders)
  92. for (auto & entry : loader->getFilteredFiles(filter))
  93. ret.insert(entry);
  94. return ret;
  95. }
  96. bool CFilesystemList::createResource(std::string filename, bool update)
  97. {
  98. logGlobal->traceStream()<< "Creating " << filename;
  99. for (auto & loader : boost::adaptors::reverse(loaders))
  100. {
  101. if (writeableLoaders.count(loader.get()) != 0 // writeable,
  102. && loader->createResource(filename, update)) // successfully created
  103. {
  104. // Check if resource was created successfully. Possible reasons for this to fail
  105. // a) loader failed to create resource (e.g. read-only FS)
  106. // b) in update mode, call with filename that does not exists
  107. assert(load(ResourceID(filename)));
  108. logGlobal->traceStream()<< "Resource created successfully";
  109. return true;
  110. }
  111. }
  112. logGlobal->traceStream()<< "Failed to create resource";
  113. return false;
  114. }
  115. std::vector<const ISimpleResourceLoader *> CFilesystemList::getResourcesWithName(const ResourceID & resourceName) const
  116. {
  117. std::vector<const ISimpleResourceLoader *> ret;
  118. for (auto & loader : loaders)
  119. boost::range::copy(loader->getResourcesWithName(resourceName), std::back_inserter(ret));
  120. return ret;
  121. }
  122. void CFilesystemList::addLoader(ISimpleResourceLoader * loader, bool writeable)
  123. {
  124. loaders.push_back(std::unique_ptr<ISimpleResourceLoader>(loader));
  125. if (writeable)
  126. writeableLoaders.insert(loader);
  127. }