DatFile.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. //
  2. // Created by Иван_Архипов on 31.10.2017.
  3. //
  4. #include "../include/DatFile.h"
  5. #include "../include/BinaryData.h"
  6. #include "../include/DatException.h"
  7. #include "../include/SubDirectory.h"
  8. #include "../include/Subfile.h"
  9. #include "../include/SubfileData.h"
  10. #include <locale>
  11. #include <algorithm>
  12. extern "C++"
  13. {
  14. namespace LOTRO_DAT {
  15. DatFile::DatFile() : dat_state_(CLOSED) {}
  16. DatFile::DatFile(const char *filename, int dat_id) : dat_id_(dat_id) , dat_state_(CLOSED) {
  17. filename_ = std::string(filename);
  18. OpenDatFile(filename);
  19. ReadSuperBlock();
  20. MakeDirectories();
  21. try {
  22. MakeDictionary();
  23. } catch (std::exception &e) {
  24. fprintf(stderr, "Caught %s exception.", e.what());
  25. fprintf(stderr, "Unable to make dictionary!! Unable to init DatFile!!!");
  26. return;
  27. }
  28. InitLocale(PATCHED, (std::string(filename) + std::string("patched.dbgm")).c_str());
  29. InitLocale(ORIGINAL,(std::string(filename) + std::string("original.dbgm")).c_str());
  30. FILE *locale = fopen((std::string(filename) + ".dbgm").c_str(), "r");
  31. if (locale == nullptr)
  32. current_locale_ = ORIGINAL;
  33. else {
  34. auto loc = new char[10];
  35. fscanf(locale, "%s", loc);
  36. if (std::string(loc) == "RU")
  37. current_locale_ = PATCHED;
  38. if (std::string(loc) == "EN")
  39. current_locale_ = ORIGINAL;
  40. }
  41. if (dat_state_ == SUCCESS_DICTIONARY)
  42. dat_state_ = READY;
  43. else
  44. throw DatException("Bad DatFile initialization! Not all init states were successfully passed!",
  45. INIT_EXCEPTION);
  46. }
  47. DatFile::~DatFile() {
  48. CommitChanges();
  49. CommitLocales();
  50. if (file_handler_ != nullptr)
  51. fclose(file_handler_);
  52. delete file_handler_;
  53. delete root_directory_;
  54. }
  55. /// Extracts file with file_id.
  56. /// If path is undefined then it will be recognised as current working directory
  57. /// Output file path consists of "path + file_id + file_extension";
  58. /// NOTICE: The directory, mentioned in "std::string path" variable SHOULD BE ALREADY CREATED;
  59. /// Otherwise DatException() will be thrown.
  60. /// Returns true, if file was successfully extracted;
  61. /// Throws DatException() if undefined behaviour happened
  62. bool DatFile::ExtractFile(long long file_id, const std::string &path) {
  63. if (dat_state_ < READY) {
  64. throw DatException("Bad DatFile::ExtractFile() - invalid DatFile state!", EXPORT_EXCEPTION);
  65. }
  66. BinaryData file_data;
  67. try {
  68. file_data = GetFileData(dictionary_[file_id], 8);
  69. } catch (std::exception &e) {
  70. fprintf(stderr, "Caught %s exception.", e.what());
  71. fprintf(stderr, "Unable to extract file due to uncaught exception while getting file data. Passing...\n");
  72. return false;
  73. }
  74. try {
  75. SubfileData export_data = dictionary_[file_id]->PrepareForExport(file_data);
  76. export_data.binary_data.WriteToFile(path + export_data.options["ext"].as<std::string>());
  77. } catch (std::exception &e) {
  78. fprintf(stderr, "Caught %s exception.", e.what());
  79. fprintf(stderr, "Unable to extract file due to uncaught exception while preparing file for export. Passing...\n");
  80. return false;
  81. }
  82. return true;
  83. }
  84. /// Extracts file with file_id to database "db".
  85. /// DATABASE SHOULD BE ALREADY CREATED; Otherwise DatException will be called.
  86. /// NOTICE: The directory, mentioned in "std::string path" variable SHOULD BE ALREADY CREATED;
  87. /// Otherwise DatException() will be thrown.
  88. /// Returns true, if file was successfully extracted;
  89. /// Throws DatException() if undefined behaviour happened
  90. bool DatFile::ExtractFile(long long file_id, Database *db) {
  91. if (dat_state_ < READY) {
  92. throw DatException("Bad DatFile::ExtractFile() - invalid DatFile state!", EXPORT_EXCEPTION);
  93. }
  94. BinaryData file_data;
  95. try {
  96. file_data = GetFileData(dictionary_[file_id], 8);
  97. } catch (std::exception &e) {
  98. fprintf(stderr, "Caught %s exception.", e.what());
  99. fprintf(stderr, "Unable to extract file due to uncaught exception while getting file data. Passing...\n");
  100. return false;
  101. }
  102. SubfileData export_data;
  103. try {
  104. export_data = dictionary_[file_id]->PrepareForExport(file_data);
  105. export_data.options["did"] = dat_id_;
  106. } catch (std::exception &e) {
  107. fprintf(stderr, "Caught %s exception.", e.what());
  108. fprintf(stderr, "Unable to extract file due to uncaught exception while preparing file for export. Passing...\n");
  109. return false;
  110. }
  111. if (export_data == SubfileData()) {
  112. fprintf(stderr, "WARNING: file with id %lld is empty. Passing it\n", dictionary_[file_id]->file_id());
  113. return true;
  114. }
  115. try {
  116. db->PushFile(export_data);
  117. } catch (std::exception &e) {
  118. fprintf(stderr, "Caught %s exception.", e.what());
  119. printf("Caught %s exception.", e.what());
  120. fflush(stdout);
  121. fprintf(stderr, "Unable to put file or it's part to database. Continuing without this part. Database may be not complete\n");
  122. }
  123. return true;
  124. }
  125. /// Extracts all files with specific type to "path + type + file_id + file_part + extension" files;
  126. /// If path is undefined then it will be recognised as current working directory
  127. /// NOTICE: The directory, mentioned in "std::string path" variable SHOULD BE ALREADY CREATED;
  128. /// Otherwise DatException() will be thrown.
  129. /// Returns number of successfully extracted files
  130. /// Throws DatException() if undefined behaviour happened
  131. int DatFile::ExtractAllFilesByType(FILE_TYPE type, std::string path) {
  132. if (dat_state_ < READY) {
  133. throw DatException("Bad DatFile::ExtractAllFilesByType() - invalid DatFile state!", EXPORT_EXCEPTION);
  134. }
  135. int success = 0;
  136. for (auto i : dictionary_) {
  137. FILE_TYPE file_type = i.second->FileType();
  138. if (file_type == type) {
  139. success += ExtractFile(i.second->file_id(), (path + std::to_string(i.second->file_id())));
  140. }
  141. }
  142. return success;
  143. }
  144. /// Extracts all files with specific type to database "db";
  145. /// DATABASE SHOULD BE ALREADY CREATED; Otherwise DatException will be called.
  146. /// Returns number of successfully extracted files
  147. /// Throws DatException() if undefined behaviour happened
  148. int DatFile::ExtractAllFilesByType(FILE_TYPE type, Database *db) {
  149. if (dat_state_ < READY) {
  150. throw DatException("Bad DatFile::ExtractAllFilesByType() - invalid DatFile state!", EXPORT_EXCEPTION);
  151. }
  152. int success = 0;
  153. for (auto i : dictionary_) {
  154. FILE_TYPE file_type = i.second->FileType();
  155. if (file_type == type) {
  156. success += ExtractFile(i.second->file_id(), db);
  157. }
  158. }
  159. return success;
  160. }
  161. // TODO: Write description and make asserts
  162. bool DatFile::PatchFile(const char *filename, YAML::Node options) {
  163. if (dat_state_ < READY) {
  164. throw DatException("Bad DatFile::PatchFile() - invalid DatFile state!", EXPORT_EXCEPTION);
  165. }
  166. if (options["did"].IsDefined() && options["did"].as<int>() != dat_id_)
  167. return false;
  168. BinaryData data;
  169. data.ReadFromFile(filename);
  170. auto file_id = options["fid"].as<long long>();
  171. if (dictionary_[file_id] == nullptr) {
  172. fprintf(stderr, "ERROR DatFile::PatchFile() - Cannot patch file - there is no file in dictionary with file_id = %lld.\n", file_id);
  173. return false;
  174. }
  175. BinaryData old_data = GetFileData(dictionary_[file_id]);
  176. data = dictionary_[file_id]->MakeForImport(old_data, SubfileData(data, u"", options));
  177. try {
  178. ApplyFilePatch(dictionary_[file_id], data);
  179. } catch (std::exception &e) {
  180. fprintf(stderr, "Caught %s exception.", e.what());
  181. fprintf(stderr,
  182. "Some errors happened while patching file with id = %lld. Continuing process without this file..\n"
  183. "WARNING: DAT FILE CAN BE CORRUPTED!\n", file_id);
  184. printf("Some errors happened while patching file with id = %lld. Continuing process without this file..\n"
  185. "WARNING: DAT FILE CAN BE CORRUPTED!\n", file_id);
  186. fflush(stdout);
  187. return false;
  188. }
  189. return true;
  190. }
  191. // TODO: Write description and make asserts
  192. bool DatFile::PatchFile(const SubfileData &data) {
  193. if (dat_state_ < READY) {
  194. throw DatException("Bad DatFile::PatchFile() - invalid DatFile state!", EXPORT_EXCEPTION);
  195. }
  196. auto file_id = data.options["fid"].as<long long>();
  197. Subfile *file = dictionary_[file_id];
  198. if (file == nullptr) {
  199. fprintf(stderr, "ERROR DatFile::PatchFile() - Cannot patch file - there is no file in dictionary with file_id = %lld.\n", file_id);
  200. return false;
  201. }
  202. BinaryData old_data = GetFileData(file);
  203. BinaryData patch_data = file->MakeForImport(old_data, data);
  204. ApplyFilePatch(dictionary_[file_id], patch_data);
  205. return true;
  206. }
  207. // TODO: Write description
  208. bool DatFile::PatchAllDatabase(Database *db) {
  209. if (dat_state_ < READY) {
  210. throw DatException("Bad DatFile::PatchAllDatabase() - invalid DatFile state!", EXPORT_EXCEPTION);
  211. }
  212. SubfileData data;
  213. try {
  214. data = db->GetNextFile();
  215. } catch (std::exception &e) {
  216. fprintf(stderr, "Caught %s exception.\n", e.what());
  217. fprintf(stderr, "DatFile::PatchAllDatabase() error! Caught exception while fetching file from database! Stopping...\n");
  218. return false;
  219. }
  220. while (data != SubfileData()) {
  221. try {
  222. PatchFile(data);
  223. } catch (std::exception &e) {
  224. fprintf(stderr, "Caught %s exception.\n", e.what());
  225. fprintf(stderr, "DatFile::PatchAllDatabase() error! Caught exception while patching file! Passing...\n");
  226. }
  227. try {
  228. data = db->GetNextFile();
  229. } catch (std::exception &e) {
  230. fprintf(stderr, "Caught %s exception.\n", e.what());
  231. fprintf(stderr, "DatFile::PatchAllDatabase() error! Caught exception while fetching file from database! Stopping...\n");
  232. return false;
  233. }
  234. }
  235. CommitChanges();
  236. CommitLocales();
  237. return true;
  238. }
  239. /// DatFile::WriteUnorderedDictionary(...);
  240. /// Prints list of all found files with some information about them to file.
  241. /// Gets std::string path - path to directory, where the file will be written with name "dict.txt"
  242. void DatFile::WriteUnorderedDictionary(std::string path) const {
  243. FILE *f;
  244. fopen_s(&f, (path + "dict.txt").c_str(), "w");
  245. fprintf(f, "file_id offset size size2 extension\n");
  246. for (auto i : dictionary_) {
  247. fprintf(f, "%lld %lld %lld %lld %s\n", i.second->file_id(), i.second->file_offset(), i.second->file_size(),
  248. i.second->block_size(), i.second->Extension().c_str());
  249. }
  250. fclose(f);
  251. }
  252. /// DatFile::files_number();
  253. /// Returns amount of files, found in dictionaries of DatFile. Some if them may be empty or erased.
  254. long long DatFile::files_number() const {
  255. return dictionary_.size();
  256. }
  257. /// DatFile::GetFileData()
  258. /// Returns BinaryData, which contains of subfile data, made from parts of file in DatFile
  259. BinaryData DatFile::GetFileData(const Subfile *file, long long int offset) {
  260. BinaryData mfile_id(4);
  261. ReadData(mfile_id, 4, file->file_offset() + 8);
  262. if (file->file_id() != mfile_id.ToNumber<4>(0))
  263. throw DatException("Bad DatFile::GetFileData() - file_id in Subfile doesn't match to file_id in DatFile.", READ_EXCEPTION);
  264. BinaryData data((unsigned)(file->file_size() + (8 - offset)));
  265. if (file->block_size() >= file->file_size() + 8) {
  266. ReadData(data, file->file_size() + (8 - offset), file->file_offset() + offset);
  267. return data;
  268. }
  269. BinaryData fragments_count(4);
  270. ReadData(fragments_count, 4, file->file_offset());
  271. long long fragments_number = fragments_count.ToNumber<4>(0);
  272. long long current_block_size = file->block_size() - offset - 8 * fragments_number;
  273. ReadData(data, current_block_size , file->file_offset() + offset);
  274. BinaryData FragmentsDictionary(8 * unsigned(fragments_number));
  275. ReadData(FragmentsDictionary, 8 * unsigned(fragments_number), file->file_offset() + file->block_size() - 8 * fragments_number);
  276. for (long long i = 0; i < fragments_number; i++) {
  277. long long fragment_size = FragmentsDictionary.ToNumber<4>(8 * i);
  278. long long fragment_offset = FragmentsDictionary.ToNumber<4>(8 * i + 4);
  279. ReadData(data, std::min(fragment_size, file->file_size() - current_block_size), fragment_offset, current_block_size );
  280. current_block_size += fragment_size;
  281. }
  282. return data;
  283. }
  284. /// DatFile special functions for opening and reading/writing raw data.
  285. /// Shouldn't be used by any external classes except Subfile and Subdirectory.
  286. void DatFile::OpenDatFile(const char *dat_name) {
  287. if (dat_state_ != CLOSED)
  288. throw DatException("Bad initialisation of DatFile - current DatFile isn't in correct state!",
  289. INIT_EXCEPTION);
  290. fopen_s(&file_handler_, dat_name, "r+b");
  291. if (file_handler_ == nullptr) {
  292. std::string err = "Bad DatFile::OpenDatFile. Unable to open file ";
  293. err += dat_name;
  294. throw DatException(err.c_str(), NOFILE_EXCEPTION);
  295. }
  296. fseek(file_handler_, 0, SEEK_END);
  297. file_size_ = ftell(file_handler_);
  298. fseek(file_handler_, 0, SEEK_SET);
  299. dat_state_ = SUCCESS_OPENED;
  300. }
  301. void DatFile::ReadSuperBlock() {
  302. if (dat_state_ != SUCCESS_OPENED)
  303. throw DatException("Bad DatFile::ReadSuperBlock() - DatFile isn't in valid state!", INIT_EXCEPTION);
  304. BinaryData data(1024);
  305. ReadData(data, 1024);
  306. constant1_ = data.ToNumber<4>(0x100);
  307. constant2_ = data.ToNumber<4>(0x140);
  308. version1_ = data.ToNumber<4>(0x14C);
  309. version2_ = data.ToNumber<4>(0x150);
  310. fragmentation_journal_offset_ = data.ToNumber<4>(0x154);
  311. root_directory_offset_ = data.ToNumber<4>(0x160);
  312. auto size1 = data.ToNumber<4>(0x148);
  313. if (constant1_ != 0x4C5000)
  314. throw DatException(
  315. "Bad DatFile::ReadSuperBlock - variable at position 0x100 is not equal to .dat file constant!",
  316. INIT_EXCEPTION);
  317. if (constant2_ != 0x5442)
  318. throw DatException(
  319. "Bad DatFile::ReadSuperBlock - variable at position 0x140 is not equal to .dat file constant!",
  320. INIT_EXCEPTION);
  321. if (file_size_ != size1)
  322. throw DatException(
  323. "Bad DatFile::ReadSuperBlock - variable at 0x148 position is not equal to .dat file size!",
  324. INIT_EXCEPTION);
  325. dat_state_ = SUCCESS_SUPERBLOCK;
  326. }
  327. void DatFile::MakeDirectories() {
  328. if (dat_state_ != SUCCESS_SUPERBLOCK)
  329. throw DatException("Bad DatFile::MakeDirectories() - DatFile isn't in valid state!", INIT_EXCEPTION);
  330. root_directory_ = new SubDirectory((unsigned) root_directory_offset_, this);
  331. dat_state_ = SUCCESS_DIRECTORIES;
  332. }
  333. void DatFile::MakeDictionary() {
  334. if (dat_state_ != SUCCESS_DIRECTORIES)
  335. throw DatException("Bad DatFile::MakeDictionary() - DatFile isn't in valid state!", INIT_EXCEPTION);
  336. try {
  337. root_directory_->MakeDictionary(dictionary_);
  338. } catch (std::exception &e) {
  339. fprintf(stderr, "Caught %s exception.", e.what());
  340. fprintf(stderr, "Bad DatFile::MakeDictionary() - File is corrupted?\n");
  341. return;
  342. }
  343. dat_state_ = SUCCESS_DICTIONARY;
  344. }
  345. void DatFile::ReadData(BinaryData &data, long long size, long long offset, long long data_offset) {
  346. if (dat_state_ == CLOSED)
  347. throw DatException("Bad DatFile::ReadData() - DatFile isn't in valid state!", READ_EXCEPTION);
  348. if (data_offset + size > data.size()) {
  349. std::string err = "Bad DatFile::ReadData - trying to read more than BinaryData size\n";
  350. err += std::string("Reading ") + std::to_string(size) + std::string(" bytes from ")
  351. + std::to_string(offset) + std::string(" position in dat file.");
  352. throw DatException(err.c_str(), READ_EXCEPTION);
  353. }
  354. if (offset + size > file_size_) {
  355. std::string err = "Bad DatFile::ReadData - trying to read more than DatFile size elapsed\n";
  356. err += std::string("Reading ") + std::to_string(size) + std::string(" bytes from ")
  357. + std::to_string(offset) + std::string(" position in dat file.");
  358. throw DatException(err.c_str(), READ_EXCEPTION);
  359. }
  360. _fseeki64(file_handler_, offset, SEEK_SET);
  361. fread(data.data() + data_offset, unsigned(size), 1, file_handler_);
  362. data.CheckCompression();
  363. }
  364. void DatFile::WriteData(const BinaryData &data, long long size, long long offset, long long data_offset) {
  365. if (dat_state_ < READY)
  366. throw DatException("Bad DatFile::WriteData() - DatFile isn't in valid state!", WRITE_EXCEPTION);
  367. _fseeki64(file_handler_, offset, SEEK_SET);
  368. if (data_offset + size > data.size())
  369. throw DatException("Bad DatFile::WriteData - trying to write more than BinaryData size", WRITE_EXCEPTION);
  370. fwrite(data.data() + data_offset, unsigned(size), 1, file_handler_);
  371. }
  372. /// Special functions used by patch process.
  373. /// Shouldn't be used by any external class.
  374. void DatFile::ApplyFilePatch(Subfile *file, const BinaryData &data) {
  375. if (patched_list.count(file->file_id()) != 0) {
  376. fprintf(stderr, "Warning: DatFile::ApplyFilePatch - found 2 files in patch with the same file_id. Passing last...\n");
  377. return;
  378. }
  379. if (current_locale() != PATCHED) {
  380. std::cout << "Changing locale to RU in order to patch file" << std::endl;
  381. SetLocale(PATCHED);
  382. }
  383. dat_state_ = UPDATED;
  384. auto journal = GetFragmentationJournal();
  385. if (journal[0].second != file_size_) {
  386. journal[0].second = file_size_;
  387. }
  388. file->file_size_ = data.size() - 8;
  389. if (patch_dict_.count(file->file_id()) == 0 || data.size() > file->block_size()) {
  390. file->file_offset_ = journal[0].second;
  391. file->block_size_ = std::max(data.size(), 256u);
  392. journal[0].second += data.size();
  393. BinaryData nulls(data.size());
  394. WriteData(nulls, nulls.size(), file_size_);
  395. this->file_size_ += data.size();
  396. }
  397. BinaryData fragments_count(4);
  398. fragments_count.FromNumber<4>(0);
  399. BinaryData file_data = fragments_count + data.CutData(4);
  400. if (file->file_id() != file_data.ToNumber<4>(8))
  401. throw DatException("Bad DatFile::ApplyFilePatch() - Created data's file_id doesn't match to original! "
  402. "Patch wasn't written to .dat file");
  403. WriteData(file_data, file_data.size(), file->file_offset());
  404. auto file_id = file->file_id();
  405. patched_list.insert(file_id);
  406. delete patch_dict_[file_id]; // Удалили старое значение в русском словаре
  407. patch_dict_[file_id] = new Subfile(this, file->MakeHeaderData()); // Создали новое значение
  408. UpdateFragmentationJournal(journal);
  409. }
  410. void DatFile::UpdateSubdirectories() {
  411. root_directory_->UpdateDirectories(patched_list, dictionary_);
  412. }
  413. std::vector<std::pair<long long, long long> > DatFile::GetFragmentationJournal() {
  414. BinaryData data(8);
  415. ReadData(data, 8, fragmentation_journal_offset_ + 8);
  416. std::vector<std::pair<long long, long long> > result;
  417. result.emplace_back(std::make_pair(data.ToNumber<4>(0), data.ToNumber<4>(4)));
  418. return result;
  419. }
  420. void DatFile::UpdateHeader() {
  421. BinaryData data(4);
  422. data.FromNumber<4>(constant1_);
  423. WriteData(data, 4, 0x100);
  424. data.FromNumber<4>(constant2_);
  425. WriteData(data, 4, 0x140);
  426. data.FromNumber<4>(file_size_);
  427. WriteData(data, 4, 0x148);
  428. data.FromNumber<4>(version1_);
  429. WriteData(data, 4, 0x14C);
  430. data.FromNumber<4>(version2_);
  431. WriteData(data, 4, 0x150);
  432. data.FromNumber<4>(fragmentation_journal_offset_);
  433. WriteData(data, 4, 0x154);
  434. data.FromNumber<4>(root_directory_offset_);
  435. WriteData(data, 4, 0x160);
  436. }
  437. void DatFile::UpdateFragmentationJournal(const std::vector<std::pair<long long, long long> > &journal) {
  438. for (unsigned i = 0; i < journal.size(); i++) {
  439. long long size = journal[i].first;
  440. long long offset = journal[i].second;
  441. BinaryData data(4);
  442. data.FromNumber<4>(size);
  443. WriteData(data, 4, fragmentation_journal_offset_ + 8 * (i + 1));
  444. data.FromNumber<4>(offset);
  445. WriteData(data, 4, fragmentation_journal_offset_ + 8 * (i + 1) + 4);
  446. }
  447. }
  448. void DatFile::CommitChanges() {
  449. if (dat_state_ != UPDATED)
  450. return;
  451. std::cout << "There are some updated files. Rewriting dictionary..." << std::endl << std::flush;
  452. auto journal = GetFragmentationJournal();
  453. journal[0].second = file_size_;
  454. BinaryData nulls(size_t(journal[0].first));
  455. WriteData(nulls, nulls.size(), file_size_);
  456. file_size_ += journal[0].first;
  457. UpdateFragmentationJournal(journal);
  458. std::cout << "Updated fragmentation journal..." << std::endl << std::flush;
  459. UpdateHeader();
  460. std::cout << "Updated header..." << std::endl << std::flush;
  461. UpdateSubdirectories();
  462. std::cout << "Updated subdirectories..." << std::endl << std::flush;
  463. std::cout << "Changed " << patched_list.size() << " files..." << std::endl << std::flush;
  464. patched_list.clear();
  465. dat_state_ = READY;
  466. }
  467. // LOCALE MANAGING SECTION
  468. void DatFile::InitLocale(LOCALE locale, const char* filename) {
  469. auto dict = GetLocaleDictReference(locale);
  470. dict->clear();
  471. FILE *dict_file = fopen(filename, "rb");
  472. if (dict_file == nullptr) {
  473. if (locale == ORIGINAL) {
  474. for (auto file : dictionary_) {
  475. (*dict)[file.first] = new Subfile(this, file.second->MakeHeaderData());
  476. }
  477. }
  478. return;
  479. }
  480. size_t size;
  481. fread(&size, sizeof(size_t), 1, dict_file);
  482. std::cout << "There are " << size << " files in " << std::string(filename) << " dictionary...\n";
  483. for (size_t i = 0; i < size; i++) {
  484. BinaryData header(32);
  485. fread(header.data(), unsigned(header.size()), 1, dict_file);
  486. auto file = new Subfile(this, header);
  487. (*dict)[file->file_id()] = file;
  488. }
  489. fclose(dict_file);
  490. }
  491. std::unordered_map<long long, Subfile *> *DatFile::GetLocaleDictReference(LOCALE locale) {
  492. switch (locale) {
  493. case PATCHED:
  494. return &patch_dict_;
  495. case ORIGINAL:
  496. return &orig_dict_;
  497. default:
  498. throw DatException("Bad DatFile::GetLocaleDictReference() - unknown locale!!!", LOCALE_EXCEPTION);
  499. }
  500. }
  501. void DatFile::SetLocale(LOCALE locale) {
  502. if (current_locale_ == locale) {
  503. return;
  504. }
  505. dat_state_ = UPDATED;
  506. auto dict = GetLocaleDictReference(locale);
  507. for (auto file : *dict) {
  508. if (dictionary_[file.first] == nullptr) {
  509. fprintf(stderr, "WARNING: In locale dictionary there is file with file_id = %lld, which is not in .dat "
  510. "file! Passing it and removing from locale dictionary\n", file.first);
  511. dict->erase(file.first);
  512. continue;
  513. }
  514. if (dictionary_[file.first]->MakeHeaderData().CutData(8, 16) == file.second->MakeHeaderData().CutData(8, 16))
  515. continue;
  516. dictionary_[file.first]->file_offset_ = file.second->file_offset_;
  517. dictionary_[file.first]->file_size_ = file.second->file_size_;
  518. dictionary_[file.first]->block_size_= file.second->block_size_;
  519. dictionary_[file.first]->timestamp_ = file.second->timestamp_;
  520. dictionary_[file.first]->version_ = file.second->version_;
  521. dictionary_[file.first] = file.second;
  522. patched_list.insert(file.first);
  523. dat_state_ = UPDATED;
  524. }
  525. current_locale_ = locale;
  526. CommitChanges();
  527. CommitLocales();
  528. }
  529. void DatFile::SaveLocale(LOCALE locale, const char *filename) {
  530. auto dict = GetLocaleDictReference(locale);
  531. FILE *dict_file = fopen(filename, "wb");
  532. size_t count = size_t(dict->size());
  533. fwrite(&count, sizeof(size_t), 1, dict_file);
  534. for (auto file : *dict) {
  535. BinaryData header = file.second->MakeHeaderData();
  536. fwrite(header.data(), unsigned(header.size()), 1, dict_file);
  537. }
  538. fclose(dict_file);
  539. }
  540. LOCALE DatFile::current_locale() {
  541. return current_locale_;
  542. }
  543. void DatFile::CommitLocales() {
  544. std::cout << "Commiting locales..." << std::endl;
  545. std::cout << "Saving patched locale..." << std::endl;
  546. SaveLocale(PATCHED, (std::string(filename_) + std::string("patched.dbgm")).c_str());
  547. std::cout << "Saving original locale..." << std::endl;
  548. SaveLocale(ORIGINAL,(std::string(filename_) + std::string("original.dbgm")).c_str());
  549. std::cout << "Writing current locale" << std::endl;
  550. FILE *locale = fopen((std::string(filename_) + ".dbgm").c_str(), "w");
  551. if (current_locale_ == ORIGINAL)
  552. fprintf(locale, "EN");
  553. else
  554. fprintf(locale, "RU");
  555. fclose(locale);
  556. std::cout << "Done!" << std::endl;
  557. }
  558. }
  559. }