patcher_example.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // Created by Иван_Архипов on 23.11.2017.
  3. //
  4. #include <iostream>
  5. #include <ctime>
  6. #include <algorithm>
  7. #ifdef WIN32
  8. #include <direct.h>
  9. #define mkdir(dir, mode) _mkdir(dir)
  10. #endif
  11. #include "LotroDat.h"
  12. using namespace LOTRO_DAT;
  13. using namespace std;
  14. int main() {
  15. std::cout << "Gi1dor's LotRO .dat patcher ver. 5.1.0" << std::endl;
  16. freopen("patcher_errors.log", "w", stderr);
  17. setbuf(stdout, nullptr);
  18. setbuf(stderr, nullptr);
  19. setvbuf (stdout, nullptr, _IONBF, BUFSIZ);
  20. setvbuf (stderr, nullptr, _IONBF, BUFSIZ);
  21. std::cout << "Hello! I'm a basic shell version of .dat file patcher. I can open .dat file directly, "
  22. "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
  23. DatFile file;
  24. ifstream in("dat_file_path.txt");
  25. if (!in.fail()) {
  26. std::string filename;
  27. getline(in, filename);
  28. std::cout << "Using .dat file from dat_file_path.txt...\n";
  29. std::cout << "Opening file " << filename << std::endl;
  30. DAT_RESULT result = file.InitDatFile(filename, 0);
  31. if (result >= 2) {
  32. std::cout << "Dat file may be corrupted! It's opened, but all functions MAY WORK INCORRECTLY! It's better to DOWNLOAD CLEAR .DAT FILE\n";
  33. }
  34. if (result <= 0) {
  35. std::cout << "Dat file path from dat_file_path.txt - " << filename << " may be incorrect (cannot open "
  36. "DatFile due to error. See it in errors.log)\n";
  37. file.CloseDatFile();
  38. }
  39. }
  40. while (file.DatFileState() == CLOSED) {
  41. std::cout << "Please, tell, where the .dat file is\n";
  42. std::cout << "Enter path to file (including filename): ";
  43. std::string filename;
  44. std::getline(std::cin, filename);
  45. std::cout << "Opening file " << filename << std::endl;
  46. if (file.InitDatFile(filename, 0) == false) {
  47. std::cout << "Some error caused while opening the file... "
  48. "Could you enter .dat filename once more?" << std::endl;
  49. file.CloseDatFile();
  50. }
  51. }
  52. std::cout << "Great! File initialised successfully!\n";
  53. if (file.CheckIfNotPatched())
  54. std::cout << "MESSAGE: Dat file is new and haven't been patched yet\n";
  55. if (file.CheckIfPatchedByOldLauncher())
  56. std::cout << "MESSAGE: Dat file was patched by old launcher. Capability isn't guaranteed! Some functions may not work properly!!!\n";
  57. if (file.CheckIfUpdatedByGame())
  58. std::cout << "MESSAGE: .dat file was updated by game! Need to repair patches with functions RepairPatches() and FinishRepairingPatches()\n";
  59. std::cout << "Files number: " << file.files_number() << std::endl;
  60. while (true) {
  61. std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1), "
  62. "change locale (enter 2), print current locale (enter 3), enable category (enter 4), disable category (enter 5), "
  63. "print disabled categories (enter 6) or exit (enter -1)\n";
  64. int cmd = 0;
  65. std::cout << "Enter number of command (1-6): ";
  66. std::cin >> cmd;
  67. std::string tmp;
  68. std::getline(std::cin, tmp);
  69. if (cmd == -1) {
  70. std::cout << "Exiting. Thanks for using me!\n";
  71. break;
  72. }
  73. if (cmd == 1) {
  74. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  75. "directory), of enter 0 to return to main dialogue.\n";
  76. while (true) {
  77. std::cout << "Enter name of file or 0: ";
  78. std::string dbname;
  79. std::getline(std::cin, dbname);
  80. if (dbname == std::to_string(0)) {
  81. std::cout << "Okay, returning back...\n\n";
  82. break;
  83. }
  84. Database db;
  85. std::cout << "Opening database... " << dbname << std::endl;
  86. if (!db.InitDatabase(dbname)) {
  87. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  88. continue;
  89. };
  90. if (db.CountRows() == 0) {
  91. std::cout << "There are no files in database or database doesn't exist. "
  92. "Please, try again!\n";
  93. continue;
  94. }
  95. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  96. std::cout << "Successfully opened database! Beginning patching...\n";
  97. const clock_t begin_time = clock();
  98. size_t all = db.CountRows();
  99. size_t now = 0;
  100. SubfileData subfile = db.GetNextFile();
  101. while (!subfile.Empty()) {
  102. if (!file.PatchFile(subfile)) {
  103. fprintf(stderr, "Error! Caught exception while patching file! Passing it\n");
  104. }
  105. subfile = db.GetNextFile();
  106. ++now;
  107. if (now * 100 / all > (now - 1) * 100 / all)
  108. std::cout << now * 100 / all << "%\n";
  109. }
  110. db.CloseDatabase();
  111. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  112. float(clock() - begin_time) / CLOCKS_PER_SEC);
  113. std::cout << "Great! File was patched successfully!\n\n";
  114. break;
  115. }
  116. }
  117. if (cmd == 2) {
  118. std::cout << "Old locale is " << (file.current_locale() == PATCHED ? "RU" : "Original") << endl;
  119. std::cout << "Changing locale..." << std::endl;
  120. file.SetLocale(file.current_locale() == PATCHED ? ORIGINAL : PATCHED);
  121. std::cout << "New locale is " << (file.current_locale() == PATCHED ? "RU" : "Original") << endl << endl;
  122. }
  123. if (cmd == 3) {
  124. std::cout << "Current locale is " << (file.current_locale() == PATCHED ? "RU" : "Original") << endl << endl;
  125. }
  126. if (cmd == 4) {
  127. int category_id = 0;
  128. std::cout << "Enter category id: ";
  129. std::cin >> category_id;
  130. file.EnableCategory(category_id);
  131. std::cout << "Category successfully enabled!" << std::endl;
  132. }
  133. if (cmd == 5) {
  134. int category_id = 0;
  135. std::cout << "Enter category id: ";
  136. std::cin >> category_id;
  137. file.DisableCategory(category_id);
  138. std::cout << "Category successfully disabled!" << std::endl;
  139. }
  140. if (cmd == 6) {
  141. std::cout << "Disabled categories: ";
  142. for (auto i : file.GetInactiveCategoriesList())
  143. std::cout << i << " ";
  144. std::cout << endl;
  145. }
  146. }
  147. file.CloseDatFile();
  148. system("pause");
  149. return 0;
  150. }