CLodArchiveLoader.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include "StdInc.h"
  2. #include "CLodArchiveLoader.h"
  3. #include "CInputStream.h"
  4. #include "CFileInputStream.h"
  5. #include "CCompressedStream.h"
  6. #include "CBinaryReader.h"
  7. #include "CFileInfo.h"
  8. #include <SDL_endian.h>
  9. ArchiveEntry::ArchiveEntry()
  10. : offset(0), realSize(0), size(0)
  11. {
  12. }
  13. CLodArchiveLoader::CLodArchiveLoader(const std::string & archive)
  14. {
  15. // Open archive file(.snd, .vid, .lod)
  16. this->archive = archive;
  17. CFileInputStream fileStream(archive);
  18. // Fake .lod file with no data has to be silently ignored.
  19. if(fileStream.getSize() < 10)
  20. return;
  21. // Retrieve file extension of archive in uppercase
  22. CFileInfo fileInfo(archive);
  23. std::string ext = fileInfo.getExtension();
  24. boost::to_upper(ext);
  25. // Init the specific lod container format
  26. if(ext == ".LOD" || ext == ".PAC")
  27. {
  28. initLODArchive(fileStream);
  29. }
  30. else if(ext == ".VID")
  31. {
  32. initVIDArchive(fileStream);
  33. }
  34. else if(ext == ".SND")
  35. {
  36. initSNDArchive(fileStream);
  37. }
  38. else
  39. {
  40. throw std::runtime_error("LOD archive format unknown. Cannot deal with " + archive);
  41. }
  42. }
  43. void CLodArchiveLoader::initLODArchive(CFileInputStream & fileStream)
  44. {
  45. // Define LodEntryBlock struct
  46. struct LodEntryBlock
  47. {
  48. char filename[16];
  49. ui32 offset;
  50. ui32 uncompressedSize;
  51. ui32 unused;
  52. ui32 size;
  53. };
  54. // Read count of total files
  55. CBinaryReader reader(fileStream);
  56. fileStream.seek(8);
  57. ui32 totalFiles = reader.readUInt32();
  58. // Get all entries from file
  59. fileStream.seek(0x5c);
  60. struct LodEntryBlock * lodEntries = new struct LodEntryBlock[totalFiles];
  61. fileStream.read(reinterpret_cast<ui8 *>(lodEntries), sizeof(struct LodEntryBlock) * totalFiles);
  62. // Insert entries to list
  63. for(ui32 i = 0; i < totalFiles; i++)
  64. {
  65. // Create archive entry
  66. ArchiveEntry entry;
  67. entry.name = lodEntries[i].filename;
  68. entry.offset= SDL_SwapLE32(lodEntries[i].offset);
  69. entry.realSize = SDL_SwapLE32(lodEntries[i].uncompressedSize);
  70. entry.size = SDL_SwapLE32(lodEntries[i].size);
  71. // Add lod entry to local entries map
  72. entries[entry.name] = entry;
  73. }
  74. // Delete lod entries array
  75. delete[] lodEntries;
  76. }
  77. void CLodArchiveLoader::initVIDArchive(CFileInputStream & fileStream)
  78. {
  79. // Define VideoEntryBlock struct
  80. struct VideoEntryBlock
  81. {
  82. char filename[40];
  83. ui32 offset;
  84. };
  85. // Read count of total files
  86. CBinaryReader reader(fileStream);
  87. fileStream.seek(0);
  88. ui32 totalFiles = reader.readUInt32();
  89. // Get all entries from file
  90. fileStream.seek(4);
  91. struct VideoEntryBlock * vidEntries = new struct VideoEntryBlock[totalFiles];
  92. fileStream.read(reinterpret_cast<ui8 *>(vidEntries), sizeof(struct VideoEntryBlock) * totalFiles);
  93. // Insert entries to list
  94. for(ui32 i = 0; i < totalFiles; i++)
  95. {
  96. VideoEntryBlock vidEntry = vidEntries[i];
  97. ArchiveEntry entry;
  98. entry.name = vidEntry.filename;
  99. entry.offset = SDL_SwapLE32(vidEntry.offset);
  100. // There is no size, so check where the next file is
  101. if (i == totalFiles - 1)
  102. {
  103. entry.size = fileStream.getSize() - entry.offset;
  104. }
  105. else
  106. {
  107. VideoEntryBlock nextVidEntry = vidEntries[i + 1];
  108. entry.realSize = SDL_SwapLE32(nextVidEntry.offset) - entry.offset;
  109. entry.size = 0;
  110. }
  111. entries[entry.name] = entry;
  112. }
  113. // Delete vid entries array
  114. delete[] vidEntries;
  115. }
  116. void CLodArchiveLoader::initSNDArchive(CFileInputStream & fileStream)
  117. {
  118. // Define SoundEntryBlock struct
  119. struct SoundEntryBlock
  120. {
  121. char filename[40];
  122. ui32 offset;
  123. ui32 size;
  124. };
  125. // Read count of total files
  126. CBinaryReader reader(fileStream);
  127. fileStream.seek(0);
  128. ui32 totalFiles = reader.readUInt32();
  129. // Get all entries from file
  130. fileStream.seek(4);
  131. struct SoundEntryBlock * sndEntries = new struct SoundEntryBlock[totalFiles];
  132. fileStream.read(reinterpret_cast<ui8 *>(sndEntries), sizeof(struct SoundEntryBlock) * totalFiles);
  133. // Insert entries to list
  134. for(ui32 i = 0; i < totalFiles; i++)
  135. {
  136. SoundEntryBlock sndEntry = sndEntries[i];
  137. ArchiveEntry entry;
  138. //for some reason entries in snd have format NAME\0WAV\0\0\0....
  139. //we need to replace first \0 with dot and trim line
  140. entry.name = std::string(sndEntry.filename, 40);
  141. entry.name[entry.name.find_first_of('\0')] = '.';
  142. entry.name.resize(entry.name.find_first_of('\0'));
  143. entry.offset = SDL_SwapLE32(sndEntry.offset);
  144. entry.realSize = SDL_SwapLE32(sndEntry.size);
  145. entry.size = 0;
  146. entries[entry.name] = entry;
  147. }
  148. // Delete snd entries array
  149. delete[] sndEntries;
  150. }
  151. std::unique_ptr<CInputStream> CLodArchiveLoader::load(const std::string & resourceName) const
  152. {
  153. assert(existsEntry(resourceName));
  154. const ArchiveEntry & entry = entries.find(resourceName)->second;
  155. if (entry.size != 0) //compressed data
  156. {
  157. std::unique_ptr<CInputStream> fileStream(new CFileInputStream(getOrigin(), entry.offset, entry.size));
  158. return std::unique_ptr<CInputStream>(new CCompressedStream(std::move(fileStream), false, entry.realSize));
  159. }
  160. else
  161. {
  162. return std::unique_ptr<CInputStream>(new CFileInputStream(getOrigin(), entry.offset, entry.realSize));
  163. }
  164. }
  165. boost::unordered_map<ResourceID, std::string> CLodArchiveLoader::getEntries() const
  166. {
  167. boost::unordered_map<ResourceID, std::string> retList;
  168. for(auto it = entries.begin(); it != entries.end(); ++it)
  169. {
  170. const ArchiveEntry & entry = it->second;
  171. retList[ResourceID(entry.name)] = entry.name;
  172. }
  173. return retList;
  174. }
  175. const ArchiveEntry * CLodArchiveLoader::getArchiveEntry(const std::string & resourceName) const
  176. {
  177. auto it = entries.find(resourceName);
  178. if(it != entries.end())
  179. {
  180. return &(it->second);
  181. }
  182. return nullptr;
  183. }
  184. bool CLodArchiveLoader::existsEntry(const std::string & resourceName) const
  185. {
  186. return entries.find(resourceName) != entries.end();
  187. }
  188. std::string CLodArchiveLoader::getOrigin() const
  189. {
  190. return archive;
  191. }