CArchiveLoader.cpp 4.6 KB

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