AdapterLoaders.cpp 4.1 KB

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