patcher_example.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // Created by Иван_Архипов on 23.11.2017.
  3. //
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <iostream>
  6. #include <ctime>
  7. #include <algorithm>
  8. #ifdef WIN32
  9. #include <direct.h>
  10. #define mkdir(dir, mode) _mkdir(dir)
  11. #endif
  12. #include "../../include/LotroDatPatcher.h"
  13. using namespace LOTRO_DAT;
  14. using namespace std;
  15. int main() {
  16. std::cout << "Gi1dor's LotRO .dat patcher ver. 2.0.0" << std::endl;
  17. freopen("errors.log", "w", stderr);
  18. // setbuf(stdout, NULL);
  19. setbuf(stderr, NULL);
  20. // setvbuf (stdout, NULL, _IONBF, BUFSIZ);
  21. setvbuf (stderr, NULL, _IONBF, BUFSIZ);
  22. try {
  23. std::cout << "Hello! I'm a basic shell version of .dat file patcher.\n";
  24. DatFile *a = nullptr;
  25. ifstream in("dat_file_path.txt");
  26. if (!in.fail()) {
  27. std::string filename;
  28. getline(in, filename);
  29. try {
  30. std::cout << "Using .dat file from dat_file_path.txt...\n";
  31. std::cout << "Opening file " << filename << std::endl;
  32. a = new DatFile(filename.c_str(), 0);
  33. } catch (DatException &e) {
  34. std::cout << "Dat file path from dat_file_path.txt - " << filename << " may be incorrect (cannot open DatFile there)\n";
  35. }
  36. }
  37. while (a == nullptr) {
  38. std::cout << "Please, tell, where the .dat file is\n";
  39. std::cout << "Enter path to file (including filename): ";
  40. std::string filename;
  41. std::getline(std::cin, filename);
  42. std::cout << "Opening file " << filename << std::endl;
  43. try {
  44. a = new DatFile(filename.c_str(), 0);
  45. } catch (DatException &e) {
  46. if (e.type() == NOFILE_EXCEPTION)
  47. std::cout << "Cannot find file... Could you enter .dat filename once more?" << std::endl;
  48. else
  49. std::cout << "Some error caused while opening the file... "
  50. "Could you enter .dat filename once more?" << std::endl;
  51. delete a;
  52. }
  53. }
  54. std::cout << "Great! File initialised successfully!\n";
  55. std::cout << "Files number: " << a->files_number() << std::endl;
  56. while (true) {
  57. std::cout << "Please, choose, what should I do. I can patch data from database to .dat file (enter 1), "
  58. "change locale (enter 2), print current locale (enter 3) or exit (enter -1)\n";
  59. int cmd = 0;
  60. std::cout << "Enter number of command (1-4): ";
  61. std::cin >> cmd;
  62. std::string tmp;
  63. std::getline(std::cin, tmp);
  64. if (cmd == -1) {
  65. std::cout << "Exiting. Thanks for using me!\n";
  66. break;
  67. }
  68. if (cmd == 1) {
  69. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  70. "directory), of enter 0 to return to main dialogue.\n";
  71. while (true) {
  72. std::cout << "Enter name of file or 0: ";
  73. std::string dbname;
  74. std::getline(std::cin, dbname);
  75. if (dbname == std::to_string(0)) {
  76. std::cout << "Okay, returning back...\n\n";
  77. break;
  78. }
  79. Database db;
  80. try {
  81. std::cout << "Opening database... " << dbname << std::endl;
  82. db.InitDatabase(dbname);
  83. if (db.CountRows() == 0) {
  84. std::cout << "There are no files in database or database doesn't exist. "
  85. "Please, try again!\n";
  86. continue;
  87. }
  88. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  89. } catch (DatException &e) {
  90. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  91. continue;
  92. }
  93. std::cout << "Successfully opened database! Beginning patching...\n";
  94. const clock_t begin_time = clock();
  95. size_t all = db.CountRows();
  96. size_t now = 0;
  97. SubfileData data;
  98. try {
  99. data = db.GetNextFile();
  100. } catch (std::exception &e) {
  101. fprintf(stderr, "Caught %s exception.\n", e.what());
  102. fprintf(stderr, "DatFile::PatchAllDatabase() error! Caught exception while fetching "
  103. "file from database! Stopping...\n");
  104. return false;
  105. }
  106. while (data != SubfileData()) {
  107. try {
  108. a->PatchFile(data);
  109. } catch (std::exception &e) {
  110. fprintf(stderr, "Caught %s exception.\n", e.what());
  111. fprintf(stderr, "DatFile::PatchAllDatabase() error! Caught exception while "
  112. "patching file! Passing...\n");
  113. }
  114. try {
  115. data = db.GetNextFile();
  116. } catch (std::exception &e) {
  117. fprintf(stderr, "Caught %s exception.\n", e.what());
  118. fprintf(stderr,"DatFile::PatchAllDatabase() error! Caught exception while fetching "
  119. "file from database! Stopping...\n");
  120. break;
  121. }
  122. ++now;
  123. if (now * 100 / all > (now - 1) * 100 / all)
  124. std::cout << now * 100 / all << "%\n";
  125. }
  126. a->CommitChanges();
  127. a->CommitLocales();
  128. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  129. float(clock() - begin_time) / CLOCKS_PER_SEC);
  130. std::cout << "Great! File was patched successfully!\n\n";
  131. break;
  132. }
  133. }
  134. if (cmd == 2) {
  135. std::cout << "Old locale is " << (a->current_locale() == PATCHED ? "RU" : "Original") << endl;
  136. std::cout << "Changing locale..." << std::endl;
  137. a->SetLocale(a->current_locale() == PATCHED ? ORIGINAL : PATCHED);
  138. std::cout << "New locale is " << (a->current_locale() == PATCHED ? "RU" : "Original") << endl << endl;
  139. }
  140. if (cmd == 3) {
  141. std::cout << "Current locale is " << (a->current_locale() == PATCHED ? "RU" : "Original") << endl << endl;
  142. }
  143. }
  144. delete a;
  145. } catch (std::exception &e) {
  146. fprintf(stderr, "Caught %s exception.", e.what());
  147. printf("Caught %s exception.", e.what());
  148. fflush(stdout);
  149. printf("Some critical errors occured. Need to stop execution. See information in errors.log file\n");
  150. fprintf(stderr, "Some critical errors occured. Need to stop execution now...\n");
  151. }
  152. system("pause");
  153. return 0;
  154. }