CFilesystemLoader.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * CFilesystemLoader.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. #include "ISimpleResourceLoader.h"
  12. #include "ResourceID.h"
  13. class CInputStream;
  14. /**
  15. * A class which can scan and load files of directories.
  16. */
  17. class DLL_LINKAGE CFilesystemLoader : public ISimpleResourceLoader
  18. {
  19. public:
  20. /**
  21. * Ctor.
  22. *
  23. * @param baseDirectory Specifies the base directory and their sub-directories which should be indexed.
  24. * @param depth - recursion depth of subdirectories search. 0 = no recursion
  25. *
  26. * @throws std::runtime_error if the base directory is not a directory or if it is not available
  27. */
  28. explicit CFilesystemLoader(std::string mountPoint, boost::filesystem::path baseDirectory, size_t depth = 16, bool initial = false);
  29. /// Interface implementation
  30. /// @see ISimpleResourceLoader
  31. std::unique_ptr<CInputStream> load(const ResourceID & resourceName) const override;
  32. bool existsResource(const ResourceID & resourceName) const override;
  33. std::string getMountPoint() const override;
  34. bool createResource(std::string filename, bool update = false) override;
  35. boost::optional<boost::filesystem::path> getResourceName(const ResourceID & resourceName) const override;
  36. void updateFilteredFiles(std::function<bool(const std::string &)> filter) const override;
  37. std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
  38. private:
  39. /** The base directory which is scanned and indexed. */
  40. boost::filesystem::path baseDirectory;
  41. std::string mountPoint;
  42. size_t recursiveDepth;
  43. /** A list of files in the directory
  44. * key = ResourceID for resource loader
  45. * value = name that can be used to access file
  46. */
  47. mutable std::unordered_map<ResourceID, boost::filesystem::path> fileList;
  48. /**
  49. * Returns a list of pathnames denoting the files in the directory denoted by this pathname.
  50. *
  51. * If the pathname of this directory is absolute, then the file info pathnames are absolute as well. If the pathname of this directory is relative
  52. * then the file info pathnames are relative to the basedir as well.
  53. *
  54. * @return a list of pathnames denoting the files and directories in the directory denoted by this pathname
  55. * The array will be empty if the directory is empty. Ptr is null if the directory doesn't exist or if it isn't a directory.
  56. */
  57. std::unordered_map<ResourceID, boost::filesystem::path> listFiles(const std::string &mountPoint, size_t depth, bool initial) const;
  58. };