2
0

AdapterLoaders.cpp 3.8 KB

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