TextSubfile.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. std::u16string to_utf16(long long x)
  11. {
  12. std::u16string res;
  13. while (x > 0) {
  14. res += u'0' + short(x % 10);
  15. x /= 10ll;
  16. }
  17. std::reverse(res.begin(), res.end());
  18. return res;
  19. }
  20. namespace LOTRO_DAT {
  21. TextSubfile::TextSubfile() = default;
  22. TextSubfile::TextSubfile(DatFile *dat, long long dictionary_offset, long long fragments_count, long long unknown1,
  23. long long file_id, long long file_offset, long long file_size,
  24. long long timestamp,
  25. long long version, long long block_size)
  26. : Subfile(dat, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size) {
  27. }
  28. FILE_TYPE TextSubfile::FileType() const {
  29. return TEXT;
  30. }
  31. std::string TextSubfile::Extension() const {
  32. return std::string(".txt");
  33. }
  34. SubfileData TextSubfile::PrepareForExport(const BinaryData &file_data) {
  35. SubfileData result;
  36. if (file_size() <= 10) // File is empty, nothing to do;
  37. return result;
  38. long long offset = 9; // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
  39. long long text_fragment_num = file_data.ToNumber<1>(offset);
  40. if ((text_fragment_num & 0x80) != 0) {
  41. text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | file_data.ToNumber<1>(offset + 1));
  42. offset += 1;
  43. }
  44. offset += 1;
  45. for (long long i = 0; i < text_fragment_num; i++) {
  46. long long fragment_id = file_data.ToNumber<8>(offset);
  47. offset += 8;
  48. std::vector<std::u16string> text_pieces = MakePieces(file_data, offset);
  49. std::vector<long long> arg_references = MakeArgumentReferences(file_data, offset);
  50. std::vector<std::vector<BinaryData>> arg_strings = MakeArgumentStrings(file_data, offset);
  51. std::u16string text = u"[";
  52. for (size_t j = 0; j + 1 < text_pieces.size(); j++)
  53. text += text_pieces[j] + u"<--DO_NOT_TOUCH!-->";
  54. text += text_pieces[text_pieces.size() - 1] + u"]";
  55. std::u16string arguments;
  56. for (size_t j = 0; j + 1 < arg_references.size(); j++)
  57. arguments += to_utf16(arg_references[j]) + u"-";
  58. if (!arg_references.empty())
  59. arguments += to_utf16(arg_references[arg_references.size() - 1]);
  60. if (result.text_data.length() > 0)
  61. result.text_data += u"|||";
  62. result.text_data += to_utf16(fragment_id) + u":::";
  63. result.text_data += arguments + u":::";
  64. result.text_data += text;
  65. }
  66. result.options["fid"] = file_id();
  67. result.options["ext"] = Extension();
  68. return result;
  69. }
  70. BinaryData TextSubfile::MakeForImport(const BinaryData &old_data, const SubfileData &data) {
  71. BinaryData new_data;
  72. // Creating map for fast access to patch fragment by it's id
  73. std::unordered_map<long long, long long> fragment_vector_dictionary;
  74. std::cerr << "Fragments for " << file_id() << std::endl;
  75. for (int i = 0; i < FileFragments.size(); i++)
  76. fragment_vector_dictionary[FileFragments[i].options["gid"].as<long long>()] = i;
  77. for (auto i : fragment_vector_dictionary)
  78. std::cerr << i.first << " ";
  79. std::cerr << std::endl;
  80. if (file_size() <= 10 + 8) // File is empty, nothing to do;
  81. return old_data;
  82. long long offset = 9 + 8; // first 8 bytes - file_info. After them:
  83. // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
  84. long long text_fragment_num = old_data.ToNumber<1>(offset);
  85. if ((text_fragment_num & 0x80) != 0) {
  86. text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | old_data.ToNumber<1>(offset + 1));
  87. offset += 1;
  88. }
  89. offset += 1;
  90. new_data = new_data + old_data.CutData(0, offset); // Adding file info
  91. for (long long i = 0; i < text_fragment_num; i++) {
  92. long long fragment_id = old_data.ToNumber<8>(offset);
  93. offset += 8;
  94. new_data = new_data + old_data.CutData(offset - 8, offset);
  95. if (fragment_vector_dictionary.count(fragment_id) == 0) {
  96. std::cerr << "Omg, what..? " << file_id() << " " << fragment_id << " is not in patch? " << std::endl;
  97. new_data = new_data + GetPieceData(old_data, offset); // Retrieving old pieces
  98. new_data = new_data + GetArgumentReferenceData(old_data, offset); // Retrieving old references
  99. new_data = new_data + GetArgumentStringsData(old_data, offset); // Retrieving old ref_strings
  100. } else {
  101. SubfileData fragment_data = FileFragments[fragment_vector_dictionary[fragment_id]];
  102. new_data = new_data + BuildPieces(old_data, fragment_data, offset); // Making and adding new pieces
  103. new_data = new_data + BuildArgumentReferences(old_data, fragment_data, offset); // Making and adding new references
  104. new_data = new_data + BuildArgumentStrings(old_data, fragment_data, offset); // Making and adding new strings
  105. }
  106. }
  107. new_data = new_data + old_data.CutData(offset); // Adding elapsed file data
  108. return new_data;
  109. }
  110. std::vector<std::u16string> TextSubfile::MakePieces(const BinaryData &data, long long &offset) {
  111. long long num_pieces = data.ToNumber<4>(offset);
  112. offset += 4;
  113. std::vector<std::u16string> text_pieces;
  114. for (long long j = 0; j < num_pieces; j++) {
  115. long long piece_size = data.ToNumber<1>(offset);
  116. if ((piece_size & 128) != 0) {
  117. piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
  118. offset += 1;
  119. }
  120. offset += 1;
  121. BinaryData piece_data = data.CutData(offset, offset + piece_size * 2);
  122. std::u16string piece;
  123. for (long long k = 0; k < piece_size; k++) {
  124. char16_t c = char16_t(
  125. ((short(piece_data[2 * unsigned(k) + 1])) << 8) | (short(piece_data[2 * unsigned(k)])));
  126. piece += c;
  127. }
  128. text_pieces.push_back(piece);
  129. offset += piece_size * 2;
  130. }
  131. return text_pieces;
  132. }
  133. std::vector<long long> TextSubfile::MakeArgumentReferences(const BinaryData &data, long long &offset) {
  134. std::vector<long long> arg_references;
  135. long long num_references = data.ToNumber<4>(offset);
  136. offset += 4;
  137. for (long long j = 0; j < num_references; j++) {
  138. arg_references.emplace_back(data.ToNumber<4>(offset));
  139. offset += 4;
  140. }
  141. return arg_references;
  142. }
  143. std::vector<std::vector<BinaryData>> TextSubfile::MakeArgumentStrings(const BinaryData &data, long long &offset) {
  144. std::vector<std::vector<BinaryData> > arg_strings;
  145. long long num_arg_strings = data.ToNumber<1>(offset);
  146. offset += 1;
  147. for (long long j = 0; j < num_arg_strings; j++) {
  148. long long num_args = data.ToNumber<4>(offset);
  149. offset += 4;
  150. arg_strings.emplace_back();
  151. for (long long k = 0; k < num_args; k++) {
  152. long long string_size = data.ToNumber<1>(offset);
  153. if ((string_size & 0x80) != 0) {
  154. string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
  155. offset += 1;
  156. }
  157. offset += 1;
  158. arg_strings[unsigned(j)].emplace_back(data.CutData(offset, offset + string_size * 2));
  159. offset += string_size * 2;
  160. }
  161. }
  162. return arg_strings;
  163. }
  164. BinaryData TextSubfile::BuildPieces(const BinaryData &data, const SubfileData &new_data, long long &offset) {
  165. // Moving &offset pointer in &data
  166. GetPieceData(data, offset);
  167. // Deleting '[' and ']' brackets
  168. std::u16string text_data = new_data.text_data.substr(1, new_data.text_data.size() - 2);
  169. std::vector<std::u16string> pieces;
  170. const std::u16string DNT = u"<--DO_NOT_TOUCH!-->";
  171. size_t prev = 0;
  172. size_t next = text_data.find(DNT, prev);
  173. while (next != std::string::npos) {
  174. std::u16string piece = text_data.substr(prev, next - prev);
  175. pieces.push_back(piece);
  176. prev = next + DNT.length();
  177. next = text_data.find(DNT, prev);
  178. }
  179. std::u16string piece = text_data.substr(prev);
  180. pieces.push_back(piece);
  181. // Building BinaryData from pieces
  182. BinaryData result;
  183. BinaryData temp_data(4);
  184. temp_data.FromNumber<4>(pieces.size());
  185. result = result + temp_data;
  186. for (long long i = 0; i < pieces.size(); i++) {
  187. long long piece_size = pieces[i].length();
  188. if (piece_size < 128) {
  189. temp_data.FromNumber<1>(piece_size);
  190. } else {
  191. temp_data.FromNumberRAW<2>((piece_size | 32768));
  192. }
  193. result = result + temp_data;
  194. for (long long j = 0; j < piece_size; j++) {
  195. temp_data.FromNumber<2>(short(pieces[i][j]));
  196. result = result + temp_data;
  197. }
  198. }
  199. return result;
  200. }
  201. BinaryData TextSubfile::BuildArgumentReferences(const BinaryData &data, const SubfileData &new_data,
  202. long long &offset) {
  203. // Moving &offset pointer in &data
  204. GetArgumentReferenceData(data, offset);
  205. // If there are no args - making 4 null-bytes and return;
  206. if (!new_data.options["args"]) {
  207. BinaryData result;
  208. result.FromNumber<4>(0);
  209. return result;
  210. }
  211. // Parsing arguments from list in options["args"]
  212. std::string args_list = new_data.options["args"].as<std::string>();
  213. std::vector<long long> arguments;
  214. size_t prev = 0;
  215. size_t next = args_list.find('-', prev);
  216. while (next != std::string::npos) {
  217. std::string argument = args_list.substr(prev, next - prev);
  218. arguments.push_back(std::stoll(argument));
  219. prev = next + 1;
  220. next = args_list.find('-', prev);
  221. }
  222. std::string argument = args_list.substr(prev);
  223. arguments.push_back(std::stoll(argument));
  224. BinaryData result;
  225. BinaryData temp_data(4);
  226. temp_data.FromNumber<4>(arguments.size());
  227. result = result + temp_data;
  228. for (auto arg_reference : arguments) {
  229. temp_data.FromNumber<4>(arg_reference);
  230. result = result + temp_data;
  231. }
  232. return result;
  233. }
  234. BinaryData TextSubfile::BuildArgumentStrings(const BinaryData &data, const SubfileData &new_data,
  235. long long &offset) {
  236. return GetArgumentStringsData(data, offset);
  237. }
  238. BinaryData TextSubfile::GetPieceData(const BinaryData &data, long long &offset) const {
  239. long long old_offset = offset;
  240. long long num_pieces = data.ToNumber<4>(offset);
  241. offset += 4;
  242. for (long long j = 0; j < num_pieces; j++) {
  243. long long piece_size = data.ToNumber<1>(offset);
  244. if ((piece_size & 128) != 0) {
  245. piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
  246. offset += 1;
  247. }
  248. offset += 1;
  249. offset += piece_size * 2;
  250. }
  251. return data.CutData(old_offset, offset);
  252. }
  253. BinaryData TextSubfile::GetArgumentReferenceData(const BinaryData &data, long long &offset) const {
  254. long long old_offset = offset;
  255. long long num_references = data.ToNumber<4>(offset);
  256. offset += 4;
  257. offset += 4 * num_references;
  258. return data.CutData(old_offset, offset);
  259. }
  260. BinaryData TextSubfile::GetArgumentStringsData(const BinaryData &data, long long &offset) const {
  261. long long old_offset = offset;
  262. long long num_arg_strings = data.ToNumber<1>(offset);
  263. offset += 1;
  264. for (long long j = 0; j < num_arg_strings; j++) {
  265. long long num_args = data.ToNumber<4>(offset);
  266. offset += 4;
  267. for (long long k = 0; k < num_args; k++) {
  268. long long string_size = data.ToNumber<1>(offset);
  269. if ((string_size & 0x80) != 0) {
  270. string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
  271. offset += 1;
  272. }
  273. offset += 1;
  274. offset += string_size * 2;
  275. }
  276. }
  277. return data.CutData(old_offset, offset);
  278. }
  279. };