ISimpleResourceLoader.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #pragma once
  2. /*
  3. * ISimpleResourceLoader.h, part of VCMI engine
  4. *
  5. * Authors: listed in file AUTHORS in main folder
  6. *
  7. * License: GNU General Public License v2.0 or later
  8. * Full text of license available in license.txt file, in main folder
  9. *
  10. */
  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<std::string> getResourceName(const ResourceID & resourceName) const
  45. {
  46. return boost::optional<std::string>();
  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<std::string> getResourceNames(const ResourceID & resourceName) const
  54. {
  55. std::set<std::string> result;
  56. auto rn = getResourceName(resourceName);
  57. if(rn)
  58. {
  59. result.insert(*rn);
  60. }
  61. return result;
  62. }
  63. /**
  64. * Get list of files that matches filter function
  65. *
  66. * @param filter Filter that returns true if specified ID matches filter
  67. * @return Returns list of flies
  68. */
  69. virtual std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const = 0;
  70. /**
  71. * Creates new resource with specified filename.
  72. *
  73. * @return true if new file was created, false on error or if file already exists
  74. */
  75. virtual bool createResource(std::string filename, bool update = false)
  76. {
  77. return false;
  78. }
  79. /**
  80. * @brief Returns all loaders that have resource with such name
  81. *
  82. * @return vector with all loaders
  83. */
  84. virtual std::vector<const ISimpleResourceLoader *> getResourcesWithName(const ResourceID & resourceName) const
  85. {
  86. if (existsResource(resourceName))
  87. return std::vector<const ISimpleResourceLoader *>(1, this);
  88. return std::vector<const ISimpleResourceLoader *>();
  89. }
  90. };