Subfile.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // Created by Иван_Архипов on 01.11.2017.
  3. //
  4. #include "Subfile.h"
  5. #include "BinaryData.h"
  6. #include "DatFile.h"
  7. #include "DatException.h"
  8. #include "Database.h"
  9. #include <iostream>
  10. const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb
  11. // will be recognized as incorrect and passed.
  12. LOTRO_DAT::Subfile::Subfile() {
  13. ext_ = UNKNOWN;
  14. compressed_ = false;
  15. }
  16. LOTRO_DAT::Subfile::Subfile(DatFile *dat, long long fragments_count, long long unknown1, long long file_id, long long file_offset,
  17. long long file_size, long long timestamp, long long version, long long block_size) :
  18. dat_(dat), fragments_count_(fragments_count), unknown1_(unknown1), file_id_(file_id), file_offset_(file_offset),
  19. file_size_(file_size), timestamp_(timestamp), version_(version), block_size_(block_size) {
  20. if (file_size_ > MAXSIZE)
  21. throw DatException("Bad Subfile::Subfile() - File size is too much... Maybe it's incorrect..?", SUBFILE_EXCEPTION);
  22. ext_ = GetExtension();
  23. BinaryData header(20);
  24. dat_->ReadData(header, 10, file_offset_ + 8);
  25. compressed_ = header.CheckCompression();
  26. }
  27. long long LOTRO_DAT::Subfile::fragments_count() const {
  28. return fragments_count_;
  29. }
  30. long long LOTRO_DAT::Subfile::unknown1() const {
  31. return unknown1_;
  32. }
  33. long long LOTRO_DAT::Subfile::file_id() const {
  34. return file_id_;
  35. }
  36. long long LOTRO_DAT::Subfile::file_offset() const {
  37. return file_offset_;
  38. }
  39. long long LOTRO_DAT::Subfile::file_size() const {
  40. return file_size_;
  41. }
  42. long long LOTRO_DAT::Subfile::timestamp() const {
  43. return timestamp_;
  44. }
  45. long long LOTRO_DAT::Subfile::version() const {
  46. return version_;
  47. }
  48. long long LOTRO_DAT::Subfile::block_size() const {
  49. return block_size_;
  50. }
  51. LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::GetExtension() const {
  52. // Text check based on file_id
  53. if ((file_id_ >> 24ll) == 0x25ll)
  54. return TXT;
  55. // Font check based on file_id
  56. if ((file_id_ >> 24ll) == 0x42ll)
  57. return FONT;
  58. BinaryData header(64);
  59. try {
  60. dat_->ReadData(header, 64, (unsigned) file_offset_ + 8);
  61. } catch (DatException &e) {
  62. if (e.type() == READ_EXCEPTION) {
  63. std::string err =
  64. "Bad Subfile::getExtension() - unable to read header of file with id = " + std::to_string(file_id()) +
  65. " and offset = " + std::to_string(file_offset());
  66. throw DatException(err.c_str(), SUBFILE_EXCEPTION);
  67. } else
  68. throw e;
  69. }
  70. // jpeg / dds check
  71. if ((file_id_ >> 24ll) == 0x41ll) {
  72. long long soi = header.ToNumber<2>(24);
  73. long long marker = header.ToNumber<2>(26);
  74. //auto markerSize = header.ToNumber<short>(28);
  75. //auto four = header.ToNumber<int>(30);
  76. if (soi == 0xD8FFll && marker == 0xE0FFll || marker == 0xE1FFll)
  77. return JPG;
  78. return DDS;
  79. }
  80. // Ogg and Wav check
  81. if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
  82. return OGG;
  83. if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
  84. return WAV;
  85. return UNKNOWN;
  86. }
  87. LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::ext() const {
  88. return ext_;
  89. }
  90. std::string LOTRO_DAT::Subfile::ExtensionToString(LOTRO_DAT::EXTENSION ext) const {
  91. switch (ext)
  92. {
  93. case LOTRO_DAT::TXT:
  94. return ".txt";
  95. case LOTRO_DAT::JPG:
  96. return ".jpg";
  97. case LOTRO_DAT::DDS:
  98. return ".dds";
  99. case LOTRO_DAT::WAV:
  100. return ".wav";
  101. case LOTRO_DAT::OGG:
  102. return ".ogg";
  103. case LOTRO_DAT::FONT:
  104. return ".fontbin";
  105. case LOTRO_DAT::UNKNOWN:
  106. return ".unk";
  107. default:
  108. return "";
  109. }
  110. }
  111. bool LOTRO_DAT::Subfile::ExportFile(const char *filename, Database *db) const {
  112. try {
  113. BinaryData data;
  114. switch (ext_)
  115. {
  116. case LOTRO_DAT::TXT:
  117. return ExportFileAsTXT(filename, db);
  118. case LOTRO_DAT::JPG:
  119. data = PrepareAsJPG(); break;
  120. case LOTRO_DAT::DDS:
  121. data = PrepareAsDDS(); break;
  122. case LOTRO_DAT::WAV:
  123. data = PrepareAsWAV(); break;
  124. case LOTRO_DAT::OGG:
  125. data = PrepareAsOGG(); break;
  126. case LOTRO_DAT::FONT:
  127. data = PrepareAsFont(); break;
  128. case LOTRO_DAT::UNKNOWN:
  129. data = PrepareAsUnk(); break;
  130. default:
  131. break;
  132. }
  133. if (db == nullptr) {
  134. std::string s = std::string(filename) + std::string(ExtensionToString(ext_));
  135. return data.WriteToFile(s);
  136. } else {
  137. db->PushBinaryFile(file_id_, data);
  138. return true;
  139. }
  140. }
  141. catch (DatException &e) {
  142. fprintf(stderr, "Caught exception while unpacking the file with id %lld and offset %lld. Continuing without this file...\n", file_id(), file_offset());
  143. fprintf(stderr, "%s\n", e.what());
  144. return false;
  145. }
  146. }
  147. bool LOTRO_DAT::Subfile::ExportFileAsTXT(const char *filename, Database *db) const {
  148. throw DatException("ExportAsTXT function is not implemented yet", EXPORT_EXCEPTION);
  149. }
  150. LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::GetFileData(long long offset) const {
  151. BinaryData data((unsigned)(file_size()));
  152. if (block_size() >= file_size() + 8) {
  153. dat_->ReadData(data, file_size(), file_offset() + offset);
  154. return data;
  155. }
  156. BinaryData fragments_count(4);
  157. dat_->ReadData(fragments_count, 4, file_offset());
  158. long long fragments_number = fragments_count.ToNumber<4>(0);
  159. long long current_block_size = block_size() - offset - 8 * fragments_number;
  160. dat_->ReadData(data, current_block_size , file_offset() + offset);
  161. BinaryData FragmentsDictionary(8 * unsigned(fragments_number));
  162. dat_->ReadData(FragmentsDictionary, 8 * unsigned(fragments_number), file_offset() + block_size() - 8 * fragments_number);
  163. for (long long i = 0; i < fragments_number; i++) {
  164. long long fragment_size = FragmentsDictionary.ToNumber<4>(8 * i);
  165. long long fragment_offset = FragmentsDictionary.ToNumber<4>(8 * i + 4);
  166. dat_->ReadData(data, std::min(fragment_size, file_size() - current_block_size), fragment_offset, current_block_size );
  167. current_block_size += fragment_size;
  168. }
  169. return data;
  170. }
  171. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsJPG() const {
  172. return GetFileData().CutData(24);
  173. }
  174. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsDDS() const {
  175. BinaryData data = GetFileData();
  176. if (compressed_)
  177. data.DecompressData(4);
  178. BinaryData ddsData(data.size() - 24 + 128);
  179. for (int i = 0; i < 128; i++)
  180. ddsData[i] = 0;
  181. memcpy(ddsData.data() + 128, data.data() + 24, data.size() - 24);
  182. ddsData[0] = 0x44; // D
  183. ddsData[1] = 0x44; // D
  184. ddsData[2] = 0x53; // S
  185. ddsData[3] = 0x20;
  186. ddsData[4] = 0x7C;
  187. ddsData[8] = 7;
  188. ddsData[9] = 0x10;
  189. // width, height
  190. ddsData[12] = data[12];
  191. ddsData[13] = data[13];
  192. ddsData[14] = data[14];
  193. ddsData[15] = data[15];
  194. ddsData[16] = data[8];
  195. ddsData[17] = data[9];
  196. ddsData[18] = data[10];
  197. ddsData[19] = data[11];
  198. long long compression = data.ToNumber<4>(0x10);
  199. switch (compression) {
  200. case 20: // 14 00 00 00 - 888 (R8G8B8)
  201. ddsData[0x4C] = 0x20; // ?
  202. ddsData[0x50] = 0x40; // compressed or not
  203. ddsData[0x58] = 0x18; // bytes per pixel
  204. ddsData[0x5E] = 0xFF;
  205. ddsData[0x61] = 0xFF;
  206. ddsData[0x64] = 0xFF;
  207. break;
  208. case 21: // 15 00 00 00 - 8888 (R8G8B8A8)
  209. ddsData[0x4C] = 0x20; // ?
  210. ddsData[0x50] = 0x40; // compressed or not
  211. ddsData[0x58] = 0x20; // bytes per pixel
  212. ddsData[0x5E] = 0xFF;
  213. ddsData[0x61] = 0xFF;
  214. ddsData[0x64] = 0xFF;
  215. ddsData[0x6B] = 0xFF;
  216. break;
  217. case 28: // 1C 00 00 00 - 332 (?)
  218. ddsData[0x4C] = 0x20; // ?
  219. ddsData[0x50] = 0x40; // compressed or not
  220. ddsData[0x58] = 0x08; // bytes per pixel
  221. ddsData[0x5E] = 0xFF;
  222. ddsData[0x61] = 0xFF;
  223. ddsData[0x64] = 0xFF;
  224. break;
  225. case 827611204: // 44 58 54 31 - DXT1
  226. ddsData[76] = 32;
  227. ddsData[80] = 4;
  228. ddsData[84] = 68;
  229. ddsData[85] = 88;
  230. ddsData[86] = 84;
  231. ddsData[87] = 49;
  232. break;
  233. case 861165636: // 44 58 54 33 - DXT3
  234. ddsData[22] = 1;
  235. ddsData[76] = 32;
  236. ddsData[80] = 4;
  237. ddsData[84] = 68;
  238. ddsData[85] = 88;
  239. ddsData[86] = 84;
  240. ddsData[87] = 51;
  241. ddsData[108] = 8;
  242. ddsData[109] = 16;
  243. ddsData[110] = 64;
  244. break;
  245. case 894720068: // 44 58 54 35 - DXT5
  246. ddsData[10] = 8;
  247. ddsData[22] = 1;
  248. ddsData[28] = 1;
  249. ddsData[76] = 32;
  250. ddsData[80] = 4;
  251. ddsData[84] = 68;
  252. ddsData[85] = 88;
  253. ddsData[86] = 84;
  254. ddsData[87] = 53;
  255. ddsData[88] = 32;
  256. ddsData[94] = 255;
  257. ddsData[97] = 255;
  258. ddsData[100] = 255;
  259. ddsData[107] = 255;
  260. ddsData[109] = 16;
  261. break;
  262. }
  263. return ddsData;
  264. }
  265. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsOGG() const {
  266. return GetFileData().CutData(8);
  267. }
  268. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsFont() const {
  269. throw DatException("Bad Subfile::ExportAsFont() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
  270. }
  271. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsWAV() const {
  272. return GetFileData().CutData(8);
  273. }
  274. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsUnk() const {
  275. throw DatException("Bad Subfile::ExportAsUnk() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
  276. }