patcher_example.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "LotroDat.h"
  13. using namespace LOTRO_DAT;
  14. using namespace std;
  15. int main() {
  16. std::cout << "Gi1dor's LotRO .dat patcher ver. 2.1.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. std::cout << "Hello! I'm a basic shell version of .dat file patcher.\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. if (file.InitDatFile(filename, 0) == false) {
  31. std::cout << "Dat file path from dat_file_path.txt - " << filename << " may be incorrect (cannot open "
  32. "DatFile due to error. See it in errors.log)\n";
  33. file.CloseDatFile();
  34. }
  35. }
  36. while (file.DatFileState() == CLOSED) {
  37. std::cout << "Please, tell, where the .dat file is\n";
  38. std::cout << "Enter path to file (including filename): ";
  39. std::string filename;
  40. std::getline(std::cin, filename);
  41. std::cout << "Opening file " << filename << std::endl;
  42. if (file.InitDatFile(filename, 0) == false) {
  43. std::cout << "Some error caused while opening the file... "
  44. "Could you enter .dat filename once more?" << std::endl;
  45. file.CloseDatFile();
  46. }
  47. }
  48. std::cout << "Great! File initialised successfully!\n";
  49. std::cout << "Files number: " << file.files_number() << std::endl;
  50. while (true) {
  51. std::cout << "Please, choose, what should I do. I can patch datfile from database to .dat file (enter 1), "
  52. "change locale (enter 2), print current locale (enter 3) or exit (enter -1)\n";
  53. int cmd = 0;
  54. std::cout << "Enter number of command (1-4): ";
  55. std::cin >> cmd;
  56. std::string tmp;
  57. std::getline(std::cin, tmp);
  58. if (cmd == -1) {
  59. std::cout << "Exiting. Thanks for using me!\n";
  60. break;
  61. }
  62. if (cmd == 1) {
  63. std::cout << "You've chosen to patch database! Write name of database file (it should be in the same "
  64. "directory), of enter 0 to return to main dialogue.\n";
  65. while (true) {
  66. std::cout << "Enter name of file or 0: ";
  67. std::string dbname;
  68. std::getline(std::cin, dbname);
  69. if (dbname == std::to_string(0)) {
  70. std::cout << "Okay, returning back...\n\n";
  71. break;
  72. }
  73. Database db;
  74. std::cout << "Opening database... " << dbname << std::endl;
  75. if (!db.InitDatabase(dbname)) {
  76. std::cout << "Unfortunately, I cannot open this database. Could you try again please?\n";
  77. continue;
  78. };
  79. if (db.CountRows() == 0) {
  80. std::cout << "There are no files in database or database doesn't exist. "
  81. "Please, try again!\n";
  82. continue;
  83. }
  84. std::cout << "There are " << db.CountRows() << " files in database." << std::endl;
  85. std::cout << "Successfully opened database! Beginning patching...\n";
  86. const clock_t begin_time = clock();
  87. size_t all = db.CountRows();
  88. size_t now = 0;
  89. SubfileData subfile = db.GetNextFile();
  90. while (!subfile.Empty()) {
  91. if (!file.PatchFile(subfile)) {
  92. fprintf(stderr, "Error! Caught exception while patching file! Passing it\n");
  93. }
  94. subfile = db.GetNextFile();
  95. ++now;
  96. if (now * 100 / all > (now - 1) * 100 / all)
  97. std::cout << now * 100 / all << "%\n";
  98. }
  99. db.CloseDatabase();
  100. file.CommitChanges();
  101. fprintf(stdout, "Spent %f seconds on patching! Thank you for your patience!\n",
  102. float(clock() - begin_time) / CLOCKS_PER_SEC);
  103. std::cout << "Great! File was patched successfully!\n\n";
  104. break;
  105. }
  106. }
  107. if (cmd == 2) {
  108. std::cout << "Old locale is " << (file.current_locale() == PATCHED ? "RU" : "Original") << endl;
  109. std::cout << "Changing locale..." << std::endl;
  110. file.SetLocale(file.current_locale() == PATCHED ? ORIGINAL : PATCHED);
  111. std::cout << "New locale is " << (file.current_locale() == PATCHED ? "RU" : "Original") << endl << endl;
  112. }
  113. if (cmd == 3) {
  114. std::cout << "Current locale is " << (file.current_locale() == PATCHED ? "RU" : "Original") << endl << endl;
  115. }
  116. }
  117. file.CloseDatFile();
  118. system("pause");
  119. return 0;
  120. }