Subfile.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 "CommonFunctions.h"
  10. #include <algorithm>
  11. const long long MAXSIZE = 50ll * 1024ll * 1024ll; // Setting maximal file size 50 MB; Files with size more than 50 mb
  12. // will be recognized as incorrect and passed.
  13. // This should be done because
  14. LOTRO_DAT::Subfile::Subfile() {
  15. ext_ = UNKNOWN;
  16. compressed_ = false;
  17. }
  18. LOTRO_DAT::Subfile::Subfile(DatFile *dat, long long fragments_count, long long unknown1, long long file_id, long long file_offset,
  19. long long file_size, long long timestamp, long long version, long long block_size) :
  20. dat_(dat), fragments_count_(fragments_count), unknown1_(unknown1), file_id_(file_id), file_offset_(file_offset),
  21. file_size_(file_size), timestamp_(timestamp), version_(version), block_size_(block_size) {
  22. if (file_size_ > MAXSIZE)
  23. throw DatException("Bad Subfile::Subfile() - File size is too much... Maybe it's incorrect..?", SUBFILE_EXCEPTION);
  24. ext_ = GetExtension();
  25. BinaryData header(20);
  26. dat_->ReadData(header, 10, file_offset_ + 8);
  27. compressed_ = header.CheckCompression();
  28. }
  29. long long LOTRO_DAT::Subfile::fragments_count() const {
  30. return fragments_count_;
  31. }
  32. long long LOTRO_DAT::Subfile::unknown1() const {
  33. return unknown1_;
  34. }
  35. long long LOTRO_DAT::Subfile::file_id() const {
  36. return file_id_;
  37. }
  38. long long LOTRO_DAT::Subfile::file_offset() const {
  39. return file_offset_;
  40. }
  41. long long LOTRO_DAT::Subfile::file_size() const {
  42. return file_size_;
  43. }
  44. long long LOTRO_DAT::Subfile::timestamp() const {
  45. return timestamp_;
  46. }
  47. long long LOTRO_DAT::Subfile::version() const {
  48. return version_;
  49. }
  50. long long LOTRO_DAT::Subfile::block_size() const {
  51. return block_size_;
  52. }
  53. LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::GetExtension() const {
  54. // Text check based on file_id
  55. if ((file_id_ >> 24ll) == 0x25ll)
  56. return TXT;
  57. // Font check based on file_id
  58. if ((file_id_ >> 24ll) == 0x42ll)
  59. return FONT;
  60. BinaryData header(64);
  61. try {
  62. dat_->ReadData(header, 64, (unsigned) file_offset_ + 8);
  63. } catch (DatException &e) {
  64. if (e.type() == READ_EXCEPTION) {
  65. std::string err =
  66. "Bad Subfile::getExtension() - unable to read header of file with id = " + std::to_string(file_id()) +
  67. " and offset = " + std::to_string(file_offset());
  68. throw DatException(err.c_str(), SUBFILE_EXCEPTION);
  69. } else
  70. throw e;
  71. }
  72. // jpeg / dds check
  73. if ((file_id_ >> 24ll) == 0x41ll) {
  74. long long soi = header.ToNumber<2>(24);
  75. long long marker = header.ToNumber<2>(26);
  76. //auto markerSize = header.ToNumber<short>(28);
  77. //auto four = header.ToNumber<int>(30);
  78. if (soi == 0xD8FFll && marker == 0xE0FFll || marker == 0xE1FFll)
  79. return JPG;
  80. return DDS;
  81. }
  82. // Ogg and Wav check
  83. if (header[8] == 0x4F && header[9] == 0x67 && header[10] == 0x67 && header[11] == 0x53)
  84. return OGG;
  85. if (header[8] == 0x52 && header[9] == 0x49 && header[10] == 0x46 && header[11] == 0x46)
  86. return WAV;
  87. return UNKNOWN;
  88. }
  89. LOTRO_DAT::EXTENSION LOTRO_DAT::Subfile::ext() const {
  90. return ext_;
  91. }
  92. std::string LOTRO_DAT::Subfile::ExtensionToString(LOTRO_DAT::EXTENSION ext) const {
  93. switch (ext)
  94. {
  95. case LOTRO_DAT::TXT:
  96. return ".txt";
  97. case LOTRO_DAT::JPG:
  98. return ".jpg";
  99. case LOTRO_DAT::DDS:
  100. return ".dds";
  101. case LOTRO_DAT::WAV:
  102. return ".wav";
  103. case LOTRO_DAT::OGG:
  104. return ".ogg";
  105. case LOTRO_DAT::FONT:
  106. return ".fontbin";
  107. case LOTRO_DAT::UNKNOWN:
  108. return ".unk";
  109. default:
  110. return "";
  111. }
  112. }
  113. bool LOTRO_DAT::Subfile::PatchBinaryFile(const LOTRO_DAT::BinaryData &file) {
  114. LOTRO_DAT::BinaryData data;
  115. // Making data for placing in .dat, depending on file extension
  116. switch (ext_)
  117. {
  118. case LOTRO_DAT::TXT:
  119. throw LOTRO_DAT::DatException("Bad Subfile::PatchBinaryFile() - trying to patch text file");
  120. case LOTRO_DAT::JPG:
  121. data = MakeFromJPG(file); break;
  122. case LOTRO_DAT::DDS:
  123. data = MakeFromDDS(file); break;
  124. case LOTRO_DAT::WAV:
  125. data = MakeFromWAV(file); break;
  126. case LOTRO_DAT::OGG:
  127. data = MakeFromOGG(file); break;
  128. case LOTRO_DAT::FONT:
  129. data = MakeFromFont(file); break;
  130. case LOTRO_DAT::UNKNOWN:
  131. data = MakeFromUnk(file); break;
  132. default:
  133. break;
  134. }
  135. if (block_size() >= data.size() + 8) {
  136. dat_->WriteData(data, data.size(), file_offset() + 8);
  137. return true;
  138. }
  139. throw DatException("Bad Subfile::PatchBinaryFile() - new data size is bigger, than block_size. This is not implemented yet"
  140. , IMPORT_EXCEPTION);
  141. return true;
  142. }
  143. bool LOTRO_DAT::Subfile::ExportFile(const char *filename, Database *db) const {
  144. try {
  145. BinaryData data;
  146. switch (ext_)
  147. {
  148. case LOTRO_DAT::TXT:
  149. return ExportFileAsTXT(filename, db);
  150. case LOTRO_DAT::JPG:
  151. data = PrepareAsJPG(); break;
  152. case LOTRO_DAT::DDS:
  153. data = PrepareAsDDS(); break;
  154. case LOTRO_DAT::WAV:
  155. data = PrepareAsWAV(); break;
  156. case LOTRO_DAT::OGG:
  157. data = PrepareAsOGG(); break;
  158. case LOTRO_DAT::FONT:
  159. data = PrepareAsFont(); break;
  160. case LOTRO_DAT::UNKNOWN:
  161. data = PrepareAsUnk(); break;
  162. default:
  163. break;
  164. }
  165. if (db == nullptr) {
  166. std::string s = std::string(filename) + std::string(ExtensionToString(ext_));
  167. return data.WriteToFile(s);
  168. } else {
  169. db->PushBinaryFile(file_id_, data);
  170. return true;
  171. }
  172. }
  173. catch (DatException &e) {
  174. fprintf(stderr, "Caught exception while unpacking the file with id %lld and offset %lld. Continuing without this file...\n", file_id(), file_offset());
  175. fprintf(stderr, "%s\n", e.what());
  176. return false;
  177. }
  178. }
  179. bool LOTRO_DAT::Subfile::ExportFileAsTXT(const char *filename, Database *db) const {
  180. if (db == nullptr) {
  181. return false;
  182. }
  183. if (file_size() == 10) // File is empty, nothing to do;
  184. return false;
  185. BinaryData data = GetFileData();
  186. long long offset = 9; // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
  187. long long text_fragment_num = data.ToNumber<1>(offset);
  188. if ((text_fragment_num & 0x80) != 0) {
  189. text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
  190. offset += 1;
  191. }
  192. offset += 1;
  193. for (long long i = 0; i < text_fragment_num; i++) {
  194. long long fragment_id = data.ToNumber<8>(offset);
  195. offset += 8;
  196. long long num_pieces = data.ToNumber<4>(offset);
  197. offset += 4;
  198. std::vector<std::u16string> text_pieces;
  199. std::vector<long long> arg_references;
  200. std::vector<std::vector<BinaryData> > arg_strings;
  201. for (long long j = 0; j < num_pieces; j++) {
  202. long long piece_size = data.ToNumber<1>(offset);
  203. if ((piece_size & 128) != 0) {
  204. piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
  205. offset += 1;
  206. }
  207. offset += 1;
  208. BinaryData piece_data = data.CutData(offset, offset + piece_size * 2);
  209. std::u16string piece;
  210. for (long long k = 0; k < piece_size; k++) {
  211. char16_t c = char16_t(((short(piece_data[2 * k + 1])) << 8) | (short(piece_data[2 * k])));
  212. piece += c;
  213. }
  214. text_pieces.push_back(piece);
  215. offset += piece_size * 2;
  216. }
  217. long long num_references = data.ToNumber<4>(offset);
  218. offset += 4;
  219. for (long long j = 0; j < num_references; j++) {
  220. arg_references.emplace_back(data.ToNumber<4>(offset));
  221. offset += 4;
  222. }
  223. long long num_arg_strings = data.ToNumber<1>(offset);
  224. offset += 1;
  225. for (long long j = 0; j < num_arg_strings; j++) {
  226. long long num_args = data.ToNumber<4>(offset);
  227. offset += 4;
  228. arg_strings.emplace_back();
  229. for (long long k = 0; k < num_args; k++) {
  230. long long string_size = data.ToNumber<1>(offset);
  231. if ((string_size & 0x80) != 0) {
  232. string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
  233. offset += 1;
  234. }
  235. offset += 1;
  236. arg_strings[j].emplace_back(data.CutData(offset, offset + string_size * 2));
  237. offset += string_size * 2;
  238. }
  239. }
  240. std::u16string text = u"[";
  241. for (int i = 0; i + 1 < text_pieces.size(); i++)
  242. text += text_pieces[i] + u"<--DO_NOT_TOUCH!-->";
  243. text += text_pieces[text_pieces.size() - 1] + u"]";
  244. std::string arguments;
  245. for (int i = 0; i + 1 < arg_references.size(); i++)
  246. arguments += std::to_string(arg_references[i]) + "-";
  247. if (arg_references.size() >= 1)
  248. arguments += std::to_string(arg_references[arg_references.size() - 1]);
  249. db->PushTextFile(file_id(), fragment_id, text.c_str(), arguments.c_str());
  250. }
  251. return true;
  252. }
  253. LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::GetFileData(long long offset) const {
  254. BinaryData mfile_id(4);
  255. dat_->ReadData(mfile_id, 4, file_offset() + 8);
  256. if (file_id() != mfile_id.ToNumber<4>(0))
  257. throw DatException("Bad Subfile::GetFileData() - file_id doesn't match to dictionary", READ_EXCEPTION);
  258. BinaryData data((unsigned)(file_size()));
  259. if (block_size() >= file_size() + 8) {
  260. dat_->ReadData(data, file_size(), file_offset() + offset);
  261. return data;
  262. }
  263. BinaryData fragments_count(4);
  264. dat_->ReadData(fragments_count, 4, file_offset());
  265. long long fragments_number = fragments_count.ToNumber<4>(0);
  266. long long current_block_size = block_size() - offset - 8 * fragments_number;
  267. dat_->ReadData(data, current_block_size , file_offset() + offset);
  268. BinaryData FragmentsDictionary(8 * unsigned(fragments_number));
  269. dat_->ReadData(FragmentsDictionary, 8 * unsigned(fragments_number), file_offset() + block_size() - 8 * fragments_number);
  270. for (long long i = 0; i < fragments_number; i++) {
  271. long long fragment_size = FragmentsDictionary.ToNumber<4>(8 * i);
  272. long long fragment_offset = FragmentsDictionary.ToNumber<4>(8 * i + 4);
  273. dat_->ReadData(data, std::min(fragment_size, file_size() - current_block_size), fragment_offset, current_block_size );
  274. current_block_size += fragment_size;
  275. }
  276. return data;
  277. }
  278. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsJPG() const {
  279. return GetFileData().CutData(24);
  280. }
  281. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsDDS() const {
  282. BinaryData data = GetFileData();
  283. if (compressed_)
  284. data.DecompressData(4);
  285. BinaryData ddsData(data.size() - 24 + 128);
  286. for (int i = 0; i < 128; i++)
  287. ddsData[i] = 0;
  288. memcpy(ddsData.data() + 128, data.data() + 24, data.size() - 24);
  289. ddsData[0] = 0x44; // D
  290. ddsData[1] = 0x44; // D
  291. ddsData[2] = 0x53; // S
  292. ddsData[3] = 0x20;
  293. ddsData[4] = 0x7C;
  294. ddsData[8] = 7;
  295. ddsData[9] = 0x10;
  296. // width, height
  297. ddsData[12] = data[12];
  298. ddsData[13] = data[13];
  299. ddsData[14] = data[14];
  300. ddsData[15] = data[15];
  301. ddsData[16] = data[8];
  302. ddsData[17] = data[9];
  303. ddsData[18] = data[10];
  304. ddsData[19] = data[11];
  305. long long compression = data.ToNumber<4>(0x10);
  306. switch (compression) {
  307. case 20: // 14 00 00 00 - 888 (R8G8B8)
  308. ddsData[0x4C] = 0x20; // ?
  309. ddsData[0x50] = 0x40; // compressed or not
  310. ddsData[0x58] = 0x18; // bytes per pixel
  311. ddsData[0x5E] = 0xFF;
  312. ddsData[0x61] = 0xFF;
  313. ddsData[0x64] = 0xFF;
  314. break;
  315. case 21: // 15 00 00 00 - 8888 (R8G8B8A8)
  316. ddsData[0x4C] = 0x20; // ?
  317. ddsData[0x50] = 0x40; // compressed or not
  318. ddsData[0x58] = 0x20; // bytes per pixel
  319. ddsData[0x5E] = 0xFF;
  320. ddsData[0x61] = 0xFF;
  321. ddsData[0x64] = 0xFF;
  322. ddsData[0x6B] = 0xFF;
  323. break;
  324. case 28: // 1C 00 00 00 - 332 (?)
  325. ddsData[0x4C] = 0x20; // ?
  326. ddsData[0x50] = 0x40; // compressed or not
  327. ddsData[0x58] = 0x08; // bytes per pixel
  328. ddsData[0x5E] = 0xFF;
  329. ddsData[0x61] = 0xFF;
  330. ddsData[0x64] = 0xFF;
  331. break;
  332. case 827611204: // 44 58 54 31 - DXT1
  333. ddsData[76] = 32;
  334. ddsData[80] = 4;
  335. ddsData[84] = 68;
  336. ddsData[85] = 88;
  337. ddsData[86] = 84;
  338. ddsData[87] = 49;
  339. break;
  340. case 861165636: // 44 58 54 33 - DXT3
  341. ddsData[22] = 1;
  342. ddsData[76] = 32;
  343. ddsData[80] = 4;
  344. ddsData[84] = 68;
  345. ddsData[85] = 88;
  346. ddsData[86] = 84;
  347. ddsData[87] = 51;
  348. ddsData[108] = 8;
  349. ddsData[109] = 16;
  350. ddsData[110] = 64;
  351. break;
  352. case 894720068: // 44 58 54 35 - DXT5
  353. ddsData[10] = 8;
  354. ddsData[22] = 1;
  355. ddsData[28] = 1;
  356. ddsData[76] = 32;
  357. ddsData[80] = 4;
  358. ddsData[84] = 68;
  359. ddsData[85] = 88;
  360. ddsData[86] = 84;
  361. ddsData[87] = 53;
  362. ddsData[88] = 32;
  363. ddsData[94] = 255;
  364. ddsData[97] = 255;
  365. ddsData[100] = 255;
  366. ddsData[107] = 255;
  367. ddsData[109] = 16;
  368. break;
  369. }
  370. return ddsData;
  371. }
  372. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsOGG() const {
  373. return GetFileData().CutData(8);
  374. }
  375. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsFont() const {
  376. return GetFileData();
  377. }
  378. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsWAV() const {
  379. return GetFileData().CutData(8);
  380. }
  381. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::PrepareAsUnk() const {
  382. return GetFileData();
  383. }
  384. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::MakeFromJPG(const LOTRO_DAT::BinaryData &file) const {
  385. return GetFileData().CutData(0, 24) + file;
  386. }
  387. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::MakeFromDDS(const LOTRO_DAT::BinaryData &file) const {
  388. throw LOTRO_DAT::DatException("Bad Subfile::MakeFromDDS() - not implemented yet", IMPORT_EXCEPTION);
  389. }
  390. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::MakeFromOGG(const LOTRO_DAT::BinaryData &file) const {
  391. throw LOTRO_DAT::DatException("Bad Subfile::MakeFromOGG() - not implemented yet", IMPORT_EXCEPTION);
  392. }
  393. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::MakeFromFont(const LOTRO_DAT::BinaryData &file) const {
  394. throw LOTRO_DAT::DatException("Bad Subfile::MakeFromFont() - not implemented yet", IMPORT_EXCEPTION);
  395. }
  396. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::MakeFromWAV(const LOTRO_DAT::BinaryData &file) const {
  397. throw LOTRO_DAT::DatException("Bad Subfile::MakeFromWAV() - not implemented yet", IMPORT_EXCEPTION);
  398. }
  399. const LOTRO_DAT::BinaryData LOTRO_DAT::Subfile::MakeFromUnk(const LOTRO_DAT::BinaryData &file) const {
  400. throw LOTRO_DAT::DatException("Bad Subfile::MakeFromUnk() - not implemented yet", IMPORT_EXCEPTION);
  401. }