CLodArchiveLoader.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * CLodArchiveLoader.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. class CFileInfo;
  13. class CFileInputStream;
  14. /**
  15. * A struct which holds information about the archive entry e.g. where it is located in space of the archive container.
  16. */
  17. struct ArchiveEntry
  18. {
  19. /**
  20. * Default c-tor.
  21. */
  22. ArchiveEntry();
  23. /** Entry name **/
  24. std::string name;
  25. /** Distance in bytes from beginning **/
  26. int offset;
  27. /** Size without compression in bytes **/
  28. int realSize;
  29. /** Size within compression in bytes **/
  30. int size;
  31. };
  32. /**
  33. * A class which can scan and load files of a LOD archive.
  34. */
  35. class DLL_LINKAGE CLodArchiveLoader : public ISimpleResourceLoader
  36. {
  37. public:
  38. /**
  39. * Default c-tor.
  40. */
  41. CLodArchiveLoader();
  42. /**
  43. * Ctor.
  44. *
  45. * The file extension of the param archive determines the type of the Lod file.
  46. * These are valid extensions: .LOD, .SND, .VID
  47. *
  48. * @param archive Specifies the file path to the archive which should be indexed and loaded.
  49. *
  50. * @throws std::runtime_error if the archive wasn't found or if the archive isn't supported
  51. */
  52. explicit CLodArchiveLoader(const std::string & archive);
  53. /**
  54. * Ctor.
  55. *
  56. * The file extension of the param archive determines the type of the Lod file.
  57. * These are valid extensions: .LOD, .SND, .VID
  58. *
  59. * @param archive Specifies the file path to the archive which should be indexed and loaded.
  60. *
  61. * @throws std::runtime_error if the archive wasn't found or if the archive isn't supported
  62. */
  63. explicit CLodArchiveLoader(const CFileInfo & archive);
  64. /**
  65. * Opens an LOD archive.
  66. *
  67. * The file extension of the param archive determines the type of the Lod file.
  68. * These are valid extensions: .LOD, .SND, .VID
  69. *
  70. * @param archive Specifies the file path to the archive which should be indexed and loaded.
  71. *
  72. * @throws std::runtime_error if the archive wasn't found or if the archive isn't supported
  73. */
  74. void open(const std::string & archive);
  75. /**
  76. * Opens an LOD archive.
  77. *
  78. * The file extension of the param archive determines the type of the Lod file.
  79. * These are valid extensions: .LOD, .SND, .VID
  80. *
  81. * @param archive Specifies the file path to the archive which should be indexed and loaded.
  82. *
  83. * @throws std::runtime_error if the archive wasn't found or if the archive isn't supported
  84. */
  85. void open(const CFileInfo & archive);
  86. /**
  87. * Loads a resource with the given resource name.
  88. *
  89. * @param resourceName The unqiue resource name in space of the archive.
  90. * @return a input stream object, not null.
  91. *
  92. * @throws std::runtime_error if the archive entry wasn't found
  93. */
  94. std::unique_ptr<CInputStream> load(const std::string & resourceName) const;
  95. /**
  96. * Gets all entries in the archive.
  97. *
  98. * @return a list of all entries in the archive.
  99. */
  100. std::list<std::string> getEntries() const;
  101. /**
  102. * Gets the archive entry for the requested resource
  103. *
  104. * @param resourceName The unqiue resource name in space of the archive.
  105. * @return the archive entry for the requested resource or a null ptr if the archive wasn't found
  106. */
  107. const ArchiveEntry * getArchiveEntry(const std::string & resourceName) const;
  108. /**
  109. * Checks if the archive entry exists.
  110. *
  111. * @return true if the entry exists, false if not.
  112. */
  113. bool existsEntry(const std::string & resourceName) const;
  114. /**
  115. * Gets the origin of the archive loader.
  116. *
  117. * @return the file path to the archive which is scanned and indexed.
  118. */
  119. std::string getOrigin() const;
  120. private:
  121. /**
  122. * Initializes a LOD archive.
  123. *
  124. * @param fileStream File stream to the .lod archive
  125. */
  126. void initLODArchive(CFileInputStream & fileStream);
  127. /**
  128. * Initializes a VID archive.
  129. *
  130. * @param fileStream File stream to the .vid archive
  131. */
  132. void initVIDArchive(CFileInputStream & fileStream);
  133. /**
  134. * Initializes a SND archive.
  135. *
  136. * @param fileStream File stream to the .snd archive
  137. */
  138. void initSNDArchive(CFileInputStream & fileStream);
  139. /** The file path to the archive which is scanned and indexed. */
  140. std::string archive;
  141. /** Holds all entries of the archive file. An entry can be accessed via the entry name. **/
  142. std::unordered_map<std::string, ArchiveEntry> entries;
  143. };