FontSubfile.cpp 17 KB

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