TextSubfile.cpp 18 KB

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