2
0

CArchiveLoader.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "VCMIDirs.h"
  13. #include "CFileInputStream.h"
  14. #include "CCompressedStream.h"
  15. #include "CBinaryReader.h"
  16. VCMI_LIB_NAMESPACE_BEGIN
  17. namespace bfs = boost::filesystem;
  18. const bool extractArchives = false;
  19. ArchiveEntry::ArchiveEntry()
  20. : offset(0), fullSize(0), compressedSize(0)
  21. {
  22. }
  23. CArchiveLoader::CArchiveLoader(std::string _mountPoint, bfs::path _archive) :
  24. archive(std::move(_archive)),
  25. mountPoint(std::move(_mountPoint))
  26. {
  27. // Open archive file(.snd, .vid, .lod)
  28. CFileInputStream fileStream(archive);
  29. // Fake .lod file with no data has to be silently ignored.
  30. if(fileStream.getSize() < 10)
  31. return;
  32. // Retrieve file extension of archive in uppercase
  33. const std::string ext = boost::to_upper_copy(archive.extension().string());
  34. // Init the specific lod container format
  35. if(ext == ".LOD" || ext == ".PAC")
  36. initLODArchive(mountPoint, fileStream);
  37. else if(ext == ".VID")
  38. initVIDArchive(mountPoint, fileStream);
  39. else if(ext == ".SND")
  40. initSNDArchive(mountPoint, fileStream);
  41. else
  42. throw std::runtime_error("LOD archive format unknown. Cannot deal with " + archive.string());
  43. logGlobal->trace("%sArchive \"%s\" loaded (%d files found).", ext, archive.filename(), entries.size());
  44. }
  45. void CArchiveLoader::initLODArchive(const std::string &mountPoint, CFileInputStream & fileStream)
  46. {
  47. // Read count of total files
  48. CBinaryReader reader(&fileStream);
  49. fileStream.seek(8);
  50. ui32 totalFiles = reader.readUInt32();
  51. // Get all entries from file
  52. fileStream.seek(0x5c);
  53. // Insert entries to list
  54. for(ui32 i = 0; i < totalFiles; ++i)
  55. {
  56. char filename[16];
  57. reader.read(reinterpret_cast<ui8*>(filename), 16);
  58. // Create archive entry
  59. ArchiveEntry entry;
  60. entry.name = filename;
  61. entry.offset = reader.readUInt32();
  62. entry.fullSize = reader.readUInt32();
  63. fileStream.skip(4); // unused, unknown
  64. entry.compressedSize = reader.readUInt32();
  65. // Add lod entry to local entries map
  66. entries[ResourceID(mountPoint + entry.name)] = entry;
  67. if(extractArchives)
  68. {
  69. si64 currentPosition = fileStream.tell(); // save filestream position
  70. boost::locale::generator gen;
  71. std::locale::global(gen("")); // Create locale generator
  72. std::string fName = filename;
  73. boost::to_upper(fName);
  74. if(fName.find(".PCX") != std::string::npos)
  75. extractToFolder("Images", mountPoint, entry);
  76. else if ((fName.find(".DEF") != std::string::npos ) || (fName.find(".MSK") != std::string::npos) || (fName.find(".FNT") != std::string::npos) || (fName.find(".PAL") != std::string::npos))
  77. extractToFolder("Sprites", mountPoint, entry);
  78. else if ((fName.find(".h3c") != std::string::npos))
  79. extractToFolder("Sprites", mountPoint, entry);
  80. else
  81. extractToFolder("Misc", mountPoint, entry);
  82. fileStream.seek(currentPosition); // restore filestream position
  83. }
  84. }
  85. }
  86. void CArchiveLoader::initVIDArchive(const std::string &mountPoint, CFileInputStream & fileStream)
  87. {
  88. // Read count of total files
  89. CBinaryReader reader(&fileStream);
  90. fileStream.seek(0);
  91. ui32 totalFiles = reader.readUInt32();
  92. std::set<int> offsets;
  93. // Insert entries to list
  94. for(ui32 i = 0; i < totalFiles; ++i)
  95. {
  96. char filename[40];
  97. reader.read(reinterpret_cast<ui8*>(filename), 40);
  98. ArchiveEntry entry;
  99. entry.name = filename;
  100. entry.offset = reader.readInt32();
  101. entry.compressedSize = 0;
  102. offsets.insert(entry.offset);
  103. entries[ResourceID(mountPoint + entry.name)] = entry;
  104. }
  105. offsets.insert((int)fileStream.getSize());
  106. // now when we know position of all files their sizes can be set correctly
  107. for (auto & entry : entries)
  108. {
  109. auto it = offsets.find(entry.second.offset);
  110. it++;
  111. entry.second.fullSize = *it - entry.second.offset;
  112. if(extractArchives)
  113. extractToFolder("Video", fileStream, entry.second);
  114. }
  115. }
  116. void CArchiveLoader::initSNDArchive(const std::string &mountPoint, CFileInputStream & fileStream)
  117. {
  118. // Read count of total files
  119. CBinaryReader reader(&fileStream);
  120. fileStream.seek(0);
  121. ui32 totalFiles = reader.readUInt32();
  122. // Insert entries to list
  123. for(ui32 i = 0; i < totalFiles; ++i)
  124. {
  125. char filename[40];
  126. reader.read(reinterpret_cast<ui8*>(filename), 40);
  127. //for some reason entries in snd have format NAME\0WAVRUBBISH....
  128. //we need to replace first \0 with dot and take the 3 chars with extension (and drop the rest)
  129. ArchiveEntry entry;
  130. entry.name = filename; // till 1st \0
  131. entry.name += '.';
  132. entry.name += std::string(filename + entry.name.size(), 3);
  133. entry.offset = reader.readInt32();
  134. entry.fullSize = reader.readInt32();
  135. entry.compressedSize = 0;
  136. entries[ResourceID(mountPoint + entry.name)] = entry;
  137. if(extractArchives)
  138. extractToFolder("Sound", fileStream, entry);
  139. }
  140. }
  141. std::unique_ptr<CInputStream> CArchiveLoader::load(const ResourceID & resourceName) const
  142. {
  143. assert(existsResource(resourceName));
  144. const ArchiveEntry & entry = entries.at(resourceName);
  145. if (entry.compressedSize != 0) //compressed data
  146. {
  147. auto fileStream = make_unique<CFileInputStream>(archive, entry.offset, entry.compressedSize);
  148. return make_unique<CCompressedStream>(std::move(fileStream), false, entry.fullSize);
  149. }
  150. else
  151. {
  152. return make_unique<CFileInputStream>(archive, entry.offset, entry.fullSize);
  153. }
  154. }
  155. bool CArchiveLoader::existsResource(const ResourceID & resourceName) const
  156. {
  157. return entries.count(resourceName) != 0;
  158. }
  159. std::string CArchiveLoader::getMountPoint() const
  160. {
  161. return mountPoint;
  162. }
  163. std::unordered_set<ResourceID> CArchiveLoader::getFilteredFiles(std::function<bool(const ResourceID &)> filter) const
  164. {
  165. std::unordered_set<ResourceID> foundID;
  166. for (auto & file : entries)
  167. {
  168. if (filter(file.first))
  169. foundID.insert(file.first);
  170. }
  171. return foundID;
  172. }
  173. void CArchiveLoader::extractToFolder( std::string outputSubFolder, CFileInputStream& fileStream, ArchiveEntry entry)
  174. {
  175. si64 currentPosition = fileStream.tell(); // save filestream position
  176. std::unique_ptr<char[]> data = std::unique_ptr<char[]>(new char[entry.fullSize]);
  177. fileStream.seek(entry.offset);
  178. fileStream.read((ui8*)data.get(), entry.fullSize);
  179. bfs::path extractedFilePath = createExtractedFilePath(outputSubFolder, entry.name);
  180. // writeToOutputFile
  181. std::ofstream out(extractedFilePath.string(), std::ofstream::binary);
  182. out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  183. out.write(data.get(), entry.fullSize);
  184. fileStream.seek(currentPosition); // restore filestream position
  185. }
  186. void CArchiveLoader::extractToFolder( std::string outputSubFolder, const std::string& mountPoint, ArchiveEntry entry)
  187. {
  188. std::unique_ptr<CInputStream> & inputStream = load(ResourceID(mountPoint + entry.name));
  189. std::unique_ptr<char[]> data = std::unique_ptr<char[]>(new char[entry.fullSize]);
  190. inputStream->read((ui8*)data.get(), entry.fullSize);
  191. bfs::path extractedFilePath = createExtractedFilePath(outputSubFolder, entry.name);
  192. // writeToOutputFile
  193. std::ofstream out(extractedFilePath.string(), std::ofstream::binary);
  194. out.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  195. out.write(data.get(), entry.fullSize);
  196. }
  197. bfs::path createExtractedFilePath(std::string outputSubFolder, std::string entryName)
  198. {
  199. bfs::path extractionFolderPath = VCMIDirs::get().userCachePath() / "extracted" / outputSubFolder;
  200. bfs::path extractedFilePath = extractionFolderPath / entryName;
  201. bfs::create_directories(extractionFolderPath);
  202. return extractedFilePath;
  203. }
  204. VCMI_LIB_NAMESPACE_END