CFilesystemLoader.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 "CResourceLoader.h"
  13. class CFileInfo;
  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(const std::string & baseDirectory, size_t depth = 16, bool initial = false);
  30. /**
  31. * Loads a resource with the given resource name.
  32. *
  33. * @param resourceName The unqiue resource name in space of the filesystem.
  34. * @return a input stream object, not null
  35. */
  36. std::unique_ptr<CInputStream> load(const std::string & resourceName) const;
  37. /**
  38. * Checks if the file entry exists.
  39. *
  40. * @return true if the entry exists, false if not.
  41. */
  42. bool existsEntry(const std::string & resourceName) const;
  43. /**
  44. * Gets all entries in the filesystem.
  45. *
  46. * @return a list of all entries in the filesystem.
  47. */
  48. boost::unordered_map<ResourceID, std::string> getEntries() const;
  49. /**
  50. * Gets the origin of the archive loader.
  51. *
  52. * @return the file path to directory with archive (e.g. path/to/h3/mp3)
  53. */
  54. std::string getOrigin() const;
  55. bool createEntry(std::string filename);
  56. private:
  57. /** The base directory which is scanned and indexed. */
  58. std::string baseDirectory;
  59. /** A list of files in the directory
  60. * key = ResourceID for resource loader
  61. * value = name that can be used to access file
  62. */
  63. boost::unordered_map<ResourceID, std::string> fileList;
  64. /**
  65. * Returns a list of pathnames denoting the files in the directory denoted by this pathname.
  66. *
  67. * 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
  68. * then the file info pathnames are relative to the basedir as well.
  69. *
  70. * @return a list of pathnames denoting the files and directories in the directory denoted by this pathname
  71. * 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.
  72. */
  73. boost::unordered_map<ResourceID, std::string> listFiles(size_t depth, bool initial) const;
  74. };