Subfile.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 unknown0, long long unknown1, long long file_id, long long file_offset, long long size1,
  17. long long timestamp, long long version, long long size2) :
  18. dat_(dat), unknown0_(unknown0), unknown1_(unknown1), file_id_(file_id), file_offset_(file_offset),
  19. size1_(size1), timestamp_(timestamp), version_(version), size2_(size2) {
  20. if (size1_ > 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_);
  25. compressed_ = header.CheckCompression();
  26. }
  27. long long LOTRO_DAT::Subfile::unknown0() const {
  28. return unknown0_;
  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::size1() const {
  40. return size1_;
  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::size2() const {
  49. return size2_;
  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() const {
  151. BinaryData data((unsigned)size1());
  152. if (size2() >= size1() + 8) {
  153. dat_->ReadData(data, size1(), file_offset() + 8);
  154. return data;
  155. }
  156. long long first_block_size = size2() - 8 - 4;
  157. dat_->ReadData(data, first_block_size, file_offset() + 8);
  158. BinaryData newFragmentOffset(4);
  159. dat_->ReadData(newFragmentOffset, 4, file_offset() + size2() - 4);
  160. long long part_offset = newFragmentOffset.ToNumber<4>(0);
  161. dat_->ReadData(data, size1() - first_block_size, part_offset, first_block_size);
  162. return data;
  163. }
  164. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsJPG() const {
  165. return GetFileData().CutData(24);
  166. }
  167. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsDDS() const {
  168. BinaryData data = GetFileData();
  169. if (compressed_)
  170. data.DecompressData(4);
  171. BinaryData ddsData(data.size() - 24 + 128);
  172. for (int i = 0; i < 128; i++)
  173. ddsData[i] = 0;
  174. memcpy(ddsData.data() + 128, data.data() + 24, data.size() - 24);
  175. ddsData[0] = 0x44; // D
  176. ddsData[1] = 0x44; // D
  177. ddsData[2] = 0x53; // S
  178. ddsData[3] = 0x20;
  179. ddsData[4] = 0x7C;
  180. ddsData[8] = 7;
  181. ddsData[9] = 0x10;
  182. // width, height
  183. ddsData[12] = data[12];
  184. ddsData[13] = data[13];
  185. ddsData[14] = data[14];
  186. ddsData[15] = data[15];
  187. ddsData[16] = data[8];
  188. ddsData[17] = data[9];
  189. ddsData[18] = data[10];
  190. ddsData[19] = data[11];
  191. long long compression = data.ToNumber<4>(0x10);
  192. switch (compression) {
  193. case 20: // 14 00 00 00 - 888 (R8G8B8)
  194. ddsData[0x4C] = 0x20; // ?
  195. ddsData[0x50] = 0x40; // compressed or not
  196. ddsData[0x58] = 0x18; // bytes per pixel
  197. ddsData[0x5E] = 0xFF;
  198. ddsData[0x61] = 0xFF;
  199. ddsData[0x64] = 0xFF;
  200. break;
  201. case 21: // 15 00 00 00 - 8888 (R8G8B8A8)
  202. ddsData[0x4C] = 0x20; // ?
  203. ddsData[0x50] = 0x40; // compressed or not
  204. ddsData[0x58] = 0x20; // bytes per pixel
  205. ddsData[0x5E] = 0xFF;
  206. ddsData[0x61] = 0xFF;
  207. ddsData[0x64] = 0xFF;
  208. ddsData[0x6B] = 0xFF;
  209. break;
  210. case 28: // 1C 00 00 00 - 332 (?)
  211. ddsData[0x4C] = 0x20; // ?
  212. ddsData[0x50] = 0x40; // compressed or not
  213. ddsData[0x58] = 0x08; // bytes per pixel
  214. ddsData[0x5E] = 0xFF;
  215. ddsData[0x61] = 0xFF;
  216. ddsData[0x64] = 0xFF;
  217. break;
  218. case 827611204: // 44 58 54 31 - DXT1
  219. ddsData[76] = 32;
  220. ddsData[80] = 4;
  221. ddsData[84] = 68;
  222. ddsData[85] = 88;
  223. ddsData[86] = 84;
  224. ddsData[87] = 49;
  225. break;
  226. case 861165636: // 44 58 54 33 - DXT3
  227. ddsData[22] = 1;
  228. ddsData[76] = 32;
  229. ddsData[80] = 4;
  230. ddsData[84] = 68;
  231. ddsData[85] = 88;
  232. ddsData[86] = 84;
  233. ddsData[87] = 51;
  234. ddsData[108] = 8;
  235. ddsData[109] = 16;
  236. ddsData[110] = 64;
  237. break;
  238. case 894720068: // 44 58 54 35 - DXT5
  239. ddsData[10] = 8;
  240. ddsData[22] = 1;
  241. ddsData[28] = 1;
  242. ddsData[76] = 32;
  243. ddsData[80] = 4;
  244. ddsData[84] = 68;
  245. ddsData[85] = 88;
  246. ddsData[86] = 84;
  247. ddsData[87] = 53;
  248. ddsData[88] = 32;
  249. ddsData[94] = 255;
  250. ddsData[97] = 255;
  251. ddsData[100] = 255;
  252. ddsData[107] = 255;
  253. ddsData[109] = 16;
  254. break;
  255. }
  256. return ddsData;
  257. }
  258. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsOGG() const {
  259. return GetFileData().CutData(8);
  260. }
  261. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsFont() const {
  262. throw DatException("Bad Subfile::ExportAsFont() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
  263. }
  264. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsWAV() const {
  265. return GetFileData().CutData(8);
  266. }
  267. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsUnk() const {
  268. throw DatException("Bad Subfile::ExportAsUnk() - this function hasn't been implemented yet", EXPORT_EXCEPTION);
  269. }