CArchiveLoader.h 3.5 KB

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