CArchiveLoader.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "StdInc.h"
  2. #include "CArchiveLoader.h"
  3. #include "CFileInputStream.h"
  4. #include "CCompressedStream.h"
  5. #include "CBinaryReader.h"
  6. #include "CFileInfo.h"
  7. ArchiveEntry::ArchiveEntry()
  8. : offset(0), fullSize(0), compressedSize(0)
  9. {
  10. }
  11. CArchiveLoader::CArchiveLoader(const std::string &mountPoint, const std::string & archive):
  12. archive(archive),
  13. mountPoint(mountPoint)
  14. {
  15. // Open archive file(.snd, .vid, .lod)
  16. CFileInputStream fileStream(archive);
  17. // Fake .lod file with no data has to be silently ignored.
  18. if(fileStream.getSize() < 10)
  19. return;
  20. // Retrieve file extension of archive in uppercase
  21. CFileInfo fileInfo(archive);
  22. std::string ext = fileInfo.getExtension();
  23. boost::to_upper(ext);
  24. // Init the specific lod container format
  25. if(ext == ".LOD" || ext == ".PAC")
  26. {
  27. initLODArchive(mountPoint, fileStream);
  28. }
  29. else if(ext == ".VID")
  30. {
  31. initVIDArchive(mountPoint, fileStream);
  32. }
  33. else if(ext == ".SND")
  34. {
  35. initSNDArchive(mountPoint, fileStream);
  36. }
  37. else
  38. {
  39. throw std::runtime_error("LOD archive format unknown. Cannot deal with " + archive);
  40. }
  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(fileStream.getSize());
  86. // now when we know postion 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. std::unique_ptr<CInputStream> fileStream(new CFileInputStream(archive, entry.offset, entry.compressedSize));
  124. return std::unique_ptr<CInputStream>(new CCompressedStream(std::move(fileStream), false, entry.fullSize));
  125. }
  126. else
  127. {
  128. return std::unique_ptr<CInputStream>(new 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. }