ISimpleResourceLoader.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * ISimpleResourceLoader.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. class CInputStream;
  12. class ResourceID;
  13. /**
  14. * A class which knows the files containing in the archive or system and how to load them.
  15. */
  16. class DLL_LINKAGE ISimpleResourceLoader
  17. {
  18. public:
  19. virtual ~ISimpleResourceLoader() { };
  20. /**
  21. * Loads a resource with the given resource name.
  22. *
  23. * @param resourceName The unqiue resource name in space of the archive.
  24. * @return a input stream object
  25. */
  26. virtual std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const = 0;
  27. /**
  28. * Checks if the entry exists.
  29. *
  30. * @return Returns true if the entry exists, false if not.
  31. */
  32. virtual bool existsResource(const ResourceID & resourceName) const = 0;
  33. /**
  34. * Gets mount point to which this loader was attached
  35. *
  36. * @return mount point URI
  37. */
  38. virtual std::string getMountPoint() const = 0;
  39. /**
  40. * Gets full name of resource, e.g. name of file in filesystem.
  41. *
  42. * @return path or empty optional if file can't be accessed independently (e.g. file in archive)
  43. */
  44. virtual boost::optional<boost::filesystem::path> getResourceName(const ResourceID & resourceName) const
  45. {
  46. return boost::optional<boost::filesystem::path>();
  47. }
  48. /**
  49. * Gets all full names of matching resources, e.g. names of files in filesystem.
  50. *
  51. * @return std::set with names.
  52. */
  53. virtual std::set<boost::filesystem::path> getResourceNames(const ResourceID & resourceName) const
  54. {
  55. std::set<boost::filesystem::path> result;
  56. auto rn = getResourceName(resourceName);
  57. if(rn)
  58. {
  59. result.insert(rn->string());
  60. }
  61. return result;
  62. }
  63. /**
  64. * Update lists of files that match filter function
  65. *
  66. * @param filter Filter that returns true if specified mount point matches filter
  67. */
  68. virtual void updateFilteredFiles(std::function<bool(const std::string &)> filter) const = 0;
  69. /**
  70. * Get list of files that match filter function
  71. *
  72. * @param filter Filter that returns true if specified ID matches filter
  73. * @return Returns list of flies
  74. */
  75. virtual std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const = 0;
  76. /**
  77. * Creates new resource with specified filename.
  78. *
  79. * @return true if new file was created, false on error or if file already exists
  80. */
  81. virtual bool createResource(std::string filename, bool update = false)
  82. {
  83. return false;
  84. }
  85. /**
  86. * @brief Returns all loaders that have resource with such name
  87. *
  88. * @return vector with all loaders
  89. */
  90. virtual std::vector<const ISimpleResourceLoader *> getResourcesWithName(const ResourceID & resourceName) const
  91. {
  92. if (existsResource(resourceName))
  93. return std::vector<const ISimpleResourceLoader *>(1, this);
  94. return std::vector<const ISimpleResourceLoader *>();
  95. }
  96. };