AdapterLoaders.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. CFilesystemList::~CFilesystemList()
  42. {
  43. delete loaders;
  44. }
  45. std::unique_ptr<CInputStream> CFilesystemList::load(const ResourceID & resourceName) const
  46. {
  47. // load resource from last loader that have it (last overriden version)
  48. for (auto & loader : boost::adaptors::reverse(*loaders))
  49. {
  50. if (loader->existsResource(resourceName))
  51. return loader->load(resourceName);
  52. }
  53. throw std::runtime_error("Resource with name " + resourceName.getName() + " and type "
  54. + EResTypeHelper::getEResTypeAsString(resourceName.getType()) + " wasn't found.");
  55. }
  56. bool CFilesystemList::existsResource(const ResourceID & resourceName) const
  57. {
  58. return !getResourcesWithName(resourceName).empty();
  59. }
  60. std::string CFilesystemList::getMountPoint() const
  61. {
  62. return "";
  63. }
  64. boost::optional<std::string> CFilesystemList::getResourceName(const ResourceID & resourceName) const
  65. {
  66. if (existsResource(resourceName))
  67. return getResourcesWithName(resourceName).back()->getResourceName(resourceName);
  68. return boost::optional<std::string>();
  69. }
  70. std::unordered_set<ResourceID> CFilesystemList::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
  71. {
  72. std::unordered_set<ResourceID> ret;
  73. for (auto & loader : *loaders)
  74. for (auto & entry : loader->getFilteredFiles(filter))
  75. ret.insert(entry);
  76. return ret;
  77. }
  78. bool CFilesystemList::createResource(std::string filename, bool update)
  79. {
  80. logGlobal->traceStream()<< "Creating " << filename;
  81. for (auto & loader : boost::adaptors::reverse(*loaders))
  82. {
  83. if (writeableLoaders.count(loader.get()) != 0 // writeable,
  84. && loader->createResource(filename, update)) // successfully created
  85. {
  86. // Check if resource was created successfully. Possible reasons for this to fail
  87. // a) loader failed to create resource (e.g. read-only FS)
  88. // b) in update mode, call with filename that does not exists
  89. assert(load(ResourceID(filename)));
  90. logGlobal->traceStream()<< "Resource created successfully";
  91. return true;
  92. }
  93. }
  94. logGlobal->traceStream()<< "Failed to create resource";
  95. return false;
  96. }
  97. std::vector<const ISimpleResourceLoader *> CFilesystemList::getResourcesWithName(const ResourceID & resourceName) const
  98. {
  99. std::vector<const ISimpleResourceLoader *> ret;
  100. for (auto & loader : *loaders)
  101. boost::range::copy(loader->getResourcesWithName(resourceName), std::back_inserter(ret));
  102. return ret;
  103. }
  104. void CFilesystemList::addLoader(ISimpleResourceLoader * loader, bool writeable)
  105. {
  106. loaders->push_back(std::unique_ptr<ISimpleResourceLoader>(loader));
  107. if (writeable)
  108. writeableLoaders.insert(loader);
  109. }
  110. ISimpleResourceLoader * CResourceHandler::get()
  111. {
  112. if(resourceLoader != nullptr)
  113. {
  114. return resourceLoader;
  115. }
  116. else
  117. {
  118. std::stringstream string;
  119. string << "Error: Resource loader wasn't initialized. "
  120. << "Make sure that you set one via FilesystemFactory::initialize";
  121. throw std::runtime_error(string.str());
  122. }
  123. }