CLodArchiveLoader.cpp 5.4 KB

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