CArchiveLoader.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * CArchiveLoader.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 CFileInputStream;
  15. /**
  16. * A struct which holds information about the archive entry e.g. where it is located in space of the archive container.
  17. */
  18. struct ArchiveEntry
  19. {
  20. /**
  21. * Default c-tor.
  22. */
  23. ArchiveEntry();
  24. /** Entry name **/
  25. std::string name;
  26. /** Distance in bytes from beginning **/
  27. int offset;
  28. /** Size without compression in bytes **/
  29. int fullSize;
  30. /** Size with compression in bytes or 0 if not compressed **/
  31. int compressedSize;
  32. };
  33. /**
  34. * A class which can scan and load files of a LOD archive.
  35. */
  36. class DLL_LINKAGE CArchiveLoader : public ISimpleResourceLoader
  37. {
  38. public:
  39. /**
  40. * Ctor.
  41. *
  42. * The file extension of the param archive determines the type of the Lod file.
  43. * These are valid extensions: .LOD, .SND, .VID
  44. *
  45. * @param archive Specifies the file path to the archive which should be indexed and loaded.
  46. * @param extractArchives Specifies if the original H3 archives should be extracted to a separate folder.
  47. *
  48. * @throws std::runtime_error if the archive wasn't found or if the archive isn't supported
  49. */
  50. CArchiveLoader(std::string mountPoint, boost::filesystem::path archive, bool extractArchives = false);
  51. /// Interface implementation
  52. /// @see ISimpleResourceLoader
  53. std::unique_ptr<CInputStream> load(const ResourcePath & resourceName) const override;
  54. bool existsResource(const ResourcePath & resourceName) const override;
  55. std::string getMountPoint() const override;
  56. const std::unordered_map<ResourcePath, ArchiveEntry> & getEntries() const;
  57. void updateFilteredFiles(std::function<bool(const std::string &)> filter) const override {}
  58. std::unordered_set<ResourcePath> getFilteredFiles(std::function<bool(const ResourcePath &)> filter) const override;
  59. /** Extracts one archive entry to the specified subfolder. Used for Video and Sound */
  60. void extractToFolder(const std::string & outputSubFolder, CInputStream & fileStream, const ArchiveEntry & entry, bool absolute = false) const;
  61. /** Extracts one archive entry to the specified subfolder. Used for Images, Sprites, etc */
  62. void extractToFolder(const std::string & outputSubFolder, const std::string & mountPoint, ArchiveEntry entry, bool absolute = false) const;
  63. std::string getFullFileURI(const ResourcePath& resourceName) const override;
  64. std::time_t getLastWriteTime(const ResourcePath& resourceName) const override;
  65. private:
  66. /**
  67. * Initializes a LOD archive.
  68. *
  69. * @param fileStream File stream to the .lod archive
  70. */
  71. void initLODArchive(const std::string &mountPoint, CFileInputStream & fileStream);
  72. /**
  73. * Initializes a VID archive.
  74. *
  75. * @param fileStream File stream to the .vid archive
  76. */
  77. void initVIDArchive(const std::string &mountPoint, CFileInputStream & fileStream);
  78. /**
  79. * Initializes a SND archive.
  80. *
  81. * @param fileStream File stream to the .snd archive
  82. */
  83. void initSNDArchive(const std::string &mountPoint, CFileInputStream & fileStream);
  84. /** The file path to the archive which is scanned and indexed. */
  85. boost::filesystem::path archive;
  86. std::string mountPoint;
  87. /** Holds all entries of the archive file. An entry can be accessed via the entry name. **/
  88. std::unordered_map<ResourcePath, ArchiveEntry> entries;
  89. /** Specifies if Original H3 archives should be extracted to a separate folder **/
  90. bool extractArchives;
  91. };
  92. /** Constructs the file path for the extracted file. Creates the subfolder hierarchy aswell **/
  93. boost::filesystem::path createExtractedFilePath(const std::string & outputSubFolder, const std::string & entryName, bool absolute);
  94. VCMI_LIB_NAMESPACE_END