CArchiveLoader.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. 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 ResourceID & resourceName) const override;
  54. bool existsResource(const ResourceID & resourceName) const override;
  55. std::string getMountPoint() const override;
  56. void updateFilteredFiles(std::function<bool(const std::string &)> filter) const override {}
  57. std::unordered_set<ResourceID> getFilteredFiles(std::function<bool(const ResourceID &)> filter) const override;
  58. /** Extracts one archive entry to the specified subfolder. Used for Video and Sound */
  59. void extractToFolder(const std::string & outputSubFolder, CInputStream & fileStream, const ArchiveEntry & entry) const;
  60. /** Extracts one archive entry to the specified subfolder. Used for Images, Sprites, etc */
  61. void extractToFolder(const std::string & outputSubFolder, const std::string & mountPoint, ArchiveEntry entry) const;
  62. private:
  63. /**
  64. * Initializes a LOD archive.
  65. *
  66. * @param fileStream File stream to the .lod archive
  67. */
  68. void initLODArchive(const std::string &mountPoint, CFileInputStream & fileStream);
  69. /**
  70. * Initializes a VID archive.
  71. *
  72. * @param fileStream File stream to the .vid archive
  73. */
  74. void initVIDArchive(const std::string &mountPoint, CFileInputStream & fileStream);
  75. /**
  76. * Initializes a SND archive.
  77. *
  78. * @param fileStream File stream to the .snd archive
  79. */
  80. void initSNDArchive(const std::string &mountPoint, CFileInputStream & fileStream);
  81. /** The file path to the archive which is scanned and indexed. */
  82. boost::filesystem::path archive;
  83. std::string mountPoint;
  84. /** Holds all entries of the archive file. An entry can be accessed via the entry name. **/
  85. std::unordered_map<ResourceID, ArchiveEntry> entries;
  86. /** Specifies if Original H3 archives should be extracted to a separate folder **/
  87. bool extractArchives;
  88. };
  89. /** Constructs the file path for the extracted file. Creates the subfolder hierarchy aswell **/
  90. boost::filesystem::path createExtractedFilePath(const std::string & outputSubFolder, const std::string & entryName);
  91. VCMI_LIB_NAMESPACE_END