123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- //
- // Created by Иван_Архипов on 24.11.2017.
- //
- #include "TextSubfile.h"
- #include "../BinaryData.h"
- #include "../DatFile.h"
- #include "../Common/DatException.h"
- #include "../SubfileData.h"
- #include <algorithm>
- std::u16string to_utf16(long long x)
- {
- std::u16string res;
- while (x > 0) {
- res += u'0' + short(x % 10);
- x /= 10ll;
- }
- std::reverse(res.begin(), res.end());
- return res;
- }
- namespace LOTRO_DAT {
- TextSubfile::TextSubfile() = default;
- TextSubfile::TextSubfile(DatFile *dat, long long dictionary_offset, long long fragments_count, long long unknown1,
- long long file_id, long long file_offset, long long file_size,
- long long timestamp,
- long long version, long long block_size)
- : Subfile(dat, dictionary_offset, fragments_count, unknown1, file_id, file_offset, file_size, timestamp, version, block_size) {
- }
- FILE_TYPE TextSubfile::FileType() const {
- return TEXT;
- }
- std::string TextSubfile::Extension() const {
- return std::string(".txt");
- }
- SubfileData TextSubfile::PrepareForExport(const BinaryData &file_data) {
- SubfileData result;
- if (file_size() <= 10) // File is empty, nothing to do;
- return result;
- long long offset = 9; // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
- long long text_fragment_num = file_data.ToNumber<1>(offset);
- if ((text_fragment_num & 0x80) != 0) {
- text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | file_data.ToNumber<1>(offset + 1));
- offset += 1;
- }
- offset += 1;
- for (long long i = 0; i < text_fragment_num; i++) {
- long long fragment_id = file_data.ToNumber<8>(offset);
- offset += 8;
- std::vector<std::u16string> text_pieces = MakePieces(file_data, offset);
- std::vector<long long> arg_references = MakeArgumentReferences(file_data, offset);
- std::vector<std::vector<BinaryData>> arg_strings = MakeArgumentStrings(file_data, offset);
- std::u16string text = u"[";
- for (size_t j = 0; j + 1 < text_pieces.size(); j++)
- text += text_pieces[j] + u"<--DO_NOT_TOUCH!-->";
- text += text_pieces[text_pieces.size() - 1] + u"]";
- std::u16string arguments;
- for (size_t j = 0; j + 1 < arg_references.size(); j++)
- arguments += to_utf16(arg_references[j]) + u"-";
- if (!arg_references.empty())
- arguments += to_utf16(arg_references[arg_references.size() - 1]);
- if (result.text_data.length() > 0)
- result.text_data += u"|||";
- result.text_data += to_utf16(fragment_id) + u":::";
- result.text_data += arguments + u":::";
- result.text_data += text;
- }
- result.options["fid"] = file_id();
- result.options["ext"] = Extension();
- return result;
- }
- BinaryData TextSubfile::MakeForImport(const BinaryData &old_data, const SubfileData &data) {
- BinaryData new_data;
- // Creating map for fast access to patch fragment by it's id
- std::unordered_map<long long, long long> fragment_vector_dictionary;
- std::cerr << "Fragments for " << file_id() << std::endl;
- for (int i = 0; i < FileFragments.size(); i++)
- fragment_vector_dictionary[FileFragments[i].options["gid"].as<long long>()] = i;
- for (auto i : fragment_vector_dictionary)
- std::cerr << i.first << " ";
- std::cerr << std::endl;
- if (file_size() <= 10 + 8) // File is empty, nothing to do;
- return old_data;
- long long offset = 9 + 8; // first 8 bytes - file_info. After them:
- // first 4 bytes - file_id, then 4 bytes - unknown, then 1 byte - unknown
- long long text_fragment_num = old_data.ToNumber<1>(offset);
- if ((text_fragment_num & 0x80) != 0) {
- text_fragment_num = (((text_fragment_num ^ 0x80) << 8) | old_data.ToNumber<1>(offset + 1));
- offset += 1;
- }
- offset += 1;
- new_data = new_data + old_data.CutData(0, offset); // Adding file info
- for (long long i = 0; i < text_fragment_num; i++) {
- long long fragment_id = old_data.ToNumber<8>(offset);
- offset += 8;
- new_data = new_data + old_data.CutData(offset - 8, offset);
- if (fragment_vector_dictionary.count(fragment_id) == 0) {
- std::cerr << "Omg, what..? " << file_id() << " " << fragment_id << " is not in patch? " << std::endl;
- new_data = new_data + GetPieceData(old_data, offset); // Retrieving old pieces
- new_data = new_data + GetArgumentReferenceData(old_data, offset); // Retrieving old references
- new_data = new_data + GetArgumentStringsData(old_data, offset); // Retrieving old ref_strings
- } else {
- SubfileData fragment_data = FileFragments[fragment_vector_dictionary[fragment_id]];
- new_data = new_data + BuildPieces(old_data, fragment_data, offset); // Making and adding new pieces
- new_data = new_data + BuildArgumentReferences(old_data, fragment_data, offset); // Making and adding new references
- new_data = new_data + BuildArgumentStrings(old_data, fragment_data, offset); // Making and adding new strings
- }
- }
- new_data = new_data + old_data.CutData(offset); // Adding elapsed file data
- return new_data;
- }
- std::vector<std::u16string> TextSubfile::MakePieces(const BinaryData &data, long long &offset) {
- long long num_pieces = data.ToNumber<4>(offset);
- offset += 4;
- std::vector<std::u16string> text_pieces;
- for (long long j = 0; j < num_pieces; j++) {
- long long piece_size = data.ToNumber<1>(offset);
- if ((piece_size & 128) != 0) {
- piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
- offset += 1;
- }
- offset += 1;
- BinaryData piece_data = data.CutData(offset, offset + piece_size * 2);
- std::u16string piece;
- for (long long k = 0; k < piece_size; k++) {
- char16_t c = char16_t(
- ((short(piece_data[2 * unsigned(k) + 1])) << 8) | (short(piece_data[2 * unsigned(k)])));
- piece += c;
- }
- text_pieces.push_back(piece);
- offset += piece_size * 2;
- }
- return text_pieces;
- }
- std::vector<long long> TextSubfile::MakeArgumentReferences(const BinaryData &data, long long &offset) {
- std::vector<long long> arg_references;
- long long num_references = data.ToNumber<4>(offset);
- offset += 4;
- for (long long j = 0; j < num_references; j++) {
- arg_references.emplace_back(data.ToNumber<4>(offset));
- offset += 4;
- }
- return arg_references;
- }
- std::vector<std::vector<BinaryData>> TextSubfile::MakeArgumentStrings(const BinaryData &data, long long &offset) {
- std::vector<std::vector<BinaryData> > arg_strings;
- long long num_arg_strings = data.ToNumber<1>(offset);
- offset += 1;
- for (long long j = 0; j < num_arg_strings; j++) {
- long long num_args = data.ToNumber<4>(offset);
- offset += 4;
- arg_strings.emplace_back();
- for (long long k = 0; k < num_args; k++) {
- long long string_size = data.ToNumber<1>(offset);
- if ((string_size & 0x80) != 0) {
- string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
- offset += 1;
- }
- offset += 1;
- arg_strings[unsigned(j)].emplace_back(data.CutData(offset, offset + string_size * 2));
- offset += string_size * 2;
- }
- }
- return arg_strings;
- }
- BinaryData TextSubfile::BuildPieces(const BinaryData &data, const SubfileData &new_data, long long &offset) {
- // Moving &offset pointer in &data
- GetPieceData(data, offset);
- // Deleting '[' and ']' brackets
- std::u16string text_data = new_data.text_data.substr(1, new_data.text_data.size() - 2);
- std::vector<std::u16string> pieces;
- const std::u16string DNT = u"<--DO_NOT_TOUCH!-->";
- size_t prev = 0;
- size_t next = text_data.find(DNT, prev);
- while (next != std::string::npos) {
- std::u16string piece = text_data.substr(prev, next - prev);
- pieces.push_back(piece);
- prev = next + DNT.length();
- next = text_data.find(DNT, prev);
- }
- std::u16string piece = text_data.substr(prev);
- pieces.push_back(piece);
- // Building BinaryData from pieces
- BinaryData result;
- BinaryData temp_data(4);
- temp_data.FromNumber<4>(pieces.size());
- result = result + temp_data;
- for (long long i = 0; i < pieces.size(); i++) {
- long long piece_size = pieces[i].length();
- if (piece_size < 128) {
- temp_data.FromNumber<1>(piece_size);
- } else {
- temp_data.FromNumberRAW<2>((piece_size | 32768));
- }
- result = result + temp_data;
- for (long long j = 0; j < piece_size; j++) {
- temp_data.FromNumber<2>(short(pieces[i][j]));
- result = result + temp_data;
- }
- }
- return result;
- }
- BinaryData TextSubfile::BuildArgumentReferences(const BinaryData &data, const SubfileData &new_data,
- long long &offset) {
- // Moving &offset pointer in &data
- GetArgumentReferenceData(data, offset);
- // If there are no args - making 4 null-bytes and return;
- if (!new_data.options["args"]) {
- BinaryData result;
- result.FromNumber<4>(0);
- return result;
- }
- // Parsing arguments from list in options["args"]
- std::string args_list = new_data.options["args"].as<std::string>();
- std::vector<long long> arguments;
- size_t prev = 0;
- size_t next = args_list.find('-', prev);
- while (next != std::string::npos) {
- std::string argument = args_list.substr(prev, next - prev);
- arguments.push_back(std::stoll(argument));
- prev = next + 1;
- next = args_list.find('-', prev);
- }
- std::string argument = args_list.substr(prev);
- arguments.push_back(std::stoll(argument));
- BinaryData result;
- BinaryData temp_data(4);
- temp_data.FromNumber<4>(arguments.size());
- result = result + temp_data;
- for (auto arg_reference : arguments) {
- temp_data.FromNumber<4>(arg_reference);
- result = result + temp_data;
- }
- return result;
- }
- BinaryData TextSubfile::BuildArgumentStrings(const BinaryData &data, const SubfileData &new_data,
- long long &offset) {
- return GetArgumentStringsData(data, offset);
- }
- BinaryData TextSubfile::GetPieceData(const BinaryData &data, long long &offset) const {
- long long old_offset = offset;
- long long num_pieces = data.ToNumber<4>(offset);
- offset += 4;
- for (long long j = 0; j < num_pieces; j++) {
- long long piece_size = data.ToNumber<1>(offset);
- if ((piece_size & 128) != 0) {
- piece_size = (((piece_size ^ 128) << 8) | data.ToNumber<1>(offset + 1));
- offset += 1;
- }
- offset += 1;
- offset += piece_size * 2;
- }
- return data.CutData(old_offset, offset);
- }
- BinaryData TextSubfile::GetArgumentReferenceData(const BinaryData &data, long long &offset) const {
- long long old_offset = offset;
- long long num_references = data.ToNumber<4>(offset);
- offset += 4;
- offset += 4 * num_references;
- return data.CutData(old_offset, offset);
- }
- BinaryData TextSubfile::GetArgumentStringsData(const BinaryData &data, long long &offset) const {
- long long old_offset = offset;
- long long num_arg_strings = data.ToNumber<1>(offset);
- offset += 1;
- for (long long j = 0; j < num_arg_strings; j++) {
- long long num_args = data.ToNumber<4>(offset);
- offset += 4;
- for (long long k = 0; k < num_args; k++) {
- long long string_size = data.ToNumber<1>(offset);
- if ((string_size & 0x80) != 0) {
- string_size = (((string_size ^ 0x80) << 8) | data.ToNumber<1>(offset + 1));
- offset += 1;
- }
- offset += 1;
- offset += string_size * 2;
- }
- }
- return data.CutData(old_offset, offset);
- }
- };
|