CArchiveLoader.cpp 4.8 KB

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