AdapterLoaders.cpp 3.9 KB

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