patcher_example.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. void DatStatusChangedHandler(DatStatus::ProgressInfo info) {
  15. if (info.status == DatStatus::DAT_STATUS::E_FREE) {
  16. std::cout << "DatStatus: operation finished" << std::endl;
  17. return;
  18. }
  19. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_CREATING) {
  20. std::cout << "DatStatus: creating backup " << info.percentage << "% "
  21. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  22. return;
  23. }
  24. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_REMOVING) {
  25. std::cout << "DatStatus: removing backup " << info.percentage << "% "
  26. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  27. return;
  28. }
  29. if (info.status == DatStatus::DAT_STATUS::E_BACKUP_RESTORING) {
  30. std::cout << "DatStatus: restoring backup " << info.percentage << "% "
  31. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  32. return;
  33. }
  34. if (info.status == DatStatus::DAT_STATUS::E_COMMITING) {
  35. std::cout << "DatStatus: applying locales " << info.percentage << "% "
  36. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  37. return;
  38. }
  39. if (info.status == DatStatus::DAT_STATUS::E_EXTRACTING) {
  40. std::cout << "DatStatus: extracting data " << info.percentage << "% "
  41. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  42. return;
  43. }
  44. if (info.status == DatStatus::DAT_STATUS::E_GATHERING_INFO) {
  45. std::cout << "DatStatus: gathering info " << info.percentage << "% "
  46. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  47. return;
  48. }
  49. if (info.status == DatStatus::DAT_STATUS::E_INITIALISING) {
  50. std::cout << "DatStatus: initialising " << info.percentage << "% "
  51. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  52. return;
  53. }
  54. if (info.status == DatStatus::DAT_STATUS::E_PATCHING) {
  55. std::cout << "DatStatus: applying patch " << info.percentage << "% "
  56. << "(" << info.finished_parts << "/" << info.total_parts << ")" << std::endl;
  57. return;
  58. }
  59. }
  60. int main() {
  61. std::cout.precision(1);
  62. std::cout << fixed;
  63. std::cout << "Gi1dor's LotRO .dat patcher ver. " << LOTRO_DAT_VERSION << std::endl;
  64. freopen("patcher_errors.log", "w", stderr);
  65. setbuf(stdout, nullptr);
  66. setbuf(stderr, nullptr);
  67. setvbuf (stdout, nullptr, _IONBF, BUFSIZ);
  68. setvbuf (stderr, nullptr, _IONBF, BUFSIZ);
  69. std::cout << "Hello! I'm a basic shell version of .dat file patcher. I can open .dat file directly, "
  70. "if you write path to it (with name of file) in file \"dat_file_path.txt\"\n";
  71. DatFile file;
  72. ifstream in("dat_file_path.txt");
  73. if (!in.fail()) {
  74. std::string filename;
  75. getline(in, filename);
  76. std::cout << "Using .dat file from dat_file_path.txt...\n";
  77. std::cout << "Opening file " << filename << std::endl;
  78. auto operation = file.Initialise(filename, 0);
  79. if (operation.result == ERROR)
  80. std::cout << "Cannot initialise dat file " << filename << " \n";
  81. }
  82. while (!file.Initialized()) {
  83. std::cout << "Please, tell, where the .dat file is\n";
  84. std::cout << "Enter path to file (including filename): ";
  85. std::string filename;
  86. std::getline(std::cin, filename);
  87. std::cout << "Opening file " << filename << std::endl;
  88. auto operation = file.Initialise(filename, 0);
  89. if (operation.result == ERROR)
  90. std::cout << "Cannot initialise dat file " << filename << ", please, try again\n";
  91. }
  92. std::cout << "Great! File initialised successfully!\n";
  93. file.GetStatusModule().AddStatusChangedCallbackFunction(&DatStatusChangedHandler);
  94. while (true) {
  95. std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1), "
  96. "change locale (enter 2), print current locale (enter 3), enable category (enter 4), disable category (enter 5), "
  97. "print disabled categories (enter 6), create backup (enter 7), remove backup (enter 8), "
  98. "restore .dat file from backup (enter 9), check if backup exists (enter 10) or exit (enter -1)\n";
  99. int cmd = 0;
  100. std::cout << "Enter number of command (1-10): ";
  101. std::cin >> cmd;
  102. std::string tmp;
  103. std::getline(std::cin, tmp);
  104. if (cmd == -1) {
  105. std::cout << "Exiting. Thanks for using me!\n";
  106. file.Deinitialize();
  107. break;
  108. }
  109. if (cmd == 1) {
  110. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  111. "directory), of enter 0 to return to main dialogue.\n";
  112. while (true) {
  113. std::cout << "Enter name of file or 0: ";
  114. std::string dbname;
  115. std::getline(std::cin, dbname);
  116. if (dbname == std::to_string(0)) {
  117. std::cout << "Okay, returning back...\n\n";
  118. break;
  119. }
  120. Database db;
  121. std::cout << "Opening database... " << dbname << std::endl;
  122. if (!db.InitDatabase(dbname)) {
  123. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  124. continue;
  125. };
  126. if (db.CountRows() == 0) {
  127. std::cout << "There are no files in database or database doesn't exist. "
  128. "Please, try again!\n";
  129. continue;
  130. }
  131. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  132. std::cout << "Successfully opened database! Beginning patching...\n";
  133. const clock_t begin_time = clock();
  134. file.GetPatcher().PatchAllDatabase(&db);
  135. db.CloseDatabase();
  136. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  137. float(clock() - begin_time) / CLOCKS_PER_SEC);
  138. std::cout << "Great! File was patched successfully!\n\n";
  139. break;
  140. }
  141. }
  142. if (cmd == 2) {
  143. std::cout << "Old locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
  144. std::cout << "Changing locale..." << std::endl;
  145. file.GetLocaleManager().SetLocale(file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? DatLocaleManager::ORIGINAL : DatLocaleManager::PATCHED);
  146. std::cout << "New locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
  147. }
  148. if (cmd == 3) {
  149. std::cout << "Current locale is " << (file.GetLocaleManager().GetCurrentLocale() == DatLocaleManager::PATCHED ? "RU" : "Original") << endl;
  150. }
  151. if (cmd == 4) {
  152. int category_id = 0;
  153. std::cout << "Enter category id: ";
  154. std::cin >> category_id;
  155. file.GetLocaleManager().EnableCategory(category_id);
  156. std::cout << "Category successfully enabled!" << std::endl;
  157. }
  158. if (cmd == 5) {
  159. int category_id = 0;
  160. std::cout << "Enter category id: ";
  161. std::cin >> category_id;
  162. file.GetLocaleManager().DisableCategory(category_id);
  163. std::cout << "Category successfully disabled!" << std::endl;
  164. }
  165. if (cmd == 6) {
  166. std::cout << "Disabled categories: ";
  167. for (auto i : file.GetLocaleManager().GetInactiveCategories())
  168. std::cout << i << " ";
  169. std::cout << endl;
  170. }
  171. if (cmd == 7) {
  172. std::cout << "Creating backup..." << std::endl;
  173. std::cout << "Create backup function returned " << file.GetBackupManager().CreateBackup("cli_local_En.backup").result << std::endl;
  174. }
  175. if (cmd == 8) {
  176. std::cout << "Removing backup..." << std::endl;
  177. std::cout << "Remove backup function returned " << file.GetBackupManager().RemoveBackup("cli_local_En.backup").result << std::endl;
  178. }
  179. if (cmd == 9) {
  180. std::cout << "Restoring file from backup..." << std::endl;
  181. std::cout << "Restore file function returned " << file.GetBackupManager().RestoreFromBackup("cli_local_En.backup").result << std::endl;
  182. }
  183. if (cmd == 10) {
  184. std::cout << "Backup file " << (file.GetBackupManager().CheckIfBackupAvailable("cli_local_En.backup") ? "exists!" : "doesn't exist.") << std::endl;
  185. }
  186. }
  187. //system("pause");
  188. return 0;
  189. }