CArchiveLoader.cpp 4.6 KB

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