patcher_example.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. 6.0.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. auto operation = file.Initialise(filename, 0);
  31. if (operation.result == ERROR)
  32. std::cout << "Cannot initialise dat file " << filename << " \n";
  33. }
  34. while (!file.Initialized()) {
  35. std::cout << "Please, tell, where the .dat file is\n";
  36. std::cout << "Enter path to file (including filename): ";
  37. std::string filename;
  38. std::getline(std::cin, filename);
  39. std::cout << "Opening file " << filename << std::endl;
  40. auto operation = file.Initialise(filename, 0);
  41. if (operation.result == ERROR)
  42. std::cout << "Cannot initialise dat file " << filename << ", please, try again\n";
  43. }
  44. std::cout << "Great! File initialised successfully!\n";
  45. while (true) {
  46. std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1), "
  47. "change locale (enter 2), print current locale (enter 3), enable category (enter 4), disable category (enter 5), "
  48. "print disabled categories (enter 6), create backup (enter 7), remove backup (enter 8), "
  49. "restore .dat file from backup (enter 9), check if backup exists (enter 10) or exit (enter -1)\n";
  50. std::cout << "\n=================\nOptions 4,5,6,7,8,9,10 are currently unavailable\n";
  51. int cmd = 0;
  52. std::cout << "Enter number of command (1-10): ";
  53. std::cin >> cmd;
  54. std::string tmp;
  55. std::getline(std::cin, tmp);
  56. if (cmd == -1) {
  57. std::cout << "Exiting. Thanks for using me!\n";
  58. file.Deinitialize();
  59. break;
  60. }
  61. if (cmd == 1) {
  62. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  63. "directory), of enter 0 to return to main dialogue.\n";
  64. while (true) {
  65. std::cout << "Enter name of file or 0: ";
  66. std::string dbname;
  67. std::getline(std::cin, dbname);
  68. if (dbname == std::to_string(0)) {
  69. std::cout << "Okay, returning back...\n\n";
  70. break;
  71. }
  72. Database db;
  73. std::cout << "Opening database... " << dbname << std::endl;
  74. if (!db.InitDatabase(dbname)) {
  75. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  76. continue;
  77. };
  78. if (db.CountRows() == 0) {
  79. std::cout << "There are no files in database or database doesn't exist. "
  80. "Please, try again!\n";
  81. continue;
  82. }
  83. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  84. std::cout << "Successfully opened database! Beginning patching...\n";
  85. const clock_t begin_time = clock();
  86. size_t all = db.CountRows();
  87. size_t now = 0;
  88. SubfileData subfile = db.GetNextFile();
  89. while (!subfile.Empty()) {
  90. if (file.getPatcher().PatchFile(subfile).result == ERROR) {
  91. fprintf(stderr, "Error! Caught exception while patching file! Passing it\n");
  92. }
  93. subfile = db.GetNextFile();
  94. ++now;
  95. if (now * 100 / all > (now - 1) * 100 / all)
  96. std::cout << now * 100 / all << "%\n";
  97. }
  98. db.CloseDatabase();
  99. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  100. float(clock() - begin_time) / CLOCKS_PER_SEC);
  101. std::cout << "Great! File was patched successfully!\n\n";
  102. break;
  103. }
  104. }
  105. if (cmd == 2) {
  106. std::cout << "Old locale is " << (file.getLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
  107. std::cout << "Changing locale..." << std::endl;
  108. file.getLocaleManager().SetLocale(file.getLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? DatLocaleManager::ORIGINAL : DatLocaleManager::PATCHED);
  109. std::cout << "New locale is " << (file.getLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
  110. }
  111. if (cmd == 3) {
  112. std::cout << "Current locale is " << (file.getLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
  113. }
  114. //
  115. // if (cmd == 4) {
  116. // int category_id = 0;
  117. // std::cout << "Enter category id: ";
  118. // std::cin >> category_id;
  119. // file.EnableCategory(category_id);
  120. // std::cout << "Category successfully enabled!" << std::endl;
  121. // }
  122. //
  123. // if (cmd == 5) {
  124. // int category_id = 0;
  125. // std::cout << "Enter category id: ";
  126. // std::cin >> category_id;
  127. // file.DisableCategory(category_id);
  128. // std::cout << "Category successfully disabled!" << std::endl;
  129. // }
  130. //
  131. // if (cmd == 6) {
  132. // std::cout << "Disabled categories: ";
  133. // for (auto i : file.GetInactiveCategoriesList())
  134. // std::cout << i << " ";
  135. // std::cout << endl;
  136. // }
  137. //
  138. // if (cmd == 7) {
  139. // std::cout << "Creating backup..." << std::endl;
  140. // std::cout << "Create backup function returned " << file.CreateBackup("cli_local_En.backup") << std::endl;
  141. // }
  142. //
  143. // if (cmd == 8) {
  144. // std::cout << "Removing backup..." << std::endl;
  145. // std::cout << "Remove backup function returned " << file.RemoveBackup("cli_local_En.backup") << std::endl;
  146. // }
  147. //
  148. // if (cmd == 9) {
  149. // std::cout << "Restoring file from backup..." << std::endl;
  150. // std::cout << "Restore file function returned " << file.RestoreFromBackup("cli_local_En.backup") << std::endl;
  151. // }
  152. //
  153. // if (cmd == 10) {
  154. // std::cout << "Backup file " << (file.CheckIfBackupAvailable("clie_local_En.backup") ? "exists!" : "doesn't exist.") << std::endl;
  155. // }
  156. }
  157. //system("pause");
  158. return 0;
  159. }