ISimpleResourceLoader.h 3.1 KB

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