CFilesystemLoader.h 2.6 KB

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