CArchiveLoader.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * CArchiveLoader.cpp, 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. #include "StdInc.h"
  11. #include "CArchiveLoader.h"
  12. #include "CFileInputStream.h"
  13. #include "CCompressedStream.h"
  14. #include "CBinaryReader.h"
  15. ArchiveEntry::ArchiveEntry()
  16. : offset(0), fullSize(0), compressedSize(0)
  17. {
  18. }
  19. CArchiveLoader::CArchiveLoader(std::string _mountPoint, boost::filesystem::path _archive) :
  20. archive(std::move(_archive)),
  21. mountPoint(std::move(_mountPoint))
  22. {
  23. // Open archive file(.snd, .vid, .lod)
  24. CFileInputStream fileStream(archive);
  25. // Fake .lod file with no data has to be silently ignored.
  26. if(fileStream.getSize() < 10)
  27. return;
  28. // Retrieve file extension of archive in uppercase
  29. const std::string ext = boost::to_upper_copy(archive.extension().string());
  30. // Init the specific lod container format
  31. if(ext == ".LOD" || ext == ".PAC")
  32. initLODArchive(mountPoint, fileStream);
  33. else if(ext == ".VID")
  34. initVIDArchive(mountPoint, fileStream);
  35. else if(ext == ".SND")
  36. initSNDArchive(mountPoint, fileStream);
  37. else
  38. throw std::runtime_error("LOD archive format unknown. Cannot deal with " + archive.string());
  39. logGlobal->trace("%sArchive \"%s\" loaded (%d files found).", ext, archive.filename(), entries.size());
  40. }
  41. void CArchiveLoader::initLODArchive(const std::string &mountPoint, CFileInputStream & fileStream)
  42. {
  43. // Read count of total files
  44. CBinaryReader reader(&fileStream);
  45. fileStream.seek(8);
  46. ui32 totalFiles = reader.readUInt32();
  47. // Get all entries from file
  48. fileStream.seek(0x5c);
  49. // Insert entries to list
  50. for(ui32 i = 0; i < totalFiles; ++i)
  51. {
  52. char filename[16];
  53. reader.read(reinterpret_cast<ui8*>(filename), 16);
  54. // Create archive entry
  55. ArchiveEntry entry;
  56. entry.name = filename;
  57. entry.offset = reader.readUInt32();
  58. entry.fullSize = reader.readUInt32();
  59. fileStream.skip(4); // unused, unknown
  60. entry.compressedSize = reader.readUInt32();
  61. // Add lod entry to local entries map
  62. entries[ResourceID(mountPoint + entry.name)] = entry;
  63. }
  64. }
  65. void CArchiveLoader::initVIDArchive(const std::string &mountPoint, CFileInputStream & fileStream)
  66. {
  67. // Read count of total files
  68. CBinaryReader reader(&fileStream);
  69. fileStream.seek(0);
  70. ui32 totalFiles = reader.readUInt32();
  71. std::set<int> offsets;
  72. // Insert entries to list
  73. for(ui32 i = 0; i < totalFiles; ++i)
  74. {
  75. char filename[40];
  76. reader.read(reinterpret_cast<ui8*>(filename), 40);
  77. ArchiveEntry entry;
  78. entry.name = filename;
  79. entry.offset = reader.readInt32();
  80. entry.compressedSize = 0;
  81. offsets.insert(entry.offset);
  82. entries[ResourceID(mountPoint + entry.name)] = entry;
  83. }
  84. offsets.insert(fileStream.getSize());
  85. // now when we know position of all files their sizes can be set correctly
  86. for (auto & entry : entries)
  87. {
  88. auto it = offsets.find(entry.second.offset);
  89. it++;
  90. entry.second.fullSize = *it - entry.second.offset;
  91. }
  92. }
  93. void CArchiveLoader::initSNDArchive(const std::string &mountPoint, CFileInputStream & fileStream)
  94. {
  95. // Read count of total files
  96. CBinaryReader reader(&fileStream);
  97. fileStream.seek(0);
  98. ui32 totalFiles = reader.readUInt32();
  99. // Insert entries to list
  100. for(ui32 i = 0; i < totalFiles; ++i)
  101. {
  102. char filename[40];
  103. reader.read(reinterpret_cast<ui8*>(filename), 40);
  104. //for some reason entries in snd have format NAME\0WAVRUBBISH....
  105. //we need to replace first \0 with dot and take the 3 chars with extension (and drop the rest)
  106. ArchiveEntry entry;
  107. entry.name = filename; // till 1st \0
  108. entry.name += '.';
  109. entry.name += std::string(filename + entry.name.size(), 3);
  110. entry.offset = reader.readInt32();
  111. entry.fullSize = reader.readInt32();
  112. entry.compressedSize = 0;
  113. entries[ResourceID(mountPoint + entry.name)] = entry;
  114. }
  115. }
  116. std::unique_ptr<CInputStream> CArchiveLoader::load(const ResourceID & resourceName) const
  117. {
  118. assert(existsResource(resourceName));
  119. const ArchiveEntry & entry = entries.at(resourceName);
  120. if (entry.compressedSize != 0) //compressed data
  121. {
  122. auto fileStream = make_unique<CFileInputStream>(archive, entry.offset, entry.compressedSize);
  123. return make_unique<CCompressedStream>(std::move(fileStream), false, entry.fullSize);
  124. }
  125. else
  126. {
  127. return make_unique<CFileInputStream>(archive, entry.offset, entry.fullSize);
  128. }
  129. }
  130. bool CArchiveLoader::existsResource(const ResourceID & resourceName) const
  131. {
  132. return entries.count(resourceName) != 0;
  133. }
  134. std::string CArchiveLoader::getMountPoint() const
  135. {
  136. return mountPoint;
  137. }
  138. std::unordered_set<ResourceID> CArchiveLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
  139. {
  140. std::unordered_set<ResourceID> foundID;
  141. for (auto & file : entries)
  142. {
  143. if (filter(file.first))
  144. foundID.insert(file.first);
  145. }
  146. return foundID;
  147. }