TextSubfile.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. //
  2. // Created by Иван_Архипов on 24.11.2017.
  3. //
  4. #include "Subfiles/TextSubfile.h"
  5. #include "BinaryData.h"
  6. #include "DatFile.h"
  7. #include "DatException.h"
  8. #include "SubfileData.h"
  9. #include "EasyLogging++/easylogging++.h"
  10. #include <codecvt>
  11. #include <locale>
  12. std::u16string to_utf16(long long x) {
  13. std::u16string res;
  14. while (x > 0) {
  15. res += char16_t(u'0' + x % 10);
  16. x /= 10ll;
  17. }
  18. std::reverse(res.begin(), res.end());
  19. return res;
  20. }
  21. long long from_utf16(const std::u16string &num) {
  22. long long res = 0;
  23. for (auto c : num) {
  24. res = res * 10ll + (c - u'0');
  25. }
  26. return res;
  27. }
  28. std::string argumentsFromUtf16(const std::u16string &args) {
  29. std::string res;
  30. size_t pointer = 0;
  31. while (pointer < args.length()) {
  32. size_t pointer1 = args.find(u'-', pointer);
  33. if (pointer1 == std::u16string::npos)
  34. pointer1 = args.length();
  35. if (!res.empty())
  36. res += "-";
  37. res += std::to_string(from_utf16(args.substr(pointer, pointer1 - pointer)));
  38. pointer = pointer1 + 1;
  39. }
  40. return res;
  41. }
  42. namespace LOTRO_DAT {
  43. TextSubfile::TextSubfile() = default;
  44. TextSubfile::TextSubfile(DatFile *dat, long long dictionary_offset, long long fragments_count, long long unknown1,
  45. long long file_id, long long file_offset, long long file_size,
  46. long long timestamp,
  47. long long version, long long block_size)
  48. : Subfile(dat, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size) {
  49. }
  50. FILE_TYPE TextSubfile::FileType() const {
  51. return TEXT;
  52. }
  53. std::string TextSubfile::Extension() const {
  54. return std::string(".txt");
  55. }
  56. SubfileData TextSubfile::PrepareForExport(const BinaryData &file_data) {
  57. SubfileData result;
  58. long long offset = 9; // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
  59. long long text_fragment_num = file_data.ToNumber<1>(offset);
  60. if ((text_fragment_num & 0x80) != 0) {
  61. text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | file_data.ToNumber<1>(offset + 1));
  62. offset += 1;
  63. }
  64. offset += 1;
  65. for (long long i = 0; i < text_fragment_num; i++) {
  66. long long fragment_id = file_data.ToNumber<8>(offset);
  67. offset += 8;
  68. std::vector<std::u16string> text_pieces = MakePieces(file_data, offset);
  69. std::vector<long long> arg_references = MakeArgumentReferences(file_data, offset);
  70. std::vector<std::vector<BinaryData>> arg_strings = MakeArgumentStrings(file_data, offset);
  71. std::u16string text = u"[";
  72. for (size_t j = 0; j + 1 < text_pieces.size(); j++)
  73. text += text_pieces[j] + u"<--DO_NOT_TOUCH!-->";
  74. text += text_pieces[text_pieces.size() - 1] + u"]";
  75. std::u16string arguments;
  76. for (size_t j = 0; j + 1 < arg_references.size(); j++)
  77. arguments += to_utf16(arg_references[j]) + u"-";
  78. if (!arg_references.empty())
  79. arguments += to_utf16(arg_references[arg_references.size() - 1]);
  80. if (result.text_data.length() > 0)
  81. result.text_data += u"|||";
  82. result.text_data += to_utf16(fragment_id) + u":::";
  83. result.text_data += arguments + u":::";
  84. result.text_data += text;
  85. }
  86. result.options["fid"] = file_id();
  87. result.options["ext"] = Extension();
  88. return result;
  89. }
  90. BinaryData TextSubfile::MakeForImport(const BinaryData &old_data, const SubfileData &data) {
  91. LOG(DEBUG) << "Preparing text file " << file_id() << " for import.";
  92. std::unordered_map<long long, SubfileData> patch_fragments = ParsePatchFragments(data);
  93. BinaryData new_data;
  94. if (file_size() <= 10 + 8) // File is empty, nothing to do;
  95. return old_data;
  96. long long offset = 9 + 8; // first 8 bytes - file_info. After them:
  97. // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
  98. long long text_fragment_num = old_data.ToNumber<1>(offset);
  99. if ((text_fragment_num & 0x80) != 0) {
  100. text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | old_data.ToNumber<1>(offset + 1));
  101. offset += 1;
  102. }
  103. offset += 1;
  104. new_data = new_data + old_data.CutData(0, offset); // Adding file info
  105. for (long long i = 0; i < text_fragment_num; i++) {
  106. long long fragment_id = old_data.ToNumber<8>(offset);
  107. offset += 8;
  108. new_data = new_data + old_data.CutData(offset - 8, offset);
  109. if (patch_fragments.count(fragment_id) == 0) {
  110. try {
  111. // Retrieving old pieces
  112. new_data = new_data + GetPieceData(old_data, offset);
  113. } catch (std::exception &e) {
  114. LOG(ERROR) << "Caught " << e.what() << " exception.";
  115. LOG(DEBUG) << "Unable to get piece data for file_id " << file_id() << " and fragment_id " << fragment_id;
  116. throw DatException("Bad TextSubfile::MakeForImport()", IMPORT_EXCEPTION);
  117. }
  118. try {
  119. // Retrieving old references
  120. new_data = new_data + GetArgumentReferenceData(old_data, offset);
  121. } catch (std::exception &e) {
  122. LOG(ERROR) << "Caught " << e.what() << " exception.";
  123. LOG(DEBUG) << "Unable to get argument reference data for file_id " << file_id() << " and fragment_id " << fragment_id;
  124. throw DatException("Bad TextSubfile::MakeForImport()", IMPORT_EXCEPTION);
  125. }
  126. try {
  127. // Retrieving old ref_strings
  128. new_data = new_data + GetArgumentStringsData(old_data, offset);
  129. } catch (std::exception &e) {
  130. LOG(ERROR) << "Caught " << e.what() << " exception.";
  131. LOG(DEBUG) << "Unable to get argument string for file_id " << file_id() << " and fragment_id " << fragment_id;
  132. throw DatException("Bad TextSubfile::MakeForImport()", IMPORT_EXCEPTION);
  133. }
  134. } else {
  135. try {
  136. // Making and adding new pieces
  137. new_data = new_data + BuildPieces(old_data, patch_fragments[fragment_id], offset);
  138. } catch (std::exception &e) {
  139. LOG(ERROR) << "Caught " << e.what() << " exception.";
  140. LOG(DEBUG) << "Unable to build piece data for file_id " << file_id() << " and fragment_id " << fragment_id;
  141. throw DatException("Bad TextSubfile::MakeForImport()", IMPORT_EXCEPTION);
  142. }
  143. try {
  144. // Making and adding new references
  145. new_data = new_data + BuildArgumentReferences(old_data, patch_fragments[fragment_id], offset);
  146. } catch (std::exception &e) {
  147. LOG(ERROR) << "Caught " << e.what() << " exception.";
  148. LOG(DEBUG) << "Unable to build argument data for file_id " << file_id() << " and fragment_id " << fragment_id;
  149. throw DatException("Bad TextSubfile::MakeForImport()", IMPORT_EXCEPTION);
  150. }
  151. try {
  152. // Making and adding new strings
  153. new_data = new_data + BuildArgumentStrings(old_data, patch_fragments[fragment_id], offset);
  154. } catch (std::exception &e) {
  155. LOG(ERROR) << "Caught " << e.what() << " exception.";
  156. LOG(DEBUG) << "Unable to build argument string data for file_id " << file_id() << " and fragment_id " << fragment_id;
  157. throw DatException("Bad TextSubfile::MakeForImport()", IMPORT_EXCEPTION);
  158. }
  159. }
  160. }
  161. new_data = new_data + old_data.CutData(offset); // Adding elapsed file data
  162. return new_data;
  163. }
  164. std::unordered_map<long long, SubfileData> TextSubfile::ParsePatchFragments(const SubfileData &data) {
  165. LOG(DEBUG) << "Started parsing patch fragments";
  166. std::unordered_map<long long, SubfileData> res;
  167. std::u16string text = data.text_data;
  168. size_t pointer = 0;
  169. while (pointer < text.length()) {
  170. // Parsing fragment_id
  171. size_t pointer1 = text.find(u":::", pointer);
  172. if (pointer1 == std::u16string::npos)
  173. throw DatException("Bad TextSubfile::ParsePatchFragments() - Unable to parse fragment id! Cannot find '...' divider");
  174. long long fragment_id = from_utf16(text.substr(pointer, pointer1 - pointer));
  175. pointer = pointer1 + 3;
  176. res[fragment_id] = SubfileData();
  177. res[fragment_id].options["gid"] = fragment_id;
  178. // Parsing arguments
  179. pointer1 = text.find(u":::", pointer);
  180. if (pointer1 == std::u16string::npos)
  181. throw DatException("Bad TextSubfile::ParsePatchFragments() - Unable to parse arguments! Cannot find '...' divider");
  182. std::u16string arguments = text.substr(pointer, pointer1 - pointer);
  183. pointer = pointer1 + 3;
  184. if (arguments.length() > 0) {
  185. res[fragment_id].options["args"] = argumentsFromUtf16(arguments);
  186. }
  187. // Parsing text
  188. pointer1 = text.find(u"|||", pointer);
  189. if (pointer1 == std::u16string::npos)
  190. pointer1 = text.length();
  191. std::u16string text_data = text.substr(pointer, pointer1 - pointer);
  192. pointer = pointer1 + 3;
  193. res[fragment_id].text_data = text_data;
  194. }
  195. LOG(DEBUG) << "Finished parsing text patch fragments";
  196. return res;
  197. }
  198. // Make pieces/arguments/argument strings functions
  199. std::vector<std::u16string> TextSubfile::MakePieces(const BinaryData &data, long long &offset) {
  200. LOG(DEBUG) << "Started making pieces";
  201. long long num_pieces = data.ToNumber<4>(offset);
  202. offset += 4;
  203. std::vector<std::u16string> text_pieces;
  204. for (long long j = 0; j < num_pieces; j++) {
  205. long long piece_size = data.ToNumber<1>(offset);
  206. if ((piece_size & 128) != 0) {
  207. piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
  208. offset += 1;
  209. }
  210. offset += 1;
  211. BinaryData piece_data = data.CutData(offset, offset + piece_size * 2);
  212. std::u16string piece;
  213. for (long long k = 0; k < piece_size; k++) {
  214. char16_t c = char16_t(
  215. ((short(piece_data[2 * unsigned(k) + 1])) << 8) | (short(piece_data[2 * unsigned(k)])));
  216. piece += c;
  217. }
  218. text_pieces.push_back(piece);
  219. offset += piece_size * 2;
  220. }
  221. LOG(DEBUG) << "Finished making pieces";
  222. return text_pieces;
  223. }
  224. std::vector<long long> TextSubfile::MakeArgumentReferences(const BinaryData &data, long long &offset) {
  225. LOG(DEBUG) << "Started making argument references";
  226. std::vector<long long> arg_references;
  227. long long num_references = data.ToNumber<4>(offset);
  228. offset += 4;
  229. for (long long j = 0; j < num_references; j++) {
  230. arg_references.emplace_back(data.ToNumber<4>(offset));
  231. offset += 4;
  232. }
  233. LOG(DEBUG) << "Finished making argument references";
  234. return arg_references;
  235. }
  236. std::vector<std::vector<BinaryData>> TextSubfile::MakeArgumentStrings(const BinaryData &data, long long &offset) {
  237. LOG(DEBUG) << "Started making argument strings";
  238. std::vector<std::vector<BinaryData> > arg_strings;
  239. long long num_arg_strings = data.ToNumber<1>(offset);
  240. offset += 1;
  241. for (long long j = 0; j < num_arg_strings; j++) {
  242. long long num_args = data.ToNumber<4>(offset);
  243. offset += 4;
  244. arg_strings.emplace_back();
  245. for (long long k = 0; k < num_args; k++) {
  246. long long string_size = data.ToNumber<1>(offset);
  247. if ((string_size & 0x80) != 0) {
  248. string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
  249. offset += 1;
  250. }
  251. offset += 1;
  252. arg_strings[unsigned(j)].emplace_back(data.CutData(offset, offset + string_size * 2));
  253. offset += string_size * 2;
  254. }
  255. }
  256. LOG(DEBUG) << "Finished making argument strings";
  257. return arg_strings;
  258. }
  259. // Build pieces/arguments/argument strings functions from fragment SubfileData
  260. BinaryData TextSubfile::BuildPieces(const BinaryData &data, const SubfileData &new_data, long long &offset) {
  261. LOG(DEBUG) << "Started building pieces";
  262. try {
  263. // Moving &offset pointer in &data
  264. GetPieceData(data, offset);
  265. } catch (std::exception &e) {
  266. LOG(ERROR) << "Caught " << e.what() << " exception.";
  267. LOG(DEBUG) << "Unable to get piece data for file_id " << file_id();
  268. throw DatException("Bad TextSubfile::BuildPieces()", IMPORT_EXCEPTION);
  269. }
  270. // Deleting '[' and ']' brackets
  271. std::u16string text_data = new_data.text_data.substr(1, new_data.text_data.size() - 2);
  272. std::vector<std::u16string> pieces;
  273. const std::u16string DNT = u"<--DO_NOT_TOUCH!-->";
  274. size_t prev = 0;
  275. size_t next = text_data.find(DNT, prev);
  276. while (next != std::string::npos) {
  277. std::u16string piece = text_data.substr(prev, next - prev);
  278. pieces.push_back(piece);
  279. prev = next + DNT.length();
  280. next = text_data.find(DNT, prev);
  281. }
  282. std::u16string piece = text_data.substr(prev);
  283. pieces.push_back(piece);
  284. // Building BinaryData from pieces
  285. BinaryData result;
  286. BinaryData temp_data = BinaryData::FromNumber<4>(pieces.size());
  287. result = result + temp_data;
  288. for (auto piece : pieces) {
  289. long long piece_size = piece.length();
  290. if (piece_size < 128) {
  291. temp_data = BinaryData::FromNumber<1>(piece_size);
  292. } else {
  293. temp_data = BinaryData::FromNumberRAW<2>((piece_size | 32768));
  294. }
  295. result = result + temp_data;
  296. for (long long j = 0; j < piece_size; j++) {
  297. temp_data = BinaryData::FromNumber<2>(short(piece[j]));
  298. result = result + temp_data;
  299. }
  300. }
  301. LOG(DEBUG) << "Pieces built successfully";
  302. return result;
  303. }
  304. BinaryData TextSubfile::BuildArgumentReferences(const BinaryData &data, const SubfileData &new_data,
  305. long long &offset) {
  306. LOG(DEBUG) << "Started building argument refs";
  307. // Moving &offset pointer in &data
  308. GetArgumentReferenceData(data, offset);
  309. // If there are no args - making 4 null-bytes and return;
  310. if (!new_data.options["args"]) {
  311. BinaryData result = BinaryData::FromNumber<4>(0);
  312. return result;
  313. }
  314. // Parsing arguments from list in options["args"]
  315. std::string args_list = new_data.options["args"].as<std::string>();
  316. std::vector<long long> arguments;
  317. size_t prev = 0;
  318. size_t next = args_list.find('-', prev);
  319. while (next != std::string::npos) {
  320. std::string argument = args_list.substr(prev, next - prev);
  321. arguments.push_back(std::stoll(argument));
  322. prev = next + 1;
  323. next = args_list.find('-', prev);
  324. }
  325. std::string argument = args_list.substr(prev);
  326. arguments.push_back(std::stoll(argument));
  327. BinaryData result;
  328. BinaryData temp_data = BinaryData::FromNumber<4>(arguments.size());
  329. result = result + temp_data;
  330. for (auto arg_reference : arguments) {
  331. temp_data = BinaryData::FromNumber<4>(arg_reference);
  332. result = result + temp_data;
  333. }
  334. LOG(DEBUG) << "Argument refs built successfully";
  335. return result;
  336. }
  337. BinaryData TextSubfile::BuildArgumentStrings(const BinaryData &data, const SubfileData &new_data,
  338. long long &offset) {
  339. LOG(DEBUG) << "Started building argument strings";
  340. LOG(DEBUG) << "Built arg strings successfully";
  341. return GetArgumentStringsData(data, offset);
  342. }
  343. // Get BinaryData contents of pieces/arguments/argument strings
  344. BinaryData TextSubfile::GetPieceData(const BinaryData &data, long long &offset) const {
  345. LOG(DEBUG) << "Started getting piece data";
  346. long long old_offset = offset;
  347. long long num_pieces = data.ToNumber<4>(offset);
  348. offset += 4;
  349. for (long long j = 0; j < num_pieces; j++) {
  350. long long piece_size = data.ToNumber<1>(offset);
  351. if ((piece_size & 128) != 0) {
  352. piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
  353. offset += 1;
  354. }
  355. offset += 1;
  356. offset += piece_size * 2;
  357. }
  358. LOG(DEBUG) << "Got piece data";
  359. return data.CutData(old_offset, offset);
  360. }
  361. BinaryData TextSubfile::GetArgumentReferenceData(const BinaryData &data, long long &offset) const {
  362. LOG(DEBUG) << "Started getting arg refs data";
  363. long long old_offset = offset;
  364. long long num_references = data.ToNumber<4>(offset);
  365. offset += 4;
  366. offset += 4 * num_references;
  367. LOG(DEBUG) << "Finished getting arg refs data";
  368. return data.CutData(old_offset, offset);
  369. }
  370. BinaryData TextSubfile::GetArgumentStringsData(const BinaryData &data, long long &offset) const {
  371. LOG(DEBUG) << "Started getting arg strings data";
  372. long long old_offset = offset;
  373. long long num_arg_strings = data.ToNumber<1>(offset);
  374. offset += 1;
  375. for (long long j = 0; j < num_arg_strings; j++) {
  376. long long num_args = data.ToNumber<4>(offset);
  377. offset += 4;
  378. for (long long k = 0; k < num_args; k++) {
  379. long long string_size = data.ToNumber<1>(offset);
  380. if ((string_size & 0x80) != 0) {
  381. string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
  382. offset += 1;
  383. }
  384. offset += 1;
  385. offset += string_size * 2;
  386. }
  387. }
  388. LOG(DEBUG) << "Finished getting arg strings data";
  389. return data.CutData(old_offset, offset);
  390. }
  391. };